Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1 | vim9script |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 3 | # Utility functions for testing vim9 script |
Bram Moolenaar | 2d870f8 | 2020-12-05 13:41:01 +0100 | [diff] [blame] | 4 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 5 | # Use a different file name for each run. |
| 6 | var sequence = 1 |
| 7 | |
| 8 | # Check that "lines" inside a ":def" function has no error when called. |
| 9 | export func CheckDefSuccess(lines) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 10 | let cwd = getcwd() |
| 11 | let fname = 'XdefSuccess' .. s:sequence |
Bram Moolenaar | 2d870f8 | 2020-12-05 13:41:01 +0100 | [diff] [blame] | 12 | let s:sequence += 1 |
| 13 | call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 14 | try |
| 15 | exe 'so ' .. fname |
| 16 | call Func() |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 17 | finally |
| 18 | call chdir(cwd) |
| 19 | call delete(fname) |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 20 | delfunc! Func |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 21 | endtry |
Bram Moolenaar | 3e06a1e | 2020-08-10 21:57:54 +0200 | [diff] [blame] | 22 | endfunc |
| 23 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 24 | # Check that "lines" inside a ":def" function has no error when compiled. |
| 25 | export func CheckDefCompileSuccess(lines) |
Bram Moolenaar | 0801822 | 2021-12-22 18:45:37 +0000 | [diff] [blame] | 26 | let fname = 'XdefSuccess' .. s:sequence |
| 27 | let s:sequence += 1 |
| 28 | call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], fname) |
| 29 | try |
| 30 | exe 'so ' .. fname |
| 31 | finally |
| 32 | call delete(fname) |
| 33 | delfunc! Func |
| 34 | endtry |
| 35 | endfunc |
| 36 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 37 | # Check that "lines" inside ":def" results in an "error" message. |
| 38 | # If "lnum" is given check that the error is reported for this line. |
| 39 | # Add a line before and after to make it less likely that the line number is |
| 40 | # accidentally correct. |
| 41 | export func CheckDefFailure(lines, error, lnum = -3) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 42 | let cwd = getcwd() |
| 43 | let fname = 'XdefFailure' .. s:sequence |
Bram Moolenaar | 2d870f8 | 2020-12-05 13:41:01 +0100 | [diff] [blame] | 44 | let s:sequence += 1 |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 45 | call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile'], fname) |
| 46 | try |
| 47 | call assert_fails('so ' .. fname, a:error, a:lines, a:lnum + 1) |
| 48 | finally |
| 49 | call chdir(cwd) |
| 50 | call delete(fname) |
| 51 | delfunc! Func |
| 52 | endtry |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 53 | endfunc |
| 54 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 55 | # Check that "lines" inside ":def" results in an "error" message when executed. |
| 56 | # If "lnum" is given check that the error is reported for this line. |
| 57 | # Add a line before and after to make it less likely that the line number is |
| 58 | # accidentally correct. |
| 59 | export func CheckDefExecFailure(lines, error, lnum = -3) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 60 | let cwd = getcwd() |
| 61 | let fname = 'XdefExecFailure' .. s:sequence |
Bram Moolenaar | 2d870f8 | 2020-12-05 13:41:01 +0100 | [diff] [blame] | 62 | let s:sequence += 1 |
| 63 | call writefile(['def Func()', '# comment'] + a:lines + ['#comment', 'enddef'], fname) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 64 | try |
| 65 | exe 'so ' .. fname |
| 66 | call assert_fails('call Func()', a:error, a:lines, a:lnum + 1) |
| 67 | finally |
| 68 | call chdir(cwd) |
| 69 | call delete(fname) |
| 70 | delfunc! Func |
| 71 | endtry |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 72 | endfunc |
| 73 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 74 | export def CheckScriptFailure(lines: list<string>, error: string, lnum = -3) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 75 | var cwd = getcwd() |
Bram Moolenaar | a749a42 | 2022-02-12 19:52:25 +0000 | [diff] [blame] | 76 | var fname = 'XScriptFailure' .. sequence |
| 77 | sequence += 1 |
Bram Moolenaar | 2d870f8 | 2020-12-05 13:41:01 +0100 | [diff] [blame] | 78 | writefile(lines, fname) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 79 | try |
| 80 | assert_fails('so ' .. fname, error, lines, lnum) |
| 81 | finally |
| 82 | chdir(cwd) |
| 83 | delete(fname) |
| 84 | endtry |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 85 | enddef |
| 86 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 87 | export def CheckScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3) |
Bram Moolenaar | c5f59fa | 2021-01-21 12:34:14 +0100 | [diff] [blame] | 88 | var cwd = getcwd() |
Bram Moolenaar | a749a42 | 2022-02-12 19:52:25 +0000 | [diff] [blame] | 89 | var fname = 'XScriptFailure' .. sequence |
| 90 | sequence += 1 |
Bram Moolenaar | c5f59fa | 2021-01-21 12:34:14 +0100 | [diff] [blame] | 91 | writefile(lines, fname) |
| 92 | try |
| 93 | assert_fails('so ' .. fname, errors, lines, lnum) |
| 94 | finally |
| 95 | chdir(cwd) |
| 96 | delete(fname) |
| 97 | endtry |
| 98 | enddef |
| 99 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 100 | export def CheckScriptSuccess(lines: list<string>) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 101 | var cwd = getcwd() |
Bram Moolenaar | a749a42 | 2022-02-12 19:52:25 +0000 | [diff] [blame] | 102 | var fname = 'XScriptSuccess' .. sequence |
| 103 | sequence += 1 |
Bram Moolenaar | 2d870f8 | 2020-12-05 13:41:01 +0100 | [diff] [blame] | 104 | writefile(lines, fname) |
Bram Moolenaar | 090728a | 2020-12-20 15:43:31 +0100 | [diff] [blame] | 105 | try |
| 106 | exe 'so ' .. fname |
| 107 | finally |
| 108 | chdir(cwd) |
| 109 | delete(fname) |
| 110 | endtry |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 111 | enddef |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 112 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 113 | export def CheckDefAndScriptSuccess(lines: list<string>) |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 114 | CheckDefSuccess(lines) |
| 115 | CheckScriptSuccess(['vim9script'] + lines) |
| 116 | enddef |
| 117 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 118 | # Check that a command fails when used in a :def function and when used in |
| 119 | # Vim9 script. |
| 120 | # When "error" is a string, both with the same error. |
| 121 | # When "error" is a list, the :def function fails with "error[0]" , the script |
| 122 | # fails with "error[1]". |
| 123 | export def CheckDefAndScriptFailure(lines: list<string>, error: any, lnum = -3) |
Bram Moolenaar | 86b3ab4 | 2021-12-19 18:33:23 +0000 | [diff] [blame] | 124 | var errorDef: string |
| 125 | var errorScript: string |
| 126 | if type(error) == v:t_string |
| 127 | errorDef = error |
| 128 | errorScript = error |
| 129 | elseif type(error) == v:t_list && len(error) == 2 |
| 130 | errorDef = error[0] |
| 131 | errorScript = error[1] |
| 132 | else |
| 133 | echoerr 'error argument must be a string or a list with two items' |
| 134 | return |
| 135 | endif |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 136 | CheckDefFailure(lines, errorDef, lnum) |
| 137 | CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) |
| 138 | enddef |
| 139 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 140 | # Check that a command fails when executed in a :def function and when used in |
| 141 | # Vim9 script. |
| 142 | # When "error" is a string, both with the same error. |
| 143 | # When "error" is a list, the :def function fails with "error[0]" , the script |
| 144 | # fails with "error[1]". |
| 145 | export def CheckDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3) |
Bram Moolenaar | 86b3ab4 | 2021-12-19 18:33:23 +0000 | [diff] [blame] | 146 | var errorDef: string |
| 147 | var errorScript: string |
| 148 | if type(error) == v:t_string |
| 149 | errorDef = error |
| 150 | errorScript = error |
| 151 | elseif type(error) == v:t_list && len(error) == 2 |
| 152 | errorDef = error[0] |
| 153 | errorScript = error[1] |
| 154 | else |
| 155 | echoerr 'error argument must be a string or a list with two items' |
| 156 | return |
| 157 | endif |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 158 | CheckDefExecFailure(lines, errorDef, lnum) |
| 159 | CheckScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) |
| 160 | enddef |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 161 | |
| 162 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 163 | # Check that "lines" inside a legacy function has no error. |
| 164 | export func CheckLegacySuccess(lines) |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 165 | let cwd = getcwd() |
| 166 | let fname = 'XlegacySuccess' .. s:sequence |
| 167 | let s:sequence += 1 |
| 168 | call writefile(['func Func()'] + a:lines + ['endfunc'], fname) |
| 169 | try |
| 170 | exe 'so ' .. fname |
| 171 | call Func() |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 172 | finally |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 173 | delfunc! Func |
| 174 | call chdir(cwd) |
| 175 | call delete(fname) |
| 176 | endtry |
| 177 | endfunc |
| 178 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 179 | # Check that "lines" inside a legacy function results in the expected error |
| 180 | export func CheckLegacyFailure(lines, error) |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 181 | let cwd = getcwd() |
| 182 | let fname = 'XlegacyFails' .. s:sequence |
| 183 | let s:sequence += 1 |
| 184 | call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname) |
| 185 | try |
| 186 | call assert_fails('so ' .. fname, a:error) |
| 187 | finally |
| 188 | delfunc! Func |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 189 | call chdir(cwd) |
| 190 | call delete(fname) |
| 191 | endtry |
| 192 | endfunc |
| 193 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 194 | # Execute "lines" in a legacy function, translated as in |
| 195 | # CheckLegacyAndVim9Success() |
| 196 | export def CheckTransLegacySuccess(lines: list<string>) |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 197 | var legacylines = lines->mapnew((_, v) => |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 198 | v->substitute('\<VAR\>', 'let', 'g') |
| 199 | ->substitute('\<LET\>', 'let', 'g') |
| 200 | ->substitute('\<LSTART\>', '{', 'g') |
| 201 | ->substitute('\<LMIDDLE\>', '->', 'g') |
Bram Moolenaar | ef98257 | 2021-08-12 19:27:57 +0200 | [diff] [blame] | 202 | ->substitute('\<LEND\>', '}', 'g') |
Bram Moolenaar | fea43e4 | 2021-12-19 21:34:05 +0000 | [diff] [blame] | 203 | ->substitute('\<TRUE\>', '1', 'g') |
| 204 | ->substitute('\<FALSE\>', '0', 'g') |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 205 | ->substitute('#"', ' "', 'g')) |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 206 | CheckLegacySuccess(legacylines) |
Bram Moolenaar | 3e9c0b9 | 2021-08-12 10:39:10 +0200 | [diff] [blame] | 207 | enddef |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 208 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 209 | export def Vim9Trans(lines: list<string>): list<string> |
Bram Moolenaar | fea43e4 | 2021-12-19 21:34:05 +0000 | [diff] [blame] | 210 | return lines->mapnew((_, v) => |
| 211 | v->substitute('\<VAR\>', 'var', 'g') |
| 212 | ->substitute('\<LET ', '', 'g') |
| 213 | ->substitute('\<LSTART\>', '(', 'g') |
| 214 | ->substitute('\<LMIDDLE\>', ') =>', 'g') |
| 215 | ->substitute(' *\<LEND\> *', '', 'g') |
| 216 | ->substitute('\<TRUE\>', 'true', 'g') |
| 217 | ->substitute('\<FALSE\>', 'false', 'g')) |
| 218 | enddef |
| 219 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 220 | # Execute "lines" in a :def function, translated as in |
| 221 | # CheckLegacyAndVim9Success() |
| 222 | export def CheckTransDefSuccess(lines: list<string>) |
Bram Moolenaar | fea43e4 | 2021-12-19 21:34:05 +0000 | [diff] [blame] | 223 | CheckDefSuccess(Vim9Trans(lines)) |
Bram Moolenaar | 3e9c0b9 | 2021-08-12 10:39:10 +0200 | [diff] [blame] | 224 | enddef |
| 225 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 226 | # Execute "lines" in a Vim9 script, translated as in |
| 227 | # CheckLegacyAndVim9Success() |
| 228 | export def CheckTransVim9Success(lines: list<string>) |
Bram Moolenaar | fea43e4 | 2021-12-19 21:34:05 +0000 | [diff] [blame] | 229 | CheckScriptSuccess(['vim9script'] + Vim9Trans(lines)) |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 230 | enddef |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 231 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 232 | # Execute "lines" in a legacy function, :def function and Vim9 script. |
| 233 | # Use 'VAR' for a declaration. |
| 234 | # Use 'LET' for an assignment |
| 235 | # Use ' #"' for a comment |
| 236 | # Use LSTART arg LMIDDLE expr LEND for lambda |
| 237 | # Use 'TRUE' for 1 in legacy, true in Vim9 |
| 238 | # Use 'FALSE' for 0 in legacy, false in Vim9 |
| 239 | export def CheckLegacyAndVim9Success(lines: list<string>) |
Bram Moolenaar | 3e9c0b9 | 2021-08-12 10:39:10 +0200 | [diff] [blame] | 240 | CheckTransLegacySuccess(lines) |
| 241 | CheckTransDefSuccess(lines) |
| 242 | CheckTransVim9Success(lines) |
| 243 | enddef |
| 244 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 245 | # Execute "lines" in a legacy function, :def function and Vim9 script. |
| 246 | # Use 'VAR' for a declaration. |
| 247 | # Use 'LET' for an assignment |
| 248 | # Use ' #"' for a comment |
| 249 | export def CheckLegacyAndVim9Failure(lines: list<string>, error: any) |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 250 | var legacyError: string |
| 251 | var defError: string |
| 252 | var scriptError: string |
| 253 | |
| 254 | if type(error) == type('string') |
| 255 | legacyError = error |
| 256 | defError = error |
| 257 | scriptError = error |
| 258 | else |
| 259 | legacyError = error[0] |
| 260 | defError = error[1] |
| 261 | scriptError = error[2] |
| 262 | endif |
| 263 | |
| 264 | var legacylines = lines->mapnew((_, v) => |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 265 | v->substitute('\<VAR\>', 'let', 'g') |
| 266 | ->substitute('\<LET\>', 'let', 'g') |
| 267 | ->substitute('#"', ' "', 'g')) |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 268 | CheckLegacyFailure(legacylines, legacyError) |
| 269 | |
| 270 | var vim9lines = lines->mapnew((_, v) => |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 271 | v->substitute('\<VAR\>', 'var', 'g') |
| 272 | ->substitute('\<LET ', '', 'g')) |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 273 | CheckDefExecFailure(vim9lines, defError) |
| 274 | CheckScriptFailure(['vim9script'] + vim9lines, scriptError) |
| 275 | enddef |
Yegappan Lakshmanan | 22697b6 | 2024-04-22 20:58:24 +0200 | [diff] [blame] | 276 | |
| 277 | # :source a list of "lines" and check whether it fails with "error" |
| 278 | export def CheckSourceScriptFailure(lines: list<string>, error: string, lnum = -3) |
| 279 | var cwd = getcwd() |
| 280 | new |
| 281 | setline(1, lines) |
| 282 | var bnr = bufnr() |
| 283 | try |
| 284 | assert_fails('source', error, lines, lnum) |
| 285 | finally |
| 286 | chdir(cwd) |
| 287 | exe $':bw! {bnr}' |
| 288 | endtry |
| 289 | enddef |
| 290 | |
| 291 | # :source a list of "lines" and check whether it fails with the list of |
| 292 | # "errors" |
| 293 | export def CheckSourceScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3) |
| 294 | var cwd = getcwd() |
| 295 | new |
| 296 | var bnr = bufnr() |
| 297 | setline(1, lines) |
| 298 | try |
| 299 | assert_fails('source', errors, lines, lnum) |
| 300 | finally |
| 301 | chdir(cwd) |
| 302 | exe $':bw! {bnr}' |
| 303 | endtry |
| 304 | enddef |
| 305 | |
| 306 | # :source a list of "lines" and check whether it succeeds |
| 307 | export def CheckSourceScriptSuccess(lines: list<string>) |
| 308 | var cwd = getcwd() |
| 309 | new |
| 310 | var bnr = bufnr() |
| 311 | setline(1, lines) |
| 312 | try |
| 313 | :source |
| 314 | finally |
| 315 | chdir(cwd) |
| 316 | exe $':bw! {bnr}' |
| 317 | endtry |
| 318 | enddef |
| 319 | |
| 320 | export def CheckSourceSuccess(lines: list<string>) |
| 321 | CheckSourceScriptSuccess(lines) |
| 322 | enddef |
| 323 | |
| 324 | export def CheckSourceFailure(lines: list<string>, error: string, lnum = -3) |
| 325 | CheckSourceScriptFailure(lines, error, lnum) |
| 326 | enddef |
| 327 | |
| 328 | export def CheckSourceFailureList(lines: list<string>, errors: list<string>, lnum = -3) |
| 329 | CheckSourceScriptFailureList(lines, errors, lnum) |
| 330 | enddef |
| 331 | |
| 332 | # :source a List of "lines" inside a ":def" function and check that no error |
| 333 | # occurs when called. |
| 334 | export func CheckSourceDefSuccess(lines) |
| 335 | let cwd = getcwd() |
| 336 | new |
| 337 | let bnr = bufnr() |
| 338 | call setline(1, ['def Func()'] + a:lines + ['enddef', 'defcompile']) |
| 339 | try |
| 340 | source |
| 341 | call Func() |
| 342 | finally |
| 343 | call chdir(cwd) |
| 344 | delfunc! Func |
| 345 | exe $'bw! {bnr}' |
| 346 | endtry |
| 347 | endfunc |
| 348 | |
| 349 | export def CheckSourceDefAndScriptSuccess(lines: list<string>) |
| 350 | CheckSourceDefSuccess(lines) |
| 351 | CheckSourceScriptSuccess(['vim9script'] + lines) |
| 352 | enddef |
| 353 | |
| 354 | # Check that "lines" inside a ":def" function has no error when compiled. |
| 355 | export func CheckSourceDefCompileSuccess(lines) |
| 356 | let cwd = getcwd() |
| 357 | new |
| 358 | let bnr = bufnr() |
| 359 | call setline(1, ['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile']) |
| 360 | try |
| 361 | source |
| 362 | finally |
| 363 | call chdir(cwd) |
| 364 | delfunc! Func |
| 365 | exe $':bw! {bnr}' |
| 366 | endtry |
| 367 | endfunc |
| 368 | |
| 369 | # Check that "lines" inside ":def" results in an "error" message. |
| 370 | # If "lnum" is given check that the error is reported for this line. |
| 371 | # Add a line before and after to make it less likely that the line number is |
| 372 | # accidentally correct. |
| 373 | export func CheckSourceDefFailure(lines, error, lnum = -3) |
| 374 | let cwd = getcwd() |
| 375 | new |
| 376 | let bnr = bufnr() |
| 377 | call setline(1, ['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile']) |
| 378 | try |
| 379 | call assert_fails('source', a:error, a:lines, a:lnum + 1) |
| 380 | finally |
| 381 | call chdir(cwd) |
| 382 | delfunc! Func |
| 383 | exe $':bw! {bnr}' |
| 384 | endtry |
| 385 | endfunc |
| 386 | |
| 387 | # Check that "lines" inside ":def" results in an "error" message when executed. |
| 388 | # If "lnum" is given check that the error is reported for this line. |
| 389 | # Add a line before and after to make it less likely that the line number is |
| 390 | # accidentally correct. |
| 391 | export func CheckSourceDefExecFailure(lines, error, lnum = -3) |
| 392 | let cwd = getcwd() |
| 393 | new |
| 394 | let bnr = bufnr() |
| 395 | call setline(1, ['def Func()', '# comment'] + a:lines + ['#comment', 'enddef']) |
| 396 | try |
| 397 | source |
| 398 | call assert_fails('call Func()', a:error, a:lines, a:lnum + 1) |
| 399 | finally |
| 400 | call chdir(cwd) |
| 401 | delfunc! Func |
| 402 | exe $':bw! {bnr}' |
| 403 | endtry |
| 404 | endfunc |
| 405 | |
| 406 | # Check that a command fails when used in a :def function and when used in |
| 407 | # Vim9 script. |
| 408 | # When "error" is a string, both with the same error. |
| 409 | # When "error" is a list, the :def function fails with "error[0]" , the script |
| 410 | # fails with "error[1]". |
| 411 | export def CheckSourceDefAndScriptFailure(lines: list<string>, error: any, lnum = -3) |
| 412 | var errorDef: string |
| 413 | var errorScript: string |
| 414 | if type(error) == v:t_string |
| 415 | errorDef = error |
| 416 | errorScript = error |
| 417 | elseif type(error) == v:t_list && len(error) == 2 |
| 418 | errorDef = error[0] |
| 419 | errorScript = error[1] |
| 420 | else |
| 421 | echoerr 'error argument must be a string or a list with two items' |
| 422 | return |
| 423 | endif |
| 424 | CheckSourceDefFailure(lines, errorDef, lnum) |
| 425 | CheckSourceScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) |
| 426 | enddef |
| 427 | |
| 428 | # Check that a command fails when executed in a :def function and when used in |
| 429 | # Vim9 script. |
| 430 | # When "error" is a string, both with the same error. |
| 431 | # When "error" is a list, the :def function fails with "error[0]" , the script |
| 432 | # fails with "error[1]". |
| 433 | export def CheckSourceDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3) |
| 434 | var errorDef: string |
| 435 | var errorScript: string |
| 436 | if type(error) == v:t_string |
| 437 | errorDef = error |
| 438 | errorScript = error |
| 439 | elseif type(error) == v:t_list && len(error) == 2 |
| 440 | errorDef = error[0] |
| 441 | errorScript = error[1] |
| 442 | else |
| 443 | echoerr 'error argument must be a string or a list with two items' |
| 444 | return |
| 445 | endif |
| 446 | CheckSourceDefExecFailure(lines, errorDef, lnum) |
| 447 | CheckSourceScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) |
| 448 | enddef |
| 449 | |