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 expected 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, translated as in 170" CheckLegacyAndVim9Success() 171def CheckTransLegacySuccess(lines: list<string>) 172 var legacylines = lines->mapnew((_, v) => 173 v->substitute('\<VAR\>', 'let', 'g') 174 ->substitute('\<LET\>', 'let', 'g') 175 ->substitute('#"', ' "', 'g')) 176 CheckLegacySuccess(legacylines) 177enddef 178 179" Execute "lines" in a :def function, translated as in 180" CheckLegacyAndVim9Success() 181def CheckTransDefSuccess(lines: list<string>) 182 var vim9lines = lines->mapnew((_, v) => 183 v->substitute('\<VAR\>', 'var', 'g') 184 ->substitute('\<LET ', '', 'g')) 185 CheckDefSuccess(vim9lines) 186enddef 187 188" Execute "lines" in a Vim9 script, translated as in 189" CheckLegacyAndVim9Success() 190def CheckTransVim9Success(lines: list<string>) 191 var vim9lines = lines->mapnew((_, v) => 192 v->substitute('\<VAR\>', 'var', 'g') 193 ->substitute('\<LET ', '', 'g')) 194 CheckScriptSuccess(['vim9script'] + vim9lines) 195enddef 196 197" Execute "lines" in a legacy function, :def function and Vim9 script. 198" Use 'VAR' for a declaration. 199" Use 'LET' for an assignment 200" Use ' #"' for a comment 201def CheckLegacyAndVim9Success(lines: list<string>) 202 CheckTransLegacySuccess(lines) 203 CheckTransDefSuccess(lines) 204 CheckTransVim9Success(lines) 205enddef 206 207" Execute "lines" in a legacy function, :def function and Vim9 script. 208" Use 'VAR' for a declaration. 209" Use 'LET' for an assignment 210" Use ' #"' for a comment 211def CheckLegacyAndVim9Failure(lines: list<string>, error: any) 212 var legacyError: string 213 var defError: string 214 var scriptError: string 215 216 if type(error) == type('string') 217 legacyError = error 218 defError = error 219 scriptError = error 220 else 221 legacyError = error[0] 222 defError = error[1] 223 scriptError = error[2] 224 endif 225 226 var legacylines = lines->mapnew((_, v) => 227 v->substitute('\<VAR\>', 'let', 'g') 228 ->substitute('\<LET\>', 'let', 'g') 229 ->substitute('#"', ' "', 'g')) 230 CheckLegacyFailure(legacylines, legacyError) 231 232 var vim9lines = lines->mapnew((_, v) => 233 v->substitute('\<VAR\>', 'var', 'g') 234 ->substitute('\<LET ', '', 'g')) 235 CheckDefExecFailure(vim9lines, defError) 236 CheckScriptFailure(['vim9script'] + vim9lines, scriptError) 237enddef 238