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 text = 1584 <string> 1585 'text' 1586 if false 1587 text = <number>'xxx' 1588 endif 1589 END 1590 CheckDefAndScriptSuccess(lines) 1591 1592 CheckDefAndScriptFailure(["var x = <nr>123"], 'E1010:', 1) 1593 CheckDefFailure(["var x = <number>"], 'E1097:', 3) 1594 CheckScriptFailure(['vim9script', "var x = <number>"], 'E15:', 2) 1595 CheckDefAndScriptFailure(["var x = <number >123"], 'E1068:', 1) 1596 CheckDefAndScriptFailure(["var x = <number 123"], 'E1104:', 1) 1597enddef 1598 1599" test low level expression 1600def Test_expr7_number() 1601 # number constant 1602 var lines =<< trim END 1603 assert_equal(0, 0) 1604 assert_equal(654, 0654) 1605 1606 assert_equal(6, 0x6) 1607 assert_equal(15, 0xf) 1608 assert_equal(255, 0xff) 1609 END 1610 CheckDefAndScriptSuccess(lines) 1611enddef 1612 1613def Test_expr7_float() 1614 # float constant 1615 if !has('float') 1616 MissingFeature 'float' 1617 else 1618 var lines =<< trim END 1619 assert_equal(g:float_zero, .0) 1620 assert_equal(g:float_zero, 0.0) 1621 assert_equal(g:float_neg, -9.8) 1622 assert_equal(g:float_big, 9.9e99) 1623 END 1624 CheckDefAndScriptSuccess(lines) 1625 endif 1626enddef 1627 1628def Test_expr7_blob() 1629 # blob constant 1630 var lines =<< trim END 1631 assert_equal(g:blob_empty, 0z) 1632 assert_equal(g:blob_one, 0z01) 1633 assert_equal(g:blob_long, 0z0102.0304) 1634 1635 var testblob = 0z010203 1636 assert_equal(0x01, testblob[0]) 1637 assert_equal(0x02, testblob[1]) 1638 assert_equal(0x03, testblob[-1]) 1639 assert_equal(0x02, testblob[-2]) 1640 1641 assert_equal(0z01, testblob[0 : 0]) 1642 assert_equal(0z0102, testblob[0 : 1]) 1643 assert_equal(0z010203, testblob[0 : 2]) 1644 assert_equal(0z010203, testblob[0 : ]) 1645 assert_equal(0z0203, testblob[1 : ]) 1646 assert_equal(0z0203, testblob[1 : 2]) 1647 assert_equal(0z0203, testblob[1 : -1]) 1648 assert_equal(0z03, testblob[-1 : -1]) 1649 assert_equal(0z02, testblob[-2 : -2]) 1650 1651 # blob slice accepts out of range 1652 assert_equal(0z, testblob[3 : 3]) 1653 assert_equal(0z, testblob[0 : -4]) 1654 END 1655 CheckDefAndScriptSuccess(lines) 1656 1657 CheckDefAndScriptFailure(["var x = 0z123"], 'E973:', 1) 1658enddef 1659 1660def Test_expr7_string() 1661 # string constant 1662 var lines =<< trim END 1663 assert_equal(g:string_empty, '') 1664 assert_equal(g:string_empty, "") 1665 assert_equal(g:string_short, 'x') 1666 assert_equal(g:string_short, "x") 1667 assert_equal(g:string_long, 'abcdefghijklm') 1668 assert_equal(g:string_long, "abcdefghijklm") 1669 assert_equal(g:string_special, "ab\ncd\ref\ekk") 1670 END 1671 CheckDefAndScriptSuccess(lines) 1672 1673 CheckDefAndScriptFailure(['var x = "abc'], 'E114:', 1) 1674 CheckDefAndScriptFailure(["var x = 'abc"], 'E115:', 1) 1675enddef 1676 1677def Test_expr7_vimvar() 1678 var old: list<string> = v:oldfiles 1679 var compl: dict<any> = v:completed_item 1680 1681 CheckDefFailure(["var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 1) 1682 CheckScriptFailure(['vim9script', 'v:oldfiles = ["foo"]', "var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 3) 1683 new 1684 exec "normal! afoo fo\<C-N>\<Esc>" 1685 CheckDefExecAndScriptFailure(["var old: dict<number> = v:completed_item"], 'E1012: Type mismatch; expected dict<number> but got dict<string>', 1) 1686 bwipe! 1687enddef 1688 1689def Test_expr7_special() 1690 # special constant 1691 var lines =<< trim END 1692 assert_equal(g:special_true, true) 1693 assert_equal(g:special_false, false) 1694 assert_equal(g:special_true, v:true) 1695 assert_equal(g:special_false, v:false) 1696 assert_equal(v:true, true) 1697 assert_equal(v:false, false) 1698 1699 assert_equal(true, !false) 1700 assert_equal(false, !true) 1701 assert_equal(true, !0) 1702 assert_equal(false, !1) 1703 assert_equal(false, !!false) 1704 assert_equal(true, !!true) 1705 assert_equal(false, !!0) 1706 assert_equal(true, !!1) 1707 1708 var t = true 1709 var f = false 1710 assert_equal(true, t) 1711 assert_equal(false, f) 1712 1713 assert_equal(g:special_null, v:null) 1714 assert_equal(g:special_null, null) 1715 assert_equal(g:special_none, v:none) 1716 END 1717 CheckDefAndScriptSuccess(lines) 1718 1719 CheckDefAndScriptFailure(['v:true = true'], 'E46:', 1) 1720 CheckDefAndScriptFailure(['v:true = false'], 'E46:', 1) 1721 CheckDefAndScriptFailure(['v:false = true'], 'E46:', 1) 1722 CheckDefAndScriptFailure(['v:null = 11'], 'E46:', 1) 1723 CheckDefAndScriptFailure(['v:none = 22'], 'E46:', 1) 1724enddef 1725 1726def Test_expr7_list() 1727 # list 1728 var lines =<< trim END 1729 assert_equal(g:list_empty, []) 1730 assert_equal(g:list_empty, [ ]) 1731 1732 var numbers: list<number> = [1, 2, 3] 1733 numbers = [1] 1734 numbers = [] 1735 1736 var strings: list<string> = ['a', 'b', 'c'] 1737 strings = ['x'] 1738 strings = [] 1739 1740 var mixed: list<any> = [1, 'b', false,] 1741 assert_equal(g:list_mixed, mixed) 1742 assert_equal('b', mixed[1]) 1743 1744 echo [1, 1745 2] [3, 1746 4] 1747 1748 var llstring: list<list<string>> = [['text'], []] 1749 llstring = [[], ['text']] 1750 llstring = [[], []] 1751 END 1752 CheckDefAndScriptSuccess(lines) 1753 1754 var rangelist: list<number> = range(3) 1755 g:rangelist = range(3) 1756 CheckDefExecAndScriptFailure(["var x: list<string> = g:rangelist"], 'E1012: Type mismatch; expected list<string> but got list<number>', 1) 1757 1758 CheckDefAndScriptFailure2(["var x = 1234[3]"], 'E1107:', 'E1062:', 1) 1759 CheckDefExecAndScriptFailure(["var x = g:anint[3]"], 'E1062:', 1) 1760 1761 CheckDefAndScriptFailure2(["var x = g:list_mixed[xxx]"], 'E1001:', 'E121:', 1) 1762 1763 CheckDefAndScriptFailure(["var x = [1,2,3]"], 'E1069:', 1) 1764 CheckDefAndScriptFailure(["var x = [1 ,2, 3]"], 'E1068:', 1) 1765 1766 CheckDefExecAndScriptFailure(["echo 1", "var x = [][0]", "echo 3"], 'E684:', 2) 1767 1768 CheckDefExecAndScriptFailure2(["var x = g:list_mixed['xx']"], 'E1012:', 'E1030:', 1) 1769 CheckDefFailure(["var x = g:list_mixed["], 'E1097:', 3) 1770 CheckScriptFailure(['vim9script', "var x = g:list_mixed["], 'E15:', 2) 1771 CheckDefFailure(["var x = g:list_mixed[0"], 'E1097:', 3) 1772 CheckScriptFailure(['vim9script', "var x = g:list_mixed[0"], 'E111:', 2) 1773 CheckDefExecAndScriptFailure(["var x = g:list_empty[3]"], 'E684:', 1) 1774 CheckDefExecAndScriptFailure(["var l: list<number> = [234, 'x']"], 'E1012:', 1) 1775 CheckDefExecAndScriptFailure(["var l: list<number> = ['x', 234]"], 'E1012:', 1) 1776 CheckDefExecAndScriptFailure(["var l: list<string> = [234, 'x']"], 'E1012:', 1) 1777 CheckDefExecAndScriptFailure(["var l: list<string> = ['x', 123]"], 'E1012:', 1) 1778 1779 lines =<< trim END 1780 var datalist: list<string> 1781 def Main() 1782 datalist += ['x'. 1783 enddef 1784 Main() 1785 END 1786 CheckDefAndScriptFailure(lines, 'E1127:') 1787 1788 lines =<< trim END 1789 var numbers = [1, 2, 3, 4] 1790 var a = 1 1791 var b = 2 1792 END 1793 CheckDefAndScriptFailure(lines + ['echo numbers[1:b]'], 1794 'E1004: White space required before and after '':'' at ":b]"', 4) 1795 CheckDefAndScriptFailure(lines + ['echo numbers[1: b]'], 'E1004:', 4) 1796 CheckDefAndScriptFailure(lines + ['echo numbers[a :b]'], 'E1004:', 4) 1797enddef 1798 1799def Test_expr7_list_vim9script() 1800 var lines =<< trim END 1801 var l = [ 1802 11, 1803 22, 1804 ] 1805 assert_equal([11, 22], l) 1806 1807 echo [1, 1808 2] [3, 1809 4] 1810 1811 echo [1, # comment 1812 # comment 1813 2] [3, 1814 # comment 1815 4] 1816 END 1817 CheckDefAndScriptSuccess(lines) 1818 1819 lines =<< trim END 1820 var l = [11, 1821 22] 1822 assert_equal([11, 22], l) 1823 END 1824 CheckDefAndScriptSuccess(lines) 1825 1826 lines =<< trim END 1827 var l = [11,22] 1828 END 1829 CheckDefAndScriptFailure(lines, 'E1069:', 1) 1830 1831 lines =<< trim END 1832 var l = [11 , 22] 1833 END 1834 CheckDefAndScriptFailure(lines, 'E1068:', 1) 1835 1836 lines =<< trim END 1837 var l: list<number> = [234, 'x'] 1838 END 1839 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1840 1841 lines =<< trim END 1842 var l: list<number> = ['x', 234] 1843 END 1844 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1845 1846 lines =<< trim END 1847 var l: list<string> = ['x', 234] 1848 END 1849 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1850 1851 lines =<< trim END 1852 var l: list<string> = [234, 'x'] 1853 END 1854 CheckDefAndScriptFailure(lines, 'E1012:', 1) 1855 1856 lines =<< trim END 1857 def Failing() 1858 job_stop() 1859 enddef 1860 var list = [Failing] 1861 END 1862 if has('channel') 1863 CheckDefAndScriptFailure(lines, 'E119:', 0) 1864 else 1865 CheckDefAndScriptFailure(lines, 'E117:', 0) 1866 endif 1867enddef 1868 1869def LambdaWithComments(): func 1870 return (x) => 1871 # some comment 1872 x == 1 1873 # some comment 1874 || 1875 x == 2 1876enddef 1877 1878def LambdaUsingArg(x: number): func 1879 return () => 1880 # some comment 1881 x == 1 1882 # some comment 1883 || 1884 x == 2 1885enddef 1886 1887def Test_expr7_lambda() 1888 var lines =<< trim END 1889 var La = () => 'result' 1890 # comment 1891 assert_equal('result', La()) 1892 assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val)) 1893 1894 # line continuation inside lambda with "cond ? expr : expr" works 1895 var ll = range(3) 1896 var dll = mapnew(ll, (k, v) => v % 2 ? { 1897 ['111']: 111 } : {} 1898 ) 1899 assert_equal([{}, {111: 111}, {}], dll) 1900 1901 # comment halfway an expression 1902 var Ref = () => 4 1903 # comment 1904 + 6 1905 assert_equal(10, Ref()) 1906 1907 ll = range(3) 1908 map(ll, (k, v) => v == 8 || v 1909 == 9 1910 || v % 2 ? 111 : 222 1911 ) 1912 assert_equal([222, 111, 222], ll) 1913 1914 ll = range(3) 1915 map(ll, (k, v) => v != 8 && v 1916 != 9 1917 && v % 2 == 0 ? 111 : 222 1918 ) 1919 assert_equal([111, 222, 111], ll) 1920 1921 var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] ) 1922 assert_equal([{key: 22}], dl) 1923 1924 dl = [{key: 12}, {['foo']: 34}] 1925 assert_equal([{key: 12}], filter(dl, 1926 (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0)) 1927 1928 assert_equal(false, LambdaWithComments()(0)) 1929 assert_equal(true, LambdaWithComments()(1)) 1930 assert_equal(true, LambdaWithComments()(2)) 1931 assert_equal(false, LambdaWithComments()(3)) 1932 1933 assert_equal(false, LambdaUsingArg(0)()) 1934 assert_equal(true, LambdaUsingArg(1)()) 1935 1936 var res = map([1, 2, 3], (i: number, v: number) => i + v) 1937 assert_equal([1, 3, 5], res) 1938 END 1939 CheckDefAndScriptSuccess(lines) 1940 1941 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:') 1942 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004: White space required before and after ''=>'' at "=> a + 1"') 1943 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 'E1004:') 1944 1945 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 1946 # error is in first line of the lambda 1947 CheckDefAndScriptFailure(["var L = (a) => a + b"], 'E1001:', 0) 1948 1949 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 1950 1951 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 'E118:') 1952 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 'E118:') 1953 CheckDefAndScriptFailure2(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 'E121:', 1) 1954 1955 CheckDefAndScriptSuccess(['var Fx = (a) => ({k1: 0,', ' k2: 1})']) 1956 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 1957 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 1958 1959 CheckDefAndScriptSuccess(['var Fx = (a) => [0,', ' 1]']) 1960 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) 1961 1962 # no error for existing script variable when checking for lambda 1963 lines =<< trim END 1964 var name = 0 1965 eval (name + 2) / 3 1966 END 1967 CheckDefAndScriptSuccess(lines) 1968enddef 1969 1970def Test_expr7_lambda_block() 1971 var lines =<< trim END 1972 var Func = (s: string): string => { 1973 return 'hello ' .. s 1974 } 1975 assert_equal('hello there', Func('there')) 1976 1977 var ll = range(3) 1978 var dll = mapnew(ll, (k, v): string => { 1979 if v % 2 1980 return 'yes' 1981 endif 1982 return 'no' 1983 }) 1984 assert_equal(['no', 'yes', 'no'], dll) 1985 1986 sandbox var Safe = (nr: number): number => { 1987 return nr + 7 1988 } 1989 assert_equal(10, Safe(3)) 1990 END 1991 CheckDefAndScriptSuccess(lines) 1992 1993 lines =<< trim END 1994 map([1, 2], (k, v) => { redrawt }) 1995 END 1996 CheckDefAndScriptFailure(lines, 'E488') 1997 1998 lines =<< trim END 1999 var Func = (nr: int) => { 2000 echo nr 2001 } 2002 END 2003 CheckDefAndScriptFailure(lines, 'E1010', 1) 2004 2005 lines =<< trim END 2006 var Func = (nr: number): int => { 2007 return nr 2008 } 2009 END 2010 CheckDefAndScriptFailure(lines, 'E1010', 1) 2011 2012 lines =<< trim END 2013 var Func = (nr: number): int => { 2014 return nr 2015 END 2016 CheckDefAndScriptFailure(lines, 'E1171', 1) # line nr is function start 2017 2018 lines =<< trim END 2019 var Func = (nr: number): int => { 2020 var ll =<< ENDIT 2021 nothing 2022 END 2023 CheckDefFailure(lines, 'E1145: Missing heredoc end marker: ENDIT', 0) 2024 CheckScriptFailure(['vim9script'] + lines, 'E1145: Missing heredoc end marker: ENDIT', 2) 2025enddef 2026 2027def NewLambdaWithComments(): func 2028 return (x) => 2029 # some comment 2030 x == 1 2031 # some comment 2032 || 2033 x == 2 2034enddef 2035 2036def NewLambdaUsingArg(x: number): func 2037 return () => 2038 # some comment 2039 x == 1 2040 # some comment 2041 || 2042 x == 2 2043enddef 2044 2045def Test_expr7_new_lambda() 2046 var lines =<< trim END 2047 var La = () => 'result' 2048 assert_equal('result', La()) 2049 assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val)) 2050 2051 # line continuation inside lambda with "cond ? expr : expr" works 2052 var ll = range(3) 2053 var dll = mapnew(ll, (k, v) => v % 2 ? { 2054 ['111']: 111 } : {} 2055 ) 2056 assert_equal([{}, {111: 111}, {}], dll) 2057 2058 ll = range(3) 2059 map(ll, (k, v) => v == 8 || v 2060 == 9 2061 || v % 2 ? 111 : 222 2062 ) 2063 assert_equal([222, 111, 222], ll) 2064 2065 ll = range(3) 2066 map(ll, (k, v) => v != 8 && v 2067 != 9 2068 && v % 2 == 0 ? 111 : 222 2069 ) 2070 assert_equal([111, 222, 111], ll) 2071 2072 var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] ) 2073 assert_equal([{key: 22}], dl) 2074 2075 dl = [{key: 12}, {['foo']: 34}] 2076 assert_equal([{key: 12}], filter(dl, 2077 (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0)) 2078 2079 assert_equal(false, NewLambdaWithComments()(0)) 2080 assert_equal(true, NewLambdaWithComments()(1)) 2081 assert_equal(true, NewLambdaWithComments()(2)) 2082 assert_equal(false, NewLambdaWithComments()(3)) 2083 2084 assert_equal(false, NewLambdaUsingArg(0)()) 2085 assert_equal(true, NewLambdaUsingArg(1)()) 2086 2087 var res = map([1, 2, 3], (i: number, v: number) => i + v) 2088 assert_equal([1, 3, 5], res) 2089 2090 # Lambda returning a dict 2091 var Lmb = () => ({key: 42}) 2092 assert_equal({key: 42}, Lmb()) 2093 2094 var RefOne: func(number): string = (a: number): string => 'x' 2095 var RefTwo: func(number): any = (a: number): any => 'x' 2096 2097 var Fx = (a) => ({k1: 0, 2098 k2: 1}) 2099 var Fy = (a) => [0, 2100 1] 2101 END 2102 CheckDefAndScriptSuccess(lines) 2103 2104 CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:') 2105 CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004:') 2106 CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 2107 'E1004: White space required before and after ''=>'' at " =>a + 1"') 2108 2109 CheckDefAndScriptFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:') 2110 CheckDefAndScriptFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:') 2111 2112 CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1) 2113 # error is in first line of the lambda 2114 CheckDefAndScriptFailure2(["var L = (a) -> a + b"], 'E1001:', 'E121:', 1) 2115 2116 assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy')) 2117 2118 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 2119 'E118: Too many arguments for function:') 2120 CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 2121 'E118: Too many arguments for function:') 2122 CheckDefFailure(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 1) 2123 2124 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) 2125 CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) 2126 2127 CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) 2128enddef 2129 2130def Test_expr7_lambda_vim9script() 2131 var lines =<< trim END 2132 var v = 10->((a) => 2133 a 2134 + 2 2135 )() 2136 assert_equal(12, v) 2137 END 2138 CheckDefAndScriptSuccess(lines) 2139 2140 # nested lambda with line breaks 2141 lines =<< trim END 2142 search('"', 'cW', 0, 0, () => 2143 synstack('.', col('.')) 2144 ->map((_, v) => synIDattr(v, 'name'))->len()) 2145 END 2146 CheckDefAndScriptSuccess(lines) 2147enddef 2148 2149def Test_expr7_funcref() 2150 var lines =<< trim END 2151 def RetNumber(): number 2152 return 123 2153 enddef 2154 var FuncRef = RetNumber 2155 assert_equal(123, FuncRef()) 2156 END 2157 CheckDefAndScriptSuccess(lines) 2158 2159 lines =<< trim END 2160 vim9script 2161 func g:GlobalFunc() 2162 return 'global' 2163 endfunc 2164 func s:ScriptFunc() 2165 return 'script' 2166 endfunc 2167 def Test() 2168 var Ref = g:GlobalFunc 2169 assert_equal('global', Ref()) 2170 Ref = GlobalFunc 2171 assert_equal('global', Ref()) 2172 2173 Ref = s:ScriptFunc 2174 assert_equal('script', Ref()) 2175 Ref = ScriptFunc 2176 assert_equal('script', Ref()) 2177 enddef 2178 Test() 2179 END 2180 CheckScriptSuccess(lines) 2181enddef 2182 2183let g:test_space_dict = {'': 'empty', ' ': 'space'} 2184let g:test_hash_dict = #{one: 1, two: 2} 2185 2186def Test_expr7_dict() 2187 # dictionary 2188 var lines =<< trim END 2189 assert_equal(g:dict_empty, {}) 2190 assert_equal(g:dict_empty, { }) 2191 assert_equal(g:dict_one, {['one']: 1}) 2192 var key = 'one' 2193 var val = 1 2194 assert_equal(g:dict_one, {[key]: val}) 2195 2196 var numbers: dict<number> = {a: 1, b: 2, c: 3} 2197 numbers = {a: 1} 2198 numbers = {} 2199 2200 var strings: dict<string> = {a: 'a', b: 'b', c: 'c'} 2201 strings = {a: 'x'} 2202 strings = {} 2203 2204 var dash = {xx-x: 8} 2205 assert_equal({['xx-x']: 8}, dash) 2206 2207 var dnr = {8: 8} 2208 assert_equal({['8']: 8}, dnr) 2209 2210 var mixed: dict<any> = {a: 'a', b: 42} 2211 mixed = {a: 'x'} 2212 mixed = {a: 234} 2213 mixed = {} 2214 2215 var dictlist: dict<list<string>> = {absent: [], present: ['hi']} 2216 dictlist = {absent: ['hi'], present: []} 2217 dictlist = {absent: [], present: []} 2218 2219 var dictdict: dict<dict<string>> = {one: {a: 'text'}, two: {}} 2220 dictdict = {one: {}, two: {a: 'text'}} 2221 dictdict = {one: {}, two: {}} 2222 2223 assert_equal({['']: 0}, {[matchstr('string', 'wont match')]: 0}) 2224 2225 assert_equal(g:test_space_dict, {['']: 'empty', [' ']: 'space'}) 2226 assert_equal(g:test_hash_dict, {one: 1, two: 2}) 2227 2228 assert_equal({['a a']: 1, ['b/c']: 2}, {'a a': 1, "b/c": 2}) 2229 2230 var d = {a: () => 3, b: () => 7} 2231 assert_equal(3, d.a()) 2232 assert_equal(7, d.b()) 2233 2234 var cd = { # comment 2235 key: 'val' # comment 2236 } 2237 2238 # different types used for the key 2239 var dkeys = {['key']: 'string', 2240 [12]: 'numberexpr', 2241 34: 'number', 2242 [true]: 'bool'} 2243 assert_equal('string', dkeys['key']) 2244 assert_equal('numberexpr', dkeys[12]) 2245 assert_equal('number', dkeys[34]) 2246 assert_equal('bool', dkeys[true]) 2247 if has('float') 2248 dkeys = {[1.2]: 'floatexpr', [3.4]: 'float'} 2249 assert_equal('floatexpr', dkeys[1.2]) 2250 assert_equal('float', dkeys[3.4]) 2251 endif 2252 2253 # automatic conversion from number to string 2254 var n = 123 2255 var dictnr = {[n]: 1} 2256 2257 # comment to start fold is OK 2258 var x1: number #{{ fold 2259 var x2 = 9 #{{ fold 2260 END 2261 CheckDefAndScriptSuccess(lines) 2262 2263 # legacy syntax doesn't work 2264 CheckDefAndScriptFailure(["var x = #{key: 8}"], 'E1170:', 1) 2265 CheckDefAndScriptFailure(["var x = 'a' #{a: 1}"], 'E1170:', 1) 2266 CheckDefAndScriptFailure(["var x = 'a' .. #{a: 1}"], 'E1170:', 1) 2267 CheckDefAndScriptFailure(["var x = true ? #{a: 1}"], 'E1170:', 1) 2268 2269 CheckDefAndScriptFailure(["var x = {a:8}"], 'E1069:', 1) 2270 CheckDefAndScriptFailure(["var x = {a : 8}"], 'E1068:', 1) 2271 CheckDefAndScriptFailure(["var x = {a :8}"], 'E1068:', 1) 2272 CheckDefAndScriptFailure(["var x = {a: 8 , b: 9}"], 'E1068:', 1) 2273 CheckDefAndScriptFailure(["var x = {a: 1,b: 2}"], 'E1069:', 1) 2274 2275 CheckDefAndScriptFailure(["var x = {xxx}"], 'E720:', 1) 2276 CheckDefAndScriptFailure(["var x = {xxx: 1", "var y = 2"], 'E722:', 2) 2277 CheckDefFailure(["var x = {xxx: 1,"], 'E723:', 2) 2278 CheckScriptFailure(['vim9script', "var x = {xxx: 1,"], 'E723:', 2) 2279 CheckDefAndScriptFailure2(["var x = {['a']: xxx}"], 'E1001:', 'E121:', 1) 2280 CheckDefAndScriptFailure(["var x = {a: 1, a: 2}"], 'E721:', 1) 2281 CheckDefExecAndScriptFailure2(["var x = g:anint.member"], 'E715:', 'E15:', 1) 2282 CheckDefExecAndScriptFailure(["var x = g:dict_empty.member"], 'E716:', 1) 2283 2284 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: 234, b: "1"}'], 'E1012:', 1) 2285 CheckDefExecAndScriptFailure(['var x: dict<number> = {a: "x", b: 134}'], 'E1012:', 1) 2286 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1) 2287 CheckDefExecAndScriptFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1) 2288 2289 # invalid types for the key 2290 CheckDefAndScriptFailure2(["var x = {[[1, 2]]: 0}"], 'E1105:', 'E730:', 1) 2291 2292 CheckDefFailure(['var x = ({'], 'E723:', 2) 2293 CheckScriptFailure(['vim9script', 'var x = ({'], 'E723:', 2) 2294 CheckDefExecAndScriptFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1) 2295enddef 2296 2297def Test_expr7_dict_vim9script() 2298 var lines =<< trim END 2299 var d = { 2300 ['one']: 2301 1, 2302 ['two']: 2, 2303 } 2304 assert_equal({one: 1, two: 2}, d) 2305 2306 d = { # comment 2307 ['one']: 2308 # comment 2309 2310 1, 2311 # comment 2312 # comment 2313 ['two']: 2, 2314 } 2315 assert_equal({one: 1, two: 2}, d) 2316 2317 var dd = {k: 123->len()} 2318 assert_equal(3, dd.k) 2319 END 2320 CheckDefAndScriptSuccess(lines) 2321 2322 lines =<< trim END 2323 var d = { ["one"]: "one", ["two"]: "two", } 2324 assert_equal({one: 'one', two: 'two'}, d) 2325 END 2326 CheckDefAndScriptSuccess(lines) 2327 2328 lines =<< trim END 2329 var d = {one: 1, 2330 two: 2, 2331 } 2332 assert_equal({one: 1, two: 2}, d) 2333 END 2334 CheckDefAndScriptSuccess(lines) 2335 2336 lines =<< trim END 2337 var d = {one:1, two: 2} 2338 END 2339 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2340 2341 lines =<< trim END 2342 var d = {one: 1,two: 2} 2343 END 2344 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2345 2346 lines =<< trim END 2347 var d = {one : 1} 2348 END 2349 CheckDefAndScriptFailure(lines, 'E1068:', 1) 2350 2351 lines =<< trim END 2352 var d = {one:1} 2353 END 2354 CheckDefAndScriptFailure(lines, 'E1069:', 1) 2355 2356 lines =<< trim END 2357 var d = {one: 1 , two: 2} 2358 END 2359 CheckDefAndScriptFailure(lines, 'E1068:', 1) 2360 2361 lines =<< trim END 2362 var l: dict<number> = {a: 234, b: 'x'} 2363 END 2364 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2365 2366 lines =<< trim END 2367 var l: dict<number> = {a: 'x', b: 234} 2368 END 2369 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2370 2371 lines =<< trim END 2372 var l: dict<string> = {a: 'x', b: 234} 2373 END 2374 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2375 2376 lines =<< trim END 2377 var l: dict<string> = {a: 234, b: 'x'} 2378 END 2379 CheckDefAndScriptFailure(lines, 'E1012:', 1) 2380 2381 lines =<< trim END 2382 var d = {['a']: 234, ['b': 'x'} 2383 END 2384 CheckDefAndScriptFailure(lines, 'E1139:', 1) 2385 2386 lines =<< trim END 2387 def Func() 2388 var d = {['a']: 234, ['b': 'x'} 2389 enddef 2390 defcompile 2391 END 2392 CheckDefAndScriptFailure(lines, 'E1139:', 0) 2393 2394 lines =<< trim END 2395 var d = {'a': 2396 END 2397 CheckDefFailure(lines, 'E723:', 2) 2398 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2) 2399 2400 lines =<< trim END 2401 def Func() 2402 var d = {'a': 2403 enddef 2404 defcompile 2405 END 2406 CheckDefAndScriptFailure(lines, 'E723:', 0) 2407 2408 lines =<< trim END 2409 def Failing() 2410 job_stop() 2411 enddef 2412 var dict = {name: Failing} 2413 END 2414 if has('channel') 2415 CheckDefAndScriptFailure(lines, 'E119:', 0) 2416 else 2417 CheckDefAndScriptFailure(lines, 'E117:', 0) 2418 endif 2419enddef 2420 2421let g:oneString = 'one' 2422 2423def Test_expr_member() 2424 var lines =<< trim END 2425 assert_equal(1, g:dict_one.one) 2426 var d: dict<number> = g:dict_one 2427 assert_equal(1, d['one']) 2428 assert_equal(1, d[ 2429 'one' 2430 ]) 2431 assert_equal(1, d 2432 .one) 2433 d = {1: 1, _: 2} 2434 assert_equal(1, d 2435 .1) 2436 assert_equal(2, d 2437 ._) 2438 2439 # getting the one member should clear the dict after getting the item 2440 assert_equal('one', {one: 'one'}.one) 2441 assert_equal('one', {one: 'one'}[g:oneString]) 2442 END 2443 CheckDefAndScriptSuccess(lines) 2444 2445 CheckDefAndScriptFailure2(["var x = g:dict_one.#$!"], 'E1002:', 'E15:', 1) 2446 CheckDefExecAndScriptFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2) 2447 CheckDefExecAndScriptFailure(["var d: dict<number>", "d = g:list_empty"], 'E1012: Type mismatch; expected dict<number> but got list<unknown>', 2) 2448enddef 2449 2450def Test_expr7_any_index_slice() 2451 var lines =<< trim END 2452 # getting the one member should clear the list only after getting the item 2453 assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1]) 2454 2455 # string is permissive, index out of range accepted 2456 g:teststring = 'abcdef' 2457 assert_equal('b', g:teststring[1]) 2458 assert_equal('f', g:teststring[-1]) 2459 assert_equal('', g:teststring[99]) 2460 2461 assert_equal('b', g:teststring[1 : 1]) 2462 assert_equal('bcdef', g:teststring[1 :]) 2463 assert_equal('abcd', g:teststring[: 3]) 2464 assert_equal('cdef', g:teststring[-4 :]) 2465 assert_equal('abcdef', g:teststring[-9 :]) 2466 assert_equal('abcd', g:teststring[: -3]) 2467 assert_equal('', g:teststring[: -9]) 2468 2469 # composing characters are included 2470 g:teststring = 'àéû' 2471 assert_equal('à', g:teststring[0]) 2472 assert_equal('é', g:teststring[1]) 2473 assert_equal('û', g:teststring[2]) 2474 assert_equal('', g:teststring[3]) 2475 assert_equal('', g:teststring[4]) 2476 2477 assert_equal('û', g:teststring[-1]) 2478 assert_equal('é', g:teststring[-2]) 2479 assert_equal('à', g:teststring[-3]) 2480 assert_equal('', g:teststring[-4]) 2481 assert_equal('', g:teststring[-5]) 2482 2483 assert_equal('à', g:teststring[0 : 0]) 2484 assert_equal('é', g:teststring[1 : 1]) 2485 assert_equal('àé', g:teststring[0 : 1]) 2486 assert_equal('àéû', g:teststring[0 : -1]) 2487 assert_equal('àé', g:teststring[0 : -2]) 2488 assert_equal('à', g:teststring[0 : -3]) 2489 assert_equal('', g:teststring[0 : -4]) 2490 assert_equal('', g:teststring[0 : -5]) 2491 assert_equal('àéû', g:teststring[ : ]) 2492 assert_equal('àéû', g:teststring[0 : ]) 2493 assert_equal('éû', g:teststring[1 : ]) 2494 assert_equal('û', g:teststring[2 : ]) 2495 assert_equal('', g:teststring[3 : ]) 2496 assert_equal('', g:teststring[4 : ]) 2497 2498 # blob index cannot be out of range 2499 g:testblob = 0z01ab 2500 assert_equal(0x01, g:testblob[0]) 2501 assert_equal(0xab, g:testblob[1]) 2502 assert_equal(0xab, g:testblob[-1]) 2503 assert_equal(0x01, g:testblob[-2]) 2504 2505 # blob slice accepts out of range 2506 assert_equal(0z01ab, g:testblob[0 : 1]) 2507 assert_equal(0z01, g:testblob[0 : 0]) 2508 assert_equal(0z01, g:testblob[-2 : -2]) 2509 assert_equal(0zab, g:testblob[1 : 1]) 2510 assert_equal(0zab, g:testblob[-1 : -1]) 2511 assert_equal(0z, g:testblob[2 : 2]) 2512 assert_equal(0z, g:testblob[0 : -3]) 2513 2514 # list index cannot be out of range 2515 g:testlist = [0, 1, 2, 3] 2516 assert_equal(0, g:testlist[0]) 2517 assert_equal(1, g:testlist[1]) 2518 assert_equal(3, g:testlist[3]) 2519 assert_equal(3, g:testlist[-1]) 2520 assert_equal(0, g:testlist[-4]) 2521 assert_equal(1, g:testlist[g:theone]) 2522 2523 # list slice accepts out of range 2524 assert_equal([0], g:testlist[0 : 0]) 2525 assert_equal([3], g:testlist[3 : 3]) 2526 assert_equal([0, 1], g:testlist[0 : 1]) 2527 assert_equal([0, 1, 2, 3], g:testlist[0 : 3]) 2528 assert_equal([0, 1, 2, 3], g:testlist[0 : 9]) 2529 assert_equal([], g:testlist[-1 : 1]) 2530 assert_equal([1], g:testlist[-3 : 1]) 2531 assert_equal([0, 1], g:testlist[-4 : 1]) 2532 assert_equal([0, 1], g:testlist[-9 : 1]) 2533 assert_equal([1, 2, 3], g:testlist[1 : -1]) 2534 assert_equal([1], g:testlist[1 : -3]) 2535 assert_equal([], g:testlist[1 : -4]) 2536 assert_equal([], g:testlist[1 : -9]) 2537 2538 g:testdict = {a: 1, b: 2} 2539 assert_equal(1, g:testdict['a']) 2540 assert_equal(2, g:testdict['b']) 2541 END 2542 2543 CheckDefAndScriptSuccess(lines) 2544 2545 CheckDefExecAndScriptFailure(['echo g:testblob[2]'], 'E979:', 1) 2546 CheckDefExecAndScriptFailure(['echo g:testblob[-3]'], 'E979:', 1) 2547 2548 CheckDefExecAndScriptFailure(['echo g:testlist[4]'], 'E684: list index out of range: 4', 1) 2549 CheckDefExecAndScriptFailure(['echo g:testlist[-5]'], 'E684:', 1) 2550 2551 CheckDefExecAndScriptFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1) 2552 CheckDefExecAndScriptFailure(['echo g:testdict[1]'], 'E716:', 1) 2553 2554 unlet g:teststring 2555 unlet g:testblob 2556 unlet g:testlist 2557enddef 2558 2559def Test_expr_member_vim9script() 2560 var lines =<< trim END 2561 var d = {one: 2562 'one', 2563 two: 'two', 2564 1: 1, 2565 _: 2} 2566 assert_equal('one', d.one) 2567 assert_equal('one', d 2568 .one) 2569 assert_equal(1, d 2570 .1) 2571 assert_equal(2, d 2572 ._) 2573 assert_equal('one', d[ 2574 'one' 2575 ]) 2576 END 2577 CheckDefAndScriptSuccess(lines) 2578 2579 lines =<< trim END 2580 var l = [1, 2581 2, 2582 3, 4 2583 ] 2584 assert_equal(2, l[ 2585 1 2586 ]) 2587 assert_equal([2, 3], l[1 : 2]) 2588 assert_equal([1, 2, 3], l[ 2589 : 2590 2 2591 ]) 2592 assert_equal([3, 4], l[ 2593 2 2594 : 2595 ]) 2596 END 2597 CheckDefAndScriptSuccess(lines) 2598enddef 2599 2600def SetSomeVar() 2601 b:someVar = &fdm 2602enddef 2603 2604def Test_expr7_option() 2605 var lines =<< trim END 2606 # option 2607 set ts=11 2608 assert_equal(11, &ts) 2609 &ts = 9 2610 assert_equal(9, &ts) 2611 set ts=8 2612 set grepprg=some\ text 2613 assert_equal('some text', &grepprg) 2614 &grepprg = test_null_string() 2615 assert_equal('', &grepprg) 2616 set grepprg& 2617 2618 # check matching type 2619 var bval: bool = &tgc 2620 var nval: number = &ts 2621 var sval: string = &path 2622 2623 # check v_lock is cleared (requires using valgrind, doesn't always show) 2624 SetSomeVar() 2625 b:someVar = 0 2626 unlet b:someVar 2627 END 2628 CheckDefAndScriptSuccess(lines) 2629enddef 2630 2631def Test_expr7_environment() 2632 var lines =<< trim END 2633 # environment variable 2634 assert_equal('testvar', $TESTVAR) 2635 assert_equal('', $ASDF_ASD_XXX) 2636 END 2637 CheckDefAndScriptSuccess(lines) 2638 2639 CheckDefAndScriptFailure2(["var x = $$$"], 'E1002:', 'E15:', 1) 2640enddef 2641 2642def Test_expr7_register() 2643 var lines =<< trim END 2644 @a = 'register a' 2645 assert_equal('register a', @a) 2646 2647 var fname = expand('%') 2648 assert_equal(fname, @%) 2649 2650 feedkeys(":echo 'some'\<CR>", "xt") 2651 assert_equal("echo 'some'", @:) 2652 2653 normal axyz 2654 assert_equal("xyz", @.) 2655 2656 @/ = 'slash' 2657 assert_equal('slash', @/) 2658 2659 @= = 'equal' 2660 assert_equal('equal', @=) 2661 END 2662 CheckDefAndScriptSuccess(lines) 2663 2664 CheckDefAndScriptFailure2(["@. = 'yes'"], 'E354:', 'E488:', 1) 2665enddef 2666 2667" This is slow when run under valgrind. 2668def Test_expr7_namespace() 2669 var lines =<< trim END 2670 g:some_var = 'some' 2671 assert_equal('some', get(g:, 'some_var')) 2672 assert_equal('some', get(g:, 'some_var', 'xxx')) 2673 assert_equal('xxx', get(g:, 'no_var', 'xxx')) 2674 unlet g:some_var 2675 2676 b:some_var = 'some' 2677 assert_equal('some', get(b:, 'some_var')) 2678 assert_equal('some', get(b:, 'some_var', 'xxx')) 2679 assert_equal('xxx', get(b:, 'no_var', 'xxx')) 2680 unlet b:some_var 2681 2682 w:some_var = 'some' 2683 assert_equal('some', get(w:, 'some_var')) 2684 assert_equal('some', get(w:, 'some_var', 'xxx')) 2685 assert_equal('xxx', get(w:, 'no_var', 'xxx')) 2686 unlet w:some_var 2687 2688 t:some_var = 'some' 2689 assert_equal('some', get(t:, 'some_var')) 2690 assert_equal('some', get(t:, 'some_var', 'xxx')) 2691 assert_equal('xxx', get(t:, 'no_var', 'xxx')) 2692 unlet t:some_var 2693 2694 # check using g: in a for loop more than DO_NOT_FREE_CNT times 2695 for i in range(100000) 2696 if has_key(g:, 'does-not-exist') 2697 endif 2698 endfor 2699 END 2700 CheckDefAndScriptSuccess(lines) 2701enddef 2702 2703def Test_expr7_parens() 2704 # (expr) 2705 var lines =<< trim END 2706 assert_equal(4, (6 * 4) / 6) 2707 assert_equal(0, 6 * ( 4 / 6 )) 2708 2709 assert_equal(6, +6) 2710 assert_equal(-6, -6) 2711 assert_equal(false, !-3) 2712 assert_equal(true, !+0) 2713 2714 assert_equal(7, 5 + ( 2715 2)) 2716 assert_equal(7, 5 + ( 2717 2 2718 )) 2719 assert_equal(7, 5 + ( # comment 2720 2)) 2721 assert_equal(7, 5 + ( # comment 2722 # comment 2723 2)) 2724 2725 var s = ( 2726 'one' 2727 .. 2728 'two' 2729 ) 2730 assert_equal('onetwo', s) 2731 END 2732 CheckDefAndScriptSuccess(lines) 2733enddef 2734 2735def Test_expr7_negate_add() 2736 var lines =<< trim END 2737 assert_equal(-99, -99) 2738 assert_equal(-99, - 99) 2739 assert_equal(99, +99) 2740 2741 var nr = 88 2742 assert_equal(-88, -nr) 2743 assert_equal(-88, - nr) 2744 assert_equal(88, + nr) 2745 END 2746 CheckDefAndScriptSuccess(lines) 2747 2748 lines =<< trim END 2749 var n = 12 2750 echo ++n 2751 END 2752 CheckDefAndScriptFailure(lines, 'E15:') 2753 lines =<< trim END 2754 var n = 12 2755 echo --n 2756 END 2757 CheckDefAndScriptFailure(lines, 'E15:') 2758 lines =<< trim END 2759 var n = 12 2760 echo +-n 2761 END 2762 CheckDefAndScriptFailure(lines, 'E15:') 2763 lines =<< trim END 2764 var n = 12 2765 echo -+n 2766 END 2767 CheckDefAndScriptFailure(lines, 'E15:') 2768 lines =<< trim END 2769 var n = 12 2770 echo - -n 2771 END 2772 CheckDefAndScriptFailure(lines, 'E15:') 2773 lines =<< trim END 2774 var n = 12 2775 echo + +n 2776 END 2777 CheckDefAndScriptFailure(lines, 'E15:') 2778enddef 2779 2780def Test_expr7_legacy_script() 2781 var lines =<< trim END 2782 let s:legacy = 'legacy' 2783 def GetLocal(): string 2784 return legacy 2785 enddef 2786 def GetLocalPrefix(): string 2787 return s:legacy 2788 enddef 2789 call assert_equal('legacy', GetLocal()) 2790 call assert_equal('legacy', GetLocalPrefix()) 2791 END 2792 CheckScriptSuccess(lines) 2793enddef 2794 2795def Echo(arg: any): string 2796 return arg 2797enddef 2798 2799def s:Echo4Arg(arg: any): string 2800 return arg 2801enddef 2802 2803def Test_expr7_call() 2804 var lines =<< trim END 2805 assert_equal('yes', 'yes'->Echo()) 2806 assert_equal(true, !range(5)->empty()) 2807 assert_equal([0, 1, 2], 3->range()) 2808 END 2809 CheckDefAndScriptSuccess(lines) 2810 2811 assert_equal('yes', 'yes' 2812 ->s:Echo4Arg()) 2813 2814 CheckDefAndScriptFailure(["var x = 'yes'->Echo"], 'E107:', 1) 2815 CheckDefAndScriptFailure2([ 2816 "var x = substitute ('x', 'x', 'x', 'x')" 2817 ], 'E1001:', 'E121:', 1) 2818 CheckDefAndScriptFailure2(["var Ref = function('len' [1, 2])"], 'E1123:', 'E116:', 1) 2819 2820 var auto_lines =<< trim END 2821 def g:some#func(): string 2822 return 'found' 2823 enddef 2824 END 2825 mkdir('Xruntime/autoload', 'p') 2826 writefile(auto_lines, 'Xruntime/autoload/some.vim') 2827 var save_rtp = &rtp 2828 &rtp = getcwd() .. '/Xruntime,' .. &rtp 2829 assert_equal('found', g:some#func()) 2830 assert_equal('found', some#func()) 2831 2832 &rtp = save_rtp 2833 delete('Xruntime', 'rf') 2834enddef 2835 2836def Test_expr7_method_call() 2837 var lines =<< trim END 2838 new 2839 setline(1, ['first', 'last']) 2840 'second'->append(1) 2841 "third"->append(2) 2842 assert_equal(['first', 'second', 'third', 'last'], getline(1, '$')) 2843 bwipe! 2844 2845 var bufnr = bufnr() 2846 var loclist = [{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] 2847 loclist->setloclist(0) 2848 assert_equal([{bufnr: bufnr, 2849 lnum: 42, 2850 col: 17, 2851 text: 'wrong', 2852 pattern: '', 2853 valid: 1, 2854 vcol: 0, 2855 nr: 0, 2856 type: '', 2857 module: ''} 2858 ], getloclist(0)) 2859 2860 var result: bool = get({n: 0}, 'n', 0) 2861 assert_equal(false, result) 2862 2863 assert_equal('+string+', 'string'->((s) => '+' .. s .. '+')()) 2864 assert_equal('-text-', 'text'->((s, c) => c .. s .. c)('-')) 2865 2866 var Join = (l) => join(l, 'x') 2867 assert_equal('axb', ['a', 'b']->(Join)()) 2868 END 2869 CheckDefAndScriptSuccess(lines) 2870enddef 2871 2872 2873def Test_expr7_not() 2874 var lines =<< trim END 2875 assert_equal(true, !'') 2876 assert_equal(true, ![]) 2877 assert_equal(false, !'asdf') 2878 assert_equal(false, ![2]) 2879 assert_equal(true, !!'asdf') 2880 assert_equal(true, !![2]) 2881 2882 assert_equal(true, ! false) 2883 assert_equal(true, !! true) 2884 assert_equal(true, ! ! true) 2885 assert_equal(true, !!! false) 2886 assert_equal(true, ! ! ! false) 2887 2888 g:true = true 2889 g:false = false 2890 assert_equal(true, ! g:false) 2891 assert_equal(true, !! g:true) 2892 assert_equal(true, ! ! g:true) 2893 assert_equal(true, !!! g:false) 2894 assert_equal(true, ! ! ! g:false) 2895 unlet g:true 2896 unlet g:false 2897 2898 assert_equal(true, !test_null_partial()) 2899 assert_equal(false, !() => 'yes') 2900 2901 assert_equal(true, !test_null_dict()) 2902 assert_equal(true, !{}) 2903 assert_equal(false, !{yes: 'no'}) 2904 2905 if has('channel') 2906 assert_equal(true, !test_null_job()) 2907 assert_equal(true, !test_null_channel()) 2908 endif 2909 2910 assert_equal(true, !test_null_blob()) 2911 assert_equal(true, !0z) 2912 assert_equal(false, !0z01) 2913 2914 assert_equal(true, !test_void()) 2915 assert_equal(true, !test_unknown()) 2916 2917 assert_equal(false, ![1, 2, 3]->reverse()) 2918 assert_equal(true, ![]->reverse()) 2919 END 2920 CheckDefAndScriptSuccess(lines) 2921enddef 2922 2923func Test_expr7_fails() 2924 call CheckDefFailure(["var x = (12"], "E1097:", 3) 2925 call CheckScriptFailure(['vim9script', "var x = (12"], 'E110:', 2) 2926 2927 call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1) 2928 call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1) 2929 call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1) 2930 call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E39:", 'E745:', 1) 2931 call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E39:", 'E728:', 1) 2932 2933 call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1) 2934 call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1) 2935 2936 call CheckDefFailure(["var x = [1, 2"], "E697:", 2) 2937 call CheckScriptFailure(['vim9script', "var x = [1, 2"], 'E696:', 2) 2938 2939 call CheckDefAndScriptFailure2(["var x = [notfound]"], "E1001:", 'E121:', 1) 2940 2941 call CheckDefAndScriptFailure2(["var X = () => 123)"], "E488:", 'E15:', 1) 2942 call CheckDefAndScriptFailure(["var x = 123->((x) => x + 5)"], "E107:", 1) 2943 2944 call CheckDefAndScriptFailure(["var x = ¬exist"], 'E113:', 1) 2945 call CheckDefAndScriptFailure2(["&grepprg = [343]"], 'E1012:', 'E730:', 1) 2946 2947 call CheckDefExecAndScriptFailure(["echo s:doesnt_exist"], 'E121:', 1) 2948 call CheckDefExecAndScriptFailure(["echo g:doesnt_exist"], 'E121:', 1) 2949 2950 call CheckDefAndScriptFailure2(["echo a:somevar"], 'E1075:', 'E121:', 1) 2951 call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1) 2952 call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1) 2953 2954 call CheckDefExecAndScriptFailure(["var x = +g:astring"], 'E1030:', 1) 2955 call CheckDefExecAndScriptFailure(["var x = +g:ablob"], 'E974:', 1) 2956 call CheckDefExecAndScriptFailure(["var x = +g:alist"], 'E745:', 1) 2957 call CheckDefExecAndScriptFailure(["var x = +g:adict"], 'E728:', 1) 2958 2959 call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E715:', 'E15:', 2) 2960 2961 call CheckDefAndScriptFailure2(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 'E260: Missing name after ->', 1) 2962 2963 call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2) 2964 call CheckScriptFailure(['vim9script', "[1, 2->len()"], 'E696:', 2) 2965 2966 call CheckDefFailure(["{a: 1->len()"], 'E723:', 2) 2967 call CheckScriptFailure(['vim9script', "{a: 1->len()"], 'E722:', 2) 2968 2969 call CheckDefExecFailure(["{['a']: 1->len()"], 'E723:', 2) 2970 call CheckScriptFailure(['vim9script', "{['a']: 1->len()"], 'E722:', 2) 2971endfunc 2972 2973let g:Funcrefs = [function('add')] 2974 2975func CallMe(arg) 2976 return a:arg 2977endfunc 2978 2979func CallMe2(one, two) 2980 return a:one .. a:two 2981endfunc 2982 2983def Test_expr7_trailing() 2984 var lines =<< trim END 2985 # user function call 2986 assert_equal(123, g:CallMe(123)) 2987 assert_equal(123, g:CallMe( 123)) 2988 assert_equal(123, g:CallMe(123 )) 2989 assert_equal('yesno', g:CallMe2('yes', 'no')) 2990 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 2991 assert_equal('nothing', g:CallMe('nothing')) 2992 2993 # partial call 2994 var Part = function('g:CallMe') 2995 assert_equal('yes', Part('yes')) 2996 2997 # funcref call, using list index 2998 var l = [] 2999 g:Funcrefs[0](l, 2) 3000 assert_equal([2], l) 3001 3002 # method call 3003 l = [2, 5, 6] 3004 l->map((k, v) => k + v) 3005 assert_equal([2, 6, 8], l) 3006 3007 # lambda method call 3008 l = [2, 5] 3009 l->((ll) => add(ll, 8))() 3010 assert_equal([2, 5, 8], l) 3011 3012 # dict member 3013 var d = {key: 123} 3014 assert_equal(123, d.key) 3015 END 3016 CheckDefAndScriptSuccess(lines) 3017enddef 3018 3019def Test_expr7_string_subscript() 3020 var lines =<< trim END 3021 var text = 'abcdef' 3022 assert_equal('f', text[-1]) 3023 assert_equal('a', text[0]) 3024 assert_equal('e', text[4]) 3025 assert_equal('f', text[5]) 3026 assert_equal('', text[6]) 3027 3028 text = 'ábçdë' 3029 assert_equal('ë', text[-1]) 3030 assert_equal('d', text[-2]) 3031 assert_equal('ç', text[-3]) 3032 assert_equal('b', text[-4]) 3033 assert_equal('á', text[-5]) 3034 assert_equal('', text[-6]) 3035 3036 text = 'ábçdëf' 3037 assert_equal('', text[-999]) 3038 assert_equal('f', text[-1]) 3039 assert_equal('á', text[0]) 3040 assert_equal('b', text[1]) 3041 assert_equal('ç', text[2]) 3042 assert_equal('d', text[3]) 3043 assert_equal('ë', text[4]) 3044 assert_equal('f', text[5]) 3045 assert_equal('', text[6]) 3046 assert_equal('', text[999]) 3047 3048 assert_equal('ábçdëf', text[0 : -1]) 3049 assert_equal('ábçdëf', text[0 : -1]) 3050 assert_equal('ábçdëf', text[0 : -1]) 3051 assert_equal('ábçdëf', text[0 : -1]) 3052 assert_equal('ábçdëf', text[0 3053 : -1]) 3054 assert_equal('ábçdëf', text[0 : 3055 -1]) 3056 assert_equal('ábçdëf', text[0 : -1 3057 ]) 3058 assert_equal('bçdëf', text[1 : -1]) 3059 assert_equal('çdëf', text[2 : -1]) 3060 assert_equal('dëf', text[3 : -1]) 3061 assert_equal('ëf', text[4 : -1]) 3062 assert_equal('f', text[5 : -1]) 3063 assert_equal('', text[6 : -1]) 3064 assert_equal('', text[999 : -1]) 3065 3066 assert_equal('ábçd', text[: 3]) 3067 assert_equal('bçdëf', text[1 :]) 3068 assert_equal('ábçdëf', text[:]) 3069 END 3070 CheckDefAndScriptSuccess(lines) 3071 3072 lines =<< trim END 3073 var d = 'asdf'[1 : 3074 END 3075 CheckDefFailure(lines, 'E1097:', 3) 3076 CheckScriptFailure(['vim9script'] + lines, 'E15:', 2) 3077 3078 lines =<< trim END 3079 var d = 'asdf'[1 : xxx] 3080 END 3081 CheckDefAndScriptFailure2(lines, 'E1001:', 'E121:', 1) 3082 3083 lines =<< trim END 3084 var d = 'asdf'[1 : 2 3085 END 3086 CheckDefFailure(lines, 'E1097:', 3) 3087 CheckScriptFailure(['vim9script'] + lines, 'E111:', 2) 3088 3089 lines =<< trim END 3090 var d = 'asdf'[1 : 2 3091 echo d 3092 END 3093 CheckDefAndScriptFailure(lines, 'E111:', 2) 3094 3095 lines =<< trim END 3096 var d = 'asdf'['1'] 3097 echo d 3098 END 3099 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1) 3100 3101 lines =<< trim END 3102 var d = 'asdf'['1' : 2] 3103 echo d 3104 END 3105 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1) 3106 3107 lines =<< trim END 3108 var d = 'asdf'[1 : '2'] 3109 echo d 3110 END 3111 CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "2"', 1) 3112enddef 3113 3114def Test_expr7_list_subscript() 3115 var lines =<< trim END 3116 var list = [0, 1, 2, 3, 4] 3117 assert_equal(0, list[0]) 3118 assert_equal(4, list[4]) 3119 assert_equal(4, list[-1]) 3120 assert_equal(0, list[-5]) 3121 3122 assert_equal([0, 1, 2, 3, 4], list[0 : 4]) 3123 assert_equal([0, 1, 2, 3, 4], list[:]) 3124 assert_equal([1, 2, 3, 4], list[1 :]) 3125 assert_equal([2, 3, 4], list[2 : -1]) 3126 assert_equal([4], list[4 : -1]) 3127 assert_equal([], list[5 : -1]) 3128 assert_equal([], list[999 : -1]) 3129 assert_equal([1, 2, 3, 4], list[g:theone : g:thefour]) 3130 3131 assert_equal([0, 1, 2, 3], list[0 : 3]) 3132 assert_equal([0], list[0 : 0]) 3133 assert_equal([0, 1, 2, 3, 4], list[0 : -1]) 3134 assert_equal([0, 1, 2], list[0 : -3]) 3135 assert_equal([0], list[0 : -5]) 3136 assert_equal([], list[0 : -6]) 3137 assert_equal([], list[0 : -99]) 3138 END 3139 CheckDefAndScriptSuccess(lines) 3140 3141 lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]'] 3142 CheckDefExecAndScriptFailure2(lines, 'E1012:', 'E1030:', 2) 3143 3144 lines =<< trim END 3145 var ld = [] 3146 def Func() 3147 eval ld[0].key 3148 enddef 3149 defcompile 3150 END 3151 CheckDefAndScriptSuccess(lines) 3152enddef 3153 3154def Test_expr7_dict_subscript() 3155 var lines =<< trim END 3156 var l = [{lnum: 2}, {lnum: 1}] 3157 var res = l[0].lnum > l[1].lnum 3158 assert_true(res) 3159 3160 var dd = {} 3161 def Func1() 3162 eval dd.key1.key2 3163 enddef 3164 def Func2() 3165 eval dd['key1'].key2 3166 enddef 3167 defcompile 3168 END 3169 CheckDefAndScriptSuccess(lines) 3170enddef 3171 3172def Test_expr7_subscript_linebreak() 3173 var lines =<< trim END 3174 var range = range( 3175 3) 3176 var l = range 3177 ->mapnew('string(v:key)') 3178 assert_equal(['0', '1', '2'], l) 3179 3180 l = range 3181 ->mapnew('string(v:key)') 3182 assert_equal(['0', '1', '2'], l) 3183 3184 l = range # comment 3185 ->mapnew('string(v:key)') 3186 assert_equal(['0', '1', '2'], l) 3187 3188 l = range 3189 3190 ->mapnew('string(v:key)') 3191 assert_equal(['0', '1', '2'], l) 3192 3193 l = range 3194 # comment 3195 ->mapnew('string(v:key)') 3196 assert_equal(['0', '1', '2'], l) 3197 3198 assert_equal('1', l[ 3199 1]) 3200 3201 var d = {one: 33} 3202 assert_equal(33, d 3203 .one) 3204 END 3205 CheckDefAndScriptSuccess(lines) 3206 3207 lines =<< trim END 3208 var d = {one: 33} 3209 assert_equal(33, d. 3210 one) 3211 END 3212 CheckDefAndScriptFailure2(lines, 'E1127:', 'E116:', 2) 3213enddef 3214 3215func Test_expr7_trailing_fails() 3216 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8))'], 'E107:', 2) 3217 call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8)) ()'], 'E274:', 2) 3218endfunc 3219 3220func Test_expr_fails() 3221 call CheckDefAndScriptFailure2(["var x = '1'is2"], 'E488:', 'E15:', 1) 3222 call CheckDefAndScriptFailure2(["var x = '1'isnot2"], 'E488:', 'E15:', 1) 3223 3224 call CheckDefAndScriptFailure2(["CallMe ('yes')"], 'E476:', 'E492:', 1) 3225 3226 call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1) 3227 3228 call CheckDefAndScriptFailure2(["v:nosuch += 3"], 'E1001:', 'E121:', 1) 3229 call CheckDefAndScriptFailure(["var v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1) 3230 call CheckDefAndScriptFailure2(["var asdf = v:nosuch"], 'E1001:', 'E121:', 1) 3231 3232 call CheckDefFailure(["echo len('asdf'"], 'E110:', 2) 3233 call CheckScriptFailure(['vim9script', "echo len('asdf'"], 'E116:', 2) 3234 3235 call CheckDefAndScriptFailure2(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 'E117:', 1) 3236 call CheckDefAndScriptFailure(["echo doesnotexist()"], 'E117:', 1) 3237endfunc 3238 3239" vim: shiftwidth=2 sts=2 expandtab 3240