xref: /vim-8.2.3635/src/testdir/test_expand.vim (revision fcfe1a9b)
1" Test for expanding file names
2
3func Test_with_directories()
4  call mkdir('Xdir1')
5  call mkdir('Xdir2')
6  call mkdir('Xdir3')
7  cd Xdir3
8  call mkdir('Xdir4')
9  cd ..
10
11  split Xdir1/file
12  call setline(1, ['a', 'b'])
13  w
14  w Xdir3/Xdir4/file
15  close
16
17  next Xdir?/*/file
18  call assert_equal('Xdir3/Xdir4/file', expand('%'))
19  if has('unix')
20    next! Xdir?/*/nofile
21    call assert_equal('Xdir?/*/nofile', expand('%'))
22  endif
23  " Edit another file, on MS-Windows the swap file would be in use and can't
24  " be deleted.
25  edit foo
26
27  call assert_equal(0, delete('Xdir1', 'rf'))
28  call assert_equal(0, delete('Xdir2', 'rf'))
29  call assert_equal(0, delete('Xdir3', 'rf'))
30endfunc
31
32func Test_with_tilde()
33  let dir = getcwd()
34  call mkdir('Xdir ~ dir')
35  call assert_true(isdirectory('Xdir ~ dir'))
36  cd Xdir\ ~\ dir
37  call assert_true(getcwd() =~ 'Xdir \~ dir')
38  exe 'cd ' . fnameescape(dir)
39  call delete('Xdir ~ dir', 'd')
40  call assert_false(isdirectory('Xdir ~ dir'))
41endfunc
42
43func Test_expand_tilde_filename()
44  split ~
45  call assert_equal('~', expand('%'))
46  call assert_notequal(expand('%:p'), expand('~/'))
47  call assert_match('\~', expand('%:p'))
48  bwipe!
49endfunc
50
51func Test_expandcmd()
52  let $FOO = 'Test'
53  call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
54  unlet $FOO
55
56  new
57  edit Xfile1
58  call assert_equal('e Xfile1', expandcmd('e %'))
59  edit Xfile2
60  edit Xfile1
61  call assert_equal('e Xfile2', expandcmd('e #'))
62  edit Xfile2
63  edit Xfile3
64  edit Xfile4
65  let bnum = bufnr('Xfile2')
66  call assert_equal('e Xfile2', expandcmd('e #' . bnum))
67  call setline('.', 'Vim!@#')
68  call assert_equal('e Vim', expandcmd('e <cword>'))
69  call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
70  enew!
71  edit Xfile.java
72  call assert_equal('e Xfile.py', expandcmd('e %:r.py'))
73  call assert_equal('make abc.java', expandcmd('make abc.%:e'))
74  call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?'))
75  edit a1a2a3.rb
76  call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
77
78  call assert_fails('call expandcmd("make <afile>")', 'E495:')
79  call assert_fails('call expandcmd("make <afile>")', 'E495:')
80  enew
81  call assert_fails('call expandcmd("make %")', 'E499:')
82  close
83endfunc
84