blob: bebe13868faf32280cb70b446bc40c1dcf54a3b7 [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
Christian Brabandt8b8d8292021-11-19 12:37:36 +00004source check.vim
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01005
Bram Moolenaar58adb142016-01-16 21:50:51 +01006func Test_with_directories()
7 call mkdir('Xdir1')
8 call mkdir('Xdir2')
9 call mkdir('Xdir3')
10 cd Xdir3
11 call mkdir('Xdir4')
12 cd ..
13
14 split Xdir1/file
15 call setline(1, ['a', 'b'])
16 w
17 w Xdir3/Xdir4/file
18 close
19
20 next Xdir?/*/file
21 call assert_equal('Xdir3/Xdir4/file', expand('%'))
Bram Moolenaarf60b7962016-01-16 22:47:23 +010022 if has('unix')
23 next! Xdir?/*/nofile
24 call assert_equal('Xdir?/*/nofile', expand('%'))
25 endif
Bram Moolenaar08b270a2016-01-17 18:34:19 +010026 " Edit another file, on MS-Windows the swap file would be in use and can't
27 " be deleted.
28 edit foo
Bram Moolenaar58adb142016-01-16 21:50:51 +010029
Bram Moolenaar08b270a2016-01-17 18:34:19 +010030 call assert_equal(0, delete('Xdir1', 'rf'))
31 call assert_equal(0, delete('Xdir2', 'rf'))
32 call assert_equal(0, delete('Xdir3', 'rf'))
Bram Moolenaar58adb142016-01-16 21:50:51 +010033endfunc
34
35func Test_with_tilde()
36 let dir = getcwd()
37 call mkdir('Xdir ~ dir')
38 call assert_true(isdirectory('Xdir ~ dir'))
39 cd Xdir\ ~\ dir
40 call assert_true(getcwd() =~ 'Xdir \~ dir')
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010041 call chdir(dir)
Bram Moolenaar58adb142016-01-16 21:50:51 +010042 call delete('Xdir ~ dir', 'd')
43 call assert_false(isdirectory('Xdir ~ dir'))
44endfunc
Bram Moolenaar00136dc2018-07-25 21:19:13 +020045
46func Test_expand_tilde_filename()
47 split ~
Bram Moolenaar94722c52023-01-28 19:19:03 +000048 call assert_equal('~', expand('%'))
Bram Moolenaar00136dc2018-07-25 21:19:13 +020049 call assert_notequal(expand('%:p'), expand('~/'))
Bram Moolenaar94722c52023-01-28 19:19:03 +000050 call assert_match('\~', expand('%:p'))
Bram Moolenaar00136dc2018-07-25 21:19:13 +020051 bwipe!
52endfunc
Bram Moolenaar80dad482019-06-09 17:22:31 +020053
zeertzjq13a01442024-03-09 17:44:46 +010054func Test_expand_env_pathsep()
55 let $FOO = './foo'
56 call assert_equal('./foo/bar', expand('$FOO/bar'))
57 let $FOO = './foo/'
58 call assert_equal('./foo/bar', expand('$FOO/bar'))
59 let $FOO = 'C:'
60 call assert_equal('C:/bar', expand('$FOO/bar'))
61 let $FOO = 'C:/'
62 call assert_equal('C:/bar', expand('$FOO/bar'))
63
64 unlet $FOO
65endfunc
66
Bram Moolenaar80dad482019-06-09 17:22:31 +020067func Test_expandcmd()
68 let $FOO = 'Test'
69 call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
70 unlet $FOO
71
72 new
Bram Moolenaarb18b4962022-09-02 21:55:50 +010073 edit Xpandfile1
74 call assert_equal('e Xpandfile1', expandcmd('e %'))
75 edit Xpandfile2
76 edit Xpandfile1
77 call assert_equal('e Xpandfile2', 'e #'->expandcmd())
78 edit Xpandfile2
79 edit Xpandfile3
80 edit Xpandfile4
81 let bnum = bufnr('Xpandfile2')
82 call assert_equal('e Xpandfile2', expandcmd('e #' . bnum))
Bram Moolenaar80dad482019-06-09 17:22:31 +020083 call setline('.', 'Vim!@#')
84 call assert_equal('e Vim', expandcmd('e <cword>'))
85 call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
86 enew!
Bram Moolenaarb18b4962022-09-02 21:55:50 +010087 edit Xpandfile.java
88 call assert_equal('e Xpandfile.py', expandcmd('e %:r.py'))
Bram Moolenaar80dad482019-06-09 17:22:31 +020089 call assert_equal('make abc.java', expandcmd('make abc.%:e'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +010090 call assert_equal('make Xabc.java', expandcmd('make %:s?pandfile?abc?'))
Bram Moolenaar80dad482019-06-09 17:22:31 +020091 edit a1a2a3.rb
Bram Moolenaarb18b4962022-09-02 21:55:50 +010092 call assert_equal('make b1b2b3.rb a1a2a3 Xpandfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
Bram Moolenaar80dad482019-06-09 17:22:31 +020093
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +010094 call assert_equal('make <afile>', expandcmd("make <afile>"))
95 call assert_equal('make <amatch>', expandcmd("make <amatch>"))
96 call assert_equal('make <abuf>', expandcmd("make <abuf>"))
Bram Moolenaar80dad482019-06-09 17:22:31 +020097 enew
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +010098 call assert_equal('make %', expandcmd("make %"))
Bram Moolenaar818fc9a2020-02-21 17:54:45 +010099 let $FOO="blue\tsky"
100 call setline(1, "$FOO")
101 call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200102
103 " Test for expression expansion `=
104 let $FOO= "blue"
105 call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
Yegappan Lakshmanan2b74b682022-04-03 21:30:32 +0100106 let x = expandcmd("`=axbycz`")
107 call assert_equal('`=axbycz`', x)
108 call assert_fails('let x = expandcmd("`=axbycz`", #{errmsg: 1})', 'E121:')
109 let x = expandcmd("`=axbycz`", #{abc: []})
110 call assert_equal('`=axbycz`', x)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200111
112 " Test for env variable with spaces
113 let $FOO= "foo bar baz"
114 call assert_equal("e foo bar baz", expandcmd("e $FOO"))
115
Yegappan Lakshmanan2b74b682022-04-03 21:30:32 +0100116 if has('unix') && executable('bash')
117 " test for using the shell to expand a command argument.
118 " only bash supports the {..} syntax
119 set shell=bash
120 let x = expandcmd('{1..4}')
121 call assert_equal('{1..4}', x)
122 call assert_fails("let x = expandcmd('{1..4}', #{errmsg: v:true})", 'E77:')
123 let x = expandcmd('{1..4}', #{error: v:true})
124 call assert_equal('{1..4}', x)
125 set shell&
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +0100126 endif
127
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100128 unlet $FOO
129 close!
Bram Moolenaar80dad482019-06-09 17:22:31 +0200130endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100131
132" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
133func Test_source_sfile()
134 let lines =<< trim [SCRIPT]
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +0100135 :call assert_equal('<sfile>', expandcmd("<sfile>"))
136 :call assert_equal('<slnum>', expandcmd("<slnum>"))
137 :call assert_equal('<sflnum>', expandcmd("<sflnum>"))
138 :call assert_equal('edit <cfile>', expandcmd("edit <cfile>"))
139 :call assert_equal('edit #', expandcmd("edit #"))
140 :call assert_equal('edit #<2', expandcmd("edit #<2"))
141 :call assert_equal('edit <cword>', expandcmd("edit <cword>"))
142 :call assert_equal('edit <cexpr>', expandcmd("edit <cexpr>"))
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100143 :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
Bram Moolenaar57544522022-04-12 12:54:11 +0100144 :
145 :call assert_equal('', expand('<script>'))
146 :verbose echo expand('<script>')
147 :call add(v:errors, v:errmsg)
148 :verbose echo expand('<sfile>')
149 :call add(v:errors, v:errmsg)
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100150 :call writefile(v:errors, 'Xresult')
151 :qall!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100152 [SCRIPT]
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100153 call writefile(lines, 'Xscript', 'D')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100154 if RunVim([], [], '--clean -s Xscript')
Bram Moolenaar57544522022-04-12 12:54:11 +0100155 call assert_equal([
156 \ 'E1274: No script file name to substitute for "<script>"',
Bram Moolenaarec892232022-05-06 17:53:06 +0100157 \ 'E498: No :source file name to substitute for "<sfile>"'],
Bram Moolenaar57544522022-04-12 12:54:11 +0100158 \ readfile('Xresult'))
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100159 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100160 call delete('Xresult')
161endfunc
162
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100163" Test for expanding filenames multiple times in a command line
164func Test_expand_filename_multicmd()
165 edit foo
166 call setline(1, 'foo!')
167 new
168 call setline(1, 'foo!')
169 new <cword> | new <cWORD>
170 call assert_equal(4, winnr('$'))
171 call assert_equal('foo!', bufname(winbufnr(1)))
172 call assert_equal('foo', bufname(winbufnr(2)))
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200173 call assert_fails('e %:s/.*//', 'E500:')
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100174 %bwipe!
175endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100176
Christian Brabandt8b8d8292021-11-19 12:37:36 +0000177func Test_expandcmd_shell_nonomatch()
178 CheckNotMSWindows
179 call assert_equal('$*', expandcmd('$*'))
180endfunc
181
LemonBoy6013d002022-04-09 21:42:10 +0100182func Test_expand_script_source()
183 let lines0 =<< trim [SCRIPT]
LemonBoyeca7c602022-04-14 15:39:43 +0100184 call extend(g:script_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100185 so Xscript1
186 func F0()
LemonBoyeca7c602022-04-14 15:39:43 +0100187 call extend(g:func_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100188 endfunc
LemonBoyeca7c602022-04-14 15:39:43 +0100189
190 au User * call extend(g:au_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100191 [SCRIPT]
192
193 let lines1 =<< trim [SCRIPT]
LemonBoyeca7c602022-04-14 15:39:43 +0100194 call extend(g:script_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100195 so Xscript2
196 func F1()
LemonBoyeca7c602022-04-14 15:39:43 +0100197 call extend(g:func_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100198 endfunc
LemonBoyeca7c602022-04-14 15:39:43 +0100199
200 au User * call extend(g:au_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100201 [SCRIPT]
202
203 let lines2 =<< trim [SCRIPT]
LemonBoyeca7c602022-04-14 15:39:43 +0100204 call extend(g:script_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100205 func F2()
LemonBoyeca7c602022-04-14 15:39:43 +0100206 call extend(g:func_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100207 endfunc
LemonBoyeca7c602022-04-14 15:39:43 +0100208
209 au User * call extend(g:au_level, [expand('<script>:t')])
LemonBoy6013d002022-04-09 21:42:10 +0100210 [SCRIPT]
211
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100212 call writefile(lines0, 'Xscript0', 'D')
213 call writefile(lines1, 'Xscript1', 'D')
214 call writefile(lines2, 'Xscript2', 'D')
LemonBoy6013d002022-04-09 21:42:10 +0100215
LemonBoyeca7c602022-04-14 15:39:43 +0100216 " Check the expansion of <script> at different levels.
217 let g:script_level = []
218 let g:func_level = []
219 let g:au_level = []
LemonBoy6013d002022-04-09 21:42:10 +0100220
221 so Xscript0
222 call F0()
223 call F1()
224 call F2()
LemonBoyeca7c602022-04-14 15:39:43 +0100225 doautocmd User
LemonBoy6013d002022-04-09 21:42:10 +0100226
227 call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:script_level)
228 call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:func_level)
LemonBoyeca7c602022-04-14 15:39:43 +0100229 call assert_equal(['Xscript2', 'Xscript1', 'Xscript0'], g:au_level)
LemonBoy6013d002022-04-09 21:42:10 +0100230
231 unlet g:script_level g:func_level
232 delfunc F0
233 delfunc F1
234 delfunc F2
LemonBoy6013d002022-04-09 21:42:10 +0100235endfunc
236
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100237" vim: shiftwidth=2 sts=2 expandtab