blob: 383921bb82b52d294b459df08b75a2bae9b138a4 [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>'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020087
88 " Test for expression expansion `=
89 let $FOO= "blue"
90 call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
91
92 " Test for env variable with spaces
93 let $FOO= "foo bar baz"
94 call assert_equal("e foo bar baz", expandcmd("e $FOO"))
95
Bram Moolenaar818fc9a2020-02-21 17:54:45 +010096 unlet $FOO
97 close!
Bram Moolenaar80dad482019-06-09 17:22:31 +020098endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010099
100" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
101func Test_source_sfile()
102 let lines =<< trim [SCRIPT]
103 :call assert_fails('echo expandcmd("<sfile>")', 'E498:')
104 :call assert_fails('echo expandcmd("<slnum>")', 'E842:')
105 :call assert_fails('echo expandcmd("<sflnum>")', 'E961:')
106 :call assert_fails('call expandcmd("edit <cfile>")', 'E446:')
107 :call assert_fails('call expandcmd("edit #")', 'E194:')
108 :call assert_fails('call expandcmd("edit #<2")', 'E684:')
109 :call assert_fails('call expandcmd("edit <cword>")', 'E348:')
110 :call assert_fails('call expandcmd("edit <cexpr>")', 'E348:')
111 :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
112 :call writefile(v:errors, 'Xresult')
113 :qall!
114
115 [SCRIPT]
116 call writefile(lines, 'Xscript')
117 if RunVim([], [], '--clean -s Xscript')
118 call assert_equal([], readfile('Xresult'))
119 endif
120 call delete('Xscript')
121 call delete('Xresult')
122endfunc
123
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100124" Test for expanding filenames multiple times in a command line
125func Test_expand_filename_multicmd()
126 edit foo
127 call setline(1, 'foo!')
128 new
129 call setline(1, 'foo!')
130 new <cword> | new <cWORD>
131 call assert_equal(4, winnr('$'))
132 call assert_equal('foo!', bufname(winbufnr(1)))
133 call assert_equal('foo', bufname(winbufnr(2)))
134 %bwipe!
135endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100136
137" vim: shiftwidth=2 sts=2 expandtab