Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 1 | " Tests for expand() |
| 2 | |
| 3 | let s:sfile = expand('<sfile>') |
| 4 | let s:slnum = str2nr(expand('<slnum>')) |
| 5 | let s:sflnum = str2nr(expand('<sflnum>')) |
| 6 | |
| 7 | func s:expand_sfile() |
| 8 | return expand('<sfile>') |
| 9 | endfunc |
| 10 | |
| 11 | func s:expand_slnum() |
| 12 | return str2nr(expand('<slnum>')) |
| 13 | endfunc |
| 14 | |
| 15 | func s:expand_sflnum() |
| 16 | return str2nr(expand('<sflnum>')) |
| 17 | endfunc |
| 18 | |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame^] | 19 | " This test depends on the location in the test file, put it first. |
| 20 | func Test_expand_sflnum() |
| 21 | call assert_equal(5, s:sflnum) |
| 22 | call assert_equal(22, str2nr(expand('<sflnum>'))) |
| 23 | |
| 24 | " Line-continuation |
| 25 | call assert_equal( |
| 26 | \ 25, |
| 27 | \ str2nr(expand('<sflnum>'))) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 28 | |
| 29 | " Call in script-local function |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame^] | 30 | call assert_equal(16, s:expand_sflnum()) |
| 31 | |
| 32 | " Call in command |
| 33 | command Flnum echo expand('<sflnum>') |
| 34 | call assert_equal(34, str2nr(trim(execute('Flnum')))) |
| 35 | delcommand Flnum |
| 36 | endfunc |
| 37 | |
| 38 | func Test_expand_sfile_and_stack() |
| 39 | call assert_match('test_expand_func\.vim$', s:sfile) |
| 40 | let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$' |
| 41 | call assert_match(expected , expand('<sfile>')) |
| 42 | call assert_match(expected , expand('<stack>')) |
| 43 | |
| 44 | " Call in script-local function |
| 45 | 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] | 46 | |
| 47 | " Call in command |
| 48 | command Sfile echo expand('<sfile>') |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame^] | 49 | 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] | 50 | delcommand Sfile |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame^] | 51 | |
| 52 | " Use <stack> from sourced script. |
| 53 | let lines =<< trim END |
| 54 | let g:stack_value = expand('<stack>') |
| 55 | END |
| 56 | call writefile(lines, 'Xstack') |
| 57 | source Xstack |
| 58 | call assert_match('\<Xstack$', g:stack_value) |
| 59 | call delete('Xstack') |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 60 | endfunc |
| 61 | |
| 62 | func Test_expand_slnum() |
| 63 | call assert_equal(4, s:slnum) |
| 64 | call assert_equal(2, str2nr(expand('<slnum>'))) |
| 65 | |
| 66 | " Line-continuation |
| 67 | call assert_equal( |
| 68 | \ 5, |
| 69 | \ str2nr(expand('<slnum>'))) |
| 70 | |
| 71 | " Call in script-local function |
| 72 | call assert_equal(1, s:expand_slnum()) |
| 73 | |
| 74 | " Call in command |
| 75 | command Slnum echo expand('<slnum>') |
| 76 | call assert_equal(14, str2nr(trim(execute('Slnum')))) |
| 77 | delcommand Slnum |
| 78 | endfunc |
| 79 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 80 | func Test_expand() |
| 81 | new |
| 82 | call assert_equal("", expand('%:S')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 83 | call assert_equal('3', '<slnum>'->expand()) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 84 | call assert_equal(['4'], expand('<slnum>', v:false, v:true)) |
| 85 | " Don't add any line above this, otherwise <slnum> will change. |
| 86 | quit |
| 87 | endfunc |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 88 | |
| 89 | " Test for 'wildignore' with expand() |
| 90 | func Test_expand_wildignore() |
| 91 | set wildignore=*.vim |
| 92 | call assert_equal('', expand('test_expand_func.vim')) |
| 93 | call assert_equal('', expand('test_expand_func.vim', 0)) |
| 94 | call assert_equal([], expand('test_expand_func.vim', 0, 1)) |
| 95 | call assert_equal('test_expand_func.vim', expand('test_expand_func.vim', 1)) |
| 96 | call assert_equal(['test_expand_func.vim'], |
| 97 | \ expand('test_expand_func.vim', 1, 1)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 98 | call assert_fails("call expand('*', [])", 'E745:') |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 99 | set wildignore& |
| 100 | endfunc |
| 101 | |
| 102 | " vim: shiftwidth=2 sts=2 expandtab |