robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
4a08596314
master
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 () 10return expand ( '<sfile>' ) 11endfunc 12 13func s: expand_slnum () 14return str2nr ( expand ( '<slnum>' )) 15endfunc 16 17func s: expand_sflnum () 18return str2nr ( expand ( '<sflnum>' )) 19endfunc 20 21" This test depends on the location in the test file, put it first. 22func Test_expand_sflnum () 23call assert_equal ( 7 , s: sflnum ) 24call assert_equal ( 24 , str2nr ( expand ( '<sflnum>' ))) 25 26" Line-continuation 27call assert_equal ( 28 \27 , 29 \str2nr ( expand ( '<sflnum>' ))) 30 31" Call in script-local function 32call assert_equal ( 18 , s: expand_sflnum ()) 33 34" Call in command 35command Flnum echo expand('<sflnum>') 36call assert_equal ( 36 , str2nr ( trim ( execute ( 'Flnum' )))) 37delcommand Flnum 38endfunc 39 40func Test_expand_sfile_and_stack () 41call assert_match ( 'test_expand_func\.vim$' , s: sfile ) 42let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack' 43call assert_match ( expected .. '$' , expand ( '<sfile>' )) 44call assert_match ( expected .. '\[4\]$' , expand ( '<stack>' )) 45 46" Call in script-local function 47call 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 50command Sfile echo expand('<sfile>') 51call assert_match ( 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$' , trim ( execute ( 'Sfile' ))) 52delcommand Sfile 53 54" Use <stack> from sourced script. 55let lines =<< trim END 56" comment here 57let g:stack_value = expand('<stack>') 58END 59call writefile ( lines , 'Xstack' ) 60source Xstack 61call assert_match ( '\<Xstack\[2\]$' , g: stack_value ) 62unlet g: stack_value 63call delete ( 'Xstack' ) 64 65if exists ( '+shellslash' ) 66call mkdir ( 'Xshellslash' ) 67let lines =<< trim END 68let g:stack1 = expand('<stack>') 69set noshellslash 70let g:stack2 = expand('<stack>') 71set shellslash 72let g:stack3 = expand('<stack>') 73END 74call writefile ( lines , 'Xshellslash/Xstack' ) 75" Test that changing 'shellslash' always affects the result of expand() 76" when sourcing a script multiple times. 77for i in range ( 2 ) 78source Xshellslash/Xstack 79call assert_match ( '\<Xshellslash/Xstack\[1\]$' , g: stack1 ) 80call assert_match ( '\<Xshellslash\\Xstack\[3\]$' , g: stack2 ) 81call assert_match ( '\<Xshellslash/Xstack\[5\]$' , g: stack3 ) 82unlet g: stack1 83unlet g: stack2 84unlet g: stack3 85endfor 86call delete ( 'Xshellslash' , 'rf' ) 87endif 88endfunc 89 90func Test_expand_slnum () 91call assert_equal ( 6 , s: slnum ) 92call assert_equal ( 2 , str2nr ( expand ( '<slnum>' ))) 93 94" Line-continuation 95call assert_equal ( 96 \5 , 97 \str2nr ( expand ( '<slnum>' ))) 98 99" Call in script-local function 100call assert_equal ( 1 , s: expand_slnum ()) 101 102" Call in command 103command Slnum echo expand('<slnum>') 104call assert_equal ( 14 , str2nr ( trim ( execute ( 'Slnum' )))) 105delcommand Slnum 106endfunc 107 108func Test_expand () 109new 110call assert_equal ( "" , expand ( '%:S' )) 111call assert_equal ( '3' , '<slnum>' ->expand ()) 112call assert_equal ([ '4' ], expand ( '<slnum>' , v: false , v: true )) 113" Don't add any line above this, otherwise <slnum> will change. 114call assert_equal ( "" , expand ( '%' )) 115set verbose = 1 116call assert_equal ( "" , expand ( '%' )) 117set verbose = 0 118call assert_equal ( "" , expand ( '%:p' )) 119quit 120endfunc 121 122func s: sid_test () 123return 'works' 124endfunc 125 126func Test_expand_SID () 127let sid = expand ( '<SID>' ) 128execute 'let g:sid_result = ' .. sid .. 'sid_test()' 129call assert_equal ( 'works' , g: sid_result ) 130endfunc 131 132 133" Test for 'wildignore' with expand() 134func Test_expand_wildignore () 135set wildignore = *.vim 136call assert_equal ( '' , expand ( 'test_expand_func.vim' )) 137call assert_equal ( '' , expand ( 'test_expand_func.vim' , 0 )) 138call assert_equal ([], expand ( 'test_expand_func.vim' , 0 , 1 )) 139call assert_equal ( 'test_expand_func.vim' , expand ( 'test_expand_func.vim' , 1 )) 140call assert_equal ([ 'test_expand_func.vim' ], 141 \expand ( 'test_expand_func.vim' , 1 , 1 )) 142call assert_fails ( "call expand('*', [])" , 'E745:' ) 143set wildignore & 144endfunc 145 146" Passing a long string to expand with 'wildignorecase' should not crash Vim. 147func Test_expand_long_str () 148set wildignorecase 149call expand ( 'a' ->repeat ( 99999 )) 150set wildignorecase & 151endfunc 152 153" vim: shiftwidth=2 sts=2 expandtab