xref: /vim-8.2.3635/src/testdir/test_expand.vim (revision 81ea1dfb)
1" Test for expanding file names
2
3source shared.vim
4
5func 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('%'))
21  if has('unix')
22    next! Xdir?/*/nofile
23    call assert_equal('Xdir?/*/nofile', expand('%'))
24  endif
25  " Edit another file, on MS-Windows the swap file would be in use and can't
26  " be deleted.
27  edit foo
28
29  call assert_equal(0, delete('Xdir1', 'rf'))
30  call assert_equal(0, delete('Xdir2', 'rf'))
31  call assert_equal(0, delete('Xdir3', 'rf'))
32endfunc
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')
40  call chdir(dir)
41  call delete('Xdir ~ dir', 'd')
42  call assert_false(isdirectory('Xdir ~ dir'))
43endfunc
44
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
52
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
63  call assert_equal('e Xfile2', 'e #'->expandcmd())
64  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:')
84  let $FOO="blue\tsky"
85  call setline(1, "$FOO")
86  call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
87
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
96  unlet $FOO
97  close!
98endfunc
99
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
124" 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  call assert_fails('e %:s/.*//', 'E500:')
135  %bwipe!
136endfunc
137
138" vim: shiftwidth=2 sts=2 expandtab
139