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:', 2) 97 lines =<< trim END 98 vim9script 99 let var = v:true? 1 : 2 100 END 101 CheckScriptFailure(lines, 'E1004:', 2) 102 lines =<< trim END 103 vim9script 104 let var = v:true ?1 : 2 105 END 106 CheckScriptFailure(lines, 'E1004:', 2) 107 lines =<< trim END 108 vim9script 109 let var = v:true ? 1: 2 110 END 111 CheckScriptFailure(lines, 'E1004:', 2) 112 lines =<< trim END 113 vim9script 114 let var = v:true ? 1 :2 115 END 116 CheckScriptFailure(lines, 'E1004:', 2) 117enddef 118 119func Test_expr1_fails() 120 call CheckDefFailure(["let x = 1 ? 'one'"], "Missing ':' after '?'", 1) 121 call CheckDefFailure(["let x = 1 ? 'one' : xxx"], "E1001:", 1) 122 123 let msg = "white space required before and after '?'" 124 call CheckDefFailure(["let x = 1? 'one' : 'two'"], msg, 1) 125 call CheckDefFailure(["let x = 1 ?'one' : 'two'"], msg, 1) 126 call CheckDefFailure(["let x = 1?'one' : 'two'"], msg, 1) 127 128 let msg = "white space required before and after ':'" 129 call CheckDefFailure(["let x = 1 ? 'one': 'two'"], msg, 1) 130 call CheckDefFailure(["let x = 1 ? 'one' :'two'"], msg, 1) 131 call CheckDefFailure(["let x = 1 ? 'one':'two'"], msg, 1) 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:', 4) 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:', 2) 211 lines =<< trim END 212 vim9script 213 let var = v:true ||v:true 214 END 215 CheckScriptFailure(lines, 'E1004:', 2) 216 lines =<< trim END 217 vim9script 218 let var = v:true|| v:true 219 END 220 CheckScriptFailure(lines, 'E1004:', 2) 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, 1) 258 call CheckDefFailure(["let x = 1 ||2"], msg, 1) 259 call CheckDefFailure(["let x = 1|| 2"], msg, 1) 260 261 call CheckDefFailure(["let x = 1 || xxx"], 'E1001:', 1) 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:', 2) 332 lines =<< trim END 333 vim9script 334 let var = v:true &&v:true 335 END 336 CheckScriptFailure(lines, 'E1004:', 2) 337 lines =<< trim END 338 vim9script 339 let var = v:true&& v:true 340 END 341 CheckScriptFailure(lines, 'E1004:', 2) 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, 1) 383 call CheckDefFailure(["let x = 1 &&2"], msg, 1) 384 call CheckDefFailure(["let x = 1&& 2"], msg, 1) 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:', 1) 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:', 2) 761 762 lines =<< trim END 763 vim9script 764 echo v:true > v:false 765 END 766 CheckScriptFailure(lines, 'Cannot compare bool with bool', 2) 767 768 lines =<< trim END 769 vim9script 770 echo 123 is 123 771 END 772 CheckScriptFailure(lines, 'Cannot use "is" with number', 2) 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:', 2) 799 lines =<< trim END 800 vim9script 801 echo 2 >3 802 END 803 CheckScriptFailure(lines, 'E1004:', 2) 804 lines =<< trim END 805 vim9script 806 echo 2> 3 807 END 808 CheckScriptFailure(lines, 'E1004:', 2) 809 lines =<< trim END 810 vim9script 811 echo 2!=3 812 END 813 CheckScriptFailure(lines, 'E1004:', 2) 814 lines =<< trim END 815 vim9script 816 echo 2 !=3 817 END 818 CheckScriptFailure(lines, 'E1004:', 2) 819 lines =<< trim END 820 vim9script 821 echo 2!= 3 822 END 823 CheckScriptFailure(lines, 'E1004:', 2) 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, 1) 842 call CheckDefFailure(["let x = 1 >2"], msg, 1) 843 call CheckDefFailure(["let x = 1> 2"], msg, 1) 844 845 let msg = "white space required before and after '=='" 846 call CheckDefFailure(["let x = 1==2"], msg, 1) 847 call CheckDefFailure(["let x = 1 ==2"], msg, 1) 848 call CheckDefFailure(["let x = 1== 2"], msg, 1) 849 850 let msg = "white space required before and after 'is'" 851 call CheckDefFailure(["let x = '1'is'2'"], msg, 1) 852 call CheckDefFailure(["let x = '1' is'2'"], msg, 1) 853 call CheckDefFailure(["let x = '1'is '2'"], msg, 1) 854 855 let msg = "white space required before and after 'isnot'" 856 call CheckDefFailure(["let x = '1'isnot'2'"], msg, 1) 857 call CheckDefFailure(["let x = '1' isnot'2'"], msg, 1) 858 call CheckDefFailure(["let x = '1'isnot '2'"], msg, 1) 859 860 call CheckDefFailure(["let x = 1 is# 2"], 'E15:', 1) 861 call CheckDefFailure(["let x = 1 is? 2"], 'E15:', 1) 862 call CheckDefFailure(["let x = 1 isnot# 2"], 'E15:', 1) 863 call CheckDefFailure(["let x = 1 isnot? 2"], 'E15:', 1) 864 865 call CheckDefFailure(["let x = 1 == '2'"], 'Cannot compare number with string', 1) 866 call CheckDefFailure(["let x = '1' == 2"], 'Cannot compare string with number', 1) 867 call CheckDefFailure(["let x = 1 == RetVoid()"], 'Cannot compare number with void', 1) 868 call CheckDefFailure(["let x = RetVoid() == 1"], 'Cannot compare void with number', 1) 869 870 call CheckDefFailure(["let x = true > false"], 'Cannot compare bool with bool', 1) 871 call CheckDefFailure(["let x = true >= false"], 'Cannot compare bool with bool', 1) 872 call CheckDefFailure(["let x = true < false"], 'Cannot compare bool with bool', 1) 873 call CheckDefFailure(["let x = true <= false"], 'Cannot compare bool with bool', 1) 874 call CheckDefFailure(["let x = true =~ false"], 'Cannot compare bool with bool', 1) 875 call CheckDefFailure(["let x = true !~ false"], 'Cannot compare bool with bool', 1) 876 call CheckDefFailure(["let x = true is false"], 'Cannot use "is" with bool', 1) 877 call CheckDefFailure(["let x = true isnot false"], 'Cannot use "isnot" with bool', 1) 878 879 call CheckDefFailure(["let x = v:none is v:null"], 'Cannot use "is" with special', 1) 880 call CheckDefFailure(["let x = v:none isnot v:null"], 'Cannot use "isnot" with special', 1) 881 call CheckDefFailure(["let x = 123 is 123"], 'Cannot use "is" with number', 1) 882 call CheckDefFailure(["let x = 123 isnot 123"], 'Cannot use "isnot" with number', 1) 883 if has('float') 884 call CheckDefFailure(["let x = 1.3 is 1.3"], 'Cannot use "is" with float', 1) 885 call CheckDefFailure(["let x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float', 1) 886 endif 887 888 call CheckDefFailure(["let x = 0za1 > 0z34"], 'Cannot compare blob with blob', 1) 889 call CheckDefFailure(["let x = 0za1 >= 0z34"], 'Cannot compare blob with blob', 1) 890 call CheckDefFailure(["let x = 0za1 < 0z34"], 'Cannot compare blob with blob', 1) 891 call CheckDefFailure(["let x = 0za1 <= 0z34"], 'Cannot compare blob with blob', 1) 892 call CheckDefFailure(["let x = 0za1 =~ 0z34"], 'Cannot compare blob with blob', 1) 893 call CheckDefFailure(["let x = 0za1 !~ 0z34"], 'Cannot compare blob with blob', 1) 894 895 call CheckDefFailure(["let x = [13] > [88]"], 'Cannot compare list with list', 1) 896 call CheckDefFailure(["let x = [13] >= [88]"], 'Cannot compare list with list', 1) 897 call CheckDefFailure(["let x = [13] < [88]"], 'Cannot compare list with list', 1) 898 call CheckDefFailure(["let x = [13] <= [88]"], 'Cannot compare list with list', 1) 899 call CheckDefFailure(["let x = [13] =~ [88]"], 'Cannot compare list with list', 1) 900 call CheckDefFailure(["let x = [13] !~ [88]"], 'Cannot compare list with list', 1) 901 902 call CheckDefFailure(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel', 3) 903 call CheckDefFailure(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list', 3) 904 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func', 3) 905 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func', 3) 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:', 2) 982 983 lines =<< trim END 984 vim9script 985 echo 'abc' is? 'abc' 986 END 987 CheckScriptFailure(lines, 'E15:', 2) 988 989 lines =<< trim END 990 vim9script 991 echo 'abc' isnot# 'abc' 992 END 993 CheckScriptFailure(lines, 'E15:', 2) 994 995 lines =<< trim END 996 vim9script 997 echo 'abc' isnot? 'abc' 998 END 999 CheckScriptFailure(lines, 'E15:', 2) 1000 1001 # check white space 1002 lines =<< trim END 1003 vim9script 1004 echo 5+6 1005 END 1006 CheckScriptFailure(lines, 'E1004:', 2) 1007 lines =<< trim END 1008 vim9script 1009 echo 5 +6 1010 END 1011 CheckScriptFailure(lines, 'E1004:', 2) 1012 lines =<< trim END 1013 vim9script 1014 echo 5+ 6 1015 END 1016 CheckScriptFailure(lines, 'E1004:', 2) 1017 1018 lines =<< trim END 1019 vim9script 1020 echo 'a'..'b' 1021 END 1022 CheckScriptFailure(lines, 'E1004:', 2) 1023 lines =<< trim END 1024 vim9script 1025 echo 'a' ..'b' 1026 END 1027 CheckScriptFailure(lines, 'E1004:', 2) 1028 lines =<< trim END 1029 vim9script 1030 echo 'a'.. 'b' 1031 END 1032 CheckScriptFailure(lines, 'E1004:', 2) 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:', 2) 1053 lines =<< trim END 1054 vim9script 1055 echo 'a' .. #{a: 1} 1056 END 1057 CheckScriptFailure(lines, 'E731:', 2) 1058 lines =<< trim END 1059 vim9script 1060 echo 'a' .. test_void() 1061 END 1062 CheckScriptFailure(lines, 'E908:', 2) 1063 lines =<< trim END 1064 vim9script 1065 echo 'a' .. 0z33 1066 END 1067 CheckScriptFailure(lines, 'E976:', 2) 1068 lines =<< trim END 1069 vim9script 1070 echo 'a' .. function('len') 1071 END 1072 CheckScriptFailure(lines, 'E729:', 2) 1073 lines =<< trim END 1074 vim9script 1075 echo 'a' .. test_null_job() 1076 END 1077 CheckScriptFailure(lines, 'E908:', 2) 1078 lines =<< trim END 1079 vim9script 1080 echo 'a' .. test_null_channel() 1081 END 1082 CheckScriptFailure(lines, 'E908:', 2) 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, 1) 1113 call CheckDefFailure(["let x = 1 +2"], msg, 1) 1114 call CheckDefFailure(["let x = 1+ 2"], msg, 1) 1115 1116 let msg = "white space required before and after '-'" 1117 call CheckDefFailure(["let x = 1-2"], msg, 1) 1118 call CheckDefFailure(["let x = 1 -2"], msg, 1) 1119 call CheckDefFailure(["let x = 1- 2"], msg, 1) 1120 1121 let msg = "white space required before and after '..'" 1122 call CheckDefFailure(["let x = '1'..'2'"], msg, 1) 1123 call CheckDefFailure(["let x = '1' ..'2'"], msg, 1) 1124 call CheckDefFailure(["let x = '1'.. '2'"], msg, 1) 1125 1126 call CheckDefFailure(["let x = 0z1122 + 33"], 'E1051', 1) 1127 call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1051', 1) 1128 call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1051', 1) 1129 call CheckDefFailure(["let x = 33 + 0z1122"], 'E1051', 1) 1130 call CheckDefFailure(["let x = [3] + 0z1122"], 'E1051', 1) 1131 call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1051', 1) 1132 call CheckDefFailure(["let x = 6 + xxx"], 'E1001', 1) 1133 1134 call CheckDefFailure(["let x = 'a' .. [1]"], 'E1105', 1) 1135 call CheckDefFailure(["let x = 'a' .. #{a: 1}"], 'E1105', 1) 1136 call CheckDefFailure(["let x = 'a' .. test_void()"], 'E1105', 1) 1137 call CheckDefFailure(["let x = 'a' .. 0z32"], 'E1105', 1) 1138 call CheckDefFailure(["let x = 'a' .. function('len')"], 'E1105', 1) 1139 call CheckDefFailure(["let x = 'a' .. function('len', ['a'])"], 'E1105', 1) 1140 call CheckDefFailure(["let x = 'a' .. test_null_job()"], 'E1105', 1) 1141 call CheckDefFailure(["let x = 'a' .. test_null_channel()"], 'E1105', 1) 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', 1) 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:', 2) 1219 lines =<< trim END 1220 vim9script 1221 echo 5 *6 1222 END 1223 CheckScriptFailure(lines, 'E1004:', 2) 1224 lines =<< trim END 1225 vim9script 1226 echo 5* 6 1227 END 1228 CheckScriptFailure(lines, 'E1004:', 2) 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, 1) 1264 call CheckDefFailure(["let x = 1 *2"], msg, 1) 1265 call CheckDefFailure(["let x = 1* 2"], msg, 1) 1266 1267 let msg = "white space required before and after '/'" 1268 call CheckDefFailure(["let x = 1/2"], msg, 1) 1269 call CheckDefFailure(["let x = 1 /2"], msg, 1) 1270 call CheckDefFailure(["let x = 1/ 2"], msg, 1) 1271 1272 let msg = "white space required before and after '%'" 1273 call CheckDefFailure(["let x = 1%2"], msg, 1) 1274 call CheckDefFailure(["let x = 1 %2"], msg, 1) 1275 call CheckDefFailure(["let x = 1% 2"], msg, 1) 1276 1277 call CheckDefFailure(["let x = '1' * '2'"], 'E1036:', 1) 1278 call CheckDefFailure(["let x = '1' / '2'"], 'E1036:', 1) 1279 call CheckDefFailure(["let x = '1' % '2'"], 'E1035:', 1) 1280 1281 call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:', 1) 1282 call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:', 1) 1283 call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:', 1) 1284 1285 call CheckDefFailure(["let x = [1] * [2]"], 'E1036:', 1) 1286 call CheckDefFailure(["let x = [1] / [2]"], 'E1036:', 1) 1287 call CheckDefFailure(["let x = [1] % [2]"], 'E1035:', 1) 1288 1289 call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:', 1) 1290 call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:', 1) 1291 call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:', 1) 1292 1293 call CheckDefFailure(["let x = 0xff[1]"], 'E1107:', 1) 1294 if has('float') 1295 call CheckDefFailure(["let x = 0.7[1]"], 'E1107:', 1) 1296 endif 1297endfunc 1298 1299func Test_expr6_float_fails() 1300 CheckFeature float 1301 call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:', 1) 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:', 1) 1340 call CheckDefFailure(["let x = <number >123"], 'E1068:', 1) 1341 call CheckDefFailure(["let x = <number 123"], 'E1104:', 1) 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:', 1) 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:', 1) 1387 call CheckDefFailure(["let x = 'abc"], 'E115:', 1) 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>', 1) 1395 call CheckDefFailure(["let old: dict<number> = v:completed_item"], 'E1012: type mismatch, expected dict<number> but got dict<any>', 1) 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:', 1) 1418 call CheckDefFailure(['v:true = false'], 'E46:', 1) 1419 call CheckDefFailure(['v:false = true'], 'E46:', 1) 1420 call CheckDefFailure(['v:null = 11'], 'E46:', 1) 1421 call CheckDefFailure(['v:none = 22'], 'E46:', 1) 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:', 1) 1467 call CheckDefExecFailure(["let x = g:anint[3]"], 'E1062:', 1) 1468 1469 call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:', 1) 1470 1471 call CheckDefFailure(["let x = [1,2,3]"], 'E1069:', 1) 1472 call CheckDefFailure(["let x = [1 ,2, 3]"], 'E1068:', 1) 1473 1474 call CheckDefExecFailure(["echo 1", "let x = [][0]", "echo 3"], 'E684:', 2) 1475 1476 call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E1029:', 1) 1477 call CheckDefFailure(["let x = g:list_mixed["], 'E1097:', 2) 1478 call CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:', 2) 1479 call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:', 1) 1480 call CheckDefFailure(["let l: list<number> = [234, 'x']"], 'E1012:', 1) 1481 call CheckDefFailure(["let l: list<number> = ['x', 234]"], 'E1012:', 1) 1482 call CheckDefFailure(["let l: list<string> = [234, 'x']"], 'E1012:', 1) 1483 call CheckDefFailure(["let l: list<string> = ['x', 123]"], 'E1012:', 1) 1484enddef 1485 1486def Test_expr7_list_vim9script() 1487 let lines =<< trim END 1488 vim9script 1489 let l = [ 1490 11, 1491 22, 1492 ] 1493 assert_equal([11, 22], l) 1494 1495 echo [1, 1496 2] [3, 1497 4] 1498 END 1499 CheckScriptSuccess(lines) 1500 1501 lines =<< trim END 1502 vim9script 1503 let l = [11, 1504 22] 1505 assert_equal([11, 22], l) 1506 END 1507 CheckScriptSuccess(lines) 1508 1509 lines =<< trim END 1510 vim9script 1511 let l = [11,22] 1512 END 1513 CheckScriptFailure(lines, 'E1069:', 2) 1514 1515 lines =<< trim END 1516 vim9script 1517 let l = [11 , 22] 1518 END 1519 CheckScriptFailure(lines, 'E1068:', 2) 1520 1521 lines =<< trim END 1522 vim9script 1523 let l: list<number> = [234, 'x'] 1524 END 1525 CheckScriptFailure(lines, 'E1012:', 2) 1526 lines =<< trim END 1527 vim9script 1528 let l: list<number> = ['x', 234] 1529 END 1530 CheckScriptFailure(lines, 'E1012:', 2) 1531 lines =<< trim END 1532 vim9script 1533 let l: list<string> = ['x', 234] 1534 END 1535 CheckScriptFailure(lines, 'E1012:', 2) 1536 lines =<< trim END 1537 vim9script 1538 let l: list<string> = [234, 'x'] 1539 END 1540 CheckScriptFailure(lines, 'E1012:', 2) 1541enddef 1542 1543def LambdaWithComments(): func 1544 return {x -> 1545 # some comment 1546 x == 1 1547 # some comment 1548 || 1549 x == 2 1550 } 1551enddef 1552 1553def LambdaUsingArg(x: number): func 1554 return {-> 1555 # some comment 1556 x == 1 1557 # some comment 1558 || 1559 x == 2 1560 } 1561enddef 1562 1563def Test_expr7_lambda() 1564 let La = { -> 'result'} 1565 assert_equal('result', La()) 1566 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) 1567 1568 # line continuation inside lambda with "cond ? expr : expr" works 1569 let ll = range(3) 1570 map(ll, {k, v -> v % 2 ? { 1571 '111': 111 } : {} 1572 }) 1573 assert_equal([{}, {'111': 111}, {}], ll) 1574 1575 ll = range(3) 1576 map(ll, {k, v -> v == 8 || v 1577 == 9 1578 || v % 2 ? 111 : 222 1579 }) 1580 assert_equal([222, 111, 222], ll) 1581 1582 ll = range(3) 1583 map(ll, {k, v -> v != 8 && v 1584 != 9 1585 && v % 2 == 0 ? 111 : 222 1586 }) 1587 assert_equal([111, 222, 111], ll) 1588 1589 let dl = [{'key': 0}, {'key': 22}]->filter({ _, v -> v['key'] }) 1590 assert_equal([{'key': 22}], dl) 1591 1592 dl = [{'key': 12}, {'foo': 34}] 1593 assert_equal([{'key': 12}], filter(dl, 1594 {_, v -> has_key(v, 'key') ? v['key'] == 12 : 0})) 1595 1596 assert_equal(false, LambdaWithComments()(0)) 1597 assert_equal(true, LambdaWithComments()(1)) 1598 assert_equal(true, LambdaWithComments()(2)) 1599 assert_equal(false, LambdaWithComments()(3)) 1600 1601 assert_equal(false, LambdaUsingArg(0)()) 1602 assert_equal(true, LambdaUsingArg(1)()) 1603 1604 call CheckDefFailure(["filter([1, 2], {k,v -> 1})"], 'E1069:', 1) 1605 # error is in first line of the lambda 1606 call CheckDefFailure(["let L = {a -> a + b}"], 'E1001:', 0) 1607 1608 assert_equal('xxxyyy', 'xxx'->{a, b -> a .. b}('yyy')) 1609 1610 CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x')"], 1611 'E1106: one argument too many') 1612 CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x', 'y')"], 1613 'E1106: 2 arguments too many') 1614 CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1) 1615enddef 1616 1617def Test_expr7_lambda_vim9script() 1618 let lines =<< trim END 1619 vim9script 1620 let v = 10->{a -> 1621 a 1622 + 2 1623 }() 1624 assert_equal(12, v) 1625 END 1626 CheckScriptSuccess(lines) 1627 1628 # nested lambda with line breaks 1629 lines =<< trim END 1630 vim9script 1631 search('"', 'cW', 0, 0, {-> 1632 synstack('.', col('.')) 1633 ->map({_, v -> synIDattr(v, 'name')})->len()}) 1634 END 1635 CheckScriptSuccess(lines) 1636enddef 1637 1638def Test_expr7_dict() 1639 # dictionary 1640 assert_equal(g:dict_empty, {}) 1641 assert_equal(g:dict_empty, { }) 1642 assert_equal(g:dict_one, {'one': 1}) 1643 let key = 'one' 1644 let val = 1 1645 assert_equal(g:dict_one, {key: val}) 1646 1647 let numbers: dict<number> = #{a: 1, b: 2, c: 3} 1648 numbers = #{a: 1} 1649 numbers = #{} 1650 1651 let strings: dict<string> = #{a: 'a', b: 'b', c: 'c'} 1652 strings = #{a: 'x'} 1653 strings = #{} 1654 1655 let mixed: dict<any> = #{a: 'a', b: 42} 1656 mixed = #{a: 'x'} 1657 mixed = #{a: 234} 1658 mixed = #{} 1659 1660 call CheckDefFailure(["let x = #{a:8}"], 'E1069:', 1) 1661 call CheckDefFailure(["let x = #{a : 8}"], 'E1068:', 1) 1662 call CheckDefFailure(["let x = #{a :8}"], 'E1068:', 1) 1663 call CheckDefFailure(["let x = #{a: 8 , b: 9}"], 'E1068:', 1) 1664 1665 call CheckDefFailure(["let x = #{8: 8}"], 'E1014:', 1) 1666 call CheckDefFailure(["let x = #{xxx}"], 'E720:', 1) 1667 call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:', 2) 1668 call CheckDefFailure(["let x = #{xxx: 1,"], 'E723:', 2) 1669 call CheckDefFailure(["let x = {'a': xxx}"], 'E1001:', 1) 1670 call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:', 1) 1671 call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:', 1) 1672 call CheckDefFailure(["let x = #"], 'E1015:', 1) 1673 call CheckDefFailure(["let x += 1"], 'E1020:', 1) 1674 call CheckDefFailure(["let x = x + 1"], 'E1001:', 1) 1675 call CheckDefExecFailure(["let x = g:anint.member"], 'E715:', 1) 1676 call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:', 1) 1677 1678 call CheckDefFailure(['let x: dict<number> = #{a: 234, b: "1"}'], 'E1012:', 1) 1679 call CheckDefFailure(['let x: dict<number> = #{a: "x", b: 134}'], 'E1012:', 1) 1680 call CheckDefFailure(['let x: dict<string> = #{a: 234, b: "1"}'], 'E1012:', 1) 1681 call CheckDefFailure(['let x: dict<string> = #{a: "x", b: 134}'], 'E1012:', 1) 1682enddef 1683 1684def Test_expr7_dict_vim9script() 1685 let lines =<< trim END 1686 vim9script 1687 let d = { 1688 'one': 1689 1, 1690 'two': 2, 1691 } 1692 assert_equal({'one': 1, 'two': 2}, d) 1693 END 1694 CheckScriptSuccess(lines) 1695 1696 lines =<< trim END 1697 vim9script 1698 let d = { "one": "one", "two": "two", } 1699 assert_equal({'one': 'one', 'two': 'two'}, d) 1700 END 1701 CheckScriptSuccess(lines) 1702 1703 lines =<< trim END 1704 vim9script 1705 let d = #{one: 1, 1706 two: 2, 1707 } 1708 assert_equal({'one': 1, 'two': 2}, d) 1709 END 1710 CheckScriptSuccess(lines) 1711 1712 lines =<< trim END 1713 vim9script 1714 let d = #{one:1, two: 2} 1715 END 1716 CheckScriptFailure(lines, 'E1069:', 2) 1717 1718 lines =<< trim END 1719 vim9script 1720 let d = #{one: 1,two: 2} 1721 END 1722 CheckScriptFailure(lines, 'E1069:', 2) 1723 1724 lines =<< trim END 1725 vim9script 1726 let d = #{one : 1} 1727 END 1728 CheckScriptFailure(lines, 'E1068:', 2) 1729 1730 lines =<< trim END 1731 vim9script 1732 let d = #{one:1} 1733 END 1734 CheckScriptFailure(lines, 'E1069:', 2) 1735 1736 lines =<< trim END 1737 vim9script 1738 let d = #{one: 1 , two: 2} 1739 END 1740 CheckScriptFailure(lines, 'E1068:', 2) 1741 1742 lines =<< trim END 1743 vim9script 1744 let l: dict<number> = #{a: 234, b: 'x'} 1745 END 1746 CheckScriptFailure(lines, 'E1012:', 2) 1747 lines =<< trim END 1748 vim9script 1749 let l: dict<number> = #{a: 'x', b: 234} 1750 END 1751 CheckScriptFailure(lines, 'E1012:', 2) 1752 lines =<< trim END 1753 vim9script 1754 let l: dict<string> = #{a: 'x', b: 234} 1755 END 1756 CheckScriptFailure(lines, 'E1012:', 2) 1757 lines =<< trim END 1758 vim9script 1759 let l: dict<string> = #{a: 234, b: 'x'} 1760 END 1761 CheckScriptFailure(lines, 'E1012:', 2) 1762enddef 1763 1764let g:oneString = 'one' 1765 1766def Test_expr_member() 1767 assert_equal(1, g:dict_one.one) 1768 let d: dict<number> = g:dict_one 1769 assert_equal(1, d['one']) 1770 assert_equal(1, d[ 1771 'one' 1772 ]) 1773 assert_equal(1, d 1774 .one) 1775 d = {'1': 1, '_': 2} 1776 assert_equal(1, d 1777 .1) 1778 assert_equal(2, d 1779 ._) 1780 1781 # getting the one member should clear the dict after getting the item 1782 assert_equal('one', #{one: 'one'}.one) 1783 assert_equal('one', #{one: 'one'}[g:oneString]) 1784 1785 call CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:', 1) 1786 call CheckDefExecFailure(["let d: dict<any>", "echo d['a']"], 'E716:', 2) 1787 call CheckDefExecFailure(["let d: dict<number>", "d = g:list_empty"], 'E1029: Expected dict but got list', 2) 1788enddef 1789 1790def Test_expr7_any_index_slice() 1791 let lines =<< trim END 1792 # getting the one member should clear the list only after getting the item 1793 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 1794 1795 # string is permissive, index out of range accepted 1796 g:teststring = 'abcdef' 1797 assert_equal('b', g:teststring[1]) 1798 assert_equal('', g:teststring[-1]) 1799 assert_equal('', g:teststring[99]) 1800 1801 assert_equal('b', g:teststring[1:1]) 1802 assert_equal('bcdef', g:teststring[1:]) 1803 assert_equal('abcd', g:teststring[:3]) 1804 assert_equal('cdef', g:teststring[-4:]) 1805 assert_equal('abcdef', g:teststring[-9:]) 1806 assert_equal('abcd', g:teststring[:-3]) 1807 assert_equal('', g:teststring[:-9]) 1808 1809 # blob index cannot be out of range 1810 g:testblob = 0z01ab 1811 assert_equal(0x01, g:testblob[0]) 1812 assert_equal(0xab, g:testblob[1]) 1813 assert_equal(0xab, g:testblob[-1]) 1814 assert_equal(0x01, g:testblob[-2]) 1815 1816 # blob slice accepts out of range 1817 assert_equal(0z01ab, g:testblob[0:1]) 1818 assert_equal(0z01, g:testblob[0:0]) 1819 assert_equal(0z01, g:testblob[-2:-2]) 1820 assert_equal(0zab, g:testblob[1:1]) 1821 assert_equal(0zab, g:testblob[-1:-1]) 1822 assert_equal(0z, g:testblob[2:2]) 1823 assert_equal(0z, g:testblob[0:-3]) 1824 1825 # list index cannot be out of range 1826 g:testlist = [0, 1, 2, 3] 1827 assert_equal(0, g:testlist[0]) 1828 assert_equal(1, g:testlist[1]) 1829 assert_equal(3, g:testlist[3]) 1830 assert_equal(3, g:testlist[-1]) 1831 assert_equal(0, g:testlist[-4]) 1832 assert_equal(1, g:testlist[g:theone]) 1833 1834 # list slice accepts out of range 1835 assert_equal([0], g:testlist[0:0]) 1836 assert_equal([3], g:testlist[3:3]) 1837 assert_equal([0, 1], g:testlist[0:1]) 1838 assert_equal([0, 1, 2, 3], g:testlist[0:3]) 1839 assert_equal([0, 1, 2, 3], g:testlist[0:9]) 1840 assert_equal([], g:testlist[-1:1]) 1841 assert_equal([1], g:testlist[-3:1]) 1842 assert_equal([0, 1], g:testlist[-4:1]) 1843 assert_equal([0, 1], g:testlist[-9:1]) 1844 assert_equal([1, 2, 3], g:testlist[1:-1]) 1845 assert_equal([1], g:testlist[1:-3]) 1846 assert_equal([], g:testlist[1:-4]) 1847 assert_equal([], g:testlist[1:-9]) 1848 1849 g:testdict = #{a: 1, b: 2} 1850 assert_equal(1, g:testdict['a']) 1851 assert_equal(2, g:testdict['b']) 1852 END 1853 1854 CheckDefSuccess(lines) 1855 CheckScriptSuccess(['vim9script'] + lines) 1856 1857 CheckDefExecFailure(['echo g:testblob[2]'], 'E979:', 1) 1858 CheckScriptFailure(['vim9script', 'echo g:testblob[2]'], 'E979:', 2) 1859 CheckDefExecFailure(['echo g:testblob[-3]'], 'E979:', 1) 1860 CheckScriptFailure(['vim9script', 'echo g:testblob[-3]'], 'E979:', 2) 1861 1862 CheckDefExecFailure(['echo g:testlist[4]'], 'E684:', 1) 1863 CheckScriptFailure(['vim9script', 'echo g:testlist[4]'], 'E684:', 2) 1864 CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1) 1865 CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684:', 2) 1866 1867 CheckDefExecFailure(['echo g:testdict["a":"b"]'], 'E719:', 1) 1868 CheckScriptFailure(['vim9script', 'echo g:testdict["a":"b"]'], 'E719:', 2) 1869 CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1) 1870 CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2) 1871 1872 unlet g:teststring 1873 unlet g:testblob 1874 unlet g:testlist 1875enddef 1876 1877def Test_expr_member_vim9script() 1878 let lines =<< trim END 1879 vim9script 1880 let d = #{one: 1881 'one', 1882 two: 'two', 1883 1: 1, 1884 _: 2} 1885 assert_equal('one', d.one) 1886 assert_equal('one', d 1887 .one) 1888 assert_equal(1, d 1889 .1) 1890 assert_equal(2, d 1891 ._) 1892 assert_equal('one', d[ 1893 'one' 1894 ]) 1895 END 1896 CheckScriptSuccess(lines) 1897 1898 lines =<< trim END 1899 vim9script 1900 let l = [1, 1901 2, 1902 3, 4 1903 ] 1904 assert_equal(2, l[ 1905 1 1906 ]) 1907 assert_equal([2, 3], l[1 : 2]) 1908 assert_equal([1, 2, 3], l[ 1909 : 1910 2 1911 ]) 1912 assert_equal([3, 4], l[ 1913 2 1914 : 1915 ]) 1916 END 1917 CheckScriptSuccess(lines) 1918enddef 1919 1920def Test_expr7_option() 1921 # option 1922 set ts=11 1923 assert_equal(11, &ts) 1924 &ts = 9 1925 assert_equal(9, &ts) 1926 set ts=8 1927 set grepprg=some\ text 1928 assert_equal('some text', &grepprg) 1929 &grepprg = test_null_string() 1930 assert_equal('', &grepprg) 1931 set grepprg& 1932enddef 1933 1934def Test_expr7_environment() 1935 # environment variable 1936 assert_equal('testvar', $TESTVAR) 1937 assert_equal('', $ASDF_ASD_XXX) 1938 1939 call CheckDefFailure(["let x = $$$"], 'E1002:', 1) 1940enddef 1941 1942def Test_expr7_register() 1943 @a = 'register a' 1944 assert_equal('register a', @a) 1945 1946 let fname = expand('%') 1947 assert_equal(fname, @%) 1948 1949 feedkeys(":echo 'some'\<CR>", "xt") 1950 assert_equal("echo 'some'", @:) 1951 1952 normal axyz 1953 assert_equal("xyz", @.) 1954 call CheckDefFailure(["@. = 'yes'"], 'E354:', 1) 1955 1956 @/ = 'slash' 1957 assert_equal('slash', @/) 1958 1959 @= = 'equal' 1960 assert_equal('equal', @=) 1961enddef 1962 1963def Test_expr7_namespace() 1964 g:some_var = 'some' 1965 assert_equal('some', get(g:, 'some_var')) 1966 assert_equal('some', get(g:, 'some_var', 'xxx')) 1967 assert_equal('xxx', get(g:, 'no_var', 'xxx')) 1968 unlet g:some_var 1969 1970 b:some_var = 'some' 1971 assert_equal('some', get(b:, 'some_var')) 1972 assert_equal('some', get(b:, 'some_var', 'xxx')) 1973 assert_equal('xxx', get(b:, 'no_var', 'xxx')) 1974 unlet b:some_var 1975 1976 w:some_var = 'some' 1977 assert_equal('some', get(w:, 'some_var')) 1978 assert_equal('some', get(w:, 'some_var', 'xxx')) 1979 assert_equal('xxx', get(w:, 'no_var', 'xxx')) 1980 unlet w:some_var 1981 1982 t:some_var = 'some' 1983 assert_equal('some', get(t:, 'some_var')) 1984 assert_equal('some', get(t:, 'some_var', 'xxx')) 1985 assert_equal('xxx', get(t:, 'no_var', 'xxx')) 1986 unlet t:some_var 1987enddef 1988 1989def Test_expr7_parens() 1990 # (expr) 1991 assert_equal(4, (6 * 4) / 6) 1992 assert_equal(0, 6 * ( 4 / 6 )) 1993 1994 assert_equal(6, +6) 1995 assert_equal(-6, -6) 1996 assert_equal(6, --6) 1997 assert_equal(6, -+-6) 1998 assert_equal(-6, ---6) 1999 assert_equal(false, !-3) 2000 assert_equal(true, !+-+0) 2001enddef 2002 2003def Test_expr7_parens_vim9script() 2004 let lines =<< trim END 2005 vim9script 2006 let s = ( 2007 'one' 2008 .. 2009 'two' 2010 ) 2011 assert_equal('onetwo', s) 2012 END 2013 CheckScriptSuccess(lines) 2014enddef 2015 2016def Test_expr7_negate() 2017 assert_equal(-99, -99) 2018 assert_equal(99, --99) 2019 let nr = 88 2020 assert_equal(-88, -nr) 2021 assert_equal(88, --nr) 2022enddef 2023 2024def Echo(arg: any): string 2025 return arg 2026enddef 2027 2028def s:Echo4Arg(arg: any): string 2029 return arg 2030enddef 2031 2032def Test_expr7_call() 2033 assert_equal('yes', 'yes'->Echo()) 2034 assert_equal('yes', 'yes' 2035 ->s:Echo4Arg()) 2036 assert_equal(true, !range(5)->empty()) 2037 assert_equal([0, 1, 2], --3->range()) 2038 2039 call CheckDefFailure(["let x = 'yes'->Echo"], 'E107:', 1) 2040 call CheckScriptFailure([ 2041 "vim9script", 2042 "let x = substitute ('x', 'x', 'x', 'x')" 2043 ], 'E121:', 2) 2044 2045 let auto_lines =<< trim END 2046 def g:some#func(): string 2047 return 'found' 2048 enddef 2049 END 2050 mkdir('Xruntime/autoload', 'p') 2051 writefile(auto_lines, 'Xruntime/autoload/some.vim') 2052 let save_rtp = &rtp 2053 &rtp = getcwd() .. '/Xruntime,' .. &rtp 2054 assert_equal('found', g:some#func()) 2055 assert_equal('found', some#func()) 2056 2057 &rtp = save_rtp 2058 delete('Xruntime', 'rf') 2059enddef 2060 2061 2062def Test_expr7_not() 2063 let lines =<< trim END 2064 assert_equal(true, !'') 2065 assert_equal(true, ![]) 2066 assert_equal(false, !'asdf') 2067 assert_equal(false, ![2]) 2068 assert_equal(true, !!'asdf') 2069 assert_equal(true, !![2]) 2070 2071 assert_equal(true, !test_null_partial()) 2072 assert_equal(false, !{-> 'yes'}) 2073 2074 assert_equal(true, !test_null_dict()) 2075 assert_equal(true, !{}) 2076 assert_equal(false, !{'yes': 'no'}) 2077 2078 if has('channel') 2079 assert_equal(true, !test_null_job()) 2080 assert_equal(true, !test_null_channel()) 2081 endif 2082 2083 assert_equal(true, !test_null_blob()) 2084 assert_equal(true, !0z) 2085 assert_equal(false, !0z01) 2086 2087 assert_equal(true, !test_void()) 2088 assert_equal(true, !test_unknown()) 2089 2090 assert_equal(false, ![1, 2, 3]->reverse()) 2091 assert_equal(true, ![]->reverse()) 2092 END 2093 CheckDefSuccess(lines) 2094 CheckScriptSuccess(['vim9script'] + lines) 2095enddef 2096 2097func Test_expr7_fails() 2098 call CheckDefFailure(["let x = (12"], "E110:", 1) 2099 2100 call CheckDefFailure(["let x = -'xx'"], "E1030:", 1) 2101 call CheckDefFailure(["let x = +'xx'"], "E1030:", 1) 2102 call CheckDefFailure(["let x = -0z12"], "E974:", 1) 2103 call CheckDefExecFailure(["let x = -[8]"], "E39:", 1) 2104 call CheckDefExecFailure(["let x = -{'a': 1}"], "E39:", 1) 2105 2106 call CheckDefFailure(["let x = @"], "E1002:", 1) 2107 call CheckDefFailure(["let x = @<"], "E354:", 1) 2108 2109 call CheckDefFailure(["let x = [1, 2"], "E697:", 2) 2110 call CheckDefFailure(["let x = [notfound]"], "E1001:", 1) 2111 2112 call CheckDefFailure(["let x = { -> 123) }"], "E451:", 1) 2113 call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:", 1) 2114 2115 call CheckDefFailure(["let x = ¬exist"], 'E113:', 1) 2116 call CheckDefFailure(["&grepprg = [343]"], 'E1012:', 1) 2117 2118 call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:', 1) 2119 call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:', 1) 2120 2121 call CheckDefFailure(["echo a:somevar"], 'E1075:', 1) 2122 call CheckDefFailure(["echo l:somevar"], 'E1075:', 1) 2123 call CheckDefFailure(["echo x:somevar"], 'E1075:', 1) 2124 2125 call CheckDefExecFailure(["let x = +g:astring"], 'E1030:', 1) 2126 call CheckDefExecFailure(["let x = +g:ablob"], 'E974:', 1) 2127 call CheckDefExecFailure(["let x = +g:alist"], 'E745:', 1) 2128 call CheckDefExecFailure(["let x = +g:adict"], 'E728:', 1) 2129 2130 call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:', 2) 2131 2132 call CheckDefFailure(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 1) 2133 2134 call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2) 2135 call CheckDefExecFailure(["#{a: 1->len()"], 'E488:', 1) 2136 call CheckDefExecFailure(["{'a': 1->len()"], 'E723:', 2) 2137endfunc 2138 2139let g:Funcrefs = [function('add')] 2140 2141func CallMe(arg) 2142 return a:arg 2143endfunc 2144 2145func CallMe2(one, two) 2146 return a:one .. a:two 2147endfunc 2148 2149def Test_expr7_trailing() 2150 # user function call 2151 assert_equal(123, g:CallMe(123)) 2152 assert_equal(123, g:CallMe( 123)) 2153 assert_equal(123, g:CallMe(123 )) 2154 assert_equal('yesno', g:CallMe2('yes', 'no')) 2155 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 2156 assert_equal('nothing', g:CallMe('nothing')) 2157 2158 # partial call 2159 let Part = function('g:CallMe') 2160 assert_equal('yes', Part('yes')) 2161 2162 # funcref call, using list index 2163 let l = [] 2164 g:Funcrefs[0](l, 2) 2165 assert_equal([2], l) 2166 2167 # method call 2168 l = [2, 5, 6] 2169 l->map({k, v -> k + v}) 2170 assert_equal([2, 6, 8], l) 2171 2172 # lambda method call 2173 l = [2, 5] 2174 l->{l -> add(l, 8)}() 2175 assert_equal([2, 5, 8], l) 2176 2177 # dict member 2178 let d = #{key: 123} 2179 assert_equal(123, d.key) 2180enddef 2181 2182def Test_expr7_string_subscript() 2183 let lines =<< trim END 2184 let text = 'abcdef' 2185 assert_equal('', text[-1]) 2186 assert_equal('a', text[0]) 2187 assert_equal('e', text[4]) 2188 assert_equal('f', text[5]) 2189 assert_equal('', text[6]) 2190 2191 text = 'ábçdëf' 2192 assert_equal('', text[-999]) 2193 assert_equal('', text[-1]) 2194 assert_equal('á', text[0]) 2195 assert_equal('b', text[1]) 2196 assert_equal('ç', text[2]) 2197 assert_equal('d', text[3]) 2198 assert_equal('ë', text[4]) 2199 assert_equal('f', text[5]) 2200 assert_equal('', text[6]) 2201 assert_equal('', text[999]) 2202 2203 assert_equal('ábçdëf', text[0:-1]) 2204 assert_equal('ábçdëf', text[0 :-1]) 2205 assert_equal('ábçdëf', text[0: -1]) 2206 assert_equal('ábçdëf', text[0 : -1]) 2207 assert_equal('ábçdëf', text[0 2208 :-1]) 2209 assert_equal('ábçdëf', text[0: 2210 -1]) 2211 assert_equal('ábçdëf', text[0 : -1 2212 ]) 2213 assert_equal('bçdëf', text[1:-1]) 2214 assert_equal('çdëf', text[2:-1]) 2215 assert_equal('dëf', text[3:-1]) 2216 assert_equal('ëf', text[4:-1]) 2217 assert_equal('f', text[5:-1]) 2218 assert_equal('', text[6:-1]) 2219 assert_equal('', text[999:-1]) 2220 2221 assert_equal('ábçd', text[:3]) 2222 assert_equal('bçdëf', text[1:]) 2223 assert_equal('ábçdëf', text[:]) 2224 END 2225 CheckDefSuccess(lines) 2226 CheckScriptSuccess(['vim9script'] + lines) 2227enddef 2228 2229def Test_expr7_list_subscript() 2230 let lines =<< trim END 2231 let list = [0, 1, 2, 3, 4] 2232 assert_equal(0, list[0]) 2233 assert_equal(4, list[4]) 2234 assert_equal(4, list[-1]) 2235 assert_equal(0, list[-5]) 2236 2237 assert_equal([0, 1, 2, 3, 4], list[0:4]) 2238 assert_equal([0, 1, 2, 3, 4], list[:]) 2239 assert_equal([1, 2, 3, 4], list[1:]) 2240 assert_equal([2, 3, 4], list[2:-1]) 2241 assert_equal([4], list[4:-1]) 2242 assert_equal([], list[5:-1]) 2243 assert_equal([], list[999:-1]) 2244 assert_equal([1, 2, 3, 4], list[g:theone:g:thefour]) 2245 2246 assert_equal([0, 1, 2, 3], list[0:3]) 2247 assert_equal([0], list[0:0]) 2248 assert_equal([0, 1, 2, 3, 4], list[0:-1]) 2249 assert_equal([0, 1, 2], list[0:-3]) 2250 assert_equal([0], list[0:-5]) 2251 assert_equal([], list[0:-6]) 2252 assert_equal([], list[0:-99]) 2253 END 2254 CheckDefSuccess(lines) 2255 CheckScriptSuccess(['vim9script'] + lines) 2256 2257 lines = ['let l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 2258 CheckDefExecFailure(lines, 'E1029:') 2259 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 3) 2260enddef 2261 2262def Test_expr7_subscript_linebreak() 2263 let range = range( 2264 3) 2265 let l = range 2266 ->map('string(v:key)') 2267 assert_equal(['0', '1', '2'], l) 2268 2269 l = range 2270 ->map('string(v:key)') 2271 assert_equal(['0', '1', '2'], l) 2272 2273 l = range # comment 2274 ->map('string(v:key)') 2275 assert_equal(['0', '1', '2'], l) 2276 2277 l = range 2278 2279 ->map('string(v:key)') 2280 assert_equal(['0', '1', '2'], l) 2281 2282 l = range 2283 # comment 2284 ->map('string(v:key)') 2285 assert_equal(['0', '1', '2'], l) 2286 2287 assert_equal('1', l[ 2288 1]) 2289 2290 let d = #{one: 33} 2291 assert_equal(33, d. 2292 one) 2293enddef 2294 2295def Test_expr7_method_call() 2296 new 2297 setline(1, ['first', 'last']) 2298 'second'->append(1) 2299 "third"->append(2) 2300 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$')) 2301 bwipe! 2302 2303 let bufnr = bufnr() 2304 let loclist = [#{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 2305 loclist->setloclist(0) 2306 assert_equal([#{bufnr: bufnr, 2307 lnum: 42, 2308 col: 17, 2309 text: 'wrong', 2310 pattern: '', 2311 valid: 1, 2312 vcol: 0, 2313 nr: 0, 2314 type: '', 2315 module: ''} 2316 ], getloclist(0)) 2317enddef 2318 2319func Test_expr7_trailing_fails() 2320 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107:', 2) 2321 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274:', 2) 2322endfunc 2323 2324func Test_expr_fails() 2325 call CheckDefFailure(["let x = '1'is2"], 'E488:', 1) 2326 call CheckDefFailure(["let x = '1'isnot2"], 'E488:', 1) 2327 2328 call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1) 2329 call CheckDefFailure(["CallMe2('yes','no')"], 'E1069:', 1) 2330 call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1) 2331 2332 call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1) 2333 call CheckDefFailure(["let v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1) 2334 call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:', 1) 2335 2336 call CheckDefFailure(["echo len('asdf'"], 'E110:', 2) 2337 call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 1) 2338 call CheckDefFailure(["echo doesnotexist()"], 'E117:', 1) 2339endfunc 2340 2341" vim: shiftwidth=2 sts=2 expandtab 2342