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