blob: 84f1c7d110d0a1f5c34efa645548a2c5ae1f28db [file] [log] [blame]
Bram Moolenaar58adb142016-01-16 21:50:51 +01001" Test for expanding file names
2
3func Test_with_directories()
4 call mkdir('Xdir1')
5 call mkdir('Xdir2')
6 call mkdir('Xdir3')
7 cd Xdir3
8 call mkdir('Xdir4')
9 cd ..
10
11 split Xdir1/file
12 call setline(1, ['a', 'b'])
13 w
14 w Xdir3/Xdir4/file
15 close
16
17 next Xdir?/*/file
18 call assert_equal('Xdir3/Xdir4/file', expand('%'))
Bram Moolenaarf60b7962016-01-16 22:47:23 +010019 if has('unix')
20 next! Xdir?/*/nofile
21 call assert_equal('Xdir?/*/nofile', expand('%'))
22 endif
Bram Moolenaar08b270a2016-01-17 18:34:19 +010023 " Edit another file, on MS-Windows the swap file would be in use and can't
24 " be deleted.
25 edit foo
Bram Moolenaar58adb142016-01-16 21:50:51 +010026
Bram Moolenaar08b270a2016-01-17 18:34:19 +010027 call assert_equal(0, delete('Xdir1', 'rf'))
28 call assert_equal(0, delete('Xdir2', 'rf'))
29 call assert_equal(0, delete('Xdir3', 'rf'))
Bram Moolenaar58adb142016-01-16 21:50:51 +010030endfunc
31
32func Test_with_tilde()
33 let dir = getcwd()
34 call mkdir('Xdir ~ dir')
35 call assert_true(isdirectory('Xdir ~ dir'))
36 cd Xdir\ ~\ dir
37 call assert_true(getcwd() =~ 'Xdir \~ dir')
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010038 call chdir(dir)
Bram Moolenaar58adb142016-01-16 21:50:51 +010039 call delete('Xdir ~ dir', 'd')
40 call assert_false(isdirectory('Xdir ~ dir'))
41endfunc
Bram Moolenaar00136dc2018-07-25 21:19:13 +020042
43func Test_expand_tilde_filename()
44 split ~
Bram Moolenaar94722c52023-01-28 19:19:03 +000045 call assert_equal('~', expand('%'))
Bram Moolenaar00136dc2018-07-25 21:19:13 +020046 call assert_notequal(expand('%:p'), expand('~/'))
Bram Moolenaar94722c52023-01-28 19:19:03 +000047 call assert_match('\~', expand('%:p'))
Bram Moolenaar00136dc2018-07-25 21:19:13 +020048 bwipe!
49endfunc
Bram Moolenaar80dad482019-06-09 17:22:31 +020050
zeertzjq13a01442024-03-09 17:44:46 +010051func Test_expand_env_pathsep()
52 let $FOO = './foo'
53 call assert_equal('./foo/bar', expand('$FOO/bar'))
54 let $FOO = './foo/'
55 call assert_equal('./foo/bar', expand('$FOO/bar'))
56 let $FOO = 'C:'
57 call assert_equal('C:/bar', expand('$FOO/bar'))
58 let $FOO = 'C:/'
59 call assert_equal('C:/bar', expand('$FOO/bar'))
60
61 unlet $FOO
62endfunc
63
Bram Moolenaar80dad482019-06-09 17:22:31 +020064func Test_expandcmd()
65 let $FOO = 'Test'
66 call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
67 unlet $FOO
68
69 new
Bram Moolenaarb18b4962022-09-02 21:55:50 +010070 edit Xpandfile1
71 call assert_equal('e Xpandfile1', expandcmd('e %'))
72 edit Xpandfile2
73 edit Xpandfile1
74 call assert_equal('e Xpandfile2', 'e #'->expandcmd())
75 edit Xpandfile2
76 edit Xpandfile3
77 edit Xpandfile4
78 let bnum = bufnr('Xpandfile2')
79 call assert_equal('e Xpandfile2', expandcmd('e #' . bnum))
Bram Moolenaar80dad482019-06-09 17:22:31 +020080 call setline('.', 'Vim!@#')
81 call assert_equal('e Vim', expandcmd('e <cword>'))
82 call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
83 enew!
Bram Moolenaarb18b4962022-09-02 21:55:50 +010084 edit Xpandfile.java
85 call assert_equal('e Xpandfile.py', expandcmd('e %:r.py'))
Bram Moolenaar80dad482019-06-09 17:22:31 +020086 call assert_equal('make abc.java', expandcmd('make abc.%:e'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +010087 call assert_equal('make Xabc.java', expandcmd('make %:s?pandfile?abc?'))
Bram Moolenaar80dad482019-06-09 17:22:31 +020088 edit a1a2a3.rb
Bram Moolenaarb18b4962022-09-02 21:55:50 +010089 call assert_equal('make b1b2b3.rb a1a2a3 Xpandfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
Bram Moolenaar80dad482019-06-09 17:22:31 +020090
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +010091 call assert_equal('make <afile>', expandcmd("make <afile>"))
92 call assert_equal('make <amatch>', expandcmd("make <amatch>"))
93 call assert_equal('make <abuf>', expandcmd("make <abuf>"))
Bram Moolenaar80dad482019-06-09 17:22:31 +020094 enew
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +010095 call assert_equal('make %', expandcmd("make %"))
Bram Moolenaar818fc9a2020-02-21 17:54:45 +010096 let $FOO="blue\tsky"
97 call setline(1, "$FOO")
98 call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020099
100 " Test for expression expansion `=
101 let $FOO= "blue"
102 call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
Yegappan Lakshmanan2b74b682022-04-03 21:30:32 +0100103 let x = expandcmd("`=axbycz`")
104 call assert_equal('`=axbycz`', x)
105 call assert_fails('let x = expandcmd("`=axbycz`", #{errmsg: 1})', 'E121:')
106 let x = expandcmd("`=axbycz`", #{abc: []})
107 call assert_equal('`=axbycz`', x)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200108
109 " Test for env variable with spaces
110 let $FOO= "foo bar baz"
111 call assert_equal("e foo bar baz", expandcmd("e $FOO"))
112
Yegappan Lakshmanan2b74b682022-04-03 21:30:32 +0100113 if has('unix') && executable('bash')
114 " test for using the shell to expand a command argument.
115 " only bash supports the {..} syntax
116 set shell=bash
117 let x = expandcmd('{1..4}')
118 call assert_equal('{1..4}', x)
119 call assert_fails("let x = expandcmd('{1..4}', #{errmsg: v:true})", 'E77:')
120 let x = expandcmd('{1..4}', #{error: v:true})
121 call assert_equal('{1..4}', x)
122 set shell&
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +0100123 endif
124
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100125 unlet $FOO
126 close!
Bram Moolenaar80dad482019-06-09 17:22:31 +0200127endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100128
129" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
130func Test_source_sfile()
131 let lines =<< trim [SCRIPT]
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +0100132 :call assert_equal('<sfile>', expandcmd("<sfile>"))
133 :call assert_equal('<slnum>', expandcmd("<slnum>"))
134 :call assert_equal('<sflnum>', expandcmd("<sflnum>"))
135 :call assert_equal('edit <cfile>', expandcmd("edit <cfile>"))
136 :call assert_equal('edit #', expandcmd("edit #"))
137 :call assert_equal('edit #<2', expandcmd("edit #<2"))
138 :call assert_equal('edit <cword>', expandcmd("edit <cword>"))
139 :call assert_equal('edit <cexpr>', expandcmd("edit <cexpr>"))
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100140 :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
Bram Moolenaar57544522022-04-12 12:54:11 +0100141 :
142 :call assert_equal('', expand('<script>'))
143 :verbose echo expand('<script>')
144 :call add(v:errors, v:errmsg)
145 :verbose echo expand('<sfile>')
146 :call add(v:errors, v:errmsg)
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100147 :call writefile(v:errors, 'Xresult')
148 :qall!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100149 [SCRIPT]
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100150 call writefile(lines, 'Xscript', 'D')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100151 if RunVim([], [], '--clean -s Xscript')
Bram Moolenaar57544522022-04-12 12:54:11 +0100152 call assert_equal([
153 \ 'E1274: No script file name to substitute for "<script>"',
Bram Moolenaarec892232022-05-06 17:53:06 +0100154 \ 'E498: No :source file name to substitute for "<sfile>"'],
Bram Moolenaar57544522022-04-12 12:54:11 +0100155 \ readfile('Xresult'))
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100156 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100157 call delete('Xresult')
158endfunc
159
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100160" Test for expanding filenames multiple times in a command line
161func Test_expand_filename_multicmd()
162 edit foo
163 call setline(1, 'foo!')
164 new
165 call setline(1, 'foo!')
166 new <cword> | new <cWORD>
167 call assert_equal(4, winnr('$'))
168 call assert_equal('foo!', bufname(winbufnr(1)))
169 call assert_equal('foo', bufname(winbufnr(2)))
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200170 call assert_fails('e %:s/.*//', 'E500:')
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100171 %bwipe!
172endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100173
Christian Brabandt8b8d8292021-11-19 12:37:36 +0000174func Test_expandcmd_shell_nonomatch()
175 CheckNotMSWindows
176 call assert_equal('$*', expandcmd('$*'))
177endfunc
178
LemonBoy6013d002022-04-09 21:42:10 +0100179func Test_expand_script_source()
180 let lines0 =<< trim [SCRIPT]
LemonBoyeca7c602022-04-14 15:39:43 +0100181 call extend(g:script_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100182 so Xscript1
183 func F0()
LemonBoyeca7c602022-04-14 15:39:43 +0100184 call extend(g:func_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100185 endfunc
LemonBoyeca7c602022-04-14 15:39:43 +0100186
187 au User * call extend(g:au_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100188 [SCRIPT]
189
190 let lines1 =<< trim [SCRIPT]
LemonBoyeca7c602022-04-14 15:39:43 +0100191 call extend(g:script_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100192 so Xscript2
193 func F1()
LemonBoyeca7c602022-04-14 15:39:43 +0100194 call extend(g:func_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100195 endfunc
LemonBoyeca7c602022-04-14 15:39:43 +0100196
197 au User * call extend(g:au_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100198 [SCRIPT]
199
200 let lines2 =<< trim [SCRIPT]
LemonBoyeca7c602022-04-14 15:39:43 +0100201 call extend(g:script_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100202 func F2()
LemonBoyeca7c602022-04-14 15:39:43 +0100203 call extend(g:func_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100204 endfunc
LemonBoyeca7c602022-04-14 15:39:43 +0100205
206 au User * call extend(g:au_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100207 [SCRIPT]
208
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100209 call writefile(lines0, 'Xscript0', 'D')
210 call writefile(lines1, 'Xscript1', 'D')
211 call writefile(lines2, 'Xscript2', 'D')
LemonBoy6013d002022-04-09 21:42:10 +0100212
LemonBoyeca7c602022-04-14 15:39:43 +0100213 " Check the expansion of <script> at different levels.
214 let g:script_level = []
215 let g:func_level = []
216 let g:au_level = []
LemonBoy6013d002022-04-09 21:42:10 +0100217
218 so Xscript0
219 call F0()
220 call F1()
221 call F2()
LemonBoyeca7c602022-04-14 15:39:43 +0100222 doautocmd User
LemonBoy6013d002022-04-09 21:42:10 +0100223
224 call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:script_level)
225 call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:func_level)
LemonBoyeca7c602022-04-14 15:39:43 +0100226 call assert_equal(['Xscript2', 'Xscript1', 'Xscript0'], g:au_level)
LemonBoy6013d002022-04-09 21:42:10 +0100227
228 unlet g:script_level g:func_level
229 delfunc F0
230 delfunc F1
231 delfunc F2
LemonBoy6013d002022-04-09 21:42:10 +0100232endfunc
233
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100234" vim: shiftwidth=2 sts=2 expandtab