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