Bram Moolenaar | 58adb14 | 2016-01-16 21:50:51 +0100 | [diff] [blame] | 1 | " Test for expanding file names |
| 2 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 3 | source shared.vim |
Christian Brabandt | 8b8d829 | 2021-11-19 12:37:36 +0000 | [diff] [blame] | 4 | source check.vim |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 5 | |
Bram Moolenaar | 58adb14 | 2016-01-16 21:50:51 +0100 | [diff] [blame] | 6 | func 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('%')) |
Bram Moolenaar | f60b796 | 2016-01-16 22:47:23 +0100 | [diff] [blame] | 22 | if has('unix') |
| 23 | next! Xdir?/*/nofile |
| 24 | call assert_equal('Xdir?/*/nofile', expand('%')) |
| 25 | endif |
Bram Moolenaar | 08b270a | 2016-01-17 18:34:19 +0100 | [diff] [blame] | 26 | " Edit another file, on MS-Windows the swap file would be in use and can't |
| 27 | " be deleted. |
| 28 | edit foo |
Bram Moolenaar | 58adb14 | 2016-01-16 21:50:51 +0100 | [diff] [blame] | 29 | |
Bram Moolenaar | 08b270a | 2016-01-17 18:34:19 +0100 | [diff] [blame] | 30 | call assert_equal(0, delete('Xdir1', 'rf')) |
| 31 | call assert_equal(0, delete('Xdir2', 'rf')) |
| 32 | call assert_equal(0, delete('Xdir3', 'rf')) |
Bram Moolenaar | 58adb14 | 2016-01-16 21:50:51 +0100 | [diff] [blame] | 33 | endfunc |
| 34 | |
| 35 | func 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') |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 41 | call chdir(dir) |
Bram Moolenaar | 58adb14 | 2016-01-16 21:50:51 +0100 | [diff] [blame] | 42 | call delete('Xdir ~ dir', 'd') |
| 43 | call assert_false(isdirectory('Xdir ~ dir')) |
| 44 | endfunc |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 45 | |
| 46 | func 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! |
| 52 | endfunc |
Bram Moolenaar | 80dad48 | 2019-06-09 17:22:31 +0200 | [diff] [blame] | 53 | |
| 54 | func 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 |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 64 | call assert_equal('e Xfile2', 'e #'->expandcmd()) |
Bram Moolenaar | 80dad48 | 2019-06-09 17:22:31 +0200 | [diff] [blame] | 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 | |
Yegappan Lakshmanan | 5018a83 | 2022-04-02 21:12:21 +0100 | [diff] [blame] | 81 | call assert_equal('make <afile>', expandcmd("make <afile>")) |
| 82 | call assert_equal('make <amatch>', expandcmd("make <amatch>")) |
| 83 | call assert_equal('make <abuf>', expandcmd("make <abuf>")) |
Bram Moolenaar | 80dad48 | 2019-06-09 17:22:31 +0200 | [diff] [blame] | 84 | enew |
Yegappan Lakshmanan | 5018a83 | 2022-04-02 21:12:21 +0100 | [diff] [blame] | 85 | call assert_equal('make %', expandcmd("make %")) |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 86 | let $FOO="blue\tsky" |
| 87 | call setline(1, "$FOO") |
| 88 | call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>')) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 89 | |
| 90 | " Test for expression expansion `= |
| 91 | let $FOO= "blue" |
| 92 | call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`")) |
Yegappan Lakshmanan | 2b74b68 | 2022-04-03 21:30:32 +0100 | [diff] [blame] | 93 | let x = expandcmd("`=axbycz`") |
| 94 | call assert_equal('`=axbycz`', x) |
| 95 | call assert_fails('let x = expandcmd("`=axbycz`", #{errmsg: 1})', 'E121:') |
| 96 | let x = expandcmd("`=axbycz`", #{abc: []}) |
| 97 | call assert_equal('`=axbycz`', x) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 98 | |
| 99 | " Test for env variable with spaces |
| 100 | let $FOO= "foo bar baz" |
| 101 | call assert_equal("e foo bar baz", expandcmd("e $FOO")) |
| 102 | |
Yegappan Lakshmanan | 2b74b68 | 2022-04-03 21:30:32 +0100 | [diff] [blame] | 103 | if has('unix') && executable('bash') |
| 104 | " test for using the shell to expand a command argument. |
| 105 | " only bash supports the {..} syntax |
| 106 | set shell=bash |
| 107 | let x = expandcmd('{1..4}') |
| 108 | call assert_equal('{1..4}', x) |
| 109 | call assert_fails("let x = expandcmd('{1..4}', #{errmsg: v:true})", 'E77:') |
| 110 | let x = expandcmd('{1..4}', #{error: v:true}) |
| 111 | call assert_equal('{1..4}', x) |
| 112 | set shell& |
Yegappan Lakshmanan | 5018a83 | 2022-04-02 21:12:21 +0100 | [diff] [blame] | 113 | endif |
| 114 | |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 115 | unlet $FOO |
| 116 | close! |
Bram Moolenaar | 80dad48 | 2019-06-09 17:22:31 +0200 | [diff] [blame] | 117 | endfunc |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 118 | |
| 119 | " Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script |
| 120 | func Test_source_sfile() |
| 121 | let lines =<< trim [SCRIPT] |
Yegappan Lakshmanan | 5018a83 | 2022-04-02 21:12:21 +0100 | [diff] [blame] | 122 | :call assert_equal('<sfile>', expandcmd("<sfile>")) |
| 123 | :call assert_equal('<slnum>', expandcmd("<slnum>")) |
| 124 | :call assert_equal('<sflnum>', expandcmd("<sflnum>")) |
| 125 | :call assert_equal('edit <cfile>', expandcmd("edit <cfile>")) |
| 126 | :call assert_equal('edit #', expandcmd("edit #")) |
| 127 | :call assert_equal('edit #<2', expandcmd("edit #<2")) |
| 128 | :call assert_equal('edit <cword>', expandcmd("edit <cword>")) |
| 129 | :call assert_equal('edit <cexpr>', expandcmd("edit <cexpr>")) |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 130 | :call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:') |
Bram Moolenaar | 5754452 | 2022-04-12 12:54:11 +0100 | [diff] [blame] | 131 | : |
| 132 | :call assert_equal('', expand('<script>')) |
| 133 | :verbose echo expand('<script>') |
| 134 | :call add(v:errors, v:errmsg) |
| 135 | :verbose echo expand('<sfile>') |
| 136 | :call add(v:errors, v:errmsg) |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 137 | :call writefile(v:errors, 'Xresult') |
| 138 | :qall! |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 139 | [SCRIPT] |
| 140 | call writefile(lines, 'Xscript') |
| 141 | if RunVim([], [], '--clean -s Xscript') |
Bram Moolenaar | 5754452 | 2022-04-12 12:54:11 +0100 | [diff] [blame] | 142 | call assert_equal([ |
| 143 | \ 'E1274: No script file name to substitute for "<script>"', |
| 144 | \ 'E498: no :source file name to substitute for "<sfile>"'], |
| 145 | \ readfile('Xresult')) |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 146 | endif |
| 147 | call delete('Xscript') |
| 148 | call delete('Xresult') |
| 149 | endfunc |
| 150 | |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 151 | " Test for expanding filenames multiple times in a command line |
| 152 | func Test_expand_filename_multicmd() |
| 153 | edit foo |
| 154 | call setline(1, 'foo!') |
| 155 | new |
| 156 | call setline(1, 'foo!') |
| 157 | new <cword> | new <cWORD> |
| 158 | call assert_equal(4, winnr('$')) |
| 159 | call assert_equal('foo!', bufname(winbufnr(1))) |
| 160 | call assert_equal('foo', bufname(winbufnr(2))) |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 161 | call assert_fails('e %:s/.*//', 'E500:') |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 162 | %bwipe! |
| 163 | endfunc |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 164 | |
Christian Brabandt | 8b8d829 | 2021-11-19 12:37:36 +0000 | [diff] [blame] | 165 | func Test_expandcmd_shell_nonomatch() |
| 166 | CheckNotMSWindows |
| 167 | call assert_equal('$*', expandcmd('$*')) |
| 168 | endfunc |
| 169 | |
LemonBoy | 6013d00 | 2022-04-09 21:42:10 +0100 | [diff] [blame] | 170 | func Test_expand_script_source() |
| 171 | let lines0 =<< trim [SCRIPT] |
| 172 | let g:script_level[0] = expand('<script>:t') |
| 173 | so Xscript1 |
| 174 | func F0() |
| 175 | let g:func_level[0] = expand('<script>:t') |
| 176 | endfunc |
| 177 | [SCRIPT] |
| 178 | |
| 179 | let lines1 =<< trim [SCRIPT] |
| 180 | let g:script_level[1] = expand('<script>:t') |
| 181 | so Xscript2 |
| 182 | func F1() |
| 183 | let g:func_level[1] = expand('<script>:t') |
| 184 | endfunc |
| 185 | [SCRIPT] |
| 186 | |
| 187 | let lines2 =<< trim [SCRIPT] |
| 188 | let g:script_level[2] = expand('<script>:t') |
| 189 | func F2() |
| 190 | let g:func_level[2] = expand('<script>:t') |
| 191 | endfunc |
| 192 | [SCRIPT] |
| 193 | |
| 194 | call writefile(lines0, 'Xscript0') |
| 195 | call writefile(lines1, 'Xscript1') |
| 196 | call writefile(lines2, 'Xscript2') |
| 197 | |
| 198 | " Check the expansion of <script> at script and function level. |
| 199 | let g:script_level = ['', '', ''] |
| 200 | let g:func_level = ['', '', ''] |
| 201 | |
| 202 | so Xscript0 |
| 203 | call F0() |
| 204 | call F1() |
| 205 | call F2() |
| 206 | |
| 207 | call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:script_level) |
| 208 | call assert_equal(['Xscript0', 'Xscript1', 'Xscript2'], g:func_level) |
| 209 | |
| 210 | unlet g:script_level g:func_level |
| 211 | delfunc F0 |
| 212 | delfunc F1 |
| 213 | delfunc F2 |
| 214 | |
| 215 | call delete('Xscript0') |
| 216 | call delete('Xscript1') |
| 217 | call delete('Xscript2') |
| 218 | endfunc |
| 219 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 220 | " vim: shiftwidth=2 sts=2 expandtab |