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 call 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 call 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 call 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) 1108 lines =<< trim END 1109 vim9script 1110 echo 'a' .. test_null_job() 1111 END 1112 CheckScriptFailure(lines, 'E908:', 2) 1113 lines =<< trim END 1114 vim9script 1115 echo 'a' .. test_null_channel() 1116 END 1117 CheckScriptFailure(lines, 'E908:', 2) 1118enddef 1119 1120def Test_expr5_float() 1121 if !has('float') 1122 MissingFeature 'float' 1123 else 1124 assert_equal(66.0, 60.0 + 6.0) 1125 assert_equal(66.0, 60.0 + 6) 1126 assert_equal(66.0, 60 + 1127 6.0) 1128 assert_equal(5.1, g:afloat 1129 + 5) 1130 assert_equal(8.1, 8 + g:afloat) 1131 assert_equal(10.1, g:anint + g:afloat) 1132 assert_equal(10.1, g:afloat + g:anint) 1133 1134 assert_equal(54.0, 60.0 - 6.0) 1135 assert_equal(54.0, 60.0 1136 - 6) 1137 assert_equal(54.0, 60 - 6.0) 1138 assert_equal(-4.9, g:afloat - 5) 1139 assert_equal(7.9, 8 - g:afloat) 1140 assert_equal(9.9, g:anint - g:afloat) 1141 assert_equal(-9.9, g:afloat - g:anint) 1142 endif 1143enddef 1144 1145func Test_expr5_fails() 1146 let msg = "white space required before and after '+'" 1147 call CheckDefFailure(["let x = 1+2"], msg, 1) 1148 call CheckDefFailure(["let x = 1 +2"], msg, 1) 1149 call CheckDefFailure(["let x = 1+ 2"], msg, 1) 1150 1151 let msg = "white space required before and after '-'" 1152 call CheckDefFailure(["let x = 1-2"], msg, 1) 1153 call CheckDefFailure(["let x = 1 -2"], msg, 1) 1154 call CheckDefFailure(["let x = 1- 2"], msg, 1) 1155 1156 let msg = "white space required before and after '..'" 1157 call CheckDefFailure(["let x = '1'..'2'"], msg, 1) 1158 call CheckDefFailure(["let x = '1' ..'2'"], msg, 1) 1159 call CheckDefFailure(["let x = '1'.. '2'"], msg, 1) 1160 1161 call CheckDefFailure(["let x = 0z1122 + 33"], 'E1051', 1) 1162 call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1051', 1) 1163 call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1051', 1) 1164 call CheckDefFailure(["let x = 33 + 0z1122"], 'E1051', 1) 1165 call CheckDefFailure(["let x = [3] + 0z1122"], 'E1051', 1) 1166 call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1051', 1) 1167 call CheckDefFailure(["let x = 6 + xxx"], 'E1001', 1) 1168 1169 call CheckDefFailure(["let x = 'a' .. [1]"], 'E1105', 1) 1170 call CheckDefFailure(["let x = 'a' .. #{a: 1}"], 'E1105', 1) 1171 call CheckDefFailure(["let x = 'a' .. test_void()"], 'E1105', 1) 1172 call CheckDefFailure(["let x = 'a' .. 0z32"], 'E1105', 1) 1173 call CheckDefFailure(["let x = 'a' .. function('len')"], 'E1105', 1) 1174 call CheckDefFailure(["let x = 'a' .. function('len', ['a'])"], 'E1105', 1) 1175 call CheckDefFailure(["let x = 'a' .. test_null_job()"], 'E1105', 1) 1176 call CheckDefFailure(["let x = 'a' .. test_null_channel()"], 'E1105', 1) 1177endfunc 1178 1179" test multiply, divide, modulo 1180def Test_expr6() 1181 assert_equal(36, 6 * 6) 1182 assert_equal(24, 6 * 1183 g:thefour) 1184 assert_equal(24, g:thefour 1185 * 6) 1186 assert_equal(40, g:anint * g:thefour) 1187 1188 assert_equal(10, 60 / 6) 1189 assert_equal(6, 60 / 1190 g:anint) 1191 assert_equal(1, g:anint / 6) 1192 assert_equal(2, g:anint 1193 / g:thefour) 1194 1195 assert_equal(5, 11 % 6) 1196 assert_equal(4, g:anint % 6) 1197 assert_equal(3, 13 % 1198 g:anint) 1199 assert_equal(2, g:anint 1200 % g:thefour) 1201 1202 assert_equal(4, 6 * 4 / 6) 1203 1204 let x = [2] 1205 let y = [3] 1206 assert_equal(5, x[0] + y[0]) 1207 assert_equal(6, x[0] * y[0]) 1208 if has('float') 1209 let xf = [2.0] 1210 let yf = [3.0] 1211 assert_equal(5.0, xf[0] 1212 + yf[0]) 1213 assert_equal(6.0, xf[0] 1214 * yf[0]) 1215 endif 1216 1217 call CheckDefFailure(["let x = 6 * xxx"], 'E1001', 1) 1218enddef 1219 1220def Test_expr6_vim9script() 1221 # check line continuation 1222 let lines =<< trim END 1223 vim9script 1224 let var = 11 1225 * 22 1226 / 3 1227 assert_equal(80, var) 1228 END 1229 CheckScriptSuccess(lines) 1230 1231 lines =<< trim END 1232 vim9script 1233 let var = 25 1234 % 10 1235 assert_equal(5, var) 1236 END 1237 CheckScriptSuccess(lines) 1238 1239 lines =<< trim END 1240 vim9script 1241 let var = 11 * 1242 22 / 1243 3 1244 assert_equal(80, var) 1245 END 1246 CheckScriptSuccess(lines) 1247 1248 # check white space 1249 lines =<< trim END 1250 vim9script 1251 echo 5*6 1252 END 1253 CheckScriptFailure(lines, 'E1004:', 2) 1254 lines =<< trim END 1255 vim9script 1256 echo 5 *6 1257 END 1258 CheckScriptFailure(lines, 'E1004:', 2) 1259 lines =<< trim END 1260 vim9script 1261 echo 5* 6 1262 END 1263 CheckScriptFailure(lines, 'E1004:', 2) 1264enddef 1265 1266def Test_expr6_float() 1267 if !has('float') 1268 MissingFeature 'float' 1269 else 1270 assert_equal(36.0, 6.0 * 6) 1271 assert_equal(36.0, 6 * 1272 6.0) 1273 assert_equal(36.0, 6.0 * 6.0) 1274 assert_equal(1.0, g:afloat * g:anint) 1275 1276 assert_equal(10.0, 60 / 6.0) 1277 assert_equal(10.0, 60.0 / 1278 6) 1279 assert_equal(10.0, 60.0 / 6.0) 1280 assert_equal(0.01, g:afloat / g:anint) 1281 1282 assert_equal(4.0, 6.0 * 4 / 6) 1283 assert_equal(4.0, 6 * 1284 4.0 / 1285 6) 1286 assert_equal(4.0, 6 * 4 / 6.0) 1287 assert_equal(4.0, 6.0 * 4.0 / 6) 1288 assert_equal(4.0, 6 * 4.0 / 6.0) 1289 assert_equal(4.0, 6.0 * 4 / 6.0) 1290 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1291 1292 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1293 endif 1294enddef 1295 1296func Test_expr6_fails() 1297 let msg = "white space required before and after '*'" 1298 call CheckDefFailure(["let x = 1*2"], msg, 1) 1299 call CheckDefFailure(["let x = 1 *2"], msg, 1) 1300 call CheckDefFailure(["let x = 1* 2"], msg, 1) 1301 1302 let msg = "white space required before and after '/'" 1303 call CheckDefFailure(["let x = 1/2"], msg, 1) 1304 call CheckDefFailure(["let x = 1 /2"], msg, 1) 1305 call CheckDefFailure(["let x = 1/ 2"], msg, 1) 1306 1307 let msg = "white space required before and after '%'" 1308 call CheckDefFailure(["let x = 1%2"], msg, 1) 1309 call CheckDefFailure(["let x = 1 %2"], msg, 1) 1310 call CheckDefFailure(["let x = 1% 2"], msg, 1) 1311 1312 call CheckDefFailure(["let x = '1' * '2'"], 'E1036:', 1) 1313 call CheckDefFailure(["let x = '1' / '2'"], 'E1036:', 1) 1314 call CheckDefFailure(["let x = '1' % '2'"], 'E1035:', 1) 1315 1316 call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:', 1) 1317 call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:', 1) 1318 call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:', 1) 1319 1320 call CheckDefFailure(["let x = [1] * [2]"], 'E1036:', 1) 1321 call CheckDefFailure(["let x = [1] / [2]"], 'E1036:', 1) 1322 call CheckDefFailure(["let x = [1] % [2]"], 'E1035:', 1) 1323 1324 call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:', 1) 1325 call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:', 1) 1326 call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:', 1) 1327 1328 call CheckDefFailure(["let x = 0xff[1]"], 'E1107:', 1) 1329 if has('float') 1330 call CheckDefFailure(["let x = 0.7[1]"], 'E1107:', 1) 1331 endif 1332endfunc 1333 1334func Test_expr6_float_fails() 1335 CheckFeature float 1336 call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:', 1) 1337endfunc 1338 1339" define here to use old style parsing 1340if has('float') 1341 let g:float_zero = 0.0 1342 let g:float_neg = -9.8 1343 let g:float_big = 9.9e99 1344endif 1345let g:blob_empty = 0z 1346let g:blob_one = 0z01 1347let g:blob_long = 0z0102.0304 1348 1349let g:string_empty = '' 1350let g:string_short = 'x' 1351let g:string_long = 'abcdefghijklm' 1352let g:string_special = "ab\ncd\ref\ekk" 1353 1354let g:special_true = v:true 1355let g:special_false = v:false 1356let g:special_null = v:null 1357let g:special_none = v:none 1358 1359let g:list_empty = [] 1360let g:list_mixed = [1, 'b', v:false] 1361 1362let g:dict_empty = {} 1363let g:dict_one = #{one: 1} 1364 1365let $TESTVAR = 'testvar' 1366 1367" type casts 1368def Test_expr7t() 1369 let ls: list<string> = ['a', <string>g:string_empty] 1370 let ln: list<number> = [<number>g:anint, <number>g:thefour] 1371 let nr = <number>234 1372 assert_equal(234, nr) 1373 1374 call CheckDefFailure(["let x = <nr>123"], 'E1010:', 1) 1375 call CheckDefFailure(["let x = <number >123"], 'E1068:', 1) 1376 call CheckDefFailure(["let x = <number 123"], 'E1104:', 1) 1377enddef 1378 1379" test low level expression 1380def Test_expr7_number() 1381 # number constant 1382 assert_equal(0, 0) 1383 assert_equal(654, 0654) 1384 1385 assert_equal(6, 0x6) 1386 assert_equal(15, 0xf) 1387 assert_equal(255, 0xff) 1388enddef 1389 1390def Test_expr7_float() 1391 # float constant 1392 if !has('float') 1393 MissingFeature 'float' 1394 else 1395 assert_equal(g:float_zero, .0) 1396 assert_equal(g:float_zero, 0.0) 1397 assert_equal(g:float_neg, -9.8) 1398 assert_equal(g:float_big, 9.9e99) 1399 endif 1400enddef 1401 1402def Test_expr7_blob() 1403 # blob constant 1404 assert_equal(g:blob_empty, 0z) 1405 assert_equal(g:blob_one, 0z01) 1406 assert_equal(g:blob_long, 0z0102.0304) 1407 1408 call CheckDefFailure(["let x = 0z123"], 'E973:', 1) 1409enddef 1410 1411def Test_expr7_string() 1412 # string constant 1413 assert_equal(g:string_empty, '') 1414 assert_equal(g:string_empty, "") 1415 assert_equal(g:string_short, 'x') 1416 assert_equal(g:string_short, "x") 1417 assert_equal(g:string_long, 'abcdefghijklm') 1418 assert_equal(g:string_long, "abcdefghijklm") 1419 assert_equal(g:string_special, "ab\ncd\ref\ekk") 1420 1421 call CheckDefFailure(['let x = "abc'], 'E114:', 1) 1422 call CheckDefFailure(["let x = 'abc"], 'E115:', 1) 1423enddef 1424 1425def Test_expr7_vimvar() 1426 let old: list<string> = v:oldfiles 1427 let compl: dict<any> = v:completed_item 1428 1429 call CheckDefFailure(["let old: list<number> = v:oldfiles"], 'E1012: type mismatch, expected list<number> but got list<string>', 1) 1430 call CheckDefFailure(["let old: dict<number> = v:completed_item"], 'E1012: type mismatch, expected dict<number> but got dict<any>', 1) 1431enddef 1432 1433def Test_expr7_special() 1434 # special constant 1435 assert_equal(g:special_true, true) 1436 assert_equal(g:special_false, false) 1437 assert_equal(g:special_true, v:true) 1438 assert_equal(g:special_false, v:false) 1439 1440 assert_equal(true, !false) 1441 assert_equal(false, !true) 1442 assert_equal(true, !0) 1443 assert_equal(false, !1) 1444 assert_equal(false, !!false) 1445 assert_equal(true, !!true) 1446 assert_equal(false, !!0) 1447 assert_equal(true, !!1) 1448 1449 assert_equal(g:special_null, v:null) 1450 assert_equal(g:special_none, v:none) 1451 1452 call CheckDefFailure(['v:true = true'], 'E46:', 1) 1453 call CheckDefFailure(['v:true = false'], 'E46:', 1) 1454 call CheckDefFailure(['v:false = true'], 'E46:', 1) 1455 call CheckDefFailure(['v:null = 11'], 'E46:', 1) 1456 call CheckDefFailure(['v:none = 22'], 'E46:', 1) 1457enddef 1458 1459def Test_expr7_special_vim9script() 1460 let lines =<< trim END 1461 vim9script 1462 let t = true 1463 let f = false 1464 assert_equal(v:true, true) 1465 assert_equal(true, t) 1466 assert_equal(v:false, false) 1467 assert_equal(false, f) 1468 assert_equal(true, !false) 1469 assert_equal(false, !true) 1470 assert_equal(true, !0) 1471 assert_equal(false, !1) 1472 assert_equal(false, !!false) 1473 assert_equal(true, !!true) 1474 assert_equal(false, !!0) 1475 assert_equal(true, !!1) 1476 END 1477 CheckScriptSuccess(lines) 1478enddef 1479 1480def Test_expr7_list() 1481 # list 1482 assert_equal(g:list_empty, []) 1483 assert_equal(g:list_empty, [ ]) 1484 1485 let numbers: list<number> = [1, 2, 3] 1486 numbers = [1] 1487 numbers = [] 1488 1489 let strings: list<string> = ['a', 'b', 'c'] 1490 strings = ['x'] 1491 strings = [] 1492 1493 let mixed: list<any> = [1, 'b', false,] 1494 assert_equal(g:list_mixed, mixed) 1495 assert_equal('b', mixed[1]) 1496 1497 echo [1, 1498 2] [3, 1499 4] 1500 1501 call CheckDefFailure(["let x = 1234[3]"], 'E1107:', 1) 1502 call CheckDefExecFailure(["let x = g:anint[3]"], 'E1062:', 1) 1503 1504 call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:', 1) 1505 1506 call CheckDefFailure(["let x = [1,2,3]"], 'E1069:', 1) 1507 call CheckDefFailure(["let x = [1 ,2, 3]"], 'E1068:', 1) 1508 1509 call CheckDefExecFailure(["echo 1", "let x = [][0]", "echo 3"], 'E684:', 2) 1510 1511 call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E1029:', 1) 1512 call CheckDefFailure(["let x = g:list_mixed["], 'E1097:', 2) 1513 call CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:', 2) 1514 call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:', 1) 1515 call CheckDefFailure(["let l: list<number> = [234, 'x']"], 'E1012:', 1) 1516 call CheckDefFailure(["let l: list<number> = ['x', 234]"], 'E1012:', 1) 1517 call CheckDefFailure(["let l: list<string> = [234, 'x']"], 'E1012:', 1) 1518 call CheckDefFailure(["let l: list<string> = ['x', 123]"], 'E1012:', 1) 1519enddef 1520 1521def Test_expr7_list_vim9script() 1522 let lines =<< trim END 1523 vim9script 1524 let l = [ 1525 11, 1526 22, 1527 ] 1528 assert_equal([11, 22], l) 1529 1530 echo [1, 1531 2] [3, 1532 4] 1533 END 1534 CheckScriptSuccess(lines) 1535 1536 lines =<< trim END 1537 vim9script 1538 let l = [11, 1539 22] 1540 assert_equal([11, 22], l) 1541 END 1542 CheckScriptSuccess(lines) 1543 1544 lines =<< trim END 1545 vim9script 1546 let l = [11,22] 1547 END 1548 CheckScriptFailure(lines, 'E1069:', 2) 1549 1550 lines =<< trim END 1551 vim9script 1552 let l = [11 , 22] 1553 END 1554 CheckScriptFailure(lines, 'E1068:', 2) 1555 1556 lines =<< trim END 1557 vim9script 1558 let l: list<number> = [234, 'x'] 1559 END 1560 CheckScriptFailure(lines, 'E1012:', 2) 1561 lines =<< trim END 1562 vim9script 1563 let l: list<number> = ['x', 234] 1564 END 1565 CheckScriptFailure(lines, 'E1012:', 2) 1566 lines =<< trim END 1567 vim9script 1568 let l: list<string> = ['x', 234] 1569 END 1570 CheckScriptFailure(lines, 'E1012:', 2) 1571 lines =<< trim END 1572 vim9script 1573 let l: list<string> = [234, 'x'] 1574 END 1575 CheckScriptFailure(lines, 'E1012:', 2) 1576enddef 1577 1578def LambdaWithComments(): func 1579 return {x -> 1580 # some comment 1581 x == 1 1582 # some comment 1583 || 1584 x == 2 1585 } 1586enddef 1587 1588def LambdaUsingArg(x: number): func 1589 return {-> 1590 # some comment 1591 x == 1 1592 # some comment 1593 || 1594 x == 2 1595 } 1596enddef 1597 1598def Test_expr7_lambda() 1599 let La = { -> 'result'} 1600 assert_equal('result', La()) 1601 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) 1602 1603 # line continuation inside lambda with "cond ? expr : expr" works 1604 let ll = range(3) 1605 map(ll, {k, v -> v % 2 ? { 1606 '111': 111 } : {} 1607 }) 1608 assert_equal([{}, {'111': 111}, {}], ll) 1609 1610 ll = range(3) 1611 map(ll, {k, v -> v == 8 || v 1612 == 9 1613 || v % 2 ? 111 : 222 1614 }) 1615 assert_equal([222, 111, 222], ll) 1616 1617 ll = range(3) 1618 map(ll, {k, v -> v != 8 && v 1619 != 9 1620 && v % 2 == 0 ? 111 : 222 1621 }) 1622 assert_equal([111, 222, 111], ll) 1623 1624 let dl = [{'key': 0}, {'key': 22}]->filter({ _, v -> v['key'] }) 1625 assert_equal([{'key': 22}], dl) 1626 1627 dl = [{'key': 12}, {'foo': 34}] 1628 assert_equal([{'key': 12}], filter(dl, 1629 {_, v -> has_key(v, 'key') ? v['key'] == 12 : 0})) 1630 1631 assert_equal(false, LambdaWithComments()(0)) 1632 assert_equal(true, LambdaWithComments()(1)) 1633 assert_equal(true, LambdaWithComments()(2)) 1634 assert_equal(false, LambdaWithComments()(3)) 1635 1636 assert_equal(false, LambdaUsingArg(0)()) 1637 assert_equal(true, LambdaUsingArg(1)()) 1638 1639 call CheckDefFailure(["filter([1, 2], {k,v -> 1})"], 'E1069:', 1) 1640 # error is in first line of the lambda 1641 call CheckDefFailure(["let L = {a -> a + b}"], 'E1001:', 0) 1642 1643 assert_equal('xxxyyy', 'xxx'->{a, b -> a .. b}('yyy')) 1644 1645 CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x')"], 1646 'E1106: one argument too many') 1647 CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x', 'y')"], 1648 'E1106: 2 arguments too many') 1649 CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1) 1650enddef 1651 1652def Test_expr7_lambda_vim9script() 1653 let lines =<< trim END 1654 vim9script 1655 let v = 10->{a -> 1656 a 1657 + 2 1658 }() 1659 assert_equal(12, v) 1660 END 1661 CheckScriptSuccess(lines) 1662 1663 # nested lambda with line breaks 1664 lines =<< trim END 1665 vim9script 1666 search('"', 'cW', 0, 0, {-> 1667 synstack('.', col('.')) 1668 ->map({_, v -> synIDattr(v, 'name')})->len()}) 1669 END 1670 CheckScriptSuccess(lines) 1671enddef 1672 1673def Test_epxr7_funcref() 1674 let lines =<< trim END 1675 def RetNumber(): number 1676 return 123 1677 enddef 1678 let FuncRef = RetNumber 1679 assert_equal(123, FuncRef()) 1680 END 1681 CheckDefAndScriptSuccess(lines) 1682enddef 1683 1684def Test_expr7_dict() 1685 # dictionary 1686 assert_equal(g:dict_empty, {}) 1687 assert_equal(g:dict_empty, { }) 1688 assert_equal(g:dict_one, {'one': 1}) 1689 let key = 'one' 1690 let val = 1 1691 assert_equal(g:dict_one, {key: val}) 1692 1693 let numbers: dict<number> = #{a: 1, b: 2, c: 3} 1694 numbers = #{a: 1} 1695 numbers = #{} 1696 1697 let strings: dict<string> = #{a: 'a', b: 'b', c: 'c'} 1698 strings = #{a: 'x'} 1699 strings = #{} 1700 1701 let mixed: dict<any> = #{a: 'a', b: 42} 1702 mixed = #{a: 'x'} 1703 mixed = #{a: 234} 1704 mixed = #{} 1705 1706 call CheckDefFailure(["let x = #{a:8}"], 'E1069:', 1) 1707 call CheckDefFailure(["let x = #{a : 8}"], 'E1068:', 1) 1708 call CheckDefFailure(["let x = #{a :8}"], 'E1068:', 1) 1709 call CheckDefFailure(["let x = #{a: 8 , b: 9}"], 'E1068:', 1) 1710 1711 call CheckDefFailure(["let x = #{8: 8}"], 'E1014:', 1) 1712 call CheckDefFailure(["let x = #{xxx}"], 'E720:', 1) 1713 call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:', 2) 1714 call CheckDefFailure(["let x = #{xxx: 1,"], 'E723:', 2) 1715 call CheckDefFailure(["let x = {'a': xxx}"], 'E1001:', 1) 1716 call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:', 1) 1717 call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:', 1) 1718 call CheckDefFailure(["let x = #"], 'E1015:', 1) 1719 call CheckDefExecFailure(["let x = g:anint.member"], 'E715:', 1) 1720 call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:', 1) 1721 1722 call CheckDefFailure(['let x: dict<number> = #{a: 234, b: "1"}'], 'E1012:', 1) 1723 call CheckDefFailure(['let x: dict<number> = #{a: "x", b: 134}'], 'E1012:', 1) 1724 call CheckDefFailure(['let x: dict<string> = #{a: 234, b: "1"}'], 'E1012:', 1) 1725 call CheckDefFailure(['let x: dict<string> = #{a: "x", b: 134}'], 'E1012:', 1) 1726enddef 1727 1728def Test_expr7_dict_vim9script() 1729 let lines =<< trim END 1730 vim9script 1731 let d = { 1732 'one': 1733 1, 1734 'two': 2, 1735 } 1736 assert_equal({'one': 1, 'two': 2}, d) 1737 END 1738 CheckScriptSuccess(lines) 1739 1740 lines =<< trim END 1741 vim9script 1742 let d = { "one": "one", "two": "two", } 1743 assert_equal({'one': 'one', 'two': 'two'}, d) 1744 END 1745 CheckScriptSuccess(lines) 1746 1747 lines =<< trim END 1748 vim9script 1749 let d = #{one: 1, 1750 two: 2, 1751 } 1752 assert_equal({'one': 1, 'two': 2}, d) 1753 END 1754 CheckScriptSuccess(lines) 1755 1756 lines =<< trim END 1757 vim9script 1758 let d = #{one:1, two: 2} 1759 END 1760 CheckScriptFailure(lines, 'E1069:', 2) 1761 1762 lines =<< trim END 1763 vim9script 1764 let d = #{one: 1,two: 2} 1765 END 1766 CheckScriptFailure(lines, 'E1069:', 2) 1767 1768 lines =<< trim END 1769 vim9script 1770 let d = #{one : 1} 1771 END 1772 CheckScriptFailure(lines, 'E1068:', 2) 1773 1774 lines =<< trim END 1775 vim9script 1776 let d = #{one:1} 1777 END 1778 CheckScriptFailure(lines, 'E1069:', 2) 1779 1780 lines =<< trim END 1781 vim9script 1782 let d = #{one: 1 , two: 2} 1783 END 1784 CheckScriptFailure(lines, 'E1068:', 2) 1785 1786 lines =<< trim END 1787 vim9script 1788 let l: dict<number> = #{a: 234, b: 'x'} 1789 END 1790 CheckScriptFailure(lines, 'E1012:', 2) 1791 lines =<< trim END 1792 vim9script 1793 let l: dict<number> = #{a: 'x', b: 234} 1794 END 1795 CheckScriptFailure(lines, 'E1012:', 2) 1796 lines =<< trim END 1797 vim9script 1798 let l: dict<string> = #{a: 'x', b: 234} 1799 END 1800 CheckScriptFailure(lines, 'E1012:', 2) 1801 lines =<< trim END 1802 vim9script 1803 let l: dict<string> = #{a: 234, b: 'x'} 1804 END 1805 CheckScriptFailure(lines, 'E1012:', 2) 1806enddef 1807 1808let g:oneString = 'one' 1809 1810def Test_expr_member() 1811 assert_equal(1, g:dict_one.one) 1812 let d: dict<number> = g:dict_one 1813 assert_equal(1, d['one']) 1814 assert_equal(1, d[ 1815 'one' 1816 ]) 1817 assert_equal(1, d 1818 .one) 1819 d = {'1': 1, '_': 2} 1820 assert_equal(1, d 1821 .1) 1822 assert_equal(2, d 1823 ._) 1824 1825 # getting the one member should clear the dict after getting the item 1826 assert_equal('one', #{one: 'one'}.one) 1827 assert_equal('one', #{one: 'one'}[g:oneString]) 1828 1829 call CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:', 1) 1830 call CheckDefExecFailure(["let d: dict<any>", "echo d['a']"], 'E716:', 2) 1831 call CheckDefExecFailure(["let d: dict<number>", "d = g:list_empty"], 'E1029: Expected dict but got list', 2) 1832enddef 1833 1834def Test_expr7_any_index_slice() 1835 let lines =<< trim END 1836 # getting the one member should clear the list only after getting the item 1837 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 1838 1839 # string is permissive, index out of range accepted 1840 g:teststring = 'abcdef' 1841 assert_equal('b', g:teststring[1]) 1842 assert_equal('', g:teststring[-1]) 1843 assert_equal('', g:teststring[99]) 1844 1845 assert_equal('b', g:teststring[1:1]) 1846 assert_equal('bcdef', g:teststring[1:]) 1847 assert_equal('abcd', g:teststring[:3]) 1848 assert_equal('cdef', g:teststring[-4:]) 1849 assert_equal('abcdef', g:teststring[-9:]) 1850 assert_equal('abcd', g:teststring[:-3]) 1851 assert_equal('', g:teststring[:-9]) 1852 1853 # blob index cannot be out of range 1854 g:testblob = 0z01ab 1855 assert_equal(0x01, g:testblob[0]) 1856 assert_equal(0xab, g:testblob[1]) 1857 assert_equal(0xab, g:testblob[-1]) 1858 assert_equal(0x01, g:testblob[-2]) 1859 1860 # blob slice accepts out of range 1861 assert_equal(0z01ab, g:testblob[0:1]) 1862 assert_equal(0z01, g:testblob[0:0]) 1863 assert_equal(0z01, g:testblob[-2:-2]) 1864 assert_equal(0zab, g:testblob[1:1]) 1865 assert_equal(0zab, g:testblob[-1:-1]) 1866 assert_equal(0z, g:testblob[2:2]) 1867 assert_equal(0z, g:testblob[0:-3]) 1868 1869 # list index cannot be out of range 1870 g:testlist = [0, 1, 2, 3] 1871 assert_equal(0, g:testlist[0]) 1872 assert_equal(1, g:testlist[1]) 1873 assert_equal(3, g:testlist[3]) 1874 assert_equal(3, g:testlist[-1]) 1875 assert_equal(0, g:testlist[-4]) 1876 assert_equal(1, g:testlist[g:theone]) 1877 1878 # list slice accepts out of range 1879 assert_equal([0], g:testlist[0:0]) 1880 assert_equal([3], g:testlist[3:3]) 1881 assert_equal([0, 1], g:testlist[0:1]) 1882 assert_equal([0, 1, 2, 3], g:testlist[0:3]) 1883 assert_equal([0, 1, 2, 3], g:testlist[0:9]) 1884 assert_equal([], g:testlist[-1:1]) 1885 assert_equal([1], g:testlist[-3:1]) 1886 assert_equal([0, 1], g:testlist[-4:1]) 1887 assert_equal([0, 1], g:testlist[-9:1]) 1888 assert_equal([1, 2, 3], g:testlist[1:-1]) 1889 assert_equal([1], g:testlist[1:-3]) 1890 assert_equal([], g:testlist[1:-4]) 1891 assert_equal([], g:testlist[1:-9]) 1892 1893 g:testdict = #{a: 1, b: 2} 1894 assert_equal(1, g:testdict['a']) 1895 assert_equal(2, g:testdict['b']) 1896 END 1897 1898 CheckDefSuccess(lines) 1899 CheckScriptSuccess(['vim9script'] + lines) 1900 1901 CheckDefExecFailure(['echo g:testblob[2]'], 'E979:', 1) 1902 CheckScriptFailure(['vim9script', 'echo g:testblob[2]'], 'E979:', 2) 1903 CheckDefExecFailure(['echo g:testblob[-3]'], 'E979:', 1) 1904 CheckScriptFailure(['vim9script', 'echo g:testblob[-3]'], 'E979:', 2) 1905 1906 CheckDefExecFailure(['echo g:testlist[4]'], 'E684:', 1) 1907 CheckScriptFailure(['vim9script', 'echo g:testlist[4]'], 'E684:', 2) 1908 CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1) 1909 CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684:', 2) 1910 1911 CheckDefExecFailure(['echo g:testdict["a":"b"]'], 'E719:', 1) 1912 CheckScriptFailure(['vim9script', 'echo g:testdict["a":"b"]'], 'E719:', 2) 1913 CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1) 1914 CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2) 1915 1916 unlet g:teststring 1917 unlet g:testblob 1918 unlet g:testlist 1919enddef 1920 1921def Test_expr_member_vim9script() 1922 let lines =<< trim END 1923 vim9script 1924 let d = #{one: 1925 'one', 1926 two: 'two', 1927 1: 1, 1928 _: 2} 1929 assert_equal('one', d.one) 1930 assert_equal('one', d 1931 .one) 1932 assert_equal(1, d 1933 .1) 1934 assert_equal(2, d 1935 ._) 1936 assert_equal('one', d[ 1937 'one' 1938 ]) 1939 END 1940 CheckScriptSuccess(lines) 1941 1942 lines =<< trim END 1943 vim9script 1944 let l = [1, 1945 2, 1946 3, 4 1947 ] 1948 assert_equal(2, l[ 1949 1 1950 ]) 1951 assert_equal([2, 3], l[1 : 2]) 1952 assert_equal([1, 2, 3], l[ 1953 : 1954 2 1955 ]) 1956 assert_equal([3, 4], l[ 1957 2 1958 : 1959 ]) 1960 END 1961 CheckScriptSuccess(lines) 1962enddef 1963 1964def Test_expr7_option() 1965 # option 1966 set ts=11 1967 assert_equal(11, &ts) 1968 &ts = 9 1969 assert_equal(9, &ts) 1970 set ts=8 1971 set grepprg=some\ text 1972 assert_equal('some text', &grepprg) 1973 &grepprg = test_null_string() 1974 assert_equal('', &grepprg) 1975 set grepprg& 1976enddef 1977 1978def Test_expr7_environment() 1979 # environment variable 1980 assert_equal('testvar', $TESTVAR) 1981 assert_equal('', $ASDF_ASD_XXX) 1982 1983 call CheckDefFailure(["let x = $$$"], 'E1002:', 1) 1984enddef 1985 1986def Test_expr7_register() 1987 @a = 'register a' 1988 assert_equal('register a', @a) 1989 1990 let fname = expand('%') 1991 assert_equal(fname, @%) 1992 1993 feedkeys(":echo 'some'\<CR>", "xt") 1994 assert_equal("echo 'some'", @:) 1995 1996 normal axyz 1997 assert_equal("xyz", @.) 1998 call CheckDefFailure(["@. = 'yes'"], 'E354:', 1) 1999 2000 @/ = 'slash' 2001 assert_equal('slash', @/) 2002 2003 @= = 'equal' 2004 assert_equal('equal', @=) 2005enddef 2006 2007def Test_expr7_namespace() 2008 g:some_var = 'some' 2009 assert_equal('some', get(g:, 'some_var')) 2010 assert_equal('some', get(g:, 'some_var', 'xxx')) 2011 assert_equal('xxx', get(g:, 'no_var', 'xxx')) 2012 unlet g:some_var 2013 2014 b:some_var = 'some' 2015 assert_equal('some', get(b:, 'some_var')) 2016 assert_equal('some', get(b:, 'some_var', 'xxx')) 2017 assert_equal('xxx', get(b:, 'no_var', 'xxx')) 2018 unlet b:some_var 2019 2020 w:some_var = 'some' 2021 assert_equal('some', get(w:, 'some_var')) 2022 assert_equal('some', get(w:, 'some_var', 'xxx')) 2023 assert_equal('xxx', get(w:, 'no_var', 'xxx')) 2024 unlet w:some_var 2025 2026 t:some_var = 'some' 2027 assert_equal('some', get(t:, 'some_var')) 2028 assert_equal('some', get(t:, 'some_var', 'xxx')) 2029 assert_equal('xxx', get(t:, 'no_var', 'xxx')) 2030 unlet t:some_var 2031enddef 2032 2033def Test_expr7_parens() 2034 # (expr) 2035 assert_equal(4, (6 * 4) / 6) 2036 assert_equal(0, 6 * ( 4 / 6 )) 2037 2038 assert_equal(6, +6) 2039 assert_equal(-6, -6) 2040 assert_equal(6, --6) 2041 assert_equal(6, -+-6) 2042 assert_equal(-6, ---6) 2043 assert_equal(false, !-3) 2044 assert_equal(true, !+-+0) 2045enddef 2046 2047def Test_expr7_parens_vim9script() 2048 let lines =<< trim END 2049 vim9script 2050 let s = ( 2051 'one' 2052 .. 2053 'two' 2054 ) 2055 assert_equal('onetwo', s) 2056 END 2057 CheckScriptSuccess(lines) 2058enddef 2059 2060def Test_expr7_negate() 2061 assert_equal(-99, -99) 2062 assert_equal(99, --99) 2063 let nr = 88 2064 assert_equal(-88, -nr) 2065 assert_equal(88, --nr) 2066enddef 2067 2068def Echo(arg: any): string 2069 return arg 2070enddef 2071 2072def s:Echo4Arg(arg: any): string 2073 return arg 2074enddef 2075 2076def Test_expr7_call() 2077 assert_equal('yes', 'yes'->Echo()) 2078 assert_equal('yes', 'yes' 2079 ->s:Echo4Arg()) 2080 assert_equal(true, !range(5)->empty()) 2081 assert_equal([0, 1, 2], --3->range()) 2082 2083 call CheckDefFailure(["let x = 'yes'->Echo"], 'E107:', 1) 2084 call CheckScriptFailure([ 2085 "vim9script", 2086 "let x = substitute ('x', 'x', 'x', 'x')" 2087 ], 'E121:', 2) 2088 2089 let auto_lines =<< trim END 2090 def g:some#func(): string 2091 return 'found' 2092 enddef 2093 END 2094 mkdir('Xruntime/autoload', 'p') 2095 writefile(auto_lines, 'Xruntime/autoload/some.vim') 2096 let save_rtp = &rtp 2097 &rtp = getcwd() .. '/Xruntime,' .. &rtp 2098 assert_equal('found', g:some#func()) 2099 assert_equal('found', some#func()) 2100 2101 &rtp = save_rtp 2102 delete('Xruntime', 'rf') 2103enddef 2104 2105 2106def Test_expr7_not() 2107 let lines =<< trim END 2108 assert_equal(true, !'') 2109 assert_equal(true, ![]) 2110 assert_equal(false, !'asdf') 2111 assert_equal(false, ![2]) 2112 assert_equal(true, !!'asdf') 2113 assert_equal(true, !![2]) 2114 2115 assert_equal(true, !test_null_partial()) 2116 assert_equal(false, !{-> 'yes'}) 2117 2118 assert_equal(true, !test_null_dict()) 2119 assert_equal(true, !{}) 2120 assert_equal(false, !{'yes': 'no'}) 2121 2122 if has('channel') 2123 assert_equal(true, !test_null_job()) 2124 assert_equal(true, !test_null_channel()) 2125 endif 2126 2127 assert_equal(true, !test_null_blob()) 2128 assert_equal(true, !0z) 2129 assert_equal(false, !0z01) 2130 2131 assert_equal(true, !test_void()) 2132 assert_equal(true, !test_unknown()) 2133 2134 assert_equal(false, ![1, 2, 3]->reverse()) 2135 assert_equal(true, ![]->reverse()) 2136 END 2137 CheckDefSuccess(lines) 2138 CheckScriptSuccess(['vim9script'] + lines) 2139enddef 2140 2141func Test_expr7_fails() 2142 call CheckDefFailure(["let x = (12"], "E110:", 1) 2143 2144 call CheckDefFailure(["let x = -'xx'"], "E1030:", 1) 2145 call CheckDefFailure(["let x = +'xx'"], "E1030:", 1) 2146 call CheckDefFailure(["let x = -0z12"], "E974:", 1) 2147 call CheckDefExecFailure(["let x = -[8]"], "E39:", 1) 2148 call CheckDefExecFailure(["let x = -{'a': 1}"], "E39:", 1) 2149 2150 call CheckDefFailure(["let x = @"], "E1002:", 1) 2151 call CheckDefFailure(["let x = @<"], "E354:", 1) 2152 2153 call CheckDefFailure(["let x = [1, 2"], "E697:", 2) 2154 call CheckDefFailure(["let x = [notfound]"], "E1001:", 1) 2155 2156 call CheckDefFailure(["let x = { -> 123) }"], "E451:", 1) 2157 call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:", 1) 2158 2159 call CheckDefFailure(["let x = ¬exist"], 'E113:', 1) 2160 call CheckDefFailure(["&grepprg = [343]"], 'E1012:', 1) 2161 2162 call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:', 1) 2163 call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:', 1) 2164 2165 call CheckDefFailure(["echo a:somevar"], 'E1075:', 1) 2166 call CheckDefFailure(["echo l:somevar"], 'E1075:', 1) 2167 call CheckDefFailure(["echo x:somevar"], 'E1075:', 1) 2168 2169 call CheckDefExecFailure(["let x = +g:astring"], 'E1030:', 1) 2170 call CheckDefExecFailure(["let x = +g:ablob"], 'E974:', 1) 2171 call CheckDefExecFailure(["let x = +g:alist"], 'E745:', 1) 2172 call CheckDefExecFailure(["let x = +g:adict"], 'E728:', 1) 2173 2174 call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:', 2) 2175 2176 call CheckDefFailure(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 1) 2177 2178 call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2) 2179 call CheckDefExecFailure(["#{a: 1->len()"], 'E488:', 1) 2180 call CheckDefExecFailure(["{'a': 1->len()"], 'E723:', 2) 2181endfunc 2182 2183let g:Funcrefs = [function('add')] 2184 2185func CallMe(arg) 2186 return a:arg 2187endfunc 2188 2189func CallMe2(one, two) 2190 return a:one .. a:two 2191endfunc 2192 2193def Test_expr7_trailing() 2194 # user function call 2195 assert_equal(123, g:CallMe(123)) 2196 assert_equal(123, g:CallMe( 123)) 2197 assert_equal(123, g:CallMe(123 )) 2198 assert_equal('yesno', g:CallMe2('yes', 'no')) 2199 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 2200 assert_equal('nothing', g:CallMe('nothing')) 2201 2202 # partial call 2203 let Part = function('g:CallMe') 2204 assert_equal('yes', Part('yes')) 2205 2206 # funcref call, using list index 2207 let l = [] 2208 g:Funcrefs[0](l, 2) 2209 assert_equal([2], l) 2210 2211 # method call 2212 l = [2, 5, 6] 2213 l->map({k, v -> k + v}) 2214 assert_equal([2, 6, 8], l) 2215 2216 # lambda method call 2217 l = [2, 5] 2218 l->{l -> add(l, 8)}() 2219 assert_equal([2, 5, 8], l) 2220 2221 # dict member 2222 let d = #{key: 123} 2223 assert_equal(123, d.key) 2224enddef 2225 2226def Test_expr7_string_subscript() 2227 let lines =<< trim END 2228 let text = 'abcdef' 2229 assert_equal('', text[-1]) 2230 assert_equal('a', text[0]) 2231 assert_equal('e', text[4]) 2232 assert_equal('f', text[5]) 2233 assert_equal('', text[6]) 2234 2235 text = 'ábçdëf' 2236 assert_equal('', text[-999]) 2237 assert_equal('', text[-1]) 2238 assert_equal('á', text[0]) 2239 assert_equal('b', text[1]) 2240 assert_equal('ç', text[2]) 2241 assert_equal('d', text[3]) 2242 assert_equal('ë', text[4]) 2243 assert_equal('f', text[5]) 2244 assert_equal('', text[6]) 2245 assert_equal('', text[999]) 2246 2247 assert_equal('ábçdëf', text[0:-1]) 2248 assert_equal('ábçdëf', text[0 :-1]) 2249 assert_equal('ábçdëf', text[0: -1]) 2250 assert_equal('ábçdëf', text[0 : -1]) 2251 assert_equal('ábçdëf', text[0 2252 :-1]) 2253 assert_equal('ábçdëf', text[0: 2254 -1]) 2255 assert_equal('ábçdëf', text[0 : -1 2256 ]) 2257 assert_equal('bçdëf', text[1:-1]) 2258 assert_equal('çdëf', text[2:-1]) 2259 assert_equal('dëf', text[3:-1]) 2260 assert_equal('ëf', text[4:-1]) 2261 assert_equal('f', text[5:-1]) 2262 assert_equal('', text[6:-1]) 2263 assert_equal('', text[999:-1]) 2264 2265 assert_equal('ábçd', text[:3]) 2266 assert_equal('bçdëf', text[1:]) 2267 assert_equal('ábçdëf', text[:]) 2268 END 2269 CheckDefSuccess(lines) 2270 CheckScriptSuccess(['vim9script'] + lines) 2271enddef 2272 2273def Test_expr7_list_subscript() 2274 let lines =<< trim END 2275 let list = [0, 1, 2, 3, 4] 2276 assert_equal(0, list[0]) 2277 assert_equal(4, list[4]) 2278 assert_equal(4, list[-1]) 2279 assert_equal(0, list[-5]) 2280 2281 assert_equal([0, 1, 2, 3, 4], list[0:4]) 2282 assert_equal([0, 1, 2, 3, 4], list[:]) 2283 assert_equal([1, 2, 3, 4], list[1:]) 2284 assert_equal([2, 3, 4], list[2:-1]) 2285 assert_equal([4], list[4:-1]) 2286 assert_equal([], list[5:-1]) 2287 assert_equal([], list[999:-1]) 2288 assert_equal([1, 2, 3, 4], list[g:theone:g:thefour]) 2289 2290 assert_equal([0, 1, 2, 3], list[0:3]) 2291 assert_equal([0], list[0:0]) 2292 assert_equal([0, 1, 2, 3, 4], list[0:-1]) 2293 assert_equal([0, 1, 2], list[0:-3]) 2294 assert_equal([0], list[0:-5]) 2295 assert_equal([], list[0:-6]) 2296 assert_equal([], list[0:-99]) 2297 END 2298 CheckDefSuccess(lines) 2299 CheckScriptSuccess(['vim9script'] + lines) 2300 2301 lines = ['let l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 2302 CheckDefExecFailure(lines, 'E1029:') 2303 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 3) 2304enddef 2305 2306def Test_expr7_subscript_linebreak() 2307 let range = range( 2308 3) 2309 let l = range 2310 ->map('string(v:key)') 2311 assert_equal(['0', '1', '2'], l) 2312 2313 l = range 2314 ->map('string(v:key)') 2315 assert_equal(['0', '1', '2'], l) 2316 2317 l = range # comment 2318 ->map('string(v:key)') 2319 assert_equal(['0', '1', '2'], l) 2320 2321 l = range 2322 2323 ->map('string(v:key)') 2324 assert_equal(['0', '1', '2'], l) 2325 2326 l = range 2327 # comment 2328 ->map('string(v:key)') 2329 assert_equal(['0', '1', '2'], l) 2330 2331 assert_equal('1', l[ 2332 1]) 2333 2334 let d = #{one: 33} 2335 assert_equal(33, d. 2336 one) 2337enddef 2338 2339def Test_expr7_method_call() 2340 new 2341 setline(1, ['first', 'last']) 2342 'second'->append(1) 2343 "third"->append(2) 2344 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$')) 2345 bwipe! 2346 2347 let bufnr = bufnr() 2348 let loclist = [#{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 2349 loclist->setloclist(0) 2350 assert_equal([#{bufnr: bufnr, 2351 lnum: 42, 2352 col: 17, 2353 text: 'wrong', 2354 pattern: '', 2355 valid: 1, 2356 vcol: 0, 2357 nr: 0, 2358 type: '', 2359 module: ''} 2360 ], getloclist(0)) 2361enddef 2362 2363func Test_expr7_trailing_fails() 2364 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107:', 2) 2365 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274:', 2) 2366endfunc 2367 2368func Test_expr_fails() 2369 call CheckDefFailure(["let x = '1'is2"], 'E488:', 1) 2370 call CheckDefFailure(["let x = '1'isnot2"], 'E488:', 1) 2371 2372 call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1) 2373 call CheckScriptFailure(["CallMe ('yes')"], 'E492:', 1) 2374 call CheckScriptAndDefFailure(["CallMe2('yes','no')"], 'E1069:', 1) 2375 call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1) 2376 2377 call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1) 2378 call CheckDefFailure(["let v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1) 2379 call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:', 1) 2380 2381 call CheckDefFailure(["echo len('asdf'"], 'E110:', 2) 2382 call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 1) 2383 call CheckDefFailure(["echo doesnotexist()"], 'E117:', 1) 2384endfunc 2385 2386" vim: shiftwidth=2 sts=2 expandtab 2387