blob: ce414e4b11bcfd1a1db50018cb692f3949cd8780 [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 ~
48 call assert_equal('~', expand('%'))
49 call assert_notequal(expand('%:p'), expand('~/'))
50 call assert_match('\~', expand('%:p'))
51 bwipe!
52endfunc
Bram Moolenaar80dad482019-06-09 17:22:31 +020053
54func Test_expandcmd()
55 let $FOO = 'Test'
56 call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
57 unlet $FOO
58
59 new
60 edit Xfile1
61 call assert_equal('e Xfile1', expandcmd('e %'))
62 edit Xfile2
63 edit Xfile1
Bram Moolenaara4208962019-08-24 20:50:19 +020064 call assert_equal('e Xfile2', 'e #'->expandcmd())
Bram Moolenaar80dad482019-06-09 17:22:31 +020065 edit Xfile2
66 edit Xfile3
67 edit Xfile4
68 let bnum = bufnr('Xfile2')
69 call assert_equal('e Xfile2', expandcmd('e #' . bnum))
70 call setline('.', 'Vim!@#')
71 call assert_equal('e Vim', expandcmd('e <cword>'))
72 call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
73 enew!
74 edit Xfile.java
75 call assert_equal('e Xfile.py', expandcmd('e %:r.py'))
76 call assert_equal('make abc.java', expandcmd('make abc.%:e'))
77 call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?'))
78 edit a1a2a3.rb
79 call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
80
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +010081 call assert_equal('make <afile>', expandcmd("make <afile>"))
82 call assert_equal('make <amatch>', expandcmd("make <amatch>"))
83 call assert_equal('make <abuf>', expandcmd("make <abuf>"))
Bram Moolenaar80dad482019-06-09 17:22:31 +020084 enew
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +010085 call assert_equal('make %', expandcmd("make %"))
Bram Moolenaar818fc9a2020-02-21 17:54:45 +010086 let $FOO="blue\tsky"
87 call setline(1, "$FOO")
88 call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020089
90 " Test for expression expansion `=
91 let $FOO= "blue"
92 call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
93
94 " Test for env variable with spaces
95 let $FOO= "foo bar baz"
96 call assert_equal("e foo bar baz", expandcmd("e $FOO"))
97
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +010098 if has('unix')
99 " test for using the shell to expand a command argument
100 call assert_equal('{1..4}', expandcmd('{1..4}'))
101 endif
102
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100103 unlet $FOO
104 close!
Bram Moolenaar80dad482019-06-09 17:22:31 +0200105endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100106
107" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
108func Test_source_sfile()
109 let lines =<< trim [SCRIPT]
Yegappan Lakshmanan5018a832022-04-02 21:12:21 +0100110 :call assert_equal('<sfile>', expandcmd("<sfile>"))
111 :call assert_equal('<slnum>', expandcmd("<slnum>"))
112 :call assert_equal('<sflnum>', expandcmd("<sflnum>"))
113 :call assert_equal('edit <cfile>', expandcmd("edit <cfile>"))
114 :call assert_equal('edit #', expandcmd("edit #"))
115 :call assert_equal('edit #<2', expandcmd("edit #<2"))
116 :call assert_equal('edit <cword>', expandcmd("edit <cword>"))
117 :call assert_equal('edit <cexpr>', expandcmd("edit <cexpr>"))
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100118 :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
119 :call writefile(v:errors, 'Xresult')
120 :qall!
121
122 [SCRIPT]
123 call writefile(lines, 'Xscript')
124 if RunVim([], [], '--clean -s Xscript')
125 call assert_equal([], readfile('Xresult'))
126 endif
127 call delete('Xscript')
128 call delete('Xresult')
129endfunc
130
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100131" Test for expanding filenames multiple times in a command line
132func Test_expand_filename_multicmd()
133 edit foo
134 call setline(1, 'foo!')
135 new
136 call setline(1, 'foo!')
137 new <cword> | new <cWORD>
138 call assert_equal(4, winnr('$'))
139 call assert_equal('foo!', bufname(winbufnr(1)))
140 call assert_equal('foo', bufname(winbufnr(2)))
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200141 call assert_fails('e %:s/.*//', 'E500:')
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100142 %bwipe!
143endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100144
Christian Brabandt8b8d8292021-11-19 12:37:36 +0000145func Test_expandcmd_shell_nonomatch()
146 CheckNotMSWindows
147 call assert_equal('$*', expandcmd('$*'))
148endfunc
149
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100150" vim: shiftwidth=2 sts=2 expandtab