1" Tests for Vim9 script expressions 2 3source check.vim 4source vim9.vim 5 6let g:cond = v:false 7def FuncOne(arg: number): string 8 return 'yes' 9enddef 10def FuncTwo(arg: number): number 11 return 123 12enddef 13 14" test cond ? expr : expr 15def Test_expr1_trinary() 16 var lines =<< trim END 17 assert_equal('one', true ? 'one' : 'two') 18 assert_equal('one', 1 ? 19 'one' : 20 'two') 21 if has('float') 22 assert_equal('one', !!0.1 ? 'one' : 'two') 23 endif 24 assert_equal('one', !!'x' ? 'one' : 'two') 25 assert_equal('one', !!'x' 26 ? 'one' 27 : 'two') 28 assert_equal('one', !!0z1234 ? 'one' : 'two') 29 assert_equal('one', !![0] ? 'one' : 'two') 30 assert_equal('one', !!{x: 0} ? 'one' : 'two') 31 var name = 1 32 assert_equal('one', name ? 'one' : 'two') 33 34 assert_equal('two', false ? 'one' : 'two') 35 assert_equal('two', 0 ? 'one' : 'two') 36 if has('float') 37 assert_equal('two', !!0.0 ? 'one' : 'two') 38 endif 39 assert_equal('two', !!'' ? 'one' : 'two') 40 assert_equal('two', !!0z ? 'one' : 'two') 41 assert_equal('two', !![] ? 'one' : 'two') 42 assert_equal('two', !!{} ? 'one' : 'two') 43 name = 0 44 assert_equal('two', name ? 'one' : 'two') 45 46 echo ['a'] + (1 ? ['b'] : ['c'] 47 ) 48 echo ['a'] + (1 ? ['b'] : ['c'] # comment 49 ) 50 51 # with constant condition expression is not evaluated 52 assert_equal('one', 1 ? 'one' : xxx) 53 54 var Some: func = function('len') 55 var Other: func = function('winnr') 56 var Res: func = g:atrue ? Some : Other 57 assert_equal(function('len'), Res) 58 59 var RetOne: func(string): number = function('len') 60 var RetTwo: func(string): number = function('winnr') 61 var RetThat: func = g:atrue ? RetOne : RetTwo 62 assert_equal(function('len'), RetThat) 63 64 var X = FuncOne 65 var Y = FuncTwo 66 var Z = g:cond ? FuncOne : FuncTwo 67 assert_equal(123, Z(3)) 68 END 69 CheckDefAndScriptSuccess(lines) 70enddef 71 72def Test_expr1_trinary_vimscript() 73 # check line continuation 74 var lines =<< trim END 75 var name = 1 76 ? 'yes' 77 : 'no' 78 assert_equal('yes', name) 79 END 80 CheckDefAndScriptSuccess(lines) 81 82 lines =<< trim END 83 var name = v:false 84 ? 'yes' 85 : 'no' 86 assert_equal('no', name) 87 END 88 CheckDefAndScriptSuccess(lines) 89 90 lines =<< trim END 91 var name = v:false ? 92 'yes' : 93 'no' 94 assert_equal('no', name) 95 END 96 CheckDefAndScriptSuccess(lines) 97 98 lines =<< trim END 99 var name = v:false ? # comment 100 'yes' : 101 # comment 102 'no' # comment 103 assert_equal('no', name) 104 END 105 CheckDefAndScriptSuccess(lines) 106 107 # check white space 108 lines =<< trim END 109 var name = v:true?1:2 110 END 111 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''?'' at "?1:2"', 1) 112 113 lines =<< trim END 114 var name = v:true? 1 : 2 115 END 116 CheckDefAndScriptFailure(lines, 'E1004:', 1) 117 118 lines =<< trim END 119 var name = v:true ?1 : 2 120 END 121 CheckDefAndScriptFailure(lines, 'E1004:', 1) 122 123 lines =<< trim END 124 var name = v:true ? 1: 2 125 END 126 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after '':'' at ": 2"', 1) 127 128 lines =<< trim END 129 var name = v:true ? 1 :2 130 END 131 CheckDefAndScriptFailure(lines, 'E1004:', 1) 132 133 lines =<< trim END 134 var name = 'x' ? 1 : 2 135 END 136 CheckDefAndScriptFailure(lines, 'E1135:', 1) 137 138 lines =<< trim END 139 var name = [] ? 1 : 2 140 END 141 CheckDefExecAndScriptFailure(lines, 'E745:', 1) 142 143 lines =<< trim END 144 var name = {} ? 1 : 2 145 END 146 CheckDefExecAndScriptFailure(lines, 'E728:', 1) 147 148 # check after failure eval_flags is reset 149 lines =<< trim END 150 try 151 eval('0 ? 1: 2') 152 catch 153 endtry 154 assert_equal(v:true, eval(string(v:true))) 155 END 156 CheckDefAndScriptSuccess(lines) 157 158 lines =<< trim END 159 try 160 eval('0 ? 1 :2') 161 catch 162 endtry 163 assert_equal(v:true, eval(string(v:true))) 164 END 165 CheckDefAndScriptSuccess(lines) 166enddef 167 168func Test_expr1_trinary_fails() 169 call CheckDefAndScriptFailure(["var x = 1 ? 'one'"], "Missing ':' after '?'", 1) 170 171 let msg = "White space required before and after '?'" 172 call CheckDefAndScriptFailure(["var x = 1? 'one' : 'two'"], msg, 1) 173 call CheckDefAndScriptFailure(["var x = 1 ?'one' : 'two'"], msg, 1) 174 call CheckDefAndScriptFailure(["var x = 1?'one' : 'two'"], msg, 1) 175 176 let msg = "White space required before and after ':'" 177 call CheckDefAndScriptFailure(["var x = 1 ? 'one': 'two'"], msg, 1) 178 call CheckDefAndScriptFailure(["var x = 1 ? 'one' :'two'"], msg, 1) 179 call CheckDefAndScriptFailure(["var x = 1 ? 'one':'two'"], msg, 1) 180 181 call CheckDefAndScriptFailure(["var x = 'x' ? 'one' : 'two'"], 'E1135:', 1) 182 call CheckDefAndScriptFailure(["var x = 0z1234 ? 'one' : 'two'"], 'E974:', 1) 183 call CheckDefExecAndScriptFailure(["var x = [] ? 'one' : 'two'"], 'E745:', 1) 184 call CheckDefExecAndScriptFailure(["var x = {} ? 'one' : 'two'"], 'E728:', 1) 185 186 call CheckDefExecFailure(["var x = false ? "], 'E1097:', 3) 187 call CheckScriptFailure(['vim9script', "var x = false ? "], 'E15:', 2) 188 call CheckDefExecFailure(["var x = false ? 'one' : "], 'E1097:', 3) 189 call CheckScriptFailure(['vim9script', "var x = false ? 'one' : "], 'E15:', 2) 190 191 call CheckDefExecAndScriptFailure2(["var x = true ? xxx : 'foo'"], 'E1001:', 'E121:', 1) 192 call CheckDefExecAndScriptFailure2(["var x = false ? 'foo' : xxx"], 'E1001:', 'E121:', 1) 193 194 if has('float') 195 call CheckDefAndScriptFailure(["var x = 0.1 ? 'one' : 'two'"], 'E805:', 1) 196 endif 197 198 " missing argument detected even when common type is used 199 call CheckDefAndScriptFailure([ 200 \ 'var X = FuncOne', 201 \ 'var Y = FuncTwo', 202 \ 'var Z = g:cond ? FuncOne : FuncTwo', 203 \ 'Z()'], 'E119:', 4) 204endfunc 205 206def Test_expr1_falsy() 207 var lines =<< trim END 208 assert_equal(v:true, v:true ?? 456) 209 assert_equal(123, 123 ?? 456) 210 assert_equal('yes', 'yes' ?? 456) 211 assert_equal([1], [1] ?? 456) 212 assert_equal({one: 1}, {one: 1} ?? 456) 213 if has('float') 214 assert_equal(0.1, 0.1 ?? 456) 215 endif 216 217 assert_equal(456, v:false ?? 456) 218 assert_equal(456, 0 ?? 456) 219 assert_equal(456, '' ?? 456) 220 assert_equal(456, [] ?? 456) 221 assert_equal(456, {} ?? 456) 222 if has('float') 223 assert_equal(456, 0.0 ?? 456) 224 endif 225 END 226 CheckDefAndScriptSuccess(lines) 227 228 var msg = "White space required before and after '??'" 229 call CheckDefAndScriptFailure(["var x = 1?? 'one' : 'two'"], msg, 1) 230 call CheckDefAndScriptFailure(["var x = 1 ??'one' : 'two'"], msg, 1) 231 call CheckDefAndScriptFailure(["var x = 1??'one' : 'two'"], msg, 1) 232enddef 233 234def Record(val: any): any 235 g:vals->add(val) 236 return val 237enddef 238 239" test || 240def Test_expr2() 241 var lines =<< trim END 242 assert_equal(true, 1 || 0) 243 assert_equal(true, 0 || 244 0 || 245 1) 246 assert_equal(true, 0 || 247 0 || 248 !!7) 249 assert_equal(false, 0 || 0) 250 assert_equal(false, 0 251 || 0) 252 assert_equal(false, 0 || false) 253 254 g:vals = [] 255 assert_equal(true, Record(1) || Record(3)) 256 assert_equal([1], g:vals) 257 258 g:vals = [] 259 assert_equal(true, Record(0) || Record(1)) 260 assert_equal([0, 1], g:vals) 261 262 g:vals = [] 263 assert_equal(true, Record(0) || Record(true)) 264 assert_equal([0, true], g:vals) 265 266 g:vals = [] 267 assert_equal(true, Record(0) 268 || Record(1) 269 || Record(0)) 270 assert_equal([0, 1], g:vals) 271 272 g:vals = [] 273 assert_equal(true, Record(0) 274 || Record(true) 275 || Record(0)) 276 assert_equal([0, true], g:vals) 277 278 g:vals = [] 279 assert_equal(true, Record(true) || Record(false)) 280 assert_equal([true], g:vals) 281 282 g:vals = [] 283 assert_equal(false, Record(0) || Record(false) || Record(0)) 284 assert_equal([0, false, 0], g:vals) 285 END 286 CheckDefAndScriptSuccess(lines) 287enddef 288 289def Test_expr2_vimscript() 290 # check line continuation 291 var lines =<< trim END 292 var name = 0 293 || 1 294 assert_equal(true, name) 295 END 296 CheckDefAndScriptSuccess(lines) 297 298 lines =<< trim END 299 var name = v:false 300 || v:true 301 || v:false 302 assert_equal(v:true, name) 303 END 304 CheckDefAndScriptSuccess(lines) 305 306 lines =<< trim END 307 var name = v:false || 308 v:true || 309 v:false 310 assert_equal(v:true, name) 311 END 312 CheckDefAndScriptSuccess(lines) 313 314 lines =<< trim END 315 var name = v:false || # comment 316 # comment 317 v:true || 318 # comment 319 v:false # comment 320 assert_equal(v:true, name) 321 END 322 CheckDefAndScriptSuccess(lines) 323 324 # check white space 325 lines =<< trim END 326 var name = v:true||v:true 327 END 328 CheckDefExecAndScriptFailure(lines, 'E1004: White space required before and after ''||'' at "||v:true"', 1) 329 330 lines =<< trim END 331 var name = v:true ||v:true 332 END 333 CheckDefAndScriptFailure(lines, 'E1004:', 1) 334 335 lines =<< trim END 336 var name = v:true|| v:true 337 END 338 CheckDefAndScriptFailure(lines, 'E1004:', 1) 339enddef 340 341def Test_expr2_fails() 342 var msg = "White space required before and after '||'" 343 call CheckDefAndScriptFailure(["var x = 1||2"], msg, 1) 344 call CheckDefAndScriptFailure(["var x = 1 ||2"], msg, 1) 345 call CheckDefAndScriptFailure(["var x = 1|| 2"], msg, 1) 346 347 call CheckDefFailure(["var x = false || "], 'E1097:', 3) 348 call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2) 349 350 # script does not fail, the second expression is skipped 351 call CheckDefFailure(["var x = 1 || xxx"], 'E1001:', 1) 352 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) 356 357 # TODO: should fail at compile time 358 call CheckDefExecAndScriptFailure(["var x = 3 || 7"], 'E1023:', 1) 359 360 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012: Type mismatch; expected bool but got list<unknown>', 'E745:', 1) 361 362enddef 363 364" test && 365def Test_expr3() 366 var lines =<< trim END 367 assert_equal(false, 1 && 0) 368 assert_equal(false, 0 && 369 0 && 370 1) 371 assert_equal(true, 1 372 && true 373 && 1) 374 assert_equal(false, 0 && 0) 375 assert_equal(false, 0 && false) 376 assert_equal(true, 1 && true) 377 378 g:vals = [] 379 assert_equal(true, Record(true) && Record(1)) 380 assert_equal([true, 1], g:vals) 381 382 g:vals = [] 383 assert_equal(true, Record(1) && Record(true)) 384 assert_equal([1, true], g:vals) 385 386 g:vals = [] 387 assert_equal(false, Record(0) && Record(1)) 388 assert_equal([0], g:vals) 389 390 g:vals = [] 391 assert_equal(false, Record(0) && Record(1) && Record(0)) 392 assert_equal([0], g:vals) 393 394 g:vals = [] 395 assert_equal(false, Record(0) && Record(4) && Record(0)) 396 assert_equal([0], g:vals) 397 398 g:vals = [] 399 assert_equal(false, Record(1) && Record(true) && Record(0)) 400 assert_equal([1, true, 0], g:vals) 401 END 402 CheckDefAndScriptSuccess(lines) 403enddef 404 405def Test_expr3_vimscript() 406 # check line continuation 407 var lines =<< trim END 408 var name = 0 409 && 1 410 assert_equal(false, name) 411 END 412 CheckDefAndScriptSuccess(lines) 413 414 lines =<< trim END 415 var name = v:true 416 && v:true 417 && v:true 418 assert_equal(v:true, name) 419 END 420 CheckDefAndScriptSuccess(lines) 421 422 lines =<< trim END 423 var name = v:true && 424 v:true && 425 v:true 426 assert_equal(v:true, name) 427 END 428 CheckDefAndScriptSuccess(lines) 429 430 lines =<< trim END 431 var name = v:true && # comment 432 # comment 433 v:true && 434 # comment 435 v:true 436 assert_equal(v:true, name) 437 END 438 CheckDefAndScriptSuccess(lines) 439 440 # check white space 441 lines =<< trim END 442 var name = v:true&&v:true 443 END 444 CheckDefAndScriptFailure(lines, 'E1004:', 1) 445 446 lines =<< trim END 447 var name = v:true &&v:true 448 END 449 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&v:true"', 1) 450 451 lines =<< trim END 452 var name = v:true&& v:true 453 END 454 CheckDefAndScriptFailure(lines, 'E1004:', 1) 455enddef 456 457def Test_expr3_fails() 458 var msg = "White space required before and after '&&'" 459 CheckDefAndScriptFailure(["var x = 1&&2"], msg, 1) 460 CheckDefAndScriptFailure(["var x = 1 &&2"], msg, 1) 461 CheckDefAndScriptFailure(["var x = 1&& 2"], msg, 1) 462 463 g:vals = [] 464 CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) 465 466 CheckDefExecAndScriptFailure(['assert_equal(false, Record(1) && Record(4) && Record(0))'], 'E1023: Using a Number as a Bool: 4', 1) 467 468 var lines =<< trim END 469 if 3 470 && true 471 endif 472 END 473 CheckDefExecAndScriptFailure(lines, 'E1023:', 1) 474 475 lines =<< trim END 476 if 'yes' 477 && true 478 endif 479 END 480 CheckDefAndScriptFailure2(lines, 'E1012:', 'E1135: Using a String as a Bool', 1) 481enddef 482 483" global variables to use for tests with the "any" type 484let atrue = v:true 485let afalse = v:false 486let anone = v:none 487let anull = v:null 488let anint = 10 489let theone = 1 490let thefour = 4 491if has('float') 492 let afloat = 0.1 493endif 494let astring = 'asdf' 495let ablob = 0z01ab 496let alist = [2, 3, 4] 497let adict = #{aaa: 2, bbb: 8} 498 499" test == comperator 500def Test_expr4_equal() 501 var lines =<< trim END 502 var trueVar = true 503 var falseVar = false 504 assert_equal(true, true == true) 505 assert_equal(false, true == 506 false) 507 assert_equal(true, true 508 == trueVar) 509 assert_equal(false, true == falseVar) 510 assert_equal(true, true == g:atrue) 511 assert_equal(false, g:atrue == false) 512 513 assert_equal(true, v:none == v:none) 514 assert_equal(false, v:none == v:null) 515 assert_equal(true, g:anone == v:none) 516 assert_equal(true, null == v:null) 517 assert_equal(true, null == g:anull) 518 assert_equal(false, v:none == g:anull) 519 520 var nr0 = 0 521 var nr61 = 61 522 assert_equal(false, 2 == 0) 523 assert_equal(false, 2 == nr0) 524 assert_equal(true, 61 == 61) 525 assert_equal(true, 61 == nr61) 526 assert_equal(true, g:anint == 10) 527 assert_equal(false, 61 == g:anint) 528 529 if has('float') 530 var ff = 0.3 531 assert_equal(true, ff == 0.3) 532 assert_equal(false, 0.4 == ff) 533 assert_equal(true, 0.1 == g:afloat) 534 assert_equal(false, g:afloat == 0.3) 535 536 ff = 3.0 537 assert_equal(true, ff == 3) 538 assert_equal(true, 3 == ff) 539 ff = 3.1 540 assert_equal(false, ff == 3) 541 assert_equal(false, 3 == ff) 542 endif 543 544 assert_equal(true, 'abc' == 'abc') 545 assert_equal(false, 'xyz' == 'abc') 546 assert_equal(true, g:astring == 'asdf') 547 assert_equal(false, 'xyz' == g:astring) 548 549 assert_equal(false, 'abc' == 'aBc') 550 assert_equal(false, 'abc' ==# 'aBc') 551 assert_equal(true, 'abc' ==? 'aBc') 552 553 assert_equal(false, 'abc' == 'ABC') 554 set ignorecase 555 assert_equal(false, 'abc' == 'ABC') 556 assert_equal(false, 'abc' ==# 'ABC') 557 assert_equal(true, 'abc' ==? 'ABC') 558 set noignorecase 559 560 var bb = 0z3f 561 assert_equal(true, 0z3f == bb) 562 assert_equal(false, bb == 0z4f) 563 assert_equal(true, g:ablob == 0z01ab) 564 assert_equal(false, 0z3f == g:ablob) 565 566 assert_equal(true, [1, 2, 3] == [1, 2, 3]) 567 assert_equal(false, [1, 2, 3] == [2, 3, 1]) 568 assert_equal(true, [2, 3, 4] == g:alist) 569 assert_equal(false, g:alist == [2, 3, 1]) 570 assert_equal(false, [1, 2, 3] == []) 571 assert_equal(false, [1, 2, 3] == ['1', '2', '3']) 572 573 assert_equal(true, {one: 1, two: 2} == {one: 1, two: 2}) 574 assert_equal(false, {one: 1, two: 2} == {one: 2, two: 2}) 575 assert_equal(false, {one: 1, two: 2} == {two: 2}) 576 assert_equal(false, {one: 1, two: 2} == {}) 577 assert_equal(true, g:adict == {bbb: 8, aaa: 2}) 578 assert_equal(false, {ccc: 9, aaa: 2} == g:adict) 579 580 assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal')) 581 assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is')) 582 583 assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123])) 584 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123])) 585 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999])) 586 587 # TODO: this unexpectedly sometimes fails on Appveyor 588 if !has('win32') 589 var OneFunc: func 590 var TwoFunc: func 591 OneFunc = function('len') 592 TwoFunc = function('len') 593 assert_equal(true, OneFunc('abc') == TwoFunc('123')) 594 endif 595 END 596 CheckDefAndScriptSuccess(lines) 597 598 CheckDefAndScriptFailure2(["var x = 'a' == xxx"], 'E1001:', 'E121:', 1) 599 CheckDefFailure(["var x = 'a' == "], 'E1097:', 3) 600 CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2) 601 602 CheckDefExecAndScriptFailure2(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4) 603 604 CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2) 605 CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 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) 608 609 for op in ['>', '>=', '<', '<=', '=~', '!~'] 610 CheckDefExecAndScriptFailure([ 611 "var a: any = 'a'", 612 'var b: any = true', 613 'echo a ' .. op .. ' b'], 'E1072:', 3) 614 endfor 615enddef 616 617" test != comperator 618def Test_expr4_notequal() 619 var lines =<< trim END 620 var trueVar = true 621 var falseVar = false 622 assert_equal(false, true != true) 623 assert_equal(true, true != 624 false) 625 assert_equal(false, true 626 != trueVar) 627 assert_equal(true, true != falseVar) 628 assert_equal(false, true != g:atrue) 629 assert_equal(true, g:atrue != false) 630 631 assert_equal(false, v:none != v:none) 632 assert_equal(true, v:none != v:null) 633 assert_equal(false, g:anone != v:none) 634 assert_equal(true, v:none != g:anull) 635 636 var nr55 = 55 637 var nr0 = 55 638 assert_equal(true, 2 != 0) 639 assert_equal(true, 2 != nr0) 640 assert_equal(false, 55 != 55) 641 assert_equal(false, 55 != nr55) 642 assert_equal(false, g:anint != 10) 643 assert_equal(true, 61 != g:anint) 644 645 if has('float') 646 var ff = 0.3 647 assert_equal(false, 0.3 != ff) 648 assert_equal(true, 0.4 != ff) 649 assert_equal(false, 0.1 != g:afloat) 650 assert_equal(true, g:afloat != 0.3) 651 652 ff = 3.0 653 assert_equal(false, ff != 3) 654 assert_equal(false, 3 != ff) 655 ff = 3.1 656 assert_equal(true, ff != 3) 657 assert_equal(true, 3 != ff) 658 endif 659 660 assert_equal(false, 'abc' != 'abc') 661 assert_equal(true, 'xyz' != 'abc') 662 assert_equal(false, g:astring != 'asdf') 663 assert_equal(true, 'xyz' != g:astring) 664 665 assert_equal(true, 'abc' != 'ABC') 666 set ignorecase 667 assert_equal(true, 'abc' != 'ABC') 668 assert_equal(true, 'abc' !=# 'ABC') 669 assert_equal(false, 'abc' !=? 'ABC') 670 set noignorecase 671 672 var bb = 0z3f 673 assert_equal(false, 0z3f != bb) 674 assert_equal(true, bb != 0z4f) 675 assert_equal(false, g:ablob != 0z01ab) 676 assert_equal(true, 0z3f != g:ablob) 677 678 assert_equal(false, [1, 2, 3] != [1, 2, 3]) 679 assert_equal(true, [1, 2, 3] != [2, 3, 1]) 680 assert_equal(false, [2, 3, 4] != g:alist) 681 assert_equal(true, g:alist != [2, 3, 1]) 682 assert_equal(true, [1, 2, 3] != []) 683 assert_equal(true, [1, 2, 3] != ['1', '2', '3']) 684 685 assert_equal(false, {one: 1, two: 2} != {one: 1, two: 2}) 686 assert_equal(true, {one: 1, two: 2} != {one: 2, two: 2}) 687 assert_equal(true, {one: 1, two: 2} != {two: 2}) 688 assert_equal(true, {one: 1, two: 2} != {}) 689 assert_equal(false, g:adict != {bbb: 8, aaa: 2}) 690 assert_equal(true, {ccc: 9, aaa: 2} != g:adict) 691 692 assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal')) 693 assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is')) 694 695 assert_equal(false, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [123])) 696 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_is', [123])) 697 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [999])) 698 END 699 CheckDefAndScriptSuccess(lines) 700enddef 701 702" test > comperator 703def Test_expr4_greater() 704 var lines =<< trim END 705 assert_true(2 > 0) 706 assert_true(2 > 707 1) 708 assert_false(2 > 2) 709 assert_false(2 > 3) 710 var nr2 = 2 711 assert_true(nr2 > 0) 712 assert_true(nr2 > 713 1) 714 assert_false(nr2 > 2) 715 assert_false(nr2 716 > 3) 717 if has('float') 718 var ff = 2.0 719 assert_true(ff > 0.0) 720 assert_true(ff > 1.0) 721 assert_false(ff > 2.0) 722 assert_false(ff > 3.0) 723 endif 724 END 725 CheckDefAndScriptSuccess(lines) 726enddef 727 728" test >= comperator 729def Test_expr4_greaterequal() 730 var lines =<< trim END 731 assert_true(2 >= 0) 732 assert_true(2 >= 733 2) 734 assert_false(2 >= 3) 735 var nr2 = 2 736 assert_true(nr2 >= 0) 737 assert_true(nr2 >= 2) 738 assert_false(nr2 >= 3) 739 if has('float') 740 var ff = 2.0 741 assert_true(ff >= 0.0) 742 assert_true(ff >= 2.0) 743 assert_false(ff >= 3.0) 744 endif 745 END 746 CheckDefAndScriptSuccess(lines) 747enddef 748 749" test < comperator 750def Test_expr4_smaller() 751 var lines =<< trim END 752 assert_false(2 < 0) 753 assert_false(2 < 754 2) 755 assert_true(2 756 < 3) 757 var nr2 = 2 758 assert_false(nr2 < 0) 759 assert_false(nr2 < 2) 760 assert_true(nr2 < 3) 761 if has('float') 762 var ff = 2.0 763 assert_false(ff < 0.0) 764 assert_false(ff < 2.0) 765 assert_true(ff < 3.0) 766 endif 767 END 768 CheckDefAndScriptSuccess(lines) 769enddef 770 771" test <= comperator 772def Test_expr4_smallerequal() 773 var lines =<< trim END 774 assert_false(2 <= 0) 775 assert_false(2 <= 776 1) 777 assert_true(2 778 <= 2) 779 assert_true(2 <= 3) 780 var nr2 = 2 781 assert_false(nr2 <= 0) 782 assert_false(nr2 <= 1) 783 assert_true(nr2 <= 2) 784 assert_true(nr2 <= 3) 785 if has('float') 786 var ff = 2.0 787 assert_false(ff <= 0.0) 788 assert_false(ff <= 1.0) 789 assert_true(ff <= 2.0) 790 assert_true(ff <= 3.0) 791 endif 792 END 793 CheckDefAndScriptSuccess(lines) 794enddef 795 796" test =~ comperator 797def Test_expr4_match() 798 var lines =<< trim END 799 assert_equal(false, '2' =~ '0') 800 assert_equal(false, '' 801 =~ '0') 802 assert_equal(true, '2' =~ 803 '[0-9]') 804 set ignorecase 805 assert_equal(false, 'abc' =~ 'ABC') 806 assert_equal(false, 'abc' =~# 'ABC') 807 assert_equal(true, 'abc' =~? 'ABC') 808 set noignorecase 809 END 810 CheckDefAndScriptSuccess(lines) 811enddef 812 813" test !~ comperator 814def Test_expr4_nomatch() 815 var lines =<< trim END 816 assert_equal(true, '2' !~ '0') 817 assert_equal(true, '' 818 !~ '0') 819 assert_equal(false, '2' !~ 820 '[0-9]') 821 END 822 CheckDefAndScriptSuccess(lines) 823enddef 824 825" test is comperator 826def Test_expr4_is() 827 var lines =<< trim END 828 var mylist = [2] 829 assert_false(mylist is [2]) 830 var other = mylist 831 assert_true(mylist is 832 other) 833 834 var myblob = 0z1234 835 assert_false(myblob 836 is 0z1234) 837 var otherblob = myblob 838 assert_true(myblob is otherblob) 839 END 840 CheckDefAndScriptSuccess(lines) 841enddef 842 843" test isnot comperator 844def Test_expr4_isnot() 845 var lines =<< trim END 846 var mylist = [2] 847 assert_true('2' isnot '0') 848 assert_true(mylist isnot [2]) 849 var other = mylist 850 assert_false(mylist isnot 851 other) 852 853 var myblob = 0z1234 854 assert_true(myblob 855 isnot 0z1234) 856 var otherblob = myblob 857 assert_false(myblob isnot otherblob) 858 END 859 CheckDefAndScriptSuccess(lines) 860enddef 861 862def RetVoid() 863 var x = 1 864enddef 865 866def Test_expr4_vim9script() 867 # check line continuation 868 var lines =<< trim END 869 var name = 0 870 < 1 871 assert_equal(true, name) 872 END 873 CheckDefAndScriptSuccess(lines) 874 875 lines =<< trim END 876 var name = 123 877 # comment 878 != 123 879 assert_equal(false, name) 880 END 881 CheckDefAndScriptSuccess(lines) 882 883 lines =<< trim END 884 var name = 123 == 885 123 886 assert_equal(true, name) 887 END 888 CheckDefAndScriptSuccess(lines) 889 890 lines =<< trim END 891 var list = [1, 2, 3] 892 var name = list 893 is list 894 assert_equal(true, name) 895 END 896 CheckDefAndScriptSuccess(lines) 897 898 lines =<< trim END 899 var list = [1, 2, 3] 900 var name = list # comment 901 # comment 902 is list 903 assert_equal(true, name) 904 END 905 CheckDefAndScriptSuccess(lines) 906 907 lines =<< trim END 908 var myblob = 0z1234 909 var name = myblob 910 isnot 0z11 911 assert_equal(true, name) 912 END 913 CheckDefAndScriptSuccess(lines) 914 915 # spot check mismatching types 916 lines =<< trim END 917 echo '' == 0 918 END 919 CheckDefAndScriptFailure(lines, 'E1072:', 1) 920 921 lines =<< trim END 922 echo v:true > v:false 923 END 924 CheckDefAndScriptFailure(lines, 'Cannot compare bool with bool', 1) 925 926 lines =<< trim END 927 echo 123 is 123 928 END 929 CheckDefAndScriptFailure(lines, 'Cannot use "is" with number', 1) 930 931 # check missing white space 932 lines =<< trim END 933 echo 2>3 934 END 935 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''>'' at ">3"', 1) 936 937 lines =<< trim END 938 echo 2 >3 939 END 940 CheckDefAndScriptFailure(lines, 'E1004:', 1) 941 942 lines =<< trim END 943 echo 2> 3 944 END 945 CheckDefAndScriptFailure(lines, 'E1004:', 1) 946 947 lines =<< trim END 948 echo 2!=3 949 END 950 CheckDefAndScriptFailure(lines, 'E1004:', 1) 951 952 lines =<< trim END 953 echo 2 !=3 954 END 955 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''!='' at "!=3"', 1) 956 957 lines =<< trim END 958 echo 2!= 3 959 END 960 CheckDefAndScriptFailure(lines, 'E1004:', 1) 961 962 lines =<< trim END 963 echo len('xxx') == 3 964 END 965 CheckDefAndScriptSuccess(lines) 966 967 lines =<< trim END 968 var line = 'abc' 969 echo line[1] =~ '\w' 970 END 971 CheckDefAndScriptSuccess(lines) 972enddef 973 974func Test_expr4_fails() 975 let msg = "White space required before and after '>'" 976 call CheckDefAndScriptFailure(["var x = 1>2"], msg, 1) 977 call CheckDefAndScriptFailure(["var x = 1 >2"], msg, 1) 978 call CheckDefAndScriptFailure(["var x = 1> 2"], msg, 1) 979 980 let msg = "White space required before and after '=='" 981 call CheckDefAndScriptFailure(["var x = 1==2"], msg, 1) 982 call CheckDefAndScriptFailure(["var x = 1 ==2"], msg, 1) 983 call CheckDefAndScriptFailure(["var x = 1== 2"], msg, 1) 984 985 let msg = "White space required before and after 'is'" 986 call CheckDefAndScriptFailure(["var x = '1'is'2'"], msg, 1) 987 call CheckDefAndScriptFailure(["var x = '1' is'2'"], msg, 1) 988 call CheckDefAndScriptFailure(["var x = '1'is '2'"], msg, 1) 989 990 let msg = "White space required before and after 'isnot'" 991 call CheckDefAndScriptFailure(["var x = '1'isnot'2'"], msg, 1) 992 call CheckDefAndScriptFailure(["var x = '1' isnot'2'"], msg, 1) 993 call CheckDefAndScriptFailure(["var x = '1'isnot '2'"], msg, 1) 994 995 call CheckDefAndScriptFailure(["var x = 1 is# 2"], 'E15:', 1) 996 call CheckDefAndScriptFailure(["var x = 1 is? 2"], 'E15:', 1) 997 call CheckDefAndScriptFailure(["var x = 1 isnot# 2"], 'E15:', 1) 998 call CheckDefAndScriptFailure(["var x = 1 isnot? 2"], 'E15:', 1) 999 1000 call CheckDefAndScriptFailure(["var x = 1 == '2'"], 'Cannot compare number with string', 1) 1001 call CheckDefAndScriptFailure(["var x = '1' == 2"], 'Cannot compare string with number', 1) 1002 call CheckDefAndScriptFailure(["var x = 1 == RetVoid()"], 'Cannot compare number with void', 1) 1003 call CheckDefAndScriptFailure(["var x = RetVoid() == 1"], 'Cannot compare void with number', 1) 1004 1005 call CheckDefAndScriptFailure(["var x = true > false"], 'Cannot compare bool with bool', 1) 1006 call CheckDefAndScriptFailure(["var x = true >= false"], 'Cannot compare bool with bool', 1) 1007 call CheckDefAndScriptFailure(["var x = true < false"], 'Cannot compare bool with bool', 1) 1008 call CheckDefAndScriptFailure(["var x = true <= false"], 'Cannot compare bool with bool', 1) 1009 call CheckDefAndScriptFailure(["var x = true =~ false"], 'Cannot compare bool with bool', 1) 1010 call CheckDefAndScriptFailure(["var x = true !~ false"], 'Cannot compare bool with bool', 1) 1011 call CheckDefAndScriptFailure(["var x = true is false"], 'Cannot use "is" with bool', 1) 1012 call CheckDefAndScriptFailure(["var x = true isnot false"], 'Cannot use "isnot" with bool', 1) 1013 1014 call CheckDefAndScriptFailure(["var x = v:none is v:null"], 'Cannot use "is" with special', 1) 1015 call CheckDefAndScriptFailure(["var x = v:none isnot v:null"], 'Cannot use "isnot" with special', 1) 1016 call CheckDefAndScriptFailure(["var x = 123 is 123"], 'Cannot use "is" with number', 1) 1017 call CheckDefAndScriptFailure(["var x = 123 isnot 123"], 'Cannot use "isnot" with number', 1) 1018 if has('float') 1019 call CheckDefAndScriptFailure(["var x = 1.3 is 1.3"], 'Cannot use "is" with float', 1) 1020 call CheckDefAndScriptFailure(["var x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float', 1) 1021 endif 1022 1023 call CheckDefAndScriptFailure(["var x = 0za1 > 0z34"], 'Cannot compare blob with blob', 1) 1024 call CheckDefAndScriptFailure(["var x = 0za1 >= 0z34"], 'Cannot compare blob with blob', 1) 1025 call CheckDefAndScriptFailure(["var x = 0za1 < 0z34"], 'Cannot compare blob with blob', 1) 1026 call CheckDefAndScriptFailure(["var x = 0za1 <= 0z34"], 'Cannot compare blob with blob', 1) 1027 call CheckDefAndScriptFailure(["var x = 0za1 =~ 0z34"], 'Cannot compare blob with blob', 1) 1028 call CheckDefAndScriptFailure(["var x = 0za1 !~ 0z34"], 'Cannot compare blob with blob', 1) 1029 1030 call CheckDefAndScriptFailure(["var x = [13] > [88]"], 'Cannot compare list with list', 1) 1031 call CheckDefAndScriptFailure(["var x = [13] >= [88]"], 'Cannot compare list with list', 1) 1032 call CheckDefAndScriptFailure(["var x = [13] < [88]"], 'Cannot compare list with list', 1) 1033 call CheckDefAndScriptFailure(["var x = [13] <= [88]"], 'Cannot compare list with list', 1) 1034 call CheckDefAndScriptFailure(["var x = [13] =~ [88]"], 'Cannot compare list with list', 1) 1035 call CheckDefAndScriptFailure(["var x = [13] !~ [88]"], 'Cannot compare list with list', 1) 1036 1037 call CheckDefAndScriptFailure(['var j: job', 'var chan: channel', 'var r = j == chan'], 'Cannot compare job with channel', 3) 1038 call CheckDefAndScriptFailure(['var j: job', 'var x: list<any>', 'var r = j == x'], 'Cannot compare job with list', 3) 1039 call CheckDefAndScriptFailure(['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) 1041endfunc 1042 1043" test addition, subtraction, concatenation 1044def Test_expr5() 1045 var lines =<< trim END 1046 assert_equal(66, 60 + 6) 1047 assert_equal(70, 60 + 1048 g:anint) 1049 assert_equal(9, g:thefour 1050 + 5) 1051 assert_equal(14, g:thefour + g:anint) 1052 assert_equal([1, 2, 3, 4], [1] + g:alist) 1053 1054 assert_equal(54, 60 - 6) 1055 assert_equal(50, 60 - 1056 g:anint) 1057 assert_equal(-1, g:thefour 1058 - 5) 1059 assert_equal(-6, g:thefour - g:anint) 1060 1061 assert_equal('hello', 'hel' .. 'lo') 1062 assert_equal('hello 123', 'hello ' .. 1063 123) 1064 assert_equal('hello 123', 'hello ' 1065 .. 123) 1066 assert_equal('123 hello', 123 .. ' hello') 1067 assert_equal('123456', 123 .. 456) 1068 1069 assert_equal('atrue', 'a' .. true) 1070 assert_equal('afalse', 'a' .. false) 1071 assert_equal('anull', 'a' .. v:null) 1072 assert_equal('av:none', 'a' .. v:none) 1073 if has('float') 1074 assert_equal('a0.123', 'a' .. 0.123) 1075 endif 1076 1077 assert_equal(3, 1 + [2, 3, 4][0]) 1078 assert_equal(5, 2 + {key: 3}['key']) 1079 1080 set digraph 1081 assert_equal('val: true', 'val: ' .. &digraph) 1082 set nodigraph 1083 assert_equal('val: false', 'val: ' .. &digraph) 1084 1085 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) 1086 assert_equal(0z11223344, 0z1122 + 0z3344) 1087 assert_equal(0z112201ab, 0z1122 1088 + g:ablob) 1089 assert_equal(0z01ab3344, g:ablob + 0z3344) 1090 assert_equal(0z01ab01ab, g:ablob + g:ablob) 1091 1092 # concatenate non-constant to constant 1093 var save_path = &path 1094 &path = 'b' 1095 assert_equal('ab', 'a' .. &path) 1096 &path = save_path 1097 1098 @b = 'b' 1099 assert_equal('ab', 'a' .. @b) 1100 1101 $ENVVAR = 'env' 1102 assert_equal('aenv', 'a' .. $ENVVAR) 1103 1104 assert_equal('val', '' .. {key: 'val'}['key']) 1105 END 1106 CheckDefAndScriptSuccess(lines) 1107enddef 1108 1109def Test_expr5_vim9script() 1110 # check line continuation 1111 var lines =<< trim END 1112 var name = 11 1113 + 77 1114 - 22 1115 assert_equal(66, name) 1116 END 1117 CheckDefAndScriptSuccess(lines) 1118 1119 lines =<< trim END 1120 var name = 11 + 1121 77 - 1122 22 1123 assert_equal(66, name) 1124 END 1125 CheckDefAndScriptSuccess(lines) 1126 1127 lines =<< trim END 1128 var name = 11 + # comment 1129 77 - 1130 # comment 1131 22 1132 assert_equal(66, name) 1133 END 1134 CheckDefAndScriptSuccess(lines) 1135 1136 lines =<< trim END 1137 var name = 'one' 1138 .. 'two' 1139 assert_equal('onetwo', name) 1140 END 1141 CheckDefAndScriptSuccess(lines) 1142 1143 lines =<< trim END 1144 echo 'abc' is# 'abc' 1145 END 1146 CheckDefAndScriptFailure(lines, 'E15:', 1) 1147 1148 lines =<< trim END 1149 echo {} - 22 1150 END 1151 CheckDefAndScriptFailure2(lines, 'E1036:', 'E728:', 1) 1152 1153 lines =<< trim END 1154 echo [] - 33 1155 END 1156 CheckDefAndScriptFailure2(lines, 'E1036:', 'E745:', 1) 1157 1158 lines =<< trim END 1159 echo 0z1234 - 44 1160 END 1161 CheckDefAndScriptFailure2(lines, 'E1036', 'E974:', 1) 1162 1163 lines =<< trim END 1164 echo 'abc' is? 'abc' 1165 END 1166 CheckDefAndScriptFailure(lines, 'E15:', 1) 1167 1168 lines =<< trim END 1169 echo 'abc' isnot# 'abc' 1170 END 1171 CheckDefAndScriptFailure(lines, 'E15:', 1) 1172 1173 lines =<< trim END 1174 echo 'abc' isnot? 'abc' 1175 END 1176 CheckDefAndScriptFailure(lines, 'E15:', 1) 1177 1178 # check white space 1179 lines =<< trim END 1180 echo 5+6 1181 END 1182 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1183 lines =<< trim END 1184 echo 5 +6 1185 END 1186 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1187 1188 lines =<< trim END 1189 echo 5+ 6 1190 END 1191 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1192 1193 lines =<< trim END 1194 echo 'a'..'b' 1195 END 1196 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at "..''b''"', 1) 1197 1198 lines =<< trim END 1199 echo 'a' ..'b' 1200 END 1201 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1202 1203 lines =<< trim END 1204 echo 'a'.. 'b' 1205 END 1206 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1207 1208 # check invalid string concatenation 1209 lines =<< trim END 1210 echo 'a' .. [1] 1211 END 1212 CheckDefAndScriptFailure2(lines, 'E1105:', 'E730:', 1) 1213 1214 lines =<< trim END 1215 echo 'a' .. {a: 1} 1216 END 1217 CheckDefAndScriptFailure2(lines, 'E1105:', 'E731:', 1) 1218 1219 lines =<< trim END 1220 echo 'a' .. test_void() 1221 END 1222 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1) 1223 1224 lines =<< trim END 1225 echo 'a' .. 0z33 1226 END 1227 CheckDefAndScriptFailure2(lines, 'E1105:', 'E976:', 1) 1228 1229 lines =<< trim END 1230 echo 'a' .. function('len') 1231 END 1232 CheckDefAndScriptFailure2(lines, 'E1105:', 'E729:', 1) 1233 1234 lines =<< trim END 1235 new 1236 ['']->setline(1) 1237 /pattern 1238 1239 eval 0 1240 bwipe! 1241 END 1242 CheckDefAndScriptFailure(lines, "E1004: White space required before and after '/' at \"/pattern", 3) 1243enddef 1244 1245def Test_expr5_vim9script_channel() 1246 if !has('channel') 1247 MissingFeature 'float' 1248 else 1249 var lines =<< trim END 1250 echo 'a' .. test_null_job() 1251 END 1252 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1) 1253 lines =<< trim END 1254 echo 'a' .. test_null_channel() 1255 END 1256 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1) 1257 endif 1258enddef 1259 1260def Test_expr5_float() 1261 if !has('float') 1262 MissingFeature 'float' 1263 else 1264 var lines =<< trim END 1265 assert_equal(66.0, 60.0 + 6.0) 1266 assert_equal(66.0, 60.0 + 6) 1267 assert_equal(66.0, 60 + 1268 6.0) 1269 assert_equal(5.1, g:afloat 1270 + 5) 1271 assert_equal(8.1, 8 + g:afloat) 1272 assert_equal(10.1, g:anint + g:afloat) 1273 assert_equal(10.1, g:afloat + g:anint) 1274 1275 assert_equal(54.0, 60.0 - 6.0) 1276 assert_equal(54.0, 60.0 1277 - 6) 1278 assert_equal(54.0, 60 - 6.0) 1279 assert_equal(-4.9, g:afloat - 5) 1280 assert_equal(7.9, 8 - g:afloat) 1281 assert_equal(9.9, g:anint - g:afloat) 1282 assert_equal(-9.9, g:afloat - g:anint) 1283 END 1284 CheckDefAndScriptSuccess(lines) 1285 endif 1286enddef 1287 1288func Test_expr5_fails() 1289 let msg = "White space required before and after '+'" 1290 call CheckDefAndScriptFailure(["var x = 1+2"], msg, 1) 1291 call CheckDefAndScriptFailure(["var x = 1 +2"], msg, 1) 1292 call CheckDefAndScriptFailure(["var x = 1+ 2"], msg, 1) 1293 1294 let msg = "White space required before and after '-'" 1295 call CheckDefAndScriptFailure(["var x = 1-2"], msg, 1) 1296 call CheckDefAndScriptFailure(["var x = 1 -2"], msg, 1) 1297 call CheckDefAndScriptFailure(["var x = 1- 2"], msg, 1) 1298 1299 let msg = "White space required before and after '..'" 1300 call CheckDefAndScriptFailure(["var x = '1'..'2'"], msg, 1) 1301 call CheckDefAndScriptFailure(["var x = '1' ..'2'"], msg, 1) 1302 call CheckDefAndScriptFailure(["var x = '1'.. '2'"], msg, 1) 1303 1304 call CheckDefAndScriptFailure2(["var x = 0z1122 + 33"], 'E1051:', 'E974:', 1) 1305 call CheckDefAndScriptFailure2(["var x = 0z1122 + [3]"], 'E1051:', 'E974:', 1) 1306 call CheckDefAndScriptFailure2(["var x = 0z1122 + 'asd'"], 'E1051:', 'E974:', 1) 1307 call CheckDefAndScriptFailure2(["var x = 33 + 0z1122"], 'E1051:', 'E974:', 1) 1308 call CheckDefAndScriptFailure2(["var x = [3] + 0z1122"], 'E1051:', 'E745:', 1) 1309 call CheckDefAndScriptFailure2(["var x = 'asdf' + 0z1122"], 'E1051:', 'E1030:', 1) 1310 call CheckDefAndScriptFailure2(["var x = 6 + xxx"], 'E1001:', 'E121:', 1) 1311 1312 call CheckDefAndScriptFailure2(["var x = 'a' .. [1]"], 'E1105:', 'E730:', 1) 1313 call CheckDefAndScriptFailure2(["var x = 'a' .. {a: 1}"], 'E1105:', 'E731:', 1) 1314 call CheckDefAndScriptFailure2(["var x = 'a' .. test_void()"], 'E1105:', 'E908:', 1) 1315 call CheckDefAndScriptFailure2(["var x = 'a' .. 0z32"], 'E1105:', 'E976:', 1) 1316 call CheckDefAndScriptFailure2(["var x = 'a' .. function('len')"], 'E1105:', 'E729:', 1) 1317 call CheckDefAndScriptFailure2(["var x = 'a' .. function('len', ['a'])"], 'E1105:', 'E729:', 1) 1318 1319 call CheckDefAndScriptFailure2(['var x = 1 + v:none'], 'E1051:', 'E611:', 1) 1320 call CheckDefAndScriptFailure2(['var x = 1 + v:null'], 'E1051:', 'E611:', 1) 1321 call CheckDefAndScriptFailure2(['var x = 1 + v:true'], 'E1051:', 'E1138:', 1) 1322 call CheckDefAndScriptFailure2(['var x = 1 + v:false'], 'E1051:', 'E1138:', 1) 1323 call CheckDefAndScriptFailure2(['var x = 1 + true'], 'E1051:', 'E1138:', 1) 1324 call CheckDefAndScriptFailure2(['var x = 1 + false'], 'E1051:', 'E1138:', 1) 1325endfunc 1326 1327func Test_expr5_fails_channel() 1328 CheckFeature channel 1329 call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_job()"], 'E1105:', 'E908:', 1) 1330 call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_channel()"], 'E1105:', 'E908:', 1) 1331endfunc 1332 1333def Test_expr5_list_add() 1334 var lines =<< trim END 1335 # concatenating two lists with same member types is OK 1336 var d = {} 1337 for i in ['a'] + ['b'] 1338 d = {[i]: 0} 1339 endfor 1340 1341 # concatenating two lists with different member types results in "any" 1342 var dany = {} 1343 for i in ['a'] + [12] 1344 dany[i] = i 1345 endfor 1346 assert_equal({a: 'a', 12: 12}, dany) 1347 1348 # result of glob() is "any", runtime type check 1349 var sl: list<string> = glob('*.txt', false, true) + [''] 1350 END 1351 CheckDefAndScriptSuccess(lines) 1352enddef 1353 1354" test multiply, divide, modulo 1355def Test_expr6() 1356 var lines =<< trim END 1357 assert_equal(36, 6 * 6) 1358 assert_equal(24, 6 * 1359 g:thefour) 1360 assert_equal(24, g:thefour 1361 * 6) 1362 assert_equal(40, g:anint * g:thefour) 1363 1364 assert_equal(10, 60 / 6) 1365 assert_equal(6, 60 / 1366 g:anint) 1367 assert_equal(1, g:anint / 6) 1368 assert_equal(2, g:anint 1369 / g:thefour) 1370 1371 assert_equal(5, 11 % 6) 1372 assert_equal(4, g:anint % 6) 1373 assert_equal(3, 13 % 1374 g:anint) 1375 assert_equal(2, g:anint 1376 % g:thefour) 1377 1378 assert_equal(4, 6 * 4 / 6) 1379 1380 var x = [2] 1381 var y = [3] 1382 assert_equal(5, x[0] + y[0]) 1383 assert_equal(6, x[0] * y[0]) 1384 if has('float') 1385 var xf = [2.0] 1386 var yf = [3.0] 1387 assert_equal(5.0, xf[0] 1388 + yf[0]) 1389 assert_equal(6.0, xf[0] 1390 * yf[0]) 1391 endif 1392 END 1393 CheckDefAndScriptSuccess(lines) 1394 1395 CheckDefAndScriptFailure2(["var x = 6 * xxx"], 'E1001:', 'E121:', 1) 1396 CheckDefFailure(["var d = 6 * "], 'E1097:', 3) 1397 CheckScriptFailure(['vim9script', "var d = 6 * "], 'E15:', 2) 1398 1399 CheckDefExecAndScriptFailure(['echo 1 / 0'], 'E1154', 1) 1400 CheckDefExecAndScriptFailure(['echo 1 % 0'], 'E1154', 1) 1401enddef 1402 1403def Test_expr6_vim9script() 1404 # check line continuation 1405 var lines =<< trim END 1406 var name = 11 1407 * 22 1408 / 3 1409 assert_equal(80, name) 1410 END 1411 CheckDefAndScriptSuccess(lines) 1412 1413 lines =<< trim END 1414 var name = 25 1415 % 10 1416 assert_equal(5, name) 1417 END 1418 CheckDefAndScriptSuccess(lines) 1419 1420 lines =<< trim END 1421 var name = 25 1422 # comment 1423 1424 # comment 1425 % 10 1426 assert_equal(5, name) 1427 END 1428 CheckDefAndScriptSuccess(lines) 1429 1430 lines =<< trim END 1431 var name = 11 * 1432 22 / 1433 3 1434 assert_equal(80, name) 1435 END 1436 CheckDefAndScriptSuccess(lines) 1437 1438 # check white space 1439 lines =<< trim END 1440 echo 5*6 1441 END 1442 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1443 1444 lines =<< trim END 1445 echo 5 *6 1446 END 1447 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1448 1449 lines =<< trim END 1450 echo 5* 6 1451 END 1452 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1453enddef 1454 1455def Test_expr6_float() 1456 if !has('float') 1457 MissingFeature 'float' 1458 else 1459 var lines =<< trim END 1460 assert_equal(36.0, 6.0 * 6) 1461 assert_equal(36.0, 6 * 1462 6.0) 1463 assert_equal(36.0, 6.0 * 6.0) 1464 assert_equal(1.0, g:afloat * g:anint) 1465 1466 assert_equal(10.0, 60 / 6.0) 1467 assert_equal(10.0, 60.0 / 1468 6) 1469 assert_equal(10.0, 60.0 / 6.0) 1470 assert_equal(0.01, g:afloat / g:anint) 1471 1472 assert_equal(4.0, 6.0 * 4 / 6) 1473 assert_equal(4.0, 6 * 1474 4.0 / 1475 6) 1476 assert_equal(4.0, 6 * 4 / 6.0) 1477 assert_equal(4.0, 6.0 * 4.0 / 6) 1478 assert_equal(4.0, 6 * 4.0 / 6.0) 1479 assert_equal(4.0, 6.0 * 4 / 6.0) 1480 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1481 1482 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1483 END 1484 CheckDefAndScriptSuccess(lines) 1485 endif 1486enddef 1487 1488func Test_expr6_fails() 1489 let msg = "White space required before and after '*'" 1490 call CheckDefAndScriptFailure(["var x = 1*2"], msg, 1) 1491 call CheckDefAndScriptFailure(["var x = 1 *2"], msg, 1) 1492 call CheckDefAndScriptFailure(["var x = 1* 2"], msg, 1) 1493 1494 let msg = "White space required before and after '/'" 1495 call CheckDefAndScriptFailure(["var x = 1/2"], msg, 1) 1496 call CheckDefAndScriptFailure(["var x = 1 /2"], msg, 1) 1497 call CheckDefAndScriptFailure(["var x = 1/ 2"], msg, 1) 1498 1499 let msg = "White space required before and after '%'" 1500 call CheckDefAndScriptFailure(["var x = 1%2"], msg, 1) 1501 call CheckDefAndScriptFailure(["var x = 1 %2"], msg, 1) 1502 call CheckDefAndScriptFailure(["var x = 1% 2"], msg, 1) 1503 1504 call CheckDefAndScriptFailure2(["var x = '1' * '2'"], 'E1036:', 'E1030:', 1) 1505 call CheckDefAndScriptFailure2(["var x = '1' / '2'"], 'E1036:', 'E1030:', 1) 1506 call CheckDefAndScriptFailure2(["var x = '1' % '2'"], 'E1035:', 'E1030:', 1) 1507 1508 call CheckDefAndScriptFailure2(["var x = 0z01 * 0z12"], 'E1036:', 'E974:', 1) 1509 call CheckDefAndScriptFailure2(["var x = 0z01 / 0z12"], 'E1036:', 'E974:', 1) 1510 call CheckDefAndScriptFailure2(["var x = 0z01 % 0z12"], 'E1035:', 'E974:', 1) 1511 1512 call CheckDefAndScriptFailure2(["var x = [1] * [2]"], 'E1036:', 'E745:', 1) 1513 call CheckDefAndScriptFailure2(["var x = [1] / [2]"], 'E1036:', 'E745:', 1) 1514 call CheckDefAndScriptFailure2(["var x = [1] % [2]"], 'E1035:', 'E745:', 1) 1515 1516 call CheckDefAndScriptFailure2(["var x = {one: 1} * {two: 2}"], 'E1036:', 'E728:', 1) 1517 call CheckDefAndScriptFailure2(["var x = {one: 1} / {two: 2}"], 'E1036:', 'E728:', 1) 1518 call CheckDefAndScriptFailure2(["var x = {one: 1} % {two: 2}"], 'E1035:', 'E728:', 1) 1519 1520 call CheckDefAndScriptFailure2(["var x = 0xff[1]"], 'E1107:', 'E1062:', 1) 1521 if has('float') 1522 call CheckDefAndScriptFailure2(["var x = 0.7[1]"], 'E1107:', 'E806:', 1) 1523 endif 1524endfunc 1525 1526func Test_expr6_float_fails() 1527 CheckFeature float 1528 call CheckDefAndScriptFailure2(["var x = 1.0 % 2"], 'E1035:', 'E804:', 1) 1529endfunc 1530 1531" define here to use old style parsing 1532if has('float') 1533 let g:float_zero = 0.0 1534 let g:float_neg = -9.8 1535 let g:float_big = 9.9e99 1536endif 1537let g:blob_empty = 0z 1538let g:blob_one = 0z01 1539let g:blob_long = 0z0102.0304 1540 1541let g:string_empty = '' 1542let g:string_short = 'x' 1543let g:string_long = 'abcdefghijklm' 1544let g:string_special = "ab\ncd\ref\ekk" 1545 1546let g:special_true = v:true 1547let g:special_false = v:false 1548let g:special_null = v:null 1549let g:special_none = v:none 1550 1551let g:list_empty = [] 1552let g:list_mixed = [1, 'b', v:false] 1553 1554let g:dict_empty = {} 1555let g:dict_one = #{one: 1} 1556 1557let $TESTVAR = 'testvar' 1558 1559" type casts 1560def Test_expr7t() 1561 var ls: list<string> = ['a', <string>g:string_empty] 1562 var ln: list<number> = [<number>g:anint, <number>g:thefour] 1563 var nr = <number>234 1564 assert_equal(234, nr) 1565 1566 CheckDefAndScriptFailure2(["var x = <nr>123"], 'E1010:', 'E15:', 1) 1567 CheckDefFailure(["var x = <number>"], 'E1097:', 3) 1568 CheckScriptFailure(['vim9script', "var x = <number>"], 'E15:', 2) 1569 CheckDefAndScriptFailure2(["var x = <number >123"], 'E1068:', 'E15:', 1) 1570 CheckDefAndScriptFailure2(["var x = <number 123"], 'E1104:', 'E15:', 1) 1571enddef 1572 1573" test low level expression 1574def Test_expr7_number() 1575 # number constant 1576 var lines =<< trim END 1577 assert_equal(0, 0) 1578 assert_equal(654, 0654) 1579 1580 assert_equal(6, 0x6) 1581 assert_equal(15, 0xf) 1582 assert_equal(255, 0xff) 1583 END 1584 CheckDefAndScriptSuccess(lines) 1585enddef 1586 1587def Test_expr7_float() 1588 # float constant 1589 if !has('float') 1590 MissingFeature 'float' 1591 else 1592 var lines =<< trim END 1593 assert_equal(g:float_zero, .0) 1594 assert_equal(g:float_zero, 0.0) 1595 assert_equal(g:float_neg, -9.8) 1596 assert_equal(g:float_big, 9.9e99) 1597 END 1598 CheckDefAndScriptSuccess(lines) 1599 endif 1600enddef 1601 1602def Test_expr7_blob() 1603 # blob constant 1604 var lines =<< trim END 1605 assert_equal(g:blob_empty, 0z) 1606 assert_equal(g:blob_one, 0z01) 1607 assert_equal(g:blob_long, 0z0102.0304) 1608 END 1609 CheckDefAndScriptSuccess(lines) 1610 1611 CheckDefAndScriptFailure(["var x = 0z123"], 'E973:', 1) 1612enddef 1613 1614def Test_expr7_string() 1615 # string constant 1616 var lines =<< trim END 1617 assert_equal(g:string_empty, '') 1618 assert_equal(g:string_empty, "") 1619 assert_equal(g:string_short, 'x') 1620 assert_equal(g:string_short, "x") 1621 assert_equal(g:string_long, 'abcdefghijklm') 1622 assert_equal(g:string_long, "abcdefghijklm") 1623 assert_equal(g:string_special, "ab\ncd\ref\ekk") 1624 END 1625 CheckDefAndScriptSuccess(lines) 1626 1627 CheckDefAndScriptFailure(['var x = "abc'], 'E114:', 1) 1628 CheckDefAndScriptFailure(["var x = 'abc"], 'E115:', 1) 1629enddef 1630 1631def Test_expr7_vimvar() 1632 var old: list<string> = v:oldfiles 1633 var compl: dict<any> = v:completed_item 1634 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) 1637 new 1638 exec "normal! afoo fo\<C-N>\<Esc>" 1639 CheckDefExecAndScriptFailure(["var old: dict<number> = v:completed_item"], 'E1012: Type mismatch; expected dict<number> but got dict<string>', 1) 1640 bwipe! 1641enddef 1642 1643def Test_expr7_special() 1644 # special constant 1645 var lines =<< trim END 1646 assert_equal(g:special_true, true) 1647 assert_equal(g:special_false, false) 1648 assert_equal(g:special_true, v:true) 1649 assert_equal(g:special_false, v:false) 1650 assert_equal(v:true, true) 1651 assert_equal(v:false, false) 1652 1653 assert_equal(true, !false) 1654 assert_equal(false, !true) 1655 assert_equal(true, !0) 1656 assert_equal(false, !1) 1657 assert_equal(false, !!false) 1658 assert_equal(true, !!true) 1659 assert_equal(false, !!0) 1660 assert_equal(true, !!1) 1661 1662 var t = true 1663 var f = false 1664 assert_equal(true, t) 1665 assert_equal(false, f) 1666 1667 assert_equal(g:special_null, v:null) 1668 assert_equal(g:special_null, null) 1669 assert_equal(g:special_none, v:none) 1670 END 1671 CheckDefAndScriptSuccess(lines) 1672 1673 CheckDefAndScriptFailure(['v:true = true'], 'E46:', 1) 1674 CheckDefAndScriptFailure(['v:true = false'], 'E46:', 1) 1675 CheckDefAndScriptFailure(['v:false = true'], 'E46:', 1) 1676 CheckDefAndScriptFailure(['v:null = 11'], 'E46:', 1) 1677 CheckDefAndScriptFailure(['v:none = 22'], 'E46:', 1) 1678enddef 1679 1680def Test_expr7_list() 1681 # list 1682 var lines =<< trim END 1683 assert_equal(g:list_empty, []) 1684 assert_equal(g:list_empty, [ ]) 1685 1686 var numbers: list<number> = [1, 2, 3] 1687 numbers = [1] 1688 numbers = [] 1689 1690 var strings: list<string> = ['a', 'b', 'c'] 1691 strings = ['x'] 1692 strings = [] 1693 1694 var mixed: list<any> = [1, 'b', false,] 1695 assert_equal(g:list_mixed, mixed) 1696 assert_equal('b', mixed[1]) 1697 1698 echo [1, 1699 2] [3, 1700 4] 1701 1702 var llstring: list<list<string>> = [['text'], []] 1703 llstring = [[], ['text']] 1704 llstring = [[], []] 1705 END 1706 CheckDefAndScriptSuccess(lines) 1707 1708 var rangelist: list<number> = range(3) 1709 g:rangelist = range(3) 1710 CheckDefExecAndScriptFailure(["var x: list<string> = g:rangelist"], 'E1012: Type mismatch; expected list<string> but got list<number>', 1) 1711 1712 CheckDefAndScriptFailure2(["var x = 1234[3]"], 'E1107:', 'E1062:', 1) 1713 CheckDefExecAndScriptFailure(["var x = g:anint[3]"], 'E1062:', 1) 1714 1715 CheckDefAndScriptFailure2(["var x = g:list_mixed[xxx]"], 'E1001:', 'E121:', 1) 1716 1717 CheckDefAndScriptFailure(["var x = [1,2,3]"], 'E1069:', 1) 1718 CheckDefAndScriptFailure(["var x = [1 ,2, 3]"], 'E1068:', 1) 1719 1720 CheckDefExecAndScriptFailure(["echo 1", "var x = [][0]", "echo 3"], 'E684:', 2) 1721 1722 CheckDefExecAndScriptFailure2(["var x = g:list_mixed['xx']"], 'E1012:', 'E1030:', 1) 1723 CheckDefFailure(["var x = g:list_mixed["], 'E1097:', 3) 1724 CheckScriptFailure(['vim9script', "var x = g:list_mixed["], 'E15:', 2) 1725 CheckDefFailure(["var x = g:list_mixed[0"], 'E1097:', 3) 1726 CheckScriptFailure(['vim9script', "var x = g:list_mixed[0"], 'E111:', 2) 1727 CheckDefExecAndScriptFailure(["var x = g:list_empty[3]"], 'E684:', 1) 1728 CheckDefExecAndScriptFailure(["var l: list<number> = [234, 'x']"], 'E1012:', 1) 1729 CheckDefExecAndScriptFailure(["var l: list<number> = ['x', 234]"], 'E1012:', 1) 1730 CheckDefExecAndScriptFailure(["var l: list<string> = [234, 'x']"], 'E1012:', 1) 1731 CheckDefExecAndScriptFailure(["var l: list<string> = ['x', 123]"], 'E1012:', 1) 1732 1733 lines =<< trim END 1734 var datalist: list<string> 1735 def Main() 1736 datalist += ['x'. 1737 enddef 1738 Main() 1739 END 1740 CheckDefAndScriptFailure(lines, 'E1127:') 1741 1742 lines =<< trim END 1743 var numbers = [1, 2, 3, 4] 1744 var a = 1 1745 var b = 2 1746 END 1747 CheckDefAndScriptFailure(lines + ['echo numbers[1:b]'], 1748 'E1004: White space required before and after '':'' at ":b]"', 4) 1749 CheckDefAndScriptFailure(lines + ['echo numbers[1: b]'], 'E1004:', 4) 1750 CheckDefAndScriptFailure(lines + ['echo numbers[a :b]'], 'E1004:', 4) 1751enddef 1752 1753def Test_expr7_list_vim9script() 1754 var lines =<< trim END 1755 var l = [ 1756 11, 1757 22, 1758 ] 1759 assert_equal([11, 22], l) 1760 1761 echo [1, 1762 2] [3, 1763 4] 1764 1765 echo [1, # comment 1766 # comment 1767 2] [3, 1768 # comment 1769 4] 1770 END 1771 CheckDefAndScriptSuccess(lines) 1772 1773 lines =<< trim END 1774 var l = [11, 1775 22] 1776 assert_equal([11, 22], l) 1777 END 1778 CheckDefAndScriptSuccess(lines) 1779 1780 lines =<< trim END 1781 var l = [11,22] 1782 END 1783 CheckDefAndScriptFailure(lines, 'E1069:', 1) 1784 1785 lines =<< trim END 1786 var l = [11 , 22] 1787 END 1788 CheckDefAndScriptFailure(lines, 'E1068:', 1) 1789 1790 lines =<< trim END 1791 var l: list<number> = [234, 'x'] 1792 END 1793 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1794 1795 lines =<< trim END 1796 var l: list<number> = ['x', 234] 1797 END 1798 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1799 1800 lines =<< trim END 1801 var l: list<string> = ['x', 234] 1802 END 1803 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1804 1805 lines =<< trim END 1806 var l: list<string> = [234, 'x'] 1807 END 1808 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1809 1810 lines =<< trim END 1811 def Failing() 1812 job_stop() 1813 enddef 1814 var list = [Failing] 1815 END 1816 if has('channel') 1817 CheckDefAndScriptFailure(lines, 'E119:', 0) 1818 else 1819 CheckDefAndScriptFailure(lines, 'E117:', 0) 1820 endif 1821enddef 1822 1823def LambdaWithComments(): func 1824 return (x) => 1825 # some comment 1826 x == 1 1827 # some comment 1828 || 1829 x == 2 1830enddef 1831 1832def LambdaUsingArg(x: number): func 1833 return () => 1834 # some comment 1835 x == 1 1836 # some comment 1837 || 1838 x == 2 1839enddef 1840 1841def Test_expr7_lambda() 1842 var lines =<< trim END 1843 var La = () => 'result' 1844 assert_equal('result', La()) 1845 assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val)) 1846 1847 # line continuation inside lambda with "cond ? expr : expr" works 1848 var ll = range(3) 1849 var dll = mapnew(ll, (k, v) => v % 2 ? { 1850 ['111']: 111 } : {} 1851 ) 1852 assert_equal([{}, {111: 111}, {}], dll) 1853 1854 ll = range(3) 1855 map(ll, (k, v) => v == 8 || v 1856 == 9 1857 || v % 2 ? 111 : 222 1858 ) 1859 assert_equal([222, 111, 222], ll) 1860 1861 ll = range(3) 1862 map(ll, (k, v) => v != 8 && v 1863 != 9 1864 && v % 2 == 0 ? 111 : 222 1865 ) 1866 assert_equal([111, 222, 111], ll) 1867 1868 var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] ) 1869 assert_equal([{key: 22}], dl) 1870 1871 dl = [{key: 12}, {['foo']: 34}] 1872 assert_equal([{key: 12}], filter(dl, 1873 (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0)) 1874 1875 assert_equal(false, LambdaWithComments()(0)) 1876 assert_equal(true, LambdaWithComments()(1)) 1877 assert_equal(true, LambdaWithComments()(2)) 1878 assert_equal(false, LambdaWithComments()(3)) 1879 1880 assert_equal(false, LambdaUsingArg(0)()) 1881 assert_equal(true, LambdaUsingArg(1)()) 1882 1883 var res = map([1, 2, 3], (i: number, v: number) => i + v) 1884 assert_equal([1, 3, 5], res) 1885 END 1886 CheckDefAndScriptSuccess(lines) 1887 1888 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:') 1889 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004: White space required before and after ''=>'' at "=> a + 1"') 1890 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 'E1004:') 1891 1892 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 1893 # error is in first line of the lambda 1894 CheckDefAndScriptFailure(["var L = (a) => a + b"], 'E1001:', 0) 1895 1896 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 1897 1898 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 'E118:') 1899 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 'E118:') 1900 CheckDefAndScriptFailure2(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 'E121:', 1) 1901 1902 CheckDefAndScriptSuccess(['var Fx = (a) => ({k1: 0,', ' k2: 1})']) 1903 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 1904 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 1905 1906 CheckDefAndScriptSuccess(['var Fx = (a) => [0,', ' 1]']) 1907 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) 1908 1909 # no error for existing script variable when checking for lambda 1910 lines =<< trim END 1911 var name = 0 1912 eval (name + 2) / 3 1913 END 1914 CheckDefAndScriptSuccess(lines) 1915enddef 1916 1917def Test_expr7_lambda_block() 1918 var lines =<< trim END 1919 var Func = (s: string): string => { 1920 return 'hello ' .. s 1921 } 1922 assert_equal('hello there', Func('there')) 1923 1924 var ll = range(3) 1925 var dll = mapnew(ll, (k, v): string => { 1926 if v % 2 1927 return 'yes' 1928 endif 1929 return 'no' 1930 }) 1931 assert_equal(['no', 'yes', 'no'], dll) 1932 1933 sandbox var Safe = (nr: number): number => { 1934 return nr + 7 1935 } 1936 assert_equal(10, Safe(3)) 1937 END 1938 CheckDefAndScriptSuccess(lines) 1939 1940 lines =<< trim END 1941 map([1, 2], (k, v) => { redrawt }) 1942 END 1943 CheckDefAndScriptFailure(lines, 'E488') 1944 1945 lines =<< trim END 1946 var Func = (nr: int) => { 1947 echo nr 1948 } 1949 END 1950 CheckDefAndScriptFailure(lines, 'E1010', 1) 1951 1952 lines =<< trim END 1953 var Func = (nr: number): int => { 1954 return nr 1955 } 1956 END 1957 CheckDefAndScriptFailure(lines, 'E1010', 1) 1958 1959 lines =<< trim END 1960 var Func = (nr: number): int => { 1961 return nr 1962 END 1963 CheckDefAndScriptFailure(lines, 'E1171', 1) # line nr is function start 1964 1965 lines =<< trim END 1966 var Func = (nr: number): int => { 1967 var ll =<< ENDIT 1968 nothing 1969 END 1970 CheckDefFailure(lines, 'E1145: Missing heredoc end marker: ENDIT', 0) 1971 CheckScriptFailure(['vim9script'] + lines, 'E1145: Missing heredoc end marker: ENDIT', 2) 1972enddef 1973 1974def NewLambdaWithComments(): func 1975 return (x) => 1976 # some comment 1977 x == 1 1978 # some comment 1979 || 1980 x == 2 1981enddef 1982 1983def NewLambdaUsingArg(x: number): func 1984 return () => 1985 # some comment 1986 x == 1 1987 # some comment 1988 || 1989 x == 2 1990enddef 1991 1992def Test_expr7_new_lambda() 1993 var lines =<< trim END 1994 var La = () => 'result' 1995 assert_equal('result', La()) 1996 assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val)) 1997 1998 # line continuation inside lambda with "cond ? expr : expr" works 1999 var ll = range(3) 2000 var dll = mapnew(ll, (k, v) => v % 2 ? { 2001 ['111']: 111 } : {} 2002 ) 2003 assert_equal([{}, {111: 111}, {}], dll) 2004 2005 ll = range(3) 2006 map(ll, (k, v) => v == 8 || v 2007 == 9 2008 || v % 2 ? 111 : 222 2009 ) 2010 assert_equal([222, 111, 222], ll) 2011 2012 ll = range(3) 2013 map(ll, (k, v) => v != 8 && v 2014 != 9 2015 && v % 2 == 0 ? 111 : 222 2016 ) 2017 assert_equal([111, 222, 111], ll) 2018 2019 var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] ) 2020 assert_equal([{key: 22}], dl) 2021 2022 dl = [{key: 12}, {['foo']: 34}] 2023 assert_equal([{key: 12}], filter(dl, 2024 (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0)) 2025 2026 assert_equal(false, NewLambdaWithComments()(0)) 2027 assert_equal(true, NewLambdaWithComments()(1)) 2028 assert_equal(true, NewLambdaWithComments()(2)) 2029 assert_equal(false, NewLambdaWithComments()(3)) 2030 2031 assert_equal(false, NewLambdaUsingArg(0)()) 2032 assert_equal(true, NewLambdaUsingArg(1)()) 2033 2034 var res = map([1, 2, 3], (i: number, v: number) => i + v) 2035 assert_equal([1, 3, 5], res) 2036 2037 # Lambda returning a dict 2038 var Lmb = () => ({key: 42}) 2039 assert_equal({key: 42}, Lmb()) 2040 2041 var RefOne: func(number): string = (a: number): string => 'x' 2042 var RefTwo: func(number): any = (a: number): any => 'x' 2043 2044 var Fx = (a) => ({k1: 0, 2045 k2: 1}) 2046 var Fy = (a) => [0, 2047 1] 2048 END 2049 CheckDefAndScriptSuccess(lines) 2050 2051 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:') 2052 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004:') 2053 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 2054 'E1004: White space required before and after ''=>'' at " =>a + 1"') 2055 2056 CheckDefAndScriptFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:') 2057 CheckDefAndScriptFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:') 2058 2059 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 2060 # error is in first line of the lambda 2061 CheckDefAndScriptFailure2(["var L = (a) -> a + b"], 'E1001:', 'E121:', 1) 2062 2063 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 2064 2065 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 2066 'E118: Too many arguments for function:') 2067 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 2068 'E118: Too many arguments for function:') 2069 CheckDefFailure(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 1) 2070 2071 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 2072 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 2073 2074 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) 2075enddef 2076 2077def Test_expr7_lambda_vim9script() 2078 var lines =<< trim END 2079 var v = 10->((a) => 2080 a 2081 + 2 2082 )() 2083 assert_equal(12, v) 2084 END 2085 CheckDefAndScriptSuccess(lines) 2086 2087 # nested lambda with line breaks 2088 lines =<< trim END 2089 search('"', 'cW', 0, 0, () => 2090 synstack('.', col('.')) 2091 ->map((_, v) => synIDattr(v, 'name'))->len()) 2092 END 2093 CheckDefAndScriptSuccess(lines) 2094enddef 2095 2096def Test_expr7_funcref() 2097 var lines =<< trim END 2098 def RetNumber(): number 2099 return 123 2100 enddef 2101 var FuncRef = RetNumber 2102 assert_equal(123, FuncRef()) 2103 END 2104 CheckDefAndScriptSuccess(lines) 2105 2106 lines =<< trim END 2107 vim9script 2108 func g:GlobalFunc() 2109 return 'global' 2110 endfunc 2111 func s:ScriptFunc() 2112 return 'script' 2113 endfunc 2114 def Test() 2115 var Ref = g:GlobalFunc 2116 assert_equal('global', Ref()) 2117 Ref = GlobalFunc 2118 assert_equal('global', Ref()) 2119 2120 Ref = s:ScriptFunc 2121 assert_equal('script', Ref()) 2122 Ref = ScriptFunc 2123 assert_equal('script', Ref()) 2124 enddef 2125 Test() 2126 END 2127 CheckScriptSuccess(lines) 2128enddef 2129 2130let g:test_space_dict = {'': 'empty', ' ': 'space'} 2131let g:test_hash_dict = #{one: 1, two: 2} 2132 2133def Test_expr7_dict() 2134 # dictionary 2135 var lines =<< trim END 2136 assert_equal(g:dict_empty, {}) 2137 assert_equal(g:dict_empty, { }) 2138 assert_equal(g:dict_one, {['one']: 1}) 2139 var key = 'one' 2140 var val = 1 2141 assert_equal(g:dict_one, {[key]: val}) 2142 2143 var numbers: dict<number> = {a: 1, b: 2, c: 3} 2144 numbers = {a: 1} 2145 numbers = {} 2146 2147 var strings: dict<string> = {a: 'a', b: 'b', c: 'c'} 2148 strings = {a: 'x'} 2149 strings = {} 2150 2151 var dash = {xx-x: 8} 2152 assert_equal({['xx-x']: 8}, dash) 2153 2154 var dnr = {8: 8} 2155 assert_equal({['8']: 8}, dnr) 2156 2157 var mixed: dict<any> = {a: 'a', b: 42} 2158 mixed = {a: 'x'} 2159 mixed = {a: 234} 2160 mixed = {} 2161 2162 var dictlist: dict<list<string>> = {absent: [], present: ['hi']} 2163 dictlist = {absent: ['hi'], present: []} 2164 dictlist = {absent: [], present: []} 2165 2166 var dictdict: dict<dict<string>> = {one: {a: 'text'}, two: {}} 2167 dictdict = {one: {}, two: {a: 'text'}} 2168 dictdict = {one: {}, two: {}} 2169 2170 assert_equal({['']: 0}, {[matchstr('string', 'wont match')]: 0}) 2171 2172 assert_equal(g:test_space_dict, {['']: 'empty', [' ']: 'space'}) 2173 assert_equal(g:test_hash_dict, {one: 1, two: 2}) 2174 2175 assert_equal({['a a']: 1, ['b/c']: 2}, {'a a': 1, "b/c": 2}) 2176 2177 var d = {a: () => 3, b: () => 7} 2178 assert_equal(3, d.a()) 2179 assert_equal(7, d.b()) 2180 2181 var cd = { # comment 2182 key: 'val' # comment 2183 } 2184 2185 # different types used for the key 2186 var dkeys = {['key']: 'string', 2187 [12]: 'numberexpr', 2188 34: 'number', 2189 [true]: 'bool'} 2190 assert_equal('string', dkeys['key']) 2191 assert_equal('numberexpr', dkeys[12]) 2192 assert_equal('number', dkeys[34]) 2193 assert_equal('bool', dkeys[true]) 2194 if has('float') 2195 dkeys = {[1.2]: 'floatexpr', [3.4]: 'float'} 2196 assert_equal('floatexpr', dkeys[1.2]) 2197 assert_equal('float', dkeys[3.4]) 2198 endif 2199 2200 # automatic conversion from number to string 2201 var n = 123 2202 var dictnr = {[n]: 1} 2203 2204 # comment to start fold is OK 2205 var x1: number #{{ fold 2206 var x2 = 9 #{{ fold 2207 END 2208 CheckDefAndScriptSuccess(lines) 2209 2210 # legacy syntax doesn't work 2211 CheckDefAndScriptFailure(["var x = #{key: 8}"], 'E1170:', 1) 2212 CheckDefAndScriptFailure(["var x = 'a' #{a: 1}"], 'E1170:', 1) 2213 CheckDefAndScriptFailure(["var x = 'a' .. #{a: 1}"], 'E1170:', 1) 2214 CheckDefAndScriptFailure(["var x = true ? #{a: 1}"], 'E1170:', 1) 2215 2216 CheckDefAndScriptFailure(["var x = {a:8}"], 'E1069:', 1) 2217 CheckDefAndScriptFailure(["var x = {a : 8}"], 'E1068:', 1) 2218 CheckDefAndScriptFailure(["var x = {a :8}"], 'E1068:', 1) 2219 CheckDefAndScriptFailure(["var x = {a: 8 , b: 9}"], 'E1068:', 1) 2220 CheckDefAndScriptFailure(["var x = {a: 1,b: 2}"], 'E1069:', 1) 2221 2222 CheckDefAndScriptFailure(["var x = {xxx}"], 'E720:', 1) 2223 CheckDefAndScriptFailure(["var x = {xxx: 1", "var y = 2"], 'E722:', 2) 2224 CheckDefFailure(["var x = {xxx: 1,"], 'E723:', 2) 2225 CheckScriptFailure(['vim9script', "var x = {xxx: 1,"], 'E723:', 2) 2226 CheckDefAndScriptFailure2(["var x = {['a']: xxx}"], 'E1001:', 'E121:', 1) 2227 CheckDefAndScriptFailure(["var x = {a: 1, a: 2}"], 'E721:', 1) 2228 CheckDefExecAndScriptFailure2(["var x = g:anint.member"], 'E715:', 'E15:', 1) 2229 CheckDefExecAndScriptFailure(["var x = g:dict_empty.member"], 'E716:', 1) 2230 2231 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: 234, b: "1"}'], 'E1012:', 1) 2232 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: "x", b: 134}'], 'E1012:', 1) 2233 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1) 2234 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1) 2235 2236 # invalid types for the key 2237 CheckDefAndScriptFailure2(["var x = {[[1, 2]]: 0}"], 'E1105:', 'E730:', 1) 2238 2239 CheckDefFailure(['var x = ({'], 'E723:', 2) 2240 CheckScriptFailure(['vim9script', 'var x = ({'], 'E723:', 2) 2241 CheckDefExecAndScriptFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1) 2242enddef 2243 2244def Test_expr7_dict_vim9script() 2245 var lines =<< trim END 2246 var d = { 2247 ['one']: 2248 1, 2249 ['two']: 2, 2250 } 2251 assert_equal({one: 1, two: 2}, d) 2252 2253 d = { # comment 2254 ['one']: 2255 # comment 2256 2257 1, 2258 # comment 2259 # comment 2260 ['two']: 2, 2261 } 2262 assert_equal({one: 1, two: 2}, d) 2263 2264 var dd = {k: 123->len()} 2265 assert_equal(3, dd.k) 2266 END 2267 CheckDefAndScriptSuccess(lines) 2268 2269 lines =<< trim END 2270 var d = { ["one"]: "one", ["two"]: "two", } 2271 assert_equal({one: 'one', two: 'two'}, d) 2272 END 2273 CheckDefAndScriptSuccess(lines) 2274 2275 lines =<< trim END 2276 var d = {one: 1, 2277 two: 2, 2278 } 2279 assert_equal({one: 1, two: 2}, d) 2280 END 2281 CheckDefAndScriptSuccess(lines) 2282 2283 lines =<< trim END 2284 var d = {one:1, two: 2} 2285 END 2286 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2287 2288 lines =<< trim END 2289 var d = {one: 1,two: 2} 2290 END 2291 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2292 2293 lines =<< trim END 2294 var d = {one : 1} 2295 END 2296 CheckDefAndScriptFailure(lines, 'E1068:', 1) 2297 2298 lines =<< trim END 2299 var d = {one:1} 2300 END 2301 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2302 2303 lines =<< trim END 2304 var d = {one: 1 , two: 2} 2305 END 2306 CheckDefAndScriptFailure(lines, 'E1068:', 1) 2307 2308 lines =<< trim END 2309 var l: dict<number> = {a: 234, b: 'x'} 2310 END 2311 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2312 2313 lines =<< trim END 2314 var l: dict<number> = {a: 'x', b: 234} 2315 END 2316 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2317 2318 lines =<< trim END 2319 var l: dict<string> = {a: 'x', b: 234} 2320 END 2321 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2322 2323 lines =<< trim END 2324 var l: dict<string> = {a: 234, b: 'x'} 2325 END 2326 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2327 2328 lines =<< trim END 2329 var d = {['a']: 234, ['b': 'x'} 2330 END 2331 CheckDefAndScriptFailure(lines, 'E1139:', 1) 2332 2333 lines =<< trim END 2334 def Func() 2335 var d = {['a']: 234, ['b': 'x'} 2336 enddef 2337 defcompile 2338 END 2339 CheckDefAndScriptFailure(lines, 'E1139:', 0) 2340 2341 lines =<< trim END 2342 var d = {'a': 2343 END 2344 CheckDefFailure(lines, 'E723:', 2) 2345 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2) 2346 2347 lines =<< trim END 2348 def Func() 2349 var d = {'a': 2350 enddef 2351 defcompile 2352 END 2353 CheckDefAndScriptFailure(lines, 'E723:', 0) 2354 2355 lines =<< trim END 2356 def Failing() 2357 job_stop() 2358 enddef 2359 var dict = {name: Failing} 2360 END 2361 if has('channel') 2362 CheckDefAndScriptFailure(lines, 'E119:', 0) 2363 else 2364 CheckDefAndScriptFailure(lines, 'E117:', 0) 2365 endif 2366enddef 2367 2368let g:oneString = 'one' 2369 2370def Test_expr_member() 2371 var lines =<< trim END 2372 assert_equal(1, g:dict_one.one) 2373 var d: dict<number> = g:dict_one 2374 assert_equal(1, d['one']) 2375 assert_equal(1, d[ 2376 'one' 2377 ]) 2378 assert_equal(1, d 2379 .one) 2380 d = {1: 1, _: 2} 2381 assert_equal(1, d 2382 .1) 2383 assert_equal(2, d 2384 ._) 2385 2386 # getting the one member should clear the dict after getting the item 2387 assert_equal('one', {one: 'one'}.one) 2388 assert_equal('one', {one: 'one'}[g:oneString]) 2389 END 2390 CheckDefAndScriptSuccess(lines) 2391 2392 CheckDefAndScriptFailure2(["var x = g:dict_one.#$!"], 'E1002:', 'E15:', 1) 2393 CheckDefExecAndScriptFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2) 2394 CheckDefExecAndScriptFailure(["var d: dict<number>", "d = g:list_empty"], 'E1012: Type mismatch; expected dict<number> but got list<unknown>', 2) 2395enddef 2396 2397def Test_expr7_any_index_slice() 2398 var lines =<< trim END 2399 # getting the one member should clear the list only after getting the item 2400 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 2401 2402 # string is permissive, index out of range accepted 2403 g:teststring = 'abcdef' 2404 assert_equal('b', g:teststring[1]) 2405 assert_equal('f', g:teststring[-1]) 2406 assert_equal('', g:teststring[99]) 2407 2408 assert_equal('b', g:teststring[1 : 1]) 2409 assert_equal('bcdef', g:teststring[1 :]) 2410 assert_equal('abcd', g:teststring[: 3]) 2411 assert_equal('cdef', g:teststring[-4 :]) 2412 assert_equal('abcdef', g:teststring[-9 :]) 2413 assert_equal('abcd', g:teststring[: -3]) 2414 assert_equal('', g:teststring[: -9]) 2415 2416 # composing characters are included 2417 g:teststring = 'àéû' 2418 assert_equal('à', g:teststring[0]) 2419 assert_equal('é', g:teststring[1]) 2420 assert_equal('û', g:teststring[2]) 2421 assert_equal('', g:teststring[3]) 2422 assert_equal('', g:teststring[4]) 2423 2424 assert_equal('û', g:teststring[-1]) 2425 assert_equal('é', g:teststring[-2]) 2426 assert_equal('à', g:teststring[-3]) 2427 assert_equal('', g:teststring[-4]) 2428 assert_equal('', g:teststring[-5]) 2429 2430 assert_equal('à', g:teststring[0 : 0]) 2431 assert_equal('é', g:teststring[1 : 1]) 2432 assert_equal('àé', g:teststring[0 : 1]) 2433 assert_equal('àéû', g:teststring[0 : -1]) 2434 assert_equal('àé', g:teststring[0 : -2]) 2435 assert_equal('à', g:teststring[0 : -3]) 2436 assert_equal('', g:teststring[0 : -4]) 2437 assert_equal('', g:teststring[0 : -5]) 2438 assert_equal('àéû', g:teststring[ : ]) 2439 assert_equal('àéû', g:teststring[0 : ]) 2440 assert_equal('éû', g:teststring[1 : ]) 2441 assert_equal('û', g:teststring[2 : ]) 2442 assert_equal('', g:teststring[3 : ]) 2443 assert_equal('', g:teststring[4 : ]) 2444 2445 # blob index cannot be out of range 2446 g:testblob = 0z01ab 2447 assert_equal(0x01, g:testblob[0]) 2448 assert_equal(0xab, g:testblob[1]) 2449 assert_equal(0xab, g:testblob[-1]) 2450 assert_equal(0x01, g:testblob[-2]) 2451 2452 # blob slice accepts out of range 2453 assert_equal(0z01ab, g:testblob[0 : 1]) 2454 assert_equal(0z01, g:testblob[0 : 0]) 2455 assert_equal(0z01, g:testblob[-2 : -2]) 2456 assert_equal(0zab, g:testblob[1 : 1]) 2457 assert_equal(0zab, g:testblob[-1 : -1]) 2458 assert_equal(0z, g:testblob[2 : 2]) 2459 assert_equal(0z, g:testblob[0 : -3]) 2460 2461 # list index cannot be out of range 2462 g:testlist = [0, 1, 2, 3] 2463 assert_equal(0, g:testlist[0]) 2464 assert_equal(1, g:testlist[1]) 2465 assert_equal(3, g:testlist[3]) 2466 assert_equal(3, g:testlist[-1]) 2467 assert_equal(0, g:testlist[-4]) 2468 assert_equal(1, g:testlist[g:theone]) 2469 2470 # list slice accepts out of range 2471 assert_equal([0], g:testlist[0 : 0]) 2472 assert_equal([3], g:testlist[3 : 3]) 2473 assert_equal([0, 1], g:testlist[0 : 1]) 2474 assert_equal([0, 1, 2, 3], g:testlist[0 : 3]) 2475 assert_equal([0, 1, 2, 3], g:testlist[0 : 9]) 2476 assert_equal([], g:testlist[-1 : 1]) 2477 assert_equal([1], g:testlist[-3 : 1]) 2478 assert_equal([0, 1], g:testlist[-4 : 1]) 2479 assert_equal([0, 1], g:testlist[-9 : 1]) 2480 assert_equal([1, 2, 3], g:testlist[1 : -1]) 2481 assert_equal([1], g:testlist[1 : -3]) 2482 assert_equal([], g:testlist[1 : -4]) 2483 assert_equal([], g:testlist[1 : -9]) 2484 2485 g:testdict = {a: 1, b: 2} 2486 assert_equal(1, g:testdict['a']) 2487 assert_equal(2, g:testdict['b']) 2488 END 2489 2490 CheckDefAndScriptSuccess(lines) 2491 2492 CheckDefExecAndScriptFailure(['echo g:testblob[2]'], 'E979:', 1) 2493 CheckDefExecAndScriptFailure(['echo g:testblob[-3]'], 'E979:', 1) 2494 2495 CheckDefExecAndScriptFailure(['echo g:testlist[4]'], 'E684: list index out of range: 4', 1) 2496 CheckDefExecAndScriptFailure(['echo g:testlist[-5]'], 'E684:', 1) 2497 2498 CheckDefExecAndScriptFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1) 2499 CheckDefExecAndScriptFailure(['echo g:testdict[1]'], 'E716:', 1) 2500 2501 unlet g:teststring 2502 unlet g:testblob 2503 unlet g:testlist 2504enddef 2505 2506def Test_expr_member_vim9script() 2507 var lines =<< trim END 2508 var d = {one: 2509 'one', 2510 two: 'two', 2511 1: 1, 2512 _: 2} 2513 assert_equal('one', d.one) 2514 assert_equal('one', d 2515 .one) 2516 assert_equal(1, d 2517 .1) 2518 assert_equal(2, d 2519 ._) 2520 assert_equal('one', d[ 2521 'one' 2522 ]) 2523 END 2524 CheckDefAndScriptSuccess(lines) 2525 2526 lines =<< trim END 2527 var l = [1, 2528 2, 2529 3, 4 2530 ] 2531 assert_equal(2, l[ 2532 1 2533 ]) 2534 assert_equal([2, 3], l[1 : 2]) 2535 assert_equal([1, 2, 3], l[ 2536 : 2537 2 2538 ]) 2539 assert_equal([3, 4], l[ 2540 2 2541 : 2542 ]) 2543 END 2544 CheckDefAndScriptSuccess(lines) 2545enddef 2546 2547def SetSomeVar() 2548 b:someVar = &fdm 2549enddef 2550 2551def Test_expr7_option() 2552 var lines =<< trim END 2553 # option 2554 set ts=11 2555 assert_equal(11, &ts) 2556 &ts = 9 2557 assert_equal(9, &ts) 2558 set ts=8 2559 set grepprg=some\ text 2560 assert_equal('some text', &grepprg) 2561 &grepprg = test_null_string() 2562 assert_equal('', &grepprg) 2563 set grepprg& 2564 2565 # check matching type 2566 var bval: bool = &tgc 2567 var nval: number = &ts 2568 var sval: string = &path 2569 2570 # check v_lock is cleared (requires using valgrind, doesn't always show) 2571 SetSomeVar() 2572 b:someVar = 0 2573 unlet b:someVar 2574 END 2575 CheckDefAndScriptSuccess(lines) 2576enddef 2577 2578def Test_expr7_environment() 2579 var lines =<< trim END 2580 # environment variable 2581 assert_equal('testvar', $TESTVAR) 2582 assert_equal('', $ASDF_ASD_XXX) 2583 END 2584 CheckDefAndScriptSuccess(lines) 2585 2586 CheckDefAndScriptFailure2(["var x = $$$"], 'E1002:', 'E15:', 1) 2587enddef 2588 2589def Test_expr7_register() 2590 var lines =<< trim END 2591 @a = 'register a' 2592 assert_equal('register a', @a) 2593 2594 var fname = expand('%') 2595 assert_equal(fname, @%) 2596 2597 feedkeys(":echo 'some'\<CR>", "xt") 2598 assert_equal("echo 'some'", @:) 2599 2600 normal axyz 2601 assert_equal("xyz", @.) 2602 2603 @/ = 'slash' 2604 assert_equal('slash', @/) 2605 2606 @= = 'equal' 2607 assert_equal('equal', @=) 2608 END 2609 CheckDefAndScriptSuccess(lines) 2610 2611 CheckDefAndScriptFailure2(["@. = 'yes'"], 'E354:', 'E488:', 1) 2612enddef 2613 2614" This is slow when run under valgrind. 2615def Test_expr7_namespace() 2616 var lines =<< trim END 2617 g:some_var = 'some' 2618 assert_equal('some', get(g:, 'some_var')) 2619 assert_equal('some', get(g:, 'some_var', 'xxx')) 2620 assert_equal('xxx', get(g:, 'no_var', 'xxx')) 2621 unlet g:some_var 2622 2623 b:some_var = 'some' 2624 assert_equal('some', get(b:, 'some_var')) 2625 assert_equal('some', get(b:, 'some_var', 'xxx')) 2626 assert_equal('xxx', get(b:, 'no_var', 'xxx')) 2627 unlet b:some_var 2628 2629 w:some_var = 'some' 2630 assert_equal('some', get(w:, 'some_var')) 2631 assert_equal('some', get(w:, 'some_var', 'xxx')) 2632 assert_equal('xxx', get(w:, 'no_var', 'xxx')) 2633 unlet w:some_var 2634 2635 t:some_var = 'some' 2636 assert_equal('some', get(t:, 'some_var')) 2637 assert_equal('some', get(t:, 'some_var', 'xxx')) 2638 assert_equal('xxx', get(t:, 'no_var', 'xxx')) 2639 unlet t:some_var 2640 2641 # check using g: in a for loop more than DO_NOT_FREE_CNT times 2642 for i in range(100000) 2643 if has_key(g:, 'does-not-exist') 2644 endif 2645 endfor 2646 END 2647 CheckDefAndScriptSuccess(lines) 2648enddef 2649 2650def Test_expr7_parens() 2651 # (expr) 2652 var lines =<< trim END 2653 assert_equal(4, (6 * 4) / 6) 2654 assert_equal(0, 6 * ( 4 / 6 )) 2655 2656 assert_equal(6, +6) 2657 assert_equal(-6, -6) 2658 assert_equal(false, !-3) 2659 assert_equal(true, !+0) 2660 2661 assert_equal(7, 5 + ( 2662 2)) 2663 assert_equal(7, 5 + ( 2664 2 2665 )) 2666 assert_equal(7, 5 + ( # comment 2667 2)) 2668 assert_equal(7, 5 + ( # comment 2669 # comment 2670 2)) 2671 2672 var s = ( 2673 'one' 2674 .. 2675 'two' 2676 ) 2677 assert_equal('onetwo', s) 2678 END 2679 CheckDefAndScriptSuccess(lines) 2680enddef 2681 2682def Test_expr7_negate_add() 2683 var lines =<< trim END 2684 assert_equal(-99, -99) 2685 assert_equal(-99, - 99) 2686 assert_equal(99, +99) 2687 2688 var nr = 88 2689 assert_equal(-88, -nr) 2690 assert_equal(-88, - nr) 2691 assert_equal(88, + nr) 2692 END 2693 CheckDefAndScriptSuccess(lines) 2694 2695 lines =<< trim END 2696 var n = 12 2697 echo ++n 2698 END 2699 CheckDefAndScriptFailure(lines, 'E15:') 2700 lines =<< trim END 2701 var n = 12 2702 echo --n 2703 END 2704 CheckDefAndScriptFailure(lines, 'E15:') 2705 lines =<< trim END 2706 var n = 12 2707 echo +-n 2708 END 2709 CheckDefAndScriptFailure(lines, 'E15:') 2710 lines =<< trim END 2711 var n = 12 2712 echo -+n 2713 END 2714 CheckDefAndScriptFailure(lines, 'E15:') 2715 lines =<< trim END 2716 var n = 12 2717 echo - -n 2718 END 2719 CheckDefAndScriptFailure(lines, 'E15:') 2720 lines =<< trim END 2721 var n = 12 2722 echo + +n 2723 END 2724 CheckDefAndScriptFailure(lines, 'E15:') 2725 2726 lines =<< trim END 2727 var n = 12 2728 :1 2729 ++n 2730 END 2731 CheckDefAndScriptFailure(lines, 'E1050:') 2732 lines =<< trim END 2733 var n = 12 2734 :1 2735 --n 2736 END 2737 CheckDefAndScriptFailure(lines, 'E1050:') 2738enddef 2739 2740def Test_expr7_legacy_script() 2741 var lines =<< trim END 2742 let s:legacy = 'legacy' 2743 def GetLocal(): string 2744 return legacy 2745 enddef 2746 def GetLocalPrefix(): string 2747 return s:legacy 2748 enddef 2749 call assert_equal('legacy', GetLocal()) 2750 call assert_equal('legacy', GetLocalPrefix()) 2751 END 2752 CheckScriptSuccess(lines) 2753enddef 2754 2755def Echo(arg: any): string 2756 return arg 2757enddef 2758 2759def s:Echo4Arg(arg: any): string 2760 return arg 2761enddef 2762 2763def Test_expr7_call() 2764 var lines =<< trim END 2765 assert_equal('yes', 'yes'->Echo()) 2766 assert_equal(true, !range(5)->empty()) 2767 assert_equal([0, 1, 2], 3->range()) 2768 END 2769 CheckDefAndScriptSuccess(lines) 2770 2771 assert_equal('yes', 'yes' 2772 ->s:Echo4Arg()) 2773 2774 CheckDefAndScriptFailure(["var x = 'yes'->Echo"], 'E107:', 1) 2775 CheckDefAndScriptFailure2([ 2776 "var x = substitute ('x', 'x', 'x', 'x')" 2777 ], 'E1001:', 'E121:', 1) 2778 CheckDefAndScriptFailure2(["var Ref = function('len' [1, 2])"], 'E1123:', 'E116:', 1) 2779 2780 var auto_lines =<< trim END 2781 def g:some#func(): string 2782 return 'found' 2783 enddef 2784 END 2785 mkdir('Xruntime/autoload', 'p') 2786 writefile(auto_lines, 'Xruntime/autoload/some.vim') 2787 var save_rtp = &rtp 2788 &rtp = getcwd() .. '/Xruntime,' .. &rtp 2789 assert_equal('found', g:some#func()) 2790 assert_equal('found', some#func()) 2791 2792 &rtp = save_rtp 2793 delete('Xruntime', 'rf') 2794enddef 2795 2796def Test_expr7_method_call() 2797 var lines =<< trim END 2798 new 2799 setline(1, ['first', 'last']) 2800 'second'->append(1) 2801 "third"->append(2) 2802 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$')) 2803 bwipe! 2804 2805 var bufnr = bufnr() 2806 var loclist = [{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 2807 loclist->setloclist(0) 2808 assert_equal([{bufnr: bufnr, 2809 lnum: 42, 2810 col: 17, 2811 text: 'wrong', 2812 pattern: '', 2813 valid: 1, 2814 vcol: 0, 2815 nr: 0, 2816 type: '', 2817 module: ''} 2818 ], getloclist(0)) 2819 2820 var result: bool = get({n: 0}, 'n', 0) 2821 assert_equal(false, result) 2822 2823 assert_equal('+string+', 'string'->((s) => '+' .. s .. '+')()) 2824 assert_equal('-text-', 'text'->((s, c) => c .. s .. c)('-')) 2825 2826 var Join = (l) => join(l, 'x') 2827 assert_equal('axb', ['a', 'b']->(Join)()) 2828 END 2829 CheckDefAndScriptSuccess(lines) 2830enddef 2831 2832 2833def Test_expr7_not() 2834 var lines =<< trim END 2835 assert_equal(true, !'') 2836 assert_equal(true, ![]) 2837 assert_equal(false, !'asdf') 2838 assert_equal(false, ![2]) 2839 assert_equal(true, !!'asdf') 2840 assert_equal(true, !![2]) 2841 2842 assert_equal(true, ! false) 2843 assert_equal(true, !! true) 2844 assert_equal(true, ! ! true) 2845 assert_equal(true, !!! false) 2846 assert_equal(true, ! ! ! false) 2847 2848 g:true = true 2849 g:false = false 2850 assert_equal(true, ! g:false) 2851 assert_equal(true, !! g:true) 2852 assert_equal(true, ! ! g:true) 2853 assert_equal(true, !!! g:false) 2854 assert_equal(true, ! ! ! g:false) 2855 unlet g:true 2856 unlet g:false 2857 2858 assert_equal(true, !test_null_partial()) 2859 assert_equal(false, !() => 'yes') 2860 2861 assert_equal(true, !test_null_dict()) 2862 assert_equal(true, !{}) 2863 assert_equal(false, !{yes: 'no'}) 2864 2865 if has('channel') 2866 assert_equal(true, !test_null_job()) 2867 assert_equal(true, !test_null_channel()) 2868 endif 2869 2870 assert_equal(true, !test_null_blob()) 2871 assert_equal(true, !0z) 2872 assert_equal(false, !0z01) 2873 2874 assert_equal(true, !test_void()) 2875 assert_equal(true, !test_unknown()) 2876 2877 assert_equal(false, ![1, 2, 3]->reverse()) 2878 assert_equal(true, ![]->reverse()) 2879 END 2880 CheckDefAndScriptSuccess(lines) 2881enddef 2882 2883func Test_expr7_fails() 2884 call CheckDefFailure(["var x = (12"], "E1097:", 3) 2885 call CheckScriptFailure(['vim9script', "var x = (12"], 'E110:', 2) 2886 2887 call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1) 2888 call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1) 2889 call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1) 2890 call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E39:", 'E745:', 1) 2891 call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E39:", 'E728:', 1) 2892 2893 call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1) 2894 call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1) 2895 2896 call CheckDefFailure(["var x = [1, 2"], "E697:", 2) 2897 call CheckScriptFailure(['vim9script', "var x = [1, 2"], 'E696:', 2) 2898 2899 call CheckDefAndScriptFailure2(["var x = [notfound]"], "E1001:", 'E121:', 1) 2900 2901 call CheckDefAndScriptFailure2(["var X = () => 123)"], "E488:", 'E15:', 1) 2902 call CheckDefAndScriptFailure(["var x = 123->((x) => x + 5)"], "E107:", 1) 2903 2904 call CheckDefAndScriptFailure(["var x = ¬exist"], 'E113:', 1) 2905 call CheckDefAndScriptFailure2(["&grepprg = [343]"], 'E1012:', 'E730:', 1) 2906 2907 call CheckDefExecAndScriptFailure(["echo s:doesnt_exist"], 'E121:', 1) 2908 call CheckDefExecAndScriptFailure(["echo g:doesnt_exist"], 'E121:', 1) 2909 2910 call CheckDefAndScriptFailure2(["echo a:somevar"], 'E1075:', 'E121:', 1) 2911 call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1) 2912 call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1) 2913 2914 call CheckDefExecAndScriptFailure(["var x = +g:astring"], 'E1030:', 1) 2915 call CheckDefExecAndScriptFailure(["var x = +g:ablob"], 'E974:', 1) 2916 call CheckDefExecAndScriptFailure(["var x = +g:alist"], 'E745:', 1) 2917 call CheckDefExecAndScriptFailure(["var x = +g:adict"], 'E728:', 1) 2918 2919 call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E715:', 'E15:', 2) 2920 2921 call CheckDefAndScriptFailure2(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 'E260: Missing name after ->', 1) 2922 2923 call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2) 2924 call CheckScriptFailure(['vim9script', "[1, 2->len()"], 'E696:', 2) 2925 2926 call CheckDefFailure(["{a: 1->len()"], 'E723:', 2) 2927 call CheckScriptFailure(['vim9script', "{a: 1->len()"], 'E722:', 2) 2928 2929 call CheckDefExecFailure(["{['a']: 1->len()"], 'E723:', 2) 2930 call CheckScriptFailure(['vim9script', "{['a']: 1->len()"], 'E722:', 2) 2931endfunc 2932 2933let g:Funcrefs = [function('add')] 2934 2935func CallMe(arg) 2936 return a:arg 2937endfunc 2938 2939func CallMe2(one, two) 2940 return a:one .. a:two 2941endfunc 2942 2943def Test_expr7_trailing() 2944 var lines =<< trim END 2945 # user function call 2946 assert_equal(123, g:CallMe(123)) 2947 assert_equal(123, g:CallMe( 123)) 2948 assert_equal(123, g:CallMe(123 )) 2949 assert_equal('yesno', g:CallMe2('yes', 'no')) 2950 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 2951 assert_equal('nothing', g:CallMe('nothing')) 2952 2953 # partial call 2954 var Part = function('g:CallMe') 2955 assert_equal('yes', Part('yes')) 2956 2957 # funcref call, using list index 2958 var l = [] 2959 g:Funcrefs[0](l, 2) 2960 assert_equal([2], l) 2961 2962 # method call 2963 l = [2, 5, 6] 2964 l->map((k, v) => k + v) 2965 assert_equal([2, 6, 8], l) 2966 2967 # lambda method call 2968 l = [2, 5] 2969 l->((ll) => add(ll, 8))() 2970 assert_equal([2, 5, 8], l) 2971 2972 # dict member 2973 var d = {key: 123} 2974 assert_equal(123, d.key) 2975 END 2976 CheckDefAndScriptSuccess(lines) 2977enddef 2978 2979def Test_expr7_string_subscript() 2980 var lines =<< trim END 2981 var text = 'abcdef' 2982 assert_equal('f', text[-1]) 2983 assert_equal('a', text[0]) 2984 assert_equal('e', text[4]) 2985 assert_equal('f', text[5]) 2986 assert_equal('', text[6]) 2987 2988 text = 'ábçdë' 2989 assert_equal('ë', text[-1]) 2990 assert_equal('d', text[-2]) 2991 assert_equal('ç', text[-3]) 2992 assert_equal('b', text[-4]) 2993 assert_equal('á', text[-5]) 2994 assert_equal('', text[-6]) 2995 2996 text = 'ábçdëf' 2997 assert_equal('', text[-999]) 2998 assert_equal('f', text[-1]) 2999 assert_equal('á', text[0]) 3000 assert_equal('b', text[1]) 3001 assert_equal('ç', text[2]) 3002 assert_equal('d', text[3]) 3003 assert_equal('ë', text[4]) 3004 assert_equal('f', text[5]) 3005 assert_equal('', text[6]) 3006 assert_equal('', text[999]) 3007 3008 assert_equal('ábçdëf', text[0 : -1]) 3009 assert_equal('ábçdëf', text[0 : -1]) 3010 assert_equal('ábçdëf', text[0 : -1]) 3011 assert_equal('ábçdëf', text[0 : -1]) 3012 assert_equal('ábçdëf', text[0 3013 : -1]) 3014 assert_equal('ábçdëf', text[0 : 3015 -1]) 3016 assert_equal('ábçdëf', text[0 : -1 3017 ]) 3018 assert_equal('bçdëf', text[1 : -1]) 3019 assert_equal('çdëf', text[2 : -1]) 3020 assert_equal('dëf', text[3 : -1]) 3021 assert_equal('ëf', text[4 : -1]) 3022 assert_equal('f', text[5 : -1]) 3023 assert_equal('', text[6 : -1]) 3024 assert_equal('', text[999 : -1]) 3025 3026 assert_equal('ábçd', text[: 3]) 3027 assert_equal('bçdëf', text[1 :]) 3028 assert_equal('ábçdëf', text[:]) 3029 END 3030 CheckDefAndScriptSuccess(lines) 3031 3032 lines =<< trim END 3033 var d = 'asdf'[1 : 3034 END 3035 CheckDefFailure(lines, 'E1097:', 3) 3036 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2) 3037 3038 lines =<< trim END 3039 var d = 'asdf'[1 : xxx] 3040 END 3041 CheckDefAndScriptFailure2(lines, 'E1001:', 'E121:', 1) 3042 3043 lines =<< trim END 3044 var d = 'asdf'[1 : 2 3045 END 3046 CheckDefFailure(lines, 'E1097:', 3) 3047 CheckScriptFailure(['vim9script'] + lines, 'E111:', 2) 3048 3049 lines =<< trim END 3050 var d = 'asdf'[1 : 2 3051 echo d 3052 END 3053 CheckDefAndScriptFailure(lines, 'E111:', 2) 3054 3055 lines =<< trim END 3056 var d = 'asdf'['1'] 3057 echo d 3058 END 3059 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1) 3060 3061 lines =<< trim END 3062 var d = 'asdf'['1' : 2] 3063 echo d 3064 END 3065 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1) 3066 3067 lines =<< trim END 3068 var d = 'asdf'[1 : '2'] 3069 echo d 3070 END 3071 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "2"', 1) 3072enddef 3073 3074def Test_expr7_list_subscript() 3075 var lines =<< trim END 3076 var list = [0, 1, 2, 3, 4] 3077 assert_equal(0, list[0]) 3078 assert_equal(4, list[4]) 3079 assert_equal(4, list[-1]) 3080 assert_equal(0, list[-5]) 3081 3082 assert_equal([0, 1, 2, 3, 4], list[0 : 4]) 3083 assert_equal([0, 1, 2, 3, 4], list[:]) 3084 assert_equal([1, 2, 3, 4], list[1 :]) 3085 assert_equal([2, 3, 4], list[2 : -1]) 3086 assert_equal([4], list[4 : -1]) 3087 assert_equal([], list[5 : -1]) 3088 assert_equal([], list[999 : -1]) 3089 assert_equal([1, 2, 3, 4], list[g:theone : g:thefour]) 3090 3091 assert_equal([0, 1, 2, 3], list[0 : 3]) 3092 assert_equal([0], list[0 : 0]) 3093 assert_equal([0, 1, 2, 3, 4], list[0 : -1]) 3094 assert_equal([0, 1, 2], list[0 : -3]) 3095 assert_equal([0], list[0 : -5]) 3096 assert_equal([], list[0 : -6]) 3097 assert_equal([], list[0 : -99]) 3098 END 3099 CheckDefAndScriptSuccess(lines) 3100 3101 lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 3102 CheckDefExecAndScriptFailure2(lines, 'E1012:', 'E1030:', 2) 3103 3104 lines =<< trim END 3105 var ld = [] 3106 def Func() 3107 eval ld[0].key 3108 enddef 3109 defcompile 3110 END 3111 CheckDefAndScriptSuccess(lines) 3112enddef 3113 3114def Test_expr7_dict_subscript() 3115 var lines =<< trim END 3116 var l = [{lnum: 2}, {lnum: 1}] 3117 var res = l[0].lnum > l[1].lnum 3118 assert_true(res) 3119 3120 var dd = {} 3121 def Func1() 3122 eval dd.key1.key2 3123 enddef 3124 def Func2() 3125 eval dd['key1'].key2 3126 enddef 3127 defcompile 3128 END 3129 CheckDefAndScriptSuccess(lines) 3130enddef 3131 3132def Test_expr7_subscript_linebreak() 3133 var lines =<< trim END 3134 var range = range( 3135 3) 3136 var l = range 3137 ->mapnew('string(v:key)') 3138 assert_equal(['0', '1', '2'], l) 3139 3140 l = range 3141 ->mapnew('string(v:key)') 3142 assert_equal(['0', '1', '2'], l) 3143 3144 l = range # comment 3145 ->mapnew('string(v:key)') 3146 assert_equal(['0', '1', '2'], l) 3147 3148 l = range 3149 3150 ->mapnew('string(v:key)') 3151 assert_equal(['0', '1', '2'], l) 3152 3153 l = range 3154 # comment 3155 ->mapnew('string(v:key)') 3156 assert_equal(['0', '1', '2'], l) 3157 3158 assert_equal('1', l[ 3159 1]) 3160 3161 var d = {one: 33} 3162 assert_equal(33, d 3163 .one) 3164 END 3165 CheckDefAndScriptSuccess(lines) 3166 3167 lines =<< trim END 3168 var d = {one: 33} 3169 assert_equal(33, d. 3170 one) 3171 END 3172 CheckDefAndScriptFailure2(lines, 'E1127:', 'E116:', 2) 3173enddef 3174 3175func Test_expr7_trailing_fails() 3176 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8))'], 'E107:', 2) 3177 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8)) ()'], 'E274:', 2) 3178endfunc 3179 3180func Test_expr_fails() 3181 call CheckDefAndScriptFailure2(["var x = '1'is2"], 'E488:', 'E15:', 1) 3182 call CheckDefAndScriptFailure2(["var x = '1'isnot2"], 'E488:', 'E15:', 1) 3183 3184 call CheckDefAndScriptFailure2(["CallMe ('yes')"], 'E476:', 'E492:', 1) 3185 3186 call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1) 3187 3188 call CheckDefAndScriptFailure2(["v:nosuch += 3"], 'E1001:', 'E121:', 1) 3189 call CheckDefAndScriptFailure(["var v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1) 3190 call CheckDefAndScriptFailure2(["var asdf = v:nosuch"], 'E1001:', 'E121:', 1) 3191 3192 call CheckDefFailure(["echo len('asdf'"], 'E110:', 2) 3193 call CheckScriptFailure(['vim9script', "echo len('asdf'"], 'E116:', 2) 3194 3195 call CheckDefAndScriptFailure2(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 'E117:', 1) 3196 call CheckDefAndScriptFailure(["echo doesnotexist()"], 'E117:', 1) 3197endfunc 3198 3199" vim: shiftwidth=2 sts=2 expandtab 3200