xref: /vim-8.2.3635/src/testdir/test_expand.vim (revision 8b8d829f)
1" Test for expanding file names
2
3source shared.vim
4source check.vim
5
6func 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('%'))
22  if has('unix')
23    next! Xdir?/*/nofile
24    call assert_equal('Xdir?/*/nofile', expand('%'))
25  endif
26  " Edit another file, on MS-Windows the swap file would be in use and can't
27  " be deleted.
28  edit foo
29
30  call assert_equal(0, delete('Xdir1', 'rf'))
31  call assert_equal(0, delete('Xdir2', 'rf'))
32  call assert_equal(0, delete('Xdir3', 'rf'))
33endfunc
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')
41  call chdir(dir)
42  call delete('Xdir ~ dir', 'd')
43  call assert_false(isdirectory('Xdir ~ dir'))
44endfunc
45
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
53
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
64  call assert_equal('e Xfile2', 'e #'->expandcmd())
65  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
81  call assert_fails('call expandcmd("make <afile>")', 'E495:')
82  call assert_fails('call expandcmd("make <afile>")', 'E495:')
83  enew
84  call assert_fails('call expandcmd("make %")', 'E499:')
85  let $FOO="blue\tsky"
86  call setline(1, "$FOO")
87  call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
88
89  " Test for expression expansion `=
90  let $FOO= "blue"
91  call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
92
93  " Test for env variable with spaces
94  let $FOO= "foo bar baz"
95  call assert_equal("e foo bar baz", expandcmd("e $FOO"))
96
97  unlet $FOO
98  close!
99endfunc
100
101" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
102func Test_source_sfile()
103  let lines =<< trim [SCRIPT]
104    :call assert_fails('echo expandcmd("<sfile>")', 'E498:')
105    :call assert_fails('echo expandcmd("<slnum>")', 'E842:')
106    :call assert_fails('echo expandcmd("<sflnum>")', 'E961:')
107    :call assert_fails('call expandcmd("edit <cfile>")', 'E446:')
108    :call assert_fails('call expandcmd("edit #")', 'E194:')
109    :call assert_fails('call expandcmd("edit #<2")', 'E684:')
110    :call assert_fails('call expandcmd("edit <cword>")', 'E348:')
111    :call assert_fails('call expandcmd("edit <cexpr>")', 'E348:')
112    :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
113    :call writefile(v:errors, 'Xresult')
114    :qall!
115
116  [SCRIPT]
117  call writefile(lines, 'Xscript')
118  if RunVim([], [], '--clean -s Xscript')
119    call assert_equal([], readfile('Xresult'))
120  endif
121  call delete('Xscript')
122  call delete('Xresult')
123endfunc
124
125" Test for expanding filenames multiple times in a command line
126func Test_expand_filename_multicmd()
127  edit foo
128  call setline(1, 'foo!')
129  new
130  call setline(1, 'foo!')
131  new <cword> | new <cWORD>
132  call assert_equal(4, winnr('$'))
133  call assert_equal('foo!', bufname(winbufnr(1)))
134  call assert_equal('foo', bufname(winbufnr(2)))
135  call assert_fails('e %:s/.*//', 'E500:')
136  %bwipe!
137endfunc
138
139func Test_expandcmd_shell_nonomatch()
140  CheckNotMSWindows
141  call assert_equal('$*', expandcmd('$*'))
142endfunc
143
144" vim: shiftwidth=2 sts=2 expandtab
145