comparison src/testdir/test_vim9_expr.vim @ 24339:236e9ebdb30e v8.2.2710

patch 8.2.2710: Vim9: not all tests cover script and :def function Commit: https://github.com/vim/vim/commit/90193e6140e5e7f1945e3e144a95697b0e16237a Author: Bram Moolenaar <Bram@vim.org> Date: Sun Apr 4 20:49:50 2021 +0200 patch 8.2.2710: Vim9: not all tests cover script and :def function Problem: Vim9: not all tests cover script and :def function. Solution: Run tests in both if possible. Fix differences.
author Bram Moolenaar <Bram@vim.org>
date Sun, 04 Apr 2021 21:00:04 +0200
parents bd010982f0be
children 21c72f782ae1
comparison
equal deleted inserted replaced
24338:79fa98c31b45 24339:236e9ebdb30e
70 enddef 70 enddef
71 71
72 def Test_expr1_trinary_vimscript() 72 def Test_expr1_trinary_vimscript()
73 # check line continuation 73 # check line continuation
74 var lines =<< trim END 74 var lines =<< trim END
75 vim9script
76 var name = 1 75 var name = 1
77 ? 'yes' 76 ? 'yes'
78 : 'no' 77 : 'no'
79 assert_equal('yes', name) 78 assert_equal('yes', name)
80 END 79 END
81 CheckScriptSuccess(lines) 80 CheckDefAndScriptSuccess(lines)
82 81
83 lines =<< trim END 82 lines =<< trim END
84 vim9script
85 var name = v:false 83 var name = v:false
86 ? 'yes' 84 ? 'yes'
87 : 'no' 85 : 'no'
88 assert_equal('no', name) 86 assert_equal('no', name)
89 END 87 END
90 CheckScriptSuccess(lines) 88 CheckDefAndScriptSuccess(lines)
91 89
92 lines =<< trim END 90 lines =<< trim END
93 vim9script
94 var name = v:false ? 91 var name = v:false ?
95 'yes' : 92 'yes' :
96 'no' 93 'no'
97 assert_equal('no', name) 94 assert_equal('no', name)
98 END 95 END
99 CheckScriptSuccess(lines) 96 CheckDefAndScriptSuccess(lines)
100 97
101 lines =<< trim END 98 lines =<< trim END
102 vim9script
103 var name = v:false ? # comment 99 var name = v:false ? # comment
104 'yes' : 100 'yes' :
105 # comment 101 # comment
106 'no' # comment 102 'no' # comment
107 assert_equal('no', name) 103 assert_equal('no', name)
108 END 104 END
109 CheckScriptSuccess(lines) 105 CheckDefAndScriptSuccess(lines)
110 106
111 # check white space 107 # check white space
112 lines =<< trim END 108 lines =<< trim END
113 vim9script
114 var name = v:true?1:2 109 var name = v:true?1:2
115 END 110 END
116 CheckScriptFailure(lines, 'E1004: White space required before and after ''?'' at "?1:2"', 2) 111 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''?'' at "?1:2"', 1)
117 lines =<< trim END 112
118 vim9script 113 lines =<< trim END
119 var name = v:true? 1 : 2 114 var name = v:true? 1 : 2
120 END 115 END
121 CheckScriptFailure(lines, 'E1004:', 2) 116 CheckDefAndScriptFailure(lines, 'E1004:', 1)
122 lines =<< trim END 117
123 vim9script 118 lines =<< trim END
124 var name = v:true ?1 : 2 119 var name = v:true ?1 : 2
125 END 120 END
126 CheckScriptFailure(lines, 'E1004:', 2) 121 CheckDefAndScriptFailure(lines, 'E1004:', 1)
127 lines =<< trim END 122
128 vim9script 123 lines =<< trim END
129 var name = v:true ? 1: 2 124 var name = v:true ? 1: 2
130 END 125 END
131 CheckScriptFailure(lines, 'E1004: White space required before and after '':'' at ": 2"', 2) 126 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after '':'' at ": 2"', 1)
132 lines =<< trim END 127
133 vim9script 128 lines =<< trim END
134 var name = v:true ? 1 :2 129 var name = v:true ? 1 :2
135 END 130 END
136 CheckScriptFailure(lines, 'E1004:', 2) 131 CheckDefAndScriptFailure(lines, 'E1004:', 1)
137 132
138 lines =<< trim END 133 lines =<< trim END
139 vim9script
140 var name = 'x' ? 1 : 2 134 var name = 'x' ? 1 : 2
141 END 135 END
142 CheckScriptFailure(lines, 'E1135:', 2) 136 CheckDefAndScriptFailure(lines, 'E1135:', 1)
143 137
144 lines =<< trim END 138 lines =<< trim END
145 vim9script
146 var name = [] ? 1 : 2 139 var name = [] ? 1 : 2
147 END 140 END
148 CheckScriptFailure(lines, 'E745:', 2) 141 CheckDefExecAndScriptFailure(lines, 'E745:', 1)
149 142
150 lines =<< trim END 143 lines =<< trim END
151 vim9script
152 var name = {} ? 1 : 2 144 var name = {} ? 1 : 2
153 END 145 END
154 CheckScriptFailure(lines, 'E728:', 2) 146 CheckDefExecAndScriptFailure(lines, 'E728:', 1)
155 147
156 # check after failure eval_flags is reset 148 # check after failure eval_flags is reset
157 lines =<< trim END 149 lines =<< trim END
158 vim9script
159 try 150 try
160 eval('0 ? 1: 2') 151 eval('0 ? 1: 2')
161 catch 152 catch
162 endtry 153 endtry
163 assert_equal(v:true, eval(string(v:true))) 154 assert_equal(v:true, eval(string(v:true)))
164 END 155 END
165 CheckScriptSuccess(lines) 156 CheckDefAndScriptSuccess(lines)
166 157
167 lines =<< trim END 158 lines =<< trim END
168 vim9script
169 try 159 try
170 eval('0 ? 1 :2') 160 eval('0 ? 1 :2')
171 catch 161 catch
172 endtry 162 endtry
173 assert_equal(v:true, eval(string(v:true))) 163 assert_equal(v:true, eval(string(v:true)))
174 END 164 END
175 CheckScriptSuccess(lines) 165 CheckDefAndScriptSuccess(lines)
176 enddef 166 enddef
177 167
178 func Test_expr1_trinary_fails() 168 func Test_expr1_trinary_fails()
179 call CheckDefFailure(["var x = 1 ? 'one'"], "Missing ':' after '?'", 1) 169 call CheckDefAndScriptFailure(["var x = 1 ? 'one'"], "Missing ':' after '?'", 1)
180 170
181 let msg = "White space required before and after '?'" 171 let msg = "White space required before and after '?'"
182 call CheckDefFailure(["var x = 1? 'one' : 'two'"], msg, 1) 172 call CheckDefAndScriptFailure(["var x = 1? 'one' : 'two'"], msg, 1)
183 call CheckDefFailure(["var x = 1 ?'one' : 'two'"], msg, 1) 173 call CheckDefAndScriptFailure(["var x = 1 ?'one' : 'two'"], msg, 1)
184 call CheckDefFailure(["var x = 1?'one' : 'two'"], msg, 1) 174 call CheckDefAndScriptFailure(["var x = 1?'one' : 'two'"], msg, 1)
185 175
186 let msg = "White space required before and after ':'" 176 let msg = "White space required before and after ':'"
187 call CheckDefFailure(["var x = 1 ? 'one': 'two'"], msg, 1) 177 call CheckDefAndScriptFailure(["var x = 1 ? 'one': 'two'"], msg, 1)
188 call CheckDefFailure(["var x = 1 ? 'one' :'two'"], msg, 1) 178 call CheckDefAndScriptFailure(["var x = 1 ? 'one' :'two'"], msg, 1)
189 call CheckDefFailure(["var x = 1 ? 'one':'two'"], msg, 1) 179 call CheckDefAndScriptFailure(["var x = 1 ? 'one':'two'"], msg, 1)
190 180
191 call CheckDefFailure(["var x = 'x' ? 'one' : 'two'"], 'E1135:', 1) 181 call CheckDefAndScriptFailure(["var x = 'x' ? 'one' : 'two'"], 'E1135:', 1)
192 call CheckDefFailure(["var x = 0z1234 ? 'one' : 'two'"], 'E974:', 1) 182 call CheckDefAndScriptFailure(["var x = 0z1234 ? 'one' : 'two'"], 'E974:', 1)
193 call CheckDefExecFailure(["var x = [] ? 'one' : 'two'"], 'E745:', 1) 183 call CheckDefExecAndScriptFailure(["var x = [] ? 'one' : 'two'"], 'E745:', 1)
194 call CheckDefExecFailure(["var x = {} ? 'one' : 'two'"], 'E728:', 1) 184 call CheckDefExecAndScriptFailure(["var x = {} ? 'one' : 'two'"], 'E728:', 1)
195 185
196 call CheckDefExecFailure(["var x = false ? "], 'E1097:', 3) 186 call CheckDefExecFailure(["var x = false ? "], 'E1097:', 3)
187 call CheckScriptFailure(['vim9script', "var x = false ? "], 'E15:', 2)
197 call CheckDefExecFailure(["var x = false ? 'one' : "], 'E1097:', 3) 188 call CheckDefExecFailure(["var x = false ? 'one' : "], 'E1097:', 3)
198 189 call CheckScriptFailure(['vim9script', "var x = false ? 'one' : "], 'E15:', 2)
199 call CheckDefExecFailure(["var x = true ? xxx : 'foo'"], 'E1001:', 1) 190
200 call CheckDefExecFailure(["var x = false ? 'foo' : xxx"], 'E1001:', 1) 191 call CheckDefExecAndScriptFailure2(["var x = true ? xxx : 'foo'"], 'E1001:', 'E121:', 1)
192 call CheckDefExecAndScriptFailure2(["var x = false ? 'foo' : xxx"], 'E1001:', 'E121:', 1)
201 193
202 if has('float') 194 if has('float')
203 call CheckDefFailure(["var x = 0.1 ? 'one' : 'two'"], 'E805:', 1) 195 call CheckDefAndScriptFailure(["var x = 0.1 ? 'one' : 'two'"], 'E805:', 1)
204 endif 196 endif
205 197
206 " missing argument detected even when common type is used 198 " missing argument detected even when common type is used
207 call CheckDefFailure([ 199 call CheckDefAndScriptFailure([
208 \ 'var X = FuncOne', 200 \ 'var X = FuncOne',
209 \ 'var Y = FuncTwo', 201 \ 'var Y = FuncTwo',
210 \ 'var Z = g:cond ? FuncOne : FuncTwo', 202 \ 'var Z = g:cond ? FuncOne : FuncTwo',
211 \ 'Z()'], 'E119:', 4) 203 \ 'Z()'], 'E119:', 4)
212 endfunc 204 endfunc
232 endif 224 endif
233 END 225 END
234 CheckDefAndScriptSuccess(lines) 226 CheckDefAndScriptSuccess(lines)
235 227
236 var msg = "White space required before and after '??'" 228 var msg = "White space required before and after '??'"
237 call CheckDefFailure(["var x = 1?? 'one' : 'two'"], msg, 1) 229 call CheckDefAndScriptFailure(["var x = 1?? 'one' : 'two'"], msg, 1)
238 call CheckDefFailure(["var x = 1 ??'one' : 'two'"], msg, 1) 230 call CheckDefAndScriptFailure(["var x = 1 ??'one' : 'two'"], msg, 1)
239 call CheckDefFailure(["var x = 1??'one' : 'two'"], msg, 1) 231 call CheckDefAndScriptFailure(["var x = 1??'one' : 'two'"], msg, 1)
240 enddef 232 enddef
241 233
242 def Record(val: any): any 234 def Record(val: any): any
243 g:vals->add(val) 235 g:vals->add(val)
244 return val 236 return val
295 enddef 287 enddef
296 288
297 def Test_expr2_vimscript() 289 def Test_expr2_vimscript()
298 # check line continuation 290 # check line continuation
299 var lines =<< trim END 291 var lines =<< trim END
300 vim9script
301 var name = 0 292 var name = 0
302 || 1 293 || 1
303 assert_equal(true, name) 294 assert_equal(true, name)
304 END 295 END
305 CheckScriptSuccess(lines) 296 CheckDefAndScriptSuccess(lines)
306 297
307 lines =<< trim END 298 lines =<< trim END
308 vim9script
309 var name = v:false 299 var name = v:false
310 || v:true 300 || v:true
311 || v:false 301 || v:false
312 assert_equal(v:true, name) 302 assert_equal(v:true, name)
313 END 303 END
314 CheckScriptSuccess(lines) 304 CheckDefAndScriptSuccess(lines)
315 305
316 lines =<< trim END 306 lines =<< trim END
317 vim9script
318 var name = v:false || 307 var name = v:false ||
319 v:true || 308 v:true ||
320 v:false 309 v:false
321 assert_equal(v:true, name) 310 assert_equal(v:true, name)
322 END 311 END
323 CheckScriptSuccess(lines) 312 CheckDefAndScriptSuccess(lines)
324 313
325 lines =<< trim END 314 lines =<< trim END
326 vim9script
327 var name = v:false || # comment 315 var name = v:false || # comment
328 # comment 316 # comment
329 v:true || 317 v:true ||
330 # comment 318 # comment
331 v:false # comment 319 v:false # comment
332 assert_equal(v:true, name) 320 assert_equal(v:true, name)
333 END 321 END
334 CheckScriptSuccess(lines) 322 CheckDefAndScriptSuccess(lines)
335 323
336 # check white space 324 # check white space
337 lines =<< trim END 325 lines =<< trim END
338 vim9script
339 var name = v:true||v:true 326 var name = v:true||v:true
340 END 327 END
341 CheckScriptFailure(lines, 'E1004: White space required before and after ''||'' at "||v:true"', 2) 328 CheckDefExecAndScriptFailure(lines, 'E1004: White space required before and after ''||'' at "||v:true"', 1)
342 lines =<< trim END 329
343 vim9script 330 lines =<< trim END
344 var name = v:true ||v:true 331 var name = v:true ||v:true
345 END 332 END
346 CheckScriptFailure(lines, 'E1004:', 2) 333 CheckDefAndScriptFailure(lines, 'E1004:', 1)
347 lines =<< trim END 334
348 vim9script 335 lines =<< trim END
349 var name = v:true|| v:true 336 var name = v:true|| v:true
350 END 337 END
351 CheckScriptFailure(lines, 'E1004:', 2) 338 CheckDefAndScriptFailure(lines, 'E1004:', 1)
352 enddef 339 enddef
353 340
354 def Test_expr2_fails() 341 def Test_expr2_fails()
355 var msg = "White space required before and after '||'" 342 var msg = "White space required before and after '||'"
356 call CheckDefFailure(["var x = 1||2"], msg, 1) 343 call CheckDefAndScriptFailure(["var x = 1||2"], msg, 1)
357 call CheckDefFailure(["var x = 1 ||2"], msg, 1) 344 call CheckDefAndScriptFailure(["var x = 1 ||2"], msg, 1)
358 call CheckDefFailure(["var x = 1|| 2"], msg, 1) 345 call CheckDefAndScriptFailure(["var x = 1|| 2"], msg, 1)
359 346
360 call CheckDefFailure(["var x = false || "], 'E1097:', 3) 347 call CheckDefFailure(["var x = false || "], 'E1097:', 3)
361 348 call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2)
349
350 # script does not fail, the second expression is skipped
362 call CheckDefFailure(["var x = 1 || xxx"], 'E1001:', 1) 351 call CheckDefFailure(["var x = 1 || xxx"], 'E1001:', 1)
363 call CheckDefFailure(["var x = [] || false"], 'E1012:', 1) 352
364 call CheckDefFailure(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 1) 353 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012:', 'E745:', 1)
354
355 call CheckDefAndScriptFailure2(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1)
365 356
366 # TODO: should fail at compile time 357 # TODO: should fail at compile time
367 call CheckDefExecFailure(["var x = 3 || 7"], 'E1023:', 1) 358 call CheckDefExecAndScriptFailure(["var x = 3 || 7"], 'E1023:', 1)
368 call CheckScriptFailure(["vim9script", "var x = 3 || 7"], 'E1023:', 2) 359
369 call CheckScriptFailure(["vim9script", "var x = [] || false"], 'E745:', 2) 360 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012: Type mismatch; expected bool but got list<unknown>', 'E745:', 1)
361
370 enddef 362 enddef
371 363
372 " test && 364 " test &&
373 def Test_expr3() 365 def Test_expr3()
374 var lines =<< trim END 366 var lines =<< trim END
411 enddef 403 enddef
412 404
413 def Test_expr3_vimscript() 405 def Test_expr3_vimscript()
414 # check line continuation 406 # check line continuation
415 var lines =<< trim END 407 var lines =<< trim END
416 vim9script
417 var name = 0 408 var name = 0
418 && 1 409 && 1
419 assert_equal(false, name) 410 assert_equal(false, name)
420 END 411 END
421 CheckScriptSuccess(lines) 412 CheckDefAndScriptSuccess(lines)
422 413
423 lines =<< trim END 414 lines =<< trim END
424 vim9script
425 var name = v:true 415 var name = v:true
426 && v:true 416 && v:true
427 && v:true 417 && v:true
428 assert_equal(v:true, name) 418 assert_equal(v:true, name)
429 END 419 END
430 CheckScriptSuccess(lines) 420 CheckDefAndScriptSuccess(lines)
431 421
432 lines =<< trim END 422 lines =<< trim END
433 vim9script
434 var name = v:true && 423 var name = v:true &&
435 v:true && 424 v:true &&
436 v:true 425 v:true
437 assert_equal(v:true, name) 426 assert_equal(v:true, name)
438 END 427 END
439 CheckScriptSuccess(lines) 428 CheckDefAndScriptSuccess(lines)
440 429
441 lines =<< trim END 430 lines =<< trim END
442 vim9script
443 var name = v:true && # comment 431 var name = v:true && # comment
444 # comment 432 # comment
445 v:true && 433 v:true &&
446 # comment 434 # comment
447 v:true 435 v:true
448 assert_equal(v:true, name) 436 assert_equal(v:true, name)
449 END 437 END
450 CheckScriptSuccess(lines) 438 CheckDefAndScriptSuccess(lines)
451 439
452 # check white space 440 # check white space
453 lines =<< trim END 441 lines =<< trim END
454 vim9script
455 var name = v:true&&v:true 442 var name = v:true&&v:true
456 END 443 END
457 CheckScriptFailure(lines, 'E1004:', 2) 444 CheckDefAndScriptFailure(lines, 'E1004:', 1)
458 lines =<< trim END 445
459 vim9script 446 lines =<< trim END
460 var name = v:true &&v:true 447 var name = v:true &&v:true
461 END 448 END
462 CheckScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&v:true"', 2) 449 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&v:true"', 1)
463 lines =<< trim END 450
464 vim9script 451 lines =<< trim END
465 var name = v:true&& v:true 452 var name = v:true&& v:true
466 END 453 END
467 CheckScriptFailure(lines, 'E1004:', 2) 454 CheckDefAndScriptFailure(lines, 'E1004:', 1)
468 enddef 455 enddef
469 456
470 def Test_expr3_fails() 457 def Test_expr3_fails()
471 var msg = "White space required before and after '&&'" 458 var msg = "White space required before and after '&&'"
472 CheckDefFailure(["var x = 1&&2"], msg, 1) 459 CheckDefAndScriptFailure(["var x = 1&&2"], msg, 1)
473 CheckDefFailure(["var x = 1 &&2"], msg, 1) 460 CheckDefAndScriptFailure(["var x = 1 &&2"], msg, 1)
474 CheckDefFailure(["var x = 1&& 2"], msg, 1) 461 CheckDefAndScriptFailure(["var x = 1&& 2"], msg, 1)
475 462
476 CheckDefFailure(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 1) 463 g:vals = []
477 464 CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1)
478 CheckDefExecFailure(['assert_equal(false, Record(1) && Record(4) && Record(0))'], 'E1023: Using a Number as a Bool: 4', 1) 465
466 CheckDefExecAndScriptFailure(['assert_equal(false, Record(1) && Record(4) && Record(0))'], 'E1023: Using a Number as a Bool: 4', 1)
479 467
480 var lines =<< trim END 468 var lines =<< trim END
481 if 3 469 if 3
482 && true 470 && true
483 endif 471 endif
484 END 472 END
485 CheckDefExecFailure(lines, 'E1023:', 1) 473 CheckDefExecAndScriptFailure(lines, 'E1023:', 1)
486 474
487 lines =<< trim END 475 lines =<< trim END
488 if 'yes' 476 if 'yes'
489 && true 477 && true
490 endif 478 endif
491 END 479 END
492 CheckDefFailure(lines, 'E1012:', 1) 480 CheckDefAndScriptFailure2(lines, 'E1012:', 'E1135: Using a String as a Bool', 1)
493 enddef 481 enddef
494 482
495 " global variables to use for tests with the "any" type 483 " global variables to use for tests with the "any" type
496 let atrue = v:true 484 let atrue = v:true
497 let afalse = v:false 485 let afalse = v:false
605 assert_equal(true, OneFunc('abc') == TwoFunc('123')) 593 assert_equal(true, OneFunc('abc') == TwoFunc('123'))
606 endif 594 endif
607 END 595 END
608 CheckDefAndScriptSuccess(lines) 596 CheckDefAndScriptSuccess(lines)
609 597
610 CheckDefFailure(["var x = 'a' == xxx"], 'E1001:', 1) 598 CheckDefAndScriptFailure2(["var x = 'a' == xxx"], 'E1001:', 'E121:', 1)
611 CheckDefFailure(["var x = 'a' == "], 'E1097:', 3) 599 CheckDefFailure(["var x = 'a' == "], 'E1097:', 3)
612 600 CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2)
613 CheckDefExecFailure(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 4) 601
614 602 CheckDefExecAndScriptFailure2(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4)
615 CheckDefExecFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2) 603
616 CheckDefExecFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2) 604 CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2)
617 CheckDefExecFailure(["var x: any = 99", 'echo x == true'], 'E1138', 2) 605 CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2)
618 CheckDefExecFailure(["var x: any = 'a'", 'echo x == 99'], 'E1030:', 2) 606 CheckDefExecAndScriptFailure2(["var x: any = 99", 'echo x == true'], 'E1138', 'E1072:', 2)
607 CheckDefExecAndScriptFailure2(["var x: any = 'a'", 'echo x == 99'], 'E1030:', 'E1072:', 2)
619 608
620 for op in ['>', '>=', '<', '<=', '=~', '!~'] 609 for op in ['>', '>=', '<', '<=', '=~', '!~']
621 CheckDefExecFailure([ 610 CheckDefExecAndScriptFailure([
622 "var a: any = 'a'", 611 "var a: any = 'a'",
623 'var b: any = true', 612 'var b: any = true',
624 'echo a ' .. op .. ' b'], 'E1072:', 3) 613 'echo a ' .. op .. ' b'], 'E1072:', 3)
625 endfor 614 endfor
626 enddef 615 enddef
875 enddef 864 enddef
876 865
877 def Test_expr4_vim9script() 866 def Test_expr4_vim9script()
878 # check line continuation 867 # check line continuation
879 var lines =<< trim END 868 var lines =<< trim END
880 vim9script
881 var name = 0 869 var name = 0
882 < 1 870 < 1
883 assert_equal(true, name) 871 assert_equal(true, name)
884 END 872 END
885 CheckScriptSuccess(lines) 873 CheckDefAndScriptSuccess(lines)
886 874
887 lines =<< trim END 875 lines =<< trim END
888 vim9script
889 var name = 123 876 var name = 123
890 # comment 877 # comment
891 != 123 878 != 123
892 assert_equal(false, name) 879 assert_equal(false, name)
893 END 880 END
894 CheckScriptSuccess(lines) 881 CheckDefAndScriptSuccess(lines)
895 882
896 lines =<< trim END 883 lines =<< trim END
897 vim9script
898 var name = 123 == 884 var name = 123 ==
899 123 885 123
900 assert_equal(true, name) 886 assert_equal(true, name)
901 END 887 END
902 CheckScriptSuccess(lines) 888 CheckDefAndScriptSuccess(lines)
903 889
904 lines =<< trim END 890 lines =<< trim END
905 vim9script
906 var list = [1, 2, 3] 891 var list = [1, 2, 3]
907 var name = list 892 var name = list
908 is list 893 is list
909 assert_equal(true, name) 894 assert_equal(true, name)
910 END 895 END
911 CheckScriptSuccess(lines) 896 CheckDefAndScriptSuccess(lines)
912 897
913 lines =<< trim END 898 lines =<< trim END
914 vim9script
915 var list = [1, 2, 3] 899 var list = [1, 2, 3]
916 var name = list # comment 900 var name = list # comment
917 # comment 901 # comment
918 is list 902 is list
919 assert_equal(true, name) 903 assert_equal(true, name)
920 END 904 END
921 CheckScriptSuccess(lines) 905 CheckDefAndScriptSuccess(lines)
922 906
923 lines =<< trim END 907 lines =<< trim END
924 vim9script
925 var myblob = 0z1234 908 var myblob = 0z1234
926 var name = myblob 909 var name = myblob
927 isnot 0z11 910 isnot 0z11
928 assert_equal(true, name) 911 assert_equal(true, name)
929 END 912 END
930 CheckScriptSuccess(lines) 913 CheckDefAndScriptSuccess(lines)
931 914
932 # spot check mismatching types 915 # spot check mismatching types
933 lines =<< trim END 916 lines =<< trim END
934 vim9script
935 echo '' == 0 917 echo '' == 0
936 END 918 END
937 CheckScriptFailure(lines, 'E1072:', 2) 919 CheckDefAndScriptFailure(lines, 'E1072:', 1)
938 920
939 lines =<< trim END 921 lines =<< trim END
940 vim9script
941 echo v:true > v:false 922 echo v:true > v:false
942 END 923 END
943 CheckScriptFailure(lines, 'Cannot compare bool with bool', 2) 924 CheckDefAndScriptFailure(lines, 'Cannot compare bool with bool', 1)
944 925
945 lines =<< trim END 926 lines =<< trim END
946 vim9script
947 echo 123 is 123 927 echo 123 is 123
948 END 928 END
949 CheckScriptFailure(lines, 'Cannot use "is" with number', 2) 929 CheckDefAndScriptFailure(lines, 'Cannot use "is" with number', 1)
950 930
951 # check missing white space 931 # check missing white space
952 lines =<< trim END 932 lines =<< trim END
953 vim9script
954 echo 2>3 933 echo 2>3
955 END 934 END
956 CheckScriptFailure(lines, 'E1004: White space required before and after ''>'' at ">3"', 2) 935 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''>'' at ">3"', 1)
957 lines =<< trim END 936
958 vim9script 937 lines =<< trim END
959 echo 2 >3 938 echo 2 >3
960 END 939 END
961 CheckScriptFailure(lines, 'E1004:', 2) 940 CheckDefAndScriptFailure(lines, 'E1004:', 1)
962 lines =<< trim END 941
963 vim9script 942 lines =<< trim END
964 echo 2> 3 943 echo 2> 3
965 END 944 END
966 CheckScriptFailure(lines, 'E1004:', 2) 945 CheckDefAndScriptFailure(lines, 'E1004:', 1)
967 lines =<< trim END 946
968 vim9script 947 lines =<< trim END
969 echo 2!=3 948 echo 2!=3
970 END 949 END
971 CheckScriptFailure(lines, 'E1004:', 2) 950 CheckDefAndScriptFailure(lines, 'E1004:', 1)
972 lines =<< trim END 951
973 vim9script 952 lines =<< trim END
974 echo 2 !=3 953 echo 2 !=3
975 END 954 END
976 CheckScriptFailure(lines, 'E1004: White space required before and after ''!'' at "!=3"', 2) 955 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''!='' at "!=3"', 1)
977 lines =<< trim END 956
978 vim9script 957 lines =<< trim END
979 echo 2!= 3 958 echo 2!= 3
980 END 959 END
981 CheckScriptFailure(lines, 'E1004:', 2) 960 CheckDefAndScriptFailure(lines, 'E1004:', 1)
982 961
983 lines =<< trim END 962 lines =<< trim END
984 vim9script
985 echo len('xxx') == 3 963 echo len('xxx') == 3
986 END 964 END
987 CheckScriptSuccess(lines) 965 CheckDefAndScriptSuccess(lines)
988 966
989 lines =<< trim END 967 lines =<< trim END
990 vim9script
991 var line = 'abc' 968 var line = 'abc'
992 echo line[1] =~ '\w' 969 echo line[1] =~ '\w'
993 END 970 END
994 CheckScriptSuccess(lines) 971 CheckDefAndScriptSuccess(lines)
995 enddef 972 enddef
996 973
997 func Test_expr4_fails() 974 func Test_expr4_fails()
998 let msg = "White space required before and after '>'" 975 let msg = "White space required before and after '>'"
999 call CheckDefFailure(["var x = 1>2"], msg, 1) 976 call CheckDefAndScriptFailure(["var x = 1>2"], msg, 1)
1000 call CheckDefFailure(["var x = 1 >2"], msg, 1) 977 call CheckDefAndScriptFailure(["var x = 1 >2"], msg, 1)
1001 call CheckDefFailure(["var x = 1> 2"], msg, 1) 978 call CheckDefAndScriptFailure(["var x = 1> 2"], msg, 1)
1002 979
1003 let msg = "White space required before and after '=='" 980 let msg = "White space required before and after '=='"
1004 call CheckDefFailure(["var x = 1==2"], msg, 1) 981 call CheckDefAndScriptFailure(["var x = 1==2"], msg, 1)
1005 call CheckDefFailure(["var x = 1 ==2"], msg, 1) 982 call CheckDefAndScriptFailure(["var x = 1 ==2"], msg, 1)
1006 call CheckDefFailure(["var x = 1== 2"], msg, 1) 983 call CheckDefAndScriptFailure(["var x = 1== 2"], msg, 1)
1007 984
1008 let msg = "White space required before and after 'is'" 985 let msg = "White space required before and after 'is'"
1009 call CheckDefFailure(["var x = '1'is'2'"], msg, 1) 986 call CheckDefAndScriptFailure(["var x = '1'is'2'"], msg, 1)
1010 call CheckDefFailure(["var x = '1' is'2'"], msg, 1) 987 call CheckDefAndScriptFailure(["var x = '1' is'2'"], msg, 1)
1011 call CheckDefFailure(["var x = '1'is '2'"], msg, 1) 988 call CheckDefAndScriptFailure(["var x = '1'is '2'"], msg, 1)
1012 989
1013 let msg = "White space required before and after 'isnot'" 990 let msg = "White space required before and after 'isnot'"
1014 call CheckDefFailure(["var x = '1'isnot'2'"], msg, 1) 991 call CheckDefAndScriptFailure(["var x = '1'isnot'2'"], msg, 1)
1015 call CheckDefFailure(["var x = '1' isnot'2'"], msg, 1) 992 call CheckDefAndScriptFailure(["var x = '1' isnot'2'"], msg, 1)
1016 call CheckDefFailure(["var x = '1'isnot '2'"], msg, 1) 993 call CheckDefAndScriptFailure(["var x = '1'isnot '2'"], msg, 1)
1017 994
1018 call CheckDefFailure(["var x = 1 is# 2"], 'E15:', 1) 995 call CheckDefAndScriptFailure(["var x = 1 is# 2"], 'E15:', 1)
1019 call CheckDefFailure(["var x = 1 is? 2"], 'E15:', 1) 996 call CheckDefAndScriptFailure(["var x = 1 is? 2"], 'E15:', 1)
1020 call CheckDefFailure(["var x = 1 isnot# 2"], 'E15:', 1) 997 call CheckDefAndScriptFailure(["var x = 1 isnot# 2"], 'E15:', 1)
1021 call CheckDefFailure(["var x = 1 isnot? 2"], 'E15:', 1) 998 call CheckDefAndScriptFailure(["var x = 1 isnot? 2"], 'E15:', 1)
1022 999
1023 call CheckDefFailure(["var x = 1 == '2'"], 'Cannot compare number with string', 1) 1000 call CheckDefAndScriptFailure(["var x = 1 == '2'"], 'Cannot compare number with string', 1)
1024 call CheckDefFailure(["var x = '1' == 2"], 'Cannot compare string with number', 1) 1001 call CheckDefAndScriptFailure(["var x = '1' == 2"], 'Cannot compare string with number', 1)
1025 call CheckDefFailure(["var x = 1 == RetVoid()"], 'Cannot compare number with void', 1) 1002 call CheckDefAndScriptFailure(["var x = 1 == RetVoid()"], 'Cannot compare number with void', 1)
1026 call CheckDefFailure(["var x = RetVoid() == 1"], 'Cannot compare void with number', 1) 1003 call CheckDefAndScriptFailure(["var x = RetVoid() == 1"], 'Cannot compare void with number', 1)
1027 1004
1028 call CheckDefFailure(["var x = true > false"], 'Cannot compare bool with bool', 1) 1005 call CheckDefAndScriptFailure(["var x = true > false"], 'Cannot compare bool with bool', 1)
1029 call CheckDefFailure(["var x = true >= false"], 'Cannot compare bool with bool', 1) 1006 call CheckDefAndScriptFailure(["var x = true >= false"], 'Cannot compare bool with bool', 1)
1030 call CheckDefFailure(["var x = true < false"], 'Cannot compare bool with bool', 1) 1007 call CheckDefAndScriptFailure(["var x = true < false"], 'Cannot compare bool with bool', 1)
1031 call CheckDefFailure(["var x = true <= false"], 'Cannot compare bool with bool', 1) 1008 call CheckDefAndScriptFailure(["var x = true <= false"], 'Cannot compare bool with bool', 1)
1032 call CheckDefFailure(["var x = true =~ false"], 'Cannot compare bool with bool', 1) 1009 call CheckDefAndScriptFailure(["var x = true =~ false"], 'Cannot compare bool with bool', 1)
1033 call CheckDefFailure(["var x = true !~ false"], 'Cannot compare bool with bool', 1) 1010 call CheckDefAndScriptFailure(["var x = true !~ false"], 'Cannot compare bool with bool', 1)
1034 call CheckDefFailure(["var x = true is false"], 'Cannot use "is" with bool', 1) 1011 call CheckDefAndScriptFailure(["var x = true is false"], 'Cannot use "is" with bool', 1)
1035 call CheckDefFailure(["var x = true isnot false"], 'Cannot use "isnot" with bool', 1) 1012 call CheckDefAndScriptFailure(["var x = true isnot false"], 'Cannot use "isnot" with bool', 1)
1036 1013
1037 call CheckDefFailure(["var x = v:none is v:null"], 'Cannot use "is" with special', 1) 1014 call CheckDefAndScriptFailure(["var x = v:none is v:null"], 'Cannot use "is" with special', 1)
1038 call CheckDefFailure(["var x = v:none isnot v:null"], 'Cannot use "isnot" with special', 1) 1015 call CheckDefAndScriptFailure(["var x = v:none isnot v:null"], 'Cannot use "isnot" with special', 1)
1039 call CheckDefFailure(["var x = 123 is 123"], 'Cannot use "is" with number', 1) 1016 call CheckDefAndScriptFailure(["var x = 123 is 123"], 'Cannot use "is" with number', 1)
1040 call CheckDefFailure(["var x = 123 isnot 123"], 'Cannot use "isnot" with number', 1) 1017 call CheckDefAndScriptFailure(["var x = 123 isnot 123"], 'Cannot use "isnot" with number', 1)
1041 if has('float') 1018 if has('float')
1042 call CheckDefFailure(["var x = 1.3 is 1.3"], 'Cannot use "is" with float', 1) 1019 call CheckDefAndScriptFailure(["var x = 1.3 is 1.3"], 'Cannot use "is" with float', 1)
1043 call CheckDefFailure(["var x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float', 1) 1020 call CheckDefAndScriptFailure(["var x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float', 1)
1044 endif 1021 endif
1045 1022
1046 call CheckDefFailure(["var x = 0za1 > 0z34"], 'Cannot compare blob with blob', 1) 1023 call CheckDefAndScriptFailure(["var x = 0za1 > 0z34"], 'Cannot compare blob with blob', 1)
1047 call CheckDefFailure(["var x = 0za1 >= 0z34"], 'Cannot compare blob with blob', 1) 1024 call CheckDefAndScriptFailure(["var x = 0za1 >= 0z34"], 'Cannot compare blob with blob', 1)
1048 call CheckDefFailure(["var x = 0za1 < 0z34"], 'Cannot compare blob with blob', 1) 1025 call CheckDefAndScriptFailure(["var x = 0za1 < 0z34"], 'Cannot compare blob with blob', 1)
1049 call CheckDefFailure(["var x = 0za1 <= 0z34"], 'Cannot compare blob with blob', 1) 1026 call CheckDefAndScriptFailure(["var x = 0za1 <= 0z34"], 'Cannot compare blob with blob', 1)
1050 call CheckDefFailure(["var x = 0za1 =~ 0z34"], 'Cannot compare blob with blob', 1) 1027 call CheckDefAndScriptFailure(["var x = 0za1 =~ 0z34"], 'Cannot compare blob with blob', 1)
1051 call CheckDefFailure(["var x = 0za1 !~ 0z34"], 'Cannot compare blob with blob', 1) 1028 call CheckDefAndScriptFailure(["var x = 0za1 !~ 0z34"], 'Cannot compare blob with blob', 1)
1052 1029
1053 call CheckDefFailure(["var x = [13] > [88]"], 'Cannot compare list with list', 1) 1030 call CheckDefAndScriptFailure(["var x = [13] > [88]"], 'Cannot compare list with list', 1)
1054 call CheckDefFailure(["var x = [13] >= [88]"], 'Cannot compare list with list', 1) 1031 call CheckDefAndScriptFailure(["var x = [13] >= [88]"], 'Cannot compare list with list', 1)
1055 call CheckDefFailure(["var x = [13] < [88]"], 'Cannot compare list with list', 1) 1032 call CheckDefAndScriptFailure(["var x = [13] < [88]"], 'Cannot compare list with list', 1)
1056 call CheckDefFailure(["var x = [13] <= [88]"], 'Cannot compare list with list', 1) 1033 call CheckDefAndScriptFailure(["var x = [13] <= [88]"], 'Cannot compare list with list', 1)
1057 call CheckDefFailure(["var x = [13] =~ [88]"], 'Cannot compare list with list', 1) 1034 call CheckDefAndScriptFailure(["var x = [13] =~ [88]"], 'Cannot compare list with list', 1)
1058 call CheckDefFailure(["var x = [13] !~ [88]"], 'Cannot compare list with list', 1) 1035 call CheckDefAndScriptFailure(["var x = [13] !~ [88]"], 'Cannot compare list with list', 1)
1059 1036
1060 call CheckDefFailure(['var j: job', 'var chan: channel', 'var r = j == chan'], 'Cannot compare job with channel', 3) 1037 call CheckDefAndScriptFailure(['var j: job', 'var chan: channel', 'var r = j == chan'], 'Cannot compare job with channel', 3)
1061 call CheckDefFailure(['var j: job', 'var x: list<any>', 'var r = j == x'], 'Cannot compare job with list', 3) 1038 call CheckDefAndScriptFailure(['var j: job', 'var x: list<any>', 'var r = j == x'], 'Cannot compare job with list', 3)
1062 call CheckDefFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3) 1039 call CheckDefAndScriptFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3)
1063 call CheckDefFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3) 1040 call CheckDefAndScriptFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3)
1064 endfunc 1041 endfunc
1065 1042
1066 " test addition, subtraction, concatenation 1043 " test addition, subtraction, concatenation
1067 def Test_expr5() 1044 def Test_expr5()
1068 var lines =<< trim END 1045 var lines =<< trim END
1130 enddef 1107 enddef
1131 1108
1132 def Test_expr5_vim9script() 1109 def Test_expr5_vim9script()
1133 # check line continuation 1110 # check line continuation
1134 var lines =<< trim END 1111 var lines =<< trim END
1135 vim9script
1136 var name = 11 1112 var name = 11
1137 + 77 1113 + 77
1138 - 22 1114 - 22
1139 assert_equal(66, name) 1115 assert_equal(66, name)
1140 END 1116 END
1141 CheckScriptSuccess(lines) 1117 CheckDefAndScriptSuccess(lines)
1142 1118
1143 lines =<< trim END 1119 lines =<< trim END
1144 vim9script
1145 var name = 11 + 1120 var name = 11 +
1146 77 - 1121 77 -
1147 22 1122 22
1148 assert_equal(66, name) 1123 assert_equal(66, name)
1149 END 1124 END
1150 CheckScriptSuccess(lines) 1125 CheckDefAndScriptSuccess(lines)
1151 1126
1152 lines =<< trim END 1127 lines =<< trim END
1153 vim9script
1154 var name = 11 + # comment 1128 var name = 11 + # comment
1155 77 - 1129 77 -
1156 # comment 1130 # comment
1157 22 1131 22
1158 assert_equal(66, name) 1132 assert_equal(66, name)
1159 END 1133 END
1160 CheckScriptSuccess(lines) 1134 CheckDefAndScriptSuccess(lines)
1161 1135
1162 lines =<< trim END 1136 lines =<< trim END
1163 vim9script
1164 var name = 'one' 1137 var name = 'one'
1165 .. 'two' 1138 .. 'two'
1166 assert_equal('onetwo', name) 1139 assert_equal('onetwo', name)
1167 END 1140 END
1168 CheckScriptSuccess(lines) 1141 CheckDefAndScriptSuccess(lines)
1169 1142
1170 lines =<< trim END 1143 lines =<< trim END
1171 vim9script
1172 echo 'abc' is# 'abc' 1144 echo 'abc' is# 'abc'
1173 END 1145 END
1174 CheckScriptFailure(lines, 'E15:', 2) 1146 CheckDefAndScriptFailure(lines, 'E15:', 1)
1175 1147
1176 lines =<< trim END 1148 lines =<< trim END
1177 vim9script
1178 echo {} - 22 1149 echo {} - 22
1179 END 1150 END
1180 CheckScriptFailure(lines, 'E728:', 2) 1151 CheckDefAndScriptFailure2(lines, 'E1036:', 'E728:', 1)
1181 1152
1182 lines =<< trim END 1153 lines =<< trim END
1183 vim9script
1184 echo [] - 33 1154 echo [] - 33
1185 END 1155 END
1186 CheckScriptFailure(lines, 'E745:', 2) 1156 CheckDefAndScriptFailure2(lines, 'E1036:', 'E745:', 1)
1187 1157
1188 lines =<< trim END 1158 lines =<< trim END
1189 vim9script
1190 echo 0z1234 - 44 1159 echo 0z1234 - 44
1191 END 1160 END
1192 CheckScriptFailure(lines, 'E974:', 2) 1161 CheckDefAndScriptFailure2(lines, 'E1036', 'E974:', 1)
1193 1162
1194 lines =<< trim END 1163 lines =<< trim END
1195 vim9script
1196 echo 'abc' is? 'abc' 1164 echo 'abc' is? 'abc'
1197 END 1165 END
1198 CheckScriptFailure(lines, 'E15:', 2) 1166 CheckDefAndScriptFailure(lines, 'E15:', 1)
1199 1167
1200 lines =<< trim END 1168 lines =<< trim END
1201 vim9script
1202 echo 'abc' isnot# 'abc' 1169 echo 'abc' isnot# 'abc'
1203 END 1170 END
1204 CheckScriptFailure(lines, 'E15:', 2) 1171 CheckDefAndScriptFailure(lines, 'E15:', 1)
1205 1172
1206 lines =<< trim END 1173 lines =<< trim END
1207 vim9script
1208 echo 'abc' isnot? 'abc' 1174 echo 'abc' isnot? 'abc'
1209 END 1175 END
1210 CheckScriptFailure(lines, 'E15:', 2) 1176 CheckDefAndScriptFailure(lines, 'E15:', 1)
1211 1177
1212 # check white space 1178 # check white space
1213 lines =<< trim END 1179 lines =<< trim END
1214 vim9script
1215 echo 5+6 1180 echo 5+6
1216 END 1181 END
1217 CheckScriptFailure(lines, 'E1004:', 2) 1182 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1218 lines =<< trim END 1183 lines =<< trim END
1219 vim9script
1220 echo 5 +6 1184 echo 5 +6
1221 END 1185 END
1222 CheckScriptFailure(lines, 'E1004:', 2) 1186 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1223 lines =<< trim END 1187
1224 vim9script 1188 lines =<< trim END
1225 echo 5+ 6 1189 echo 5+ 6
1226 END 1190 END
1227 CheckScriptFailure(lines, 'E1004:', 2) 1191 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1228 1192
1229 lines =<< trim END 1193 lines =<< trim END
1230 vim9script
1231 echo 'a'..'b' 1194 echo 'a'..'b'
1232 END 1195 END
1233 CheckScriptFailure(lines, 'E1004: White space required before and after ''..'' at "..''b''"', 2) 1196 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at "..''b''"', 1)
1234 lines =<< trim END 1197
1235 vim9script 1198 lines =<< trim END
1236 echo 'a' ..'b' 1199 echo 'a' ..'b'
1237 END 1200 END
1238 CheckScriptFailure(lines, 'E1004:', 2) 1201 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1239 lines =<< trim END 1202
1240 vim9script 1203 lines =<< trim END
1241 echo 'a'.. 'b' 1204 echo 'a'.. 'b'
1242 END 1205 END
1243 CheckScriptFailure(lines, 'E1004:', 2) 1206 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1244 1207
1245 # check invalid string concatenation 1208 # check invalid string concatenation
1246 lines =<< trim END 1209 lines =<< trim END
1247 vim9script
1248 echo 'a' .. [1] 1210 echo 'a' .. [1]
1249 END 1211 END
1250 CheckScriptFailure(lines, 'E730:', 2) 1212 CheckDefAndScriptFailure2(lines, 'E1105:', 'E730:', 1)
1251 lines =<< trim END 1213
1252 vim9script 1214 lines =<< trim END
1253 echo 'a' .. {a: 1} 1215 echo 'a' .. {a: 1}
1254 END 1216 END
1255 CheckScriptFailure(lines, 'E731:', 2) 1217 CheckDefAndScriptFailure2(lines, 'E1105:', 'E731:', 1)
1256 lines =<< trim END 1218
1257 vim9script 1219 lines =<< trim END
1258 echo 'a' .. test_void() 1220 echo 'a' .. test_void()
1259 END 1221 END
1260 CheckScriptFailure(lines, 'E908:', 2) 1222 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1)
1261 lines =<< trim END 1223
1262 vim9script 1224 lines =<< trim END
1263 echo 'a' .. 0z33 1225 echo 'a' .. 0z33
1264 END 1226 END
1265 CheckScriptFailure(lines, 'E976:', 2) 1227 CheckDefAndScriptFailure2(lines, 'E1105:', 'E976:', 1)
1266 lines =<< trim END 1228
1267 vim9script 1229 lines =<< trim END
1268 echo 'a' .. function('len') 1230 echo 'a' .. function('len')
1269 END 1231 END
1270 CheckScriptFailure(lines, 'E729:', 2) 1232 CheckDefAndScriptFailure2(lines, 'E1105:', 'E729:', 1)
1271 1233
1272 lines =<< trim END 1234 lines =<< trim END
1273 vim9script
1274 new 1235 new
1275 ['']->setline(1) 1236 ['']->setline(1)
1276 /pattern 1237 /pattern
1277 1238
1278 eval 0 1239 eval 0
1279 bwipe! 1240 bwipe!
1280 END 1241 END
1281 CheckScriptFailure(lines, "E1004: White space required before and after '/' at \"/pattern") 1242 CheckDefAndScriptFailure(lines, "E1004: White space required before and after '/' at \"/pattern", 3)
1282 enddef 1243 enddef
1283 1244
1284 def Test_expr5_vim9script_channel() 1245 def Test_expr5_vim9script_channel()
1285 if !has('channel') 1246 if !has('channel')
1286 MissingFeature 'float' 1247 MissingFeature 'float'
1287 else 1248 else
1288 var lines =<< trim END 1249 var lines =<< trim END
1289 vim9script
1290 echo 'a' .. test_null_job() 1250 echo 'a' .. test_null_job()
1291 END 1251 END
1292 CheckScriptFailure(lines, 'E908:', 2) 1252 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1)
1293 lines =<< trim END 1253 lines =<< trim END
1294 vim9script
1295 echo 'a' .. test_null_channel() 1254 echo 'a' .. test_null_channel()
1296 END 1255 END
1297 CheckScriptFailure(lines, 'E908:', 2) 1256 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1)
1298 endif 1257 endif
1299 enddef 1258 enddef
1300 1259
1301 def Test_expr5_float() 1260 def Test_expr5_float()
1302 if !has('float') 1261 if !has('float')
1326 endif 1285 endif
1327 enddef 1286 enddef
1328 1287
1329 func Test_expr5_fails() 1288 func Test_expr5_fails()
1330 let msg = "White space required before and after '+'" 1289 let msg = "White space required before and after '+'"
1331 call CheckDefFailure(["var x = 1+2"], msg, 1) 1290 call CheckDefAndScriptFailure(["var x = 1+2"], msg, 1)
1332 call CheckDefFailure(["var x = 1 +2"], msg, 1) 1291 call CheckDefAndScriptFailure(["var x = 1 +2"], msg, 1)
1333 call CheckDefFailure(["var x = 1+ 2"], msg, 1) 1292 call CheckDefAndScriptFailure(["var x = 1+ 2"], msg, 1)
1334 1293
1335 let msg = "White space required before and after '-'" 1294 let msg = "White space required before and after '-'"
1336 call CheckDefFailure(["var x = 1-2"], msg, 1) 1295 call CheckDefAndScriptFailure(["var x = 1-2"], msg, 1)
1337 call CheckDefFailure(["var x = 1 -2"], msg, 1) 1296 call CheckDefAndScriptFailure(["var x = 1 -2"], msg, 1)
1338 call CheckDefFailure(["var x = 1- 2"], msg, 1) 1297 call CheckDefAndScriptFailure(["var x = 1- 2"], msg, 1)
1339 1298
1340 let msg = "White space required before and after '..'" 1299 let msg = "White space required before and after '..'"
1341 call CheckDefFailure(["var x = '1'..'2'"], msg, 1) 1300 call CheckDefAndScriptFailure(["var x = '1'..'2'"], msg, 1)
1342 call CheckDefFailure(["var x = '1' ..'2'"], msg, 1) 1301 call CheckDefAndScriptFailure(["var x = '1' ..'2'"], msg, 1)
1343 call CheckDefFailure(["var x = '1'.. '2'"], msg, 1) 1302 call CheckDefAndScriptFailure(["var x = '1'.. '2'"], msg, 1)
1344 1303
1345 call CheckDefFailure(["var x = 0z1122 + 33"], 'E1051:', 1) 1304 call CheckDefAndScriptFailure2(["var x = 0z1122 + 33"], 'E1051:', 'E974:', 1)
1346 call CheckDefFailure(["var x = 0z1122 + [3]"], 'E1051:', 1) 1305 call CheckDefAndScriptFailure2(["var x = 0z1122 + [3]"], 'E1051:', 'E974:', 1)
1347 call CheckDefFailure(["var x = 0z1122 + 'asd'"], 'E1051:', 1) 1306 call CheckDefAndScriptFailure2(["var x = 0z1122 + 'asd'"], 'E1051:', 'E974:', 1)
1348 call CheckDefFailure(["var x = 33 + 0z1122"], 'E1051:', 1) 1307 call CheckDefAndScriptFailure2(["var x = 33 + 0z1122"], 'E1051:', 'E974:', 1)
1349 call CheckDefFailure(["var x = [3] + 0z1122"], 'E1051:', 1) 1308 call CheckDefAndScriptFailure2(["var x = [3] + 0z1122"], 'E1051:', 'E745:', 1)
1350 call CheckDefFailure(["var x = 'asdf' + 0z1122"], 'E1051:', 1) 1309 call CheckDefAndScriptFailure2(["var x = 'asdf' + 0z1122"], 'E1051:', 'E1030:', 1)
1351 call CheckDefFailure(["var x = 6 + xxx"], 'E1001:', 1) 1310 call CheckDefAndScriptFailure2(["var x = 6 + xxx"], 'E1001:', 'E121:', 1)
1352 1311
1353 call CheckDefFailure(["var x = 'a' .. [1]"], 'E1105:', 1) 1312 call CheckDefAndScriptFailure2(["var x = 'a' .. [1]"], 'E1105:', 'E730:', 1)
1354 call CheckDefFailure(["var x = 'a' .. {a: 1}"], 'E1105:', 1) 1313 call CheckDefAndScriptFailure2(["var x = 'a' .. {a: 1}"], 'E1105:', 'E731:', 1)
1355 call CheckDefFailure(["var x = 'a' .. test_void()"], 'E1105:', 1) 1314 call CheckDefAndScriptFailure2(["var x = 'a' .. test_void()"], 'E1105:', 'E908:', 1)
1356 call CheckDefFailure(["var x = 'a' .. 0z32"], 'E1105:', 1) 1315 call CheckDefAndScriptFailure2(["var x = 'a' .. 0z32"], 'E1105:', 'E976:', 1)
1357 call CheckDefFailure(["var x = 'a' .. function('len')"], 'E1105:', 1) 1316 call CheckDefAndScriptFailure2(["var x = 'a' .. function('len')"], 'E1105:', 'E729:', 1)
1358 call CheckDefFailure(["var x = 'a' .. function('len', ['a'])"], 'E1105:', 1) 1317 call CheckDefAndScriptFailure2(["var x = 'a' .. function('len', ['a'])"], 'E1105:', 'E729:', 1)
1359 1318
1360 call CheckScriptFailure(['vim9script', 'var x = 1 + v:none'], 'E611:', 2) 1319 call CheckDefAndScriptFailure2(['var x = 1 + v:none'], 'E1051:', 'E611:', 1)
1361 call CheckScriptFailure(['vim9script', 'var x = 1 + v:null'], 'E611:', 2) 1320 call CheckDefAndScriptFailure2(['var x = 1 + v:null'], 'E1051:', 'E611:', 1)
1362 call CheckScriptFailure(['vim9script', 'var x = 1 + v:true'], 'E1138:', 2) 1321 call CheckDefAndScriptFailure2(['var x = 1 + v:true'], 'E1051:', 'E1138:', 1)
1363 call CheckScriptFailure(['vim9script', 'var x = 1 + v:false'], 'E1138:', 2) 1322 call CheckDefAndScriptFailure2(['var x = 1 + v:false'], 'E1051:', 'E1138:', 1)
1364 call CheckScriptFailure(['vim9script', 'var x = 1 + true'], 'E1138:', 2) 1323 call CheckDefAndScriptFailure2(['var x = 1 + true'], 'E1051:', 'E1138:', 1)
1365 call CheckScriptFailure(['vim9script', 'var x = 1 + false'], 'E1138:', 2) 1324 call CheckDefAndScriptFailure2(['var x = 1 + false'], 'E1051:', 'E1138:', 1)
1366 endfunc 1325 endfunc
1367 1326
1368 func Test_expr5_fails_channel() 1327 func Test_expr5_fails_channel()
1369 CheckFeature channel 1328 CheckFeature channel
1370 call CheckDefFailure(["var x = 'a' .. test_null_job()"], 'E1105:', 1) 1329 call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_job()"], 'E1105:', 'E908:', 1)
1371 call CheckDefFailure(["var x = 'a' .. test_null_channel()"], 'E1105:', 1) 1330 call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_channel()"], 'E1105:', 'E908:', 1)
1372 endfunc 1331 endfunc
1373 1332
1374 def Test_expr5_list_add() 1333 def Test_expr5_list_add()
1375 # concatenating two lists with same member types is OK 1334 var lines =<< trim END
1376 var d = {} 1335 # concatenating two lists with same member types is OK
1377 for i in ['a'] + ['b'] 1336 var d = {}
1378 d = {[i]: 0} 1337 for i in ['a'] + ['b']
1379 endfor 1338 d = {[i]: 0}
1380 1339 endfor
1381 # concatenating two lists with different member types results in "any" 1340
1382 var dany = {} 1341 # concatenating two lists with different member types results in "any"
1383 for i in ['a'] + [12] 1342 var dany = {}
1384 dany[i] = i 1343 for i in ['a'] + [12]
1385 endfor 1344 dany[i] = i
1386 assert_equal({a: 'a', 12: 12}, dany) 1345 endfor
1387 1346 assert_equal({a: 'a', 12: 12}, dany)
1388 # result of glob() is "any", runtime type check 1347
1389 var sl: list<string> = glob('*.txt', false, true) + [''] 1348 # result of glob() is "any", runtime type check
1349 var sl: list<string> = glob('*.txt', false, true) + ['']
1350 END
1351 CheckDefAndScriptSuccess(lines)
1390 enddef 1352 enddef
1391 1353
1392 " test multiply, divide, modulo 1354 " test multiply, divide, modulo
1393 def Test_expr6() 1355 def Test_expr6()
1394 var lines =<< trim END 1356 var lines =<< trim END
1428 * yf[0]) 1390 * yf[0])
1429 endif 1391 endif
1430 END 1392 END
1431 CheckDefAndScriptSuccess(lines) 1393 CheckDefAndScriptSuccess(lines)
1432 1394
1433 CheckDefFailure(["var x = 6 * xxx"], 'E1001:', 1) 1395 CheckDefAndScriptFailure2(["var x = 6 * xxx"], 'E1001:', 'E121:', 1)
1434 CheckDefFailure(["var d = 6 * "], 'E1097:', 3) 1396 CheckDefFailure(["var d = 6 * "], 'E1097:', 3)
1397 CheckScriptFailure(['vim9script', "var d = 6 * "], 'E15:', 2)
1435 1398
1436 CheckDefExecAndScriptFailure(['echo 1 / 0'], 'E1154', 1) 1399 CheckDefExecAndScriptFailure(['echo 1 / 0'], 'E1154', 1)
1437 CheckDefExecAndScriptFailure(['echo 1 % 0'], 'E1154', 1) 1400 CheckDefExecAndScriptFailure(['echo 1 % 0'], 'E1154', 1)
1438 enddef 1401 enddef
1439 1402
1440 def Test_expr6_vim9script() 1403 def Test_expr6_vim9script()
1441 # check line continuation 1404 # check line continuation
1442 var lines =<< trim END 1405 var lines =<< trim END
1443 vim9script
1444 var name = 11 1406 var name = 11
1445 * 22 1407 * 22
1446 / 3 1408 / 3
1447 assert_equal(80, name) 1409 assert_equal(80, name)
1448 END 1410 END
1449 CheckScriptSuccess(lines) 1411 CheckDefAndScriptSuccess(lines)
1450 1412
1451 lines =<< trim END 1413 lines =<< trim END
1452 vim9script
1453 var name = 25 1414 var name = 25
1454 % 10 1415 % 10
1455 assert_equal(5, name) 1416 assert_equal(5, name)
1456 END 1417 END
1457 CheckScriptSuccess(lines) 1418 CheckDefAndScriptSuccess(lines)
1458 1419
1459 lines =<< trim END 1420 lines =<< trim END
1460 vim9script
1461 var name = 25 1421 var name = 25
1462 # comment 1422 # comment
1463 1423
1464 # comment 1424 # comment
1465 % 10 1425 % 10
1466 assert_equal(5, name) 1426 assert_equal(5, name)
1467 END 1427 END
1468 CheckScriptSuccess(lines) 1428 CheckDefAndScriptSuccess(lines)
1469 1429
1470 lines =<< trim END 1430 lines =<< trim END
1471 vim9script
1472 var name = 11 * 1431 var name = 11 *
1473 22 / 1432 22 /
1474 3 1433 3
1475 assert_equal(80, name) 1434 assert_equal(80, name)
1476 END 1435 END
1477 CheckScriptSuccess(lines) 1436 CheckDefAndScriptSuccess(lines)
1478 1437
1479 # check white space 1438 # check white space
1480 lines =<< trim END 1439 lines =<< trim END
1481 vim9script
1482 echo 5*6 1440 echo 5*6
1483 END 1441 END
1484 CheckScriptFailure(lines, 'E1004:', 2) 1442 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1485 lines =<< trim END 1443
1486 vim9script 1444 lines =<< trim END
1487 echo 5 *6 1445 echo 5 *6
1488 END 1446 END
1489 CheckScriptFailure(lines, 'E1004:', 2) 1447 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1490 lines =<< trim END 1448
1491 vim9script 1449 lines =<< trim END
1492 echo 5* 6 1450 echo 5* 6
1493 END 1451 END
1494 CheckScriptFailure(lines, 'E1004:', 2) 1452 CheckDefAndScriptFailure(lines, 'E1004:', 1)
1495 enddef 1453 enddef
1496 1454
1497 def Test_expr6_float() 1455 def Test_expr6_float()
1498 if !has('float') 1456 if !has('float')
1499 MissingFeature 'float' 1457 MissingFeature 'float'
1527 endif 1485 endif
1528 enddef 1486 enddef
1529 1487
1530 func Test_expr6_fails() 1488 func Test_expr6_fails()
1531 let msg = "White space required before and after '*'" 1489 let msg = "White space required before and after '*'"
1532 call CheckDefFailure(["var x = 1*2"], msg, 1) 1490 call CheckDefAndScriptFailure(["var x = 1*2"], msg, 1)
1533 call CheckDefFailure(["var x = 1 *2"], msg, 1) 1491 call CheckDefAndScriptFailure(["var x = 1 *2"], msg, 1)
1534 call CheckDefFailure(["var x = 1* 2"], msg, 1) 1492 call CheckDefAndScriptFailure(["var x = 1* 2"], msg, 1)
1535 1493
1536 let msg = "White space required before and after '/'" 1494 let msg = "White space required before and after '/'"
1537 call CheckDefFailure(["var x = 1/2"], msg, 1) 1495 call CheckDefAndScriptFailure(["var x = 1/2"], msg, 1)
1538 call CheckDefFailure(["var x = 1 /2"], msg, 1) 1496 call CheckDefAndScriptFailure(["var x = 1 /2"], msg, 1)
1539 call CheckDefFailure(["var x = 1/ 2"], msg, 1) 1497 call CheckDefAndScriptFailure(["var x = 1/ 2"], msg, 1)
1540 1498
1541 let msg = "White space required before and after '%'" 1499 let msg = "White space required before and after '%'"
1542 call CheckDefFailure(["var x = 1%2"], msg, 1) 1500 call CheckDefAndScriptFailure(["var x = 1%2"], msg, 1)
1543 call CheckDefFailure(["var x = 1 %2"], msg, 1) 1501 call CheckDefAndScriptFailure(["var x = 1 %2"], msg, 1)
1544 call CheckDefFailure(["var x = 1% 2"], msg, 1) 1502 call CheckDefAndScriptFailure(["var x = 1% 2"], msg, 1)
1545 1503
1546 call CheckDefFailure(["var x = '1' * '2'"], 'E1036:', 1) 1504 call CheckDefAndScriptFailure2(["var x = '1' * '2'"], 'E1036:', 'E1030:', 1)
1547 call CheckDefFailure(["var x = '1' / '2'"], 'E1036:', 1) 1505 call CheckDefAndScriptFailure2(["var x = '1' / '2'"], 'E1036:', 'E1030:', 1)
1548 call CheckDefFailure(["var x = '1' % '2'"], 'E1035:', 1) 1506 call CheckDefAndScriptFailure2(["var x = '1' % '2'"], 'E1035:', 'E1030:', 1)
1549 1507
1550 call CheckDefFailure(["var x = 0z01 * 0z12"], 'E1036:', 1) 1508 call CheckDefAndScriptFailure2(["var x = 0z01 * 0z12"], 'E1036:', 'E974:', 1)
1551 call CheckDefFailure(["var x = 0z01 / 0z12"], 'E1036:', 1) 1509 call CheckDefAndScriptFailure2(["var x = 0z01 / 0z12"], 'E1036:', 'E974:', 1)
1552 call CheckDefFailure(["var x = 0z01 % 0z12"], 'E1035:', 1) 1510 call CheckDefAndScriptFailure2(["var x = 0z01 % 0z12"], 'E1035:', 'E974:', 1)
1553 1511
1554 call CheckDefFailure(["var x = [1] * [2]"], 'E1036:', 1) 1512 call CheckDefAndScriptFailure2(["var x = [1] * [2]"], 'E1036:', 'E745:', 1)
1555 call CheckDefFailure(["var x = [1] / [2]"], 'E1036:', 1) 1513 call CheckDefAndScriptFailure2(["var x = [1] / [2]"], 'E1036:', 'E745:', 1)
1556 call CheckDefFailure(["var x = [1] % [2]"], 'E1035:', 1) 1514 call CheckDefAndScriptFailure2(["var x = [1] % [2]"], 'E1035:', 'E745:', 1)
1557 1515
1558 call CheckDefFailure(["var x = {one: 1} * {two: 2}"], 'E1036:', 1) 1516 call CheckDefAndScriptFailure2(["var x = {one: 1} * {two: 2}"], 'E1036:', 'E728:', 1)
1559 call CheckDefFailure(["var x = {one: 1} / {two: 2}"], 'E1036:', 1) 1517 call CheckDefAndScriptFailure2(["var x = {one: 1} / {two: 2}"], 'E1036:', 'E728:', 1)
1560 call CheckDefFailure(["var x = {one: 1} % {two: 2}"], 'E1035:', 1) 1518 call CheckDefAndScriptFailure2(["var x = {one: 1} % {two: 2}"], 'E1035:', 'E728:', 1)
1561 1519
1562 call CheckDefFailure(["var x = 0xff[1]"], 'E1107:', 1) 1520 call CheckDefAndScriptFailure2(["var x = 0xff[1]"], 'E1107:', 'E1062:', 1)
1563 if has('float') 1521 if has('float')
1564 call CheckDefFailure(["var x = 0.7[1]"], 'E1107:', 1) 1522 call CheckDefAndScriptFailure2(["var x = 0.7[1]"], 'E1107:', 'E806:', 1)
1565 endif 1523 endif
1566 endfunc 1524 endfunc
1567 1525
1568 func Test_expr6_float_fails() 1526 func Test_expr6_float_fails()
1569 CheckFeature float 1527 CheckFeature float
1570 call CheckDefFailure(["var x = 1.0 % 2"], 'E1035:', 1) 1528 call CheckDefAndScriptFailure2(["var x = 1.0 % 2"], 'E1035:', 'E804:', 1)
1571 endfunc 1529 endfunc
1572 1530
1573 " define here to use old style parsing 1531 " define here to use old style parsing
1574 if has('float') 1532 if has('float')
1575 let g:float_zero = 0.0 1533 let g:float_zero = 0.0
1603 var ls: list<string> = ['a', <string>g:string_empty] 1561 var ls: list<string> = ['a', <string>g:string_empty]
1604 var ln: list<number> = [<number>g:anint, <number>g:thefour] 1562 var ln: list<number> = [<number>g:anint, <number>g:thefour]
1605 var nr = <number>234 1563 var nr = <number>234
1606 assert_equal(234, nr) 1564 assert_equal(234, nr)
1607 1565
1608 CheckDefFailure(["var x = <nr>123"], 'E1010:', 1) 1566 CheckDefAndScriptFailure2(["var x = <nr>123"], 'E1010:', 'E15:', 1)
1609 CheckDefFailure(["var x = <number>"], 'E1097:', 3) 1567 CheckDefFailure(["var x = <number>"], 'E1097:', 3)
1610 CheckDefFailure(["var x = <number >123"], 'E1068:', 1) 1568 CheckScriptFailure(['vim9script', "var x = <number>"], 'E15:', 2)
1611 CheckDefFailure(["var x = <number 123"], 'E1104:', 1) 1569 CheckDefAndScriptFailure2(["var x = <number >123"], 'E1068:', 'E15:', 1)
1570 CheckDefAndScriptFailure2(["var x = <number 123"], 'E1104:', 'E15:', 1)
1612 enddef 1571 enddef
1613 1572
1614 " test low level expression 1573 " test low level expression
1615 def Test_expr7_number() 1574 def Test_expr7_number()
1616 # number constant 1575 # number constant
1647 assert_equal(g:blob_one, 0z01) 1606 assert_equal(g:blob_one, 0z01)
1648 assert_equal(g:blob_long, 0z0102.0304) 1607 assert_equal(g:blob_long, 0z0102.0304)
1649 END 1608 END
1650 CheckDefAndScriptSuccess(lines) 1609 CheckDefAndScriptSuccess(lines)
1651 1610
1652 CheckDefFailure(["var x = 0z123"], 'E973:', 1) 1611 CheckDefAndScriptFailure(["var x = 0z123"], 'E973:', 1)
1653 enddef 1612 enddef
1654 1613
1655 def Test_expr7_string() 1614 def Test_expr7_string()
1656 # string constant 1615 # string constant
1657 var lines =<< trim END 1616 var lines =<< trim END
1663 assert_equal(g:string_long, "abcdefghijklm") 1622 assert_equal(g:string_long, "abcdefghijklm")
1664 assert_equal(g:string_special, "ab\ncd\ref\ekk") 1623 assert_equal(g:string_special, "ab\ncd\ref\ekk")
1665 END 1624 END
1666 CheckDefAndScriptSuccess(lines) 1625 CheckDefAndScriptSuccess(lines)
1667 1626
1668 CheckDefFailure(['var x = "abc'], 'E114:', 1) 1627 CheckDefAndScriptFailure(['var x = "abc'], 'E114:', 1)
1669 CheckDefFailure(["var x = 'abc"], 'E115:', 1) 1628 CheckDefAndScriptFailure(["var x = 'abc"], 'E115:', 1)
1670 enddef 1629 enddef
1671 1630
1672 def Test_expr7_vimvar() 1631 def Test_expr7_vimvar()
1673 var old: list<string> = v:oldfiles 1632 var old: list<string> = v:oldfiles
1674 var compl: dict<any> = v:completed_item 1633 var compl: dict<any> = v:completed_item
1675 1634
1676 CheckDefFailure(["var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 1) 1635 CheckDefFailure(["var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 1)
1636 CheckScriptFailure(['vim9script', 'v:oldfiles = ["foo"]', "var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 3)
1677 new 1637 new
1678 exec "normal! afoo fo\<C-N>\<Esc>" 1638 exec "normal! afoo fo\<C-N>\<Esc>"
1679 CheckDefExecFailure(["var old: dict<number> = v:completed_item"], 'E1012: Type mismatch; expected dict<number> but got dict<string>', 1) 1639 CheckDefExecAndScriptFailure(["var old: dict<number> = v:completed_item"], 'E1012: Type mismatch; expected dict<number> but got dict<string>', 1)
1680 bwipe! 1640 bwipe!
1681 enddef 1641 enddef
1682 1642
1683 def Test_expr7_special() 1643 def Test_expr7_special()
1684 # special constant 1644 # special constant
1708 assert_equal(g:special_null, null) 1668 assert_equal(g:special_null, null)
1709 assert_equal(g:special_none, v:none) 1669 assert_equal(g:special_none, v:none)
1710 END 1670 END
1711 CheckDefAndScriptSuccess(lines) 1671 CheckDefAndScriptSuccess(lines)
1712 1672
1713 CheckDefFailure(['v:true = true'], 'E46:', 1) 1673 CheckDefAndScriptFailure(['v:true = true'], 'E46:', 1)
1714 CheckDefFailure(['v:true = false'], 'E46:', 1) 1674 CheckDefAndScriptFailure(['v:true = false'], 'E46:', 1)
1715 CheckDefFailure(['v:false = true'], 'E46:', 1) 1675 CheckDefAndScriptFailure(['v:false = true'], 'E46:', 1)
1716 CheckDefFailure(['v:null = 11'], 'E46:', 1) 1676 CheckDefAndScriptFailure(['v:null = 11'], 'E46:', 1)
1717 CheckDefFailure(['v:none = 22'], 'E46:', 1) 1677 CheckDefAndScriptFailure(['v:none = 22'], 'E46:', 1)
1718 enddef 1678 enddef
1719 1679
1720 def Test_expr7_list() 1680 def Test_expr7_list()
1721 # list 1681 # list
1722 var lines =<< trim END 1682 var lines =<< trim END
1745 END 1705 END
1746 CheckDefAndScriptSuccess(lines) 1706 CheckDefAndScriptSuccess(lines)
1747 1707
1748 var rangelist: list<number> = range(3) 1708 var rangelist: list<number> = range(3)
1749 g:rangelist = range(3) 1709 g:rangelist = range(3)
1750 CheckDefExecFailure(["var x: list<string> = g:rangelist"], 'E1012: Type mismatch; expected list<string> but got list<number>', 1) 1710 CheckDefExecAndScriptFailure(["var x: list<string> = g:rangelist"], 'E1012: Type mismatch; expected list<string> but got list<number>', 1)
1751 1711
1752 CheckDefFailure(["var x = 1234[3]"], 'E1107:', 1) 1712 CheckDefAndScriptFailure2(["var x = 1234[3]"], 'E1107:', 'E1062:', 1)
1753 CheckDefExecFailure(["var x = g:anint[3]"], 'E1062:', 1) 1713 CheckDefExecAndScriptFailure(["var x = g:anint[3]"], 'E1062:', 1)
1754 1714
1755 CheckDefFailure(["var x = g:list_mixed[xxx]"], 'E1001:', 1) 1715 CheckDefAndScriptFailure2(["var x = g:list_mixed[xxx]"], 'E1001:', 'E121:', 1)
1756 1716
1757 CheckDefFailure(["var x = [1,2,3]"], 'E1069:', 1) 1717 CheckDefAndScriptFailure(["var x = [1,2,3]"], 'E1069:', 1)
1758 CheckDefFailure(["var x = [1 ,2, 3]"], 'E1068:', 1) 1718 CheckDefAndScriptFailure(["var x = [1 ,2, 3]"], 'E1068:', 1)
1759 1719
1760 CheckDefExecFailure(["echo 1", "var x = [][0]", "echo 3"], 'E684:', 2) 1720 CheckDefExecAndScriptFailure(["echo 1", "var x = [][0]", "echo 3"], 'E684:', 2)
1761 1721
1762 CheckDefExecFailure(["var x = g:list_mixed['xx']"], 'E1012:', 1) 1722 CheckDefExecAndScriptFailure2(["var x = g:list_mixed['xx']"], 'E1012:', 'E1030:', 1)
1763 CheckDefFailure(["var x = g:list_mixed["], 'E1097:', 3) 1723 CheckDefFailure(["var x = g:list_mixed["], 'E1097:', 3)
1724 CheckScriptFailure(['vim9script', "var x = g:list_mixed["], 'E15:', 2)
1764 CheckDefFailure(["var x = g:list_mixed[0"], 'E1097:', 3) 1725 CheckDefFailure(["var x = g:list_mixed[0"], 'E1097:', 3)
1765 CheckDefExecFailure(["var x = g:list_empty[3]"], 'E684:', 1) 1726 CheckScriptFailure(['vim9script', "var x = g:list_mixed[0"], 'E111:', 2)
1766 CheckDefExecFailure(["var l: list<number> = [234, 'x']"], 'E1012:', 1) 1727 CheckDefExecAndScriptFailure(["var x = g:list_empty[3]"], 'E684:', 1)
1767 CheckDefExecFailure(["var l: list<number> = ['x', 234]"], 'E1012:', 1) 1728 CheckDefExecAndScriptFailure(["var l: list<number> = [234, 'x']"], 'E1012:', 1)
1768 CheckDefExecFailure(["var l: list<string> = [234, 'x']"], 'E1012:', 1) 1729 CheckDefExecAndScriptFailure(["var l: list<number> = ['x', 234]"], 'E1012:', 1)
1769 CheckDefExecFailure(["var l: list<string> = ['x', 123]"], 'E1012:', 1) 1730 CheckDefExecAndScriptFailure(["var l: list<string> = [234, 'x']"], 'E1012:', 1)
1770 1731 CheckDefExecAndScriptFailure(["var l: list<string> = ['x', 123]"], 'E1012:', 1)
1771 lines =<< trim END 1732
1772 vim9script 1733 lines =<< trim END
1773 var datalist: list<string> 1734 var datalist: list<string>
1774 def Main() 1735 def Main()
1775 datalist += ['x'. 1736 datalist += ['x'.
1776 enddef 1737 enddef
1777 Main() 1738 Main()
1778 END 1739 END
1779 CheckScriptFailure(lines, 'E1127:') 1740 CheckDefAndScriptFailure(lines, 'E1127:')
1780 1741
1781 lines =<< trim END 1742 lines =<< trim END
1782 var numbers = [1, 2, 3, 4] 1743 var numbers = [1, 2, 3, 4]
1783 var a = 1 1744 var a = 1
1784 var b = 2 1745 var b = 2
1789 CheckDefAndScriptFailure(lines + ['echo numbers[a :b]'], 'E1004:', 4) 1750 CheckDefAndScriptFailure(lines + ['echo numbers[a :b]'], 'E1004:', 4)
1790 enddef 1751 enddef
1791 1752
1792 def Test_expr7_list_vim9script() 1753 def Test_expr7_list_vim9script()
1793 var lines =<< trim END 1754 var lines =<< trim END
1794 vim9script
1795 var l = [ 1755 var l = [
1796 11, 1756 11,
1797 22, 1757 22,
1798 ] 1758 ]
1799 assert_equal([11, 22], l) 1759 assert_equal([11, 22], l)
1806 # comment 1766 # comment
1807 2] [3, 1767 2] [3,
1808 # comment 1768 # comment
1809 4] 1769 4]
1810 END 1770 END
1811 CheckScriptSuccess(lines) 1771 CheckDefAndScriptSuccess(lines)
1812 1772
1813 lines =<< trim END 1773 lines =<< trim END
1814 vim9script
1815 var l = [11, 1774 var l = [11,
1816 22] 1775 22]
1817 assert_equal([11, 22], l) 1776 assert_equal([11, 22], l)
1818 END 1777 END
1819 CheckScriptSuccess(lines) 1778 CheckDefAndScriptSuccess(lines)
1820 1779
1821 lines =<< trim END 1780 lines =<< trim END
1822 vim9script
1823 var l = [11,22] 1781 var l = [11,22]
1824 END 1782 END
1825 CheckScriptFailure(lines, 'E1069:', 2) 1783 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1826 1784
1827 lines =<< trim END 1785 lines =<< trim END
1828 vim9script
1829 var l = [11 , 22] 1786 var l = [11 , 22]
1830 END 1787 END
1831 CheckScriptFailure(lines, 'E1068:', 2) 1788 CheckDefAndScriptFailure(lines, 'E1068:', 1)
1832 1789
1833 lines =<< trim END 1790 lines =<< trim END
1834 vim9script
1835 var l: list<number> = [234, 'x'] 1791 var l: list<number> = [234, 'x']
1836 END 1792 END
1837 CheckScriptFailure(lines, 'E1012:', 2) 1793 CheckDefAndScriptFailure(lines, 'E1012:', 1)
1838 lines =<< trim END 1794
1839 vim9script 1795 lines =<< trim END
1840 var l: list<number> = ['x', 234] 1796 var l: list<number> = ['x', 234]
1841 END 1797 END
1842 CheckScriptFailure(lines, 'E1012:', 2) 1798 CheckDefAndScriptFailure(lines, 'E1012:', 1)
1843 lines =<< trim END 1799
1844 vim9script 1800 lines =<< trim END
1845 var l: list<string> = ['x', 234] 1801 var l: list<string> = ['x', 234]
1846 END 1802 END
1847 CheckScriptFailure(lines, 'E1012:', 2) 1803 CheckDefAndScriptFailure(lines, 'E1012:', 1)
1848 lines =<< trim END 1804
1849 vim9script 1805 lines =<< trim END
1850 var l: list<string> = [234, 'x'] 1806 var l: list<string> = [234, 'x']
1851 END 1807 END
1852 CheckScriptFailure(lines, 'E1012:', 2) 1808 CheckDefAndScriptFailure(lines, 'E1012:', 1)
1853 1809
1854 lines =<< trim END 1810 lines =<< trim END
1855 vim9script
1856 def Failing() 1811 def Failing()
1857 job_stop() 1812 job_stop()
1858 enddef 1813 enddef
1859 var list = [Failing] 1814 var list = [Failing]
1860 END 1815 END
1861 if has('channel') 1816 if has('channel')
1862 CheckScriptFailure(lines, 'E119:', 1) 1817 CheckDefAndScriptFailure(lines, 'E119:', 0)
1863 else 1818 else
1864 CheckScriptFailure(lines, 'E117:', 1) 1819 CheckDefAndScriptFailure(lines, 'E117:', 0)
1865 endif 1820 endif
1866 enddef 1821 enddef
1867 1822
1868 def LambdaWithComments(): func 1823 def LambdaWithComments(): func
1869 return (x) => 1824 return (x) =>
1928 var res = map([1, 2, 3], (i: number, v: number) => i + v) 1883 var res = map([1, 2, 3], (i: number, v: number) => i + v)
1929 assert_equal([1, 3, 5], res) 1884 assert_equal([1, 3, 5], res)
1930 END 1885 END
1931 CheckDefAndScriptSuccess(lines) 1886 CheckDefAndScriptSuccess(lines)
1932 1887
1933 CheckDefFailure(["var Ref = (a)=>a + 1"], 'E1004:') 1888 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:')
1934 CheckDefFailure(["var Ref = (a)=> a + 1"], 'E1004: White space required before and after ''=>'' at "=> a + 1"') 1889 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004: White space required before and after ''=>'' at "=> a + 1"')
1935 CheckDefFailure(["var Ref = (a) =>a + 1"], 'E1004:') 1890 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 'E1004:')
1936 1891
1937 CheckDefFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 1892 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1)
1938 # error is in first line of the lambda 1893 # error is in first line of the lambda
1939 CheckDefFailure(["var L = (a) => a + b"], 'E1001:', 0) 1894 CheckDefAndScriptFailure(["var L = (a) => a + b"], 'E1001:', 0)
1940 1895
1941 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 1896 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy'))
1942 1897
1943 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 'E118:') 1898 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 'E118:')
1944 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 'E118:') 1899 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 'E118:')
1945 CheckDefFailure(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 1) 1900 CheckDefAndScriptFailure2(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 'E121:', 1)
1946 1901
1947 CheckDefSuccess(['var Fx = (a) => ({k1: 0,', ' k2: 1})']) 1902 CheckDefAndScriptSuccess(['var Fx = (a) => ({k1: 0,', ' k2: 1})'])
1948 CheckDefFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 1903 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2)
1949 CheckDefFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 1904 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2)
1950 1905
1951 CheckDefSuccess(['var Fx = (a) => [0,', ' 1]']) 1906 CheckDefAndScriptSuccess(['var Fx = (a) => [0,', ' 1]'])
1952 CheckDefFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) 1907 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2)
1953 1908
1954 # no error for existing script variable when checking for lambda 1909 # no error for existing script variable when checking for lambda
1955 lines =<< trim END 1910 lines =<< trim END
1956 vim9script
1957 var name = 0 1911 var name = 0
1958 eval (name + 2) / 3 1912 eval (name + 2) / 3
1959 END 1913 END
1960 CheckScriptSuccess(lines) 1914 CheckDefAndScriptSuccess(lines)
1961 enddef 1915 enddef
1962 1916
1963 def Test_expr7_lambda_block() 1917 def Test_expr7_lambda_block()
1964 var lines =<< trim END 1918 var lines =<< trim END
1965 var Func = (s: string): string => { 1919 var Func = (s: string): string => {
2007 return nr 1961 return nr
2008 END 1962 END
2009 CheckDefAndScriptFailure(lines, 'E1171', 1) # line nr is function start 1963 CheckDefAndScriptFailure(lines, 'E1171', 1) # line nr is function start
2010 1964
2011 lines =<< trim END 1965 lines =<< trim END
2012 vim9script
2013 var Func = (nr: number): int => { 1966 var Func = (nr: number): int => {
2014 var ll =<< ENDIT 1967 var ll =<< ENDIT
2015 nothing 1968 nothing
2016 END 1969 END
2017 CheckScriptFailure(lines, 'E1145: Missing heredoc end marker: ENDIT', 2) 1970 CheckDefFailure(lines, 'E1145: Missing heredoc end marker: ENDIT', 0)
1971 CheckScriptFailure(['vim9script'] + lines, 'E1145: Missing heredoc end marker: ENDIT', 2)
2018 enddef 1972 enddef
2019 1973
2020 def NewLambdaWithComments(): func 1974 def NewLambdaWithComments(): func
2021 return (x) => 1975 return (x) =>
2022 # some comment 1976 # some comment
2097 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:') 2051 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:')
2098 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004:') 2052 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004:')
2099 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 2053 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"],
2100 'E1004: White space required before and after ''=>'' at " =>a + 1"') 2054 'E1004: White space required before and after ''=>'' at " =>a + 1"')
2101 2055
2102 CheckDefFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:') 2056 CheckDefAndScriptFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:')
2103 CheckDefFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:') 2057 CheckDefAndScriptFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:')
2104 2058
2105 CheckDefFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 2059 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1)
2106 # error is in first line of the lambda 2060 # error is in first line of the lambda
2107 CheckDefFailure(["var L = (a) -> a + b"], 'E1001:', 1) 2061 CheckDefAndScriptFailure2(["var L = (a) -> a + b"], 'E1001:', 'E121:', 1)
2108 2062
2109 # TODO: ->(lambda)() doesn't work yet 2063 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy'))
2110 # assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 2064
2111 2065 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"],
2112 # CheckDefExecFailure(["var s = 'asdf'->{a -> a}('x')"], 2066 'E118: Too many arguments for function:')
2113 # 'E1106: One argument too many') 2067 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"],
2114 # CheckDefExecFailure(["var s = 'asdf'->{a -> a}('x', 'y')"], 2068 'E118: Too many arguments for function:')
2115 # 'E1106: 2 arguments too many') 2069 CheckDefFailure(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 1)
2116 # CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1) 2070
2117 2071 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2)
2118 CheckDefFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 2072 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2)
2119 CheckDefFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 2073
2120 2074 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2)
2121 CheckDefFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2)
2122 enddef 2075 enddef
2123 2076
2124 def Test_expr7_lambda_vim9script() 2077 def Test_expr7_lambda_vim9script()
2078 # TODO: make this work in a :def function
2125 var lines =<< trim END 2079 var lines =<< trim END
2126 vim9script 2080 vim9script
2127 var v = 10->((a) => 2081 var v = 10->((a) =>
2128 a 2082 a
2129 + 2 2083 + 2
2132 END 2086 END
2133 CheckScriptSuccess(lines) 2087 CheckScriptSuccess(lines)
2134 2088
2135 # nested lambda with line breaks 2089 # nested lambda with line breaks
2136 lines =<< trim END 2090 lines =<< trim END
2137 vim9script
2138 search('"', 'cW', 0, 0, () => 2091 search('"', 'cW', 0, 0, () =>
2139 synstack('.', col('.')) 2092 synstack('.', col('.'))
2140 ->map((_, v) => synIDattr(v, 'name'))->len()) 2093 ->map((_, v) => synIDattr(v, 'name'))->len())
2141 END 2094 END
2142 CheckScriptSuccess(lines) 2095 CheckDefAndScriptSuccess(lines)
2143 enddef 2096 enddef
2144 2097
2145 def Test_epxr7_funcref() 2098 def Test_expr7_funcref()
2146 var lines =<< trim END 2099 var lines =<< trim END
2147 def RetNumber(): number 2100 def RetNumber(): number
2148 return 123 2101 return 123
2149 enddef 2102 enddef
2150 var FuncRef = RetNumber 2103 var FuncRef = RetNumber
2237 CheckDefAndScriptFailure(["var x = #{key: 8}"], 'E1170:', 1) 2190 CheckDefAndScriptFailure(["var x = #{key: 8}"], 'E1170:', 1)
2238 CheckDefAndScriptFailure(["var x = 'a' #{a: 1}"], 'E1170:', 1) 2191 CheckDefAndScriptFailure(["var x = 'a' #{a: 1}"], 'E1170:', 1)
2239 CheckDefAndScriptFailure(["var x = 'a' .. #{a: 1}"], 'E1170:', 1) 2192 CheckDefAndScriptFailure(["var x = 'a' .. #{a: 1}"], 'E1170:', 1)
2240 CheckDefAndScriptFailure(["var x = true ? #{a: 1}"], 'E1170:', 1) 2193 CheckDefAndScriptFailure(["var x = true ? #{a: 1}"], 'E1170:', 1)
2241 2194
2242 CheckDefFailure(["var x = {a:8}"], 'E1069:', 1) 2195 CheckDefAndScriptFailure(["var x = {a:8}"], 'E1069:', 1)
2243 CheckDefFailure(["var x = {a : 8}"], 'E1068:', 1) 2196 CheckDefAndScriptFailure(["var x = {a : 8}"], 'E1068:', 1)
2244 CheckDefFailure(["var x = {a :8}"], 'E1068:', 1) 2197 CheckDefAndScriptFailure(["var x = {a :8}"], 'E1068:', 1)
2245 CheckDefFailure(["var x = {a: 8 , b: 9}"], 'E1068:', 1) 2198 CheckDefAndScriptFailure(["var x = {a: 8 , b: 9}"], 'E1068:', 1)
2246 CheckDefFailure(["var x = {a: 1,b: 2}"], 'E1069:', 1) 2199 CheckDefAndScriptFailure(["var x = {a: 1,b: 2}"], 'E1069:', 1)
2247 2200
2248 CheckDefFailure(["var x = {xxx}"], 'E720:', 1) 2201 CheckDefAndScriptFailure(["var x = {xxx}"], 'E720:', 1)
2249 CheckDefFailure(["var x = {xxx: 1", "var y = 2"], 'E722:', 2) 2202 CheckDefAndScriptFailure(["var x = {xxx: 1", "var y = 2"], 'E722:', 2)
2250 CheckDefFailure(["var x = {xxx: 1,"], 'E723:', 2) 2203 CheckDefFailure(["var x = {xxx: 1,"], 'E723:', 2)
2251 CheckDefFailure(["var x = {['a']: xxx}"], 'E1001:', 1) 2204 CheckScriptFailure(['vim9script', "var x = {xxx: 1,"], 'E723:', 2)
2252 CheckDefFailure(["var x = {a: 1, a: 2}"], 'E721:', 1) 2205 CheckDefAndScriptFailure2(["var x = {['a']: xxx}"], 'E1001:', 'E121:', 1)
2253 CheckDefExecFailure(["var x = g:anint.member"], 'E715:', 1) 2206 CheckDefAndScriptFailure(["var x = {a: 1, a: 2}"], 'E721:', 1)
2254 CheckDefExecFailure(["var x = g:dict_empty.member"], 'E716:', 1) 2207 CheckDefExecAndScriptFailure2(["var x = g:anint.member"], 'E715:', 'E15:', 1)
2255 2208 CheckDefExecAndScriptFailure(["var x = g:dict_empty.member"], 'E716:', 1)
2256 CheckDefExecFailure(['var x: dict<number> = {a: 234, b: "1"}'], 'E1012:', 1) 2209
2257 CheckDefExecFailure(['var x: dict<number> = {a: "x", b: 134}'], 'E1012:', 1) 2210 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: 234, b: "1"}'], 'E1012:', 1)
2258 CheckDefExecFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1) 2211 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: "x", b: 134}'], 'E1012:', 1)
2259 CheckDefExecFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1) 2212 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1)
2213 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1)
2260 2214
2261 # invalid types for the key 2215 # invalid types for the key
2262 CheckDefFailure(["var x = {[[1, 2]]: 0}"], 'E1105:', 1) 2216 CheckDefAndScriptFailure2(["var x = {[[1, 2]]: 0}"], 'E1105:', 'E730:', 1)
2263 2217
2264 CheckDefFailure(['var x = ({'], 'E723:', 2) 2218 CheckDefFailure(['var x = ({'], 'E723:', 2)
2265 CheckDefExecFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1) 2219 CheckScriptFailure(['vim9script', 'var x = ({'], 'E723:', 2)
2220 CheckDefExecAndScriptFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1)
2266 enddef 2221 enddef
2267 2222
2268 def Test_expr7_dict_vim9script() 2223 def Test_expr7_dict_vim9script()
2269 var lines =<< trim END 2224 var lines =<< trim END
2270 vim9script
2271 var d = { 2225 var d = {
2272 ['one']: 2226 ['one']:
2273 1, 2227 1,
2274 ['two']: 2, 2228 ['two']: 2,
2275 } 2229 }
2287 assert_equal({one: 1, two: 2}, d) 2241 assert_equal({one: 1, two: 2}, d)
2288 2242
2289 var dd = {k: 123->len()} 2243 var dd = {k: 123->len()}
2290 assert_equal(3, dd.k) 2244 assert_equal(3, dd.k)
2291 END 2245 END
2292 CheckScriptSuccess(lines) 2246 CheckDefAndScriptSuccess(lines)
2293 2247
2294 lines =<< trim END 2248 lines =<< trim END
2295 vim9script
2296 var d = { ["one"]: "one", ["two"]: "two", } 2249 var d = { ["one"]: "one", ["two"]: "two", }
2297 assert_equal({one: 'one', two: 'two'}, d) 2250 assert_equal({one: 'one', two: 'two'}, d)
2298 END 2251 END
2299 CheckScriptSuccess(lines) 2252 CheckDefAndScriptSuccess(lines)
2300 2253
2301 lines =<< trim END 2254 lines =<< trim END
2302 vim9script
2303 var d = {one: 1, 2255 var d = {one: 1,
2304 two: 2, 2256 two: 2,
2305 } 2257 }
2306 assert_equal({one: 1, two: 2}, d) 2258 assert_equal({one: 1, two: 2}, d)
2307 END 2259 END
2308 CheckScriptSuccess(lines) 2260 CheckDefAndScriptSuccess(lines)
2309 2261
2310 lines =<< trim END 2262 lines =<< trim END
2311 vim9script
2312 var d = {one:1, two: 2} 2263 var d = {one:1, two: 2}
2313 END 2264 END
2314 CheckScriptFailure(lines, 'E1069:', 2) 2265 CheckDefAndScriptFailure(lines, 'E1069:', 1)
2315 2266
2316 lines =<< trim END 2267 lines =<< trim END
2317 vim9script
2318 var d = {one: 1,two: 2} 2268 var d = {one: 1,two: 2}
2319 END 2269 END
2320 CheckScriptFailure(lines, 'E1069:', 2) 2270 CheckDefAndScriptFailure(lines, 'E1069:', 1)
2321 2271
2322 lines =<< trim END 2272 lines =<< trim END
2323 vim9script
2324 var d = {one : 1} 2273 var d = {one : 1}
2325 END 2274 END
2326 CheckScriptFailure(lines, 'E1068:', 2) 2275 CheckDefAndScriptFailure(lines, 'E1068:', 1)
2327 2276
2328 lines =<< trim END 2277 lines =<< trim END
2329 vim9script
2330 var d = {one:1} 2278 var d = {one:1}
2331 END 2279 END
2332 CheckScriptFailure(lines, 'E1069:', 2) 2280 CheckDefAndScriptFailure(lines, 'E1069:', 1)
2333 2281
2334 lines =<< trim END 2282 lines =<< trim END
2335 vim9script
2336 var d = {one: 1 , two: 2} 2283 var d = {one: 1 , two: 2}
2337 END 2284 END
2338 CheckScriptFailure(lines, 'E1068:', 2) 2285 CheckDefAndScriptFailure(lines, 'E1068:', 1)
2339 2286
2340 lines =<< trim END 2287 lines =<< trim END
2341 vim9script
2342 var l: dict<number> = {a: 234, b: 'x'} 2288 var l: dict<number> = {a: 234, b: 'x'}
2343 END 2289 END
2344 CheckScriptFailure(lines, 'E1012:', 2) 2290 CheckDefAndScriptFailure(lines, 'E1012:', 1)
2345 lines =<< trim END 2291
2346 vim9script 2292 lines =<< trim END
2347 var l: dict<number> = {a: 'x', b: 234} 2293 var l: dict<number> = {a: 'x', b: 234}
2348 END 2294 END
2349 CheckScriptFailure(lines, 'E1012:', 2) 2295 CheckDefAndScriptFailure(lines, 'E1012:', 1)
2350 lines =<< trim END 2296
2351 vim9script 2297 lines =<< trim END
2352 var l: dict<string> = {a: 'x', b: 234} 2298 var l: dict<string> = {a: 'x', b: 234}
2353 END 2299 END
2354 CheckScriptFailure(lines, 'E1012:', 2) 2300 CheckDefAndScriptFailure(lines, 'E1012:', 1)
2355 lines =<< trim END 2301
2356 vim9script 2302 lines =<< trim END
2357 var l: dict<string> = {a: 234, b: 'x'} 2303 var l: dict<string> = {a: 234, b: 'x'}
2358 END 2304 END
2359 CheckScriptFailure(lines, 'E1012:', 2) 2305 CheckDefAndScriptFailure(lines, 'E1012:', 1)
2360 2306
2361 lines =<< trim END 2307 lines =<< trim END
2362 vim9script
2363 var d = {['a']: 234, ['b': 'x'} 2308 var d = {['a']: 234, ['b': 'x'}
2364 END 2309 END
2365 CheckScriptFailure(lines, 'E1139:', 2) 2310 CheckDefAndScriptFailure(lines, 'E1139:', 1)
2366 lines =<< trim END 2311
2367 vim9script 2312 lines =<< trim END
2368 def Func() 2313 def Func()
2369 var d = {['a']: 234, ['b': 'x'} 2314 var d = {['a']: 234, ['b': 'x'}
2370 enddef 2315 enddef
2371 defcompile 2316 defcompile
2372 END 2317 END
2373 CheckScriptFailure(lines, 'E1139:', 1) 2318 CheckDefAndScriptFailure(lines, 'E1139:', 0)
2374 lines =<< trim END 2319
2375 vim9script 2320 lines =<< trim END
2376 var d = {'a': 2321 var d = {'a':
2377 END 2322 END
2378 CheckScriptFailure(lines, 'E15:', 2) 2323 CheckDefFailure(lines, 'E723:', 2)
2379 lines =<< trim END 2324 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2)
2380 vim9script 2325
2326 lines =<< trim END
2381 def Func() 2327 def Func()
2382 var d = {'a': 2328 var d = {'a':
2383 enddef 2329 enddef
2384 defcompile 2330 defcompile
2385 END 2331 END
2386 CheckScriptFailure(lines, 'E723:', 1) 2332 CheckDefAndScriptFailure(lines, 'E723:', 0)
2387 2333
2388 lines =<< trim END 2334 lines =<< trim END
2389 vim9script
2390 def Failing() 2335 def Failing()
2391 job_stop() 2336 job_stop()
2392 enddef 2337 enddef
2393 var dict = {name: Failing} 2338 var dict = {name: Failing}
2394 END 2339 END
2395 if has('channel') 2340 if has('channel')
2396 CheckScriptFailure(lines, 'E119:', 1) 2341 CheckDefAndScriptFailure(lines, 'E119:', 0)
2397 else 2342 else
2398 CheckScriptFailure(lines, 'E117:', 1) 2343 CheckDefAndScriptFailure(lines, 'E117:', 1)
2399 endif 2344 endif
2400 enddef 2345 enddef
2401 2346
2402 let g:oneString = 'one' 2347 let g:oneString = 'one'
2403 2348
2404 def Test_expr_member() 2349 def Test_expr_member()
2405 assert_equal(1, g:dict_one.one) 2350 var lines =<< trim END
2406 var d: dict<number> = g:dict_one 2351 assert_equal(1, g:dict_one.one)
2407 assert_equal(1, d['one']) 2352 var d: dict<number> = g:dict_one
2408 assert_equal(1, d[ 2353 assert_equal(1, d['one'])
2409 'one' 2354 assert_equal(1, d[
2410 ]) 2355 'one'
2411 assert_equal(1, d 2356 ])
2412 .one) 2357 assert_equal(1, d
2413 d = {1: 1, _: 2} 2358 .one)
2414 assert_equal(1, d 2359 d = {1: 1, _: 2}
2415 .1) 2360 assert_equal(1, d
2416 assert_equal(2, d 2361 .1)
2417 ._) 2362 assert_equal(2, d
2418 2363 ._)
2419 # getting the one member should clear the dict after getting the item 2364
2420 assert_equal('one', {one: 'one'}.one) 2365 # getting the one member should clear the dict after getting the item
2421 assert_equal('one', {one: 'one'}[g:oneString]) 2366 assert_equal('one', {one: 'one'}.one)
2422 2367 assert_equal('one', {one: 'one'}[g:oneString])
2423 CheckDefFailure(["var x = g:dict_one.#$!"], 'E1002:', 1) 2368 END
2424 CheckDefExecFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2) 2369 CheckDefAndScriptSuccess(lines)
2425 CheckDefExecFailure(["var d: dict<number>", "d = g:list_empty"], 'E1012: Type mismatch; expected dict<number> but got list<unknown>', 2) 2370
2371 CheckDefAndScriptFailure2(["var x = g:dict_one.#$!"], 'E1002:', 'E15:', 1)
2372 CheckDefExecAndScriptFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2)
2373 CheckDefExecAndScriptFailure(["var d: dict<number>", "d = g:list_empty"], 'E1012: Type mismatch; expected dict<number> but got list<unknown>', 2)
2426 enddef 2374 enddef
2427 2375
2428 def Test_expr7_any_index_slice() 2376 def Test_expr7_any_index_slice()
2429 var lines =<< trim END 2377 var lines =<< trim END
2430 # getting the one member should clear the list only after getting the item 2378 # getting the one member should clear the list only after getting the item
2516 g:testdict = {a: 1, b: 2} 2464 g:testdict = {a: 1, b: 2}
2517 assert_equal(1, g:testdict['a']) 2465 assert_equal(1, g:testdict['a'])
2518 assert_equal(2, g:testdict['b']) 2466 assert_equal(2, g:testdict['b'])
2519 END 2467 END
2520 2468
2521 CheckDefSuccess(lines) 2469 CheckDefAndScriptSuccess(lines)
2522 CheckScriptSuccess(['vim9script'] + lines) 2470
2523 2471 CheckDefExecAndScriptFailure(['echo g:testblob[2]'], 'E979:', 1)
2524 CheckDefExecFailure(['echo g:testblob[2]'], 'E979:', 1) 2472 CheckDefExecAndScriptFailure(['echo g:testblob[-3]'], 'E979:', 1)
2525 CheckScriptFailure(['vim9script', 'echo g:testblob[2]'], 'E979:', 2) 2473
2526 CheckDefExecFailure(['echo g:testblob[-3]'], 'E979:', 1) 2474 CheckDefExecAndScriptFailure(['echo g:testlist[4]'], 'E684: list index out of range: 4', 1)
2527 CheckScriptFailure(['vim9script', 'echo g:testblob[-3]'], 'E979:', 2) 2475 CheckDefExecAndScriptFailure(['echo g:testlist[-5]'], 'E684:', 1)
2528 2476
2529 CheckDefExecFailure(['echo g:testlist[4]'], 'E684: list index out of range: 4', 1) 2477 CheckDefExecAndScriptFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1)
2530 CheckScriptFailure(['vim9script', 'echo g:testlist[4]'], 'E684:', 2) 2478 CheckDefExecAndScriptFailure(['echo g:testdict[1]'], 'E716:', 1)
2531 CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1)
2532 CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684: list index out of range: -5', 2)
2533
2534 CheckDefExecFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1)
2535 CheckScriptFailure(['vim9script', 'echo g:testdict["a" : "b"]'], 'E719:', 2)
2536 CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1)
2537 CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2)
2538 2479
2539 unlet g:teststring 2480 unlet g:teststring
2540 unlet g:testblob 2481 unlet g:testblob
2541 unlet g:testlist 2482 unlet g:testlist
2542 enddef 2483 enddef
2543 2484
2544 def Test_expr_member_vim9script() 2485 def Test_expr_member_vim9script()
2545 var lines =<< trim END 2486 var lines =<< trim END
2546 vim9script
2547 var d = {one: 2487 var d = {one:
2548 'one', 2488 'one',
2549 two: 'two', 2489 two: 'two',
2550 1: 1, 2490 1: 1,
2551 _: 2} 2491 _: 2}
2558 ._) 2498 ._)
2559 assert_equal('one', d[ 2499 assert_equal('one', d[
2560 'one' 2500 'one'
2561 ]) 2501 ])
2562 END 2502 END
2563 CheckScriptSuccess(lines) 2503 CheckDefAndScriptSuccess(lines)
2564 2504
2565 lines =<< trim END 2505 lines =<< trim END
2566 vim9script
2567 var l = [1, 2506 var l = [1,
2568 2, 2507 2,
2569 3, 4 2508 3, 4
2570 ] 2509 ]
2571 assert_equal(2, l[ 2510 assert_equal(2, l[
2579 assert_equal([3, 4], l[ 2518 assert_equal([3, 4], l[
2580 2 2519 2
2581 : 2520 :
2582 ]) 2521 ])
2583 END 2522 END
2584 CheckScriptSuccess(lines) 2523 CheckDefAndScriptSuccess(lines)
2585 enddef 2524 enddef
2586 2525
2587 def SetSomeVar() 2526 def SetSomeVar()
2588 b:someVar = &fdm 2527 b:someVar = &fdm
2589 enddef 2528 enddef
2590 2529
2591 def Test_expr7_option() 2530 def Test_expr7_option()
2592 # option 2531 var lines =<< trim END
2593 set ts=11 2532 # option
2594 assert_equal(11, &ts) 2533 set ts=11
2595 &ts = 9 2534 assert_equal(11, &ts)
2596 assert_equal(9, &ts) 2535 &ts = 9
2597 set ts=8 2536 assert_equal(9, &ts)
2598 set grepprg=some\ text 2537 set ts=8
2599 assert_equal('some text', &grepprg) 2538 set grepprg=some\ text
2600 &grepprg = test_null_string() 2539 assert_equal('some text', &grepprg)
2601 assert_equal('', &grepprg) 2540 &grepprg = test_null_string()
2602 set grepprg& 2541 assert_equal('', &grepprg)
2603 2542 set grepprg&
2604 # check matching type 2543
2605 var bval: bool = &tgc 2544 # check matching type
2606 var nval: number = &ts 2545 var bval: bool = &tgc
2607 var sval: string = &path 2546 var nval: number = &ts
2608 2547 var sval: string = &path
2609 # check v_lock is cleared (requires using valgrind, doesn't always show) 2548
2610 SetSomeVar() 2549 # check v_lock is cleared (requires using valgrind, doesn't always show)
2611 b:someVar = 0 2550 SetSomeVar()
2612 unlet b:someVar 2551 b:someVar = 0
2552 unlet b:someVar
2553 END
2554 CheckDefAndScriptSuccess(lines)
2613 enddef 2555 enddef
2614 2556
2615 def Test_expr7_environment() 2557 def Test_expr7_environment()
2616 # environment variable 2558 var lines =<< trim END
2617 assert_equal('testvar', $TESTVAR) 2559 # environment variable
2618 assert_equal('', $ASDF_ASD_XXX) 2560 assert_equal('testvar', $TESTVAR)
2619 2561 assert_equal('', $ASDF_ASD_XXX)
2620 CheckDefFailure(["var x = $$$"], 'E1002:', 1) 2562 END
2563 CheckDefAndScriptSuccess(lines)
2564
2565 CheckDefAndScriptFailure2(["var x = $$$"], 'E1002:', 'E15:', 1)
2621 enddef 2566 enddef
2622 2567
2623 def Test_expr7_register() 2568 def Test_expr7_register()
2624 @a = 'register a' 2569 var lines =<< trim END
2625 assert_equal('register a', @a) 2570 @a = 'register a'
2626 2571 assert_equal('register a', @a)
2627 var fname = expand('%') 2572
2628 assert_equal(fname, @%) 2573 var fname = expand('%')
2629 2574 assert_equal(fname, @%)
2630 feedkeys(":echo 'some'\<CR>", "xt") 2575
2631 assert_equal("echo 'some'", @:) 2576 feedkeys(":echo 'some'\<CR>", "xt")
2632 2577 assert_equal("echo 'some'", @:)
2633 normal axyz 2578
2634 assert_equal("xyz", @.) 2579 normal axyz
2635 CheckDefFailure(["@. = 'yes'"], 'E354:', 1) 2580 assert_equal("xyz", @.)
2636 2581
2637 @/ = 'slash' 2582 @/ = 'slash'
2638 assert_equal('slash', @/) 2583 assert_equal('slash', @/)
2639 2584
2640 @= = 'equal' 2585 @= = 'equal'
2641 assert_equal('equal', @=) 2586 assert_equal('equal', @=)
2587 END
2588 CheckDefAndScriptSuccess(lines)
2589
2590 CheckDefAndScriptFailure2(["@. = 'yes'"], 'E354:', 'E488:', 1)
2642 enddef 2591 enddef
2643 2592
2644 def Test_expr7_namespace() 2593 def Test_expr7_namespace()
2645 g:some_var = 'some' 2594 var lines =<< trim END
2646 assert_equal('some', get(g:, 'some_var')) 2595 g:some_var = 'some'
2647 assert_equal('some', get(g:, 'some_var', 'xxx')) 2596 assert_equal('some', get(g:, 'some_var'))
2648 assert_equal('xxx', get(g:, 'no_var', 'xxx')) 2597 assert_equal('some', get(g:, 'some_var', 'xxx'))
2649 unlet g:some_var 2598 assert_equal('xxx', get(g:, 'no_var', 'xxx'))
2650 2599 unlet g:some_var
2651 b:some_var = 'some' 2600
2652 assert_equal('some', get(b:, 'some_var')) 2601 b:some_var = 'some'
2653 assert_equal('some', get(b:, 'some_var', 'xxx')) 2602 assert_equal('some', get(b:, 'some_var'))
2654 assert_equal('xxx', get(b:, 'no_var', 'xxx')) 2603 assert_equal('some', get(b:, 'some_var', 'xxx'))
2655 unlet b:some_var 2604 assert_equal('xxx', get(b:, 'no_var', 'xxx'))
2656 2605 unlet b:some_var
2657 w:some_var = 'some' 2606
2658 assert_equal('some', get(w:, 'some_var')) 2607 w:some_var = 'some'
2659 assert_equal('some', get(w:, 'some_var', 'xxx')) 2608 assert_equal('some', get(w:, 'some_var'))
2660 assert_equal('xxx', get(w:, 'no_var', 'xxx')) 2609 assert_equal('some', get(w:, 'some_var', 'xxx'))
2661 unlet w:some_var 2610 assert_equal('xxx', get(w:, 'no_var', 'xxx'))
2662 2611 unlet w:some_var
2663 t:some_var = 'some' 2612
2664 assert_equal('some', get(t:, 'some_var')) 2613 t:some_var = 'some'
2665 assert_equal('some', get(t:, 'some_var', 'xxx')) 2614 assert_equal('some', get(t:, 'some_var'))
2666 assert_equal('xxx', get(t:, 'no_var', 'xxx')) 2615 assert_equal('some', get(t:, 'some_var', 'xxx'))
2667 unlet t:some_var 2616 assert_equal('xxx', get(t:, 'no_var', 'xxx'))
2668 2617 unlet t:some_var
2669 # check using g: in a for loop more than DO_NOT_FREE_CNT times 2618
2670 for i in range(100000) 2619 # check using g: in a for loop more than DO_NOT_FREE_CNT times
2671 if has_key(g:, 'does-not-exist') 2620 for i in range(100000)
2672 endif 2621 if has_key(g:, 'does-not-exist')
2673 endfor 2622 endif
2623 endfor
2624 END
2625 CheckDefAndScriptSuccess(lines)
2674 enddef 2626 enddef
2675 2627
2676 def Test_expr7_parens() 2628 def Test_expr7_parens()
2677 # (expr) 2629 # (expr)
2678 var lines =<< trim END 2630 var lines =<< trim END
2704 END 2656 END
2705 CheckDefAndScriptSuccess(lines) 2657 CheckDefAndScriptSuccess(lines)
2706 enddef 2658 enddef
2707 2659
2708 def Test_expr7_negate_add() 2660 def Test_expr7_negate_add()
2709 assert_equal(-99, -99) 2661 var lines =<< trim END
2710 assert_equal(-99, - 99) 2662 assert_equal(-99, -99)
2711 assert_equal(99, +99) 2663 assert_equal(-99, - 99)
2712 2664 assert_equal(99, +99)
2713 var nr = 88 2665
2714 assert_equal(-88, -nr) 2666 var nr = 88
2715 assert_equal(-88, - nr) 2667 assert_equal(-88, -nr)
2716 assert_equal(88, + nr) 2668 assert_equal(-88, - nr)
2717 2669 assert_equal(88, + nr)
2718 var lines =<< trim END 2670 END
2671 CheckDefAndScriptSuccess(lines)
2672
2673 lines =<< trim END
2719 var n = 12 2674 var n = 12
2720 echo ++n 2675 echo ++n
2721 END 2676 END
2722 CheckDefAndScriptFailure(lines, 'E15:') 2677 CheckDefAndScriptFailure(lines, 'E15:')
2723 lines =<< trim END 2678 lines =<< trim END
2777 CheckDefAndScriptSuccess(lines) 2732 CheckDefAndScriptSuccess(lines)
2778 2733
2779 assert_equal('yes', 'yes' 2734 assert_equal('yes', 'yes'
2780 ->s:Echo4Arg()) 2735 ->s:Echo4Arg())
2781 2736
2782 CheckDefFailure(["var x = 'yes'->Echo"], 'E107:', 1) 2737 CheckDefAndScriptFailure(["var x = 'yes'->Echo"], 'E107:', 1)
2783 CheckScriptFailure([ 2738 CheckDefAndScriptFailure2([
2784 "vim9script", 2739 "var x = substitute ('x', 'x', 'x', 'x')"
2785 "var x = substitute ('x', 'x', 'x', 'x')" 2740 ], 'E1001:', 'E121:', 1)
2786 ], 'E121:', 2) 2741 CheckDefAndScriptFailure2(["var Ref = function('len' [1, 2])"], 'E1123:', 'E116:', 1)
2787 CheckDefFailure(["var Ref = function('len' [1, 2])"], 'E1123:', 1)
2788 2742
2789 var auto_lines =<< trim END 2743 var auto_lines =<< trim END
2790 def g:some#func(): string 2744 def g:some#func(): string
2791 return 'found' 2745 return 'found'
2792 enddef 2746 enddef
2801 &rtp = save_rtp 2755 &rtp = save_rtp
2802 delete('Xruntime', 'rf') 2756 delete('Xruntime', 'rf')
2803 enddef 2757 enddef
2804 2758
2805 def Test_expr7_method_call() 2759 def Test_expr7_method_call()
2806 new 2760 var lines =<< trim END
2807 setline(1, ['first', 'last']) 2761 new
2808 'second'->append(1) 2762 setline(1, ['first', 'last'])
2809 "third"->append(2) 2763 'second'->append(1)
2810 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$')) 2764 "third"->append(2)
2811 bwipe! 2765 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$'))
2812 2766 bwipe!
2813 var bufnr = bufnr() 2767
2814 var loclist = [{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 2768 var bufnr = bufnr()
2815 loclist->setloclist(0) 2769 var loclist = [{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}]
2816 assert_equal([{bufnr: bufnr, 2770 loclist->setloclist(0)
2817 lnum: 42, 2771 assert_equal([{bufnr: bufnr,
2818 col: 17, 2772 lnum: 42,
2819 text: 'wrong', 2773 col: 17,
2820 pattern: '', 2774 text: 'wrong',
2821 valid: 1, 2775 pattern: '',
2822 vcol: 0, 2776 valid: 1,
2823 nr: 0, 2777 vcol: 0,
2824 type: '', 2778 nr: 0,
2825 module: ''} 2779 type: '',
2826 ], getloclist(0)) 2780 module: ''}
2827 2781 ], getloclist(0))
2828 var result: bool = get({n: 0}, 'n', 0) 2782
2829 assert_equal(false, result) 2783 var result: bool = get({n: 0}, 'n', 0)
2830 2784 assert_equal(false, result)
2831 assert_equal('+string+', 'string'->((s) => '+' .. s .. '+')()) 2785
2832 assert_equal('-text-', 'text'->((s, c) => c .. s .. c)('-')) 2786 assert_equal('+string+', 'string'->((s) => '+' .. s .. '+')())
2833 2787 assert_equal('-text-', 'text'->((s, c) => c .. s .. c)('-'))
2834 var Join = (l) => join(l, 'x') 2788
2835 assert_equal('axb', ['a', 'b']->(Join)()) 2789 var Join = (l) => join(l, 'x')
2790 assert_equal('axb', ['a', 'b']->(Join)())
2791 END
2792 CheckDefAndScriptSuccess(lines)
2836 enddef 2793 enddef
2837 2794
2838 2795
2839 def Test_expr7_not() 2796 def Test_expr7_not()
2840 var lines =<< trim END 2797 var lines =<< trim END
2886 CheckDefAndScriptSuccess(lines) 2843 CheckDefAndScriptSuccess(lines)
2887 enddef 2844 enddef
2888 2845
2889 func Test_expr7_fails() 2846 func Test_expr7_fails()
2890 call CheckDefFailure(["var x = (12"], "E1097:", 3) 2847 call CheckDefFailure(["var x = (12"], "E1097:", 3)
2891 2848 call CheckScriptFailure(['vim9script', "var x = (12"], 'E110:', 2)
2892 call CheckDefFailure(["var x = -'xx'"], "E1030:", 1) 2849
2893 call CheckDefFailure(["var x = +'xx'"], "E1030:", 1) 2850 call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1)
2894 call CheckDefFailure(["var x = -0z12"], "E974:", 1) 2851 call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1)
2895 call CheckDefExecFailure(["var x = -[8]"], "E39:", 1) 2852 call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1)
2896 call CheckDefExecFailure(["var x = -{a: 1}"], "E39:", 1) 2853 call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E39:", 'E745:', 1)
2897 2854 call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E39:", 'E728:', 1)
2898 call CheckDefFailure(["var x = @"], "E1002:", 1) 2855
2899 call CheckDefFailure(["var x = @<"], "E354:", 1) 2856 call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1)
2857 call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1)
2900 2858
2901 call CheckDefFailure(["var x = [1, 2"], "E697:", 2) 2859 call CheckDefFailure(["var x = [1, 2"], "E697:", 2)
2902 call CheckDefFailure(["var x = [notfound]"], "E1001:", 1) 2860 call CheckScriptFailure(['vim9script', "var x = [1, 2"], 'E696:', 2)
2903 2861
2904 call CheckDefFailure(["var X = () => 123)"], "E488:", 1) 2862 call CheckDefAndScriptFailure2(["var x = [notfound]"], "E1001:", 'E121:', 1)
2905 call CheckDefFailure(["var x = 123->((x) => x + 5)"], "E107:", 1) 2863
2906 2864 call CheckDefAndScriptFailure2(["var X = () => 123)"], "E488:", 'E15:', 1)
2907 call CheckDefFailure(["var x = &notexist"], 'E113:', 1) 2865 call CheckDefAndScriptFailure(["var x = 123->((x) => x + 5)"], "E107:", 1)
2908 call CheckDefFailure(["&grepprg = [343]"], 'E1012:', 1) 2866
2909 2867 call CheckDefAndScriptFailure(["var x = &notexist"], 'E113:', 1)
2910 call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:', 1) 2868 call CheckDefAndScriptFailure2(["&grepprg = [343]"], 'E1012:', 'E730:', 1)
2911 call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:', 1) 2869
2912 2870 call CheckDefExecAndScriptFailure(["echo s:doesnt_exist"], 'E121:', 1)
2913 call CheckDefFailure(["echo a:somevar"], 'E1075:', 1) 2871 call CheckDefExecAndScriptFailure(["echo g:doesnt_exist"], 'E121:', 1)
2914 call CheckDefFailure(["echo l:somevar"], 'E1075:', 1) 2872
2915 call CheckDefFailure(["echo x:somevar"], 'E1075:', 1) 2873 call CheckDefAndScriptFailure2(["echo a:somevar"], 'E1075:', 'E121:', 1)
2916 2874 call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1)
2917 call CheckDefExecFailure(["var x = +g:astring"], 'E1030:', 1) 2875 call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1)
2918 call CheckDefExecFailure(["var x = +g:ablob"], 'E974:', 1) 2876
2919 call CheckDefExecFailure(["var x = +g:alist"], 'E745:', 1) 2877 call CheckDefExecAndScriptFailure(["var x = +g:astring"], 'E1030:', 1)
2920 call CheckDefExecFailure(["var x = +g:adict"], 'E728:', 1) 2878 call CheckDefExecAndScriptFailure(["var x = +g:ablob"], 'E974:', 1)
2921 2879 call CheckDefExecAndScriptFailure(["var x = +g:alist"], 'E745:', 1)
2922 call CheckDefFailure(["var x = ''", "var y = x.memb"], 'E715:', 2) 2880 call CheckDefExecAndScriptFailure(["var x = +g:adict"], 'E728:', 1)
2923 2881
2924 call CheckDefFailure(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 1) 2882 call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E715:', 'E15:', 2)
2883
2884 call CheckDefAndScriptFailure2(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 'E260: Missing name after ->', 1)
2925 2885
2926 call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2) 2886 call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2)
2927 call CheckDefExecFailure(["{a: 1->len()"], 'E723:', 2) 2887 call CheckScriptFailure(['vim9script', "[1, 2->len()"], 'E696:', 2)
2888
2889 call CheckDefFailure(["{a: 1->len()"], 'E723:', 2)
2890 call CheckScriptFailure(['vim9script', "{a: 1->len()"], 'E722:', 2)
2891
2928 call CheckDefExecFailure(["{['a']: 1->len()"], 'E723:', 2) 2892 call CheckDefExecFailure(["{['a']: 1->len()"], 'E723:', 2)
2893 call CheckScriptFailure(['vim9script', "{['a']: 1->len()"], 'E722:', 2)
2929 endfunc 2894 endfunc
2930 2895
2931 let g:Funcrefs = [function('add')] 2896 let g:Funcrefs = [function('add')]
2932 2897
2933 func CallMe(arg) 2898 func CallMe(arg)
2937 func CallMe2(one, two) 2902 func CallMe2(one, two)
2938 return a:one .. a:two 2903 return a:one .. a:two
2939 endfunc 2904 endfunc
2940 2905
2941 def Test_expr7_trailing() 2906 def Test_expr7_trailing()
2942 # user function call 2907 var lines =<< trim END
2943 assert_equal(123, g:CallMe(123)) 2908 # user function call
2944 assert_equal(123, g:CallMe( 123)) 2909 assert_equal(123, g:CallMe(123))
2945 assert_equal(123, g:CallMe(123 )) 2910 assert_equal(123, g:CallMe( 123))
2946 assert_equal('yesno', g:CallMe2('yes', 'no')) 2911 assert_equal(123, g:CallMe(123 ))
2947 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 2912 assert_equal('yesno', g:CallMe2('yes', 'no'))
2948 assert_equal('nothing', g:CallMe('nothing')) 2913 assert_equal('yesno', g:CallMe2( 'yes', 'no' ))
2949 2914 assert_equal('nothing', g:CallMe('nothing'))
2950 # partial call 2915
2951 var Part = function('g:CallMe') 2916 # partial call
2952 assert_equal('yes', Part('yes')) 2917 var Part = function('g:CallMe')
2953 2918 assert_equal('yes', Part('yes'))
2954 # funcref call, using list index 2919
2955 var l = [] 2920 # funcref call, using list index
2956 g:Funcrefs[0](l, 2) 2921 var l = []
2957 assert_equal([2], l) 2922 g:Funcrefs[0](l, 2)
2958 2923 assert_equal([2], l)
2959 # method call 2924
2960 l = [2, 5, 6] 2925 # method call
2961 l->map((k, v) => k + v) 2926 l = [2, 5, 6]
2962 assert_equal([2, 6, 8], l) 2927 l->map((k, v) => k + v)
2963 2928 assert_equal([2, 6, 8], l)
2964 # lambda method call 2929
2965 l = [2, 5] 2930 # lambda method call
2966 l->((ll) => add(ll, 8))() 2931 l = [2, 5]
2967 assert_equal([2, 5, 8], l) 2932 l->((ll) => add(ll, 8))()
2968 2933 assert_equal([2, 5, 8], l)
2969 # dict member 2934
2970 var d = {key: 123} 2935 # dict member
2971 assert_equal(123, d.key) 2936 var d = {key: 123}
2937 assert_equal(123, d.key)
2938 END
2939 CheckDefAndScriptSuccess(lines)
2972 enddef 2940 enddef
2973 2941
2974 def Test_expr7_string_subscript() 2942 def Test_expr7_string_subscript()
2975 var lines =<< trim END 2943 var lines =<< trim END
2976 var text = 'abcdef' 2944 var text = 'abcdef'
3020 2988
3021 assert_equal('ábçd', text[: 3]) 2989 assert_equal('ábçd', text[: 3])
3022 assert_equal('bçdëf', text[1 :]) 2990 assert_equal('bçdëf', text[1 :])
3023 assert_equal('ábçdëf', text[:]) 2991 assert_equal('ábçdëf', text[:])
3024 END 2992 END
3025 CheckDefSuccess(lines) 2993 CheckDefAndScriptSuccess(lines)
3026 CheckScriptSuccess(['vim9script'] + lines)
3027 2994
3028 lines =<< trim END 2995 lines =<< trim END
3029 var d = 'asdf'[1 : 2996 var d = 'asdf'[1 :
3030 END 2997 END
3031 CheckDefFailure(lines, 'E1097:', 3) 2998 CheckDefFailure(lines, 'E1097:', 3)
2999 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2)
3000
3032 lines =<< trim END 3001 lines =<< trim END
3033 var d = 'asdf'[1 : xxx] 3002 var d = 'asdf'[1 : xxx]
3034 END 3003 END
3035 CheckDefFailure(lines, 'E1001:', 1) 3004 CheckDefAndScriptFailure2(lines, 'E1001:', 'E121:', 1)
3005
3036 lines =<< trim END 3006 lines =<< trim END
3037 var d = 'asdf'[1 : 2 3007 var d = 'asdf'[1 : 2
3038 END 3008 END
3039 CheckDefFailure(lines, 'E1097:', 3) 3009 CheckDefFailure(lines, 'E1097:', 3)
3010 CheckScriptFailure(['vim9script'] + lines, 'E111:', 2)
3011
3040 lines =<< trim END 3012 lines =<< trim END
3041 var d = 'asdf'[1 : 2 3013 var d = 'asdf'[1 : 2
3042 echo d 3014 echo d
3043 END 3015 END
3044 CheckDefFailure(lines, 'E111:', 2) 3016 CheckDefAndScriptFailure(lines, 'E111:', 2)
3017
3045 lines =<< trim END 3018 lines =<< trim END
3046 var d = 'asdf'['1'] 3019 var d = 'asdf'['1']
3047 echo d 3020 echo d
3048 END 3021 END
3049 CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1) 3022 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1)
3023
3050 lines =<< trim END 3024 lines =<< trim END
3051 var d = 'asdf'['1' : 2] 3025 var d = 'asdf'['1' : 2]
3052 echo d 3026 echo d
3053 END 3027 END
3054 CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1) 3028 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1)
3029
3055 lines =<< trim END 3030 lines =<< trim END
3056 var d = 'asdf'[1 : '2'] 3031 var d = 'asdf'[1 : '2']
3057 echo d 3032 echo d
3058 END 3033 END
3059 CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1) 3034 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "2"', 1)
3060 enddef 3035 enddef
3061 3036
3062 def Test_expr7_list_subscript() 3037 def Test_expr7_list_subscript()
3063 var lines =<< trim END 3038 var lines =<< trim END
3064 var list = [0, 1, 2, 3, 4] 3039 var list = [0, 1, 2, 3, 4]
3065 assert_equal(0, list[0]) 3040 assert_equal(0, list[0])
3066 assert_equal(4, list[4]) 3041 assert_equal(4, list[4])
3067 assert_equal(4, list[-1]) 3042 assert_equal(4, list[-1])
3068 assert_equal(0, list[-5]) 3043 assert_equal(0, list[-5])
3069 3044
3070 assert_equal([0, 1, 2, 3, 4], list[0 : 4]) 3045 assert_equal([0, 1, 2, 3, 4], list[0 : 4])
3071 assert_equal([0, 1, 2, 3, 4], list[:]) 3046 assert_equal([0, 1, 2, 3, 4], list[:])
3072 assert_equal([1, 2, 3, 4], list[1 :]) 3047 assert_equal([1, 2, 3, 4], list[1 :])
3073 assert_equal([2, 3, 4], list[2 : -1]) 3048 assert_equal([2, 3, 4], list[2 : -1])
3074 assert_equal([4], list[4 : -1]) 3049 assert_equal([4], list[4 : -1])
3075 assert_equal([], list[5 : -1]) 3050 assert_equal([], list[5 : -1])
3076 assert_equal([], list[999 : -1]) 3051 assert_equal([], list[999 : -1])
3077 assert_equal([1, 2, 3, 4], list[g:theone : g:thefour]) 3052 assert_equal([1, 2, 3, 4], list[g:theone : g:thefour])
3078 3053
3079 assert_equal([0, 1, 2, 3], list[0 : 3]) 3054 assert_equal([0, 1, 2, 3], list[0 : 3])
3080 assert_equal([0], list[0 : 0]) 3055 assert_equal([0], list[0 : 0])
3081 assert_equal([0, 1, 2, 3, 4], list[0 : -1]) 3056 assert_equal([0, 1, 2, 3, 4], list[0 : -1])
3082 assert_equal([0, 1, 2], list[0 : -3]) 3057 assert_equal([0, 1, 2], list[0 : -3])
3083 assert_equal([0], list[0 : -5]) 3058 assert_equal([0], list[0 : -5])
3084 assert_equal([], list[0 : -6]) 3059 assert_equal([], list[0 : -6])
3085 assert_equal([], list[0 : -99]) 3060 assert_equal([], list[0 : -99])
3086 END 3061 END
3087 CheckDefAndScriptSuccess(lines) 3062 CheckDefAndScriptSuccess(lines)
3088 3063
3089 lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 3064 lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]']
3090 CheckDefExecFailure(lines, 'E1012:') 3065 CheckDefExecAndScriptFailure2(lines, 'E1012:', 'E1030:', 2)
3091 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 3) 3066
3092 3067 lines =<< trim END
3093 lines =<< trim END
3094 vim9script
3095 var ld = [] 3068 var ld = []
3096 def Func() 3069 def Func()
3097 eval ld[0].key 3070 eval ld[0].key
3098 enddef 3071 enddef
3099 defcompile 3072 defcompile
3100 END 3073 END
3101 CheckScriptSuccess(lines) 3074 CheckDefAndScriptSuccess(lines)
3102 enddef 3075 enddef
3103 3076
3104 def Test_expr7_dict_subscript() 3077 def Test_expr7_dict_subscript()
3105 var lines =<< trim END 3078 var lines =<< trim END
3106 vim9script
3107 var l = [{lnum: 2}, {lnum: 1}] 3079 var l = [{lnum: 2}, {lnum: 1}]
3108 var res = l[0].lnum > l[1].lnum 3080 var res = l[0].lnum > l[1].lnum
3109 assert_true(res) 3081 assert_true(res)
3110 3082
3111 var dd = {} 3083 var dd = {}
3115 def Func2() 3087 def Func2()
3116 eval dd['key1'].key2 3088 eval dd['key1'].key2
3117 enddef 3089 enddef
3118 defcompile 3090 defcompile
3119 END 3091 END
3120 CheckScriptSuccess(lines) 3092 CheckDefAndScriptSuccess(lines)
3121 enddef 3093 enddef
3122 3094
3123 def Test_expr7_subscript_linebreak() 3095 def Test_expr7_subscript_linebreak()
3124 var range = range( 3096 var lines =<< trim END
3125 3) 3097 var range = range(
3126 var l = range 3098 3)
3127 ->mapnew('string(v:key)') 3099 var l = range
3128 assert_equal(['0', '1', '2'], l) 3100 ->mapnew('string(v:key)')
3129 3101 assert_equal(['0', '1', '2'], l)
3130 l = range 3102
3131 ->mapnew('string(v:key)') 3103 l = range
3132 assert_equal(['0', '1', '2'], l) 3104 ->mapnew('string(v:key)')
3133 3105 assert_equal(['0', '1', '2'], l)
3134 l = range # comment 3106
3135 ->mapnew('string(v:key)') 3107 l = range # comment
3136 assert_equal(['0', '1', '2'], l) 3108 ->mapnew('string(v:key)')
3137 3109 assert_equal(['0', '1', '2'], l)
3138 l = range 3110
3139 3111 l = range
3140 ->mapnew('string(v:key)') 3112
3141 assert_equal(['0', '1', '2'], l) 3113 ->mapnew('string(v:key)')
3142 3114 assert_equal(['0', '1', '2'], l)
3143 l = range 3115
3144 # comment 3116 l = range
3145 ->mapnew('string(v:key)') 3117 # comment
3146 assert_equal(['0', '1', '2'], l) 3118 ->mapnew('string(v:key)')
3147 3119 assert_equal(['0', '1', '2'], l)
3148 assert_equal('1', l[ 3120
3149 1]) 3121 assert_equal('1', l[
3150 3122 1])
3151 var d = {one: 33} 3123
3152 assert_equal(33, d. 3124 var d = {one: 33}
3153 one) 3125 assert_equal(33, d
3126 .one)
3127 END
3128 CheckDefAndScriptSuccess(lines)
3129
3130 lines =<< trim END
3131 var d = {one: 33}
3132 assert_equal(33, d.
3133 one)
3134 END
3135 CheckDefAndScriptFailure2(lines, 'E1127:', 'E116:', 2)
3154 enddef 3136 enddef
3155 3137
3156 func Test_expr7_trailing_fails() 3138 func Test_expr7_trailing_fails()
3157 call CheckDefFailure(['var l = [2]', 'l->((ll) => add(ll, 8))'], 'E107:', 2) 3139 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8))'], 'E107:', 2)
3158 call CheckDefFailure(['var l = [2]', 'l->((ll) => add(ll, 8)) ()'], 'E274:', 2) 3140 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8)) ()'], 'E274:', 2)
3159 endfunc 3141 endfunc
3160 3142
3161 func Test_expr_fails() 3143 func Test_expr_fails()
3162 call CheckDefFailure(["var x = '1'is2"], 'E488:', 1) 3144 call CheckDefAndScriptFailure2(["var x = '1'is2"], 'E488:', 'E15:', 1)
3163 call CheckDefFailure(["var x = '1'isnot2"], 'E488:', 1) 3145 call CheckDefAndScriptFailure2(["var x = '1'isnot2"], 'E488:', 'E15:', 1)
3164 3146
3165 call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1) 3147 call CheckDefAndScriptFailure2(["CallMe ('yes')"], 'E476:', 'E492:', 1)
3166 call CheckScriptFailure(["CallMe ('yes')"], 'E492:', 1) 3148
3167 call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1) 3149 call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1)
3168 call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1) 3150
3169 3151 call CheckDefAndScriptFailure2(["v:nosuch += 3"], 'E1001:', 'E121:', 1)
3170 call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1) 3152 call CheckDefAndScriptFailure(["var v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1)
3171 call CheckDefFailure(["var v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1) 3153 call CheckDefAndScriptFailure2(["var asdf = v:nosuch"], 'E1001:', 'E121:', 1)
3172 call CheckDefFailure(["var asdf = v:nosuch"], 'E1001:', 1)
3173 3154
3174 call CheckDefFailure(["echo len('asdf'"], 'E110:', 2) 3155 call CheckDefFailure(["echo len('asdf'"], 'E110:', 2)
3175 call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 1) 3156 call CheckScriptFailure(['vim9script', "echo len('asdf'"], 'E116:', 2)
3176 call CheckDefFailure(["echo doesnotexist()"], 'E117:', 1) 3157
3158 call CheckDefAndScriptFailure2(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 'E117:', 1)
3159 call CheckDefAndScriptFailure(["echo doesnotexist()"], 'E117:', 1)
3177 endfunc 3160 endfunc
3178 3161
3179 " vim: shiftwidth=2 sts=2 expandtab 3162 " vim: shiftwidth=2 sts=2 expandtab