1" Tests for Vim9 script expressions 2 3source check.vim 4source vim9.vim 5 6let g:cond = v:false 7def FuncOne(arg: number): string 8 return 'yes' 9enddef 10def FuncTwo(arg: number): number 11 return 123 12enddef 13 14" test cond ? expr : expr 15def Test_expr1_trinary() 16 var lines =<< trim END 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 var name = 1 32 assert_equal('one', name ? '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 name = 0 44 assert_equal('two', name ? 'one' : 'two') 45 46 echo ['a'] + (1 ? ['b'] : ['c'] 47 ) 48 echo ['a'] + (1 ? ['b'] : ['c'] # comment 49 ) 50 51 # with constant condition expression is not evaluated 52 assert_equal('one', 1 ? 'one' : xxx) 53 54 var Some: func = function('len') 55 var Other: func = function('winnr') 56 var Res: func = g:atrue ? Some : Other 57 assert_equal(function('len'), Res) 58 59 var RetOne: func(string): number = function('len') 60 var RetTwo: func(string): number = function('charcol') 61 var RetThat: func = g:atrue ? RetOne : RetTwo 62 assert_equal(function('len'), RetThat) 63 64 var X = FuncOne 65 var Y = FuncTwo 66 var Z = g:cond ? FuncOne : FuncTwo 67 assert_equal(123, Z(3)) 68 END 69 CheckDefAndScriptSuccess(lines) 70enddef 71 72def Test_expr1_trinary_vimscript() 73 # check line continuation 74 var lines =<< trim END 75 var name = 1 76 ? 'yes' 77 : 'no' 78 assert_equal('yes', name) 79 END 80 CheckDefAndScriptSuccess(lines) 81 82 lines =<< trim END 83 var name = v:false 84 ? 'yes' 85 : 'no' 86 assert_equal('no', name) 87 END 88 CheckDefAndScriptSuccess(lines) 89 90 lines =<< trim END 91 var name = v:false ? 92 'yes' : 93 'no' 94 assert_equal('no', name) 95 END 96 CheckDefAndScriptSuccess(lines) 97 98 lines =<< trim END 99 var name = v:false ? # comment 100 'yes' : 101 # comment 102 'no' # comment 103 assert_equal('no', name) 104 END 105 CheckDefAndScriptSuccess(lines) 106 107 # check white space 108 lines =<< trim END 109 var name = v:true?1:2 110 END 111 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''?'' at "?1:2"', 1) 112 113 lines =<< trim END 114 var name = v:true? 1 : 2 115 END 116 CheckDefAndScriptFailure(lines, 'E1004:', 1) 117 118 lines =<< trim END 119 var name = v:true ?1 : 2 120 END 121 CheckDefAndScriptFailure(lines, 'E1004:', 1) 122 123 lines =<< trim END 124 var name = v:true ? 1: 2 125 END 126 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after '':'' at ": 2"', 1) 127 128 lines =<< trim END 129 var name = v:true ? 1 :2 130 END 131 CheckDefAndScriptFailure(lines, 'E1004:', 1) 132 133 lines =<< trim END 134 var name = 'x' ? 1 : 2 135 END 136 CheckDefAndScriptFailure(lines, 'E1135:', 1) 137 138 lines =<< trim END 139 var name = [] ? 1 : 2 140 END 141 CheckDefExecAndScriptFailure(lines, 'E745:', 1) 142 143 lines =<< trim END 144 var name = {} ? 1 : 2 145 END 146 CheckDefExecAndScriptFailure(lines, 'E728:', 1) 147 148 # check after failure eval_flags is reset 149 lines =<< trim END 150 try 151 eval('0 ? 1: 2') 152 catch 153 endtry 154 assert_equal(v:true, eval(string(v:true))) 155 END 156 CheckDefAndScriptSuccess(lines) 157 158 lines =<< trim END 159 try 160 eval('0 ? 1 :2') 161 catch 162 endtry 163 assert_equal(v:true, eval(string(v:true))) 164 END 165 CheckDefAndScriptSuccess(lines) 166enddef 167 168func Test_expr1_trinary_fails() 169 call CheckDefAndScriptFailure(["var x = 1 ? 'one'"], "Missing ':' after '?'", 1) 170 171 let msg = "White space required before and after '?'" 172 call CheckDefAndScriptFailure(["var x = 1? 'one' : 'two'"], msg, 1) 173 call CheckDefAndScriptFailure(["var x = 1 ?'one' : 'two'"], msg, 1) 174 call CheckDefAndScriptFailure(["var x = 1?'one' : 'two'"], msg, 1) 175 let lines =<< trim END 176 var x = 1 177 ?'one' : 'two' 178 # comment 179 END 180 call CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''?'' at "?''one'' : ''two''"', 2) 181 182 let msg = "White space required before and after ':'" 183 call CheckDefAndScriptFailure(["var x = 1 ? 'one': 'two'"], msg, 1) 184 call CheckDefAndScriptFailure(["var x = 1 ? 'one' :'two'"], msg, 1) 185 call CheckDefAndScriptFailure(["var x = 1 ? 'one':'two'"], msg, 1) 186 let lines =<< trim END 187 var x = 1 ? 'one' 188 :'two' 189 # Comment 190 END 191 call CheckDefAndScriptFailure(lines, 'E1004: White space required before and after '':'' at ":''two''"', 2) 192 193 call CheckDefAndScriptFailure(["var x = 'x' ? 'one' : 'two'"], 'E1135:', 1) 194 call CheckDefAndScriptFailure(["var x = 0z1234 ? 'one' : 'two'"], 'E974:', 1) 195 call CheckDefExecAndScriptFailure(["var x = [] ? 'one' : 'two'"], 'E745:', 1) 196 call CheckDefExecAndScriptFailure(["var x = {} ? 'one' : 'two'"], 'E728:', 1) 197 198 call CheckDefExecFailure(["var x = false ? "], 'E1097:', 3) 199 call CheckScriptFailure(['vim9script', "var x = false ? "], 'E15:', 2) 200 call CheckDefExecFailure(["var x = false ? 'one' : "], 'E1097:', 3) 201 call CheckScriptFailure(['vim9script', "var x = false ? 'one' : "], 'E15:', 2) 202 203 call CheckDefExecAndScriptFailure2(["var x = true ? xxx : 'foo'"], 'E1001:', 'E121:', 1) 204 call CheckDefExecAndScriptFailure2(["var x = false ? 'foo' : xxx"], 'E1001:', 'E121:', 1) 205 206 if has('float') 207 call CheckDefAndScriptFailure(["var x = 0.1 ? 'one' : 'two'"], 'E805:', 1) 208 endif 209 210 " missing argument detected even when common type is used 211 call CheckDefAndScriptFailure([ 212 \ 'var X = FuncOne', 213 \ 'var Y = FuncTwo', 214 \ 'var Z = g:cond ? FuncOne : FuncTwo', 215 \ 'Z()'], 'E119:', 4) 216endfunc 217 218def Test_expr1_falsy() 219 var lines =<< trim END 220 assert_equal(v:true, v:true ?? 456) 221 assert_equal(123, 123 ?? 456) 222 assert_equal('yes', 'yes' ?? 456) 223 assert_equal([1], [1] ?? 456) 224 assert_equal({one: 1}, {one: 1} ?? 456) 225 if has('float') 226 assert_equal(0.1, 0.1 ?? 456) 227 endif 228 229 assert_equal(456, v:false ?? 456) 230 assert_equal(456, 0 ?? 456) 231 assert_equal(456, '' ?? 456) 232 assert_equal(456, [] ?? 456) 233 assert_equal(456, {} ?? 456) 234 if has('float') 235 assert_equal(456, 0.0 ?? 456) 236 endif 237 END 238 CheckDefAndScriptSuccess(lines) 239 240 var msg = "White space required before and after '??'" 241 call CheckDefAndScriptFailure(["var x = 1?? 'one' : 'two'"], msg, 1) 242 call CheckDefAndScriptFailure(["var x = 1 ??'one' : 'two'"], msg, 1) 243 call CheckDefAndScriptFailure(["var x = 1??'one' : 'two'"], msg, 1) 244 lines =<< trim END 245 var x = 1 246 ??'one' : 'two' 247 #comment 248 END 249 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''??'' at "??''one'' : ''two''"', 2) 250enddef 251 252def Record(val: any): any 253 g:vals->add(val) 254 return val 255enddef 256 257" test || 258def Test_expr2() 259 var lines =<< trim END 260 assert_equal(true, 1 || 0) 261 assert_equal(true, 0 || 262 0 || 263 1) 264 assert_equal(true, 0 || 265 0 || 266 !!7) 267 assert_equal(false, 0 || 0) 268 assert_equal(false, 0 269 || 0) 270 assert_equal(false, 0 || false) 271 272 g:vals = [] 273 assert_equal(true, Record(1) || Record(3)) 274 assert_equal([1], g:vals) 275 276 g:vals = [] 277 assert_equal(true, Record(0) || Record(1)) 278 assert_equal([0, 1], g:vals) 279 280 g:vals = [] 281 assert_equal(true, Record(0) || Record(true)) 282 assert_equal([0, true], g:vals) 283 284 g:vals = [] 285 assert_equal(true, Record(0) 286 || Record(1) 287 || Record(0)) 288 assert_equal([0, 1], g:vals) 289 290 g:vals = [] 291 assert_equal(true, Record(0) 292 || Record(true) 293 || Record(0)) 294 assert_equal([0, true], g:vals) 295 296 g:vals = [] 297 assert_equal(true, Record(true) || Record(false)) 298 assert_equal([true], g:vals) 299 300 g:vals = [] 301 assert_equal(false, Record(0) || Record(false) || Record(0)) 302 assert_equal([0, false, 0], g:vals) 303 304 g:vals = [] 305 var x = 1 306 if x || true 307 g:vals = [1] 308 endif 309 assert_equal([1], g:vals) 310 311 g:vals = [] 312 x = 3 313 if true || x 314 g:vals = [1] 315 endif 316 assert_equal([1], g:vals) 317 END 318 CheckDefAndScriptSuccess(lines) 319enddef 320 321def Test_expr2_vimscript() 322 # check line continuation 323 var lines =<< trim END 324 var name = 0 325 || 1 326 assert_equal(true, name) 327 END 328 CheckDefAndScriptSuccess(lines) 329 330 lines =<< trim END 331 var name = v:false 332 || v:true 333 || v:false 334 assert_equal(v:true, name) 335 END 336 CheckDefAndScriptSuccess(lines) 337 338 lines =<< trim END 339 var name = v:false || 340 v:true || 341 v:false 342 assert_equal(v:true, name) 343 END 344 CheckDefAndScriptSuccess(lines) 345 346 lines =<< trim END 347 var name = v:false || # comment 348 # comment 349 v:true || 350 # comment 351 v:false # comment 352 assert_equal(v:true, name) 353 END 354 CheckDefAndScriptSuccess(lines) 355 356 # check white space 357 lines =<< trim END 358 var name = v:true||v:true 359 END 360 CheckDefExecAndScriptFailure(lines, 'E1004: White space required before and after ''||'' at "||v:true"', 1) 361 362 lines =<< trim END 363 var name = v:true ||v:true 364 END 365 CheckDefAndScriptFailure(lines, 'E1004:', 1) 366 367 lines =<< trim END 368 var name = v:true|| v:true 369 END 370 CheckDefAndScriptFailure(lines, 'E1004:', 1) 371enddef 372 373def Test_expr2_fails() 374 var msg = "White space required before and after '||'" 375 call CheckDefAndScriptFailure(["var x = 1||0"], msg, 1) 376 call CheckDefAndScriptFailure(["var x = 1 ||0"], msg, 1) 377 call CheckDefAndScriptFailure(["var x = 1|| 0"], msg, 1) 378 379 call CheckDefFailure(["var x = false || "], 'E1097:', 3) 380 call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2) 381 382 # script does not fail, the second expression is skipped 383 call CheckDefFailure(["var x = 1 || xxx"], 'E1001:', 1) 384 385 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012:', 'E745:', 1) 386 387 call CheckDefAndScriptFailure2(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) 388 389 call CheckDefAndScriptFailure2(["var x = 3 || false"], 'E1012:', 'E1023:', 1) 390 call CheckDefAndScriptFailure2(["var x = false || 3"], 'E1012:', 'E1023:', 1) 391 392 call CheckDefAndScriptFailure(["if 3"], 'E1023:', 1) 393 call CheckDefExecAndScriptFailure(['var x = 3', 'if x', 'endif'], 'E1023:', 2) 394 395 call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012: Type mismatch; expected bool but got list<unknown>', 'E745:', 1) 396 397 var lines =<< trim END 398 vim9script 399 echo false 400 ||true 401 # comment 402 END 403 CheckScriptFailure(lines, 'E1004: White space required before and after ''||'' at "||true"', 3) 404 405 lines =<< trim END 406 var x = false 407 || false 408 || a.b 409 END 410 CheckDefFailure(lines, 'E1001:', 3) 411enddef 412 413" test && 414def Test_expr3() 415 var lines =<< trim END 416 assert_equal(false, 1 && 0) 417 assert_equal(false, 0 && 418 0 && 419 1) 420 assert_equal(true, 1 421 && true 422 && 1) 423 assert_equal(false, 0 && 0) 424 assert_equal(false, 0 && false) 425 assert_equal(true, 1 && true) 426 427 g:vals = [] 428 assert_equal(true, Record(true) && Record(1)) 429 assert_equal([true, 1], g:vals) 430 431 g:vals = [] 432 assert_equal(true, Record(1) && Record(true)) 433 assert_equal([1, true], g:vals) 434 435 g:vals = [] 436 assert_equal(false, Record(0) && Record(1)) 437 assert_equal([0], g:vals) 438 439 g:vals = [] 440 assert_equal(false, Record(0) && Record(1) && Record(0)) 441 assert_equal([0], g:vals) 442 443 g:vals = [] 444 assert_equal(false, Record(0) && Record(4) && Record(0)) 445 assert_equal([0], g:vals) 446 447 g:vals = [] 448 assert_equal(false, Record(1) && Record(true) && Record(0)) 449 assert_equal([1, true, 0], g:vals) 450 END 451 CheckDefAndScriptSuccess(lines) 452enddef 453 454def Test_expr3_vimscript() 455 # check line continuation 456 var lines =<< trim END 457 var name = 0 458 && 1 459 assert_equal(false, name) 460 END 461 CheckDefAndScriptSuccess(lines) 462 463 lines =<< trim END 464 var name = v:true 465 && v:true 466 && v:true 467 assert_equal(v:true, name) 468 END 469 CheckDefAndScriptSuccess(lines) 470 471 lines =<< trim END 472 var name = v:true && 473 v:true && 474 v:true 475 assert_equal(v:true, name) 476 END 477 CheckDefAndScriptSuccess(lines) 478 479 lines =<< trim END 480 var name = v:true && # comment 481 # comment 482 v:true && 483 # comment 484 v:true 485 assert_equal(v:true, name) 486 END 487 CheckDefAndScriptSuccess(lines) 488 489 # check white space 490 lines =<< trim END 491 var name = v:true&&v:true 492 END 493 CheckDefAndScriptFailure(lines, 'E1004:', 1) 494 495 lines =<< trim END 496 var name = v:true &&v:true 497 END 498 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&v:true"', 1) 499 500 lines =<< trim END 501 var name = v:true&& v:true 502 END 503 CheckDefAndScriptFailure(lines, 'E1004:', 1) 504enddef 505 506def Test_expr3_fails() 507 var msg = "White space required before and after '&&'" 508 CheckDefAndScriptFailure(["var x = 1&&0"], msg, 1) 509 CheckDefAndScriptFailure(["var x = 1 &&0"], msg, 1) 510 CheckDefAndScriptFailure(["var x = 1&& 0"], msg, 1) 511 var lines =<< trim END 512 var x = 1 513 &&0 514 # comment 515 END 516 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&0"', 2) 517 518 g:vals = [] 519 CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) 520 521 CheckDefExecAndScriptFailure(['assert_equal(false, Record(1) && Record(4) && Record(0))'], 'E1023: Using a Number as a Bool: 4', 1) 522 523 lines =<< trim END 524 if 3 525 && true 526 endif 527 END 528 CheckDefAndScriptFailure2(lines, 'E1012:', 'E1023:', 1) 529 530 lines =<< trim END 531 if true 532 && 3 533 endif 534 END 535 CheckDefAndScriptFailure2(lines, 'E1012:', 'E1023:', 2) 536 537 lines =<< trim END 538 if 'yes' 539 && true 540 endif 541 END 542 CheckDefAndScriptFailure2(lines, 'E1012:', 'E1135: Using a String as a Bool', 1) 543enddef 544 545" global variables to use for tests with the "any" type 546let atrue = v:true 547let afalse = v:false 548let anone = v:none 549let anull = v:null 550let anint = 10 551let theone = 1 552let thefour = 4 553if has('float') 554 let afloat = 0.1 555endif 556let astring = 'asdf' 557let ablob = 0z01ab 558let alist = [2, 3, 4] 559let adict = #{aaa: 2, bbb: 8} 560 561" test == comperator 562def Test_expr4_equal() 563 var lines =<< trim END 564 var trueVar = true 565 var falseVar = false 566 assert_equal(true, true == true) 567 assert_equal(false, true == 568 false) 569 assert_equal(true, true 570 == trueVar) 571 assert_equal(false, true == falseVar) 572 assert_equal(true, true == g:atrue) 573 assert_equal(false, g:atrue == false) 574 575 assert_equal(true, v:none == v:none) 576 assert_equal(false, v:none == v:null) 577 assert_equal(true, g:anone == v:none) 578 assert_equal(true, null == v:null) 579 assert_equal(true, null == g:anull) 580 assert_equal(false, v:none == g:anull) 581 582 var nr0 = 0 583 var nr61 = 61 584 assert_equal(false, 2 == 0) 585 assert_equal(false, 2 == nr0) 586 assert_equal(true, 61 == 61) 587 assert_equal(true, 61 == nr61) 588 assert_equal(true, g:anint == 10) 589 assert_equal(false, 61 == g:anint) 590 591 if has('float') 592 var ff = 0.3 593 assert_equal(true, ff == 0.3) 594 assert_equal(false, 0.4 == ff) 595 assert_equal(true, 0.1 == g:afloat) 596 assert_equal(false, g:afloat == 0.3) 597 598 ff = 3.0 599 assert_equal(true, ff == 3) 600 assert_equal(true, 3 == ff) 601 ff = 3.1 602 assert_equal(false, ff == 3) 603 assert_equal(false, 3 == ff) 604 endif 605 606 assert_equal(true, 'abc' == 'abc') 607 assert_equal(false, 'xyz' == 'abc') 608 assert_equal(true, g:astring == 'asdf') 609 assert_equal(false, 'xyz' == g:astring) 610 611 assert_equal(false, 'abc' == 'aBc') 612 assert_equal(false, 'abc' ==# 'aBc') 613 assert_equal(true, 'abc' ==? 'aBc') 614 615 assert_equal(false, 'abc' == 'ABC') 616 set ignorecase 617 assert_equal(false, 'abc' == 'ABC') 618 assert_equal(false, 'abc' ==# 'ABC') 619 assert_equal(true, 'abc' ==? 'ABC') 620 set noignorecase 621 622 var bb = 0z3f 623 assert_equal(true, 0z3f == bb) 624 assert_equal(false, bb == 0z4f) 625 assert_equal(true, g:ablob == 0z01ab) 626 assert_equal(false, 0z3f == g:ablob) 627 628 assert_equal(true, [1, 2, 3] == [1, 2, 3]) 629 assert_equal(false, [1, 2, 3] == [2, 3, 1]) 630 assert_equal(true, [2, 3, 4] == g:alist) 631 assert_equal(false, g:alist == [2, 3, 1]) 632 assert_equal(false, [1, 2, 3] == []) 633 assert_equal(false, [1, 2, 3] == ['1', '2', '3']) 634 635 assert_equal(true, {one: 1, two: 2} == {one: 1, two: 2}) 636 assert_equal(false, {one: 1, two: 2} == {one: 2, two: 2}) 637 assert_equal(false, {one: 1, two: 2} == {two: 2}) 638 assert_equal(false, {one: 1, two: 2} == {}) 639 assert_equal(true, g:adict == {bbb: 8, aaa: 2}) 640 assert_equal(false, {ccc: 9, aaa: 2} == g:adict) 641 642 assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal')) 643 assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is')) 644 645 assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123])) 646 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123])) 647 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999])) 648 649 # TODO: this unexpectedly sometimes fails on Appveyor 650 if !has('win32') 651 var OneFunc: func 652 var TwoFunc: func 653 OneFunc = function('len') 654 TwoFunc = function('len') 655 assert_equal(true, OneFunc('abc') == TwoFunc('123')) 656 endif 657 END 658 CheckDefAndScriptSuccess(lines) 659 660 CheckDefAndScriptFailure2(["var x = 'a' == xxx"], 'E1001:', 'E121:', 1) 661 CheckDefFailure(["var x = 'a' == "], 'E1097:', 3) 662 CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2) 663 664 CheckDefExecAndScriptFailure2(['var items: any', 'eval 1 + 1', 'eval 2 + 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4) 665 666 CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2) 667 CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2) 668 CheckDefExecAndScriptFailure2(["var x: any = 99", 'echo x == true'], 'E1138', 'E1072:', 2) 669 CheckDefExecAndScriptFailure2(["var x: any = 'a'", 'echo x == 99'], 'E1030:', 'E1072:', 2) 670enddef 671 672def Test_expr4_wrong_type() 673 for op in ['>', '>=', '<', '<=', '=~', '!~'] 674 CheckDefExecAndScriptFailure([ 675 "var a: any = 'a'", 676 'var b: any = true', 677 'echo a ' .. op .. ' b'], 'E1072:', 3) 678 endfor 679 for op in ['>', '>=', '<', '<='] 680 CheckDefExecAndScriptFailure2([ 681 "var n: any = 2", 682 'echo n ' .. op .. ' "3"'], 'E1030:', 'E1072:', 2) 683 endfor 684 for op in ['=~', '!~'] 685 CheckDefExecAndScriptFailure([ 686 "var n: any = 2", 687 'echo n ' .. op .. ' "3"'], 'E1072:', 2) 688 endfor 689 690 CheckDefAndScriptFailure([ 691 'echo v:none == true'], 'E1072:', 1) 692 CheckDefAndScriptFailure([ 693 'echo false >= true'], 'E1072:', 1) 694 CheckDefExecAndScriptFailure([ 695 "var n: any = v:none", 696 'echo n == true'], 'E1072:', 2) 697 CheckDefExecAndScriptFailure([ 698 "var n: any = v:none", 699 'echo n < true'], 'E1072:', 2) 700enddef 701 702" test != comperator 703def Test_expr4_notequal() 704 var lines =<< trim END 705 var trueVar = true 706 var falseVar = false 707 assert_equal(false, true != true) 708 assert_equal(true, true != 709 false) 710 assert_equal(false, true 711 != trueVar) 712 assert_equal(true, true != falseVar) 713 assert_equal(false, true != g:atrue) 714 assert_equal(true, g:atrue != false) 715 716 assert_equal(false, v:none != v:none) 717 assert_equal(true, v:none != v:null) 718 assert_equal(false, g:anone != v:none) 719 assert_equal(true, v:none != g:anull) 720 721 var nr55 = 55 722 var nr0 = 55 723 assert_equal(true, 2 != 0) 724 assert_equal(true, 2 != nr0) 725 assert_equal(false, 55 != 55) 726 assert_equal(false, 55 != nr55) 727 assert_equal(false, g:anint != 10) 728 assert_equal(true, 61 != g:anint) 729 730 if has('float') 731 var ff = 0.3 732 assert_equal(false, 0.3 != ff) 733 assert_equal(true, 0.4 != ff) 734 assert_equal(false, 0.1 != g:afloat) 735 assert_equal(true, g:afloat != 0.3) 736 737 ff = 3.0 738 assert_equal(false, ff != 3) 739 assert_equal(false, 3 != ff) 740 ff = 3.1 741 assert_equal(true, ff != 3) 742 assert_equal(true, 3 != ff) 743 endif 744 745 assert_equal(false, 'abc' != 'abc') 746 assert_equal(true, 'xyz' != 'abc') 747 assert_equal(false, g:astring != 'asdf') 748 assert_equal(true, 'xyz' != g:astring) 749 750 assert_equal(true, 'abc' != 'ABC') 751 set ignorecase 752 assert_equal(true, 'abc' != 'ABC') 753 assert_equal(true, 'abc' !=# 'ABC') 754 assert_equal(false, 'abc' !=? 'ABC') 755 set noignorecase 756 757 var bb = 0z3f 758 assert_equal(false, 0z3f != bb) 759 assert_equal(true, bb != 0z4f) 760 assert_equal(false, g:ablob != 0z01ab) 761 assert_equal(true, 0z3f != g:ablob) 762 763 assert_equal(false, [1, 2, 3] != [1, 2, 3]) 764 assert_equal(true, [1, 2, 3] != [2, 3, 1]) 765 assert_equal(false, [2, 3, 4] != g:alist) 766 assert_equal(true, g:alist != [2, 3, 1]) 767 assert_equal(true, [1, 2, 3] != []) 768 assert_equal(true, [1, 2, 3] != ['1', '2', '3']) 769 770 assert_equal(false, {one: 1, two: 2} != {one: 1, two: 2}) 771 assert_equal(true, {one: 1, two: 2} != {one: 2, two: 2}) 772 assert_equal(true, {one: 1, two: 2} != {two: 2}) 773 assert_equal(true, {one: 1, two: 2} != {}) 774 assert_equal(false, g:adict != {bbb: 8, aaa: 2}) 775 assert_equal(true, {ccc: 9, aaa: 2} != g:adict) 776 777 assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal')) 778 assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is')) 779 780 assert_equal(false, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [123])) 781 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_is', [123])) 782 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [999])) 783 END 784 CheckDefAndScriptSuccess(lines) 785enddef 786 787" test > comperator 788def Test_expr4_greater() 789 var lines =<< trim END 790 assert_true(2 > 0) 791 assert_true(2 > 792 1) 793 assert_false(2 > 2) 794 assert_false(2 > 3) 795 var nr2 = 2 796 assert_true(nr2 > 0) 797 assert_true(nr2 > 798 1) 799 assert_false(nr2 > 2) 800 assert_false(nr2 801 > 3) 802 if has('float') 803 var ff = 2.0 804 assert_true(ff > 0.0) 805 assert_true(ff > 1.0) 806 assert_false(ff > 2.0) 807 assert_false(ff > 3.0) 808 endif 809 END 810 CheckDefAndScriptSuccess(lines) 811enddef 812 813" test >= comperator 814def Test_expr4_greaterequal() 815 var lines =<< trim END 816 assert_true(2 >= 0) 817 assert_true(2 >= 818 2) 819 assert_false(2 >= 3) 820 var nr2 = 2 821 assert_true(nr2 >= 0) 822 assert_true(nr2 >= 2) 823 assert_false(nr2 >= 3) 824 if has('float') 825 var ff = 2.0 826 assert_true(ff >= 0.0) 827 assert_true(ff >= 2.0) 828 assert_false(ff >= 3.0) 829 endif 830 END 831 CheckDefAndScriptSuccess(lines) 832enddef 833 834" test < comperator 835def Test_expr4_smaller() 836 var lines =<< trim END 837 assert_false(2 < 0) 838 assert_false(2 < 839 2) 840 assert_true(2 841 < 3) 842 var nr2 = 2 843 assert_false(nr2 < 0) 844 assert_false(nr2 < 2) 845 assert_true(nr2 < 3) 846 if has('float') 847 var ff = 2.0 848 assert_false(ff < 0.0) 849 assert_false(ff < 2.0) 850 assert_true(ff < 3.0) 851 endif 852 END 853 CheckDefAndScriptSuccess(lines) 854enddef 855 856" test <= comperator 857def Test_expr4_smallerequal() 858 var lines =<< trim END 859 assert_false(2 <= 0) 860 assert_false(2 <= 861 1) 862 assert_true(2 863 <= 2) 864 assert_true(2 <= 3) 865 var nr2 = 2 866 assert_false(nr2 <= 0) 867 assert_false(nr2 <= 1) 868 assert_true(nr2 <= 2) 869 assert_true(nr2 <= 3) 870 if has('float') 871 var ff = 2.0 872 assert_false(ff <= 0.0) 873 assert_false(ff <= 1.0) 874 assert_true(ff <= 2.0) 875 assert_true(ff <= 3.0) 876 endif 877 END 878 CheckDefAndScriptSuccess(lines) 879enddef 880 881" test =~ comperator 882def Test_expr4_match() 883 var lines =<< trim END 884 assert_equal(false, '2' =~ '0') 885 assert_equal(false, '' 886 =~ '0') 887 assert_equal(true, '2' =~ 888 '[0-9]') 889 set ignorecase 890 assert_equal(false, 'abc' =~ 'ABC') 891 assert_equal(false, 'abc' =~# 'ABC') 892 assert_equal(true, 'abc' =~? 'ABC') 893 set noignorecase 894 END 895 CheckDefAndScriptSuccess(lines) 896enddef 897 898" test !~ comperator 899def Test_expr4_nomatch() 900 var lines =<< trim END 901 assert_equal(true, '2' !~ '0') 902 assert_equal(true, '' 903 !~ '0') 904 assert_equal(false, '2' !~ 905 '[0-9]') 906 END 907 CheckDefAndScriptSuccess(lines) 908enddef 909 910" test is comperator 911def Test_expr4_is() 912 var lines =<< trim END 913 var mylist = [2] 914 assert_false(mylist is [2]) 915 var other = mylist 916 assert_true(mylist is 917 other) 918 919 var myblob = 0z1234 920 assert_false(myblob 921 is 0z1234) 922 var otherblob = myblob 923 assert_true(myblob is otherblob) 924 END 925 CheckDefAndScriptSuccess(lines) 926enddef 927 928" test isnot comperator 929def Test_expr4_isnot() 930 var lines =<< trim END 931 var mylist = [2] 932 assert_true('2' isnot '0') 933 assert_true(mylist isnot [2]) 934 var other = mylist 935 assert_false(mylist isnot 936 other) 937 938 var myblob = 0z1234 939 assert_true(myblob 940 isnot 0z1234) 941 var otherblob = myblob 942 assert_false(myblob isnot otherblob) 943 END 944 CheckDefAndScriptSuccess(lines) 945enddef 946 947def RetVoid() 948 var x = 1 949enddef 950 951def Test_expr4_vim9script() 952 # check line continuation 953 var lines =<< trim END 954 var name = 0 955 < 1 956 assert_equal(true, name) 957 END 958 CheckDefAndScriptSuccess(lines) 959 960 lines =<< trim END 961 var name = 123 962 # comment 963 != 123 964 assert_equal(false, name) 965 END 966 CheckDefAndScriptSuccess(lines) 967 968 lines =<< trim END 969 var name = 123 == 970 123 971 assert_equal(true, name) 972 END 973 CheckDefAndScriptSuccess(lines) 974 975 lines =<< trim END 976 var list = [1, 2, 3] 977 var name = list 978 is list 979 assert_equal(true, name) 980 END 981 CheckDefAndScriptSuccess(lines) 982 983 lines =<< trim END 984 var list = [1, 2, 3] 985 var name = list # comment 986 # comment 987 is list 988 assert_equal(true, name) 989 END 990 CheckDefAndScriptSuccess(lines) 991 992 lines =<< trim END 993 var myblob = 0z1234 994 var name = myblob 995 isnot 0z11 996 assert_equal(true, name) 997 END 998 CheckDefAndScriptSuccess(lines) 999 1000 # spot check mismatching types 1001 lines =<< trim END 1002 echo '' == 0 1003 END 1004 CheckDefAndScriptFailure(lines, 'E1072:', 1) 1005 1006 lines =<< trim END 1007 echo v:true > v:false 1008 END 1009 CheckDefAndScriptFailure(lines, 'Cannot compare bool with bool', 1) 1010 1011 lines =<< trim END 1012 echo 123 is 123 1013 END 1014 CheckDefAndScriptFailure(lines, 'Cannot use "is" with number', 1) 1015 1016 # check missing white space 1017 lines =<< trim END 1018 echo 2>3 1019 END 1020 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''>'' at ">3"', 1) 1021 1022 lines =<< trim END 1023 echo 2 >3 1024 END 1025 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1026 1027 lines =<< trim END 1028 echo 2> 3 1029 END 1030 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1031 1032 lines =<< trim END 1033 echo 2!=3 1034 END 1035 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1036 1037 lines =<< trim END 1038 echo 2 !=3 1039 END 1040 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''!='' at "!=3"', 1) 1041 1042 lines =<< trim END 1043 echo 2!= 3 1044 END 1045 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1046 1047 for op in ['==', '>', '>=', '<', '<=', '=~', '!~', 'is', 'isnot'] 1048 lines = ["echo 'aaa'", op .. "'bbb'", '# comment'] 1049 var msg = printf("E1004: White space required before and after '%s'", op) 1050 CheckDefAndScriptFailure(lines, msg, 2) 1051 endfor 1052 1053 lines =<< trim END 1054 echo len('xxx') == 3 1055 END 1056 CheckDefAndScriptSuccess(lines) 1057 1058 lines =<< trim END 1059 var line = 'abc' 1060 echo line[1] =~ '\w' 1061 END 1062 CheckDefAndScriptSuccess(lines) 1063enddef 1064 1065func Test_expr4_fails() 1066 let msg = "White space required before and after '>'" 1067 call CheckDefAndScriptFailure(["var x = 1>2"], msg, 1) 1068 call CheckDefAndScriptFailure(["var x = 1 >2"], msg, 1) 1069 call CheckDefAndScriptFailure(["var x = 1> 2"], msg, 1) 1070 1071 let msg = "White space required before and after '=='" 1072 call CheckDefAndScriptFailure(["var x = 1==2"], msg, 1) 1073 call CheckDefAndScriptFailure(["var x = 1 ==2"], msg, 1) 1074 call CheckDefAndScriptFailure(["var x = 1== 2"], msg, 1) 1075 1076 let msg = "White space required before and after 'is'" 1077 call CheckDefAndScriptFailure(["var x = '1'is'2'"], msg, 1) 1078 call CheckDefAndScriptFailure(["var x = '1' is'2'"], msg, 1) 1079 call CheckDefAndScriptFailure(["var x = '1'is '2'"], msg, 1) 1080 1081 let msg = "White space required before and after 'isnot'" 1082 call CheckDefAndScriptFailure(["var x = '1'isnot'2'"], msg, 1) 1083 call CheckDefAndScriptFailure(["var x = '1' isnot'2'"], msg, 1) 1084 call CheckDefAndScriptFailure(["var x = '1'isnot '2'"], msg, 1) 1085 1086 call CheckDefAndScriptFailure(["var x = 1 is# 2"], 'E15:', 1) 1087 call CheckDefAndScriptFailure(["var x = 1 is? 2"], 'E15:', 1) 1088 call CheckDefAndScriptFailure(["var x = 1 isnot# 2"], 'E15:', 1) 1089 call CheckDefAndScriptFailure(["var x = 1 isnot? 2"], 'E15:', 1) 1090 1091 call CheckDefAndScriptFailure(["var x = 1 == '2'"], 'Cannot compare number with string', 1) 1092 call CheckDefAndScriptFailure(["var x = '1' == 2"], 'Cannot compare string with number', 1) 1093 call CheckDefAndScriptFailure(["var x = 1 == RetVoid()"], 'Cannot compare number with void', 1) 1094 call CheckDefAndScriptFailure(["var x = RetVoid() == 1"], 'Cannot compare void with number', 1) 1095 1096 call CheckDefAndScriptFailure(["var x = true > false"], 'Cannot compare bool with bool', 1) 1097 call CheckDefAndScriptFailure(["var x = true >= false"], 'Cannot compare bool with bool', 1) 1098 call CheckDefAndScriptFailure(["var x = true < false"], 'Cannot compare bool with bool', 1) 1099 call CheckDefAndScriptFailure(["var x = true <= false"], 'Cannot compare bool with bool', 1) 1100 call CheckDefAndScriptFailure(["var x = true =~ false"], 'Cannot compare bool with bool', 1) 1101 call CheckDefAndScriptFailure(["var x = true !~ false"], 'Cannot compare bool with bool', 1) 1102 call CheckDefAndScriptFailure(["var x = true is false"], 'Cannot use "is" with bool', 1) 1103 call CheckDefAndScriptFailure(["var x = true isnot false"], 'Cannot use "isnot" with bool', 1) 1104 1105 call CheckDefAndScriptFailure(["var x = v:none is v:null"], 'Cannot use "is" with special', 1) 1106 call CheckDefAndScriptFailure(["var x = v:none isnot v:null"], 'Cannot use "isnot" with special', 1) 1107 call CheckDefAndScriptFailure(["var x = 123 is 123"], 'Cannot use "is" with number', 1) 1108 call CheckDefAndScriptFailure(["var x = 123 isnot 123"], 'Cannot use "isnot" with number', 1) 1109 if has('float') 1110 call CheckDefAndScriptFailure(["var x = 1.3 is 1.3"], 'Cannot use "is" with float', 1) 1111 call CheckDefAndScriptFailure(["var x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float', 1) 1112 endif 1113 1114 call CheckDefAndScriptFailure(["var x = 0za1 > 0z34"], 'Cannot compare blob with blob', 1) 1115 call CheckDefAndScriptFailure(["var x = 0za1 >= 0z34"], 'Cannot compare blob with blob', 1) 1116 call CheckDefAndScriptFailure(["var x = 0za1 < 0z34"], 'Cannot compare blob with blob', 1) 1117 call CheckDefAndScriptFailure(["var x = 0za1 <= 0z34"], 'Cannot compare blob with blob', 1) 1118 call CheckDefAndScriptFailure(["var x = 0za1 =~ 0z34"], 'Cannot compare blob with blob', 1) 1119 call CheckDefAndScriptFailure(["var x = 0za1 !~ 0z34"], 'Cannot compare blob with blob', 1) 1120 1121 call CheckDefAndScriptFailure(["var x = [13] > [88]"], 'Cannot compare list with list', 1) 1122 call CheckDefAndScriptFailure(["var x = [13] >= [88]"], 'Cannot compare list with list', 1) 1123 call CheckDefAndScriptFailure(["var x = [13] < [88]"], 'Cannot compare list with list', 1) 1124 call CheckDefAndScriptFailure(["var x = [13] <= [88]"], 'Cannot compare list with list', 1) 1125 call CheckDefAndScriptFailure(["var x = [13] =~ [88]"], 'Cannot compare list with list', 1) 1126 call CheckDefAndScriptFailure(["var x = [13] !~ [88]"], 'Cannot compare list with list', 1) 1127 1128 call CheckDefAndScriptFailure(['var j: job', 'var chan: channel', 'var r = j == chan'], 'Cannot compare job with channel', 3) 1129 call CheckDefAndScriptFailure(['var j: job', 'var x: list<any>', 'var r = j == x'], 'Cannot compare job with list', 3) 1130 call CheckDefAndScriptFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3) 1131 call CheckDefAndScriptFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3) 1132endfunc 1133 1134" test addition, subtraction, concatenation 1135def Test_expr5() 1136 var lines =<< trim END 1137 assert_equal(66, 60 + 6) 1138 assert_equal(70, 60 + 1139 g:anint) 1140 assert_equal(9, g:thefour 1141 + 5) 1142 assert_equal(14, g:thefour + g:anint) 1143 assert_equal([1, 2, 3, 4], [1] + g:alist) 1144 1145 assert_equal(54, 60 - 6) 1146 assert_equal(50, 60 - 1147 g:anint) 1148 assert_equal(-1, g:thefour 1149 - 5) 1150 assert_equal(-6, g:thefour - g:anint) 1151 1152 assert_equal('hello', 'hel' .. 'lo') 1153 assert_equal('hello 123', 'hello ' .. 1154 123) 1155 assert_equal('hello 123', 'hello ' 1156 .. 123) 1157 assert_equal('123 hello', 123 .. ' hello') 1158 assert_equal('123456', 123 .. 456) 1159 1160 assert_equal('atrue', 'a' .. true) 1161 assert_equal('afalse', 'a' .. false) 1162 assert_equal('anull', 'a' .. v:null) 1163 assert_equal('av:none', 'a' .. v:none) 1164 if has('float') 1165 assert_equal('a0.123', 'a' .. 0.123) 1166 endif 1167 1168 assert_equal(3, 1 + [2, 3, 4][0]) 1169 assert_equal(5, 2 + {key: 3}['key']) 1170 1171 set digraph 1172 assert_equal('val: true', 'val: ' .. &digraph) 1173 set nodigraph 1174 assert_equal('val: false', 'val: ' .. &digraph) 1175 1176 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) 1177 assert_equal(0z11223344, 0z1122 + 0z3344) 1178 assert_equal(0z112201ab, 0z1122 1179 + g:ablob) 1180 assert_equal(0z01ab3344, g:ablob + 0z3344) 1181 assert_equal(0z01ab01ab, g:ablob + g:ablob) 1182 1183 # concatenate non-constant to constant 1184 var save_path = &path 1185 &path = 'b' 1186 assert_equal('ab', 'a' .. &path) 1187 &path = save_path 1188 1189 @b = 'b' 1190 assert_equal('ab', 'a' .. @b) 1191 1192 $ENVVAR = 'env' 1193 assert_equal('aenv', 'a' .. $ENVVAR) 1194 1195 assert_equal('val', '' .. {key: 'val'}['key']) 1196 END 1197 CheckDefAndScriptSuccess(lines) 1198enddef 1199 1200def Test_expr5_vim9script() 1201 # check line continuation 1202 var lines =<< trim END 1203 var name = 11 1204 + 77 1205 - 22 1206 assert_equal(66, name) 1207 END 1208 CheckDefAndScriptSuccess(lines) 1209 1210 lines =<< trim END 1211 var name = 11 + 1212 77 - 1213 22 1214 assert_equal(66, name) 1215 END 1216 CheckDefAndScriptSuccess(lines) 1217 1218 lines =<< trim END 1219 var name = 11 + # comment 1220 77 - 1221 # comment 1222 22 1223 assert_equal(66, name) 1224 END 1225 CheckDefAndScriptSuccess(lines) 1226 1227 lines =<< trim END 1228 var name = 'one' 1229 .. 'two' 1230 assert_equal('onetwo', name) 1231 END 1232 CheckDefAndScriptSuccess(lines) 1233 1234 lines =<< trim END 1235 echo 'abc' is# 'abc' 1236 END 1237 CheckDefAndScriptFailure(lines, 'E15:', 1) 1238 1239 lines =<< trim END 1240 echo {} - 22 1241 END 1242 CheckDefAndScriptFailure2(lines, 'E1036:', 'E728:', 1) 1243 1244 lines =<< trim END 1245 echo [] - 33 1246 END 1247 CheckDefAndScriptFailure2(lines, 'E1036:', 'E745:', 1) 1248 1249 lines =<< trim END 1250 echo 0z1234 - 44 1251 END 1252 CheckDefAndScriptFailure2(lines, 'E1036', 'E974:', 1) 1253 1254 lines =<< trim END 1255 echo 'abc' is? 'abc' 1256 END 1257 CheckDefAndScriptFailure(lines, 'E15:', 1) 1258 1259 lines =<< trim END 1260 echo 'abc' isnot# 'abc' 1261 END 1262 CheckDefAndScriptFailure(lines, 'E15:', 1) 1263 1264 lines =<< trim END 1265 echo 'abc' isnot? 'abc' 1266 END 1267 CheckDefAndScriptFailure(lines, 'E15:', 1) 1268 1269 # check white space 1270 lines =<< trim END 1271 echo 5+6 1272 END 1273 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1274 lines =<< trim END 1275 echo 5 +6 1276 END 1277 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1278 1279 lines =<< trim END 1280 echo 5+ 6 1281 END 1282 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1283 1284 lines =<< trim END 1285 echo 'a'..'b' 1286 END 1287 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at "..''b''"', 1) 1288 1289 lines =<< trim END 1290 echo 'a' ..'b' 1291 END 1292 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1293 1294 lines =<< trim END 1295 echo 'a'.. 'b' 1296 END 1297 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at ".. ''b''"', 1) 1298 1299 lines =<< trim END 1300 echo 'a' 1301 ..'b' 1302 # comment 1303 END 1304 CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at "..''b''"', 2) 1305 1306 # check invalid string concatenation 1307 lines =<< trim END 1308 echo 'a' .. [1] 1309 END 1310 CheckDefAndScriptFailure2(lines, 'E1105:', 'E730:', 1) 1311 1312 lines =<< trim END 1313 echo 'a' .. {a: 1} 1314 END 1315 CheckDefAndScriptFailure2(lines, 'E1105:', 'E731:', 1) 1316 1317 lines =<< trim END 1318 echo 'a' .. test_void() 1319 END 1320 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1) 1321 1322 lines =<< trim END 1323 echo 'a' .. 0z33 1324 END 1325 CheckDefAndScriptFailure2(lines, 'E1105:', 'E976:', 1) 1326 1327 lines =<< trim END 1328 echo 'a' .. function('len') 1329 END 1330 CheckDefAndScriptFailure2(lines, 'E1105:', 'E729:', 1) 1331 1332 lines =<< trim END 1333 new 1334 ['']->setline(1) 1335 /pattern 1336 1337 eval 0 1338 bwipe! 1339 END 1340 CheckDefAndScriptFailure(lines, "E1004: White space required before and after '/' at \"/pattern", 3) 1341 1342 for op in ['+', '-'] 1343 lines = ['var x = 1', op .. '2', '# comment'] 1344 var msg = printf("E1004: White space required before and after '%s' at \"%s2\"", op, op) 1345 CheckDefAndScriptFailure(lines, msg, 2) 1346 endfor 1347enddef 1348 1349def Test_expr5_vim9script_channel() 1350 if !has('channel') 1351 MissingFeature 'float' 1352 else 1353 var lines =<< trim END 1354 echo 'a' .. test_null_job() 1355 END 1356 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1) 1357 lines =<< trim END 1358 echo 'a' .. test_null_channel() 1359 END 1360 CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1) 1361 endif 1362enddef 1363 1364def Test_expr5_float() 1365 if !has('float') 1366 MissingFeature 'float' 1367 else 1368 var lines =<< trim END 1369 assert_equal(66.0, 60.0 + 6.0) 1370 assert_equal(66.0, 60.0 + 6) 1371 assert_equal(66.0, 60 + 1372 6.0) 1373 assert_equal(5.1, g:afloat 1374 + 5) 1375 assert_equal(8.1, 8 + g:afloat) 1376 assert_equal(10.1, g:anint + g:afloat) 1377 assert_equal(10.1, g:afloat + g:anint) 1378 1379 assert_equal(54.0, 60.0 - 6.0) 1380 assert_equal(54.0, 60.0 1381 - 6) 1382 assert_equal(54.0, 60 - 6.0) 1383 assert_equal(-4.9, g:afloat - 5) 1384 assert_equal(7.9, 8 - g:afloat) 1385 assert_equal(9.9, g:anint - g:afloat) 1386 assert_equal(-9.9, g:afloat - g:anint) 1387 END 1388 CheckDefAndScriptSuccess(lines) 1389 endif 1390enddef 1391 1392func Test_expr5_fails() 1393 let msg = "White space required before and after '+'" 1394 call CheckDefAndScriptFailure(["var x = 1+2"], msg, 1) 1395 call CheckDefAndScriptFailure(["var x = 1 +2"], msg, 1) 1396 call CheckDefAndScriptFailure(["var x = 1+ 2"], msg, 1) 1397 1398 let msg = "White space required before and after '-'" 1399 call CheckDefAndScriptFailure(["var x = 1-2"], msg, 1) 1400 call CheckDefAndScriptFailure(["var x = 1 -2"], msg, 1) 1401 call CheckDefAndScriptFailure(["var x = 1- 2"], msg, 1) 1402 1403 let msg = "White space required before and after '..'" 1404 call CheckDefAndScriptFailure(["var x = '1'..'2'"], msg, 1) 1405 call CheckDefAndScriptFailure(["var x = '1' ..'2'"], msg, 1) 1406 call CheckDefAndScriptFailure(["var x = '1'.. '2'"], msg, 1) 1407 1408 call CheckDefAndScriptFailure2(["var x = 0z1122 + 33"], 'E1051:', 'E974:', 1) 1409 call CheckDefAndScriptFailure2(["var x = 0z1122 + [3]"], 'E1051:', 'E974:', 1) 1410 call CheckDefAndScriptFailure2(["var x = 0z1122 + 'asd'"], 'E1051:', 'E974:', 1) 1411 call CheckDefAndScriptFailure2(["var x = 33 + 0z1122"], 'E1051:', 'E974:', 1) 1412 call CheckDefAndScriptFailure2(["var x = [3] + 0z1122"], 'E1051:', 'E745:', 1) 1413 call CheckDefAndScriptFailure2(["var x = 'asdf' + 0z1122"], 'E1051:', 'E1030:', 1) 1414 call CheckDefAndScriptFailure2(["var x = 6 + xxx"], 'E1001:', 'E121:', 1) 1415 1416 call CheckDefAndScriptFailure2(["var x = 'a' .. [1]"], 'E1105:', 'E730:', 1) 1417 call CheckDefAndScriptFailure2(["var x = 'a' .. {a: 1}"], 'E1105:', 'E731:', 1) 1418 call CheckDefAndScriptFailure2(["var x = 'a' .. test_void()"], 'E1105:', 'E908:', 1) 1419 call CheckDefAndScriptFailure2(["var x = 'a' .. 0z32"], 'E1105:', 'E976:', 1) 1420 call CheckDefAndScriptFailure2(["var x = 'a' .. function('len')"], 'E1105:', 'E729:', 1) 1421 call CheckDefAndScriptFailure2(["var x = 'a' .. function('len', ['a'])"], 'E1105:', 'E729:', 1) 1422 1423 call CheckDefAndScriptFailure2(['var x = 1 + v:none'], 'E1051:', 'E611:', 1) 1424 call CheckDefAndScriptFailure2(['var x = 1 + v:null'], 'E1051:', 'E611:', 1) 1425 call CheckDefAndScriptFailure2(['var x = 1 + v:true'], 'E1051:', 'E1138:', 1) 1426 call CheckDefAndScriptFailure2(['var x = 1 + v:false'], 'E1051:', 'E1138:', 1) 1427 call CheckDefAndScriptFailure2(['var x = 1 + true'], 'E1051:', 'E1138:', 1) 1428 call CheckDefAndScriptFailure2(['var x = 1 + false'], 'E1051:', 'E1138:', 1) 1429endfunc 1430 1431func Test_expr5_fails_channel() 1432 CheckFeature channel 1433 call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_job()"], 'E1105:', 'E908:', 1) 1434 call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_channel()"], 'E1105:', 'E908:', 1) 1435endfunc 1436 1437def Test_expr5_list_add() 1438 var lines =<< trim END 1439 # concatenating two lists with same member types is OK 1440 var d = {} 1441 for i in ['a'] + ['b'] 1442 d = {[i]: 0} 1443 endfor 1444 1445 # concatenating two lists with different member types results in "any" 1446 var dany = {} 1447 for i in ['a'] + [12] 1448 dany[i] = i 1449 endfor 1450 assert_equal({a: 'a', 12: 12}, dany) 1451 1452 # result of glob() is "any", runtime type check 1453 var sl: list<string> = glob('*.txt', false, true) + [''] 1454 END 1455 CheckDefAndScriptSuccess(lines) 1456enddef 1457 1458" test multiply, divide, modulo 1459def Test_expr6() 1460 var lines =<< trim END 1461 assert_equal(36, 6 * 6) 1462 assert_equal(24, 6 * 1463 g:thefour) 1464 assert_equal(24, g:thefour 1465 * 6) 1466 assert_equal(40, g:anint * g:thefour) 1467 1468 assert_equal(10, 60 / 6) 1469 assert_equal(6, 60 / 1470 g:anint) 1471 assert_equal(1, g:anint / 6) 1472 assert_equal(2, g:anint 1473 / g:thefour) 1474 1475 assert_equal(5, 11 % 6) 1476 assert_equal(4, g:anint % 6) 1477 assert_equal(3, 13 % 1478 g:anint) 1479 assert_equal(2, g:anint 1480 % g:thefour) 1481 1482 assert_equal(4, 6 * 4 / 6) 1483 1484 var x = [2] 1485 var y = [3] 1486 assert_equal(5, x[0] + y[0]) 1487 assert_equal(6, x[0] * y[0]) 1488 if has('float') 1489 var xf = [2.0] 1490 var yf = [3.0] 1491 assert_equal(5.0, xf[0] 1492 + yf[0]) 1493 assert_equal(6.0, xf[0] 1494 * yf[0]) 1495 endif 1496 END 1497 CheckDefAndScriptSuccess(lines) 1498 1499 CheckDefAndScriptFailure2(["var x = 6 * xxx"], 'E1001:', 'E121:', 1) 1500 CheckDefFailure(["var d = 6 * "], 'E1097:', 3) 1501 CheckScriptFailure(['vim9script', "var d = 6 * "], 'E15:', 2) 1502 1503 CheckDefExecAndScriptFailure(['echo 1 / 0'], 'E1154', 1) 1504 CheckDefExecAndScriptFailure(['echo 1 % 0'], 'E1154', 1) 1505enddef 1506 1507def Test_expr6_vim9script() 1508 # check line continuation 1509 var lines =<< trim END 1510 var name = 11 1511 * 22 1512 / 3 1513 assert_equal(80, name) 1514 END 1515 CheckDefAndScriptSuccess(lines) 1516 1517 lines =<< trim END 1518 var name = 25 1519 % 10 1520 assert_equal(5, name) 1521 END 1522 CheckDefAndScriptSuccess(lines) 1523 1524 lines =<< trim END 1525 var name = 25 1526 # comment 1527 1528 # comment 1529 % 10 1530 assert_equal(5, name) 1531 END 1532 CheckDefAndScriptSuccess(lines) 1533 1534 lines =<< trim END 1535 var name = 11 * 1536 22 / 1537 3 1538 assert_equal(80, name) 1539 END 1540 CheckDefAndScriptSuccess(lines) 1541 1542 # check white space 1543 lines =<< trim END 1544 echo 5*6 1545 END 1546 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1547 1548 lines =<< trim END 1549 echo 5 *6 1550 END 1551 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1552 1553 lines =<< trim END 1554 echo 5* 6 1555 END 1556 CheckDefAndScriptFailure(lines, 'E1004:', 1) 1557enddef 1558 1559def Test_expr6_float() 1560 if !has('float') 1561 MissingFeature 'float' 1562 else 1563 var lines =<< trim END 1564 assert_equal(36.0, 6.0 * 6) 1565 assert_equal(36.0, 6 * 1566 6.0) 1567 assert_equal(36.0, 6.0 * 6.0) 1568 assert_equal(1.0, g:afloat * g:anint) 1569 1570 assert_equal(10.0, 60 / 6.0) 1571 assert_equal(10.0, 60.0 / 1572 6) 1573 assert_equal(10.0, 60.0 / 6.0) 1574 assert_equal(0.01, g:afloat / g:anint) 1575 1576 assert_equal(4.0, 6.0 * 4 / 6) 1577 assert_equal(4.0, 6 * 1578 4.0 / 1579 6) 1580 assert_equal(4.0, 6 * 4 / 6.0) 1581 assert_equal(4.0, 6.0 * 4.0 / 6) 1582 assert_equal(4.0, 6 * 4.0 / 6.0) 1583 assert_equal(4.0, 6.0 * 4 / 6.0) 1584 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1585 1586 assert_equal(4.0, 6.0 * 4.0 / 6.0) 1587 END 1588 CheckDefAndScriptSuccess(lines) 1589 endif 1590enddef 1591 1592func Test_expr6_fails() 1593 let msg = "White space required before and after '*'" 1594 call CheckDefAndScriptFailure(["var x = 1*2"], msg, 1) 1595 call CheckDefAndScriptFailure(["var x = 1 *2"], msg, 1) 1596 call CheckDefAndScriptFailure(["var x = 1* 2"], msg, 1) 1597 1598 let msg = "White space required before and after '/'" 1599 call CheckDefAndScriptFailure(["var x = 1/2"], msg, 1) 1600 call CheckDefAndScriptFailure(["var x = 1 /2"], msg, 1) 1601 call CheckDefAndScriptFailure(["var x = 1/ 2"], msg, 1) 1602 1603 let msg = "White space required before and after '%'" 1604 call CheckDefAndScriptFailure(["var x = 1%2"], msg, 1) 1605 call CheckDefAndScriptFailure(["var x = 1 %2"], msg, 1) 1606 call CheckDefAndScriptFailure(["var x = 1% 2"], msg, 1) 1607 1608 call CheckDefAndScriptFailure2(["var x = '1' * '2'"], 'E1036:', 'E1030:', 1) 1609 call CheckDefAndScriptFailure2(["var x = '1' / '2'"], 'E1036:', 'E1030:', 1) 1610 call CheckDefAndScriptFailure2(["var x = '1' % '2'"], 'E1035:', 'E1030:', 1) 1611 1612 call CheckDefAndScriptFailure2(["var x = 0z01 * 0z12"], 'E1036:', 'E974:', 1) 1613 call CheckDefAndScriptFailure2(["var x = 0z01 / 0z12"], 'E1036:', 'E974:', 1) 1614 call CheckDefAndScriptFailure2(["var x = 0z01 % 0z12"], 'E1035:', 'E974:', 1) 1615 1616 call CheckDefAndScriptFailure2(["var x = [1] * [2]"], 'E1036:', 'E745:', 1) 1617 call CheckDefAndScriptFailure2(["var x = [1] / [2]"], 'E1036:', 'E745:', 1) 1618 call CheckDefAndScriptFailure2(["var x = [1] % [2]"], 'E1035:', 'E745:', 1) 1619 1620 call CheckDefAndScriptFailure2(["var x = {one: 1} * {two: 2}"], 'E1036:', 'E728:', 1) 1621 call CheckDefAndScriptFailure2(["var x = {one: 1} / {two: 2}"], 'E1036:', 'E728:', 1) 1622 call CheckDefAndScriptFailure2(["var x = {one: 1} % {two: 2}"], 'E1035:', 'E728:', 1) 1623 1624 call CheckDefAndScriptFailure2(["var x = 0xff[1]"], 'E1107:', 'E1062:', 1) 1625 if has('float') 1626 call CheckDefAndScriptFailure2(["var x = 0.7[1]"], 'E1107:', 'E806:', 1) 1627 endif 1628 1629 for op in ['*', '/', '%'] 1630 let lines = ['var x = 1', op .. '2', '# comment'] 1631 let msg = printf("E1004: White space required before and after '%s' at \"%s2\"", op, op) 1632 call CheckDefAndScriptFailure(lines, msg, 2) 1633 endfor 1634endfunc 1635 1636func Test_expr6_float_fails() 1637 CheckFeature float 1638 call CheckDefAndScriptFailure2(["var x = 1.0 % 2"], 'E1035:', 'E804:', 1) 1639endfunc 1640 1641" define here to use old style parsing 1642if has('float') 1643 let g:float_zero = 0.0 1644 let g:float_neg = -9.8 1645 let g:float_big = 9.9e99 1646endif 1647let g:blob_empty = 0z 1648let g:blob_one = 0z01 1649let g:blob_long = 0z0102.0304 1650 1651let g:string_empty = '' 1652let g:string_short = 'x' 1653let g:string_long = 'abcdefghijklm' 1654let g:string_special = "ab\ncd\ref\ekk" 1655 1656let g:special_true = v:true 1657let g:special_false = v:false 1658let g:special_null = v:null 1659let g:special_none = v:none 1660 1661let g:list_empty = [] 1662let g:list_mixed = [1, 'b', v:false] 1663 1664let g:dict_empty = {} 1665let g:dict_one = #{one: 1} 1666 1667let $TESTVAR = 'testvar' 1668 1669" type casts 1670def Test_expr7t() 1671 var lines =<< trim END 1672 var ls: list<string> = ['a', <string>g:string_empty] 1673 var ln: list<number> = [<number>g:anint, <number>g:thefour] 1674 var nr = <number>234 1675 assert_equal(234, nr) 1676 var b: bool = <bool>1 1677 assert_equal(true, b) 1678 var text = 1679 <string> 1680 'text' 1681 if false 1682 text = <number>'xxx' 1683 endif 1684 END 1685 CheckDefAndScriptSuccess(lines) 1686 1687 CheckDefAndScriptFailure(["var x = <nr>123"], 'E1010:', 1) 1688 CheckDefFailure(["var x = <number>"], 'E1097:', 3) 1689 CheckDefFailure(["var x = <number>string(1)"], 'E1012:', 1) 1690 CheckScriptFailure(['vim9script', "var x = <number>"], 'E15:', 2) 1691 CheckDefAndScriptFailure(["var x = <number >123"], 'E1068:', 1) 1692 CheckDefAndScriptFailure(["var x = <number 123"], 'E1104:', 1) 1693enddef 1694 1695" test low level expression 1696def Test_expr7_number() 1697 # number constant 1698 var lines =<< trim END 1699 assert_equal(0, 0) 1700 assert_equal(654, 0654) 1701 1702 assert_equal(6, 0x6) 1703 assert_equal(15, 0xf) 1704 assert_equal(255, 0xff) 1705 END 1706 CheckDefAndScriptSuccess(lines) 1707enddef 1708 1709def Test_expr7_float() 1710 # float constant 1711 if !has('float') 1712 MissingFeature 'float' 1713 else 1714 var lines =<< trim END 1715 assert_equal(g:float_zero, .0) 1716 assert_equal(g:float_zero, 0.0) 1717 assert_equal(g:float_neg, -9.8) 1718 assert_equal(g:float_big, 9.9e99) 1719 END 1720 CheckDefAndScriptSuccess(lines) 1721 endif 1722enddef 1723 1724def Test_expr7_blob() 1725 # blob constant 1726 var lines =<< trim END 1727 assert_equal(g:blob_empty, 0z) 1728 assert_equal(g:blob_one, 0z01) 1729 assert_equal(g:blob_long, 0z0102.0304) 1730 1731 var testblob = 0z010203 1732 assert_equal(0x01, testblob[0]) 1733 assert_equal(0x02, testblob[1]) 1734 assert_equal(0x03, testblob[-1]) 1735 assert_equal(0x02, testblob[-2]) 1736 1737 assert_equal(0z01, testblob[0 : 0]) 1738 assert_equal(0z0102, testblob[0 : 1]) 1739 assert_equal(0z010203, testblob[0 : 2]) 1740 assert_equal(0z010203, testblob[0 : ]) 1741 assert_equal(0z0203, testblob[1 : ]) 1742 assert_equal(0z0203, testblob[1 : 2]) 1743 assert_equal(0z0203, testblob[1 : -1]) 1744 assert_equal(0z03, testblob[-1 : -1]) 1745 assert_equal(0z02, testblob[-2 : -2]) 1746 1747 # blob slice accepts out of range 1748 assert_equal(0z, testblob[3 : 3]) 1749 assert_equal(0z, testblob[0 : -4]) 1750 END 1751 CheckDefAndScriptSuccess(lines) 1752 1753 CheckDefAndScriptFailure(["var x = 0z123"], 'E973:', 1) 1754enddef 1755 1756def Test_expr7_string() 1757 # string constant 1758 var lines =<< trim END 1759 assert_equal(g:string_empty, '') 1760 assert_equal(g:string_empty, "") 1761 assert_equal(g:string_short, 'x') 1762 assert_equal(g:string_short, "x") 1763 assert_equal(g:string_long, 'abcdefghijklm') 1764 assert_equal(g:string_long, "abcdefghijklm") 1765 assert_equal(g:string_special, "ab\ncd\ref\ekk") 1766 END 1767 CheckDefAndScriptSuccess(lines) 1768 1769 CheckDefAndScriptFailure(['var x = "abc'], 'E114:', 1) 1770 CheckDefAndScriptFailure(["var x = 'abc"], 'E115:', 1) 1771enddef 1772 1773def Test_expr7_vimvar() 1774 var old: list<string> = v:oldfiles 1775 var compl: dict<any> = v:completed_item 1776 1777 CheckDefFailure(["var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 1) 1778 CheckScriptFailure(['vim9script', 'v:oldfiles = ["foo"]', "var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 3) 1779 new 1780 exec "normal! afoo fo\<C-N>\<Esc>" 1781 CheckDefExecAndScriptFailure(["var old: dict<number> = v:completed_item"], 'E1012: Type mismatch; expected dict<number> but got dict<string>', 1) 1782 bwipe! 1783enddef 1784 1785def Test_expr7_special() 1786 # special constant 1787 var lines =<< trim END 1788 assert_equal(g:special_true, true) 1789 assert_equal(g:special_false, false) 1790 assert_equal(g:special_true, v:true) 1791 assert_equal(g:special_false, v:false) 1792 assert_equal(v:true, true) 1793 assert_equal(v:false, false) 1794 1795 assert_equal(true, !false) 1796 assert_equal(false, !true) 1797 assert_equal(true, !0) 1798 assert_equal(false, !1) 1799 assert_equal(false, !!false) 1800 assert_equal(true, !!true) 1801 assert_equal(false, !!0) 1802 assert_equal(true, !!1) 1803 1804 var t = true 1805 var f = false 1806 assert_equal(true, t) 1807 assert_equal(false, f) 1808 1809 assert_equal(g:special_null, v:null) 1810 assert_equal(g:special_null, null) 1811 assert_equal(g:special_none, v:none) 1812 END 1813 CheckDefAndScriptSuccess(lines) 1814 1815 CheckDefAndScriptFailure(['v:true = true'], 'E46:', 1) 1816 CheckDefAndScriptFailure(['v:true = false'], 'E46:', 1) 1817 CheckDefAndScriptFailure(['v:false = true'], 'E46:', 1) 1818 CheckDefAndScriptFailure(['v:null = 11'], 'E46:', 1) 1819 CheckDefAndScriptFailure(['v:none = 22'], 'E46:', 1) 1820enddef 1821 1822def Test_expr7_list() 1823 # list 1824 var lines =<< trim END 1825 assert_equal(g:list_empty, []) 1826 assert_equal(g:list_empty, [ ]) 1827 1828 var numbers: list<number> = [1, 2, 3] 1829 numbers = [1] 1830 numbers = [] 1831 1832 var strings: list<string> = ['a', 'b', 'c'] 1833 strings = ['x'] 1834 strings = [] 1835 1836 var mixed: list<any> = [1, 'b', false,] 1837 assert_equal(g:list_mixed, mixed) 1838 assert_equal('b', mixed[1]) 1839 1840 echo [1, 1841 2] [3, 1842 4] 1843 1844 var llstring: list<list<string>> = [['text'], []] 1845 llstring = [[], ['text']] 1846 llstring = [[], []] 1847 END 1848 CheckDefAndScriptSuccess(lines) 1849 1850 var rangelist: list<number> = range(3) 1851 g:rangelist = range(3) 1852 CheckDefExecAndScriptFailure(["var x: list<string> = g:rangelist"], 'E1012: Type mismatch; expected list<string> but got list<number>', 1) 1853 1854 CheckDefAndScriptFailure2(["var x = 1234[3]"], 'E1107:', 'E1062:', 1) 1855 CheckDefExecAndScriptFailure(["var x = g:anint[3]"], 'E1062:', 1) 1856 1857 CheckDefAndScriptFailure2(["var x = g:list_mixed[xxx]"], 'E1001:', 'E121:', 1) 1858 1859 CheckDefAndScriptFailure(["var x = [1,2,3]"], 'E1069:', 1) 1860 CheckDefAndScriptFailure(["var x = [1 ,2, 3]"], 'E1068:', 1) 1861 1862 CheckDefExecAndScriptFailure(["echo 1", "var x = [][0]", "echo 3"], 'E684:', 2) 1863 1864 CheckDefExecAndScriptFailure2(["var x = g:list_mixed['xx']"], 'E1012:', 'E1030:', 1) 1865 CheckDefFailure(["var x = g:list_mixed["], 'E1097:', 3) 1866 CheckScriptFailure(['vim9script', "var x = g:list_mixed["], 'E15:', 2) 1867 CheckDefFailure(["var x = g:list_mixed[0"], 'E1097:', 3) 1868 CheckScriptFailure(['vim9script', "var x = g:list_mixed[0"], 'E111:', 2) 1869 CheckDefExecAndScriptFailure(["var x = g:list_empty[3]"], 'E684:', 1) 1870 CheckDefExecAndScriptFailure(["var l: list<number> = [234, 'x']"], 'E1012:', 1) 1871 CheckDefExecAndScriptFailure(["var l: list<number> = ['x', 234]"], 'E1012:', 1) 1872 CheckDefExecAndScriptFailure(["var l: list<string> = [234, 'x']"], 'E1012:', 1) 1873 CheckDefExecAndScriptFailure(["var l: list<string> = ['x', 123]"], 'E1012:', 1) 1874 1875 lines =<< trim END 1876 var datalist: list<string> 1877 def Main() 1878 datalist += ['x'. 1879 enddef 1880 Main() 1881 END 1882 CheckDefAndScriptFailure(lines, 'E1127:') 1883 1884 lines =<< trim END 1885 var numbers = [1, 2, 3, 4] 1886 var a = 1 1887 var b = 2 1888 END 1889 CheckDefAndScriptFailure(lines + ['echo numbers[1:b]'], 1890 'E1004: White space required before and after '':'' at ":b]"', 4) 1891 CheckDefAndScriptFailure(lines + ['echo numbers[1: b]'], 'E1004:', 4) 1892 CheckDefAndScriptFailure(lines + ['echo numbers[a :b]'], 'E1004:', 4) 1893enddef 1894 1895def Test_expr7_list_vim9script() 1896 var lines =<< trim END 1897 var l = [ 1898 11, 1899 22, 1900 ] 1901 assert_equal([11, 22], l) 1902 1903 echo [1, 1904 2] [3, 1905 4] 1906 1907 echo [1, # comment 1908 # comment 1909 2] [3, 1910 # comment 1911 4] 1912 END 1913 CheckDefAndScriptSuccess(lines) 1914 1915 lines =<< trim END 1916 var l = [11, 1917 22] 1918 assert_equal([11, 22], l) 1919 END 1920 CheckDefAndScriptSuccess(lines) 1921 1922 lines =<< trim END 1923 var l = [11,22] 1924 END 1925 CheckDefAndScriptFailure(lines, 'E1069:', 1) 1926 1927 lines =<< trim END 1928 var l = [11 , 22] 1929 END 1930 CheckDefAndScriptFailure(lines, 'E1068:', 1) 1931 1932 lines =<< trim END 1933 var l: list<number> = [234, 'x'] 1934 END 1935 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1936 1937 lines =<< trim END 1938 var l: list<number> = ['x', 234] 1939 END 1940 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1941 1942 lines =<< trim END 1943 var l: list<string> = ['x', 234] 1944 END 1945 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1946 1947 lines =<< trim END 1948 var l: list<string> = [234, 'x'] 1949 END 1950 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1951 1952 lines =<< trim END 1953 def Failing() 1954 job_stop() 1955 enddef 1956 var list = [Failing] 1957 END 1958 if has('channel') 1959 CheckDefAndScriptFailure(lines, 'E119:', 0) 1960 else 1961 CheckDefAndScriptFailure(lines, 'E117:', 0) 1962 endif 1963enddef 1964 1965def LambdaWithComments(): func 1966 return (x) => 1967 # some comment 1968 x == 1 1969 # some comment 1970 || 1971 x == 2 1972enddef 1973 1974def LambdaUsingArg(x: number): func 1975 return () => 1976 # some comment 1977 x == 1 1978 # some comment 1979 || 1980 x == 2 1981enddef 1982 1983def Test_expr7_lambda() 1984 var lines =<< trim END 1985 var La = () => 'result' 1986 # comment 1987 assert_equal('result', La()) 1988 assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val)) 1989 1990 # line continuation inside lambda with "cond ? expr : expr" works 1991 var ll = range(3) 1992 var dll = mapnew(ll, (k, v) => v % 2 ? { 1993 ['111']: 111 } : {} 1994 ) 1995 assert_equal([{}, {111: 111}, {}], dll) 1996 1997 # comment halfway an expression 1998 var Ref = () => 4 1999 # comment 2000 + 6 2001 assert_equal(10, Ref()) 2002 2003 ll = range(3) 2004 map(ll, (k, v) => v == 8 || v 2005 == 9 2006 || v % 2 ? 111 : 222 2007 ) 2008 assert_equal([222, 111, 222], ll) 2009 2010 ll = range(3) 2011 map(ll, (k, v) => v != 8 && v 2012 != 9 2013 && v % 2 == 0 ? 111 : 222 2014 ) 2015 assert_equal([111, 222, 111], ll) 2016 2017 var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] ) 2018 assert_equal([{key: 22}], dl) 2019 2020 dl = [{key: 12}, {['foo']: 34}] 2021 assert_equal([{key: 12}], filter(dl, 2022 (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0)) 2023 2024 assert_equal(false, LambdaWithComments()(0)) 2025 assert_equal(true, LambdaWithComments()(1)) 2026 assert_equal(true, LambdaWithComments()(2)) 2027 assert_equal(false, LambdaWithComments()(3)) 2028 2029 assert_equal(false, LambdaUsingArg(0)()) 2030 assert_equal(true, LambdaUsingArg(1)()) 2031 2032 var res = map([1, 2, 3], (i: number, v: number) => i + v) 2033 assert_equal([1, 3, 5], res) 2034 END 2035 CheckDefAndScriptSuccess(lines) 2036 2037 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:') 2038 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004: White space required before and after ''=>'' at "=> a + 1"') 2039 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 'E1004:') 2040 CheckDefAndScriptFailure2(["var Ref = (a) =< a + 1"], 'E1001:', 'E121:') 2041 CheckDefAndScriptFailure(["var Ref = (a: int) => a + 1"], 'E1010:') 2042 CheckDefAndScriptFailure(["var Ref = (a): int => a + 1"], 'E1010:') 2043 2044 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 2045 # error is in first line of the lambda 2046 CheckDefAndScriptFailure(["var L = (a) => a + b"], 'E1001:', 0) 2047 2048 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 2049 2050 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 'E118:') 2051 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 'E118:') 2052 CheckDefAndScriptFailure2(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 'E121:', 1) 2053 2054 CheckDefAndScriptSuccess(['var Fx = (a) => ({k1: 0,', ' k2: 1})']) 2055 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 2056 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 2057 2058 CheckDefAndScriptSuccess(['var Fx = (a) => [0,', ' 1]']) 2059 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) 2060 2061 # no error for existing script variable when checking for lambda 2062 lines =<< trim END 2063 var name = 0 2064 eval (name + 2) / 3 2065 END 2066 CheckDefAndScriptSuccess(lines) 2067enddef 2068 2069def Test_expr7_lambda_block() 2070 var lines =<< trim END 2071 var Func = (s: string): string => { 2072 return 'hello ' .. s 2073 } 2074 assert_equal('hello there', Func('there')) 2075 2076 var ll = range(3) 2077 var dll = mapnew(ll, (k, v): string => { 2078 if v % 2 2079 return 'yes' 2080 endif 2081 return 'no' 2082 }) 2083 assert_equal(['no', 'yes', 'no'], dll) 2084 2085 # ignored_inline(0, (_) => { 2086 # echo 'body' 2087 # }) 2088 2089 sandbox var Safe = (nr: number): number => { 2090 return nr + 7 2091 } 2092 assert_equal(10, Safe(3)) 2093 END 2094 CheckDefAndScriptSuccess(lines) 2095 2096 lines =<< trim END 2097 map([1, 2], (k, v) => { redrawt }) 2098 END 2099 CheckDefAndScriptFailure(lines, 'E488') 2100 2101 lines =<< trim END 2102 var Func = (nr: int) => { 2103 echo nr 2104 } 2105 END 2106 CheckDefAndScriptFailure(lines, 'E1010', 1) 2107 2108 lines =<< trim END 2109 var Func = (nr: number): int => { 2110 return nr 2111 } 2112 END 2113 CheckDefAndScriptFailure(lines, 'E1010', 1) 2114 2115 lines =<< trim END 2116 var Func = (nr: number): int => { 2117 return nr 2118 END 2119 CheckDefFailure(lines, 'E1171', 0) # line nr is function start 2120 CheckScriptFailure(['vim9script'] + lines, 'E1171', 2) 2121 2122 lines =<< trim END 2123 var Func = (nr: number): int => { 2124 var ll =<< ENDIT 2125 nothing 2126 END 2127 CheckDefFailure(lines, 'E1145: Missing heredoc end marker: ENDIT', 0) 2128 CheckScriptFailure(['vim9script'] + lines, 'E1145: Missing heredoc end marker: ENDIT', 2) 2129enddef 2130 2131def NewLambdaWithComments(): func 2132 return (x) => 2133 # some comment 2134 x == 1 2135 # some comment 2136 || 2137 x == 2 2138enddef 2139 2140def NewLambdaUsingArg(x: number): func 2141 return () => 2142 # some comment 2143 x == 1 2144 # some comment 2145 || 2146 x == 2 2147enddef 2148 2149def Test_expr7_new_lambda() 2150 var lines =<< trim END 2151 var La = () => 'result' 2152 assert_equal('result', La()) 2153 assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val)) 2154 2155 # line continuation inside lambda with "cond ? expr : expr" works 2156 var ll = range(3) 2157 var dll = mapnew(ll, (k, v) => v % 2 ? { 2158 ['111']: 111 } : {} 2159 ) 2160 assert_equal([{}, {111: 111}, {}], dll) 2161 2162 ll = range(3) 2163 map(ll, (k, v) => v == 8 || v 2164 == 9 2165 || v % 2 ? 111 : 222 2166 ) 2167 assert_equal([222, 111, 222], ll) 2168 2169 ll = range(3) 2170 map(ll, (k, v) => v != 8 && v 2171 != 9 2172 && v % 2 == 0 ? 111 : 222 2173 ) 2174 assert_equal([111, 222, 111], ll) 2175 2176 var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] ) 2177 assert_equal([{key: 22}], dl) 2178 2179 dl = [{key: 12}, {['foo']: 34}] 2180 assert_equal([{key: 12}], filter(dl, 2181 (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0)) 2182 2183 assert_equal(false, NewLambdaWithComments()(0)) 2184 assert_equal(true, NewLambdaWithComments()(1)) 2185 assert_equal(true, NewLambdaWithComments()(2)) 2186 assert_equal(false, NewLambdaWithComments()(3)) 2187 2188 assert_equal(false, NewLambdaUsingArg(0)()) 2189 assert_equal(true, NewLambdaUsingArg(1)()) 2190 2191 var res = map([1, 2, 3], (i: number, v: number) => i + v) 2192 assert_equal([1, 3, 5], res) 2193 2194 # Lambda returning a dict 2195 var Lmb = () => ({key: 42}) 2196 assert_equal({key: 42}, Lmb()) 2197 2198 var RefOne: func(number): string = (a: number): string => 'x' 2199 var RefTwo: func(number): any = (a: number): any => 'x' 2200 2201 var Fx = (a) => ({k1: 0, 2202 k2: 1}) 2203 var Fy = (a) => [0, 2204 1] 2205 END 2206 CheckDefAndScriptSuccess(lines) 2207 2208 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:') 2209 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004:') 2210 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 2211 'E1004: White space required before and after ''=>'' at " =>a + 1"') 2212 2213 CheckDefAndScriptFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:') 2214 CheckDefAndScriptFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:') 2215 2216 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 2217 # error is in first line of the lambda 2218 CheckDefAndScriptFailure2(["var L = (a) -> a + b"], 'E1001:', 'E121:', 1) 2219 2220 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 2221 2222 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 2223 'E118: Too many arguments for function:') 2224 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 2225 'E118: Too many arguments for function:') 2226 CheckDefFailure(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 1) 2227 2228 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 2229 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 2230 2231 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) 2232enddef 2233 2234def Test_expr7_lambda_vim9script() 2235 var lines =<< trim END 2236 var v = 10->((a) => 2237 a 2238 + 2 2239 )() 2240 assert_equal(12, v) 2241 END 2242 CheckDefAndScriptSuccess(lines) 2243 2244 # nested lambda with line breaks 2245 lines =<< trim END 2246 search('"', 'cW', 0, 0, () => 2247 synstack('.', col('.')) 2248 ->map((_, v) => synIDattr(v, 'name'))->len()) 2249 END 2250 CheckDefAndScriptSuccess(lines) 2251enddef 2252 2253def Test_expr7_funcref() 2254 var lines =<< trim END 2255 def RetNumber(): number 2256 return 123 2257 enddef 2258 var FuncRef = RetNumber 2259 assert_equal(123, FuncRef()) 2260 END 2261 CheckDefAndScriptSuccess(lines) 2262 2263 lines =<< trim END 2264 vim9script 2265 func g:GlobalFunc() 2266 return 'global' 2267 endfunc 2268 func s:ScriptFunc() 2269 return 'script' 2270 endfunc 2271 def Test() 2272 var Ref = g:GlobalFunc 2273 assert_equal('global', Ref()) 2274 Ref = GlobalFunc 2275 assert_equal('global', Ref()) 2276 2277 Ref = s:ScriptFunc 2278 assert_equal('script', Ref()) 2279 Ref = ScriptFunc 2280 assert_equal('script', Ref()) 2281 enddef 2282 Test() 2283 END 2284 CheckScriptSuccess(lines) 2285enddef 2286 2287let g:test_space_dict = {'': 'empty', ' ': 'space'} 2288let g:test_hash_dict = #{one: 1, two: 2} 2289 2290def Test_expr7_dict() 2291 # dictionary 2292 var lines =<< trim END 2293 assert_equal(g:dict_empty, {}) 2294 assert_equal(g:dict_empty, { }) 2295 assert_equal(g:dict_one, {['one']: 1}) 2296 var key = 'one' 2297 var val = 1 2298 assert_equal(g:dict_one, {[key]: val}) 2299 2300 var numbers: dict<number> = {a: 1, b: 2, c: 3} 2301 numbers = {a: 1} 2302 numbers = {} 2303 2304 var strings: dict<string> = {a: 'a', b: 'b', c: 'c'} 2305 strings = {a: 'x'} 2306 strings = {} 2307 2308 var dash = {xx-x: 8} 2309 assert_equal({['xx-x']: 8}, dash) 2310 2311 var dnr = {8: 8} 2312 assert_equal({['8']: 8}, dnr) 2313 2314 var mixed: dict<any> = {a: 'a', b: 42} 2315 mixed = {a: 'x'} 2316 mixed = {a: 234} 2317 mixed = {} 2318 2319 var dictlist: dict<list<string>> = {absent: [], present: ['hi']} 2320 dictlist = {absent: ['hi'], present: []} 2321 dictlist = {absent: [], present: []} 2322 2323 var dictdict: dict<dict<string>> = {one: {a: 'text'}, two: {}} 2324 dictdict = {one: {}, two: {a: 'text'}} 2325 dictdict = {one: {}, two: {}} 2326 2327 assert_equal({['']: 0}, {[matchstr('string', 'wont match')]: 0}) 2328 2329 assert_equal(g:test_space_dict, {['']: 'empty', [' ']: 'space'}) 2330 assert_equal(g:test_hash_dict, {one: 1, two: 2}) 2331 2332 assert_equal({['a a']: 1, ['b/c']: 2}, {'a a': 1, "b/c": 2}) 2333 2334 var d = {a: () => 3, b: () => 7} 2335 assert_equal(3, d.a()) 2336 assert_equal(7, d.b()) 2337 2338 var cd = { # comment 2339 key: 'val' # comment 2340 } 2341 2342 # different types used for the key 2343 var dkeys = {['key']: 'string', 2344 [12]: 'numberexpr', 2345 34: 'number', 2346 [true]: 'bool'} 2347 assert_equal('string', dkeys['key']) 2348 assert_equal('numberexpr', dkeys[12]) 2349 assert_equal('number', dkeys[34]) 2350 assert_equal('bool', dkeys[true]) 2351 if has('float') 2352 dkeys = {[1.2]: 'floatexpr', [3.4]: 'float'} 2353 assert_equal('floatexpr', dkeys[1.2]) 2354 assert_equal('float', dkeys[3.4]) 2355 endif 2356 2357 # automatic conversion from number to string 2358 var n = 123 2359 var dictnr = {[n]: 1} 2360 2361 # comment to start fold is OK 2362 var x1: number #{{ fold 2363 var x2 = 9 #{{ fold 2364 END 2365 CheckDefAndScriptSuccess(lines) 2366 2367 # legacy syntax doesn't work 2368 CheckDefAndScriptFailure(["var x = #{key: 8}"], 'E1170:', 1) 2369 CheckDefAndScriptFailure(["var x = 'a' #{a: 1}"], 'E1170:', 1) 2370 CheckDefAndScriptFailure(["var x = 'a' .. #{a: 1}"], 'E1170:', 1) 2371 CheckDefAndScriptFailure(["var x = true ? #{a: 1}"], 'E1170:', 1) 2372 2373 CheckDefAndScriptFailure(["var x = {a:8}"], 'E1069:', 1) 2374 CheckDefAndScriptFailure(["var x = {a : 8}"], 'E1068:', 1) 2375 CheckDefAndScriptFailure(["var x = {a :8}"], 'E1068:', 1) 2376 CheckDefAndScriptFailure(["var x = {a: 8 , b: 9}"], 'E1068:', 1) 2377 CheckDefAndScriptFailure(["var x = {a: 1,b: 2}"], 'E1069:', 1) 2378 2379 CheckDefAndScriptFailure(["var x = {xxx}"], 'E720:', 1) 2380 CheckDefAndScriptFailure(["var x = {xxx: 1", "var y = 2"], 'E722:', 2) 2381 CheckDefFailure(["var x = {xxx: 1,"], 'E723:', 2) 2382 CheckScriptFailure(['vim9script', "var x = {xxx: 1,"], 'E723:', 2) 2383 CheckDefAndScriptFailure2(["var x = {['a']: xxx}"], 'E1001:', 'E121:', 1) 2384 CheckDefAndScriptFailure(["var x = {a: 1, a: 2}"], 'E721:', 1) 2385 CheckDefExecAndScriptFailure2(["var x = g:anint.member"], 'E715:', 'E488:', 1) 2386 CheckDefExecAndScriptFailure(["var x = g:dict_empty.member"], 'E716:', 1) 2387 2388 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: 234, b: "1"}'], 'E1012:', 1) 2389 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: "x", b: 134}'], 'E1012:', 1) 2390 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1) 2391 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1) 2392 2393 # invalid types for the key 2394 CheckDefAndScriptFailure2(["var x = {[[1, 2]]: 0}"], 'E1105:', 'E730:', 1) 2395 2396 CheckDefFailure(['var x = ({'], 'E723:', 2) 2397 CheckScriptFailure(['vim9script', 'var x = ({'], 'E723:', 2) 2398 CheckDefExecAndScriptFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1) 2399enddef 2400 2401def Test_expr7_dict_vim9script() 2402 var lines =<< trim END 2403 var d = { 2404 ['one']: 2405 1, 2406 ['two']: 2, 2407 } 2408 assert_equal({one: 1, two: 2}, d) 2409 2410 d = { # comment 2411 ['one']: 2412 # comment 2413 2414 1, 2415 # comment 2416 # comment 2417 ['two']: 2, 2418 } 2419 assert_equal({one: 1, two: 2}, d) 2420 2421 var dd = {k: 123->len()} 2422 assert_equal(3, dd.k) 2423 END 2424 CheckDefAndScriptSuccess(lines) 2425 2426 lines =<< trim END 2427 var d = { ["one"]: "one", ["two"]: "two", } 2428 assert_equal({one: 'one', two: 'two'}, d) 2429 END 2430 CheckDefAndScriptSuccess(lines) 2431 2432 lines =<< trim END 2433 var d = {one: 1, 2434 two: 2, 2435 } 2436 assert_equal({one: 1, two: 2}, d) 2437 END 2438 CheckDefAndScriptSuccess(lines) 2439 2440 lines =<< trim END 2441 var d = {one:1, two: 2} 2442 END 2443 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2444 2445 lines =<< trim END 2446 var d = {one: 1,two: 2} 2447 END 2448 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2449 2450 lines =<< trim END 2451 var d = {one : 1} 2452 END 2453 CheckDefAndScriptFailure(lines, 'E1068:', 1) 2454 2455 lines =<< trim END 2456 var d = {one:1} 2457 END 2458 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2459 2460 lines =<< trim END 2461 var d = {one: 1 , two: 2} 2462 END 2463 CheckDefAndScriptFailure(lines, 'E1068:', 1) 2464 2465 lines =<< trim END 2466 var l: dict<number> = {a: 234, b: 'x'} 2467 END 2468 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2469 2470 lines =<< trim END 2471 var l: dict<number> = {a: 'x', b: 234} 2472 END 2473 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2474 2475 lines =<< trim END 2476 var l: dict<string> = {a: 'x', b: 234} 2477 END 2478 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2479 2480 lines =<< trim END 2481 var l: dict<string> = {a: 234, b: 'x'} 2482 END 2483 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2484 2485 lines =<< trim END 2486 var d = {['a']: 234, ['b': 'x'} 2487 END 2488 CheckDefAndScriptFailure(lines, 'E1139:', 1) 2489 2490 lines =<< trim END 2491 def Func() 2492 var d = {['a']: 234, ['b': 'x'} 2493 enddef 2494 defcompile 2495 END 2496 CheckDefAndScriptFailure(lines, 'E1139:', 0) 2497 2498 lines =<< trim END 2499 var d = {'a': 2500 END 2501 CheckDefFailure(lines, 'E723:', 2) 2502 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2) 2503 2504 lines =<< trim END 2505 def Func() 2506 var d = {'a': 2507 enddef 2508 defcompile 2509 END 2510 CheckDefAndScriptFailure(lines, 'E723:', 0) 2511 2512 lines =<< trim END 2513 def Failing() 2514 job_stop() 2515 enddef 2516 var dict = {name: Failing} 2517 END 2518 if has('channel') 2519 CheckDefAndScriptFailure(lines, 'E119:', 0) 2520 else 2521 CheckDefAndScriptFailure(lines, 'E117:', 0) 2522 endif 2523 2524 lines =<< trim END 2525 vim9script 2526 var x = 99 2527 assert_equal({x: 99}, s:) 2528 END 2529 CheckScriptSuccess(lines) 2530enddef 2531 2532def Test_expr7_call_2bool() 2533 var lines =<< trim END 2534 vim9script 2535 2536 def BrokenCall(nr: number, mode: bool, use: string): void 2537 assert_equal(3, nr) 2538 assert_equal(false, mode) 2539 assert_equal('ab', use) 2540 enddef 2541 2542 def TestBrokenCall(): void 2543 BrokenCall(3, 0, 'ab') 2544 enddef 2545 2546 TestBrokenCall() 2547 END 2548 CheckScriptSuccess(lines) 2549enddef 2550 2551let g:oneString = 'one' 2552 2553def Test_expr_member() 2554 var lines =<< trim END 2555 assert_equal(1, g:dict_one.one) 2556 var d: dict<number> = g:dict_one 2557 assert_equal(1, d['one']) 2558 assert_equal(1, d[ 2559 'one' 2560 ]) 2561 assert_equal(1, d 2562 .one) 2563 d = {1: 1, _: 2} 2564 assert_equal(1, d 2565 .1) 2566 assert_equal(2, d 2567 ._) 2568 2569 # getting the one member should clear the dict after getting the item 2570 assert_equal('one', {one: 'one'}.one) 2571 assert_equal('one', {one: 'one'}[g:oneString]) 2572 END 2573 CheckDefAndScriptSuccess(lines) 2574 2575 CheckDefAndScriptFailure2(["var x = g:dict_one.#$!"], 'E1002:', 'E15:', 1) 2576 CheckDefExecAndScriptFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2) 2577 CheckDefExecAndScriptFailure(["var d: dict<number>", "d = g:list_empty"], 'E1012: Type mismatch; expected dict<number> but got list<unknown>', 2) 2578enddef 2579 2580def Test_expr7_any_index_slice() 2581 var lines =<< trim END 2582 # getting the one member should clear the list only after getting the item 2583 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 2584 2585 # string is permissive, index out of range accepted 2586 g:teststring = 'abcdef' 2587 assert_equal('b', g:teststring[1]) 2588 assert_equal('f', g:teststring[-1]) 2589 assert_equal('', g:teststring[99]) 2590 2591 assert_equal('b', g:teststring[1 : 1]) 2592 assert_equal('bcdef', g:teststring[1 :]) 2593 assert_equal('abcd', g:teststring[: 3]) 2594 assert_equal('cdef', g:teststring[-4 :]) 2595 assert_equal('abcdef', g:teststring[-9 :]) 2596 assert_equal('abcd', g:teststring[: -3]) 2597 assert_equal('', g:teststring[: -9]) 2598 2599 # composing characters are included 2600 g:teststring = 'àéû' 2601 assert_equal('à', g:teststring[0]) 2602 assert_equal('é', g:teststring[1]) 2603 assert_equal('û', g:teststring[2]) 2604 assert_equal('', g:teststring[3]) 2605 assert_equal('', g:teststring[4]) 2606 2607 assert_equal('û', g:teststring[-1]) 2608 assert_equal('é', g:teststring[-2]) 2609 assert_equal('à', g:teststring[-3]) 2610 assert_equal('', g:teststring[-4]) 2611 assert_equal('', g:teststring[-5]) 2612 2613 assert_equal('à', g:teststring[0 : 0]) 2614 assert_equal('é', g:teststring[1 : 1]) 2615 assert_equal('àé', g:teststring[0 : 1]) 2616 assert_equal('àéû', g:teststring[0 : -1]) 2617 assert_equal('àé', g:teststring[0 : -2]) 2618 assert_equal('à', g:teststring[0 : -3]) 2619 assert_equal('', g:teststring[0 : -4]) 2620 assert_equal('', g:teststring[0 : -5]) 2621 assert_equal('àéû', g:teststring[ : ]) 2622 assert_equal('àéû', g:teststring[0 : ]) 2623 assert_equal('éû', g:teststring[1 : ]) 2624 assert_equal('û', g:teststring[2 : ]) 2625 assert_equal('', g:teststring[3 : ]) 2626 assert_equal('', g:teststring[4 : ]) 2627 2628 # blob index cannot be out of range 2629 g:testblob = 0z01ab 2630 assert_equal(0x01, g:testblob[0]) 2631 assert_equal(0xab, g:testblob[1]) 2632 assert_equal(0xab, g:testblob[-1]) 2633 assert_equal(0x01, g:testblob[-2]) 2634 2635 # blob slice accepts out of range 2636 assert_equal(0z01ab, g:testblob[0 : 1]) 2637 assert_equal(0z01, g:testblob[0 : 0]) 2638 assert_equal(0z01, g:testblob[-2 : -2]) 2639 assert_equal(0zab, g:testblob[1 : 1]) 2640 assert_equal(0zab, g:testblob[-1 : -1]) 2641 assert_equal(0z, g:testblob[2 : 2]) 2642 assert_equal(0z, g:testblob[0 : -3]) 2643 2644 # list index cannot be out of range 2645 g:testlist = [0, 1, 2, 3] 2646 assert_equal(0, g:testlist[0]) 2647 assert_equal(1, g:testlist[1]) 2648 assert_equal(3, g:testlist[3]) 2649 assert_equal(3, g:testlist[-1]) 2650 assert_equal(0, g:testlist[-4]) 2651 assert_equal(1, g:testlist[g:theone]) 2652 2653 # list slice accepts out of range 2654 assert_equal([0], g:testlist[0 : 0]) 2655 assert_equal([3], g:testlist[3 : 3]) 2656 assert_equal([0, 1], g:testlist[0 : 1]) 2657 assert_equal([0, 1, 2, 3], g:testlist[0 : 3]) 2658 assert_equal([0, 1, 2, 3], g:testlist[0 : 9]) 2659 assert_equal([], g:testlist[-1 : 1]) 2660 assert_equal([1], g:testlist[-3 : 1]) 2661 assert_equal([0, 1], g:testlist[-4 : 1]) 2662 assert_equal([0, 1], g:testlist[-9 : 1]) 2663 assert_equal([1, 2, 3], g:testlist[1 : -1]) 2664 assert_equal([1], g:testlist[1 : -3]) 2665 assert_equal([], g:testlist[1 : -4]) 2666 assert_equal([], g:testlist[1 : -9]) 2667 2668 g:testdict = {a: 1, b: 2} 2669 assert_equal(1, g:testdict['a']) 2670 assert_equal(2, g:testdict['b']) 2671 END 2672 2673 CheckDefAndScriptSuccess(lines) 2674 2675 CheckDefExecAndScriptFailure(['echo g:testblob[2]'], 'E979:', 1) 2676 CheckDefExecAndScriptFailure(['echo g:testblob[-3]'], 'E979:', 1) 2677 2678 CheckDefExecAndScriptFailure(['echo g:testlist[4]'], 'E684: list index out of range: 4', 1) 2679 CheckDefExecAndScriptFailure(['echo g:testlist[-5]'], 'E684:', 1) 2680 2681 CheckDefExecAndScriptFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1) 2682 CheckDefExecAndScriptFailure(['echo g:testdict[1]'], 'E716:', 1) 2683 2684 unlet g:teststring 2685 unlet g:testblob 2686 unlet g:testlist 2687enddef 2688 2689def Test_expr_member_vim9script() 2690 var lines =<< trim END 2691 var d = {one: 2692 'one', 2693 two: 'two', 2694 1: 1, 2695 _: 2} 2696 assert_equal('one', d.one) 2697 assert_equal('one', d 2698 .one) 2699 assert_equal(1, d 2700 .1) 2701 assert_equal(2, d 2702 ._) 2703 assert_equal('one', d[ 2704 'one' 2705 ]) 2706 END 2707 CheckDefAndScriptSuccess(lines) 2708 2709 lines =<< trim END 2710 var l = [1, 2711 2, 2712 3, 4 2713 ] 2714 assert_equal(2, l[ 2715 1 2716 ]) 2717 assert_equal([2, 3], l[1 : 2]) 2718 assert_equal([1, 2, 3], l[ 2719 : 2720 2 2721 ]) 2722 assert_equal([3, 4], l[ 2723 2 2724 : 2725 ]) 2726 END 2727 CheckDefAndScriptSuccess(lines) 2728enddef 2729 2730def SetSomeVar() 2731 b:someVar = &fdm 2732enddef 2733 2734def Test_expr7_option() 2735 var lines =<< trim END 2736 # option 2737 set ts=11 2738 assert_equal(11, &ts) 2739 &ts = 9 2740 assert_equal(9, &ts) 2741 set ts=8 2742 set grepprg=some\ text 2743 assert_equal('some text', &grepprg) 2744 &grepprg = test_null_string() 2745 assert_equal('', &grepprg) 2746 set grepprg& 2747 2748 # check matching type 2749 var bval: bool = &tgc 2750 var nval: number = &ts 2751 var sval: string = &path 2752 2753 # check v_lock is cleared (requires using valgrind, doesn't always show) 2754 SetSomeVar() 2755 b:someVar = 0 2756 unlet b:someVar 2757 END 2758 CheckDefAndScriptSuccess(lines) 2759enddef 2760 2761def Test_expr7_environment() 2762 var lines =<< trim END 2763 # environment variable 2764 assert_equal('testvar', $TESTVAR) 2765 assert_equal('', $ASDF_ASD_XXX) 2766 END 2767 CheckDefAndScriptSuccess(lines) 2768 2769 CheckDefAndScriptFailure2(["var x = $$$"], 'E1002:', 'E15:', 1) 2770enddef 2771 2772def Test_expr7_register() 2773 var lines =<< trim END 2774 @a = 'register a' 2775 assert_equal('register a', @a) 2776 2777 var fname = expand('%') 2778 assert_equal(fname, @%) 2779 2780 feedkeys(":echo 'some'\<CR>", "xt") 2781 assert_equal("echo 'some'", @:) 2782 2783 normal axyz 2784 assert_equal("xyz", @.) 2785 2786 @/ = 'slash' 2787 assert_equal('slash', @/) 2788 2789 @= = 'equal' 2790 assert_equal('equal', @=) 2791 END 2792 CheckDefAndScriptSuccess(lines) 2793 2794 CheckDefAndScriptFailure2(["@. = 'yes'"], 'E354:', 'E488:', 1) 2795enddef 2796 2797" This is slow when run under valgrind. 2798def Test_expr7_namespace() 2799 var lines =<< trim END 2800 g:some_var = 'some' 2801 assert_equal('some', get(g:, 'some_var')) 2802 assert_equal('some', get(g:, 'some_var', 'xxx')) 2803 assert_equal('xxx', get(g:, 'no_var', 'xxx')) 2804 unlet g:some_var 2805 2806 b:some_var = 'some' 2807 assert_equal('some', get(b:, 'some_var')) 2808 assert_equal('some', get(b:, 'some_var', 'xxx')) 2809 assert_equal('xxx', get(b:, 'no_var', 'xxx')) 2810 unlet b:some_var 2811 2812 w:some_var = 'some' 2813 assert_equal('some', get(w:, 'some_var')) 2814 assert_equal('some', get(w:, 'some_var', 'xxx')) 2815 assert_equal('xxx', get(w:, 'no_var', 'xxx')) 2816 unlet w:some_var 2817 2818 t:some_var = 'some' 2819 assert_equal('some', get(t:, 'some_var')) 2820 assert_equal('some', get(t:, 'some_var', 'xxx')) 2821 assert_equal('xxx', get(t:, 'no_var', 'xxx')) 2822 unlet t:some_var 2823 2824 # check using g: in a for loop more than DO_NOT_FREE_CNT times 2825 for i in range(100000) 2826 if has_key(g:, 'does-not-exist') 2827 endif 2828 endfor 2829 END 2830 CheckDefAndScriptSuccess(lines) 2831enddef 2832 2833def Test_expr7_parens() 2834 # (expr) 2835 var lines =<< trim END 2836 assert_equal(4, (6 * 4) / 6) 2837 assert_equal(0, 6 * ( 4 / 6 )) 2838 2839 assert_equal(6, +6) 2840 assert_equal(-6, -6) 2841 assert_equal(false, !-3) 2842 assert_equal(true, !+0) 2843 2844 assert_equal(7, 5 + ( 2845 2)) 2846 assert_equal(7, 5 + ( 2847 2 2848 )) 2849 assert_equal(7, 5 + ( # comment 2850 2)) 2851 assert_equal(7, 5 + ( # comment 2852 # comment 2853 2)) 2854 2855 var s = ( 2856 'one' 2857 .. 2858 'two' 2859 ) 2860 assert_equal('onetwo', s) 2861 END 2862 CheckDefAndScriptSuccess(lines) 2863enddef 2864 2865def Test_expr7_negate_add() 2866 var lines =<< trim END 2867 assert_equal(-99, -99) 2868 assert_equal(-99, - 99) 2869 assert_equal(99, +99) 2870 2871 var nr = 88 2872 assert_equal(-88, -nr) 2873 assert_equal(-88, - nr) 2874 assert_equal(88, + nr) 2875 END 2876 CheckDefAndScriptSuccess(lines) 2877 2878 lines =<< trim END 2879 var n = 12 2880 echo ++n 2881 END 2882 CheckDefAndScriptFailure(lines, 'E15:') 2883 lines =<< trim END 2884 var n = 12 2885 echo --n 2886 END 2887 CheckDefAndScriptFailure(lines, 'E15:') 2888 lines =<< trim END 2889 var n = 12 2890 echo +-n 2891 END 2892 CheckDefAndScriptFailure(lines, 'E15:') 2893 lines =<< trim END 2894 var n = 12 2895 echo -+n 2896 END 2897 CheckDefAndScriptFailure(lines, 'E15:') 2898 lines =<< trim END 2899 var n = 12 2900 echo - -n 2901 END 2902 CheckDefAndScriptFailure(lines, 'E15:') 2903 lines =<< trim END 2904 var n = 12 2905 echo + +n 2906 END 2907 CheckDefAndScriptFailure(lines, 'E15:') 2908enddef 2909 2910def LegacyReturn(): string 2911 legacy return #{key: 'ok'}.key 2912enddef 2913 2914def Test_expr7_legacy_script() 2915 var lines =<< trim END 2916 let s:legacy = 'legacy' 2917 def GetLocal(): string 2918 return legacy 2919 enddef 2920 def GetLocalPrefix(): string 2921 return s:legacy 2922 enddef 2923 call assert_equal('legacy', GetLocal()) 2924 call assert_equal('legacy', GetLocalPrefix()) 2925 END 2926 CheckScriptSuccess(lines) 2927 2928 assert_equal('ok', LegacyReturn()) 2929 2930 lines =<< trim END 2931 vim9script 2932 def GetNumber(): number 2933 legacy return range(3)->map('v:val + 1') 2934 enddef 2935 echo GetNumber() 2936 END 2937 CheckScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<number>') 2938enddef 2939 2940def Echo(arg: any): string 2941 return arg 2942enddef 2943 2944def s:Echo4Arg(arg: any): string 2945 return arg 2946enddef 2947 2948def Test_expr7_call() 2949 var lines =<< trim END 2950 assert_equal('yes', 'yes'->Echo()) 2951 assert_equal(true, !range(5)->empty()) 2952 assert_equal([0, 1, 2], 3->range()) 2953 END 2954 CheckDefAndScriptSuccess(lines) 2955 2956 assert_equal('yes', 'yes' 2957 ->s:Echo4Arg()) 2958 2959 CheckDefAndScriptFailure(["var x = 'yes'->Echo"], 'E107:', 1) 2960 CheckDefAndScriptFailure2([ 2961 "var x = substitute ('x', 'x', 'x', 'x')" 2962 ], 'E1001:', 'E121:', 1) 2963 CheckDefAndScriptFailure2(["var Ref = function('len' [1, 2])"], 'E1123:', 'E116:', 1) 2964 2965 var auto_lines =<< trim END 2966 def g:some#func(): string 2967 return 'found' 2968 enddef 2969 END 2970 mkdir('Xruntime/autoload', 'p') 2971 writefile(auto_lines, 'Xruntime/autoload/some.vim') 2972 var save_rtp = &rtp 2973 &rtp = getcwd() .. '/Xruntime,' .. &rtp 2974 assert_equal('found', g:some#func()) 2975 assert_equal('found', some#func()) 2976 2977 &rtp = save_rtp 2978 delete('Xruntime', 'rf') 2979enddef 2980 2981def Test_expr7_method_call() 2982 var lines =<< trim END 2983 new 2984 setline(1, ['first', 'last']) 2985 'second'->append(1) 2986 "third"->append(2) 2987 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$')) 2988 bwipe! 2989 2990 var bufnr = bufnr() 2991 var loclist = [{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 2992 loclist->setloclist(0) 2993 assert_equal([{bufnr: bufnr, 2994 lnum: 42, 2995 end_lnum: 0, 2996 col: 17, 2997 end_col: 0, 2998 text: 'wrong', 2999 pattern: '', 3000 valid: 1, 3001 vcol: 0, 3002 nr: 0, 3003 type: '', 3004 module: ''} 3005 ], getloclist(0)) 3006 3007 var result: bool = get({n: 0}, 'n', 0) 3008 assert_equal(false, result) 3009 3010 assert_equal('+string+', 'string'->((s) => '+' .. s .. '+')()) 3011 assert_equal('-text-', 'text'->((s, c) => c .. s .. c)('-')) 3012 3013 var Join = (l) => join(l, 'x') 3014 assert_equal('axb', ['a', 'b']->(Join)()) 3015 3016 var sorted = [3, 1, 2] 3017 -> sort() 3018 assert_equal([1, 2, 3], sorted) 3019 END 3020 CheckDefAndScriptSuccess(lines) 3021 3022 lines =<< trim END 3023 def RetVoid() 3024 enddef 3025 RetVoid()->byteidx(3) 3026 END 3027 CheckDefExecFailure(lines, 'E1013:') 3028enddef 3029 3030 3031def Test_expr7_not() 3032 var lines =<< trim END 3033 assert_equal(true, !'') 3034 assert_equal(true, ![]) 3035 assert_equal(false, !'asdf') 3036 assert_equal(false, ![2]) 3037 assert_equal(true, !!'asdf') 3038 assert_equal(true, !![2]) 3039 3040 assert_equal(true, ! false) 3041 assert_equal(true, !! true) 3042 assert_equal(true, ! ! true) 3043 assert_equal(true, !!! false) 3044 assert_equal(true, ! ! ! false) 3045 3046 g:true = true 3047 g:false = false 3048 assert_equal(true, ! g:false) 3049 assert_equal(true, !! g:true) 3050 assert_equal(true, ! ! g:true) 3051 assert_equal(true, !!! g:false) 3052 assert_equal(true, ! ! ! g:false) 3053 unlet g:true 3054 unlet g:false 3055 3056 assert_equal(true, !test_null_partial()) 3057 assert_equal(false, !() => 'yes') 3058 3059 assert_equal(true, !test_null_dict()) 3060 assert_equal(true, !{}) 3061 assert_equal(false, !{yes: 'no'}) 3062 3063 if has('channel') 3064 assert_equal(true, !test_null_job()) 3065 assert_equal(true, !test_null_channel()) 3066 endif 3067 3068 assert_equal(true, !test_null_blob()) 3069 assert_equal(true, !0z) 3070 assert_equal(false, !0z01) 3071 3072 assert_equal(true, !test_void()) 3073 assert_equal(true, !test_unknown()) 3074 3075 assert_equal(false, ![1, 2, 3]->reverse()) 3076 assert_equal(true, ![]->reverse()) 3077 END 3078 CheckDefAndScriptSuccess(lines) 3079enddef 3080 3081func Test_expr7_fails() 3082 call CheckDefFailure(["var x = (12"], "E1097:", 3) 3083 call CheckScriptFailure(['vim9script', "var x = (12"], 'E110:', 2) 3084 3085 call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1) 3086 call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1) 3087 call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1) 3088 call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E39:", 'E745:', 1) 3089 call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E39:", 'E728:', 1) 3090 3091 call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1) 3092 call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1) 3093 3094 call CheckDefFailure(["var x = [1, 2"], "E697:", 2) 3095 call CheckScriptFailure(['vim9script', "var x = [1, 2"], 'E696:', 2) 3096 3097 call CheckDefAndScriptFailure2(["var x = [notfound]"], "E1001:", 'E121:', 1) 3098 3099 call CheckDefAndScriptFailure(["var X = () => 123)"], 'E488:', 1) 3100 call CheckDefAndScriptFailure(["var x = 123->((x) => x + 5)"], "E107:", 1) 3101 3102 call CheckDefAndScriptFailure(["var x = ¬exist"], 'E113:', 1) 3103 call CheckDefAndScriptFailure2(["&grepprg = [343]"], 'E1012:', 'E730:', 1) 3104 3105 call CheckDefExecAndScriptFailure(["echo s:doesnt_exist"], 'E121:', 1) 3106 call CheckDefExecAndScriptFailure(["echo g:doesnt_exist"], 'E121:', 1) 3107 3108 call CheckDefAndScriptFailure2(["echo a:somevar"], 'E1075:', 'E121:', 1) 3109 call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1) 3110 call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1) 3111 3112 call CheckDefExecAndScriptFailure(["var x = +g:astring"], 'E1030:', 1) 3113 call CheckDefExecAndScriptFailure(["var x = +g:ablob"], 'E974:', 1) 3114 call CheckDefExecAndScriptFailure(["var x = +g:alist"], 'E745:', 1) 3115 call CheckDefExecAndScriptFailure(["var x = +g:adict"], 'E728:', 1) 3116 3117 call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E715:', 'E488:', 2) 3118 3119 call CheckDefAndScriptFailure2(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 'E260: Missing name after ->', 1) 3120 3121 call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2) 3122 call CheckScriptFailure(['vim9script', "[1, 2->len()"], 'E696:', 2) 3123 3124 call CheckDefFailure(["{a: 1->len()"], 'E723:', 2) 3125 call CheckScriptFailure(['vim9script', "{a: 1->len()"], 'E722:', 2) 3126 3127 call CheckDefExecFailure(["{['a']: 1->len()"], 'E723:', 2) 3128 call CheckScriptFailure(['vim9script', "{['a']: 1->len()"], 'E722:', 2) 3129endfunc 3130 3131let g:Funcrefs = [function('add')] 3132 3133func CallMe(arg) 3134 return a:arg 3135endfunc 3136 3137func CallMe2(one, two) 3138 return a:one .. a:two 3139endfunc 3140 3141def Test_expr7_trailing() 3142 var lines =<< trim END 3143 # user function call 3144 assert_equal(123, g:CallMe(123)) 3145 assert_equal(123, g:CallMe( 123)) 3146 assert_equal(123, g:CallMe(123 )) 3147 assert_equal('yesno', g:CallMe2('yes', 'no')) 3148 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 3149 assert_equal('nothing', g:CallMe('nothing')) 3150 3151 # partial call 3152 var Part = function('g:CallMe') 3153 assert_equal('yes', Part('yes')) 3154 3155 # funcref call, using list index 3156 var l = [] 3157 g:Funcrefs[0](l, 2) 3158 assert_equal([2], l) 3159 3160 # method call 3161 l = [2, 5, 6] 3162 l->map((k, v) => k + v) 3163 assert_equal([2, 6, 8], l) 3164 3165 # lambda method call 3166 l = [2, 5] 3167 l->((ll) => add(ll, 8))() 3168 assert_equal([2, 5, 8], l) 3169 3170 # dict member 3171 var d = {key: 123} 3172 assert_equal(123, d.key) 3173 END 3174 CheckDefAndScriptSuccess(lines) 3175enddef 3176 3177def Test_expr7_string_subscript() 3178 var lines =<< trim END 3179 var text = 'abcdef' 3180 assert_equal('f', text[-1]) 3181 assert_equal('a', text[0]) 3182 assert_equal('e', text[4]) 3183 assert_equal('f', text[5]) 3184 assert_equal('', text[6]) 3185 3186 text = 'ábçdë' 3187 assert_equal('ë', text[-1]) 3188 assert_equal('d', text[-2]) 3189 assert_equal('ç', text[-3]) 3190 assert_equal('b', text[-4]) 3191 assert_equal('á', text[-5]) 3192 assert_equal('', text[-6]) 3193 3194 text = 'ábçdëf' 3195 assert_equal('', text[-999]) 3196 assert_equal('f', text[-1]) 3197 assert_equal('á', text[0]) 3198 assert_equal('b', text[1]) 3199 assert_equal('ç', text[2]) 3200 assert_equal('d', text[3]) 3201 assert_equal('ë', text[4]) 3202 assert_equal('f', text[5]) 3203 assert_equal('', text[6]) 3204 assert_equal('', text[999]) 3205 3206 assert_equal('ábçdëf', text[0 : -1]) 3207 assert_equal('ábçdëf', text[0 : -1]) 3208 assert_equal('ábçdëf', text[0 : -1]) 3209 assert_equal('ábçdëf', text[0 : -1]) 3210 assert_equal('ábçdëf', text[0 3211 : -1]) 3212 assert_equal('ábçdëf', text[0 : 3213 -1]) 3214 assert_equal('ábçdëf', text[0 : -1 3215 ]) 3216 assert_equal('bçdëf', text[1 : -1]) 3217 assert_equal('çdëf', text[2 : -1]) 3218 assert_equal('dëf', text[3 : -1]) 3219 assert_equal('ëf', text[4 : -1]) 3220 assert_equal('f', text[5 : -1]) 3221 assert_equal('', text[6 : -1]) 3222 assert_equal('', text[999 : -1]) 3223 3224 assert_equal('ábçd', text[: 3]) 3225 assert_equal('bçdëf', text[1 :]) 3226 assert_equal('ábçdëf', text[:]) 3227 3228 assert_equal('a', g:astring[0]) 3229 assert_equal('sd', g:astring[1 : 2]) 3230 assert_equal('asdf', g:astring[:]) 3231 END 3232 CheckDefAndScriptSuccess(lines) 3233 3234 lines =<< trim END 3235 var d = 'asdf'[1 : 3236 END 3237 CheckDefFailure(lines, 'E1097:', 3) 3238 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2) 3239 3240 lines =<< trim END 3241 var d = 'asdf'[1 : xxx] 3242 END 3243 CheckDefAndScriptFailure2(lines, 'E1001:', 'E121:', 1) 3244 3245 lines =<< trim END 3246 var d = 'asdf'[1 : 2 3247 END 3248 CheckDefFailure(lines, 'E1097:', 3) 3249 CheckScriptFailure(['vim9script'] + lines, 'E111:', 2) 3250 3251 lines =<< trim END 3252 var d = 'asdf'[1 : 2 3253 echo d 3254 END 3255 CheckDefAndScriptFailure(lines, 'E111:', 2) 3256 3257 lines =<< trim END 3258 var d = 'asdf'['1'] 3259 echo d 3260 END 3261 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1) 3262 3263 lines =<< trim END 3264 var d = 'asdf'['1' : 2] 3265 echo d 3266 END 3267 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1) 3268 3269 lines =<< trim END 3270 var d = 'asdf'[1 : '2'] 3271 echo d 3272 END 3273 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "2"', 1) 3274enddef 3275 3276def Test_expr7_list_subscript() 3277 var lines =<< trim END 3278 var list = [0, 1, 2, 3, 4] 3279 assert_equal(0, list[0]) 3280 assert_equal(4, list[4]) 3281 assert_equal(4, list[-1]) 3282 assert_equal(0, list[-5]) 3283 3284 assert_equal([0, 1, 2, 3, 4], list[0 : 4]) 3285 assert_equal([0, 1, 2, 3, 4], list[:]) 3286 assert_equal([1, 2, 3, 4], list[1 :]) 3287 assert_equal([2, 3, 4], list[2 : -1]) 3288 assert_equal([4], list[4 : -1]) 3289 assert_equal([], list[5 : -1]) 3290 assert_equal([], list[999 : -1]) 3291 assert_equal([1, 2, 3, 4], list[g:theone : g:thefour]) 3292 3293 assert_equal([0, 1, 2, 3], list[0 : 3]) 3294 assert_equal([0], list[0 : 0]) 3295 assert_equal([0, 1, 2, 3, 4], list[0 : -1]) 3296 assert_equal([0, 1, 2], list[0 : -3]) 3297 assert_equal([0], list[0 : -5]) 3298 assert_equal([], list[0 : -6]) 3299 assert_equal([], list[0 : -99]) 3300 3301 assert_equal(2, g:alist[0]) 3302 assert_equal([2, 3, 4], g:alist[:]) 3303 END 3304 CheckDefAndScriptSuccess(lines) 3305 3306 lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 3307 CheckDefExecAndScriptFailure2(lines, 'E1012:', 'E1030:', 2) 3308 3309 lines =<< trim END 3310 var ld = [] 3311 def Func() 3312 eval ld[0].key 3313 enddef 3314 defcompile 3315 END 3316 CheckDefAndScriptSuccess(lines) 3317enddef 3318 3319def Test_expr7_dict_subscript() 3320 var lines =<< trim END 3321 var l = [{lnum: 2}, {lnum: 1}] 3322 var res = l[0].lnum > l[1].lnum 3323 assert_true(res) 3324 3325 assert_equal(2, g:adict['aaa']) 3326 assert_equal(8, g:adict.bbb) 3327 3328 var dd = {} 3329 def Func1() 3330 eval dd.key1.key2 3331 enddef 3332 def Func2() 3333 eval dd['key1'].key2 3334 enddef 3335 defcompile 3336 END 3337 CheckDefAndScriptSuccess(lines) 3338enddef 3339 3340def Test_expr7_blob_subscript() 3341 var lines =<< trim END 3342 var b = 0z112233 3343 assert_equal(0x11, b[0]) 3344 assert_equal(0z112233, b[:]) 3345 3346 assert_equal(0x01, g:ablob[0]) 3347 assert_equal(0z01ab, g:ablob[:]) 3348 END 3349 CheckDefAndScriptSuccess(lines) 3350enddef 3351 3352def Test_expr7_subscript_linebreak() 3353 var lines =<< trim END 3354 var range = range( 3355 3) 3356 var l = range 3357 ->mapnew('string(v:key)') 3358 assert_equal(['0', '1', '2'], l) 3359 3360 l = range 3361 ->mapnew('string(v:key)') 3362 assert_equal(['0', '1', '2'], l) 3363 3364 l = range # comment 3365 ->mapnew('string(v:key)') 3366 assert_equal(['0', '1', '2'], l) 3367 3368 l = range 3369 3370 ->mapnew('string(v:key)') 3371 assert_equal(['0', '1', '2'], l) 3372 3373 l = range 3374 # comment 3375 ->mapnew('string(v:key)') 3376 assert_equal(['0', '1', '2'], l) 3377 3378 assert_equal('1', l[ 3379 1]) 3380 3381 var d = {one: 33} 3382 assert_equal(33, d 3383 .one) 3384 END 3385 CheckDefAndScriptSuccess(lines) 3386 3387 lines =<< trim END 3388 var d = {one: 33} 3389 assert_equal(33, d. 3390 one) 3391 END 3392 CheckDefAndScriptFailure2(lines, 'E1127:', 'E116:', 2) 3393enddef 3394 3395func Test_expr7_trailing_fails() 3396 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8))'], 'E107:', 2) 3397 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8)) ()'], 'E274:', 2) 3398endfunc 3399 3400func Test_expr_fails() 3401 call CheckDefAndScriptFailure(["var x = '1'is2"], 'E488:', 1) 3402 call CheckDefAndScriptFailure(["var x = '1'isnot2"], 'E488:', 1) 3403 3404 call CheckDefAndScriptFailure2(["CallMe ('yes')"], 'E476:', 'E492:', 1) 3405 3406 call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1) 3407 3408 call CheckDefAndScriptFailure2(["v:nosuch += 3"], 'E1001:', 'E121:', 1) 3409 call CheckDefAndScriptFailure(["var v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1) 3410 call CheckDefAndScriptFailure2(["var asdf = v:nosuch"], 'E1001:', 'E121:', 1) 3411 3412 call CheckDefFailure(["echo len('asdf'"], 'E110:', 2) 3413 call CheckScriptFailure(['vim9script', "echo len('asdf'"], 'E116:', 2) 3414 3415 call CheckDefAndScriptFailure2(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 'E117:', 1) 3416 call CheckDefAndScriptFailure(["echo doesnotexist()"], 'E117:', 1) 3417endfunc 3418 3419" vim: shiftwidth=2 sts=2 expandtab 3420