1" Tests for Vim9 script expressions 2 3source check.vim 4source vim9.vim 5 6" test cond ? expr : expr 7def Test_expr1() 8 assert_equal('one', true ? 'one' : 'two') 9 assert_equal('one', 1 ? 10 'one' : 11 'two') 12 if has('float') 13 assert_equal('one', 0.1 ? 'one' : 'two') 14 endif 15 assert_equal('one', 'x' ? 'one' : 'two') 16 assert_equal('one', 'x' 17 ? 'one' 18 : 'two') 19 assert_equal('one', 0z1234 ? 'one' : 'two') 20 assert_equal('one', [0] ? 'one' : 'two') 21 assert_equal('one', #{x: 0} ? 'one' : 'two') 22 let var = 1 23 assert_equal('one', var ? 'one' : 'two') 24 25 assert_equal('two', false ? 'one' : 'two') 26 assert_equal('two', 0 ? 'one' : 'two') 27 if has('float') 28 assert_equal('two', 0.0 ? 'one' : 'two') 29 endif 30 assert_equal('two', '' ? 'one' : 'two') 31 assert_equal('two', 0z ? 'one' : 'two') 32 assert_equal('two', [] ? 'one' : 'two') 33 assert_equal('two', {} ? 'one' : 'two') 34 var = 0 35 assert_equal('two', var ? 'one' : 'two') 36 37 let Some: func = function('len') 38 let Other: func = function('winnr') 39 let Res: func = g:atrue ? Some : Other 40 assert_equal(function('len'), Res) 41 42 let RetOne: func(string): number = function('len') 43 let RetTwo: func(string): number = function('winnr') 44 let RetThat: func = g:atrue ? RetOne : RetTwo 45 assert_equal(function('len'), RetThat) 46enddef 47 48def Test_expr1_vimscript() 49 " only checks line continuation 50 let lines =<< trim END 51 vim9script 52 let var = 1 53 ? 'yes' 54 : 'no' 55 assert_equal('yes', var) 56 END 57 CheckScriptSuccess(lines) 58 59 lines =<< trim END 60 vim9script 61 let var = v:false 62 ? 'yes' 63 : 'no' 64 assert_equal('no', var) 65 END 66 CheckScriptSuccess(lines) 67 68 lines =<< trim END 69 vim9script 70 let var = v:false ? 71 'yes' : 72 'no' 73 assert_equal('no', var) 74 END 75 CheckScriptSuccess(lines) 76enddef 77 78func Test_expr1_fails() 79 call CheckDefFailure(["let x = 1 ? 'one'"], "Missing ':' after '?'") 80 call CheckDefFailure(["let x = 1 ? 'one' : xxx"], "E1001:") 81 82 let msg = "white space required before and after '?'" 83 call CheckDefFailure(["let x = 1? 'one' : 'two'"], msg) 84 call CheckDefFailure(["let x = 1 ?'one' : 'two'"], msg) 85 call CheckDefFailure(["let x = 1?'one' : 'two'"], msg) 86 87 let msg = "white space required before and after ':'" 88 call CheckDefFailure(["let x = 1 ? 'one': 'two'"], msg) 89 call CheckDefFailure(["let x = 1 ? 'one' :'two'"], msg) 90 call CheckDefFailure(["let x = 1 ? 'one':'two'"], msg) 91endfunc 92 93" TODO: define inside test function 94def Record(val: any): any 95 g:vals->add(val) 96 return val 97enddef 98 99" test || 100def Test_expr2() 101 assert_equal(2, 2 || 0) 102 assert_equal(7, 0 || 103 0 || 104 7) 105 assert_equal(0, 0 || 0) 106 assert_equal(0, 0 107 || 0) 108 assert_equal('', 0 || '') 109 110 g:vals = [] 111 assert_equal(3, Record(3) || Record(1)) 112 assert_equal([3], g:vals) 113 114 g:vals = [] 115 assert_equal(5, Record(0) || Record(5)) 116 assert_equal([0, 5], g:vals) 117 118 g:vals = [] 119 assert_equal(4, Record(0) 120 || Record(4) 121 || Record(0)) 122 assert_equal([0, 4], g:vals) 123 124 g:vals = [] 125 assert_equal(0, Record([]) || Record('') || Record(0)) 126 assert_equal([[], '', 0], g:vals) 127enddef 128 129def Test_expr2_vimscript() 130 " only checks line continuation 131 let lines =<< trim END 132 vim9script 133 let var = 0 134 || 1 135 assert_equal(1, var) 136 END 137 CheckScriptSuccess(lines) 138 139 lines =<< trim END 140 vim9script 141 let var = v:false 142 || v:true 143 || v:false 144 assert_equal(1, var) 145 END 146 CheckScriptSuccess(lines) 147 148 lines =<< trim END 149 vim9script 150 let var = v:false || 151 v:true || 152 v:false 153 assert_equal(1, var) 154 END 155 CheckScriptSuccess(lines) 156enddef 157 158func Test_expr2_fails() 159 let msg = "white space required before and after '||'" 160 call CheckDefFailure(["let x = 1||2"], msg) 161 call CheckDefFailure(["let x = 1 ||2"], msg) 162 call CheckDefFailure(["let x = 1|| 2"], msg) 163 164 call CheckDefFailure(["let x = 1 || xxx"], 'E1001:') 165endfunc 166 167" test && 168def Test_expr3() 169 assert_equal(0, 2 && 0) 170 assert_equal(0, 0 && 171 0 && 172 7) 173 assert_equal(7, 2 174 && 3 175 && 7) 176 assert_equal(0, 0 && 0) 177 assert_equal(0, 0 && '') 178 assert_equal('', 8 && '') 179 180 g:vals = [] 181 assert_equal(1, Record(3) && Record(1)) 182 assert_equal([3, 1], g:vals) 183 184 g:vals = [] 185 assert_equal(0, Record(0) && Record(5)) 186 assert_equal([0], g:vals) 187 188 g:vals = [] 189 assert_equal(0, Record(0) && Record(4) && Record(0)) 190 assert_equal([0], g:vals) 191 192 g:vals = [] 193 assert_equal(0, Record(8) && Record(4) && Record(0)) 194 assert_equal([8, 4, 0], g:vals) 195 196 g:vals = [] 197 assert_equal(0, Record([1]) && Record('z') && Record(0)) 198 assert_equal([[1], 'z', 0], g:vals) 199enddef 200 201def Test_expr3_vimscript() 202 " only checks line continuation 203 let lines =<< trim END 204 vim9script 205 let var = 0 206 && 1 207 assert_equal(0, var) 208 END 209 CheckScriptSuccess(lines) 210 211 lines =<< trim END 212 vim9script 213 let var = v:true 214 && v:true 215 && v:true 216 assert_equal(1, var) 217 END 218 CheckScriptSuccess(lines) 219 220 lines =<< trim END 221 vim9script 222 let var = v:true && 223 v:true && 224 v:true 225 assert_equal(1, var) 226 END 227 CheckScriptSuccess(lines) 228enddef 229 230func Test_expr3_fails() 231 let msg = "white space required before and after '&&'" 232 call CheckDefFailure(["let x = 1&&2"], msg) 233 call CheckDefFailure(["let x = 1 &&2"], msg) 234 call CheckDefFailure(["let x = 1&& 2"], msg) 235endfunc 236 237let atrue = v:true 238let afalse = v:false 239let anone = v:none 240let anull = v:null 241let anint = 10 242let alsoint = 4 243if has('float') 244 let afloat = 0.1 245endif 246let astring = 'asdf' 247let ablob = 0z01ab 248let alist = [2, 3, 4] 249let adict = #{aaa: 2, bbb: 8} 250 251" test == comperator 252def Test_expr4_equal() 253 let trueVar = true 254 let falseVar = false 255 assert_equal(true, true == true) 256 assert_equal(false, true == 257 false) 258 assert_equal(true, true 259 == trueVar) 260 assert_equal(false, true == falseVar) 261 assert_equal(true, true == g:atrue) 262 assert_equal(false, g:atrue == false) 263 264 assert_equal(true, v:none == v:none) 265 assert_equal(false, v:none == v:null) 266 assert_equal(true, g:anone == v:none) 267 assert_equal(false, v:none == g:anull) 268 269 let nr0 = 0 270 let nr61 = 61 271 assert_equal(false, 2 == 0) 272 assert_equal(false, 2 == nr0) 273 assert_equal(true, 61 == 61) 274 assert_equal(true, 61 == nr61) 275 assert_equal(true, g:anint == 10) 276 assert_equal(false, 61 == g:anint) 277 278 if has('float') 279 let ff = 0.3 280 assert_equal(true, ff == 0.3) 281 assert_equal(false, 0.4 == ff) 282 assert_equal(true, 0.1 == g:afloat) 283 assert_equal(false, g:afloat == 0.3) 284 285 ff = 3.0 286 assert_equal(true, ff == 3) 287 assert_equal(true, 3 == ff) 288 ff = 3.1 289 assert_equal(false, ff == 3) 290 assert_equal(false, 3 == ff) 291 endif 292 293 assert_equal(true, 'abc' == 'abc') 294 assert_equal(false, 'xyz' == 'abc') 295 assert_equal(true, g:astring == 'asdf') 296 assert_equal(false, 'xyz' == g:astring) 297 298 assert_equal(false, 'abc' == 'aBc') 299 assert_equal(false, 'abc' ==# 'aBc') 300 assert_equal(true, 'abc' ==? 'aBc') 301 302 assert_equal(false, 'abc' == 'ABC') 303 set ignorecase 304 assert_equal(false, 'abc' == 'ABC') 305 assert_equal(false, 'abc' ==# 'ABC') 306 set noignorecase 307 308 call CheckDefFailure(["let x = 'a' == xxx"], 'E1001:') 309 310 let bb = 0z3f 311 assert_equal(true, 0z3f == bb) 312 assert_equal(false, bb == 0z4f) 313 assert_equal(true, g:ablob == 0z01ab) 314 assert_equal(false, 0z3f == g:ablob) 315 316 assert_equal(true, [1, 2, 3] == [1, 2, 3]) 317 assert_equal(false, [1, 2, 3] == [2, 3, 1]) 318 assert_equal(true, [2, 3, 4] == g:alist) 319 assert_equal(false, g:alist == [2, 3, 1]) 320 assert_equal(false, [1, 2, 3] == []) 321 assert_equal(false, [1, 2, 3] == ['1', '2', '3']) 322 323 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2}) 324 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2}) 325 assert_equal(false, #{one: 1, two: 2} == #{two: 2}) 326 assert_equal(false, #{one: 1, two: 2} == #{}) 327 assert_equal(true, g:adict == #{bbb: 8, aaa: 2}) 328 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict) 329 330 assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal')) 331 assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is')) 332 333 assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123])) 334 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123])) 335 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999])) 336 337 let OneFunc: func 338 let TwoFunc: func 339 OneFunc = function('len') 340 TwoFunc = function('len') 341 assert_equal(true, OneFunc('abc') == TwoFunc('123')) 342enddef 343 344" test != comperator 345def Test_expr4_notequal() 346 let trueVar = true 347 let falseVar = false 348 assert_equal(false, true != true) 349 assert_equal(true, true != 350 false) 351 assert_equal(false, true 352 != trueVar) 353 assert_equal(true, true != falseVar) 354 assert_equal(false, true != g:atrue) 355 assert_equal(true, g:atrue != false) 356 357 assert_equal(false, v:none != v:none) 358 assert_equal(true, v:none != v:null) 359 assert_equal(false, g:anone != v:none) 360 assert_equal(true, v:none != g:anull) 361 362 let nr55 = 55 363 let nr0 = 55 364 assert_equal(true, 2 != 0) 365 assert_equal(true, 2 != nr0) 366 assert_equal(false, 55 != 55) 367 assert_equal(false, 55 != nr55) 368 assert_equal(false, g:anint != 10) 369 assert_equal(true, 61 != g:anint) 370 371 if has('float') 372 let ff = 0.3 373 assert_equal(false, 0.3 != ff) 374 assert_equal(true, 0.4 != ff) 375 assert_equal(false, 0.1 != g:afloat) 376 assert_equal(true, g:afloat != 0.3) 377 378 ff = 3.0 379 assert_equal(false, ff != 3) 380 assert_equal(false, 3 != ff) 381 ff = 3.1 382 assert_equal(true, ff != 3) 383 assert_equal(true, 3 != ff) 384 endif 385 386 assert_equal(false, 'abc' != 'abc') 387 assert_equal(true, 'xyz' != 'abc') 388 assert_equal(false, g:astring != 'asdf') 389 assert_equal(true, 'xyz' != g:astring) 390 391 assert_equal(true, 'abc' != 'ABC') 392 set ignorecase 393 assert_equal(true, 'abc' != 'ABC') 394 set noignorecase 395 396 let bb = 0z3f 397 assert_equal(false, 0z3f != bb) 398 assert_equal(true, bb != 0z4f) 399 assert_equal(false, g:ablob != 0z01ab) 400 assert_equal(true, 0z3f != g:ablob) 401 402 assert_equal(false, [1, 2, 3] != [1, 2, 3]) 403 assert_equal(true, [1, 2, 3] != [2, 3, 1]) 404 assert_equal(false, [2, 3, 4] != g:alist) 405 assert_equal(true, g:alist != [2, 3, 1]) 406 assert_equal(true, [1, 2, 3] != []) 407 assert_equal(true, [1, 2, 3] != ['1', '2', '3']) 408 409 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2}) 410 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2}) 411 assert_equal(true, #{one: 1, two: 2} != #{two: 2}) 412 assert_equal(true, #{one: 1, two: 2} != #{}) 413 assert_equal(false, g:adict != #{bbb: 8, aaa: 2}) 414 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict) 415 416 assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal')) 417 assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is')) 418 419 assert_equal(false, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [123])) 420 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_is', [123])) 421 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [999])) 422enddef 423 424" test > comperator 425def Test_expr4_greater() 426 assert_true(2 > 0) 427 assert_true(2 > 428 1) 429 assert_false(2 > 2) 430 assert_false(2 > 3) 431 let nr2 = 2 432 assert_true(nr2 > 0) 433 assert_true(nr2 > 434 1) 435 assert_false(nr2 > 2) 436 assert_false(nr2 437 > 3) 438 if has('float') 439 let ff = 2.0 440 assert_true(ff > 0.0) 441 assert_true(ff > 1.0) 442 assert_false(ff > 2.0) 443 assert_false(ff > 3.0) 444 endif 445enddef 446 447" test >= comperator 448def Test_expr4_greaterequal() 449 assert_true(2 >= 0) 450 assert_true(2 >= 451 2) 452 assert_false(2 >= 3) 453 let nr2 = 2 454 assert_true(nr2 >= 0) 455 assert_true(nr2 >= 2) 456 assert_false(nr2 >= 3) 457 if has('float') 458 let ff = 2.0 459 assert_true(ff >= 0.0) 460 assert_true(ff >= 2.0) 461 assert_false(ff >= 3.0) 462 endif 463enddef 464 465" test < comperator 466def Test_expr4_smaller() 467 assert_false(2 < 0) 468 assert_false(2 < 469 2) 470 assert_true(2 471 < 3) 472 let nr2 = 2 473 assert_false(nr2 < 0) 474 assert_false(nr2 < 2) 475 assert_true(nr2 < 3) 476 if has('float') 477 let ff = 2.0 478 assert_false(ff < 0.0) 479 assert_false(ff < 2.0) 480 assert_true(ff < 3.0) 481 endif 482enddef 483 484" test <= comperator 485def Test_expr4_smallerequal() 486 assert_false(2 <= 0) 487 assert_false(2 <= 488 1) 489 assert_true(2 490 <= 2) 491 assert_true(2 <= 3) 492 let nr2 = 2 493 assert_false(nr2 <= 0) 494 assert_false(nr2 <= 1) 495 assert_true(nr2 <= 2) 496 assert_true(nr2 <= 3) 497 if has('float') 498 let ff = 2.0 499 assert_false(ff <= 0.0) 500 assert_false(ff <= 1.0) 501 assert_true(ff <= 2.0) 502 assert_true(ff <= 3.0) 503 endif 504enddef 505 506" test =~ comperator 507def Test_expr4_match() 508 assert_equal(false, '2' =~ '0') 509 assert_equal(false, '' 510 =~ '0') 511 assert_equal(true, '2' =~ 512 '[0-9]') 513enddef 514 515" test !~ comperator 516def Test_expr4_nomatch() 517 assert_equal(true, '2' !~ '0') 518 assert_equal(true, '' 519 !~ '0') 520 assert_equal(false, '2' !~ 521 '[0-9]') 522enddef 523 524" test is comperator 525def Test_expr4_is() 526 let mylist = [2] 527 assert_false(mylist is [2]) 528 let other = mylist 529 assert_true(mylist is 530 other) 531 532 let myblob = 0z1234 533 assert_false(myblob 534 is 0z1234) 535 let otherblob = myblob 536 assert_true(myblob is otherblob) 537enddef 538 539" test isnot comperator 540def Test_expr4_isnot() 541 let mylist = [2] 542 assert_true('2' isnot '0') 543 assert_true(mylist isnot [2]) 544 let other = mylist 545 assert_false(mylist isnot 546 other) 547 548 let myblob = 0z1234 549 assert_true(myblob 550 isnot 0z1234) 551 let otherblob = myblob 552 assert_false(myblob isnot otherblob) 553enddef 554 555def RetVoid() 556 let x = 1 557enddef 558 559def Test_expr4_vimscript() 560 " only checks line continuation 561 let lines =<< trim END 562 vim9script 563 let var = 0 564 < 1 565 assert_equal(1, var) 566 END 567 CheckScriptSuccess(lines) 568 569 lines =<< trim END 570 vim9script 571 let var = 123 572 != 123 573 assert_equal(0, var) 574 END 575 CheckScriptSuccess(lines) 576 577 lines =<< trim END 578 vim9script 579 let var = 123 == 580 123 581 assert_equal(1, var) 582 END 583 CheckScriptSuccess(lines) 584 585 lines =<< trim END 586 vim9script 587 let list = [1, 2, 3] 588 let var = list 589 is list 590 assert_equal(1, var) 591 END 592 CheckScriptSuccess(lines) 593 594 lines =<< trim END 595 vim9script 596 let myblob = 0z1234 597 let var = myblob 598 isnot 0z11 599 assert_equal(1, var) 600 END 601 CheckScriptSuccess(lines) 602enddef 603 604func Test_expr4_fails() 605 let msg = "white space required before and after '>'" 606 call CheckDefFailure(["let x = 1>2"], msg) 607 call CheckDefFailure(["let x = 1 >2"], msg) 608 call CheckDefFailure(["let x = 1> 2"], msg) 609 610 let msg = "white space required before and after '=='" 611 call CheckDefFailure(["let x = 1==2"], msg) 612 call CheckDefFailure(["let x = 1 ==2"], msg) 613 call CheckDefFailure(["let x = 1== 2"], msg) 614 615 let msg = "white space required before and after 'is'" 616 call CheckDefFailure(["let x = '1'is'2'"], msg) 617 call CheckDefFailure(["let x = '1' is'2'"], msg) 618 call CheckDefFailure(["let x = '1'is '2'"], msg) 619 620 let msg = "white space required before and after 'isnot'" 621 call CheckDefFailure(["let x = '1'isnot'2'"], msg) 622 call CheckDefFailure(["let x = '1' isnot'2'"], msg) 623 call CheckDefFailure(["let x = '1'isnot '2'"], msg) 624 625 call CheckDefFailure(["let x = 1 is# 2"], 'E15:') 626 call CheckDefFailure(["let x = 1 is? 2"], 'E15:') 627 call CheckDefFailure(["let x = 1 isnot# 2"], 'E15:') 628 call CheckDefFailure(["let x = 1 isnot? 2"], 'E15:') 629 630 call CheckDefFailure(["let x = 1 == '2'"], 'Cannot compare number with string') 631 call CheckDefFailure(["let x = '1' == 2"], 'Cannot compare string with number') 632 call CheckDefFailure(["let x = 1 == RetVoid()"], 'Cannot compare number with void') 633 call CheckDefFailure(["let x = RetVoid() == 1"], 'Cannot compare void with number') 634 635 call CheckDefFailure(["let x = true > false"], 'Cannot compare bool with bool') 636 call CheckDefFailure(["let x = true >= false"], 'Cannot compare bool with bool') 637 call CheckDefFailure(["let x = true < false"], 'Cannot compare bool with bool') 638 call CheckDefFailure(["let x = true <= false"], 'Cannot compare bool with bool') 639 call CheckDefFailure(["let x = true =~ false"], 'Cannot compare bool with bool') 640 call CheckDefFailure(["let x = true !~ false"], 'Cannot compare bool with bool') 641 call CheckDefFailure(["let x = true is false"], 'Cannot use "is" with bool') 642 call CheckDefFailure(["let x = true isnot false"], 'Cannot use "isnot" with bool') 643 644 call CheckDefFailure(["let x = v:none is v:null"], 'Cannot use "is" with special') 645 call CheckDefFailure(["let x = v:none isnot v:null"], 'Cannot use "isnot" with special') 646 call CheckDefFailure(["let x = 123 is 123"], 'Cannot use "is" with number') 647 call CheckDefFailure(["let x = 123 isnot 123"], 'Cannot use "isnot" with number') 648 if has('float') 649 call CheckDefFailure(["let x = 1.3 is 1.3"], 'Cannot use "is" with float') 650 call CheckDefFailure(["let x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float') 651 endif 652 653 call CheckDefFailure(["let x = 0za1 > 0z34"], 'Cannot compare blob with blob') 654 call CheckDefFailure(["let x = 0za1 >= 0z34"], 'Cannot compare blob with blob') 655 call CheckDefFailure(["let x = 0za1 < 0z34"], 'Cannot compare blob with blob') 656 call CheckDefFailure(["let x = 0za1 <= 0z34"], 'Cannot compare blob with blob') 657 call CheckDefFailure(["let x = 0za1 =~ 0z34"], 'Cannot compare blob with blob') 658 call CheckDefFailure(["let x = 0za1 !~ 0z34"], 'Cannot compare blob with blob') 659 660 call CheckDefFailure(["let x = [13] > [88]"], 'Cannot compare list with list') 661 call CheckDefFailure(["let x = [13] >= [88]"], 'Cannot compare list with list') 662 call CheckDefFailure(["let x = [13] < [88]"], 'Cannot compare list with list') 663 call CheckDefFailure(["let x = [13] <= [88]"], 'Cannot compare list with list') 664 call CheckDefFailure(["let x = [13] =~ [88]"], 'Cannot compare list with list') 665 call CheckDefFailure(["let x = [13] !~ [88]"], 'Cannot compare list with list') 666 667 call CheckDefFailure(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel') 668 call CheckDefFailure(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list') 669 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') 670 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') 671endfunc 672 673" test addition, subtraction, concatenation 674def Test_expr5() 675 assert_equal(66, 60 + 6) 676 assert_equal(70, 60 + 677 g:anint) 678 assert_equal(9, g:alsoint 679 + 5) 680 assert_equal(14, g:alsoint + g:anint) 681 assert_equal([1, 2, 3, 4], [1] + g:alist) 682 683 assert_equal(54, 60 - 6) 684 assert_equal(50, 60 - 685 g:anint) 686 assert_equal(-1, g:alsoint 687 - 5) 688 assert_equal(-6, g:alsoint - g:anint) 689 690 assert_equal('hello', 'hel' .. 'lo') 691 assert_equal('hello 123', 'hello ' .. 692 123) 693 assert_equal('hello 123', 'hello ' 694 .. 123) 695 assert_equal('123 hello', 123 .. ' hello') 696 assert_equal('123456', 123 .. 456) 697 698 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) 699 assert_equal(0z11223344, 0z1122 + 0z3344) 700 assert_equal(0z112201ab, 0z1122 701 + g:ablob) 702 assert_equal(0z01ab3344, g:ablob + 0z3344) 703 assert_equal(0z01ab01ab, g:ablob + g:ablob) 704enddef 705 706def Test_expr5_vim9script() 707 " only checks line continuation 708 let lines =<< trim END 709 vim9script 710 let var = 11 711 + 77 712 - 22 713 assert_equal(66, var) 714 END 715 CheckScriptSuccess(lines) 716 717 lines =<< trim END 718 vim9script 719 let var = 'one' 720 .. 'two' 721 assert_equal('onetwo', var) 722 END 723 CheckScriptSuccess(lines) 724enddef 725 726def Test_expr5_float() 727 if !has('float') 728 MissingFeature 'float' 729 else 730 assert_equal(66.0, 60.0 + 6.0) 731 assert_equal(66.0, 60.0 + 6) 732 assert_equal(66.0, 60 + 733 6.0) 734 assert_equal(5.1, g:afloat 735 + 5) 736 assert_equal(8.1, 8 + g:afloat) 737 assert_equal(10.1, g:anint + g:afloat) 738 assert_equal(10.1, g:afloat + g:anint) 739 740 assert_equal(54.0, 60.0 - 6.0) 741 assert_equal(54.0, 60.0 742 - 6) 743 assert_equal(54.0, 60 - 6.0) 744 assert_equal(-4.9, g:afloat - 5) 745 assert_equal(7.9, 8 - g:afloat) 746 assert_equal(9.9, g:anint - g:afloat) 747 assert_equal(-9.9, g:afloat - g:anint) 748 endif 749enddef 750 751func Test_expr5_fails() 752 let msg = "white space required before and after '+'" 753 call CheckDefFailure(["let x = 1+2"], msg) 754 call CheckDefFailure(["let x = 1 +2"], msg) 755 call CheckDefFailure(["let x = 1+ 2"], msg) 756 757 let msg = "white space required before and after '-'" 758 call CheckDefFailure(["let x = 1-2"], msg) 759 call CheckDefFailure(["let x = 1 -2"], msg) 760 call CheckDefFailure(["let x = 1- 2"], msg) 761 762 let msg = "white space required before and after '..'" 763 call CheckDefFailure(["let x = '1'..'2'"], msg) 764 call CheckDefFailure(["let x = '1' ..'2'"], msg) 765 call CheckDefFailure(["let x = '1'.. '2'"], msg) 766 767 call CheckDefFailure(["let x = 0z1122 + 33"], 'E1051') 768 call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1051') 769 call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1051') 770 call CheckDefFailure(["let x = 33 + 0z1122"], 'E1051') 771 call CheckDefFailure(["let x = [3] + 0z1122"], 'E1051') 772 call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1051') 773 call CheckDefFailure(["let x = 6 + xxx"], 'E1001') 774endfunc 775 776" test multiply, divide, modulo 777def Test_expr6() 778 assert_equal(36, 6 * 6) 779 assert_equal(24, 6 * 780 g:alsoint) 781 assert_equal(24, g:alsoint 782 * 6) 783 assert_equal(40, g:anint * g:alsoint) 784 785 assert_equal(10, 60 / 6) 786 assert_equal(6, 60 / 787 g:anint) 788 assert_equal(1, g:anint / 6) 789 assert_equal(2, g:anint 790 / g:alsoint) 791 792 assert_equal(5, 11 % 6) 793 assert_equal(4, g:anint % 6) 794 assert_equal(3, 13 % 795 g:anint) 796 assert_equal(2, g:anint 797 % g:alsoint) 798 799 assert_equal(4, 6 * 4 / 6) 800 801 let x = [2] 802 let y = [3] 803 assert_equal(5, x[0] + y[0]) 804 assert_equal(6, x[0] * y[0]) 805 if has('float') 806 let xf = [2.0] 807 let yf = [3.0] 808 assert_equal(5.0, xf[0] 809 + yf[0]) 810 assert_equal(6.0, xf[0] 811 * yf[0]) 812 endif 813 814 call CheckDefFailure(["let x = 6 * xxx"], 'E1001') 815enddef 816 817def Test_expr6_vim9script() 818 " only checks line continuation 819 let lines =<< trim END 820 vim9script 821 let var = 11 822 * 22 823 / 3 824 assert_equal(80, var) 825 END 826 CheckScriptSuccess(lines) 827 828 lines =<< trim END 829 vim9script 830 let var = 25 831 % 10 832 assert_equal(5, var) 833 END 834 CheckScriptSuccess(lines) 835enddef 836 837def Test_expr6_float() 838 if !has('float') 839 MissingFeature 'float' 840 else 841 assert_equal(36.0, 6.0 * 6) 842 assert_equal(36.0, 6 * 843 6.0) 844 assert_equal(36.0, 6.0 * 6.0) 845 assert_equal(1.0, g:afloat * g:anint) 846 847 assert_equal(10.0, 60 / 6.0) 848 assert_equal(10.0, 60.0 / 849 6) 850 assert_equal(10.0, 60.0 / 6.0) 851 assert_equal(0.01, g:afloat / g:anint) 852 853 assert_equal(4.0, 6.0 * 4 / 6) 854 assert_equal(4.0, 6 * 855 4.0 / 856 6) 857 assert_equal(4.0, 6 * 4 / 6.0) 858 assert_equal(4.0, 6.0 * 4.0 / 6) 859 assert_equal(4.0, 6 * 4.0 / 6.0) 860 assert_equal(4.0, 6.0 * 4 / 6.0) 861 assert_equal(4.0, 6.0 * 4.0 / 6.0) 862 863 assert_equal(4.0, 6.0 * 4.0 / 6.0) 864 endif 865enddef 866 867func Test_expr6_fails() 868 let msg = "white space required before and after '*'" 869 call CheckDefFailure(["let x = 1*2"], msg) 870 call CheckDefFailure(["let x = 1 *2"], msg) 871 call CheckDefFailure(["let x = 1* 2"], msg) 872 873 let msg = "white space required before and after '/'" 874 call CheckDefFailure(["let x = 1/2"], msg) 875 call CheckDefFailure(["let x = 1 /2"], msg) 876 call CheckDefFailure(["let x = 1/ 2"], msg) 877 878 let msg = "white space required before and after '%'" 879 call CheckDefFailure(["let x = 1%2"], msg) 880 call CheckDefFailure(["let x = 1 %2"], msg) 881 call CheckDefFailure(["let x = 1% 2"], msg) 882 883 call CheckDefFailure(["let x = '1' * '2'"], 'E1036:') 884 call CheckDefFailure(["let x = '1' / '2'"], 'E1036:') 885 call CheckDefFailure(["let x = '1' % '2'"], 'E1035:') 886 887 call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:') 888 call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:') 889 call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:') 890 891 call CheckDefFailure(["let x = [1] * [2]"], 'E1036:') 892 call CheckDefFailure(["let x = [1] / [2]"], 'E1036:') 893 call CheckDefFailure(["let x = [1] % [2]"], 'E1035:') 894 895 call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:') 896 call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:') 897 call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:') 898 899 call CheckDefFailure(["let x = 0xff[1]"], 'E1090:') 900 if has('float') 901 call CheckDefFailure(["let x = 0.7[1]"], 'E1090:') 902 endif 903endfunc 904 905func Test_expr6_float_fails() 906 CheckFeature float 907 call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:') 908endfunc 909 910" define here to use old style parsing 911if has('float') 912 let g:float_zero = 0.0 913 let g:float_neg = -9.8 914 let g:float_big = 9.9e99 915endif 916let g:blob_empty = 0z 917let g:blob_one = 0z01 918let g:blob_long = 0z0102.0304 919 920let g:string_empty = '' 921let g:string_short = 'x' 922let g:string_long = 'abcdefghijklm' 923let g:string_special = "ab\ncd\ref\ekk" 924 925let g:special_true = v:true 926let g:special_false = v:false 927let g:special_null = v:null 928let g:special_none = v:none 929 930let g:list_empty = [] 931let g:list_mixed = [1, 'b', v:false] 932 933let g:dict_empty = {} 934let g:dict_one = #{one: 1} 935 936let $TESTVAR = 'testvar' 937 938" test low level expression 939def Test_expr7_number() 940 " number constant 941 assert_equal(0, 0) 942 assert_equal(654, 0654) 943 944 assert_equal(6, 0x6) 945 assert_equal(15, 0xf) 946 assert_equal(255, 0xff) 947enddef 948 949def Test_expr7_float() 950 " float constant 951 if !has('float') 952 MissingFeature 'float' 953 else 954 assert_equal(g:float_zero, .0) 955 assert_equal(g:float_zero, 0.0) 956 assert_equal(g:float_neg, -9.8) 957 assert_equal(g:float_big, 9.9e99) 958 endif 959enddef 960 961def Test_expr7_blob() 962 " blob constant 963 assert_equal(g:blob_empty, 0z) 964 assert_equal(g:blob_one, 0z01) 965 assert_equal(g:blob_long, 0z0102.0304) 966 967 call CheckDefFailure(["let x = 0z123"], 'E973:') 968enddef 969 970def Test_expr7_string() 971 " string constant 972 assert_equal(g:string_empty, '') 973 assert_equal(g:string_empty, "") 974 assert_equal(g:string_short, 'x') 975 assert_equal(g:string_short, "x") 976 assert_equal(g:string_long, 'abcdefghijklm') 977 assert_equal(g:string_long, "abcdefghijklm") 978 assert_equal(g:string_special, "ab\ncd\ref\ekk") 979 980 call CheckDefFailure(['let x = "abc'], 'E114:') 981 call CheckDefFailure(["let x = 'abc"], 'E115:') 982enddef 983 984def Test_expr7_vimvar() 985 let old: list<string> = v:oldfiles 986 let compl: dict<any> = v:completed_item 987 988 call CheckDefFailure(["let old: list<number> = v:oldfiles"], 'E1013: type mismatch, expected list<number> but got list<string>') 989 call CheckDefFailure(["let old: dict<number> = v:completed_item"], 'E1013: type mismatch, expected dict<number> but got dict<any>') 990enddef 991 992def Test_expr7_special() 993 " special constant 994 assert_equal(g:special_true, true) 995 assert_equal(g:special_false, false) 996 assert_equal(g:special_true, v:true) 997 assert_equal(g:special_false, v:false) 998 assert_equal(g:special_null, v:null) 999 assert_equal(g:special_none, v:none) 1000 1001 call CheckDefFailure(['v:true = true'], 'E46:') 1002 call CheckDefFailure(['v:true = false'], 'E46:') 1003 call CheckDefFailure(['v:false = true'], 'E46:') 1004 call CheckDefFailure(['v:null = 11'], 'E46:') 1005 call CheckDefFailure(['v:none = 22'], 'E46:') 1006enddef 1007 1008def Test_expr7_list() 1009 " list 1010 assert_equal(g:list_empty, []) 1011 assert_equal(g:list_empty, [ ]) 1012 assert_equal(g:list_mixed, [1, 'b', false,]) 1013 assert_equal('b', g:list_mixed[1]) 1014 1015 call CheckDefExecFailure(["let x = g:anint[3]"], 'E714:') 1016 call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:') 1017 call CheckDefFailure(["let x = [1,2,3]"], 'E1069:') 1018 call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E39:') 1019 call CheckDefFailure(["let x = g:list_mixed["], 'E1097:') 1020 call CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:') 1021 call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:') 1022enddef 1023 1024def Test_expr7_list_vim9script() 1025 let lines =<< trim END 1026 vim9script 1027 let l = [ 1028 11, 1029 22, 1030 ] 1031 assert_equal([11, 22], l) 1032 END 1033 CheckScriptSuccess(lines) 1034 1035 lines =<< trim END 1036 vim9script 1037 let l = [11, 1038 22] 1039 assert_equal([11, 22], l) 1040 END 1041 CheckScriptSuccess(lines) 1042 1043 lines =<< trim END 1044 vim9script 1045 let l = [11,22] 1046 END 1047 CheckScriptFailure(lines, 'E1069:') 1048enddef 1049 1050def Test_expr7_lambda() 1051 " lambda 1052 let La = { -> 'result'} 1053 assert_equal('result', La()) 1054 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) 1055enddef 1056 1057def Test_expr7_lambda_vim9script() 1058 let lines =<< trim END 1059 vim9script 1060 let v = 10->{a -> 1061 a 1062 + 2 1063 }() 1064 assert_equal(12, v) 1065 END 1066 CheckScriptSuccess(lines) 1067enddef 1068 1069def Test_expr7_dict() 1070 " dictionary 1071 assert_equal(g:dict_empty, {}) 1072 assert_equal(g:dict_empty, { }) 1073 assert_equal(g:dict_one, {'one': 1}) 1074 let key = 'one' 1075 let val = 1 1076 assert_equal(g:dict_one, {key: val}) 1077 1078 call CheckDefFailure(["let x = #{8: 8}"], 'E1014:') 1079 call CheckDefFailure(["let x = #{xxx}"], 'E720:') 1080 call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:') 1081 call CheckDefFailure(["let x = #{xxx: 1,"], 'E723:') 1082 call CheckDefFailure(["let x = {'a': xxx}"], 'E1001:') 1083 call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:') 1084 call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:') 1085 call CheckDefFailure(["let x = #"], 'E1015:') 1086 call CheckDefFailure(["let x += 1"], 'E1020:') 1087 call CheckDefFailure(["let x = x + 1"], 'E1001:') 1088 call CheckDefExecFailure(["let x = g:anint.member"], 'E715:') 1089 call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:') 1090enddef 1091 1092def Test_expr7_dict_vim9script() 1093 let lines =<< trim END 1094 vim9script 1095 let d = { 1096 'one': 1097 1, 1098 'two': 2, 1099 } 1100 assert_equal({'one': 1, 'two': 2}, d) 1101 END 1102 CheckScriptSuccess(lines) 1103 1104 lines =<< trim END 1105 vim9script 1106 let d = { "one": "one", "two": "two", } 1107 assert_equal({'one': 'one', 'two': 'two'}, d) 1108 END 1109 CheckScriptSuccess(lines) 1110 1111 lines =<< trim END 1112 vim9script 1113 let d = #{one: 1, 1114 two: 2, 1115 } 1116 assert_equal({'one': 1, 'two': 2}, d) 1117 END 1118 CheckScriptSuccess(lines) 1119 1120 lines =<< trim END 1121 vim9script 1122 let d = #{one:1, two: 2} 1123 END 1124 CheckScriptFailure(lines, 'E1069:') 1125 1126 lines =<< trim END 1127 vim9script 1128 let d = #{one: 1,two: 2} 1129 END 1130 CheckScriptFailure(lines, 'E1069:') 1131enddef 1132 1133let g:oneString = 'one' 1134 1135def Test_expr_member() 1136 assert_equal(1, g:dict_one.one) 1137 let d: dict<number> = g:dict_one 1138 assert_equal(1, d['one']) 1139 assert_equal(1, d[ 1140 'one' 1141 ]) 1142 assert_equal(1, d 1143 .one) 1144 1145 # getting the one member should clear the dict after getting the item 1146 assert_equal('one', #{one: 'one'}.one) 1147 assert_equal('one', #{one: 'one'}[g:oneString]) 1148 1149 call CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:') 1150 call CheckDefExecFailure(["let d: dict<any>", "echo d['a']"], 'E716:') 1151 call CheckDefExecFailure(["let d: dict<number>", "d = g:list_empty"], 'E1029: Expected dict but got list') 1152enddef 1153 1154def Test_expr_index() 1155 # getting the one member should clear the list only after getting the item 1156 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 1157enddef 1158 1159def Test_expr_member_vim9script() 1160 let lines =<< trim END 1161 vim9script 1162 let d = #{one: 1163 'one', 1164 two: 'two'} 1165 assert_equal('one', d.one) 1166 assert_equal('one', d 1167 .one) 1168 assert_equal('one', d[ 1169 'one' 1170 ]) 1171 END 1172 CheckScriptSuccess(lines) 1173 1174 lines =<< trim END 1175 vim9script 1176 let l = [1, 1177 2, 1178 3, 4 1179 ] 1180 assert_equal(2, l[ 1181 1 1182 ]) 1183 assert_equal([2, 3], l[1 : 2]) 1184 assert_equal([1, 2, 3], l[ 1185 : 1186 2 1187 ]) 1188 assert_equal([3, 4], l[ 1189 2 1190 : 1191 ]) 1192 END 1193 CheckScriptSuccess(lines) 1194enddef 1195 1196def Test_expr7_option() 1197 " option 1198 set ts=11 1199 assert_equal(11, &ts) 1200 &ts = 9 1201 assert_equal(9, &ts) 1202 set ts=8 1203 set grepprg=some\ text 1204 assert_equal('some text', &grepprg) 1205 &grepprg = test_null_string() 1206 assert_equal('', &grepprg) 1207 set grepprg& 1208enddef 1209 1210def Test_expr7_environment() 1211 " environment variable 1212 assert_equal('testvar', $TESTVAR) 1213 assert_equal('', $ASDF_ASD_XXX) 1214 1215 call CheckDefFailure(["let x = $$$"], 'E1002:') 1216enddef 1217 1218def Test_expr7_register() 1219 @a = 'register a' 1220 assert_equal('register a', @a) 1221enddef 1222 1223def Test_expr7_parens() 1224 " (expr) 1225 assert_equal(4, (6 * 4) / 6) 1226 assert_equal(0, 6 * ( 4 / 6 )) 1227 1228 assert_equal(6, +6) 1229 assert_equal(-6, -6) 1230 assert_equal(6, --6) 1231 assert_equal(6, -+-6) 1232 assert_equal(-6, ---6) 1233 assert_equal(false, !-3) 1234 assert_equal(true, !+-+0) 1235enddef 1236 1237def Test_expr7_parens_vim9script() 1238 let lines =<< trim END 1239 vim9script 1240 let s = ( 1241 'one' 1242 .. 1243 'two' 1244 ) 1245 assert_equal('onetwo', s) 1246 END 1247 CheckScriptSuccess(lines) 1248enddef 1249 1250def Test_expr7_negate() 1251 assert_equal(-99, -99) 1252 assert_equal(99, --99) 1253 let nr = 88 1254 assert_equal(-88, -nr) 1255 assert_equal(88, --nr) 1256enddef 1257 1258def Echo(arg: any): string 1259 return arg 1260enddef 1261 1262def s:EchoArg(arg: any): string 1263 return arg 1264enddef 1265 1266def Test_expr7_call() 1267 assert_equal('yes', 'yes'->Echo()) 1268 assert_equal('yes', 'yes'->s:EchoArg()) 1269 assert_equal(1, !range(5)->empty()) 1270 assert_equal([0, 1, 2], --3->range()) 1271 1272 call CheckDefFailure(["let x = 'yes'->Echo"], 'E107:') 1273enddef 1274 1275 1276def Test_expr7_not() 1277 assert_equal(true, !'') 1278 assert_equal(true, ![]) 1279 assert_equal(false, !'asdf') 1280 assert_equal(false, ![2]) 1281 assert_equal(true, !!'asdf') 1282 assert_equal(true, !![2]) 1283 1284 assert_equal(true, !test_null_partial()) 1285 assert_equal(false, !{-> 'yes'}) 1286 1287 assert_equal(true, !test_null_dict()) 1288 assert_equal(true, !{}) 1289 assert_equal(false, !{'yes': 'no'}) 1290 1291 if has('channel') 1292 assert_equal(true, !test_null_job()) 1293 assert_equal(true, !test_null_channel()) 1294 endif 1295 1296 assert_equal(true, !test_null_blob()) 1297 assert_equal(true, !0z) 1298 assert_equal(false, !0z01) 1299 1300 assert_equal(true, !test_void()) 1301 assert_equal(true, !test_unknown()) 1302enddef 1303 1304func Test_expr7_fails() 1305 call CheckDefFailure(["let x = (12"], "E110:") 1306 1307 call CheckDefFailure(["let x = -'xx'"], "E1030:") 1308 call CheckDefFailure(["let x = +'xx'"], "E1030:") 1309 call CheckDefFailure(["let x = -0z12"], "E974:") 1310 call CheckDefExecFailure(["let x = -[8]"], "E39:") 1311 call CheckDefExecFailure(["let x = -{'a': 1}"], "E39:") 1312 1313 call CheckDefFailure(["let x = @"], "E1002:") 1314 call CheckDefFailure(["let x = @<"], "E354:") 1315 1316 call CheckDefFailure(["let x = [1, 2"], "E697:") 1317 call CheckDefFailure(["let x = [notfound]"], "E1001:") 1318 1319 call CheckDefFailure(["let x = { -> 123) }"], "E451:") 1320 call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:") 1321 1322 call CheckDefFailure(["let x = ¬exist"], 'E113:') 1323 call CheckDefFailure(["&grepprg = [343]"], 'E1013:') 1324 1325 call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:') 1326 call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:') 1327 1328 call CheckDefFailure(["echo a:somevar"], 'E1075:') 1329 call CheckDefFailure(["echo l:somevar"], 'E1075:') 1330 call CheckDefFailure(["echo x:somevar"], 'E1075:') 1331 1332 call CheckDefExecFailure(["let x = +g:astring"], 'E1030:') 1333 call CheckDefExecFailure(["let x = +g:ablob"], 'E974:') 1334 call CheckDefExecFailure(["let x = +g:alist"], 'E745:') 1335 call CheckDefExecFailure(["let x = +g:adict"], 'E728:') 1336 1337 call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:') 1338 1339 call CheckDefExecFailure(["[1, 2->len()"], 'E697:') 1340 call CheckDefExecFailure(["#{a: 1->len()"], 'E488:') 1341 call CheckDefExecFailure(["{'a': 1->len()"], 'E723:') 1342endfunc 1343 1344let g:Funcrefs = [function('add')] 1345 1346func CallMe(arg) 1347 return a:arg 1348endfunc 1349 1350func CallMe2(one, two) 1351 return a:one .. a:two 1352endfunc 1353 1354def Test_expr7_trailing() 1355 " user function call 1356 assert_equal(123, g:CallMe(123)) 1357 assert_equal(123, g:CallMe( 123)) 1358 assert_equal(123, g:CallMe(123 )) 1359 assert_equal('yesno', g:CallMe2('yes', 'no')) 1360 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 1361 assert_equal('nothing', g:CallMe('nothing')) 1362 1363 " partial call 1364 let Part = function('g:CallMe') 1365 assert_equal('yes', Part('yes')) 1366 1367 " funcref call, using list index 1368 let l = [] 1369 g:Funcrefs[0](l, 2) 1370 assert_equal([2], l) 1371 1372 " method call 1373 l = [2, 5, 6] 1374 l->map({k, v -> k + v}) 1375 assert_equal([2, 6, 8], l) 1376 1377 " lambda method call 1378 l = [2, 5] 1379 l->{l -> add(l, 8)}() 1380 assert_equal([2, 5, 8], l) 1381 1382 " dict member 1383 let d = #{key: 123} 1384 assert_equal(123, d.key) 1385enddef 1386 1387def Test_expr7_subscript_linebreak() 1388 let range = range( 1389 3) 1390 let l = range-> 1391 map('string(v:key)') 1392 assert_equal(['0', '1', '2'], l) 1393 1394 l = range 1395 ->map('string(v:key)') 1396 assert_equal(['0', '1', '2'], l) 1397 1398 l = range # comment 1399 ->map('string(v:key)') 1400 assert_equal(['0', '1', '2'], l) 1401 1402 l = range 1403 1404 ->map('string(v:key)') 1405 assert_equal(['0', '1', '2'], l) 1406 1407 l = range 1408 # comment 1409 ->map('string(v:key)') 1410 assert_equal(['0', '1', '2'], l) 1411 1412 assert_equal('1', l[ 1413 1]) 1414 1415 let d = #{one: 33} 1416 assert_equal(33, d. 1417 one) 1418enddef 1419 1420def Test_expr7_method_call() 1421 new 1422 setline(1, ['first', 'last']) 1423 eval 'second'->append(1) 1424 assert_equal(['first', 'second', 'last'], getline(1, '$')) 1425 bwipe! 1426 1427 let bufnr = bufnr() 1428 let loclist = [#{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 1429 loclist->setloclist(0) 1430 assert_equal([#{bufnr: bufnr, 1431 lnum: 42, 1432 col: 17, 1433 text: 'wrong', 1434 pattern: '', 1435 valid: 1, 1436 vcol: 0, 1437 nr: 0, 1438 type: '', 1439 module: ''} 1440 ], getloclist(0)) 1441enddef 1442 1443func Test_expr7_trailing_fails() 1444 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107') 1445 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274') 1446endfunc 1447 1448func Test_expr_fails() 1449 call CheckDefFailure(["let x = '1'is2"], 'E488:') 1450 call CheckDefFailure(["let x = '1'isnot2"], 'E488:') 1451 1452 call CheckDefFailure(["CallMe ('yes')"], 'E476:') 1453 call CheckDefFailure(["CallMe2('yes','no')"], 'E1069:') 1454 call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:') 1455 1456 call CheckDefFailure(["v:nosuch += 3"], 'E1001:') 1457 call CheckDefFailure(["let v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:') 1458 call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:') 1459 1460 call CheckDefFailure(["echo len('asdf'"], 'E110:') 1461 call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:') 1462 call CheckDefFailure(["echo doesnotexist()"], 'E117:') 1463endfunc 1464