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