Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 1 | " Tests for expand() |
| 2 | |
Bram Moolenaar | 9094430 | 2020-08-01 20:45:11 +0200 | [diff] [blame] | 3 | source shared.vim |
| 4 | |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 5 | let s:sfile = expand('<sfile>') |
| 6 | let s:slnum = str2nr(expand('<slnum>')) |
| 7 | let s:sflnum = str2nr(expand('<sflnum>')) |
| 8 | |
| 9 | func s:expand_sfile() |
| 10 | return expand('<sfile>') |
| 11 | endfunc |
| 12 | |
| 13 | func s:expand_slnum() |
| 14 | return str2nr(expand('<slnum>')) |
| 15 | endfunc |
| 16 | |
| 17 | func s:expand_sflnum() |
| 18 | return str2nr(expand('<sflnum>')) |
| 19 | endfunc |
| 20 | |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 21 | " This test depends on the location in the test file, put it first. |
| 22 | func Test_expand_sflnum() |
Bram Moolenaar | 9094430 | 2020-08-01 20:45:11 +0200 | [diff] [blame] | 23 | call assert_equal(7, s:sflnum) |
| 24 | call assert_equal(24, str2nr(expand('<sflnum>'))) |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 25 | |
| 26 | " Line-continuation |
| 27 | call assert_equal( |
Bram Moolenaar | 9094430 | 2020-08-01 20:45:11 +0200 | [diff] [blame] | 28 | \ 27, |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 29 | \ str2nr(expand('<sflnum>'))) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 30 | |
| 31 | " Call in script-local function |
Bram Moolenaar | 9094430 | 2020-08-01 20:45:11 +0200 | [diff] [blame] | 32 | call assert_equal(18, s:expand_sflnum()) |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 33 | |
| 34 | " Call in command |
| 35 | command Flnum echo expand('<sflnum>') |
Bram Moolenaar | 9094430 | 2020-08-01 20:45:11 +0200 | [diff] [blame] | 36 | call assert_equal(36, str2nr(trim(execute('Flnum')))) |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 37 | delcommand Flnum |
| 38 | endfunc |
| 39 | |
| 40 | func Test_expand_sfile_and_stack() |
| 41 | call assert_match('test_expand_func\.vim$', s:sfile) |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 42 | let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack' |
| 43 | call assert_match(expected .. '$', expand('<sfile>')) |
zeertzjq | 5a4fff4 | 2022-08-15 17:53:55 +0100 | [diff] [blame] | 44 | call assert_match(expected .. '\[4\]$' , expand('<stack>')) |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 45 | |
| 46 | " Call in script-local function |
| 47 | call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack\[7\]\.\.<SNR>\d\+_expand_sfile$', s:expand_sfile()) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 48 | |
| 49 | " Call in command |
| 50 | command Sfile echo expand('<sfile>') |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 51 | call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$', trim(execute('Sfile'))) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 52 | delcommand Sfile |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 53 | |
| 54 | " Use <stack> from sourced script. |
| 55 | let lines =<< trim END |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 56 | " comment here |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 57 | let g:stack_value = expand('<stack>') |
| 58 | END |
| 59 | call writefile(lines, 'Xstack') |
| 60 | source Xstack |
Bram Moolenaar | a810db3 | 2020-09-11 17:59:23 +0200 | [diff] [blame] | 61 | call assert_match('\<Xstack\[2\]$', g:stack_value) |
zeertzjq | 5a4fff4 | 2022-08-15 17:53:55 +0100 | [diff] [blame] | 62 | unlet g:stack_value |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 63 | call delete('Xstack') |
zeertzjq | 5a4fff4 | 2022-08-15 17:53:55 +0100 | [diff] [blame] | 64 | |
| 65 | if exists('+shellslash') |
| 66 | call mkdir('Xshellslash') |
| 67 | let lines =<< trim END |
| 68 | let g:stack1 = expand('<stack>') |
| 69 | set noshellslash |
| 70 | let g:stack2 = expand('<stack>') |
| 71 | set shellslash |
| 72 | let g:stack3 = expand('<stack>') |
| 73 | END |
| 74 | call writefile(lines, 'Xshellslash/Xstack') |
| 75 | " Test that changing 'shellslash' always affects the result of expand() |
| 76 | " when sourcing a script multiple times. |
| 77 | for i in range(2) |
| 78 | source Xshellslash/Xstack |
| 79 | call assert_match('\<Xshellslash/Xstack\[1\]$', g:stack1) |
| 80 | call assert_match('\<Xshellslash\\Xstack\[3\]$', g:stack2) |
| 81 | call assert_match('\<Xshellslash/Xstack\[5\]$', g:stack3) |
| 82 | unlet g:stack1 |
| 83 | unlet g:stack2 |
| 84 | unlet g:stack3 |
| 85 | endfor |
| 86 | call delete('Xshellslash', 'rf') |
| 87 | endif |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 88 | endfunc |
| 89 | |
| 90 | func Test_expand_slnum() |
Bram Moolenaar | 9094430 | 2020-08-01 20:45:11 +0200 | [diff] [blame] | 91 | call assert_equal(6, s:slnum) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 92 | call assert_equal(2, str2nr(expand('<slnum>'))) |
| 93 | |
| 94 | " Line-continuation |
| 95 | call assert_equal( |
| 96 | \ 5, |
| 97 | \ str2nr(expand('<slnum>'))) |
| 98 | |
| 99 | " Call in script-local function |
| 100 | call assert_equal(1, s:expand_slnum()) |
| 101 | |
| 102 | " Call in command |
| 103 | command Slnum echo expand('<slnum>') |
| 104 | call assert_equal(14, str2nr(trim(execute('Slnum')))) |
| 105 | delcommand Slnum |
| 106 | endfunc |
| 107 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 108 | func Test_expand() |
| 109 | new |
Bram Moolenaar | 211a5bb | 2022-04-28 19:09:03 +0100 | [diff] [blame] | 110 | call assert_equal("", expand('%:S')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 111 | call assert_equal('3', '<slnum>'->expand()) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 112 | call assert_equal(['4'], expand('<slnum>', v:false, v:true)) |
| 113 | " Don't add any line above this, otherwise <slnum> will change. |
Bram Moolenaar | a96edb7 | 2022-04-28 17:52:24 +0100 | [diff] [blame] | 114 | call assert_equal("", expand('%')) |
| 115 | set verbose=1 |
| 116 | call assert_equal("", expand('%')) |
| 117 | set verbose=0 |
Bram Moolenaar | 211a5bb | 2022-04-28 19:09:03 +0100 | [diff] [blame] | 118 | call assert_equal("", expand('%:p')) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 119 | quit |
| 120 | endfunc |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 121 | |
Bram Moolenaar | 9094430 | 2020-08-01 20:45:11 +0200 | [diff] [blame] | 122 | func s:sid_test() |
| 123 | return 'works' |
| 124 | endfunc |
| 125 | |
| 126 | func Test_expand_SID() |
| 127 | let sid = expand('<SID>') |
| 128 | execute 'let g:sid_result = ' .. sid .. 'sid_test()' |
| 129 | call assert_equal('works', g:sid_result) |
| 130 | endfunc |
| 131 | |
| 132 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 133 | " Test for 'wildignore' with expand() |
| 134 | func Test_expand_wildignore() |
| 135 | set wildignore=*.vim |
| 136 | call assert_equal('', expand('test_expand_func.vim')) |
| 137 | call assert_equal('', expand('test_expand_func.vim', 0)) |
| 138 | call assert_equal([], expand('test_expand_func.vim', 0, 1)) |
| 139 | call assert_equal('test_expand_func.vim', expand('test_expand_func.vim', 1)) |
| 140 | call assert_equal(['test_expand_func.vim'], |
| 141 | \ expand('test_expand_func.vim', 1, 1)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 142 | call assert_fails("call expand('*', [])", 'E745:') |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 143 | set wildignore& |
| 144 | endfunc |
| 145 | |
| 146 | " vim: shiftwidth=2 sts=2 expandtab |