robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
846a2019c0
master
1" Test for getstacktrace() and v:stacktrace 2 3source vim9.vim 4 5let s: thisfile = expand ( '%:p' ) 6let s: testdir = s: thisfile ->fnamemodify ( ':h' ) 7 8func Filepath ( name ) 9return s: testdir .. '/' .. a: name 10endfunc 11 12func AssertStacktrace ( expect , actual ) 13call assert_equal ( Filepath ( 'runtest.vim' ), a: actual [ 0 ][ 'filepath' ]) 14call assert_equal ( a: expect , a: actual [ - len ( a: expect ):]) 15endfunc 16 17func Test_getstacktrace () 18let g: stacktrace = [] 19let lines1 =<< trim [SCRIPT] 20" Xscript1 21source Xscript2 22func Xfunc1() 23" Xfunc1 24call Xfunc2() 25endfunc 26[SCRIPT] 27let lines2 =<< trim [SCRIPT] 28" Xscript2 29func Xfunc2() 30" Xfunc2 31let g:stacktrace = getstacktrace() 32endfunc 33[SCRIPT] 34call writefile ( lines1 , 'Xscript1' , 'D' ) 35call writefile ( lines2 , 'Xscript2' , 'D' ) 36source Xscript1 37call Xfunc1 () 38call AssertStacktrace ([ 39 \#{ funcref : funcref ( 'Test_getstacktrace' ), lnum : 37 , filepath : s: thisfile }, 40 \#{ funcref : funcref ( 'Xfunc1' ), lnum : 5 , filepath : Filepath ( 'Xscript1' )}, 41 \#{ funcref : funcref ( 'Xfunc2' ), lnum : 4 , filepath : Filepath ( 'Xscript2' )}, 42 \], g: stacktrace ) 43unlet g: stacktrace 44endfunc 45 46func Test_getstacktrace_event () 47let g: stacktrace = [] 48let lines1 =<< trim [SCRIPT] 49" Xscript1 50func Xfunc() 51" Xfunc 52let g:stacktrace = getstacktrace() 53endfunc 54augroup test_stacktrace 55autocmd SourcePre * call Xfunc() 56augroup END 57[SCRIPT] 58let lines2 =<< trim [SCRIPT] 59" Xscript2 60[SCRIPT] 61call writefile ( lines1 , 'Xscript1' , 'D' ) 62call writefile ( lines2 , 'Xscript2' , 'D' ) 63source Xscript1 64source Xscript2 65call AssertStacktrace ([ 66 \#{ funcref : funcref ( 'Test_getstacktrace_event' ), lnum : 64 , filepath : s: thisfile }, 67 \#{ event : 'SourcePre Autocommands for "*"' , lnum : 7 , filepath : Filepath ( 'Xscript1' )}, 68 \#{ funcref : funcref ( 'Xfunc' ), lnum : 4 , filepath : Filepath ( 'Xscript1' )}, 69 \], g: stacktrace ) 70augroup test_stacktrace 71autocmd ! 72augroup END 73unlet g: stacktrace 74endfunc 75 76func Test_vstacktrace () 77let lines1 =<< trim [SCRIPT] 78" Xscript1 79source Xscript2 80func Xfunc1() 81" Xfunc1 82call Xfunc2() 83endfunc 84[SCRIPT] 85let lines2 =<< trim [SCRIPT] 86" Xscript2 87func Xfunc2() 88" Xfunc2 89throw 'Exception from Xfunc2' 90endfunc 91[SCRIPT] 92call writefile ( lines1 , 'Xscript1' , 'D' ) 93call writefile ( lines2 , 'Xscript2' , 'D' ) 94source Xscript1 95call assert_equal ([], v: stacktrace ) 96try 97call Xfunc1 () 98catch 99let stacktrace = v: stacktrace 100try 101call Xfunc1 () 102catch 103let stacktrace_inner = v: stacktrace 104endtry 105let stacktrace_after = v: stacktrace " should be restored by the exception stack to the previous one 106endtry 107call assert_equal ([], v: stacktrace ) 108call AssertStacktrace ([ 109 \#{ funcref : funcref ( 'Test_vstacktrace' ), lnum : 97 , filepath : s: thisfile }, 110 \#{ funcref : funcref ( 'Xfunc1' ), lnum : 5 , filepath : Filepath ( 'Xscript1' )}, 111 \#{ funcref : funcref ( 'Xfunc2' ), lnum : 4 , filepath : Filepath ( 'Xscript2' )}, 112 \], stacktrace ) 113call AssertStacktrace ([ 114 \#{ funcref : funcref ( 'Test_vstacktrace' ), lnum : 101 , filepath : s: thisfile }, 115 \#{ funcref : funcref ( 'Xfunc1' ), lnum : 5 , filepath : Filepath ( 'Xscript1' )}, 116 \#{ funcref : funcref ( 'Xfunc2' ), lnum : 4 , filepath : Filepath ( 'Xscript2' )}, 117 \], stacktrace_inner ) 118call assert_equal ( stacktrace , stacktrace_after ) 119endfunc 120 121func Test_stacktrace_vim9 () 122let lines =<< trim [SCRIPT] 123var stacktrace = getstacktrace() 124assert_notequal([], stacktrace) 125for d in stacktrace 126assert_true(has_key(d, 'lnum')) 127endfor 128try 129throw 'Exception from s:Func' 130catch 131assert_notequal([], v:stacktrace) 132assert_equal(len(stacktrace), len(v:stacktrace)) 133for d in v:stacktrace 134assert_true(has_key(d, 'lnum')) 135endfor 136endtry 137call assert_equal([], v:stacktrace) 138[SCRIPT] 139call CheckDefSuccess ( lines ) 140endfunc 141 142" vim: shiftwidth=2 sts=2 expandtab