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