1" Utility functions for testing vim9 script 2 3" Use a different file name for each run. 4let s:sequence = 1 5 6" Check that "lines" inside a ":def" function has no error. 7func CheckDefSuccess(lines) 8 let cwd = getcwd() 9 let fname = 'XdefSuccess' .. s:sequence 10 let s:sequence += 1 11 call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname) 12 try 13 exe 'so ' .. fname 14 call Func() 15 delfunc! Func 16 finally 17 call chdir(cwd) 18 call delete(fname) 19 endtry 20endfunc 21 22" Check that "lines" inside ":def" results in an "error" message. 23" If "lnum" is given check that the error is reported for this line. 24" Add a line before and after to make it less likely that the line number is 25" accidentally correct. 26func CheckDefFailure(lines, error, lnum = -3) 27 let cwd = getcwd() 28 let fname = 'XdefFailure' .. s:sequence 29 let s:sequence += 1 30 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile'], fname) 31 try 32 call assert_fails('so ' .. fname, a:error, a:lines, a:lnum + 1) 33 finally 34 call chdir(cwd) 35 call delete(fname) 36 delfunc! Func 37 endtry 38endfunc 39 40" Check that "lines" inside ":def" results in an "error" message when executed. 41" If "lnum" is given check that the error is reported for this line. 42" Add a line before and after to make it less likely that the line number is 43" accidentally correct. 44func CheckDefExecFailure(lines, error, lnum = -3) 45 let cwd = getcwd() 46 let fname = 'XdefExecFailure' .. s:sequence 47 let s:sequence += 1 48 call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef'], fname) 49 try 50 exe 'so ' .. fname 51 call assert_fails('call Func()', a:error, a:lines, a:lnum + 1) 52 finally 53 call chdir(cwd) 54 call delete(fname) 55 delfunc! Func 56 endtry 57endfunc 58 59def CheckScriptFailure(lines: list<string>, error: string, lnum = -3) 60 var cwd = getcwd() 61 var fname = 'XScriptFailure' .. s:sequence 62 s:sequence += 1 63 writefile(lines, fname) 64 try 65 assert_fails('so ' .. fname, error, lines, lnum) 66 finally 67 chdir(cwd) 68 delete(fname) 69 endtry 70enddef 71 72def CheckScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3) 73 var cwd = getcwd() 74 var fname = 'XScriptFailure' .. s:sequence 75 s:sequence += 1 76 writefile(lines, fname) 77 try 78 assert_fails('so ' .. fname, errors, lines, lnum) 79 finally 80 chdir(cwd) 81 delete(fname) 82 endtry 83enddef 84 85def CheckScriptSuccess(lines: list<string>) 86 var cwd = getcwd() 87 var fname = 'XScriptSuccess' .. s:sequence 88 s:sequence += 1 89 writefile(lines, fname) 90 try 91 exe 'so ' .. fname 92 finally 93 chdir(cwd) 94 delete(fname) 95 endtry 96enddef 97 98def CheckDefAndScriptSuccess(lines: list<string>) 99 CheckDefSuccess(lines) 100 CheckScriptSuccess(['vim9script'] + lines) 101enddef 102 103" Check that a command fails with the same error when used in a :def function 104" and when used in Vim9 script. 105def CheckDefAndScriptFailure(lines: list<string>, error: string, lnum = -3) 106 CheckDefFailure(lines, error, lnum) 107 CheckScriptFailure(['vim9script'] + lines, error, lnum + 1) 108enddef 109 110" As CheckDefAndScriptFailure() but with two different exepcted errors. 111def CheckDefAndScriptFailure2( 112 lines: list<string>, 113 errorDef: string, 114 errorScript: string, 115 lnum = -3) 116 CheckDefFailure(lines, errorDef, lnum) 117 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) 118enddef 119 120" Check that a command fails with the same error when executed in a :def 121" function and when used in Vim9 script. 122def CheckDefExecAndScriptFailure(lines: list<string>, error: string, lnum = -3) 123 CheckDefExecFailure(lines, error, lnum) 124 CheckScriptFailure(['vim9script'] + lines, error, lnum + 1) 125enddef 126 127" As CheckDefExecAndScriptFailure() but with two different expected errors. 128def CheckDefExecAndScriptFailure2( 129 lines: list<string>, 130 errorDef: string, 131 errorScript: string, 132 lnum = -3) 133 CheckDefExecFailure(lines, errorDef, lnum) 134 CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) 135enddef 136 137 138" Check that "lines" inside a legacy function has no error. 139func CheckLegacySuccess(lines) 140 let cwd = getcwd() 141 let fname = 'XlegacySuccess' .. s:sequence 142 let s:sequence += 1 143 call writefile(['func Func()'] + a:lines + ['endfunc'], fname) 144 try 145 exe 'so ' .. fname 146 call Func() 147 finally 148 delfunc! Func 149 call chdir(cwd) 150 call delete(fname) 151 endtry 152endfunc 153 154" Check that "lines" inside a legacy function results in the expected error 155func CheckLegacyFailure(lines, error) 156 let cwd = getcwd() 157 let fname = 'XlegacyFails' .. s:sequence 158 let s:sequence += 1 159 call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname) 160 try 161 call assert_fails('so ' .. fname, a:error) 162 finally 163 delfunc! Func 164 call chdir(cwd) 165 call delete(fname) 166 endtry 167endfunc 168 169" Execute "lines" in a legacy function, :def function and Vim9 script. 170" Use 'VAR' for a declaration. 171" Use 'LET' for an assignment 172" Use ' #"' for a comment 173def CheckLegacyAndVim9Success(lines: list<string>) 174 var legacylines = lines->mapnew((_, v) => 175 v->substitute('\<VAR\>', 'let', 'g') 176 ->substitute('\<LET\>', 'let', 'g') 177 ->substitute('#"', ' "', 'g')) 178 CheckLegacySuccess(legacylines) 179 180 var vim9lines = lines->mapnew((_, v) => 181 v->substitute('\<VAR\>', 'var', 'g') 182 ->substitute('\<LET ', '', 'g')) 183 CheckDefSuccess(vim9lines) 184 CheckScriptSuccess(['vim9script'] + vim9lines) 185enddef 186 187" Execute "lines" in a legacy function, :def function and Vim9 script. 188" Use 'VAR' for a declaration. 189" Use 'LET' for an assignment 190" Use ' #"' for a comment 191def CheckLegacyAndVim9Failure(lines: list<string>, error: any) 192 var legacyError: string 193 var defError: string 194 var scriptError: string 195 196 if type(error) == type('string') 197 legacyError = error 198 defError = error 199 scriptError = error 200 else 201 legacyError = error[0] 202 defError = error[1] 203 scriptError = error[2] 204 endif 205 206 var legacylines = lines->mapnew((_, v) => 207 v->substitute('\<VAR\>', 'let', 'g') 208 ->substitute('\<LET\>', 'let', 'g') 209 ->substitute('#"', ' "', 'g')) 210 CheckLegacyFailure(legacylines, legacyError) 211 212 var vim9lines = lines->mapnew((_, v) => 213 v->substitute('\<VAR\>', 'var', 'g') 214 ->substitute('\<LET ', '', 'g')) 215 CheckDefExecFailure(vim9lines, defError) 216 CheckScriptFailure(['vim9script'] + vim9lines, scriptError) 217enddef 218