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