1" Tests for Vim9 script expressions 2 3source check.vim 4source vim9.vim 5 6 7let g:cond = v:false 8def FuncOne(arg: number): string 9 return 'yes' 10enddef 11def FuncTwo(arg: number): number 12 return 123 13enddef 14 15" test cond ? expr : expr 16def Test_expr1() 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 let var = 1 32 assert_equal('one', var ? '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 var = 0 44 assert_equal('two', var ? 'one' : 'two') 45 46 let Some: func = function('len') 47 let Other: func = function('winnr') 48 let Res: func = g:atrue ? Some : Other 49 assert_equal(function('len'), Res) 50 51 let RetOne: func(string): number = function('len') 52 let RetTwo: func(string): number = function('winnr') 53 let RetThat: func = g:atrue ? RetOne : RetTwo 54 assert_equal(function('len'), RetThat) 55 56 let X = FuncOne 57 let Y = FuncTwo 58 let Z = g:cond ? FuncOne : FuncTwo 59 assert_equal(123, Z(3)) 60enddef 61 62def Test_expr1_vimscript() 63 # check line continuation 64 let lines =<< trim END 65 vim9script 66 let var = 1 67 ? 'yes' 68 : 'no' 69 assert_equal('yes', var) 70 END 71 CheckScriptSuccess(lines) 72 73 lines =<< trim END 74 vim9script 75 let var = v:false 76 ? 'yes' 77 : 'no' 78 assert_equal('no', var) 79 END 80 CheckScriptSuccess(lines) 81 82 lines =<< trim END 83 vim9script 84 let var = v:false ? 85 'yes' : 86 'no' 87 assert_equal('no', var) 88 END 89 CheckScriptSuccess(lines) 90 91 # check white space 92 lines =<< trim END 93 vim9script 94 let var = v:true?1:2 95 END 96 CheckScriptFailure(lines, 'E1004:') 97 lines =<< trim END 98 vim9script 99 let var = v:true? 1 : 2 100 END 101 CheckScriptFailure(lines, 'E1004:') 102 lines =<< trim END 103 vim9script 104 let var = v:true ?1 : 2 105 END 106 CheckScriptFailure(lines, 'E1004:') 107 lines =<< trim END 108 vim9script 109 let var = v:true ? 1: 2 110 END 111 CheckScriptFailure(lines, 'E1004:') 112 lines =<< trim END 113 vim9script 114 let var = v:true ? 1 :2 115 END 116 CheckScriptFailure(lines, 'E1004:') 117enddef 118 119func Test_expr1_fails() 120 call CheckDefFailure(["let x = 1 ? 'one'"], "Missing ':' after '?'") 121 call CheckDefFailure(["let x = 1 ? 'one' : xxx"], "E1001:") 122 123 let msg = "white space required before and after '?'" 124 call CheckDefFailure(["let x = 1? 'one' : 'two'"], msg) 125 call CheckDefFailure(["let x = 1 ?'one' : 'two'"], msg) 126 call CheckDefFailure(["let x = 1?'one' : 'two'"], msg) 127 128 let msg = "white space required before and after ':'" 129 call CheckDefFailure(["let x = 1 ? 'one': 'two'"], msg) 130 call CheckDefFailure(["let x = 1 ? 'one' :'two'"], msg) 131 call CheckDefFailure(["let x = 1 ? 'one':'two'"], msg) 132 133 " missing argument detected even when common type is used 134 call CheckDefFailure([ 135 \ 'let X = FuncOne', 136 \ 'let Y = FuncTwo', 137 \ 'let Z = g:cond ? FuncOne : FuncTwo', 138 \ 'Z()'], 'E119:') 139endfunc 140 141" TODO: define inside test function 142def Record(val: any): any 143 g:vals->add(val) 144 return val 145enddef 146 147" test || 148def Test_expr2() 149 assert_equal(2, 2 || 0) 150 assert_equal(7, 0 || 151 0 || 152 7) 153 assert_equal(0, 0 || 0) 154 assert_equal(0, 0 155 || 0) 156 assert_equal('', 0 || '') 157 158 g:vals = [] 159 assert_equal(3, Record(3) || Record(1)) 160 assert_equal([3], g:vals) 161 162 g:vals = [] 163 assert_equal(5, Record(0) || Record(5)) 164 assert_equal([0, 5], g:vals) 165 166 g:vals = [] 167 assert_equal(4, Record(0) 168 || Record(4) 169 || Record(0)) 170 assert_equal([0, 4], g:vals) 171 172 g:vals = [] 173 assert_equal(0, Record([]) || Record('') || Record(0)) 174 assert_equal([[], '', 0], g:vals) 175enddef 176 177def Test_expr2_vimscript() 178 # check line continuation 179 let lines =<< trim END 180 vim9script 181 let var = 0 182 || 1 183 assert_equal(1, var) 184 END 185 CheckScriptSuccess(lines) 186 187 lines =<< trim END 188 vim9script 189 let var = v:false 190 || v:true 191 || v:false 192 assert_equal(v:true, var) 193 END 194 CheckScriptSuccess(lines) 195 196 lines =<< trim END 197 vim9script 198 let var = v:false || 199 v:true || 200 v:false 201 assert_equal(v:true, var) 202 END 203 CheckScriptSuccess(lines) 204 205 # check white space 206 lines =<< trim END 207 vim9script 208 let var = v:true||v:true 209 END 210 CheckScriptFailure(lines, 'E1004:') 211 lines =<< trim END 212 vim9script 213 let var = v:true ||v:true 214 END 215 CheckScriptFailure(lines, 'E1004:') 216 lines =<< trim END 217 vim9script 218 let var = v:true|| v:true 219 END 220 CheckScriptFailure(lines, 'E1004:') 221 222 # check keeping the value 223 lines =<< trim END 224 vim9script 225 assert_equal(2, 2 || 0) 226 assert_equal(7, 0 || 227 0 || 228 7) 229 assert_equal(0, 0 || 0) 230 assert_equal(0, 0 231 || 0) 232 assert_equal('', 0 || '') 233 234 g:vals = [] 235 assert_equal(3, Record(3) || Record(1)) 236 assert_equal([3], g:vals) 237 238 g:vals = [] 239 assert_equal(5, Record(0) || Record(5)) 240 assert_equal([0, 5], g:vals) 241 242 g:vals = [] 243 assert_equal(4, Record(0) 244 || Record(4) 245 || Record(0)) 246 assert_equal([0, 4], g:vals) 247 248 g:vals = [] 249 assert_equal(0, Record([]) || Record('') || Record(0)) 250 assert_equal([[], '', 0], g:vals) 251 END 252 CheckScriptSuccess(lines) 253enddef 254 255func Test_expr2_fails() 256 let msg = "white space required before and after '||'" 257 call CheckDefFailure(["let x = 1||2"], msg) 258 call CheckDefFailure(["let x = 1 ||2"], msg) 259 call CheckDefFailure(["let x = 1|| 2"], msg) 260 261 call CheckDefFailure(["let x = 1 || xxx"], 'E1001:') 262endfunc 263 264" test && 265def Test_expr3() 266 assert_equal(0, 2 && 0) 267 assert_equal(0, 0 && 268 0 && 269 7) 270 assert_equal(7, 2 271 && 3 272 && 7) 273 assert_equal(0, 0 && 0) 274 assert_equal(0, 0 && '') 275 assert_equal('', 8 && '') 276 277 g:vals = [] 278 assert_equal(1, Record(3) && Record(1)) 279 assert_equal([3, 1], g:vals) 280 281 g:vals = [] 282 assert_equal(0, Record(0) && Record(5)) 283 assert_equal([0], g:vals) 284 285 g:vals = [] 286 assert_equal(0, Record(0) && Record(4) && Record(0)) 287 assert_equal([0], g:vals) 288 289 g:vals = [] 290 assert_equal(0, Record(8) && Record(4) && Record(0)) 291 assert_equal([8, 4, 0], g:vals) 292 293 g:vals = [] 294 assert_equal(0, Record([1]) && Record('z') && Record(0)) 295 assert_equal([[1], 'z', 0], g:vals) 296enddef 297 298def Test_expr3_vimscript() 299 # check line continuation 300 let lines =<< trim END 301 vim9script 302 let var = 0 303 && 1 304 assert_equal(0, var) 305 END 306 CheckScriptSuccess(lines) 307 308 lines =<< trim END 309 vim9script 310 let var = v:true 311 && v:true 312 && v:true 313 assert_equal(v:true, var) 314 END 315 CheckScriptSuccess(lines) 316 317 lines =<< trim END 318 vim9script 319 let var = v:true && 320 v:true && 321 v:true 322 assert_equal(v:true, var) 323 END 324 CheckScriptSuccess(lines) 325 326 # check white space 327 lines =<< trim END 328 vim9script 329 let var = v:true&&v:true 330 END 331 CheckScriptFailure(lines, 'E1004:') 332 lines =<< trim END 333 vim9script 334 let var = v:true &&v:true 335 END 336 CheckScriptFailure(lines, 'E1004:') 337 lines =<< trim END 338 vim9script 339 let var = v:true&& v:true 340 END 341 CheckScriptFailure(lines, 'E1004:') 342 343 # check keeping the value 344 lines =<< trim END 345 vim9script 346 assert_equal(0, 2 && 0) 347 assert_equal(0, 0 && 348 0 && 349 7) 350 assert_equal(7, 2 351 && 3 352 && 7) 353 assert_equal(0, 0 && 0) 354 assert_equal(0, 0 && '') 355 assert_equal('', 8 && '') 356 357 g:vals = [] 358 assert_equal(1, Record(3) && Record(1)) 359 assert_equal([3, 1], g:vals) 360 361 g:vals = [] 362 assert_equal(0, Record(0) && Record(5)) 363 assert_equal([0], g:vals) 364 365 g:vals = [] 366 assert_equal(0, Record(0) && Record(4) && Record(0)) 367 assert_equal([0], g:vals) 368 369 g:vals = [] 370 assert_equal(0, Record(8) && Record(4) && Record(0)) 371 assert_equal([8, 4, 0], g:vals) 372 373 g:vals = [] 374 assert_equal(0, Record([1]) && Record('z') && Record(0)) 375 assert_equal([[1], 'z', 0], g:vals) 376 END 377 CheckScriptSuccess(lines) 378enddef 379 380func Test_expr3_fails() 381 let msg = "white space required before and after '&&'" 382 call CheckDefFailure(["let x = 1&&2"], msg) 383 call CheckDefFailure(["let x = 1 &&2"], msg) 384 call CheckDefFailure(["let x = 1&& 2"], msg) 385endfunc 386 387" global variables to use for tests with the "any" type 388let atrue = v:true 389let afalse = v:false 390let anone = v:none 391let anull = v:null 392let anint = 10 393let theone = 1 394let thefour = 4 395if has('float') 396 let afloat = 0.1 397endif 398let astring = 'asdf' 399let ablob = 0z01ab 400let alist = [2, 3, 4] 401let adict = #{aaa: 2, bbb: 8} 402 403" test == comperator 404def Test_expr4_equal() 405 let trueVar = true 406 let falseVar = false 407 assert_equal(true, true == true) 408 assert_equal(false, true == 409 false) 410 assert_equal(true, true 411 == trueVar) 412 assert_equal(false, true == falseVar) 413 assert_equal(true, true == g:atrue) 414 assert_equal(false, g:atrue == false) 415 416 assert_equal(true, v:none == v:none) 417 assert_equal(false, v:none == v:null) 418 assert_equal(true, g:anone == v:none) 419 assert_equal(false, v:none == g:anull) 420 421 let nr0 = 0 422 let nr61 = 61 423 assert_equal(false, 2 == 0) 424 assert_equal(false, 2 == nr0) 425 assert_equal(true, 61 == 61) 426 assert_equal(true, 61 == nr61) 427 assert_equal(true, g:anint == 10) 428 assert_equal(false, 61 == g:anint) 429 430 if has('float') 431 let ff = 0.3 432 assert_equal(true, ff == 0.3) 433 assert_equal(false, 0.4 == ff) 434 assert_equal(true, 0.1 == g:afloat) 435 assert_equal(false, g:afloat == 0.3) 436 437 ff = 3.0 438 assert_equal(true, ff == 3) 439 assert_equal(true, 3 == ff) 440 ff = 3.1 441 assert_equal(false, ff == 3) 442 assert_equal(false, 3 == ff) 443 endif 444 445 assert_equal(true, 'abc' == 'abc') 446 assert_equal(false, 'xyz' == 'abc') 447 assert_equal(true, g:astring == 'asdf') 448 assert_equal(false, 'xyz' == g:astring) 449 450 assert_equal(false, 'abc' == 'aBc') 451 assert_equal(false, 'abc' ==# 'aBc') 452 assert_equal(true, 'abc' ==? 'aBc') 453 454 assert_equal(false, 'abc' == 'ABC') 455 set ignorecase 456 assert_equal(false, 'abc' == 'ABC') 457 assert_equal(false, 'abc' ==# 'ABC') 458 set noignorecase 459 460 call CheckDefFailure(["let x = 'a' == xxx"], 'E1001:') 461 462 let bb = 0z3f 463 assert_equal(true, 0z3f == bb) 464 assert_equal(false, bb == 0z4f) 465 assert_equal(true, g:ablob == 0z01ab) 466 assert_equal(false, 0z3f == g:ablob) 467 468 assert_equal(true, [1, 2, 3] == [1, 2, 3]) 469 assert_equal(false, [1, 2, 3] == [2, 3, 1]) 470 assert_equal(true, [2, 3, 4] == g:alist) 471 assert_equal(false, g:alist == [2, 3, 1]) 472 assert_equal(false, [1, 2, 3] == []) 473 assert_equal(false, [1, 2, 3] == ['1', '2', '3']) 474 475 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2}) 476 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2}) 477 assert_equal(false, #{one: 1, two: 2} == #{two: 2}) 478 assert_equal(false, #{one: 1, two: 2} == #{}) 479 assert_equal(true, g:adict == #{bbb: 8, aaa: 2}) 480 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict) 481 482 assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal')) 483 assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is')) 484 485 assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123])) 486 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123])) 487 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999])) 488 489 let OneFunc: func 490 let TwoFunc: func 491 OneFunc = function('len') 492 TwoFunc = function('len') 493 assert_equal(true, OneFunc('abc') == TwoFunc('123')) 494enddef 495 496" test != comperator 497def Test_expr4_notequal() 498 let trueVar = true 499 let falseVar = false 500 assert_equal(false, true != true) 501 assert_equal(true, true != 502 false) 503 assert_equal(false, true 504 != trueVar) 505 assert_equal(true, true != falseVar) 506 assert_equal(false, true != g:atrue) 507 assert_equal(true, g:atrue != false) 508 509 assert_equal(false, v:none != v:none) 510 assert_equal(true, v:none != v:null) 511 assert_equal(false, g:anone != v:none) 512 assert_equal(true, v:none != g:anull) 513 514 let nr55 = 55 515 let nr0 = 55 516 assert_equal(true, 2 != 0) 517 assert_equal(true, 2 != nr0) 518 assert_equal(false, 55 != 55) 519 assert_equal(false, 55 != nr55) 520 assert_equal(false, g:anint != 10) 521 assert_equal(true, 61 != g:anint) 522 523 if has('float') 524 let ff = 0.3 525 assert_equal(false, 0.3 != ff) 526 assert_equal(true, 0.4 != ff) 527 assert_equal(false, 0.1 != g:afloat) 528 assert_equal(true, g:afloat != 0.3) 529 530 ff = 3.0 531 assert_equal(false, ff != 3) 532 assert_equal(false, 3 != ff) 533 ff = 3.1 534 assert_equal(true, ff != 3) 535 assert_equal(true, 3 != ff) 536 endif 537 538 assert_equal(false, 'abc' != 'abc') 539 assert_equal(true, 'xyz' != 'abc') 540 assert_equal(false, g:astring != 'asdf') 541 assert_equal(true, 'xyz' != g:astring) 542 543 assert_equal(true, 'abc' != 'ABC') 544 set ignorecase 545 assert_equal(true, 'abc' != 'ABC') 546 set noignorecase 547 548 let bb = 0z3f 549 assert_equal(false, 0z3f != bb) 550 assert_equal(true, bb != 0z4f) 551 assert_equal(false, g:ablob != 0z01ab) 552 assert_equal(true, 0z3f != g:ablob) 553 554 assert_equal(false, [1, 2, 3] != [1, 2, 3]) 555 assert_equal(true, [1, 2, 3] != [2, 3, 1]) 556 assert_equal(false, [2, 3, 4] != g:alist) 557 assert_equal(true, g:alist != [2, 3, 1]) 558 assert_equal(true, [1, 2, 3] != []) 559 assert_equal(true, [1, 2, 3] != ['1', '2', '3']) 560 561 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2}) 562 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2}) 563 assert_equal(true, #{one: 1, two: 2} != #{two: 2}) 564 assert_equal(true, #{one: 1, two: 2} != #{}) 565 assert_equal(false, g:adict != #{bbb: 8, aaa: 2}) 566 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict) 567 568 assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal')) 569 assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is')) 570 571 assert_equal(false, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [123])) 572 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_is', [123])) 573 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [999])) 574enddef 575 576" test > comperator 577def Test_expr4_greater() 578 assert_true(2 > 0) 579 assert_true(2 > 580 1) 581 assert_false(2 > 2) 582 assert_false(2 > 3) 583 let nr2 = 2 584 assert_true(nr2 > 0) 585 assert_true(nr2 > 586 1) 587 assert_false(nr2 > 2) 588 assert_false(nr2 589 > 3) 590 if has('float') 591 let ff = 2.0 592 assert_true(ff > 0.0) 593 assert_true(ff > 1.0) 594 assert_false(ff > 2.0) 595 assert_false(ff > 3.0) 596 endif 597enddef 598 599" test >= comperator 600def Test_expr4_greaterequal() 601 assert_true(2 >= 0) 602 assert_true(2 >= 603 2) 604 assert_false(2 >= 3) 605 let nr2 = 2 606 assert_true(nr2 >= 0) 607 assert_true(nr2 >= 2) 608 assert_false(nr2 >= 3) 609 if has('float') 610 let ff = 2.0 611 assert_true(ff >= 0.0) 612 assert_true(ff >= 2.0) 613 assert_false(ff >= 3.0) 614 endif 615enddef 616 617" test < comperator 618def Test_expr4_smaller() 619 assert_false(2 < 0) 620 assert_false(2 < 621 2) 622 assert_true(2 623 < 3) 624 let nr2 = 2 625 assert_false(nr2 < 0) 626 assert_false(nr2 < 2) 627 assert_true(nr2 < 3) 628 if has('float') 629 let ff = 2.0 630 assert_false(ff < 0.0) 631 assert_false(ff < 2.0) 632 assert_true(ff < 3.0) 633 endif 634enddef 635 636" test <= comperator 637def Test_expr4_smallerequal() 638 assert_false(2 <= 0) 639 assert_false(2 <= 640 1) 641 assert_true(2 642 <= 2) 643 assert_true(2 <= 3) 644 let nr2 = 2 645 assert_false(nr2 <= 0) 646 assert_false(nr2 <= 1) 647 assert_true(nr2 <= 2) 648 assert_true(nr2 <= 3) 649 if has('float') 650 let ff = 2.0 651 assert_false(ff <= 0.0) 652 assert_false(ff <= 1.0) 653 assert_true(ff <= 2.0) 654 assert_true(ff <= 3.0) 655 endif 656enddef 657 658" test =~ comperator 659def Test_expr4_match() 660 assert_equal(false, '2' =~ '0') 661 assert_equal(false, '' 662 =~ '0') 663 assert_equal(true, '2' =~ 664 '[0-9]') 665enddef 666 667" test !~ comperator 668def Test_expr4_nomatch() 669 assert_equal(true, '2' !~ '0') 670 assert_equal(true, '' 671 !~ '0') 672 assert_equal(false, '2' !~ 673 '[0-9]') 674enddef 675 676" test is comperator 677def Test_expr4_is() 678 let mylist = [2] 679 assert_false(mylist is [2]) 680 let other = mylist 681 assert_true(mylist is 682 other) 683 684 let myblob = 0z1234 685 assert_false(myblob 686 is 0z1234) 687 let otherblob = myblob 688 assert_true(myblob is otherblob) 689enddef 690 691" test isnot comperator 692def Test_expr4_isnot() 693 let mylist = [2] 694 assert_true('2' isnot '0') 695 assert_true(mylist isnot [2]) 696 let other = mylist 697 assert_false(mylist isnot 698 other) 699 700 let myblob = 0z1234 701 assert_true(myblob 702 isnot 0z1234) 703 let otherblob = myblob 704 assert_false(myblob isnot otherblob) 705enddef 706 707def RetVoid() 708 let x = 1 709enddef 710 711def Test_expr4_vim9script() 712 # check line continuation 713 let lines =<< trim END 714 vim9script 715 let var = 0 716 < 1 717 assert_equal(true, var) 718 END 719 CheckScriptSuccess(lines) 720 721 lines =<< trim END 722 vim9script 723 let var = 123 724 != 123 725 assert_equal(false, var) 726 END 727 CheckScriptSuccess(lines) 728 729 lines =<< trim END 730 vim9script 731 let var = 123 == 732 123 733 assert_equal(true, var) 734 END 735 CheckScriptSuccess(lines) 736 737 lines =<< trim END 738 vim9script 739 let list = [1, 2, 3] 740 let var = list 741 is list 742 assert_equal(true, var) 743 END 744 CheckScriptSuccess(lines) 745 746 lines =<< trim END 747 vim9script 748 let myblob = 0z1234 749 let var = myblob 750 isnot 0z11 751 assert_equal(true, var) 752 END 753 CheckScriptSuccess(lines) 754 755 # spot check mismatching types 756 lines =<< trim END 757 vim9script 758 echo '' == 0 759 END 760 CheckScriptFailure(lines, 'E1072:') 761 762 lines =<< trim END 763 vim9script 764 echo v:true > v:false 765 END 766 CheckScriptFailure(lines, 'Cannot compare bool with bool') 767 768 lines =<< trim END 769 vim9script 770 echo 123 is 123 771 END 772 CheckScriptFailure(lines, 'Cannot use "is" with number') 773 774 # check 'ignorecase' not being used 775 lines =<< trim END 776 vim9script 777 set ignorecase 778 assert_equal(false, 'abc' == 'ABC') 779 assert_equal(false, 'abc' ==# 'ABC') 780 assert_equal(true, 'abc' ==? 'ABC') 781 782 assert_equal(true, 'abc' != 'ABC') 783 assert_equal(true, 'abc' !=# 'ABC') 784 assert_equal(false, 'abc' !=? 'ABC') 785 786 assert_equal(false, 'abc' =~ 'ABC') 787 assert_equal(false, 'abc' =~# 'ABC') 788 assert_equal(true, 'abc' =~? 'ABC') 789 set noignorecase 790 END 791 CheckScriptSuccess(lines) 792 793 # check missing white space 794 lines =<< trim END 795 vim9script 796 echo 2>3 797 END 798 CheckScriptFailure(lines, 'E1004:') 799 lines =<< trim END 800 vim9script 801 echo 2 >3 802 END 803 CheckScriptFailure(lines, 'E1004:') 804 lines =<< trim END 805 vim9script 806 echo 2> 3 807 END 808 CheckScriptFailure(lines, 'E1004:') 809 lines =<< trim END 810 vim9script 811 echo 2!=3 812 END 813 CheckScriptFailure(lines, 'E1004:') 814 lines =<< trim END 815 vim9script 816 echo 2 !=3 817 END 818 CheckScriptFailure(lines, 'E1004:') 819 lines =<< trim END 820 vim9script 821 echo 2!= 3 822 END 823 CheckScriptFailure(lines, 'E1004:') 824 825 lines =<< trim END 826 vim9script 827 echo len('xxx') == 3 828 END 829 CheckScriptSuccess(lines) 830 831 lines =<< trim END 832 vim9script 833 let line = 'abc' 834 echo line[1] =~ '\w' 835 END 836 CheckScriptSuccess(lines) 837enddef 838 839func Test_expr4_fails() 840 let msg = "white space required before and after '>'" 841 call CheckDefFailure(["let x = 1>2"], msg) 842 call CheckDefFailure(["let x = 1 >2"], msg) 843 call CheckDefFailure(["let x = 1> 2"], msg) 844 845 let msg = "white space required before and after '=='" 846 call CheckDefFailure(["let x = 1==2"], msg) 847 call CheckDefFailure(["let x = 1 ==2"], msg) 848 call CheckDefFailure(["let x = 1== 2"], msg) 849 850 let msg = "white space required before and after 'is'" 851 call CheckDefFailure(["let x = '1'is'2'"], msg) 852 call CheckDefFailure(["let x = '1' is'2'"], msg) 853 call CheckDefFailure(["let x = '1'is '2'"], msg) 854 855 let msg = "white space required before and after 'isnot'" 856 call CheckDefFailure(["let x = '1'isnot'2'"], msg) 857 call CheckDefFailure(["let x = '1' isnot'2'"], msg) 858 call CheckDefFailure(["let x = '1'isnot '2'"], msg) 859 860 call CheckDefFailure(["let x = 1 is# 2"], 'E15:') 861 call CheckDefFailure(["let x = 1 is? 2"], 'E15:') 862 call CheckDefFailure(["let x = 1 isnot# 2"], 'E15:') 863 call CheckDefFailure(["let x = 1 isnot? 2"], 'E15:') 864 865 call CheckDefFailure(["let x = 1 == '2'"], 'Cannot compare number with string') 866 call CheckDefFailure(["let x = '1' == 2"], 'Cannot compare string with number') 867 call CheckDefFailure(["let x = 1 == RetVoid()"], 'Cannot compare number with void') 868 call CheckDefFailure(["let x = RetVoid() == 1"], 'Cannot compare void with number') 869 870 call CheckDefFailure(["let x = true > false"], 'Cannot compare bool with bool') 871 call CheckDefFailure(["let x = true >= false"], 'Cannot compare bool with bool') 872 call CheckDefFailure(["let x = true < false"], 'Cannot compare bool with bool') 873 call CheckDefFailure(["let x = true <= false"], 'Cannot compare bool with bool') 874 call CheckDefFailure(["let x = true =~ false"], 'Cannot compare bool with bool') 875 call CheckDefFailure(["let x = true !~ false"], 'Cannot compare bool with bool') 876 call CheckDefFailure(["let x = true is false"], 'Cannot use "is" with bool') 877 call CheckDefFailure(["let x = true isnot false"], 'Cannot use "isnot" with bool') 878 879 call CheckDefFailure(["let x = v:none is v:null"], 'Cannot use "is" with special') 880 call CheckDefFailure(["let x = v:none isnot v:null"], 'Cannot use "isnot" with special') 881 call CheckDefFailure(["let x = 123 is 123"], 'Cannot use "is" with number') 882 call CheckDefFailure(["let x = 123 isnot 123"], 'Cannot use "isnot" with number') 883 if has('float') 884 call CheckDefFailure(["let x = 1.3 is 1.3"], 'Cannot use "is" with float') 885 call CheckDefFailure(["let x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float') 886 endif 887 888 call CheckDefFailure(["let x = 0za1 > 0z34"], 'Cannot compare blob with blob') 889 call CheckDefFailure(["let x = 0za1 >= 0z34"], 'Cannot compare blob with blob') 890 call CheckDefFailure(["let x = 0za1 < 0z34"], 'Cannot compare blob with blob') 891 call CheckDefFailure(["let x = 0za1 <= 0z34"], 'Cannot compare blob with blob') 892 call CheckDefFailure(["let x = 0za1 =~ 0z34"], 'Cannot compare blob with blob') 893 call CheckDefFailure(["let x = 0za1 !~ 0z34"], 'Cannot compare blob with blob') 894 895 call CheckDefFailure(["let x = [13] > [88]"], 'Cannot compare list with list') 896 call CheckDefFailure(["let x = [13] >= [88]"], 'Cannot compare list with list') 897 call CheckDefFailure(["let x = [13] < [88]"], 'Cannot compare list with list') 898 call CheckDefFailure(["let x = [13] <= [88]"], 'Cannot compare list with list') 899 call CheckDefFailure(["let x = [13] =~ [88]"], 'Cannot compare list with list') 900 call CheckDefFailure(["let x = [13] !~ [88]"], 'Cannot compare list with list') 901 902 call CheckDefFailure(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel') 903 call CheckDefFailure(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list') 904 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') 905 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') 906endfunc 907 908" test addition, subtraction, concatenation 909def Test_expr5() 910 assert_equal(66, 60 + 6) 911 assert_equal(70, 60 + 912 g:anint) 913 assert_equal(9, g:thefour 914 + 5) 915 assert_equal(14, g:thefour + g:anint) 916 assert_equal([1, 2, 3, 4], [1] + g:alist) 917 918 assert_equal(54, 60 - 6) 919 assert_equal(50, 60 - 920 g:anint) 921 assert_equal(-1, g:thefour 922 - 5) 923 assert_equal(-6, g:thefour - g:anint) 924 925 assert_equal('hello', 'hel' .. 'lo') 926 assert_equal('hello 123', 'hello ' .. 927 123) 928 assert_equal('hello 123', 'hello ' 929 .. 123) 930 assert_equal('123 hello', 123 .. ' hello') 931 assert_equal('123456', 123 .. 456) 932 933 assert_equal('av:true', 'a' .. true) 934 assert_equal('av:false', 'a' .. false) 935 assert_equal('av:null', 'a' .. v:null) 936 assert_equal('av:none', 'a' .. v:none) 937 if has('float') 938 assert_equal('a0.123', 'a' .. 0.123) 939 endif 940 941 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) 942 assert_equal(0z11223344, 0z1122 + 0z3344) 943 assert_equal(0z112201ab, 0z1122 944 + g:ablob) 945 assert_equal(0z01ab3344, g:ablob + 0z3344) 946 assert_equal(0z01ab01ab, g:ablob + g:ablob) 947enddef 948 949def Test_expr5_vim9script() 950 # check line continuation 951 let lines =<< trim END 952 vim9script 953 let var = 11 954 + 77 955 - 22 956 assert_equal(66, var) 957 END 958 CheckScriptSuccess(lines) 959 960 lines =<< trim END 961 vim9script 962 let var = 11 + 963 77 - 964 22 965 assert_equal(66, var) 966 END 967 CheckScriptSuccess(lines) 968 969 lines =<< trim END 970 vim9script 971 let var = 'one' 972 .. 'two' 973 assert_equal('onetwo', var) 974 END 975 CheckScriptSuccess(lines) 976 977 lines =<< trim END 978 vim9script 979 echo 'abc' is# 'abc' 980 END 981 CheckScriptFailure(lines, 'E15:') 982 983 lines =<< trim END 984 vim9script 985 echo 'abc' is? 'abc' 986 END 987 CheckScriptFailure(lines, 'E15:') 988 989 lines =<< trim END 990 vim9script 991 echo 'abc' isnot# 'abc' 992 END 993 CheckScriptFailure(lines, 'E15:') 994 995 lines =<< trim END 996 vim9script 997 echo 'abc' isnot? 'abc' 998 END 999 CheckScriptFailure(lines, 'E15:') 1000 1001 # check white space 1002 lines =<< trim END 1003 vim9script 1004 echo 5+6 1005 END 1006 CheckScriptFailure(lines, 'E1004:') 1007 lines =<< trim END 1008 vim9script 1009 echo 5 +6 1010 END 1011 CheckScriptFailure(lines, 'E1004:') 1012 lines =<< trim END 1013 vim9script 1014 echo 5+ 6 1015 END 1016 CheckScriptFailure(lines, 'E1004:') 1017 1018 lines =<< trim END 1019 vim9script 1020 echo 'a'..'b' 1021 END 1022 CheckScriptFailure(lines, 'E1004:') 1023 lines =<< trim END 1024 vim9script 1025 echo 'a' ..'b' 1026 END 1027 CheckScriptFailure(lines, 'E1004:') 1028 lines =<< trim END 1029 vim9script 1030 echo 'a'.. 'b' 1031 END 1032 CheckScriptFailure(lines, 'E1004:') 1033 1034 # check valid string concatenation 1035 lines =<< trim END 1036 vim9script 1037 assert_equal('one123', 'one' .. 123) 1038 assert_equal('onev:true', 'one' .. true) 1039 assert_equal('onev:null', 'one' .. v:null) 1040 assert_equal('onev:none', 'one' .. v:none) 1041 if has('float') 1042 assert_equal('a0.123', 'a' .. 0.123) 1043 endif 1044 END 1045 CheckScriptSuccess(lines) 1046 1047 # check invalid string concatenation 1048 lines =<< trim END 1049 vim9script 1050 echo 'a' .. [1] 1051 END 1052 CheckScriptFailure(lines, 'E730:') 1053 lines =<< trim END 1054 vim9script 1055 echo 'a' .. #{a: 1} 1056 END 1057 CheckScriptFailure(lines, 'E731:') 1058 lines =<< trim END 1059 vim9script 1060 echo 'a' .. test_void() 1061 END 1062 CheckScriptFailure(lines, 'E908:') 1063 lines =<< trim END 1064 vim9script 1065 echo 'a' .. 0z33 1066 END 1067 CheckScriptFailure(lines, 'E976:') 1068 lines =<< trim END 1069 vim9script 1070 echo 'a' .. function('len') 1071 END 1072 CheckScriptFailure(lines, 'E729:') 1073 lines =<< trim END 1074 vim9script 1075 echo 'a' .. test_null_job() 1076 END 1077 CheckScriptFailure(lines, 'E908:') 1078 lines =<< trim END 1079 vim9script 1080 echo 'a' .. test_null_channel() 1081 END 1082 CheckScriptFailure(lines, 'E908:') 1083enddef 1084 1085def Test_expr5_float() 1086 if !has('float') 1087 MissingFeature 'float' 1088 else 1089 assert_equal(66.0, 60.0 + 6.0) 1090 assert_equal(66.0, 60.0 + 6) 1091 assert_equal(66.0, 60 + 1092 6.0) 1093 assert_equal(5.1, g:afloat 1094 + 5) 1095 assert_equal(8.1, 8 + g:afloat) 1096 assert_equal(10.1, g:anint + g:afloat) 1097 assert_equal(10.1, g:afloat + g:anint) 1098 1099 assert_equal(54.0, 60.0 - 6.0) 1100 assert_equal(54.0, 60.0 1101 - 6) 1102 assert_equal(54.0, 60 - 6.0) 1103 assert_equal(-4.9, g:afloat - 5) 1104 assert_equal(7.9, 8 - g:afloat) 1105 assert_equal(9.9, g:anint - g:afloat) 1106 assert_equal(-9.9, g:afloat - g:anint) 1107 endif 1108enddef 1109 1110func Test_expr5_fails() 1111 let msg = "white space required before and after '+'" 1112 call CheckDefFailure(["let x = 1+2"], msg) 1113 call CheckDefFailure(["let x = 1 +2"], msg) 1114 call CheckDefFailure(["let x = 1+ 2"], msg) 1115 1116 let msg = "white space required before and after '-'" 1117 call CheckDefFailure(["let x = 1-2"], msg) 1118 call CheckDefFailure(["let x = 1 -2"], msg) 1119 call CheckDefFailure(["let x = 1- 2"], msg) 1120 1121 let msg = "white space required before and after '..'" 1122 call CheckDefFailure(["let x = '1'..'2'"], msg) 1123 call CheckDefFailure(["let x = '1' ..'2'"], msg) 1124 call CheckDefFailure(["let x = '1'.. '2'"], msg) 1125 1126 call CheckDefFailure(["let x = 0z1122 + 33"], 'E1051') 1127 call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1051') 1128 call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1051') 1129 call CheckDefFailure(["let x = 33 + 0z1122"], 'E1051') 1130 call CheckDefFailure(["let x = [3] + 0z1122"], 'E1051') 1131 call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1051') 1132 call CheckDefFailure(["let x = 6 + xxx"], 'E1001') 1133 1134 call CheckDefFailure(["let x = 'a' .. [1]"], 'E1105') 1135 call CheckDefFailure(["let x = 'a' .. #{a: 1}"], 'E1105') 1136 call CheckDefFailure(["let x = 'a' .. test_void()"], 'E1105') 1137 call CheckDefFailure(["let x = 'a' .. 0z32"], 'E1105') 1138 call CheckDefFailure(["let x = 'a' .. function('len')"], 'E1105') 1139 call CheckDefFailure(["let x = 'a' .. function('len', ['a'])"], 'E1105') 1140 call CheckDefFailure(["let x = 'a' .. test_null_job()"], 'E1105') 1141 call CheckDefFailure(["let x = 'a' .. test_null_channel()"], 'E1105') 1142endfunc 1143 1144" test multiply, divide, modulo 1145def Test_expr6() 1146 assert_equal(36, 6 * 6) 1147 assert_equal(24, 6 * 1148 g:thefour) 1149 assert_equal(24, g:thefour 1150 * 6) 1151 assert_equal(40, g:anint * g:thefour) 1152 1153 assert_equal(10, 60 / 6) 1154 assert_equal(6, 60 / 1155 g:anint) 1156 assert_equal(1, g:anint / 6) 1157 assert_equal(2, g:anint 1158 / g:thefour) 1159 1160 assert_equal(5, 11 % 6) 1161 assert_equal(4, g:anint % 6) 1162 assert_equal(3, 13 % 1163 g:anint) 1164 assert_equal(2, g:anint 1165 % g:thefour) 1166 1167 assert_equal(4, 6 * 4 / 6) 1168 1169 let x = [2] 1170 let y = [3] 1171 assert_equal(5, x[0] + y[0]) 1172 assert_equal(6, x[0] * y[0]) 1173 if has('float') 1174 let xf = [2.0] 1175 let yf = [3.0] 1176 assert_equal(5.0, xf[0] 1177 + yf[0]) 1178 assert_equal(6.0, xf[0] 1179 * yf[0]) 1180 endif 1181 1182 call CheckDefFailure(["let x = 6 * xxx"], 'E1001') 1183enddef 1184 1185def Test_expr6_vim9script() 1186 # check line continuation 1187 let lines =<< trim END 1188 vim9script 1189 let var = 11 1190 * 22 1191 / 3 1192 assert_equal(80, var) 1193 END 1194 CheckScriptSuccess(lines) 1195 1196 lines =<< trim END 1197 vim9script 1198 let var = 25 1199 % 10 1200 assert_equal(5, var) 1201 END 1202 CheckScriptSuccess(lines) 1203 1204 lines =<< trim END 1205 vim9script 1206 let var = 11 * 1207 22 / 1208 3 1209 assert_equal(80, var) 1210 END 1211 CheckScriptSuccess(lines) 1212 1213 # check white space 1214 lines =<< trim END 1215 vim9script 1216 echo 5*6 1217 END 1218 CheckScriptFailure(lines, 'E1004:') 1219 lines =<< trim END 1220 vim9script 1221 echo 5 *6 1222 END 1223 CheckScriptFailure(lines, 'E1004:') 1224 lines =<< trim END 1225 vim9script 1226 echo 5* 6 1227 END 1228 CheckScriptFailure(lines, 'E1004:') 1229enddef 1230 1231def Test_expr6_float() 1232 if !has('float') 1233 MissingFeature 'float' 1234 else 1235 assert_equal(36.0, 6.0 * 6) 1236 assert_equal(36.0, 6 * 1237 6.0) 1238 assert_equal(36.0, 6.0 * 6.0) 1239 assert_equal(1.0, g:afloat * g:anint) 1240 1241 assert_equal(10.0, 60 / 6.0) 1242 assert_equal(10.0, 60.0 / 1243 6) 1244 assert_equal(10.0, 60.0 / 6.0) 1245 assert_equal(0.01, g:afloat / g:anint) 1246 1247 assert_equal(4.0, 6.0 * 4 / 6) 1248 assert_equal(4.0, 6 * 1249 4.0 / 1250 6) 1251 assert_equal(4.0, 6 * 4 / 6.0) 1252 assert_equal(4.0, 6.0 * 4.0 / 6) 1253 assert_equal(4.0, 6 * 4.0 / 6.0) 1254 assert_equal(4.0, 6.0 * 4 / 6.0) 1255 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1256 1257 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1258 endif 1259enddef 1260 1261func Test_expr6_fails() 1262 let msg = "white space required before and after '*'" 1263 call CheckDefFailure(["let x = 1*2"], msg) 1264 call CheckDefFailure(["let x = 1 *2"], msg) 1265 call CheckDefFailure(["let x = 1* 2"], msg) 1266 1267 let msg = "white space required before and after '/'" 1268 call CheckDefFailure(["let x = 1/2"], msg) 1269 call CheckDefFailure(["let x = 1 /2"], msg) 1270 call CheckDefFailure(["let x = 1/ 2"], msg) 1271 1272 let msg = "white space required before and after '%'" 1273 call CheckDefFailure(["let x = 1%2"], msg) 1274 call CheckDefFailure(["let x = 1 %2"], msg) 1275 call CheckDefFailure(["let x = 1% 2"], msg) 1276 1277 call CheckDefFailure(["let x = '1' * '2'"], 'E1036:') 1278 call CheckDefFailure(["let x = '1' / '2'"], 'E1036:') 1279 call CheckDefFailure(["let x = '1' % '2'"], 'E1035:') 1280 1281 call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:') 1282 call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:') 1283 call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:') 1284 1285 call CheckDefFailure(["let x = [1] * [2]"], 'E1036:') 1286 call CheckDefFailure(["let x = [1] / [2]"], 'E1036:') 1287 call CheckDefFailure(["let x = [1] % [2]"], 'E1035:') 1288 1289 call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:') 1290 call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:') 1291 call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:') 1292 1293 call CheckDefFailure(["let x = 0xff[1]"], 'E1107:') 1294 if has('float') 1295 call CheckDefFailure(["let x = 0.7[1]"], 'E1107:') 1296 endif 1297endfunc 1298 1299func Test_expr6_float_fails() 1300 CheckFeature float 1301 call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:') 1302endfunc 1303 1304" define here to use old style parsing 1305if has('float') 1306 let g:float_zero = 0.0 1307 let g:float_neg = -9.8 1308 let g:float_big = 9.9e99 1309endif 1310let g:blob_empty = 0z 1311let g:blob_one = 0z01 1312let g:blob_long = 0z0102.0304 1313 1314let g:string_empty = '' 1315let g:string_short = 'x' 1316let g:string_long = 'abcdefghijklm' 1317let g:string_special = "ab\ncd\ref\ekk" 1318 1319let g:special_true = v:true 1320let g:special_false = v:false 1321let g:special_null = v:null 1322let g:special_none = v:none 1323 1324let g:list_empty = [] 1325let g:list_mixed = [1, 'b', v:false] 1326 1327let g:dict_empty = {} 1328let g:dict_one = #{one: 1} 1329 1330let $TESTVAR = 'testvar' 1331 1332" type casts 1333def Test_expr7t() 1334 let ls: list<string> = ['a', <string>g:string_empty] 1335 let ln: list<number> = [<number>g:anint, <number>g:thefour] 1336 let nr = <number>234 1337 assert_equal(234, nr) 1338 1339 call CheckDefFailure(["let x = <nr>123"], 'E1010:') 1340 call CheckDefFailure(["let x = <number >123"], 'E1068:') 1341 call CheckDefFailure(["let x = <number 123"], 'E1104:') 1342enddef 1343 1344" test low level expression 1345def Test_expr7_number() 1346 # number constant 1347 assert_equal(0, 0) 1348 assert_equal(654, 0654) 1349 1350 assert_equal(6, 0x6) 1351 assert_equal(15, 0xf) 1352 assert_equal(255, 0xff) 1353enddef 1354 1355def Test_expr7_float() 1356 # float constant 1357 if !has('float') 1358 MissingFeature 'float' 1359 else 1360 assert_equal(g:float_zero, .0) 1361 assert_equal(g:float_zero, 0.0) 1362 assert_equal(g:float_neg, -9.8) 1363 assert_equal(g:float_big, 9.9e99) 1364 endif 1365enddef 1366 1367def Test_expr7_blob() 1368 # blob constant 1369 assert_equal(g:blob_empty, 0z) 1370 assert_equal(g:blob_one, 0z01) 1371 assert_equal(g:blob_long, 0z0102.0304) 1372 1373 call CheckDefFailure(["let x = 0z123"], 'E973:') 1374enddef 1375 1376def Test_expr7_string() 1377 # string constant 1378 assert_equal(g:string_empty, '') 1379 assert_equal(g:string_empty, "") 1380 assert_equal(g:string_short, 'x') 1381 assert_equal(g:string_short, "x") 1382 assert_equal(g:string_long, 'abcdefghijklm') 1383 assert_equal(g:string_long, "abcdefghijklm") 1384 assert_equal(g:string_special, "ab\ncd\ref\ekk") 1385 1386 call CheckDefFailure(['let x = "abc'], 'E114:') 1387 call CheckDefFailure(["let x = 'abc"], 'E115:') 1388enddef 1389 1390def Test_expr7_vimvar() 1391 let old: list<string> = v:oldfiles 1392 let compl: dict<any> = v:completed_item 1393 1394 call CheckDefFailure(["let old: list<number> = v:oldfiles"], 'E1012: type mismatch, expected list<number> but got list<string>') 1395 call CheckDefFailure(["let old: dict<number> = v:completed_item"], 'E1012: type mismatch, expected dict<number> but got dict<any>') 1396enddef 1397 1398def Test_expr7_special() 1399 # special constant 1400 assert_equal(g:special_true, true) 1401 assert_equal(g:special_false, false) 1402 assert_equal(g:special_true, v:true) 1403 assert_equal(g:special_false, v:false) 1404 1405 assert_equal(true, !false) 1406 assert_equal(false, !true) 1407 assert_equal(true, !0) 1408 assert_equal(false, !1) 1409 assert_equal(false, !!false) 1410 assert_equal(true, !!true) 1411 assert_equal(false, !!0) 1412 assert_equal(true, !!1) 1413 1414 assert_equal(g:special_null, v:null) 1415 assert_equal(g:special_none, v:none) 1416 1417 call CheckDefFailure(['v:true = true'], 'E46:') 1418 call CheckDefFailure(['v:true = false'], 'E46:') 1419 call CheckDefFailure(['v:false = true'], 'E46:') 1420 call CheckDefFailure(['v:null = 11'], 'E46:') 1421 call CheckDefFailure(['v:none = 22'], 'E46:') 1422enddef 1423 1424def Test_expr7_special_vim9script() 1425 let lines =<< trim END 1426 vim9script 1427 let t = true 1428 let f = false 1429 assert_equal(v:true, true) 1430 assert_equal(true, t) 1431 assert_equal(v:false, false) 1432 assert_equal(false, f) 1433 assert_equal(true, !false) 1434 assert_equal(false, !true) 1435 assert_equal(true, !0) 1436 assert_equal(false, !1) 1437 assert_equal(false, !!false) 1438 assert_equal(true, !!true) 1439 assert_equal(false, !!0) 1440 assert_equal(true, !!1) 1441 END 1442 CheckScriptSuccess(lines) 1443enddef 1444 1445def Test_expr7_list() 1446 # list 1447 assert_equal(g:list_empty, []) 1448 assert_equal(g:list_empty, [ ]) 1449 1450 let numbers: list<number> = [1, 2, 3] 1451 numbers = [1] 1452 numbers = [] 1453 1454 let strings: list<string> = ['a', 'b', 'c'] 1455 strings = ['x'] 1456 strings = [] 1457 1458 let mixed: list<any> = [1, 'b', false,] 1459 assert_equal(g:list_mixed, mixed) 1460 assert_equal('b', mixed[1]) 1461 1462 echo [1, 1463 2] [3, 1464 4] 1465 1466 call CheckDefFailure(["let x = 1234[3]"], 'E1107:') 1467 call CheckDefExecFailure(["let x = g:anint[3]"], 'E1062:') 1468 1469 call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:') 1470 1471 call CheckDefFailure(["let x = [1,2,3]"], 'E1069:') 1472 call CheckDefFailure(["let x = [1 ,2, 3]"], 'E1068:') 1473 1474 call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E1029:') 1475 call CheckDefFailure(["let x = g:list_mixed["], 'E1097:') 1476 call CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:') 1477 call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:') 1478 call CheckDefFailure(["let l: list<number> = [234, 'x']"], 'E1012:') 1479 call CheckDefFailure(["let l: list<number> = ['x', 234]"], 'E1012:') 1480 call CheckDefFailure(["let l: list<string> = [234, 'x']"], 'E1012:') 1481 call CheckDefFailure(["let l: list<string> = ['x', 123]"], 'E1012:') 1482enddef 1483 1484def Test_expr7_list_vim9script() 1485 let lines =<< trim END 1486 vim9script 1487 let l = [ 1488 11, 1489 22, 1490 ] 1491 assert_equal([11, 22], l) 1492 1493 echo [1, 1494 2] [3, 1495 4] 1496 END 1497 CheckScriptSuccess(lines) 1498 1499 lines =<< trim END 1500 vim9script 1501 let l = [11, 1502 22] 1503 assert_equal([11, 22], l) 1504 END 1505 CheckScriptSuccess(lines) 1506 1507 lines =<< trim END 1508 vim9script 1509 let l = [11,22] 1510 END 1511 CheckScriptFailure(lines, 'E1069:') 1512 1513 lines =<< trim END 1514 vim9script 1515 let l = [11 , 22] 1516 END 1517 CheckScriptFailure(lines, 'E1068:') 1518 1519 lines =<< trim END 1520 vim9script 1521 let l: list<number> = [234, 'x'] 1522 END 1523 CheckScriptFailure(lines, 'E1012:') 1524 lines =<< trim END 1525 vim9script 1526 let l: list<number> = ['x', 234] 1527 END 1528 CheckScriptFailure(lines, 'E1012:') 1529 lines =<< trim END 1530 vim9script 1531 let l: list<string> = ['x', 234] 1532 END 1533 CheckScriptFailure(lines, 'E1012:') 1534 lines =<< trim END 1535 vim9script 1536 let l: list<string> = [234, 'x'] 1537 END 1538 CheckScriptFailure(lines, 'E1012:') 1539enddef 1540 1541def LambdaWithComments(): func 1542 return {x -> 1543 # some comment 1544 x == 1 1545 # some comment 1546 || 1547 x == 2 1548 } 1549enddef 1550 1551def LambdaUsingArg(x: number): func 1552 return {-> 1553 # some comment 1554 x == 1 1555 # some comment 1556 || 1557 x == 2 1558 } 1559enddef 1560 1561def Test_expr7_lambda() 1562 let La = { -> 'result'} 1563 assert_equal('result', La()) 1564 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) 1565 1566 # line continuation inside lambda with "cond ? expr : expr" works 1567 let ll = range(3) 1568 map(ll, {k, v -> v % 2 ? { 1569 '111': 111 } : {} 1570 }) 1571 assert_equal([{}, {'111': 111}, {}], ll) 1572 1573 ll = range(3) 1574 map(ll, {k, v -> v == 8 || v 1575 == 9 1576 || v % 2 ? 111 : 222 1577 }) 1578 assert_equal([222, 111, 222], ll) 1579 1580 ll = range(3) 1581 map(ll, {k, v -> v != 8 && v 1582 != 9 1583 && v % 2 == 0 ? 111 : 222 1584 }) 1585 assert_equal([111, 222, 111], ll) 1586 1587 let dl = [{'key': 0}, {'key': 22}]->filter({ _, v -> v['key'] }) 1588 assert_equal([{'key': 22}], dl) 1589 1590 dl = [{'key': 12}, {'foo': 34}] 1591 assert_equal([{'key': 12}], filter(dl, 1592 {_, v -> has_key(v, 'key') ? v['key'] == 12 : 0})) 1593 1594 assert_equal(false, LambdaWithComments()(0)) 1595 assert_equal(true, LambdaWithComments()(1)) 1596 assert_equal(true, LambdaWithComments()(2)) 1597 assert_equal(false, LambdaWithComments()(3)) 1598 1599 assert_equal(false, LambdaUsingArg(0)()) 1600 assert_equal(true, LambdaUsingArg(1)()) 1601 1602 call CheckDefFailure(["filter([1, 2], {k,v -> 1})"], 'E1069:') 1603 call CheckDefFailure(["let L = {a -> a + b}"], 'E1001:') 1604 1605 assert_equal('xxxyyy', 'xxx'->{a, b -> a .. b}('yyy')) 1606 1607 CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x')"], 1608 'E1106: one argument too many') 1609 CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x', 'y')"], 1610 'E1106: 2 arguments too many') 1611 CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:') 1612enddef 1613 1614def Test_expr7_lambda_vim9script() 1615 let lines =<< trim END 1616 vim9script 1617 let v = 10->{a -> 1618 a 1619 + 2 1620 }() 1621 assert_equal(12, v) 1622 END 1623 CheckScriptSuccess(lines) 1624enddef 1625 1626def Test_expr7_dict() 1627 # dictionary 1628 assert_equal(g:dict_empty, {}) 1629 assert_equal(g:dict_empty, { }) 1630 assert_equal(g:dict_one, {'one': 1}) 1631 let key = 'one' 1632 let val = 1 1633 assert_equal(g:dict_one, {key: val}) 1634 1635 let numbers: dict<number> = #{a: 1, b: 2, c: 3} 1636 numbers = #{a: 1} 1637 numbers = #{} 1638 1639 let strings: dict<string> = #{a: 'a', b: 'b', c: 'c'} 1640 strings = #{a: 'x'} 1641 strings = #{} 1642 1643 let mixed: dict<any> = #{a: 'a', b: 42} 1644 mixed = #{a: 'x'} 1645 mixed = #{a: 234} 1646 mixed = #{} 1647 1648 call CheckDefFailure(["let x = #{a:8}"], 'E1069:') 1649 call CheckDefFailure(["let x = #{a : 8}"], 'E1068:') 1650 call CheckDefFailure(["let x = #{a :8}"], 'E1068:') 1651 call CheckDefFailure(["let x = #{a: 8 , b: 9}"], 'E1068:') 1652 1653 call CheckDefFailure(["let x = #{8: 8}"], 'E1014:') 1654 call CheckDefFailure(["let x = #{xxx}"], 'E720:') 1655 call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:') 1656 call CheckDefFailure(["let x = #{xxx: 1,"], 'E723:') 1657 call CheckDefFailure(["let x = {'a': xxx}"], 'E1001:') 1658 call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:') 1659 call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:') 1660 call CheckDefFailure(["let x = #"], 'E1015:') 1661 call CheckDefFailure(["let x += 1"], 'E1020:') 1662 call CheckDefFailure(["let x = x + 1"], 'E1001:') 1663 call CheckDefExecFailure(["let x = g:anint.member"], 'E715:') 1664 call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:') 1665 1666 call CheckDefFailure(['let x: dict<number> = #{a: 234, b: "1"}'], 'E1012:') 1667 call CheckDefFailure(['let x: dict<number> = #{a: "x", b: 134}'], 'E1012:') 1668 call CheckDefFailure(['let x: dict<string> = #{a: 234, b: "1"}'], 'E1012:') 1669 call CheckDefFailure(['let x: dict<string> = #{a: "x", b: 134}'], 'E1012:') 1670enddef 1671 1672def Test_expr7_dict_vim9script() 1673 let lines =<< trim END 1674 vim9script 1675 let d = { 1676 'one': 1677 1, 1678 'two': 2, 1679 } 1680 assert_equal({'one': 1, 'two': 2}, d) 1681 END 1682 CheckScriptSuccess(lines) 1683 1684 lines =<< trim END 1685 vim9script 1686 let d = { "one": "one", "two": "two", } 1687 assert_equal({'one': 'one', 'two': 'two'}, d) 1688 END 1689 CheckScriptSuccess(lines) 1690 1691 lines =<< trim END 1692 vim9script 1693 let d = #{one: 1, 1694 two: 2, 1695 } 1696 assert_equal({'one': 1, 'two': 2}, d) 1697 END 1698 CheckScriptSuccess(lines) 1699 1700 lines =<< trim END 1701 vim9script 1702 let d = #{one:1, two: 2} 1703 END 1704 CheckScriptFailure(lines, 'E1069:') 1705 1706 lines =<< trim END 1707 vim9script 1708 let d = #{one: 1,two: 2} 1709 END 1710 CheckScriptFailure(lines, 'E1069:') 1711 1712 lines =<< trim END 1713 vim9script 1714 let d = #{one : 1} 1715 END 1716 CheckScriptFailure(lines, 'E1068:') 1717 1718 lines =<< trim END 1719 vim9script 1720 let d = #{one:1} 1721 END 1722 CheckScriptFailure(lines, 'E1069:') 1723 1724 lines =<< trim END 1725 vim9script 1726 let d = #{one: 1 , two: 2} 1727 END 1728 CheckScriptFailure(lines, 'E1068:') 1729 1730 lines =<< trim END 1731 vim9script 1732 let l: dict<number> = #{a: 234, b: 'x'} 1733 END 1734 CheckScriptFailure(lines, 'E1012:') 1735 lines =<< trim END 1736 vim9script 1737 let l: dict<number> = #{a: 'x', b: 234} 1738 END 1739 CheckScriptFailure(lines, 'E1012:') 1740 lines =<< trim END 1741 vim9script 1742 let l: dict<string> = #{a: 'x', b: 234} 1743 END 1744 CheckScriptFailure(lines, 'E1012:') 1745 lines =<< trim END 1746 vim9script 1747 let l: dict<string> = #{a: 234, b: 'x'} 1748 END 1749 CheckScriptFailure(lines, 'E1012:') 1750enddef 1751 1752let g:oneString = 'one' 1753 1754def Test_expr_member() 1755 assert_equal(1, g:dict_one.one) 1756 let d: dict<number> = g:dict_one 1757 assert_equal(1, d['one']) 1758 assert_equal(1, d[ 1759 'one' 1760 ]) 1761 assert_equal(1, d 1762 .one) 1763 d = {'1': 1, '_': 2} 1764 assert_equal(1, d 1765 .1) 1766 assert_equal(2, d 1767 ._) 1768 1769 # getting the one member should clear the dict after getting the item 1770 assert_equal('one', #{one: 'one'}.one) 1771 assert_equal('one', #{one: 'one'}[g:oneString]) 1772 1773 call CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:') 1774 call CheckDefExecFailure(["let d: dict<any>", "echo d['a']"], 'E716:') 1775 call CheckDefExecFailure(["let d: dict<number>", "d = g:list_empty"], 'E1029: Expected dict but got list') 1776enddef 1777 1778def Test_expr7_any_index_slice() 1779 let lines =<< trim END 1780 # getting the one member should clear the list only after getting the item 1781 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 1782 1783 # string is permissive, index out of range accepted 1784 g:teststring = 'abcdef' 1785 assert_equal('b', g:teststring[1]) 1786 assert_equal('', g:teststring[-1]) 1787 assert_equal('', g:teststring[99]) 1788 1789 assert_equal('b', g:teststring[1:1]) 1790 assert_equal('bcdef', g:teststring[1:]) 1791 assert_equal('abcd', g:teststring[:3]) 1792 assert_equal('cdef', g:teststring[-4:]) 1793 assert_equal('abcdef', g:teststring[-9:]) 1794 assert_equal('abcd', g:teststring[:-3]) 1795 assert_equal('', g:teststring[:-9]) 1796 1797 # blob index cannot be out of range 1798 g:testblob = 0z01ab 1799 assert_equal(0x01, g:testblob[0]) 1800 assert_equal(0xab, g:testblob[1]) 1801 assert_equal(0xab, g:testblob[-1]) 1802 assert_equal(0x01, g:testblob[-2]) 1803 1804 # blob slice accepts out of range 1805 assert_equal(0z01ab, g:testblob[0:1]) 1806 assert_equal(0z01, g:testblob[0:0]) 1807 assert_equal(0z01, g:testblob[-2:-2]) 1808 assert_equal(0zab, g:testblob[1:1]) 1809 assert_equal(0zab, g:testblob[-1:-1]) 1810 assert_equal(0z, g:testblob[2:2]) 1811 assert_equal(0z, g:testblob[0:-3]) 1812 1813 # list index cannot be out of range 1814 g:testlist = [0, 1, 2, 3] 1815 assert_equal(0, g:testlist[0]) 1816 assert_equal(1, g:testlist[1]) 1817 assert_equal(3, g:testlist[3]) 1818 assert_equal(3, g:testlist[-1]) 1819 assert_equal(0, g:testlist[-4]) 1820 assert_equal(1, g:testlist[g:theone]) 1821 1822 # list slice accepts out of range 1823 assert_equal([0], g:testlist[0:0]) 1824 assert_equal([3], g:testlist[3:3]) 1825 assert_equal([0, 1], g:testlist[0:1]) 1826 assert_equal([0, 1, 2, 3], g:testlist[0:3]) 1827 assert_equal([0, 1, 2, 3], g:testlist[0:9]) 1828 assert_equal([], g:testlist[-1:1]) 1829 assert_equal([1], g:testlist[-3:1]) 1830 assert_equal([0, 1], g:testlist[-4:1]) 1831 assert_equal([0, 1], g:testlist[-9:1]) 1832 assert_equal([1, 2, 3], g:testlist[1:-1]) 1833 assert_equal([1], g:testlist[1:-3]) 1834 assert_equal([], g:testlist[1:-4]) 1835 assert_equal([], g:testlist[1:-9]) 1836 1837 g:testdict = #{a: 1, b: 2} 1838 assert_equal(1, g:testdict['a']) 1839 assert_equal(2, g:testdict['b']) 1840 END 1841 1842 CheckDefSuccess(lines) 1843 CheckScriptSuccess(['vim9script'] + lines) 1844 1845 CheckDefExecFailure(['echo g:testblob[2]'], 'E979:') 1846 CheckScriptFailure(['vim9script', 'echo g:testblob[2]'], 'E979:') 1847 CheckDefExecFailure(['echo g:testblob[-3]'], 'E979:') 1848 CheckScriptFailure(['vim9script', 'echo g:testblob[-3]'], 'E979:') 1849 1850 CheckDefExecFailure(['echo g:testlist[4]'], 'E684:') 1851 CheckScriptFailure(['vim9script', 'echo g:testlist[4]'], 'E684:') 1852 CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:') 1853 CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684:') 1854 1855 CheckDefExecFailure(['echo g:testdict["a":"b"]'], 'E719:') 1856 CheckScriptFailure(['vim9script', 'echo g:testdict["a":"b"]'], 'E719:') 1857 CheckDefExecFailure(['echo g:testdict[1]'], 'E716:') 1858 CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:') 1859 1860 unlet g:teststring 1861 unlet g:testblob 1862 unlet g:testlist 1863enddef 1864 1865def Test_expr_member_vim9script() 1866 let lines =<< trim END 1867 vim9script 1868 let d = #{one: 1869 'one', 1870 two: 'two', 1871 1: 1, 1872 _: 2} 1873 assert_equal('one', d.one) 1874 assert_equal('one', d 1875 .one) 1876 assert_equal(1, d 1877 .1) 1878 assert_equal(2, d 1879 ._) 1880 assert_equal('one', d[ 1881 'one' 1882 ]) 1883 END 1884 CheckScriptSuccess(lines) 1885 1886 lines =<< trim END 1887 vim9script 1888 let l = [1, 1889 2, 1890 3, 4 1891 ] 1892 assert_equal(2, l[ 1893 1 1894 ]) 1895 assert_equal([2, 3], l[1 : 2]) 1896 assert_equal([1, 2, 3], l[ 1897 : 1898 2 1899 ]) 1900 assert_equal([3, 4], l[ 1901 2 1902 : 1903 ]) 1904 END 1905 CheckScriptSuccess(lines) 1906enddef 1907 1908def Test_expr7_option() 1909 # option 1910 set ts=11 1911 assert_equal(11, &ts) 1912 &ts = 9 1913 assert_equal(9, &ts) 1914 set ts=8 1915 set grepprg=some\ text 1916 assert_equal('some text', &grepprg) 1917 &grepprg = test_null_string() 1918 assert_equal('', &grepprg) 1919 set grepprg& 1920enddef 1921 1922def Test_expr7_environment() 1923 # environment variable 1924 assert_equal('testvar', $TESTVAR) 1925 assert_equal('', $ASDF_ASD_XXX) 1926 1927 call CheckDefFailure(["let x = $$$"], 'E1002:') 1928enddef 1929 1930def Test_expr7_register() 1931 @a = 'register a' 1932 assert_equal('register a', @a) 1933 1934 let fname = expand('%') 1935 assert_equal(fname, @%) 1936 1937 feedkeys(":echo 'some'\<CR>", "xt") 1938 assert_equal("echo 'some'", @:) 1939 1940 normal axyz 1941 assert_equal("xyz", @.) 1942 call CheckDefFailure(["@. = 'yes'"], 'E354:') 1943 1944 @/ = 'slash' 1945 assert_equal('slash', @/) 1946 1947 @= = 'equal' 1948 assert_equal('equal', @=) 1949enddef 1950 1951def Test_expr7_namespace() 1952 g:some_var = 'some' 1953 assert_equal('some', get(g:, 'some_var')) 1954 assert_equal('some', get(g:, 'some_var', 'xxx')) 1955 assert_equal('xxx', get(g:, 'no_var', 'xxx')) 1956 unlet g:some_var 1957 1958 b:some_var = 'some' 1959 assert_equal('some', get(b:, 'some_var')) 1960 assert_equal('some', get(b:, 'some_var', 'xxx')) 1961 assert_equal('xxx', get(b:, 'no_var', 'xxx')) 1962 unlet b:some_var 1963 1964 w:some_var = 'some' 1965 assert_equal('some', get(w:, 'some_var')) 1966 assert_equal('some', get(w:, 'some_var', 'xxx')) 1967 assert_equal('xxx', get(w:, 'no_var', 'xxx')) 1968 unlet w:some_var 1969 1970 t:some_var = 'some' 1971 assert_equal('some', get(t:, 'some_var')) 1972 assert_equal('some', get(t:, 'some_var', 'xxx')) 1973 assert_equal('xxx', get(t:, 'no_var', 'xxx')) 1974 unlet t:some_var 1975enddef 1976 1977def Test_expr7_parens() 1978 # (expr) 1979 assert_equal(4, (6 * 4) / 6) 1980 assert_equal(0, 6 * ( 4 / 6 )) 1981 1982 assert_equal(6, +6) 1983 assert_equal(-6, -6) 1984 assert_equal(6, --6) 1985 assert_equal(6, -+-6) 1986 assert_equal(-6, ---6) 1987 assert_equal(false, !-3) 1988 assert_equal(true, !+-+0) 1989enddef 1990 1991def Test_expr7_parens_vim9script() 1992 let lines =<< trim END 1993 vim9script 1994 let s = ( 1995 'one' 1996 .. 1997 'two' 1998 ) 1999 assert_equal('onetwo', s) 2000 END 2001 CheckScriptSuccess(lines) 2002enddef 2003 2004def Test_expr7_negate() 2005 assert_equal(-99, -99) 2006 assert_equal(99, --99) 2007 let nr = 88 2008 assert_equal(-88, -nr) 2009 assert_equal(88, --nr) 2010enddef 2011 2012def Echo(arg: any): string 2013 return arg 2014enddef 2015 2016def s:Echo4Arg(arg: any): string 2017 return arg 2018enddef 2019 2020def Test_expr7_call() 2021 assert_equal('yes', 'yes'->Echo()) 2022 assert_equal('yes', 'yes' 2023 ->s:Echo4Arg()) 2024 assert_equal(true, !range(5)->empty()) 2025 assert_equal([0, 1, 2], --3->range()) 2026 2027 call CheckDefFailure(["let x = 'yes'->Echo"], 'E107:') 2028 call CheckScriptFailure([ 2029 "vim9script", 2030 "let x = substitute ('x', 'x', 'x', 'x')" 2031 ], 'E121:') 2032 2033 let auto_lines =<< trim END 2034 def g:some#func(): string 2035 return 'found' 2036 enddef 2037 END 2038 mkdir('Xruntime/autoload', 'p') 2039 writefile(auto_lines, 'Xruntime/autoload/some.vim') 2040 let save_rtp = &rtp 2041 &rtp = getcwd() .. '/Xruntime,' .. &rtp 2042 assert_equal('found', g:some#func()) 2043 assert_equal('found', some#func()) 2044 2045 &rtp = save_rtp 2046 delete('Xruntime', 'rf') 2047enddef 2048 2049 2050def Test_expr7_not() 2051 let lines =<< trim END 2052 assert_equal(true, !'') 2053 assert_equal(true, ![]) 2054 assert_equal(false, !'asdf') 2055 assert_equal(false, ![2]) 2056 assert_equal(true, !!'asdf') 2057 assert_equal(true, !![2]) 2058 2059 assert_equal(true, !test_null_partial()) 2060 assert_equal(false, !{-> 'yes'}) 2061 2062 assert_equal(true, !test_null_dict()) 2063 assert_equal(true, !{}) 2064 assert_equal(false, !{'yes': 'no'}) 2065 2066 if has('channel') 2067 assert_equal(true, !test_null_job()) 2068 assert_equal(true, !test_null_channel()) 2069 endif 2070 2071 assert_equal(true, !test_null_blob()) 2072 assert_equal(true, !0z) 2073 assert_equal(false, !0z01) 2074 2075 assert_equal(true, !test_void()) 2076 assert_equal(true, !test_unknown()) 2077 2078 assert_equal(false, ![1, 2, 3]->reverse()) 2079 assert_equal(true, ![]->reverse()) 2080 END 2081 CheckDefSuccess(lines) 2082 CheckScriptSuccess(['vim9script'] + lines) 2083enddef 2084 2085func Test_expr7_fails() 2086 call CheckDefFailure(["let x = (12"], "E110:") 2087 2088 call CheckDefFailure(["let x = -'xx'"], "E1030:") 2089 call CheckDefFailure(["let x = +'xx'"], "E1030:") 2090 call CheckDefFailure(["let x = -0z12"], "E974:") 2091 call CheckDefExecFailure(["let x = -[8]"], "E39:") 2092 call CheckDefExecFailure(["let x = -{'a': 1}"], "E39:") 2093 2094 call CheckDefFailure(["let x = @"], "E1002:") 2095 call CheckDefFailure(["let x = @<"], "E354:") 2096 2097 call CheckDefFailure(["let x = [1, 2"], "E697:") 2098 call CheckDefFailure(["let x = [notfound]"], "E1001:") 2099 2100 call CheckDefFailure(["let x = { -> 123) }"], "E451:") 2101 call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:") 2102 2103 call CheckDefFailure(["let x = ¬exist"], 'E113:') 2104 call CheckDefFailure(["&grepprg = [343]"], 'E1012:') 2105 2106 call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:') 2107 call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:') 2108 2109 call CheckDefFailure(["echo a:somevar"], 'E1075:') 2110 call CheckDefFailure(["echo l:somevar"], 'E1075:') 2111 call CheckDefFailure(["echo x:somevar"], 'E1075:') 2112 2113 call CheckDefExecFailure(["let x = +g:astring"], 'E1030:') 2114 call CheckDefExecFailure(["let x = +g:ablob"], 'E974:') 2115 call CheckDefExecFailure(["let x = +g:alist"], 'E745:') 2116 call CheckDefExecFailure(["let x = +g:adict"], 'E728:') 2117 2118 call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:') 2119 2120 call CheckDefFailure(["'yes'->", "Echo()"], 'E488: Trailing characters: ->') 2121 2122 call CheckDefExecFailure(["[1, 2->len()"], 'E697:') 2123 call CheckDefExecFailure(["#{a: 1->len()"], 'E488:') 2124 call CheckDefExecFailure(["{'a': 1->len()"], 'E723:') 2125endfunc 2126 2127let g:Funcrefs = [function('add')] 2128 2129func CallMe(arg) 2130 return a:arg 2131endfunc 2132 2133func CallMe2(one, two) 2134 return a:one .. a:two 2135endfunc 2136 2137def Test_expr7_trailing() 2138 # user function call 2139 assert_equal(123, g:CallMe(123)) 2140 assert_equal(123, g:CallMe( 123)) 2141 assert_equal(123, g:CallMe(123 )) 2142 assert_equal('yesno', g:CallMe2('yes', 'no')) 2143 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 2144 assert_equal('nothing', g:CallMe('nothing')) 2145 2146 # partial call 2147 let Part = function('g:CallMe') 2148 assert_equal('yes', Part('yes')) 2149 2150 # funcref call, using list index 2151 let l = [] 2152 g:Funcrefs[0](l, 2) 2153 assert_equal([2], l) 2154 2155 # method call 2156 l = [2, 5, 6] 2157 l->map({k, v -> k + v}) 2158 assert_equal([2, 6, 8], l) 2159 2160 # lambda method call 2161 l = [2, 5] 2162 l->{l -> add(l, 8)}() 2163 assert_equal([2, 5, 8], l) 2164 2165 # dict member 2166 let d = #{key: 123} 2167 assert_equal(123, d.key) 2168enddef 2169 2170def Test_expr7_string_subscript() 2171 let lines =<< trim END 2172 let text = 'abcdef' 2173 assert_equal('', text[-1]) 2174 assert_equal('a', text[0]) 2175 assert_equal('e', text[4]) 2176 assert_equal('f', text[5]) 2177 assert_equal('', text[6]) 2178 2179 text = 'ábçdëf' 2180 assert_equal('', text[-999]) 2181 assert_equal('', text[-1]) 2182 assert_equal('á', text[0]) 2183 assert_equal('b', text[1]) 2184 assert_equal('ç', text[2]) 2185 assert_equal('d', text[3]) 2186 assert_equal('ë', text[4]) 2187 assert_equal('f', text[5]) 2188 assert_equal('', text[6]) 2189 assert_equal('', text[999]) 2190 2191 assert_equal('ábçdëf', text[0:-1]) 2192 assert_equal('ábçdëf', text[0 :-1]) 2193 assert_equal('ábçdëf', text[0: -1]) 2194 assert_equal('ábçdëf', text[0 : -1]) 2195 assert_equal('ábçdëf', text[0 2196 :-1]) 2197 assert_equal('ábçdëf', text[0: 2198 -1]) 2199 assert_equal('ábçdëf', text[0 : -1 2200 ]) 2201 assert_equal('bçdëf', text[1:-1]) 2202 assert_equal('çdëf', text[2:-1]) 2203 assert_equal('dëf', text[3:-1]) 2204 assert_equal('ëf', text[4:-1]) 2205 assert_equal('f', text[5:-1]) 2206 assert_equal('', text[6:-1]) 2207 assert_equal('', text[999:-1]) 2208 2209 assert_equal('ábçd', text[:3]) 2210 assert_equal('bçdëf', text[1:]) 2211 assert_equal('ábçdëf', text[:]) 2212 END 2213 CheckDefSuccess(lines) 2214 CheckScriptSuccess(['vim9script'] + lines) 2215enddef 2216 2217def Test_expr7_list_subscript() 2218 let lines =<< trim END 2219 let list = [0, 1, 2, 3, 4] 2220 assert_equal(0, list[0]) 2221 assert_equal(4, list[4]) 2222 assert_equal(4, list[-1]) 2223 assert_equal(0, list[-5]) 2224 2225 assert_equal([0, 1, 2, 3, 4], list[0:4]) 2226 assert_equal([0, 1, 2, 3, 4], list[:]) 2227 assert_equal([1, 2, 3, 4], list[1:]) 2228 assert_equal([2, 3, 4], list[2:-1]) 2229 assert_equal([4], list[4:-1]) 2230 assert_equal([], list[5:-1]) 2231 assert_equal([], list[999:-1]) 2232 assert_equal([1, 2, 3, 4], list[g:theone:g:thefour]) 2233 2234 assert_equal([0, 1, 2, 3], list[0:3]) 2235 assert_equal([0], list[0:0]) 2236 assert_equal([0, 1, 2, 3, 4], list[0:-1]) 2237 assert_equal([0, 1, 2], list[0:-3]) 2238 assert_equal([0], list[0:-5]) 2239 assert_equal([], list[0:-6]) 2240 assert_equal([], list[0:-99]) 2241 END 2242 CheckDefSuccess(lines) 2243 CheckScriptSuccess(['vim9script'] + lines) 2244 2245 lines = ['let l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 2246 CheckDefExecFailure(lines, 'E1029:') 2247 CheckScriptFailure(['vim9script'] + lines, 'E1030:') 2248enddef 2249 2250def Test_expr7_subscript_linebreak() 2251 let range = range( 2252 3) 2253 let l = range 2254 ->map('string(v:key)') 2255 assert_equal(['0', '1', '2'], l) 2256 2257 l = range 2258 ->map('string(v:key)') 2259 assert_equal(['0', '1', '2'], l) 2260 2261 l = range # comment 2262 ->map('string(v:key)') 2263 assert_equal(['0', '1', '2'], l) 2264 2265 l = range 2266 2267 ->map('string(v:key)') 2268 assert_equal(['0', '1', '2'], l) 2269 2270 l = range 2271 # comment 2272 ->map('string(v:key)') 2273 assert_equal(['0', '1', '2'], l) 2274 2275 assert_equal('1', l[ 2276 1]) 2277 2278 let d = #{one: 33} 2279 assert_equal(33, d. 2280 one) 2281enddef 2282 2283def Test_expr7_method_call() 2284 new 2285 setline(1, ['first', 'last']) 2286 'second'->append(1) 2287 "third"->append(2) 2288 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$')) 2289 bwipe! 2290 2291 let bufnr = bufnr() 2292 let loclist = [#{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 2293 loclist->setloclist(0) 2294 assert_equal([#{bufnr: bufnr, 2295 lnum: 42, 2296 col: 17, 2297 text: 'wrong', 2298 pattern: '', 2299 valid: 1, 2300 vcol: 0, 2301 nr: 0, 2302 type: '', 2303 module: ''} 2304 ], getloclist(0)) 2305enddef 2306 2307func Test_expr7_trailing_fails() 2308 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107') 2309 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274') 2310endfunc 2311 2312func Test_expr_fails() 2313 call CheckDefFailure(["let x = '1'is2"], 'E488:') 2314 call CheckDefFailure(["let x = '1'isnot2"], 'E488:') 2315 2316 call CheckDefFailure(["CallMe ('yes')"], 'E476:') 2317 call CheckDefFailure(["CallMe2('yes','no')"], 'E1069:') 2318 call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:') 2319 2320 call CheckDefFailure(["v:nosuch += 3"], 'E1001:') 2321 call CheckDefFailure(["let v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:') 2322 call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:') 2323 2324 call CheckDefFailure(["echo len('asdf'"], 'E110:') 2325 call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:') 2326 call CheckDefFailure(["echo doesnotexist()"], 'E117:') 2327endfunc 2328 2329" vim: shiftwidth=2 sts=2 expandtab 2330