1" Tests for expand() 2 3let s:sfile = expand('<sfile>') 4let s:slnum = str2nr(expand('<slnum>')) 5let s:sflnum = str2nr(expand('<sflnum>')) 6 7func s:expand_sfile() 8 return expand('<sfile>') 9endfunc 10 11func s:expand_slnum() 12 return str2nr(expand('<slnum>')) 13endfunc 14 15func s:expand_sflnum() 16 return str2nr(expand('<sflnum>')) 17endfunc 18 19" This test depends on the location in the test file, put it first. 20func 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>'))) 28 29 " Call in script-local function 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 36endfunc 37 38func 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()) 46 47 " Call in command 48 command Sfile echo expand('<sfile>') 49 call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$', trim(execute('Sfile'))) 50 delcommand Sfile 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') 60endfunc 61 62func 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 78endfunc 79 80func Test_expand() 81 new 82 call assert_equal("", expand('%:S')) 83 call assert_equal('3', '<slnum>'->expand()) 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 87endfunc 88 89" Test for 'wildignore' with expand() 90func 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)) 98 call assert_fails("call expand('*', [])", 'E745:') 99 set wildignore& 100endfunc 101 102" vim: shiftwidth=2 sts=2 expandtab 103