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