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 | |
Hirohito Higashi | fbe4a8f | 2025-04-27 15:28:30 +0200 | [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 | |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 194 | # Translate "lines" to legacy Vim script |
| 195 | def LegacyTrans(lines: list<string>): list<string> |
| 196 | return lines->mapnew((_, v) => |
| 197 | v->substitute('\<VAR\>', 'let', 'g') |
| 198 | ->substitute('\<LET\>', 'let', 'g') |
| 199 | ->substitute('\<LSTART\>', '{', 'g') |
| 200 | ->substitute('\<LMIDDLE\>', '->', 'g') |
| 201 | ->substitute('\<LEND\>', '}', 'g') |
| 202 | ->substitute('\<TRUE\>', '1', 'g') |
| 203 | ->substitute('\<FALSE\>', '0', 'g') |
| 204 | ->substitute('#"', ' "', 'g')) |
| 205 | enddef |
| 206 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 207 | # Execute "lines" in a legacy function, translated as in |
| 208 | # CheckLegacyAndVim9Success() |
| 209 | export def CheckTransLegacySuccess(lines: list<string>) |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 210 | CheckLegacySuccess(LegacyTrans(lines)) |
Bram Moolenaar | 3e9c0b9 | 2021-08-12 10:39:10 +0200 | [diff] [blame] | 211 | enddef |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 212 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 213 | export def Vim9Trans(lines: list<string>): list<string> |
Bram Moolenaar | fea43e4 | 2021-12-19 21:34:05 +0000 | [diff] [blame] | 214 | return lines->mapnew((_, v) => |
| 215 | v->substitute('\<VAR\>', 'var', 'g') |
| 216 | ->substitute('\<LET ', '', 'g') |
| 217 | ->substitute('\<LSTART\>', '(', 'g') |
| 218 | ->substitute('\<LMIDDLE\>', ') =>', 'g') |
| 219 | ->substitute(' *\<LEND\> *', '', 'g') |
| 220 | ->substitute('\<TRUE\>', 'true', 'g') |
| 221 | ->substitute('\<FALSE\>', 'false', 'g')) |
| 222 | enddef |
| 223 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 224 | # Execute "lines" in a :def function, translated as in |
| 225 | # CheckLegacyAndVim9Success() |
| 226 | export def CheckTransDefSuccess(lines: list<string>) |
Bram Moolenaar | fea43e4 | 2021-12-19 21:34:05 +0000 | [diff] [blame] | 227 | CheckDefSuccess(Vim9Trans(lines)) |
Bram Moolenaar | 3e9c0b9 | 2021-08-12 10:39:10 +0200 | [diff] [blame] | 228 | enddef |
| 229 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 230 | # Execute "lines" in a Vim9 script, translated as in |
| 231 | # CheckLegacyAndVim9Success() |
| 232 | export def CheckTransVim9Success(lines: list<string>) |
Bram Moolenaar | fea43e4 | 2021-12-19 21:34:05 +0000 | [diff] [blame] | 233 | CheckScriptSuccess(['vim9script'] + Vim9Trans(lines)) |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 234 | enddef |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 235 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 236 | # Execute "lines" in a legacy function, :def function and Vim9 script. |
| 237 | # Use 'VAR' for a declaration. |
| 238 | # Use 'LET' for an assignment |
| 239 | # Use ' #"' for a comment |
| 240 | # Use LSTART arg LMIDDLE expr LEND for lambda |
| 241 | # Use 'TRUE' for 1 in legacy, true in Vim9 |
| 242 | # Use 'FALSE' for 0 in legacy, false in Vim9 |
| 243 | export def CheckLegacyAndVim9Success(lines: list<string>) |
Bram Moolenaar | 3e9c0b9 | 2021-08-12 10:39:10 +0200 | [diff] [blame] | 244 | CheckTransLegacySuccess(lines) |
| 245 | CheckTransDefSuccess(lines) |
| 246 | CheckTransVim9Success(lines) |
| 247 | enddef |
| 248 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 249 | # Execute "lines" in a legacy function, :def function and Vim9 script. |
| 250 | # Use 'VAR' for a declaration. |
| 251 | # Use 'LET' for an assignment |
| 252 | # Use ' #"' for a comment |
| 253 | export def CheckLegacyAndVim9Failure(lines: list<string>, error: any) |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 254 | var legacyError: string |
| 255 | var defError: string |
| 256 | var scriptError: string |
| 257 | |
| 258 | if type(error) == type('string') |
| 259 | legacyError = error |
| 260 | defError = error |
| 261 | scriptError = error |
| 262 | else |
| 263 | legacyError = error[0] |
| 264 | defError = error[1] |
| 265 | scriptError = error[2] |
| 266 | endif |
| 267 | |
| 268 | var legacylines = lines->mapnew((_, v) => |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 269 | v->substitute('\<VAR\>', 'let', 'g') |
| 270 | ->substitute('\<LET\>', 'let', 'g') |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 271 | ->substitute('\<LSTART\>', '{', 'g') |
| 272 | ->substitute('\<LMIDDLE\>', '->', 'g') |
| 273 | ->substitute('\<LEND\>', '}', 'g') |
| 274 | ->substitute('\<TRUE\>', '1', 'g') |
| 275 | ->substitute('\<FALSE\>', '0', 'g') |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 276 | ->substitute('#"', ' "', 'g')) |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 277 | CheckLegacyFailure(legacylines, legacyError) |
| 278 | |
| 279 | var vim9lines = lines->mapnew((_, v) => |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 280 | v->substitute('\<VAR\>', 'var', 'g') |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 281 | ->substitute('\<LET ', '', 'g') |
| 282 | ->substitute('\<LSTART\>', '(', 'g') |
| 283 | ->substitute('\<LMIDDLE\>', ') =>', 'g') |
| 284 | ->substitute(' *\<LEND\> *', '', 'g') |
| 285 | ->substitute('\<TRUE\>', 'true', 'g') |
| 286 | ->substitute('\<FALSE\>', 'false', 'g')) |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 287 | CheckDefExecFailure(vim9lines, defError) |
| 288 | CheckScriptFailure(['vim9script'] + vim9lines, scriptError) |
| 289 | enddef |
Yegappan Lakshmanan | 22697b6 | 2024-04-22 20:58:24 +0200 | [diff] [blame] | 290 | |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 291 | # Check that "lines" inside a legacy function has no error. |
| 292 | export func CheckSourceLegacySuccess(lines) |
| 293 | let cwd = getcwd() |
| 294 | new |
| 295 | call setline(1, ['func Func()'] + a:lines + ['endfunc', 'call Func()']) |
| 296 | let bnr = bufnr() |
| 297 | try |
| 298 | :source |
| 299 | finally |
| 300 | delfunc! Func |
| 301 | call chdir(cwd) |
| 302 | exe $':bw! {bnr}' |
| 303 | endtry |
| 304 | endfunc |
| 305 | |
| 306 | # Check that "lines" inside a legacy function results in the expected error |
| 307 | export func CheckSourceLegacyFailure(lines, error) |
| 308 | let cwd = getcwd() |
| 309 | new |
| 310 | call setline(1, ['func Func()'] + a:lines + ['endfunc', 'call Func()']) |
| 311 | let bnr = bufnr() |
| 312 | try |
| 313 | call assert_fails('source', a:error) |
| 314 | finally |
| 315 | delfunc! Func |
| 316 | call chdir(cwd) |
| 317 | exe $':bw! {bnr}' |
| 318 | endtry |
| 319 | endfunc |
| 320 | |
| 321 | # Execute "lines" in a legacy function, translated as in |
| 322 | # CheckSourceLegacyAndVim9Success() |
| 323 | export def CheckSourceTransLegacySuccess(lines: list<string>) |
| 324 | CheckSourceLegacySuccess(LegacyTrans(lines)) |
| 325 | enddef |
| 326 | |
| 327 | # Execute "lines" in a :def function, translated as in |
| 328 | # CheckLegacyAndVim9Success() |
| 329 | export def CheckSourceTransDefSuccess(lines: list<string>) |
| 330 | CheckSourceDefSuccess(Vim9Trans(lines)) |
| 331 | enddef |
| 332 | |
| 333 | # Execute "lines" in a Vim9 script, translated as in |
| 334 | # CheckLegacyAndVim9Success() |
| 335 | export def CheckSourceTransVim9Success(lines: list<string>) |
| 336 | CheckSourceScriptSuccess(['vim9script'] + Vim9Trans(lines)) |
| 337 | enddef |
| 338 | |
| 339 | # Execute "lines" in a legacy function, :def function and Vim9 script. |
| 340 | # Use 'VAR' for a declaration. |
| 341 | # Use 'LET' for an assignment |
| 342 | # Use ' #"' for a comment |
| 343 | # Use LSTART arg LMIDDLE expr LEND for lambda |
| 344 | # Use 'TRUE' for 1 in legacy, true in Vim9 |
| 345 | # Use 'FALSE' for 0 in legacy, false in Vim9 |
| 346 | export def CheckSourceLegacyAndVim9Success(lines: list<string>) |
| 347 | CheckSourceTransLegacySuccess(lines) |
| 348 | CheckSourceTransDefSuccess(lines) |
| 349 | CheckSourceTransVim9Success(lines) |
| 350 | enddef |
| 351 | |
Yegappan Lakshmanan | 22697b6 | 2024-04-22 20:58:24 +0200 | [diff] [blame] | 352 | # :source a list of "lines" and check whether it fails with "error" |
| 353 | export def CheckSourceScriptFailure(lines: list<string>, error: string, lnum = -3) |
| 354 | var cwd = getcwd() |
| 355 | new |
| 356 | setline(1, lines) |
| 357 | var bnr = bufnr() |
| 358 | try |
| 359 | assert_fails('source', error, lines, lnum) |
| 360 | finally |
| 361 | chdir(cwd) |
| 362 | exe $':bw! {bnr}' |
| 363 | endtry |
| 364 | enddef |
| 365 | |
| 366 | # :source a list of "lines" and check whether it fails with the list of |
| 367 | # "errors" |
| 368 | export def CheckSourceScriptFailureList(lines: list<string>, errors: list<string>, lnum = -3) |
| 369 | var cwd = getcwd() |
| 370 | new |
| 371 | var bnr = bufnr() |
| 372 | setline(1, lines) |
| 373 | try |
| 374 | assert_fails('source', errors, lines, lnum) |
| 375 | finally |
| 376 | chdir(cwd) |
| 377 | exe $':bw! {bnr}' |
| 378 | endtry |
| 379 | enddef |
| 380 | |
| 381 | # :source a list of "lines" and check whether it succeeds |
| 382 | export def CheckSourceScriptSuccess(lines: list<string>) |
| 383 | var cwd = getcwd() |
| 384 | new |
| 385 | var bnr = bufnr() |
| 386 | setline(1, lines) |
| 387 | try |
| 388 | :source |
| 389 | finally |
| 390 | chdir(cwd) |
| 391 | exe $':bw! {bnr}' |
| 392 | endtry |
| 393 | enddef |
| 394 | |
Yegappan Lakshmanan | 22697b6 | 2024-04-22 20:58:24 +0200 | [diff] [blame] | 395 | # :source a List of "lines" inside a ":def" function and check that no error |
| 396 | # occurs when called. |
| 397 | export func CheckSourceDefSuccess(lines) |
| 398 | let cwd = getcwd() |
| 399 | new |
| 400 | let bnr = bufnr() |
| 401 | call setline(1, ['def Func()'] + a:lines + ['enddef', 'defcompile']) |
| 402 | try |
| 403 | source |
| 404 | call Func() |
| 405 | finally |
| 406 | call chdir(cwd) |
| 407 | delfunc! Func |
| 408 | exe $'bw! {bnr}' |
| 409 | endtry |
| 410 | endfunc |
| 411 | |
Yegappan Lakshmanan | 22697b6 | 2024-04-22 20:58:24 +0200 | [diff] [blame] | 412 | # Check that "lines" inside a ":def" function has no error when compiled. |
| 413 | export func CheckSourceDefCompileSuccess(lines) |
| 414 | let cwd = getcwd() |
| 415 | new |
| 416 | let bnr = bufnr() |
| 417 | call setline(1, ['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile']) |
| 418 | try |
| 419 | source |
| 420 | finally |
| 421 | call chdir(cwd) |
| 422 | delfunc! Func |
| 423 | exe $':bw! {bnr}' |
| 424 | endtry |
| 425 | endfunc |
| 426 | |
| 427 | # Check that "lines" inside ":def" results in an "error" message. |
| 428 | # If "lnum" is given check that the error is reported for this line. |
| 429 | # Add a line before and after to make it less likely that the line number is |
| 430 | # accidentally correct. |
| 431 | export func CheckSourceDefFailure(lines, error, lnum = -3) |
| 432 | let cwd = getcwd() |
| 433 | new |
| 434 | let bnr = bufnr() |
| 435 | call setline(1, ['def Func()', '# comment'] + a:lines + ['#comment', 'enddef', 'defcompile']) |
| 436 | try |
| 437 | call assert_fails('source', a:error, a:lines, a:lnum + 1) |
| 438 | finally |
| 439 | call chdir(cwd) |
| 440 | delfunc! Func |
| 441 | exe $':bw! {bnr}' |
| 442 | endtry |
| 443 | endfunc |
| 444 | |
| 445 | # Check that "lines" inside ":def" results in an "error" message when executed. |
| 446 | # If "lnum" is given check that the error is reported for this line. |
| 447 | # Add a line before and after to make it less likely that the line number is |
| 448 | # accidentally correct. |
| 449 | export func CheckSourceDefExecFailure(lines, error, lnum = -3) |
| 450 | let cwd = getcwd() |
| 451 | new |
| 452 | let bnr = bufnr() |
| 453 | call setline(1, ['def Func()', '# comment'] + a:lines + ['#comment', 'enddef']) |
| 454 | try |
| 455 | source |
| 456 | call assert_fails('call Func()', a:error, a:lines, a:lnum + 1) |
| 457 | finally |
| 458 | call chdir(cwd) |
| 459 | delfunc! Func |
| 460 | exe $':bw! {bnr}' |
| 461 | endtry |
| 462 | endfunc |
| 463 | |
| 464 | # Check that a command fails when used in a :def function and when used in |
| 465 | # Vim9 script. |
| 466 | # When "error" is a string, both with the same error. |
| 467 | # When "error" is a list, the :def function fails with "error[0]" , the script |
| 468 | # fails with "error[1]". |
| 469 | export def CheckSourceDefAndScriptFailure(lines: list<string>, error: any, lnum = -3) |
| 470 | var errorDef: string |
| 471 | var errorScript: string |
| 472 | if type(error) == v:t_string |
| 473 | errorDef = error |
| 474 | errorScript = error |
| 475 | elseif type(error) == v:t_list && len(error) == 2 |
| 476 | errorDef = error[0] |
| 477 | errorScript = error[1] |
| 478 | else |
| 479 | echoerr 'error argument must be a string or a list with two items' |
| 480 | return |
| 481 | endif |
| 482 | CheckSourceDefFailure(lines, errorDef, lnum) |
| 483 | CheckSourceScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) |
| 484 | enddef |
| 485 | |
| 486 | # Check that a command fails when executed in a :def function and when used in |
| 487 | # Vim9 script. |
| 488 | # When "error" is a string, both with the same error. |
| 489 | # When "error" is a list, the :def function fails with "error[0]" , the script |
| 490 | # fails with "error[1]". |
| 491 | export def CheckSourceDefExecAndScriptFailure(lines: list<string>, error: any, lnum = -3) |
| 492 | var errorDef: string |
| 493 | var errorScript: string |
| 494 | if type(error) == v:t_string |
| 495 | errorDef = error |
| 496 | errorScript = error |
| 497 | elseif type(error) == v:t_list && len(error) == 2 |
| 498 | errorDef = error[0] |
| 499 | errorScript = error[1] |
| 500 | else |
| 501 | echoerr 'error argument must be a string or a list with two items' |
| 502 | return |
| 503 | endif |
| 504 | CheckSourceDefExecFailure(lines, errorDef, lnum) |
| 505 | CheckSourceScriptFailure(['vim9script'] + lines, errorScript, lnum + 1) |
| 506 | enddef |
| 507 | |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 508 | export def CheckSourceSuccess(lines: list<string>) |
| 509 | CheckSourceScriptSuccess(lines) |
| 510 | enddef |
| 511 | |
| 512 | export def CheckSourceFailure(lines: list<string>, error: string, lnum = -3) |
| 513 | CheckSourceScriptFailure(lines, error, lnum) |
| 514 | enddef |
| 515 | |
| 516 | export def CheckSourceFailureList(lines: list<string>, errors: list<string>, lnum = -3) |
| 517 | CheckSourceScriptFailureList(lines, errors, lnum) |
| 518 | enddef |
| 519 | |
| 520 | export def CheckSourceDefAndScriptSuccess(lines: list<string>) |
| 521 | CheckSourceDefSuccess(lines) |
| 522 | CheckSourceScriptSuccess(['vim9script'] + lines) |
| 523 | enddef |
| 524 | |
| 525 | # Execute "lines" in a legacy function, :def function and Vim9 script. |
| 526 | # Use 'VAR' for a declaration. |
| 527 | # Use 'LET' for an assignment |
| 528 | # Use ' #"' for a comment |
| 529 | export def CheckSourceLegacyAndVim9Failure(lines: list<string>, error: any) |
| 530 | var legacyError: string |
| 531 | var defError: string |
| 532 | var scriptError: string |
| 533 | |
| 534 | if type(error) == type('string') |
| 535 | legacyError = error |
| 536 | defError = error |
| 537 | scriptError = error |
| 538 | else |
| 539 | legacyError = error[0] |
| 540 | defError = error[1] |
| 541 | scriptError = error[2] |
| 542 | endif |
| 543 | |
| 544 | CheckSourceLegacyFailure(LegacyTrans(lines), legacyError) |
| 545 | var vim9lines = Vim9Trans(lines) |
| 546 | CheckSourceDefExecFailure(vim9lines, defError) |
| 547 | CheckSourceScriptFailure(['vim9script'] + vim9lines, scriptError) |
| 548 | enddef |
| 549 | |