blob: d86fea4f459daa88ebb0fb4e1f248e33bcbd6201 [file] [log] [blame]
Bram Moolenaar58adb142016-01-16 21:50:51 +01001" Test for expanding file names
2
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003source shared.vim
4
Bram Moolenaar58adb142016-01-16 21:50:51 +01005func Test_with_directories()
6 call mkdir('Xdir1')
7 call mkdir('Xdir2')
8 call mkdir('Xdir3')
9 cd Xdir3
10 call mkdir('Xdir4')
11 cd ..
12
13 split Xdir1/file
14 call setline(1, ['a', 'b'])
15 w
16 w Xdir3/Xdir4/file
17 close
18
19 next Xdir?/*/file
20 call assert_equal('Xdir3/Xdir4/file', expand('%'))
Bram Moolenaarf60b7962016-01-16 22:47:23 +010021 if has('unix')
22 next! Xdir?/*/nofile
23 call assert_equal('Xdir?/*/nofile', expand('%'))
24 endif
Bram Moolenaar08b270a2016-01-17 18:34:19 +010025 " Edit another file, on MS-Windows the swap file would be in use and can't
26 " be deleted.
27 edit foo
Bram Moolenaar58adb142016-01-16 21:50:51 +010028
Bram Moolenaar08b270a2016-01-17 18:34:19 +010029 call assert_equal(0, delete('Xdir1', 'rf'))
30 call assert_equal(0, delete('Xdir2', 'rf'))
31 call assert_equal(0, delete('Xdir3', 'rf'))
Bram Moolenaar58adb142016-01-16 21:50:51 +010032endfunc
33
34func Test_with_tilde()
35 let dir = getcwd()
36 call mkdir('Xdir ~ dir')
37 call assert_true(isdirectory('Xdir ~ dir'))
38 cd Xdir\ ~\ dir
39 call assert_true(getcwd() =~ 'Xdir \~ dir')
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010040 call chdir(dir)
Bram Moolenaar58adb142016-01-16 21:50:51 +010041 call delete('Xdir ~ dir', 'd')
42 call assert_false(isdirectory('Xdir ~ dir'))
43endfunc
Bram Moolenaar00136dc2018-07-25 21:19:13 +020044
45func Test_expand_tilde_filename()
46 split ~
47 call assert_equal('~', expand('%'))
48 call assert_notequal(expand('%:p'), expand('~/'))
49 call assert_match('\~', expand('%:p'))
50 bwipe!
51endfunc
Bram Moolenaar80dad482019-06-09 17:22:31 +020052
53func Test_expandcmd()
54 let $FOO = 'Test'
55 call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
56 unlet $FOO
57
58 new
59 edit Xfile1
60 call assert_equal('e Xfile1', expandcmd('e %'))
61 edit Xfile2
62 edit Xfile1
Bram Moolenaara4208962019-08-24 20:50:19 +020063 call assert_equal('e Xfile2', 'e #'->expandcmd())
Bram Moolenaar80dad482019-06-09 17:22:31 +020064 edit Xfile2
65 edit Xfile3
66 edit Xfile4
67 let bnum = bufnr('Xfile2')
68 call assert_equal('e Xfile2', expandcmd('e #' . bnum))
69 call setline('.', 'Vim!@#')
70 call assert_equal('e Vim', expandcmd('e <cword>'))
71 call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
72 enew!
73 edit Xfile.java
74 call assert_equal('e Xfile.py', expandcmd('e %:r.py'))
75 call assert_equal('make abc.java', expandcmd('make abc.%:e'))
76 call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?'))
77 edit a1a2a3.rb
78 call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
79
80 call assert_fails('call expandcmd("make <afile>")', 'E495:')
81 call assert_fails('call expandcmd("make <afile>")', 'E495:')
82 enew
83 call assert_fails('call expandcmd("make %")', 'E499:')
Bram Moolenaar818fc9a2020-02-21 17:54:45 +010084 let $FOO="blue\tsky"
85 call setline(1, "$FOO")
86 call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
87 unlet $FOO
88 close!
Bram Moolenaar80dad482019-06-09 17:22:31 +020089endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010090
91" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
92func Test_source_sfile()
93 let lines =<< trim [SCRIPT]
94 :call assert_fails('echo expandcmd("<sfile>")', 'E498:')
95 :call assert_fails('echo expandcmd("<slnum>")', 'E842:')
96 :call assert_fails('echo expandcmd("<sflnum>")', 'E961:')
97 :call assert_fails('call expandcmd("edit <cfile>")', 'E446:')
98 :call assert_fails('call expandcmd("edit #")', 'E194:')
99 :call assert_fails('call expandcmd("edit #<2")', 'E684:')
100 :call assert_fails('call expandcmd("edit <cword>")', 'E348:')
101 :call assert_fails('call expandcmd("edit <cexpr>")', 'E348:')
102 :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
103 :call writefile(v:errors, 'Xresult')
104 :qall!
105
106 [SCRIPT]
107 call writefile(lines, 'Xscript')
108 if RunVim([], [], '--clean -s Xscript')
109 call assert_equal([], readfile('Xresult'))
110 endif
111 call delete('Xscript')
112 call delete('Xresult')
113endfunc
114
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100115" Test for expanding filenames multiple times in a command line
116func Test_expand_filename_multicmd()
117 edit foo
118 call setline(1, 'foo!')
119 new
120 call setline(1, 'foo!')
121 new <cword> | new <cWORD>
122 call assert_equal(4, winnr('$'))
123 call assert_equal('foo!', bufname(winbufnr(1)))
124 call assert_equal('foo', bufname(winbufnr(2)))
125 %bwipe!
126endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100127
128" vim: shiftwidth=2 sts=2 expandtab