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