xref: /vim-8.2.3635/src/testdir/test_expand.vim (revision bc93cebb)
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  unlet $FOO
88  close!
89endfunc
90
91" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
92func Test_source_sfile()
93  let lines =<< trim [SCRIPT]
94    :call assert_fails('echo expandcmd("<sfile>")', 'E498:')
95    :call assert_fails('echo expandcmd("<slnum>")', 'E842:')
96    :call assert_fails('echo expandcmd("<sflnum>")', 'E961:')
97    :call assert_fails('call expandcmd("edit <cfile>")', 'E446:')
98    :call assert_fails('call expandcmd("edit #")', 'E194:')
99    :call assert_fails('call expandcmd("edit #<2")', 'E684:')
100    :call assert_fails('call expandcmd("edit <cword>")', 'E348:')
101    :call assert_fails('call expandcmd("edit <cexpr>")', 'E348:')
102    :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
103    :call writefile(v:errors, 'Xresult')
104    :qall!
105
106  [SCRIPT]
107  call writefile(lines, 'Xscript')
108  if RunVim([], [], '--clean -s Xscript')
109    call assert_equal([], readfile('Xresult'))
110  endif
111  call delete('Xscript')
112  call delete('Xresult')
113endfunc
114
115" Test for expanding filenames multiple times in a command line
116func Test_expand_filename_multicmd()
117  edit foo
118  call setline(1, 'foo!')
119  new
120  call setline(1, 'foo!')
121  new <cword> | new <cWORD>
122  call assert_equal(4, winnr('$'))
123  call assert_equal('foo!', bufname(winbufnr(1)))
124  call assert_equal('foo', bufname(winbufnr(2)))
125  %bwipe!
126endfunc
127
128" vim: shiftwidth=2 sts=2 expandtab
129