robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d393975b04
master
1 2" Use a different file name for each run. 3let s: sequence = 1 4 5func CheckDefSuccess ( lines ) 6return 7endfunc 8 9func CheckDefFailure ( lines , error , lnum = -3 ) 10return 11endfunc 12 13func CheckDefExecFailure ( lines , error , lnum = -3 ) 14return 15endfunc 16 17func CheckScriptFailure ( lines , error , lnum = -3 ) 18if get ( a: lines , 0 , '' ) ==# 'vim9script' 19return 20endif 21let cwd = getcwd () 22let fname = 'XScriptFailure' .. s: sequence 23let s: sequence += 1 24call writefile ( a: lines , fname ) 25try 26call assert_fails ( 'so ' .. fname , a: error , a: lines , a: lnum ) 27finally 28call chdir ( cwd ) 29call delete ( fname ) 30endtry 31endfunc 32 33func CheckScriptSuccess ( lines ) 34if get ( a: lines , 0 , '' ) ==# 'vim9script' 35return 36endif 37let cwd = getcwd () 38let fname = 'XScriptSuccess' .. s: sequence 39let s: sequence += 1 40call writefile ( a: lines , fname ) 41try 42exe 'so ' .. fname 43finally 44call chdir ( cwd ) 45call delete ( fname ) 46endtry 47endfunc 48 49func CheckDefAndScriptSuccess ( lines ) 50return 51endfunc 52 53func CheckDefAndScriptFailure ( lines , error , lnum = -3 ) 54return 55endfunc 56 57func CheckDefExecAndScriptFailure ( lines , error , lnum = -3 ) 58return 59endfunc 60 61" As CheckDefExecAndScriptFailure() but with two different expected errors. 62func CheckDefExecAndScriptFailure2 ( lines , errorDef , errorScript , lnum = -3 ) 63return 64endfunc 65 66" Check that "lines" inside a legacy function has no error. 67func CheckLegacySuccess ( lines ) 68let cwd = getcwd () 69let fname = 'XlegacySuccess' .. s: sequence 70let s: sequence += 1 71call writefile ([ 'func Func()' ] + a: lines + [ 'endfunc' ], fname ) 72try 73exe 'so ' .. fname 74call Func () 75finally 76delfunc ! Func 77call chdir ( cwd ) 78call delete ( fname ) 79endtry 80endfunc 81 82" Check that "lines" inside a legacy function results in the expected error 83func CheckLegacyFailure ( lines , error ) 84let cwd = getcwd () 85let fname = 'XlegacyFails' .. s: sequence 86let s: sequence += 1 87call writefile ([ 'func Func()' ] + a: lines + [ 'endfunc' , 'call Func()' ], fname ) 88try 89call assert_fails ( 'so ' .. fname , a: error ) 90finally 91delfunc ! Func 92call chdir ( cwd ) 93call delete ( fname ) 94endtry 95endfunc 96 97" Translate "lines" to legacy Vim script 98func s: LegacyTrans ( lines ) 99return a: lines ->mapnew ({ _ , v -> 100 \v ->substitute ( '\<VAR\>' , 'let' , 'g' ) 101 \ ->substitute ( '\<LET\>' , 'let' , 'g' ) 102 \ ->substitute ( '\<LSTART\>' , '{' , 'g' ) 103 \ ->substitute ( '\<LMIDDLE\>' , '->' , 'g' ) 104 \ ->substitute ( '\<LEND\>' , '}' , 'g' ) 105 \ ->substitute ( '\<TRUE\>' , '1' , 'g' ) 106 \ ->substitute ( '\<FALSE\>' , '0' , 'g' ) 107 \ ->substitute ( '#"' , ' "' , 'g' ) 108 \}) 109endfunc 110 111" Execute "lines" in a legacy function, translated as in 112" CheckLegacyAndVim9Success() 113func CheckTransLegacySuccess ( lines ) 114call CheckLegacySuccess ( s: LegacyTrans ( a: lines )) 115endfunc 116 117func CheckTransDefSuccess ( lines ) 118return 119endfunc 120 121func CheckTransVim9Success ( lines ) 122return 123endfunc 124 125" Execute "lines" in a legacy function 126" Use 'VAR' for a declaration. 127" Use 'LET' for an assignment 128" Use ' #"' for a comment 129" Use LSTART arg LMIDDLE expr LEND for lambda 130" Use 'TRUE' for 1 131" Use 'FALSE' for 0 132func CheckLegacyAndVim9Success ( lines ) 133call CheckTransLegacySuccess ( a: lines ) 134endfunc 135 136" Execute "lines" in a legacy function 137" Use 'VAR' for a declaration. 138" Use 'LET' for an assignment 139" Use ' #"' for a comment 140func CheckLegacyAndVim9Failure ( lines , error ) 141if type ( a: error ) == type ( 'string' ) 142let legacyError = a: error 143else 144let legacyError = a: error [ 0 ] 145endif 146 147let legacylines = a: lines ->mapnew ({ _ , v -> 148 \v ->substitute ( '\<VAR\>' , 'let' , 'g' ) 149 \ ->substitute ( '\<LET\>' , 'let' , 'g' ) 150 \ ->substitute ( '#"' , ' "' , 'g' ) 151 \}) 152call CheckLegacyFailure ( legacylines , legacyError ) 153endfunc 154 155" Check that "lines" inside a legacy function has no error. 156func CheckSourceLegacySuccess ( lines ) 157let cwd = getcwd () 158new 159call setline ( 1 , [ 'func Func()' ] + a: lines + [ 'endfunc' , 'call Func()' ]) 160let bnr = bufnr () 161try 162: source 163finally 164delfunc ! Func 165call chdir ( cwd ) 166exe $ ':bw! {bnr}' 167endtry 168endfunc 169 170" Check that "lines" inside a legacy function results in the expected error 171func CheckSourceLegacyFailure ( lines , error ) 172let cwd = getcwd () 173new 174call setline ( 1 , [ 'func Func()' ] + a: lines + [ 'endfunc' , 'call Func()' ]) 175let bnr = bufnr () 176try 177call assert_fails ( 'source' , a: error ) 178finally 179delfunc ! Func 180call chdir ( cwd ) 181exe $ ':bw! {bnr}' 182endtry 183endfunc 184 185" Execute "lines" in a legacy function, translated as in 186" CheckSourceLegacyAndVim9Success() 187func CheckSourceTransLegacySuccess ( lines ) 188call CheckSourceLegacySuccess ( s: LegacyTrans ( a: lines )) 189endfunc 190 191" Execute "lines" in a :def function, translated as in 192" CheckLegacyAndVim9Success() 193func CheckSourceTransDefSuccess ( lines ) 194return 195endfunc 196 197" Execute "lines" in a Vim9 script, translated as in 198" CheckLegacyAndVim9Success() 199func CheckSourceTransVim9Success ( lines ) 200return 201endfunc 202 203" Execute "lines" in a legacy function, :def function and Vim9 script. 204" Use 'VAR' for a declaration. 205" Use 'LET' for an assignment 206" Use ' #"' for a comment 207" Use LSTART arg LMIDDLE expr LEND for lambda 208" Use 'TRUE' for 1 in legacy, true in Vim9 209" Use 'FALSE' for 0 in legacy, false in Vim9 210func CheckSourceLegacyAndVim9Success ( lines ) 211call CheckSourceTransLegacySuccess ( a: lines ) 212endfunc 213 214" :source a list of "lines" and check whether it fails with "error" 215func CheckSourceScriptFailure ( lines , error , lnum = -3 ) 216if get ( a: lines , 0 , '' ) ==# 'vim9script' 217return 218endif 219let cwd = getcwd () 220new 221call setline ( 1 , a: lines ) 222let bnr = bufnr () 223try 224call assert_fails ( 'source' , a: error , a: lines , a: lnum ) 225finally 226call chdir ( cwd ) 227exe $ ':bw! {bnr}' 228endtry 229endfunc 230 231" :source a list of "lines" and check whether it fails with the list of 232" "errors" 233func CheckSourceScriptFailureList ( lines , errors , lnum = -3 ) 234if get ( a: lines , 0 , '' ) ==# 'vim9script' 235return 236endif 237let cwd = getcwd () 238new 239let bnr = bufnr () 240call setline ( 1 , a: lines ) 241try 242call assert_fails ( 'source' , a: errors , a: lines , a: lnum ) 243finally 244call chdir ( cwd ) 245exe $ ':bw! {bnr}' 246endtry 247endfunc 248 249" :source a list of "lines" and check whether it succeeds 250func CheckSourceScriptSuccess ( lines ) 251if get ( a: lines , 0 , '' ) ==# 'vim9script' 252return 253endif 254let cwd = getcwd () 255new 256let bnr = bufnr () 257call setline ( 1 , a: lines ) 258try 259: source 260finally 261call chdir ( cwd ) 262exe $ ':bw! {bnr}' 263endtry 264endfunc 265 266func CheckSourceDefSuccess ( lines ) 267return 268endfunc 269 270func CheckSourceDefCompileSuccess ( lines ) 271return 272endfunc 273 274func CheckSourceDefFailure ( lines , error , lnum = -3 ) 275return 276endfunc 277 278func CheckSourceDefExecFailure ( lines , error , lnum = -3 ) 279return 280endfunc 281 282func CheckSourceDefAndScriptFailure ( lines , error , lnum = -3 ) 283return 284endfunc 285 286func CheckSourceDefExecAndScriptFailure ( lines , error , lnum = -3 ) 287return 288endfunc 289 290func CheckSourceSuccess ( lines ) 291call CheckSourceScriptSuccess ( a: lines ) 292endfunc 293 294func CheckSourceFailure ( lines , error , lnum = -3 ) 295call CheckSourceScriptFailure ( a: lines , a: error , a: lnum ) 296endfunc 297 298func CheckSourceFailureList ( lines , errors , lnum = -3 ) 299call CheckSourceScriptFailureList ( a: lines , a: errors , a: lnum ) 300endfunc 301 302func CheckSourceDefAndScriptSuccess ( lines ) 303return 304endfunc 305 306" Execute "lines" in a legacy function, :def function and Vim9 script. 307" Use 'VAR' for a declaration. 308" Use 'LET' for an assignment 309" Use ' #"' for a comment 310func CheckSourceLegacyAndVim9Failure ( lines , error ) 311let legacyError = type ( a: error ) == type ( 'string' ) ? a: error : a: error [ 0 ] 312call CheckSourceLegacyFailure ( s: LegacyTrans ( a: lines ), legacyError ) 313endfunc 314