blob: 5a51903712a3b6dbda15f9c2c65c544429b2e61a [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
4
Bram Moolenaar58adb142016-01-16 21:50:51 +01005func 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('%'))
Bram Moolenaarf60b7962016-01-16 22:47:23 +010021 if has('unix')
22 next! Xdir?/*/nofile
23 call assert_equal('Xdir?/*/nofile', expand('%'))
24 endif
Bram Moolenaar08b270a2016-01-17 18:34:19 +010025 " Edit another file, on MS-Windows the swap file would be in use and can't
26 " be deleted.
27 edit foo
Bram Moolenaar58adb142016-01-16 21:50:51 +010028
Bram Moolenaar08b270a2016-01-17 18:34:19 +010029 call assert_equal(0, delete('Xdir1', 'rf'))
30 call assert_equal(0, delete('Xdir2', 'rf'))
31 call assert_equal(0, delete('Xdir3', 'rf'))
Bram Moolenaar58adb142016-01-16 21:50:51 +010032endfunc
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')
Bram Moolenaar3503d7c2019-11-09 20:10:17 +010040 call chdir(dir)
Bram Moolenaar58adb142016-01-16 21:50:51 +010041 call delete('Xdir ~ dir', 'd')
42 call assert_false(isdirectory('Xdir ~ dir'))
43endfunc
Bram Moolenaar00136dc2018-07-25 21:19:13 +020044
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
Bram Moolenaar80dad482019-06-09 17:22:31 +020052
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
Bram Moolenaara4208962019-08-24 20:50:19 +020063 call assert_equal('e Xfile2', 'e #'->expandcmd())
Bram Moolenaar80dad482019-06-09 17:22:31 +020064 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 close
85endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010086
87" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script
88func Test_source_sfile()
89 let lines =<< trim [SCRIPT]
90 :call assert_fails('echo expandcmd("<sfile>")', 'E498:')
91 :call assert_fails('echo expandcmd("<slnum>")', 'E842:')
92 :call assert_fails('echo expandcmd("<sflnum>")', 'E961:')
93 :call assert_fails('call expandcmd("edit <cfile>")', 'E446:')
94 :call assert_fails('call expandcmd("edit #")', 'E194:')
95 :call assert_fails('call expandcmd("edit #<2")', 'E684:')
96 :call assert_fails('call expandcmd("edit <cword>")', 'E348:')
97 :call assert_fails('call expandcmd("edit <cexpr>")', 'E348:')
98 :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:')
99 :call writefile(v:errors, 'Xresult')
100 :qall!
101
102 [SCRIPT]
103 call writefile(lines, 'Xscript')
104 if RunVim([], [], '--clean -s Xscript')
105 call assert_equal([], readfile('Xresult'))
106 endif
107 call delete('Xscript')
108 call delete('Xresult')
109endfunc
110
111
112" vim: shiftwidth=2 sts=2 expandtab