1" Test various aspects of the Vim9 script language. 2 3source check.vim 4source term_util.vim 5source view_util.vim 6source vim9.vim 7source shared.vim 8 9def Test_syntax() 10 let var = 234 11 let other: list<string> = ['asdf'] 12enddef 13 14def Test_range_only() 15 new 16 setline(1, ['blah', 'Blah']) 17 :/Blah/ 18 assert_equal(2, getcurpos()[1]) 19 bwipe! 20 21 # without range commands use current line 22 new 23 setline(1, ['one', 'two', 'three']) 24 :2 25 print 26 assert_equal('two', Screenline(&lines)) 27 :3 28 list 29 assert_equal('three$', Screenline(&lines)) 30 bwipe! 31enddef 32 33let s:appendToMe = 'xxx' 34let s:addToMe = 111 35let g:existing = 'yes' 36let g:inc_counter = 1 37let $SOME_ENV_VAR = 'some' 38let g:alist = [7] 39let g:astring = 'text' 40let g:anumber = 123 41 42def Test_assignment() 43 let bool1: bool = true 44 assert_equal(v:true, bool1) 45 let bool2: bool = false 46 assert_equal(v:false, bool2) 47 48 call CheckDefFailure(['let x:string'], 'E1069:') 49 call CheckDefFailure(['let x:string = "x"'], 'E1069:') 50 call CheckDefFailure(['let a:string = "x"'], 'E1069:') 51 call CheckDefFailure(['let lambda = {-> "lambda"}'], 'E704:') 52 53 let nr: number = 1234 54 call CheckDefFailure(['let nr: number = "asdf"'], 'E1012:') 55 56 let a: number = 6 #comment 57 assert_equal(6, a) 58 59 if has('channel') 60 let chan1: channel 61 let job1: job 62 let job2: job = job_start('willfail') 63 endif 64 if has('float') 65 let float1: float = 3.4 66 endif 67 let Funky1: func 68 let Funky2: func = function('len') 69 let Party2: func = funcref('g:Test_syntax') 70 71 g:newvar = 'new' #comment 72 assert_equal('new', g:newvar) 73 74 assert_equal('yes', g:existing) 75 g:existing = 'no' 76 assert_equal('no', g:existing) 77 78 v:char = 'abc' 79 assert_equal('abc', v:char) 80 81 $ENVVAR = 'foobar' 82 assert_equal('foobar', $ENVVAR) 83 $ENVVAR = '' 84 85 let lines =<< trim END 86 vim9script 87 $ENVVAR = 'barfoo' 88 assert_equal('barfoo', $ENVVAR) 89 $ENVVAR = '' 90 END 91 call CheckScriptSuccess(lines) 92 93 s:appendToMe ..= 'yyy' 94 assert_equal('xxxyyy', s:appendToMe) 95 s:addToMe += 222 96 assert_equal(333, s:addToMe) 97 s:newVar = 'new' 98 assert_equal('new', s:newVar) 99 100 set ts=7 101 &ts += 1 102 assert_equal(8, &ts) 103 &ts -= 3 104 assert_equal(5, &ts) 105 &ts *= 2 106 assert_equal(10, &ts) 107 &ts /= 3 108 assert_equal(3, &ts) 109 set ts=10 110 &ts %= 4 111 assert_equal(2, &ts) 112 113 if has('float') 114 let f100: float = 100.0 115 f100 /= 5 116 assert_equal(20.0, f100) 117 118 let f200: float = 200.0 119 f200 /= 5.0 120 assert_equal(40.0, f200) 121 122 CheckDefFailure(['let nr: number = 200', 'nr /= 5.0'], 'E1012:') 123 endif 124 125 lines =<< trim END 126 &ts = 6 127 &ts += 3 128 assert_equal(9, &ts) 129 130 &l:ts = 6 131 assert_equal(6, &ts) 132 &l:ts += 2 133 assert_equal(8, &ts) 134 135 &g:ts = 6 136 assert_equal(6, &g:ts) 137 &g:ts += 2 138 assert_equal(8, &g:ts) 139 END 140 CheckDefAndScriptSuccess(lines) 141 142 CheckDefFailure(['¬ex += 3'], 'E113:') 143 CheckDefFailure(['&ts ..= "xxx"'], 'E1019:') 144 CheckDefFailure(['&ts = [7]'], 'E1012:') 145 CheckDefExecFailure(['&ts = g:alist'], 'E1029: Expected number but got list') 146 CheckDefFailure(['&ts = "xx"'], 'E1012:') 147 CheckDefExecFailure(['&ts = g:astring'], 'E1029: Expected number but got string') 148 CheckDefFailure(['&path += 3'], 'E1012:') 149 CheckDefExecFailure(['&bs = "asdf"'], 'E474:') 150 # test freeing ISN_STOREOPT 151 CheckDefFailure(['&ts = 3', 'let asdf'], 'E1022:') 152 &ts = 8 153 154 lines =<< trim END 155 let save_TI = &t_TI 156 &t_TI = '' 157 assert_equal('', &t_TI) 158 &t_TI = 'xxx' 159 assert_equal('xxx', &t_TI) 160 &t_TI = save_TI 161 END 162 CheckDefSuccess(lines) 163 CheckScriptSuccess(['vim9script'] + lines) 164 165 CheckDefFailure(['&t_TI = 123'], 'E1012:') 166 CheckScriptFailure(['vim9script', '&t_TI = 123'], 'E928:') 167 168 CheckDefFailure(['let s:var = 123'], 'E1101:') 169 CheckDefFailure(['let s:var: number'], 'E1101:') 170 171 lines =<< trim END 172 vim9script 173 def SomeFunc() 174 s:var = 123 175 enddef 176 defcompile 177 END 178 call CheckScriptFailure(lines, 'E1089:') 179 180 g:inc_counter += 1 181 assert_equal(2, g:inc_counter) 182 183 $SOME_ENV_VAR ..= 'more' 184 assert_equal('somemore', $SOME_ENV_VAR) 185 call CheckDefFailure(['$SOME_ENV_VAR += "more"'], 'E1051:') 186 call CheckDefFailure(['$SOME_ENV_VAR += 123'], 'E1012:') 187 188 lines =<< trim END 189 @c = 'areg' 190 @c ..= 'add' 191 assert_equal('aregadd', @c) 192 END 193 CheckDefAndScriptSuccess(lines) 194 195 call CheckDefFailure(['@a += "more"'], 'E1051:') 196 call CheckDefFailure(['@a += 123'], 'E1012:') 197 198 v:errmsg = 'none' 199 v:errmsg ..= 'again' 200 assert_equal('noneagain', v:errmsg) 201 call CheckDefFailure(['v:errmsg += "more"'], 'E1051:') 202 call CheckDefFailure(['v:errmsg += 123'], 'E1012:') 203 204 # single letter variables 205 a = 123 206 assert_equal(123, a) 207 let b: number 208 b = 123 209 assert_equal(123, b) 210 let g: number 211 g = 123 212 assert_equal(123, g) 213 let s: number 214 s = 123 215 assert_equal(123, s) 216 let t: number 217 t = 123 218 assert_equal(123, t) 219 let v: number 220 v = 123 221 assert_equal(123, v) 222 let w: number 223 w = 123 224 assert_equal(123, w) 225enddef 226 227def Test_vim9_single_char_vars() 228 let lines =<< trim END 229 vim9script 230 231 # single character variable declarations work 232 let a: string 233 let b: number 234 let l: list<any> 235 let s: string 236 let t: number 237 let v: number 238 let w: number 239 240 # script-local variables can be used without s: prefix 241 a = 'script-a' 242 b = 111 243 l = [1, 2, 3] 244 s = 'script-s' 245 t = 222 246 v = 333 247 w = 444 248 249 assert_equal('script-a', a) 250 assert_equal(111, b) 251 assert_equal([1, 2, 3], l) 252 assert_equal('script-s', s) 253 assert_equal(222, t) 254 assert_equal(333, v) 255 assert_equal(444, w) 256 END 257 writefile(lines, 'Xsinglechar') 258 source Xsinglechar 259 delete('Xsinglechar') 260enddef 261 262def Test_assignment_list() 263 let list1: list<bool> = [false, true, false] 264 let list2: list<number> = [1, 2, 3] 265 let list3: list<string> = ['sdf', 'asdf'] 266 let list4: list<any> = ['yes', true, 1234] 267 let list5: list<blob> = [0z01, 0z02] 268 269 let listS: list<string> = [] 270 let listN: list<number> = [] 271 272 assert_equal([1, 2, 3], list2) 273 list2[-1] = 99 274 assert_equal([1, 2, 99], list2) 275 list2[-2] = 88 276 assert_equal([1, 88, 99], list2) 277 list2[-3] = 77 278 assert_equal([77, 88, 99], list2) 279 list2 += [100] 280 assert_equal([77, 88, 99, 100], list2) 281 282 list3 += ['end'] 283 assert_equal(['sdf', 'asdf', 'end'], list3) 284 285 286 call CheckDefExecFailure(['let ll = [1, 2, 3]', 'll[-4] = 6'], 'E684:') 287 call CheckDefExecFailure(['let [v1, v2] = [1, 2]'], 'E1092:') 288 289 # type becomes list<any> 290 let somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c'] 291enddef 292 293def Test_assignment_list_vim9script() 294 let lines =<< trim END 295 vim9script 296 let v1: number 297 let v2: number 298 let v3: number 299 [v1, v2, v3] = [1, 2, 3] 300 assert_equal([1, 2, 3], [v1, v2, v3]) 301 END 302 call CheckScriptSuccess(lines) 303enddef 304 305def Test_assignment_dict() 306 let dict1: dict<bool> = #{one: false, two: true} 307 let dict2: dict<number> = #{one: 1, two: 2} 308 let dict3: dict<string> = #{key: 'value'} 309 let dict4: dict<any> = #{one: 1, two: '2'} 310 let dict5: dict<blob> = #{one: 0z01, two: 0z02} 311 312 # overwrite 313 dict3['key'] = 'another' 314 315 # empty key can be used 316 let dd = {} 317 dd[""] = 6 318 assert_equal({'': 6}, dd) 319 320 # type becomes dict<any> 321 let somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'} 322 323 # assignment to script-local dict 324 let lines =<< trim END 325 vim9script 326 let test: dict<any> = {} 327 def FillDict(): dict<any> 328 test['a'] = 43 329 return test 330 enddef 331 assert_equal(#{a: 43}, FillDict()) 332 END 333 call CheckScriptSuccess(lines) 334 335 lines =<< trim END 336 vim9script 337 let test: dict<any> 338 def FillDict(): dict<any> 339 test['a'] = 43 340 return test 341 enddef 342 FillDict() 343 END 344 call CheckScriptFailure(lines, 'E1103:') 345 346 # assignment to global dict 347 lines =<< trim END 348 vim9script 349 g:test = {} 350 def FillDict(): dict<any> 351 g:test['a'] = 43 352 return g:test 353 enddef 354 assert_equal(#{a: 43}, FillDict()) 355 END 356 call CheckScriptSuccess(lines) 357 358 # assignment to buffer dict 359 lines =<< trim END 360 vim9script 361 b:test = {} 362 def FillDict(): dict<any> 363 b:test['a'] = 43 364 return b:test 365 enddef 366 assert_equal(#{a: 43}, FillDict()) 367 END 368 call CheckScriptSuccess(lines) 369enddef 370 371def Test_assignment_local() 372 # Test in a separated file in order not to the current buffer/window/tab is 373 # changed. 374 let script_lines: list<string> =<< trim END 375 let b:existing = 'yes' 376 let w:existing = 'yes' 377 let t:existing = 'yes' 378 379 def Test_assignment_local_internal() 380 b:newvar = 'new' 381 assert_equal('new', b:newvar) 382 assert_equal('yes', b:existing) 383 b:existing = 'no' 384 assert_equal('no', b:existing) 385 b:existing ..= 'NO' 386 assert_equal('noNO', b:existing) 387 388 w:newvar = 'new' 389 assert_equal('new', w:newvar) 390 assert_equal('yes', w:existing) 391 w:existing = 'no' 392 assert_equal('no', w:existing) 393 w:existing ..= 'NO' 394 assert_equal('noNO', w:existing) 395 396 t:newvar = 'new' 397 assert_equal('new', t:newvar) 398 assert_equal('yes', t:existing) 399 t:existing = 'no' 400 assert_equal('no', t:existing) 401 t:existing ..= 'NO' 402 assert_equal('noNO', t:existing) 403 enddef 404 call Test_assignment_local_internal() 405 END 406 call CheckScriptSuccess(script_lines) 407enddef 408 409def Test_assignment_default() 410 411 # Test default values. 412 let thebool: bool 413 assert_equal(v:false, thebool) 414 415 let thenumber: number 416 assert_equal(0, thenumber) 417 418 if has('float') 419 let thefloat: float 420 assert_equal(0.0, thefloat) 421 endif 422 423 let thestring: string 424 assert_equal('', thestring) 425 426 let theblob: blob 427 assert_equal(0z, theblob) 428 429 let Thefunc: func 430 assert_equal(test_null_function(), Thefunc) 431 432 let thelist: list<any> 433 assert_equal([], thelist) 434 435 let thedict: dict<any> 436 assert_equal({}, thedict) 437 438 if has('channel') 439 let thejob: job 440 assert_equal(test_null_job(), thejob) 441 442 let thechannel: channel 443 assert_equal(test_null_channel(), thechannel) 444 445 if has('unix') && executable('cat') 446 # check with non-null job and channel, types must match 447 thejob = job_start("cat ", #{}) 448 thechannel = job_getchannel(thejob) 449 job_stop(thejob, 'kill') 450 endif 451 endif 452 453 let nr = 1234 | nr = 5678 454 assert_equal(5678, nr) 455enddef 456 457def Test_assignment_var_list() 458 let v1: string 459 let v2: string 460 let vrem: list<string> 461 [v1] = ['aaa'] 462 assert_equal('aaa', v1) 463 464 [v1, v2] = ['one', 'two'] 465 assert_equal('one', v1) 466 assert_equal('two', v2) 467 468 [v1, v2; vrem] = ['one', 'two'] 469 assert_equal('one', v1) 470 assert_equal('two', v2) 471 assert_equal([], vrem) 472 473 [v1, v2; vrem] = ['one', 'two', 'three'] 474 assert_equal('one', v1) 475 assert_equal('two', v2) 476 assert_equal(['three'], vrem) 477 478 [&ts, &sw] = [3, 4] 479 assert_equal(3, &ts) 480 assert_equal(4, &sw) 481 set ts=8 sw=4 482enddef 483 484def Test_assignment_vim9script() 485 let lines =<< trim END 486 vim9script 487 def Func(): list<number> 488 return [1, 2] 489 enddef 490 let var1: number 491 let var2: number 492 [var1, var2] = 493 Func() 494 assert_equal(1, var1) 495 assert_equal(2, var2) 496 let ll = 497 Func() 498 assert_equal([1, 2], ll) 499 500 @/ = 'text' 501 assert_equal('text', @/) 502 @0 = 'zero' 503 assert_equal('zero', @0) 504 @1 = 'one' 505 assert_equal('one', @1) 506 @9 = 'nine' 507 assert_equal('nine', @9) 508 @- = 'minus' 509 assert_equal('minus', @-) 510 if has('clipboard_working') 511 @* = 'star' 512 assert_equal('star', @*) 513 @+ = 'plus' 514 assert_equal('plus', @+) 515 endif 516 517 let a: number = 123 518 assert_equal(123, a) 519 let s: string = 'yes' 520 assert_equal('yes', s) 521 let b: number = 42 522 assert_equal(42, b) 523 let w: number = 43 524 assert_equal(43, w) 525 let t: number = 44 526 assert_equal(44, t) 527 END 528 CheckScriptSuccess(lines) 529enddef 530 531def Mess(): string 532 v:foldstart = 123 533 return 'xxx' 534enddef 535 536def Test_assignment_failure() 537 call CheckDefFailure(['let var=234'], 'E1004:') 538 call CheckDefFailure(['let var =234'], 'E1004:') 539 call CheckDefFailure(['let var= 234'], 'E1004:') 540 541 call CheckScriptFailure(['vim9script', 'let var=234'], 'E1004:') 542 call CheckScriptFailure(['vim9script', 'let var=234'], "before and after '='") 543 call CheckScriptFailure(['vim9script', 'let var =234'], 'E1004:') 544 call CheckScriptFailure(['vim9script', 'let var= 234'], 'E1004:') 545 call CheckScriptFailure(['vim9script', 'let var = 234', 'var+=234'], 'E1004:') 546 call CheckScriptFailure(['vim9script', 'let var = 234', 'var+=234'], "before and after '+='") 547 call CheckScriptFailure(['vim9script', 'let var = "x"', 'var..="y"'], 'E1004:') 548 call CheckScriptFailure(['vim9script', 'let var = "x"', 'var..="y"'], "before and after '..='") 549 550 call CheckDefFailure(['let true = 1'], 'E1034:') 551 call CheckDefFailure(['let false = 1'], 'E1034:') 552 553 call CheckDefFailure(['[a; b; c] = g:list'], 'E452:') 554 call CheckDefExecFailure(['let a: number', 555 '[a] = test_null_list()'], 'E1093:') 556 call CheckDefExecFailure(['let a: number', 557 '[a] = []'], 'E1093:') 558 call CheckDefExecFailure(['let x: number', 559 'let y: number', 560 '[x, y] = [1]'], 'E1093:') 561 call CheckDefExecFailure(['let x: number', 562 'let y: number', 563 'let z: list<number>', 564 '[x, y; z] = [1]'], 'E1093:') 565 566 call CheckDefFailure(['let somevar'], "E1022:") 567 call CheckDefFailure(['let &tabstop = 4'], 'E1052:') 568 call CheckDefFailure(['&g:option = 5'], 'E113:') 569 call CheckScriptFailure(['vim9script', 'let &tabstop = 4'], 'E1052:') 570 571 call CheckDefFailure(['let $VAR = 5'], 'E1016: Cannot declare an environment variable:') 572 call CheckScriptFailure(['vim9script', 'let $ENV = "xxx"'], 'E1016:') 573 574 if has('dnd') 575 call CheckDefFailure(['let @~ = 5'], 'E1066:') 576 else 577 call CheckDefFailure(['let @~ = 5'], 'E354:') 578 call CheckDefFailure(['@~ = 5'], 'E354:') 579 endif 580 call CheckDefFailure(['let @a = 5'], 'E1066:') 581 call CheckDefFailure(['let @/ = "x"'], 'E1066:') 582 call CheckScriptFailure(['vim9script', 'let @a = "abc"'], 'E1066:') 583 584 call CheckDefFailure(['let g:var = 5'], 'E1016: Cannot declare a global variable:') 585 call CheckDefFailure(['let w:var = 5'], 'E1016: Cannot declare a window variable:') 586 call CheckDefFailure(['let b:var = 5'], 'E1016: Cannot declare a buffer variable:') 587 call CheckDefFailure(['let t:var = 5'], 'E1016: Cannot declare a tab variable:') 588 589 call CheckDefFailure(['let anr = 4', 'anr ..= "text"'], 'E1019:') 590 call CheckDefFailure(['let xnr += 4'], 'E1020:', 1) 591 call CheckScriptFailure(['vim9script', 'let xnr += 4'], 'E1020:') 592 call CheckDefFailure(["let xnr = xnr + 1"], 'E1001:', 1) 593 call CheckScriptFailure(['vim9script', 'let xnr = xnr + 4'], 'E121:') 594 595 call CheckScriptFailure(['vim9script', 'def Func()', 'let dummy = s:notfound', 'enddef', 'defcompile'], 'E1108:') 596 597 call CheckDefFailure(['let var: list<string> = [123]'], 'expected list<string> but got list<number>') 598 call CheckDefFailure(['let var: list<number> = ["xx"]'], 'expected list<number> but got list<string>') 599 600 call CheckDefFailure(['let var: dict<string> = #{key: 123}'], 'expected dict<string> but got dict<number>') 601 call CheckDefFailure(['let var: dict<number> = #{key: "xx"}'], 'expected dict<number> but got dict<string>') 602 603 call CheckDefFailure(['let var = feedkeys("0")'], 'E1031:') 604 call CheckDefFailure(['let var: number = feedkeys("0")'], 'expected number but got void') 605 606 call CheckDefFailure(['let var: dict <number>'], 'E1068:') 607 call CheckDefFailure(['let var: dict<number'], 'E1009:') 608 609 call assert_fails('s/^/\=Mess()/n', 'E794:') 610 call CheckDefFailure(['let var: dict<number'], 'E1009:') 611 612 call CheckDefFailure(['w:foo: number = 10'], 613 'E488: Trailing characters: : number = 1') 614 call CheckDefFailure(['t:foo: bool = true'], 615 'E488: Trailing characters: : bool = true') 616 call CheckDefFailure(['b:foo: string = "x"'], 617 'E488: Trailing characters: : string = "x"') 618 call CheckDefFailure(['g:foo: number = 123'], 619 'E488: Trailing characters: : number = 123') 620enddef 621 622def Test_unlet() 623 g:somevar = 'yes' 624 assert_true(exists('g:somevar')) 625 unlet g:somevar 626 assert_false(exists('g:somevar')) 627 unlet! g:somevar 628 629 # also works for script-local variable in legacy Vim script 630 s:somevar = 'legacy' 631 assert_true(exists('s:somevar')) 632 unlet s:somevar 633 assert_false(exists('s:somevar')) 634 unlet! s:somevar 635 636 call CheckScriptFailure([ 637 'vim9script', 638 'let svar = 123', 639 'unlet svar', 640 ], 'E1081:') 641 call CheckScriptFailure([ 642 'vim9script', 643 'let svar = 123', 644 'unlet s:svar', 645 ], 'E1081:') 646 call CheckScriptFailure([ 647 'vim9script', 648 'let svar = 123', 649 'def Func()', 650 ' unlet svar', 651 'enddef', 652 'defcompile', 653 ], 'E1081:') 654 call CheckScriptFailure([ 655 'vim9script', 656 'let svar = 123', 657 'def Func()', 658 ' unlet s:svar', 659 'enddef', 660 'defcompile', 661 ], 'E1081:') 662 663 $ENVVAR = 'foobar' 664 assert_equal('foobar', $ENVVAR) 665 unlet $ENVVAR 666 assert_equal('', $ENVVAR) 667enddef 668 669def Test_delfunction() 670 # Check function is defined in script namespace 671 CheckScriptSuccess([ 672 'vim9script', 673 'func CheckMe()', 674 ' return 123', 675 'endfunc', 676 'assert_equal(123, s:CheckMe())', 677 ]) 678 679 # Check function in script namespace cannot be deleted 680 CheckScriptFailure([ 681 'vim9script', 682 'func DeleteMe1()', 683 'endfunc', 684 'delfunction DeleteMe1', 685 ], 'E1084:') 686 CheckScriptFailure([ 687 'vim9script', 688 'func DeleteMe2()', 689 'endfunc', 690 'def DoThat()', 691 ' delfunction DeleteMe2', 692 'enddef', 693 'DoThat()', 694 ], 'E1084:') 695 CheckScriptFailure([ 696 'vim9script', 697 'def DeleteMe3()', 698 'enddef', 699 'delfunction DeleteMe3', 700 ], 'E1084:') 701 CheckScriptFailure([ 702 'vim9script', 703 'def DeleteMe4()', 704 'enddef', 705 'def DoThat()', 706 ' delfunction DeleteMe4', 707 'enddef', 708 'DoThat()', 709 ], 'E1084:') 710 711 # Check that global :def function can be replaced and deleted 712 let lines =<< trim END 713 vim9script 714 def g:Global(): string 715 return "yes" 716 enddef 717 assert_equal("yes", g:Global()) 718 def! g:Global(): string 719 return "no" 720 enddef 721 assert_equal("no", g:Global()) 722 delfunc g:Global 723 assert_false(exists('*g:Global')) 724 END 725 CheckScriptSuccess(lines) 726 727 # Check that global function can be replaced by a :def function and deleted 728 lines =<< trim END 729 vim9script 730 func g:Global() 731 return "yes" 732 endfunc 733 assert_equal("yes", g:Global()) 734 def! g:Global(): string 735 return "no" 736 enddef 737 assert_equal("no", g:Global()) 738 delfunc g:Global 739 assert_false(exists('*g:Global')) 740 END 741 CheckScriptSuccess(lines) 742 743 # Check that global :def function can be replaced by a function and deleted 744 lines =<< trim END 745 vim9script 746 def g:Global(): string 747 return "yes" 748 enddef 749 assert_equal("yes", g:Global()) 750 func! g:Global() 751 return "no" 752 endfunc 753 assert_equal("no", g:Global()) 754 delfunc g:Global 755 assert_false(exists('*g:Global')) 756 END 757 CheckScriptSuccess(lines) 758enddef 759 760func Test_wrong_type() 761 call CheckDefFailure(['let var: list<nothing>'], 'E1010:') 762 call CheckDefFailure(['let var: list<list<nothing>>'], 'E1010:') 763 call CheckDefFailure(['let var: dict<nothing>'], 'E1010:') 764 call CheckDefFailure(['let var: dict<dict<nothing>>'], 'E1010:') 765 766 call CheckDefFailure(['let var: dict<number'], 'E1009:') 767 call CheckDefFailure(['let var: dict<list<number>'], 'E1009:') 768 769 call CheckDefFailure(['let var: ally'], 'E1010:') 770 call CheckDefFailure(['let var: bram'], 'E1010:') 771 call CheckDefFailure(['let var: cathy'], 'E1010:') 772 call CheckDefFailure(['let var: dom'], 'E1010:') 773 call CheckDefFailure(['let var: freddy'], 'E1010:') 774 call CheckDefFailure(['let var: john'], 'E1010:') 775 call CheckDefFailure(['let var: larry'], 'E1010:') 776 call CheckDefFailure(['let var: ned'], 'E1010:') 777 call CheckDefFailure(['let var: pam'], 'E1010:') 778 call CheckDefFailure(['let var: sam'], 'E1010:') 779 call CheckDefFailure(['let var: vim'], 'E1010:') 780 781 call CheckDefFailure(['let Ref: number', 'Ref()'], 'E1085:') 782 call CheckDefFailure(['let Ref: string', 'let res = Ref()'], 'E1085:') 783endfunc 784 785func Test_const() 786 call CheckDefFailure(['const var = 234', 'var = 99'], 'E1018:') 787 call CheckDefFailure(['const one = 234', 'let one = 99'], 'E1017:') 788 call CheckDefFailure(['const two'], 'E1021:') 789 call CheckDefFailure(['const &option'], 'E996:') 790endfunc 791 792def Test_range_no_colon() 793 call CheckDefFailure(['%s/a/b/'], 'E1050:') 794 call CheckDefFailure(['+ s/a/b/'], 'E1050:') 795 call CheckDefFailure(['- s/a/b/'], 'E1050:') 796 call CheckDefFailure(['. s/a/b/'], 'E1050:') 797enddef 798 799 800def Test_block() 801 let outer = 1 802 { 803 let inner = 2 804 assert_equal(1, outer) 805 assert_equal(2, inner) 806 } 807 assert_equal(1, outer) 808enddef 809 810func Test_block_failure() 811 call CheckDefFailure(['{', 'let inner = 1', '}', 'echo inner'], 'E1001:') 812 call CheckDefFailure(['}'], 'E1025:') 813 call CheckDefFailure(['{', 'echo 1'], 'E1026:') 814endfunc 815 816func g:NoSuchFunc() 817 echo 'none' 818endfunc 819 820def Test_try_catch() 821 let l = [] 822 try # comment 823 add(l, '1') 824 throw 'wrong' 825 add(l, '2') 826 catch # comment 827 add(l, v:exception) 828 finally # comment 829 add(l, '3') 830 endtry # comment 831 assert_equal(['1', 'wrong', '3'], l) 832 833 l = [] 834 try 835 try 836 add(l, '1') 837 throw 'wrong' 838 add(l, '2') 839 catch /right/ 840 add(l, v:exception) 841 endtry 842 catch /wrong/ 843 add(l, 'caught') 844 finally 845 add(l, 'finally') 846 endtry 847 assert_equal(['1', 'caught', 'finally'], l) 848 849 let n: number 850 try 851 n = l[3] 852 catch /E684:/ 853 n = 99 854 endtry 855 assert_equal(99, n) 856 857 try 858 # string slice returns a string, not a number 859 n = g:astring[3] 860 catch /E1029:/ 861 n = 77 862 endtry 863 assert_equal(77, n) 864 865 try 866 n = l[g:astring] 867 catch /E1029:/ 868 n = 88 869 endtry 870 assert_equal(88, n) 871 872 try 873 n = s:does_not_exist 874 catch /E121:/ 875 n = 111 876 endtry 877 assert_equal(111, n) 878 879 try 880 n = g:does_not_exist 881 catch /E121:/ 882 n = 121 883 endtry 884 assert_equal(121, n) 885 886 let d = #{one: 1} 887 try 888 n = d[g:astring] 889 catch /E716:/ 890 n = 222 891 endtry 892 assert_equal(222, n) 893 894 try 895 n = -g:astring 896 catch /E39:/ 897 n = 233 898 endtry 899 assert_equal(233, n) 900 901 try 902 n = +g:astring 903 catch /E1030:/ 904 n = 244 905 endtry 906 assert_equal(244, n) 907 908 try 909 n = +g:alist 910 catch /E745:/ 911 n = 255 912 endtry 913 assert_equal(255, n) 914 915 let nd: dict<any> 916 try 917 nd = {g:anumber: 1} 918 catch /E1029:/ 919 n = 266 920 endtry 921 assert_equal(266, n) 922 923 try 924 [n] = [1, 2, 3] 925 catch /E1093:/ 926 n = 277 927 endtry 928 assert_equal(277, n) 929 930 try 931 &ts = g:astring 932 catch /E1029:/ 933 n = 288 934 endtry 935 assert_equal(288, n) 936 937 try 938 &backspace = 'asdf' 939 catch /E474:/ 940 n = 299 941 endtry 942 assert_equal(299, n) 943 944 l = [1] 945 try 946 l[3] = 3 947 catch /E684:/ 948 n = 300 949 endtry 950 assert_equal(300, n) 951 952 try 953 unlet g:does_not_exist 954 catch /E108:/ 955 n = 322 956 endtry 957 assert_equal(322, n) 958 959 try 960 d = {'text': 1, g:astring: 2} 961 catch /E721:/ 962 n = 333 963 endtry 964 assert_equal(333, n) 965 966 try 967 l = DeletedFunc() 968 catch /E933:/ 969 n = 344 970 endtry 971 assert_equal(344, n) 972 973 try 974 echo len(v:true) 975 catch /E701:/ 976 n = 355 977 endtry 978 assert_equal(355, n) 979 980 let P = function('g:NoSuchFunc') 981 delfunc g:NoSuchFunc 982 try 983 echo P() 984 catch /E117:/ 985 n = 366 986 endtry 987 assert_equal(366, n) 988 989 try 990 echo g:NoSuchFunc() 991 catch /E117:/ 992 n = 377 993 endtry 994 assert_equal(377, n) 995 996 try 997 echo g:alist + 4 998 catch /E745:/ 999 n = 388 1000 endtry 1001 assert_equal(388, n) 1002 1003 try 1004 echo 4 + g:alist 1005 catch /E745:/ 1006 n = 399 1007 endtry 1008 assert_equal(399, n) 1009 1010 try 1011 echo g:alist.member 1012 catch /E715:/ 1013 n = 400 1014 endtry 1015 assert_equal(400, n) 1016 1017 try 1018 echo d.member 1019 catch /E716:/ 1020 n = 411 1021 endtry 1022 assert_equal(411, n) 1023enddef 1024 1025def DeletedFunc(): list<any> 1026 return ['delete me'] 1027enddef 1028defcompile 1029delfunc DeletedFunc 1030 1031def ThrowFromDef() 1032 throw "getout" # comment 1033enddef 1034 1035func CatchInFunc() 1036 try 1037 call ThrowFromDef() 1038 catch 1039 let g:thrown_func = v:exception 1040 endtry 1041endfunc 1042 1043def CatchInDef() 1044 try 1045 ThrowFromDef() 1046 catch 1047 g:thrown_def = v:exception 1048 endtry 1049enddef 1050 1051def ReturnFinally(): string 1052 try 1053 return 'intry' 1054 finally 1055 g:in_finally = 'finally' 1056 endtry 1057 return 'end' 1058enddef 1059 1060def Test_try_catch_nested() 1061 CatchInFunc() 1062 assert_equal('getout', g:thrown_func) 1063 1064 CatchInDef() 1065 assert_equal('getout', g:thrown_def) 1066 1067 assert_equal('intry', ReturnFinally()) 1068 assert_equal('finally', g:in_finally) 1069enddef 1070 1071def Test_try_catch_match() 1072 let seq = 'a' 1073 try 1074 throw 'something' 1075 catch /nothing/ 1076 seq ..= 'x' 1077 catch /some/ 1078 seq ..= 'b' 1079 catch /asdf/ 1080 seq ..= 'x' 1081 catch ?a\?sdf? 1082 seq ..= 'y' 1083 finally 1084 seq ..= 'c' 1085 endtry 1086 assert_equal('abc', seq) 1087enddef 1088 1089def Test_try_catch_fails() 1090 call CheckDefFailure(['catch'], 'E603:') 1091 call CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:') 1092 call CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:') 1093 call CheckDefFailure(['finally'], 'E606:') 1094 call CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:') 1095 call CheckDefFailure(['endtry'], 'E602:') 1096 call CheckDefFailure(['while 1', 'endtry'], 'E170:') 1097 call CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:') 1098 call CheckDefFailure(['if 2', 'endtry'], 'E171:') 1099 call CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:') 1100 1101 call CheckDefFailure(['throw'], 'E1015:') 1102 call CheckDefFailure(['throw xxx'], 'E1001:') 1103enddef 1104 1105def Test_throw_vimscript() 1106 # only checks line continuation 1107 let lines =<< trim END 1108 vim9script 1109 try 1110 throw 'one' 1111 .. 'two' 1112 catch 1113 assert_equal('onetwo', v:exception) 1114 endtry 1115 END 1116 CheckScriptSuccess(lines) 1117enddef 1118 1119def Test_error_in_nested_function() 1120 # an error in a nested :function aborts executin in the calling :def function 1121 let lines =<< trim END 1122 vim9script 1123 def Func() 1124 Error() 1125 g:test_var = 1 1126 enddef 1127 func Error() abort 1128 eval [][0] 1129 endfunc 1130 Func() 1131 END 1132 g:test_var = 0 1133 CheckScriptFailure(lines, 'E684:') 1134 assert_equal(0, g:test_var) 1135enddef 1136 1137def Test_cexpr_vimscript() 1138 # only checks line continuation 1139 set errorformat=File\ %f\ line\ %l 1140 let lines =<< trim END 1141 vim9script 1142 cexpr 'File' 1143 .. ' someFile' .. 1144 ' line 19' 1145 assert_equal(19, getqflist()[0].lnum) 1146 END 1147 CheckScriptSuccess(lines) 1148 set errorformat& 1149enddef 1150 1151def Test_statusline_syntax() 1152 # legacy syntax is used for 'statusline' 1153 let lines =<< trim END 1154 vim9script 1155 func g:Status() 1156 return '%{"x" is# "x"}' 1157 endfunc 1158 set laststatus=2 statusline=%!Status() 1159 redrawstatus 1160 set laststatus statusline= 1161 END 1162 CheckScriptSuccess(lines) 1163enddef 1164 1165def Test_list_vimscript() 1166 # checks line continuation and comments 1167 let lines =<< trim END 1168 vim9script 1169 let mylist = [ 1170 'one', 1171 # comment 1172 'two', # empty line follows 1173 1174 'three', 1175 ] 1176 assert_equal(['one', 'two', 'three'], mylist) 1177 END 1178 CheckScriptSuccess(lines) 1179 1180 # check all lines from heredoc are kept 1181 lines =<< trim END 1182 # comment 1 1183 two 1184 # comment 3 1185 1186 five 1187 # comment 6 1188 END 1189 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines) 1190enddef 1191 1192if has('channel') 1193 let someJob = test_null_job() 1194 1195 def FuncWithError() 1196 echomsg g:someJob 1197 enddef 1198 1199 func Test_convert_emsg_to_exception() 1200 try 1201 call FuncWithError() 1202 catch 1203 call assert_match('Vim:E908:', v:exception) 1204 endtry 1205 endfunc 1206endif 1207 1208let s:export_script_lines =<< trim END 1209 vim9script 1210 let name: string = 'bob' 1211 def Concat(arg: string): string 1212 return name .. arg 1213 enddef 1214 g:result = Concat('bie') 1215 g:localname = name 1216 1217 export const CONST = 1234 1218 export let exported = 9876 1219 export let exp_name = 'John' 1220 export def Exported(): string 1221 return 'Exported' 1222 enddef 1223END 1224 1225def Undo_export_script_lines() 1226 unlet g:result 1227 unlet g:localname 1228enddef 1229 1230def Test_vim9_import_export() 1231 let import_script_lines =<< trim END 1232 vim9script 1233 import {exported, Exported} from './Xexport.vim' 1234 g:imported = exported 1235 exported += 3 1236 g:imported_added = exported 1237 g:imported_func = Exported() 1238 1239 def GetExported(): string 1240 let local_dict = #{ref: Exported} 1241 return local_dict.ref() 1242 enddef 1243 g:funcref_result = GetExported() 1244 1245 import {exp_name} from './Xexport.vim' 1246 g:imported_name = exp_name 1247 exp_name ..= ' Doe' 1248 g:imported_name_appended = exp_name 1249 g:imported_later = exported 1250 END 1251 1252 writefile(import_script_lines, 'Ximport.vim') 1253 writefile(s:export_script_lines, 'Xexport.vim') 1254 1255 source Ximport.vim 1256 1257 assert_equal('bobbie', g:result) 1258 assert_equal('bob', g:localname) 1259 assert_equal(9876, g:imported) 1260 assert_equal(9879, g:imported_added) 1261 assert_equal(9879, g:imported_later) 1262 assert_equal('Exported', g:imported_func) 1263 assert_equal('Exported', g:funcref_result) 1264 assert_equal('John', g:imported_name) 1265 assert_equal('John Doe', g:imported_name_appended) 1266 assert_false(exists('g:name')) 1267 1268 Undo_export_script_lines() 1269 unlet g:imported 1270 unlet g:imported_added 1271 unlet g:imported_later 1272 unlet g:imported_func 1273 unlet g:imported_name g:imported_name_appended 1274 delete('Ximport.vim') 1275 1276 # similar, with line breaks 1277 let import_line_break_script_lines =<< trim END 1278 vim9script 1279 import { 1280 exported, 1281 Exported, 1282 } 1283 from 1284 './Xexport.vim' 1285 g:imported = exported 1286 exported += 5 1287 g:imported_added = exported 1288 g:imported_func = Exported() 1289 END 1290 writefile(import_line_break_script_lines, 'Ximport_lbr.vim') 1291 source Ximport_lbr.vim 1292 1293 assert_equal(9876, g:imported) 1294 assert_equal(9881, g:imported_added) 1295 assert_equal('Exported', g:imported_func) 1296 1297 # exported script not sourced again 1298 assert_false(exists('g:result')) 1299 unlet g:imported 1300 unlet g:imported_added 1301 unlet g:imported_func 1302 delete('Ximport_lbr.vim') 1303 1304 # import inside :def function 1305 let import_in_def_lines =<< trim END 1306 vim9script 1307 def ImportInDef() 1308 import exported from './Xexport.vim' 1309 g:imported = exported 1310 exported += 7 1311 g:imported_added = exported 1312 enddef 1313 ImportInDef() 1314 END 1315 writefile(import_in_def_lines, 'Ximport2.vim') 1316 source Ximport2.vim 1317 # TODO: this should be 9879 1318 assert_equal(9876, g:imported) 1319 assert_equal(9883, g:imported_added) 1320 unlet g:imported 1321 unlet g:imported_added 1322 delete('Ximport2.vim') 1323 1324 let import_star_as_lines =<< trim END 1325 vim9script 1326 import * as Export from './Xexport.vim' 1327 def UseExport() 1328 g:imported = Export.exported 1329 enddef 1330 UseExport() 1331 END 1332 writefile(import_star_as_lines, 'Ximport.vim') 1333 source Ximport.vim 1334 assert_equal(9883, g:imported) 1335 1336 let import_star_as_lines_no_dot =<< trim END 1337 vim9script 1338 import * as Export from './Xexport.vim' 1339 def Func() 1340 let dummy = 1 1341 let imported = Export + dummy 1342 enddef 1343 defcompile 1344 END 1345 writefile(import_star_as_lines_no_dot, 'Ximport.vim') 1346 assert_fails('source Ximport.vim', 'E1060:') 1347 1348 let import_star_as_lines_dot_space =<< trim END 1349 vim9script 1350 import * as Export from './Xexport.vim' 1351 def Func() 1352 let imported = Export . exported 1353 enddef 1354 defcompile 1355 END 1356 writefile(import_star_as_lines_dot_space, 'Ximport.vim') 1357 assert_fails('source Ximport.vim', 'E1074:') 1358 1359 let import_star_as_lines_missing_name =<< trim END 1360 vim9script 1361 import * as Export from './Xexport.vim' 1362 def Func() 1363 let imported = Export. 1364 enddef 1365 defcompile 1366 END 1367 writefile(import_star_as_lines_missing_name, 'Ximport.vim') 1368 assert_fails('source Ximport.vim', 'E1048:') 1369 1370 let import_star_as_lbr_lines =<< trim END 1371 vim9script 1372 import * 1373 as Export 1374 from 1375 './Xexport.vim' 1376 def UseExport() 1377 g:imported = Export.exported 1378 enddef 1379 UseExport() 1380 END 1381 writefile(import_star_as_lbr_lines, 'Ximport.vim') 1382 source Ximport.vim 1383 assert_equal(9883, g:imported) 1384 1385 let import_star_lines =<< trim END 1386 vim9script 1387 import * from './Xexport.vim' 1388 END 1389 writefile(import_star_lines, 'Ximport.vim') 1390 assert_fails('source Ximport.vim', 'E1045:') 1391 1392 # try to import something that exists but is not exported 1393 let import_not_exported_lines =<< trim END 1394 vim9script 1395 import name from './Xexport.vim' 1396 END 1397 writefile(import_not_exported_lines, 'Ximport.vim') 1398 assert_fails('source Ximport.vim', 'E1049:') 1399 1400 # try to import something that is already defined 1401 let import_already_defined =<< trim END 1402 vim9script 1403 let exported = 'something' 1404 import exported from './Xexport.vim' 1405 END 1406 writefile(import_already_defined, 'Ximport.vim') 1407 assert_fails('source Ximport.vim', 'E1073:') 1408 1409 # try to import something that is already defined 1410 import_already_defined =<< trim END 1411 vim9script 1412 let exported = 'something' 1413 import * as exported from './Xexport.vim' 1414 END 1415 writefile(import_already_defined, 'Ximport.vim') 1416 assert_fails('source Ximport.vim', 'E1073:') 1417 1418 # try to import something that is already defined 1419 import_already_defined =<< trim END 1420 vim9script 1421 let exported = 'something' 1422 import {exported} from './Xexport.vim' 1423 END 1424 writefile(import_already_defined, 'Ximport.vim') 1425 assert_fails('source Ximport.vim', 'E1073:') 1426 1427 # import a very long name, requires making a copy 1428 let import_long_name_lines =<< trim END 1429 vim9script 1430 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim' 1431 END 1432 writefile(import_long_name_lines, 'Ximport.vim') 1433 assert_fails('source Ximport.vim', 'E1048:') 1434 1435 let import_no_from_lines =<< trim END 1436 vim9script 1437 import name './Xexport.vim' 1438 END 1439 writefile(import_no_from_lines, 'Ximport.vim') 1440 assert_fails('source Ximport.vim', 'E1070:') 1441 1442 let import_invalid_string_lines =<< trim END 1443 vim9script 1444 import name from Xexport.vim 1445 END 1446 writefile(import_invalid_string_lines, 'Ximport.vim') 1447 assert_fails('source Ximport.vim', 'E1071:') 1448 1449 let import_wrong_name_lines =<< trim END 1450 vim9script 1451 import name from './XnoExport.vim' 1452 END 1453 writefile(import_wrong_name_lines, 'Ximport.vim') 1454 assert_fails('source Ximport.vim', 'E1053:') 1455 1456 let import_missing_comma_lines =<< trim END 1457 vim9script 1458 import {exported name} from './Xexport.vim' 1459 END 1460 writefile(import_missing_comma_lines, 'Ximport3.vim') 1461 assert_fails('source Ximport3.vim', 'E1046:') 1462 1463 delete('Ximport.vim') 1464 delete('Ximport3.vim') 1465 delete('Xexport.vim') 1466 1467 # Check that in a Vim9 script 'cpo' is set to the Vim default. 1468 set cpo&vi 1469 let cpo_before = &cpo 1470 let lines =<< trim END 1471 vim9script 1472 g:cpo_in_vim9script = &cpo 1473 END 1474 writefile(lines, 'Xvim9_script') 1475 source Xvim9_script 1476 assert_equal(cpo_before, &cpo) 1477 set cpo&vim 1478 assert_equal(&cpo, g:cpo_in_vim9script) 1479 delete('Xvim9_script') 1480enddef 1481 1482func g:Trigger() 1483 source Ximport.vim 1484 return "echo 'yes'\<CR>" 1485endfunc 1486 1487def Test_import_export_expr_map() 1488 # check that :import and :export work when buffer is locked 1489 let export_lines =<< trim END 1490 vim9script 1491 export def That(): string 1492 return 'yes' 1493 enddef 1494 END 1495 writefile(export_lines, 'Xexport_that.vim') 1496 1497 let import_lines =<< trim END 1498 vim9script 1499 import That from './Xexport_that.vim' 1500 assert_equal('yes', That()) 1501 END 1502 writefile(import_lines, 'Ximport.vim') 1503 1504 nnoremap <expr> trigger g:Trigger() 1505 feedkeys('trigger', "xt") 1506 1507 delete('Xexport_that.vim') 1508 delete('Ximport.vim') 1509 nunmap trigger 1510enddef 1511 1512def Test_import_in_filetype() 1513 # check that :import works when the buffer is locked 1514 mkdir('ftplugin', 'p') 1515 let export_lines =<< trim END 1516 vim9script 1517 export let That = 'yes' 1518 END 1519 writefile(export_lines, 'ftplugin/Xexport_ft.vim') 1520 1521 let import_lines =<< trim END 1522 vim9script 1523 import That from './Xexport_ft.vim' 1524 assert_equal('yes', That) 1525 g:did_load_mytpe = 1 1526 END 1527 writefile(import_lines, 'ftplugin/qf.vim') 1528 1529 let save_rtp = &rtp 1530 &rtp = getcwd() .. ',' .. &rtp 1531 1532 filetype plugin on 1533 copen 1534 assert_equal(1, g:did_load_mytpe) 1535 1536 quit! 1537 delete('Xexport_ft.vim') 1538 delete('ftplugin', 'rf') 1539 &rtp = save_rtp 1540enddef 1541 1542def Test_use_import_in_mapping() 1543 let lines =<< trim END 1544 vim9script 1545 export def Funcx() 1546 g:result = 42 1547 enddef 1548 END 1549 writefile(lines, 'XsomeExport.vim') 1550 lines =<< trim END 1551 vim9script 1552 import Funcx from './XsomeExport.vim' 1553 nnoremap <F3> :call <sid>Funcx()<cr> 1554 END 1555 writefile(lines, 'Xmapscript.vim') 1556 1557 source Xmapscript.vim 1558 feedkeys("\<F3>", "xt") 1559 assert_equal(42, g:result) 1560 1561 unlet g:result 1562 delete('XsomeExport.vim') 1563 delete('Xmapscript.vim') 1564 nunmap <F3> 1565enddef 1566 1567def Test_vim9script_fails() 1568 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:') 1569 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:') 1570 CheckScriptFailure(['export let some = 123'], 'E1042:') 1571 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:') 1572 CheckScriptFailure(['vim9script', 'export let g:some'], 'E1022:') 1573 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:') 1574 1575 CheckScriptFailure(['vim9script', 'let str: string', 'str = 1234'], 'E1012:') 1576 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:') 1577 1578 assert_fails('vim9script', 'E1038') 1579 assert_fails('export something', 'E1043') 1580enddef 1581 1582func Test_import_fails_without_script() 1583 CheckRunVimInTerminal 1584 1585 " call indirectly to avoid compilation error for missing functions 1586 call Run_Test_import_fails_on_command_line() 1587endfunc 1588 1589def Run_Test_import_fails_on_command_line() 1590 let export =<< trim END 1591 vim9script 1592 export def Foo(): number 1593 return 0 1594 enddef 1595 END 1596 writefile(export, 'XexportCmd.vim') 1597 1598 let buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', #{ 1599 rows: 6, wait_for_ruler: 0}) 1600 WaitForAssert({-> assert_match('^E1094:', term_getline(buf, 5))}) 1601 1602 delete('XexportCmd.vim') 1603 StopVimInTerminal(buf) 1604enddef 1605 1606def Test_vim9script_reload_import() 1607 let lines =<< trim END 1608 vim9script 1609 const var = '' 1610 let valone = 1234 1611 def MyFunc(arg: string) 1612 valone = 5678 1613 enddef 1614 END 1615 let morelines =<< trim END 1616 let valtwo = 222 1617 export def GetValtwo(): number 1618 return valtwo 1619 enddef 1620 END 1621 writefile(lines + morelines, 'Xreload.vim') 1622 source Xreload.vim 1623 source Xreload.vim 1624 source Xreload.vim 1625 1626 let testlines =<< trim END 1627 vim9script 1628 def TheFunc() 1629 import GetValtwo from './Xreload.vim' 1630 assert_equal(222, GetValtwo()) 1631 enddef 1632 TheFunc() 1633 END 1634 writefile(testlines, 'Ximport.vim') 1635 source Ximport.vim 1636 1637 # Test that when not using "morelines" GetValtwo() and valtwo are still 1638 # defined, because import doesn't reload a script. 1639 writefile(lines, 'Xreload.vim') 1640 source Ximport.vim 1641 1642 # cannot declare a var twice 1643 lines =<< trim END 1644 vim9script 1645 let valone = 1234 1646 let valone = 5678 1647 END 1648 writefile(lines, 'Xreload.vim') 1649 assert_fails('source Xreload.vim', 'E1041:') 1650 1651 delete('Xreload.vim') 1652 delete('Ximport.vim') 1653enddef 1654 1655def s:RetSome(): string 1656 return 'some' 1657enddef 1658 1659" Not exported function that is referenced needs to be accessed by the 1660" script-local name. 1661def Test_vim9script_funcref() 1662 let sortlines =<< trim END 1663 vim9script 1664 def Compare(i1: number, i2: number): number 1665 return i2 - i1 1666 enddef 1667 1668 export def FastSort(): list<number> 1669 return range(5)->sort(Compare) 1670 enddef 1671 END 1672 writefile(sortlines, 'Xsort.vim') 1673 1674 let lines =<< trim END 1675 vim9script 1676 import FastSort from './Xsort.vim' 1677 def Test() 1678 g:result = FastSort() 1679 enddef 1680 Test() 1681 END 1682 writefile(lines, 'Xscript.vim') 1683 1684 source Xscript.vim 1685 assert_equal([4, 3, 2, 1, 0], g:result) 1686 1687 unlet g:result 1688 delete('Xsort.vim') 1689 delete('Xscript.vim') 1690 1691 let Funcref = function('s:RetSome') 1692 assert_equal('some', Funcref()) 1693enddef 1694 1695" Check that when searching for "FilterFunc" it finds the import in the 1696" script where FastFilter() is called from, both as a string and as a direct 1697" function reference. 1698def Test_vim9script_funcref_other_script() 1699 let filterLines =<< trim END 1700 vim9script 1701 export def FilterFunc(idx: number, val: number): bool 1702 return idx % 2 == 1 1703 enddef 1704 export def FastFilter(): list<number> 1705 return range(10)->filter('FilterFunc') 1706 enddef 1707 export def FastFilterDirect(): list<number> 1708 return range(10)->filter(FilterFunc) 1709 enddef 1710 END 1711 writefile(filterLines, 'Xfilter.vim') 1712 1713 let lines =<< trim END 1714 vim9script 1715 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim' 1716 def Test() 1717 let x: list<number> = FastFilter() 1718 enddef 1719 Test() 1720 def TestDirect() 1721 let x: list<number> = FastFilterDirect() 1722 enddef 1723 TestDirect() 1724 END 1725 CheckScriptSuccess(lines) 1726 delete('Xfilter.vim') 1727enddef 1728 1729def Test_vim9script_reload_delfunc() 1730 let first_lines =<< trim END 1731 vim9script 1732 def FuncYes(): string 1733 return 'yes' 1734 enddef 1735 END 1736 let withno_lines =<< trim END 1737 def FuncNo(): string 1738 return 'no' 1739 enddef 1740 def g:DoCheck(no_exists: bool) 1741 assert_equal('yes', FuncYes()) 1742 assert_equal('no', FuncNo()) 1743 enddef 1744 END 1745 let nono_lines =<< trim END 1746 def g:DoCheck(no_exists: bool) 1747 assert_equal('yes', FuncYes()) 1748 assert_fails('call FuncNo()', 'E117:') 1749 enddef 1750 END 1751 1752 # FuncNo() is defined 1753 writefile(first_lines + withno_lines, 'Xreloaded.vim') 1754 source Xreloaded.vim 1755 g:DoCheck(true) 1756 1757 # FuncNo() is not redefined 1758 writefile(first_lines + nono_lines, 'Xreloaded.vim') 1759 source Xreloaded.vim 1760 g:DoCheck() 1761 1762 # FuncNo() is back 1763 writefile(first_lines + withno_lines, 'Xreloaded.vim') 1764 source Xreloaded.vim 1765 g:DoCheck() 1766 1767 delete('Xreloaded.vim') 1768enddef 1769 1770def Test_vim9script_reload_delvar() 1771 # write the script with a script-local variable 1772 let lines =<< trim END 1773 vim9script 1774 let var = 'string' 1775 END 1776 writefile(lines, 'XreloadVar.vim') 1777 source XreloadVar.vim 1778 1779 # now write the script using the same variable locally - works 1780 lines =<< trim END 1781 vim9script 1782 def Func() 1783 let var = 'string' 1784 enddef 1785 END 1786 writefile(lines, 'XreloadVar.vim') 1787 source XreloadVar.vim 1788 1789 delete('XreloadVar.vim') 1790enddef 1791 1792def Test_import_absolute() 1793 let import_lines = [ 1794 'vim9script', 1795 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"', 1796 'def UseExported()', 1797 ' g:imported_abs = exported', 1798 ' exported = 8888', 1799 ' g:imported_after = exported', 1800 'enddef', 1801 'UseExported()', 1802 'g:import_disassembled = execute("disass UseExported")', 1803 ] 1804 writefile(import_lines, 'Ximport_abs.vim') 1805 writefile(s:export_script_lines, 'Xexport_abs.vim') 1806 1807 source Ximport_abs.vim 1808 1809 assert_equal(9876, g:imported_abs) 1810 assert_equal(8888, g:imported_after) 1811 assert_match('<SNR>\d\+_UseExported.*' .. 1812 'g:imported_abs = exported.*' .. 1813 '0 LOADSCRIPT exported from .*Xexport_abs.vim.*' .. 1814 '1 STOREG g:imported_abs.*' .. 1815 'exported = 8888.*' .. 1816 '3 STORESCRIPT exported in .*Xexport_abs.vim.*' .. 1817 'g:imported_after = exported.*' .. 1818 '4 LOADSCRIPT exported from .*Xexport_abs.vim.*' .. 1819 '5 STOREG g:imported_after.*', 1820 g:import_disassembled) 1821 1822 Undo_export_script_lines() 1823 unlet g:imported_abs 1824 unlet g:import_disassembled 1825 1826 delete('Ximport_abs.vim') 1827 delete('Xexport_abs.vim') 1828enddef 1829 1830def Test_import_rtp() 1831 let import_lines = [ 1832 'vim9script', 1833 'import exported from "Xexport_rtp.vim"', 1834 'g:imported_rtp = exported', 1835 ] 1836 writefile(import_lines, 'Ximport_rtp.vim') 1837 mkdir('import') 1838 writefile(s:export_script_lines, 'import/Xexport_rtp.vim') 1839 1840 let save_rtp = &rtp 1841 &rtp = getcwd() 1842 source Ximport_rtp.vim 1843 &rtp = save_rtp 1844 1845 assert_equal(9876, g:imported_rtp) 1846 1847 Undo_export_script_lines() 1848 unlet g:imported_rtp 1849 delete('Ximport_rtp.vim') 1850 delete('import', 'rf') 1851enddef 1852 1853def Test_import_compile_error() 1854 let export_lines = [ 1855 'vim9script', 1856 'export def ExpFunc(): string', 1857 ' return notDefined', 1858 'enddef', 1859 ] 1860 writefile(export_lines, 'Xexported.vim') 1861 1862 let import_lines = [ 1863 'vim9script', 1864 'import ExpFunc from "./Xexported.vim"', 1865 'def ImpFunc()', 1866 ' echo ExpFunc()', 1867 'enddef', 1868 'defcompile', 1869 ] 1870 writefile(import_lines, 'Ximport.vim') 1871 1872 try 1873 source Ximport.vim 1874 catch /E1001/ 1875 # Error should be fore the Xexported.vim file. 1876 assert_match('E1001: variable not found: notDefined', v:exception) 1877 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint) 1878 endtry 1879 1880 delete('Xexported.vim') 1881 delete('Ximport.vim') 1882enddef 1883 1884def Test_func_redefine_error() 1885 let lines = [ 1886 'vim9script', 1887 'def Func()', 1888 ' eval [][0]', 1889 'enddef', 1890 'Func()', 1891 ] 1892 writefile(lines, 'Xtestscript.vim') 1893 1894 for count in range(3) 1895 try 1896 source Xtestscript.vim 1897 catch /E684/ 1898 # function name should contain <SNR> every time 1899 assert_match('E684: list index out of range', v:exception) 1900 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint) 1901 endtry 1902 endfor 1903 1904 delete('Xtestscript.vim') 1905enddef 1906 1907def Test_func_overrules_import_fails() 1908 let export_lines =<< trim END 1909 vim9script 1910 export def Func() 1911 echo 'imported' 1912 enddef 1913 END 1914 writefile(export_lines, 'XexportedFunc.vim') 1915 1916 let lines =<< trim END 1917 vim9script 1918 import Func from './XexportedFunc.vim' 1919 def Func() 1920 echo 'local to function' 1921 enddef 1922 END 1923 CheckScriptFailure(lines, 'E1073:') 1924 1925 lines =<< trim END 1926 vim9script 1927 import Func from './XexportedFunc.vim' 1928 def Outer() 1929 def Func() 1930 echo 'local to function' 1931 enddef 1932 enddef 1933 defcompile 1934 END 1935 CheckScriptFailure(lines, 'E1073:') 1936 1937 delete('XexportedFunc.vim') 1938enddef 1939 1940def Test_func_redefine_fails() 1941 let lines =<< trim END 1942 vim9script 1943 def Func() 1944 echo 'one' 1945 enddef 1946 def Func() 1947 echo 'two' 1948 enddef 1949 END 1950 CheckScriptFailure(lines, 'E1073:') 1951 1952 lines =<< trim END 1953 vim9script 1954 def Foo(): string 1955 return 'foo' 1956 enddef 1957 def Func() 1958 let Foo = {-> 'lambda'} 1959 enddef 1960 defcompile 1961 END 1962 CheckScriptFailure(lines, 'E1073:') 1963enddef 1964 1965def Test_fixed_size_list() 1966 # will be allocated as one piece of memory, check that changes work 1967 let l = [1, 2, 3, 4] 1968 l->remove(0) 1969 l->add(5) 1970 l->insert(99, 1) 1971 assert_equal([2, 99, 3, 4, 5], l) 1972enddef 1973 1974def Test_no_insert_xit() 1975 call CheckDefExecFailure(['a = 1'], 'E1100:') 1976 call CheckDefExecFailure(['c = 1'], 'E1100:') 1977 call CheckDefExecFailure(['i = 1'], 'E1100:') 1978 call CheckDefExecFailure(['t = 1'], 'E1100:') 1979 call CheckDefExecFailure(['x = 1'], 'E1100:') 1980 1981 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:') 1982 CheckScriptFailure(['vim9script', 'a'], 'E1100:') 1983 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:') 1984 CheckScriptFailure(['vim9script', 'c'], 'E1100:') 1985 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:') 1986 CheckScriptFailure(['vim9script', 'i'], 'E1100:') 1987 CheckScriptFailure(['vim9script', 't'], 'E1100:') 1988 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:') 1989 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:') 1990enddef 1991 1992def IfElse(what: number): string 1993 let res = '' 1994 if what == 1 1995 res = "one" 1996 elseif what == 2 1997 res = "two" 1998 else 1999 res = "three" 2000 endif 2001 return res 2002enddef 2003 2004def Test_if_elseif_else() 2005 assert_equal('one', IfElse(1)) 2006 assert_equal('two', IfElse(2)) 2007 assert_equal('three', IfElse(3)) 2008enddef 2009 2010def Test_if_elseif_else_fails() 2011 call CheckDefFailure(['elseif true'], 'E582:') 2012 call CheckDefFailure(['else'], 'E581:') 2013 call CheckDefFailure(['endif'], 'E580:') 2014 call CheckDefFailure(['if true', 'elseif xxx'], 'E1001:') 2015 call CheckDefFailure(['if true', 'echo 1'], 'E171:') 2016enddef 2017 2018let g:bool_true = v:true 2019let g:bool_false = v:false 2020 2021def Test_if_const_expr() 2022 let res = false 2023 if true ? true : false 2024 res = true 2025 endif 2026 assert_equal(true, res) 2027 2028 g:glob = 2 2029 if false 2030 execute('g:glob = 3') 2031 endif 2032 assert_equal(2, g:glob) 2033 if true 2034 execute('g:glob = 3') 2035 endif 2036 assert_equal(3, g:glob) 2037 2038 res = false 2039 if g:bool_true ? true : false 2040 res = true 2041 endif 2042 assert_equal(true, res) 2043 2044 res = false 2045 if true ? g:bool_true : false 2046 res = true 2047 endif 2048 assert_equal(true, res) 2049 2050 res = false 2051 if true ? true : g:bool_false 2052 res = true 2053 endif 2054 assert_equal(true, res) 2055 2056 res = false 2057 if true ? false : true 2058 res = true 2059 endif 2060 assert_equal(false, res) 2061 2062 res = false 2063 if false ? false : true 2064 res = true 2065 endif 2066 assert_equal(true, res) 2067 2068 res = false 2069 if false ? true : false 2070 res = true 2071 endif 2072 assert_equal(false, res) 2073 2074 res = false 2075 if has('xyz') ? true : false 2076 res = true 2077 endif 2078 assert_equal(false, res) 2079 2080 res = false 2081 if true && true 2082 res = true 2083 endif 2084 assert_equal(true, res) 2085 2086 res = false 2087 if true && false 2088 res = true 2089 endif 2090 assert_equal(false, res) 2091 2092 res = false 2093 if g:bool_true && false 2094 res = true 2095 endif 2096 assert_equal(false, res) 2097 2098 res = false 2099 if true && g:bool_false 2100 res = true 2101 endif 2102 assert_equal(false, res) 2103 2104 res = false 2105 if false && false 2106 res = true 2107 endif 2108 assert_equal(false, res) 2109 2110 res = false 2111 if true || false 2112 res = true 2113 endif 2114 assert_equal(true, res) 2115 2116 res = false 2117 if g:bool_true || false 2118 res = true 2119 endif 2120 assert_equal(true, res) 2121 2122 res = false 2123 if true || g:bool_false 2124 res = true 2125 endif 2126 assert_equal(true, res) 2127 2128 res = false 2129 if false || false 2130 res = true 2131 endif 2132 assert_equal(false, res) 2133 2134 # with constant "false" expression may be invalid so long as the syntax is OK 2135 if false | eval 0 | endif 2136 if false | eval burp + 234 | endif 2137 if false | echo burp 234 'asd' | endif 2138 if false 2139 burp 2140 endif 2141enddef 2142 2143def Test_if_const_expr_fails() 2144 call CheckDefFailure(['if "aaa" == "bbb'], 'E114:') 2145 call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:') 2146 call CheckDefFailure(["if has('aaa'"], 'E110:') 2147 call CheckDefFailure(["if has('aaa') ? true false"], 'E109:') 2148enddef 2149 2150def RunNested(i: number): number 2151 let x: number = 0 2152 if i % 2 2153 if 1 2154 # comment 2155 else 2156 # comment 2157 endif 2158 x += 1 2159 else 2160 x += 1000 2161 endif 2162 return x 2163enddef 2164 2165def Test_nested_if() 2166 assert_equal(1, RunNested(1)) 2167 assert_equal(1000, RunNested(2)) 2168enddef 2169 2170def Test_execute_cmd() 2171 new 2172 setline(1, 'default') 2173 execute 'call setline(1, "execute-string")' 2174 assert_equal('execute-string', getline(1)) 2175 2176 execute "call setline(1, 'execute-string')" 2177 assert_equal('execute-string', getline(1)) 2178 2179 let cmd1 = 'call setline(1,' 2180 let cmd2 = '"execute-var")' 2181 execute cmd1 cmd2 # comment 2182 assert_equal('execute-var', getline(1)) 2183 2184 execute cmd1 cmd2 '|call setline(1, "execute-var-string")' 2185 assert_equal('execute-var-string', getline(1)) 2186 2187 let cmd_first = 'call ' 2188 let cmd_last = 'setline(1, "execute-var-var")' 2189 execute cmd_first .. cmd_last 2190 assert_equal('execute-var-var', getline(1)) 2191 bwipe! 2192 2193 let n = true 2194 execute 'echomsg' (n ? '"true"' : '"no"') 2195 assert_match('^true$', Screenline(&lines)) 2196 2197 echomsg [1, 2, 3] #{a: 1, b: 2} 2198 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines)) 2199 2200 call CheckDefFailure(['execute xxx'], 'E1001:', 1) 2201 call CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1) 2202 call CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1) 2203enddef 2204 2205def Test_execute_cmd_vimscript() 2206 # only checks line continuation 2207 let lines =<< trim END 2208 vim9script 2209 execute 'g:someVar' 2210 .. ' = ' .. 2211 '28' 2212 assert_equal(28, g:someVar) 2213 unlet g:someVar 2214 END 2215 CheckScriptSuccess(lines) 2216enddef 2217 2218def Test_echo_cmd() 2219 echo 'some' # comment 2220 echon 'thing' 2221 assert_match('^something$', Screenline(&lines)) 2222 2223 echo "some" # comment 2224 echon "thing" 2225 assert_match('^something$', Screenline(&lines)) 2226 2227 let str1 = 'some' 2228 let str2 = 'more' 2229 echo str1 str2 2230 assert_match('^some more$', Screenline(&lines)) 2231 2232 call CheckDefFailure(['echo "xxx"# comment'], 'E488:') 2233enddef 2234 2235def Test_echomsg_cmd() 2236 echomsg 'some' 'more' # comment 2237 assert_match('^some more$', Screenline(&lines)) 2238 echo 'clear' 2239 :1messages 2240 assert_match('^some more$', Screenline(&lines)) 2241 2242 call CheckDefFailure(['echomsg "xxx"# comment'], 'E488:') 2243enddef 2244 2245def Test_echomsg_cmd_vimscript() 2246 # only checks line continuation 2247 let lines =<< trim END 2248 vim9script 2249 echomsg 'here' 2250 .. ' is ' .. 2251 'a message' 2252 assert_match('^here is a message$', Screenline(&lines)) 2253 END 2254 CheckScriptSuccess(lines) 2255enddef 2256 2257def Test_echoerr_cmd() 2258 try 2259 echoerr 'something' 'wrong' # comment 2260 catch 2261 assert_match('something wrong', v:exception) 2262 endtry 2263enddef 2264 2265def Test_echoerr_cmd_vimscript() 2266 # only checks line continuation 2267 let lines =<< trim END 2268 vim9script 2269 try 2270 echoerr 'this' 2271 .. ' is ' .. 2272 'wrong' 2273 catch 2274 assert_match('this is wrong', v:exception) 2275 endtry 2276 END 2277 CheckScriptSuccess(lines) 2278enddef 2279 2280def Test_for_outside_of_function() 2281 let lines =<< trim END 2282 vim9script 2283 new 2284 for var in range(0, 3) 2285 append(line('$'), var) 2286 endfor 2287 assert_equal(['', '0', '1', '2', '3'], getline(1, '$')) 2288 bwipe! 2289 END 2290 writefile(lines, 'Xvim9for.vim') 2291 source Xvim9for.vim 2292 delete('Xvim9for.vim') 2293enddef 2294 2295def Test_for_loop() 2296 let result = '' 2297 for cnt in range(7) 2298 if cnt == 4 2299 break 2300 endif 2301 if cnt == 2 2302 continue 2303 endif 2304 result ..= cnt .. '_' 2305 endfor 2306 assert_equal('0_1_3_', result) 2307 2308 let concat = '' 2309 for str in eval('["one", "two"]') 2310 concat ..= str 2311 endfor 2312 assert_equal('onetwo', concat) 2313enddef 2314 2315def Test_for_loop_fails() 2316 CheckDefFailure(['for # in range(5)'], 'E690:') 2317 CheckDefFailure(['for i In range(5)'], 'E690:') 2318 CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1017:') 2319 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:') 2320 CheckDefFailure(['for i in "text"'], 'E1012:') 2321 CheckDefFailure(['for i in xxx'], 'E1001:') 2322 CheckDefFailure(['endfor'], 'E588:') 2323 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:') 2324enddef 2325 2326def Test_while_loop() 2327 let result = '' 2328 let cnt = 0 2329 while cnt < 555 2330 if cnt == 3 2331 break 2332 endif 2333 cnt += 1 2334 if cnt == 2 2335 continue 2336 endif 2337 result ..= cnt .. '_' 2338 endwhile 2339 assert_equal('1_3_', result) 2340enddef 2341 2342def Test_while_loop_fails() 2343 CheckDefFailure(['while xxx'], 'E1001:') 2344 CheckDefFailure(['endwhile'], 'E588:') 2345 CheckDefFailure(['continue'], 'E586:') 2346 CheckDefFailure(['if true', 'continue'], 'E586:') 2347 CheckDefFailure(['break'], 'E587:') 2348 CheckDefFailure(['if true', 'break'], 'E587:') 2349 CheckDefFailure(['while 1', 'echo 3'], 'E170:') 2350enddef 2351 2352def Test_interrupt_loop() 2353 let caught = false 2354 let x = 0 2355 try 2356 while 1 2357 x += 1 2358 if x == 100 2359 feedkeys("\<C-C>", 'Lt') 2360 endif 2361 endwhile 2362 catch 2363 caught = true 2364 assert_equal(100, x) 2365 endtry 2366 assert_true(caught, 'should have caught an exception') 2367 # consume the CTRL-C 2368 getchar(0) 2369enddef 2370 2371def Test_automatic_line_continuation() 2372 let mylist = [ 2373 'one', 2374 'two', 2375 'three', 2376 ] # comment 2377 assert_equal(['one', 'two', 'three'], mylist) 2378 2379 let mydict = { 2380 'one': 1, 2381 'two': 2, 2382 'three': 2383 3, 2384 } # comment 2385 assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict) 2386 mydict = #{ 2387 one: 1, # comment 2388 two: # comment 2389 2, # comment 2390 three: 3 # comment 2391 } 2392 assert_equal(#{one: 1, two: 2, three: 3}, mydict) 2393 mydict = #{ 2394 one: 1, 2395 two: 2396 2, 2397 three: 3 2398 } 2399 assert_equal(#{one: 1, two: 2, three: 3}, mydict) 2400 2401 assert_equal( 2402 ['one', 'two', 'three'], 2403 split('one two three') 2404 ) 2405enddef 2406 2407def Test_vim9_comment() 2408 CheckScriptSuccess([ 2409 'vim9script', 2410 '# something', 2411 ]) 2412 CheckScriptFailure([ 2413 'vim9script', 2414 ':# something', 2415 ], 'E488:') 2416 CheckScriptFailure([ 2417 '# something', 2418 ], 'E488:') 2419 CheckScriptFailure([ 2420 ':# something', 2421 ], 'E488:') 2422 2423 { # block start 2424 } # block end 2425 CheckDefFailure([ 2426 '{# comment', 2427 ], 'E488:') 2428 CheckDefFailure([ 2429 '{', 2430 '}# comment', 2431 ], 'E488:') 2432 2433 echo "yes" # comment 2434 CheckDefFailure([ 2435 'echo "yes"# comment', 2436 ], 'E488:') 2437 CheckScriptSuccess([ 2438 'vim9script', 2439 'echo "yes" # something', 2440 ]) 2441 CheckScriptFailure([ 2442 'vim9script', 2443 'echo "yes"# something', 2444 ], 'E121:') 2445 CheckScriptFailure([ 2446 'vim9script', 2447 'echo# something', 2448 ], 'E121:') 2449 CheckScriptFailure([ 2450 'echo "yes" # something', 2451 ], 'E121:') 2452 2453 exe "echo" # comment 2454 CheckDefFailure([ 2455 'exe "echo"# comment', 2456 ], 'E488:') 2457 CheckScriptSuccess([ 2458 'vim9script', 2459 'exe "echo" # something', 2460 ]) 2461 CheckScriptFailure([ 2462 'vim9script', 2463 'exe "echo"# something', 2464 ], 'E121:') 2465 CheckDefFailure([ 2466 'exe # comment', 2467 ], 'E1015:') 2468 CheckScriptFailure([ 2469 'vim9script', 2470 'exe# something', 2471 ], 'E121:') 2472 CheckScriptFailure([ 2473 'exe "echo" # something', 2474 ], 'E121:') 2475 2476 CheckDefFailure([ 2477 'try# comment', 2478 ' echo "yes"', 2479 'catch', 2480 'endtry', 2481 ], 'E488:') 2482 CheckScriptFailure([ 2483 'vim9script', 2484 'try# comment', 2485 'echo "yes"', 2486 ], 'E488:') 2487 CheckDefFailure([ 2488 'try', 2489 ' throw#comment', 2490 'catch', 2491 'endtry', 2492 ], 'E1015:') 2493 CheckDefFailure([ 2494 'try', 2495 ' throw "yes"#comment', 2496 'catch', 2497 'endtry', 2498 ], 'E488:') 2499 CheckDefFailure([ 2500 'try', 2501 ' echo "yes"', 2502 'catch# comment', 2503 'endtry', 2504 ], 'E488:') 2505 CheckScriptFailure([ 2506 'vim9script', 2507 'try', 2508 ' echo "yes"', 2509 'catch# comment', 2510 'endtry', 2511 ], 'E654:') 2512 CheckDefFailure([ 2513 'try', 2514 ' echo "yes"', 2515 'catch /pat/# comment', 2516 'endtry', 2517 ], 'E488:') 2518 CheckDefFailure([ 2519 'try', 2520 'echo "yes"', 2521 'catch', 2522 'endtry# comment', 2523 ], 'E488:') 2524 CheckScriptFailure([ 2525 'vim9script', 2526 'try', 2527 ' echo "yes"', 2528 'catch', 2529 'endtry# comment', 2530 ], 'E488:') 2531 2532 CheckScriptSuccess([ 2533 'vim9script', 2534 'hi # comment', 2535 ]) 2536 CheckScriptFailure([ 2537 'vim9script', 2538 'hi# comment', 2539 ], 'E416:') 2540 CheckScriptSuccess([ 2541 'vim9script', 2542 'hi Search # comment', 2543 ]) 2544 CheckScriptFailure([ 2545 'vim9script', 2546 'hi Search# comment', 2547 ], 'E416:') 2548 CheckScriptSuccess([ 2549 'vim9script', 2550 'hi link This Search # comment', 2551 ]) 2552 CheckScriptFailure([ 2553 'vim9script', 2554 'hi link This That# comment', 2555 ], 'E413:') 2556 CheckScriptSuccess([ 2557 'vim9script', 2558 'hi clear This # comment', 2559 'hi clear # comment', 2560 ]) 2561 # not tested, because it doesn't give an error but a warning: 2562 # hi clear This# comment', 2563 CheckScriptFailure([ 2564 'vim9script', 2565 'hi clear# comment', 2566 ], 'E416:') 2567 2568 CheckScriptSuccess([ 2569 'vim9script', 2570 'hi Group term=bold', 2571 'match Group /todo/ # comment', 2572 ]) 2573 CheckScriptFailure([ 2574 'vim9script', 2575 'hi Group term=bold', 2576 'match Group /todo/# comment', 2577 ], 'E488:') 2578 CheckScriptSuccess([ 2579 'vim9script', 2580 'match # comment', 2581 ]) 2582 CheckScriptFailure([ 2583 'vim9script', 2584 'match# comment', 2585 ], 'E475:') 2586 CheckScriptSuccess([ 2587 'vim9script', 2588 'match none # comment', 2589 ]) 2590 CheckScriptFailure([ 2591 'vim9script', 2592 'match none# comment', 2593 ], 'E475:') 2594 2595 CheckScriptSuccess([ 2596 'vim9script', 2597 'menutrans clear # comment', 2598 ]) 2599 CheckScriptFailure([ 2600 'vim9script', 2601 'menutrans clear# comment text', 2602 ], 'E474:') 2603 2604 CheckScriptSuccess([ 2605 'vim9script', 2606 'syntax clear # comment', 2607 ]) 2608 CheckScriptFailure([ 2609 'vim9script', 2610 'syntax clear# comment text', 2611 ], 'E28:') 2612 CheckScriptSuccess([ 2613 'vim9script', 2614 'syntax keyword Word some', 2615 'syntax clear Word # comment', 2616 ]) 2617 CheckScriptFailure([ 2618 'vim9script', 2619 'syntax keyword Word some', 2620 'syntax clear Word# comment text', 2621 ], 'E28:') 2622 2623 CheckScriptSuccess([ 2624 'vim9script', 2625 'syntax list # comment', 2626 ]) 2627 CheckScriptFailure([ 2628 'vim9script', 2629 'syntax list# comment text', 2630 ], 'E28:') 2631 2632 CheckScriptSuccess([ 2633 'vim9script', 2634 'syntax match Word /pat/ oneline # comment', 2635 ]) 2636 CheckScriptFailure([ 2637 'vim9script', 2638 'syntax match Word /pat/ oneline# comment', 2639 ], 'E475:') 2640 2641 CheckScriptSuccess([ 2642 'vim9script', 2643 'syntax keyword Word word # comm[ent', 2644 ]) 2645 CheckScriptFailure([ 2646 'vim9script', 2647 'syntax keyword Word word# comm[ent', 2648 ], 'E789:') 2649 2650 CheckScriptSuccess([ 2651 'vim9script', 2652 'syntax match Word /pat/ # comment', 2653 ]) 2654 CheckScriptFailure([ 2655 'vim9script', 2656 'syntax match Word /pat/# comment', 2657 ], 'E402:') 2658 2659 CheckScriptSuccess([ 2660 'vim9script', 2661 'syntax match Word /pat/ contains=Something # comment', 2662 ]) 2663 CheckScriptFailure([ 2664 'vim9script', 2665 'syntax match Word /pat/ contains=Something# comment', 2666 ], 'E475:') 2667 CheckScriptFailure([ 2668 'vim9script', 2669 'syntax match Word /pat/ contains= # comment', 2670 ], 'E406:') 2671 CheckScriptFailure([ 2672 'vim9script', 2673 'syntax match Word /pat/ contains=# comment', 2674 ], 'E475:') 2675 2676 CheckScriptSuccess([ 2677 'vim9script', 2678 'syntax region Word start=/pat/ end=/pat/ # comment', 2679 ]) 2680 CheckScriptFailure([ 2681 'vim9script', 2682 'syntax region Word start=/pat/ end=/pat/# comment', 2683 ], 'E402:') 2684 2685 CheckScriptSuccess([ 2686 'vim9script', 2687 'syntax sync # comment', 2688 ]) 2689 CheckScriptFailure([ 2690 'vim9script', 2691 'syntax sync# comment', 2692 ], 'E404:') 2693 CheckScriptSuccess([ 2694 'vim9script', 2695 'syntax sync ccomment # comment', 2696 ]) 2697 CheckScriptFailure([ 2698 'vim9script', 2699 'syntax sync ccomment# comment', 2700 ], 'E404:') 2701 2702 CheckScriptSuccess([ 2703 'vim9script', 2704 'syntax cluster Some contains=Word # comment', 2705 ]) 2706 CheckScriptFailure([ 2707 'vim9script', 2708 'syntax cluster Some contains=Word# comment', 2709 ], 'E475:') 2710 2711 CheckScriptSuccess([ 2712 'vim9script', 2713 'command Echo echo # comment', 2714 'command Echo # comment', 2715 ]) 2716 CheckScriptFailure([ 2717 'vim9script', 2718 'command Echo echo# comment', 2719 'Echo', 2720 ], 'E121:') 2721 CheckScriptFailure([ 2722 'vim9script', 2723 'command Echo# comment', 2724 ], 'E182:') 2725 CheckScriptFailure([ 2726 'vim9script', 2727 'command Echo echo', 2728 'command Echo# comment', 2729 ], 'E182:') 2730 2731 CheckScriptSuccess([ 2732 'vim9script', 2733 'function # comment', 2734 ]) 2735 CheckScriptFailure([ 2736 'vim9script', 2737 'function " comment', 2738 ], 'E129:') 2739 CheckScriptFailure([ 2740 'vim9script', 2741 'function# comment', 2742 ], 'E129:') 2743 CheckScriptSuccess([ 2744 'vim9script', 2745 'function CheckScriptSuccess # comment', 2746 ]) 2747 CheckScriptFailure([ 2748 'vim9script', 2749 'function CheckScriptSuccess# comment', 2750 ], 'E488:') 2751 2752 CheckScriptSuccess([ 2753 'vim9script', 2754 'func g:DeleteMeA()', 2755 'endfunc', 2756 'delfunction g:DeleteMeA # comment', 2757 ]) 2758 CheckScriptFailure([ 2759 'vim9script', 2760 'func g:DeleteMeB()', 2761 'endfunc', 2762 'delfunction g:DeleteMeB# comment', 2763 ], 'E488:') 2764 2765 CheckScriptSuccess([ 2766 'vim9script', 2767 'call execute("ls") # comment', 2768 ]) 2769 CheckScriptFailure([ 2770 'vim9script', 2771 'call execute("ls")# comment', 2772 ], 'E488:') 2773 2774 CheckScriptFailure([ 2775 'def Test() " comment', 2776 'enddef', 2777 ], 'E488:') 2778 CheckScriptFailure([ 2779 'vim9script', 2780 'def Test() " comment', 2781 'enddef', 2782 ], 'E488:') 2783 2784 CheckScriptSuccess([ 2785 'func Test() " comment', 2786 'endfunc', 2787 ]) 2788 CheckScriptSuccess([ 2789 'vim9script', 2790 'func Test() " comment', 2791 'endfunc', 2792 ]) 2793 2794 CheckScriptSuccess([ 2795 'def Test() # comment', 2796 'enddef', 2797 ]) 2798 CheckScriptFailure([ 2799 'func Test() # comment', 2800 'endfunc', 2801 ], 'E488:') 2802enddef 2803 2804def Test_vim9_comment_gui() 2805 CheckCanRunGui 2806 2807 CheckScriptFailure([ 2808 'vim9script', 2809 'gui#comment' 2810 ], 'E499:') 2811 CheckScriptFailure([ 2812 'vim9script', 2813 'gui -f#comment' 2814 ], 'E499:') 2815enddef 2816 2817def Test_vim9_comment_not_compiled() 2818 au TabEnter *.vim g:entered = 1 2819 au TabEnter *.x g:entered = 2 2820 2821 edit test.vim 2822 doautocmd TabEnter #comment 2823 assert_equal(1, g:entered) 2824 2825 doautocmd TabEnter f.x 2826 assert_equal(2, g:entered) 2827 2828 g:entered = 0 2829 doautocmd TabEnter f.x #comment 2830 assert_equal(2, g:entered) 2831 2832 assert_fails('doautocmd Syntax#comment', 'E216:') 2833 2834 au! TabEnter 2835 unlet g:entered 2836 2837 CheckScriptSuccess([ 2838 'vim9script', 2839 'g:var = 123', 2840 'b:var = 456', 2841 'w:var = 777', 2842 't:var = 888', 2843 'unlet g:var w:var # something', 2844 ]) 2845 2846 CheckScriptFailure([ 2847 'vim9script', 2848 'let g:var = 123', 2849 ], 'E1016: Cannot declare a global variable:') 2850 2851 CheckScriptFailure([ 2852 'vim9script', 2853 'let b:var = 123', 2854 ], 'E1016: Cannot declare a buffer variable:') 2855 2856 CheckScriptFailure([ 2857 'vim9script', 2858 'let w:var = 123', 2859 ], 'E1016: Cannot declare a window variable:') 2860 2861 CheckScriptFailure([ 2862 'vim9script', 2863 'let t:var = 123', 2864 ], 'E1016: Cannot declare a tab variable:') 2865 2866 CheckScriptFailure([ 2867 'vim9script', 2868 'let v:version = 123', 2869 ], 'E1016: Cannot declare a v: variable:') 2870 2871 CheckScriptFailure([ 2872 'vim9script', 2873 'let $VARIABLE = "text"', 2874 ], 'E1016: Cannot declare an environment variable:') 2875 2876 CheckScriptFailure([ 2877 'vim9script', 2878 'g:var = 123', 2879 'unlet g:var# comment1', 2880 ], 'E108:') 2881 2882 CheckScriptFailure([ 2883 'let g:var = 123', 2884 'unlet g:var # something', 2885 ], 'E488:') 2886 2887 CheckScriptSuccess([ 2888 'vim9script', 2889 'if 1 # comment2', 2890 ' echo "yes"', 2891 'elseif 2 #comment', 2892 ' echo "no"', 2893 'endif', 2894 ]) 2895 2896 CheckScriptFailure([ 2897 'vim9script', 2898 'if 1# comment3', 2899 ' echo "yes"', 2900 'endif', 2901 ], 'E15:') 2902 2903 CheckScriptFailure([ 2904 'vim9script', 2905 'if 0 # comment4', 2906 ' echo "yes"', 2907 'elseif 2#comment', 2908 ' echo "no"', 2909 'endif', 2910 ], 'E15:') 2911 2912 CheckScriptSuccess([ 2913 'vim9script', 2914 'let v = 1 # comment5', 2915 ]) 2916 2917 CheckScriptFailure([ 2918 'vim9script', 2919 'let v = 1# comment6', 2920 ], 'E15:') 2921 2922 CheckScriptSuccess([ 2923 'vim9script', 2924 'new' 2925 'call setline(1, ["# define pat", "last"])', 2926 ':$', 2927 'dsearch /pat/ #comment', 2928 'bwipe!', 2929 ]) 2930 2931 CheckScriptFailure([ 2932 'vim9script', 2933 'new' 2934 'call setline(1, ["# define pat", "last"])', 2935 ':$', 2936 'dsearch /pat/#comment', 2937 'bwipe!', 2938 ], 'E488:') 2939 2940 CheckScriptFailure([ 2941 'vim9script', 2942 'func! SomeFunc()', 2943 ], 'E477:') 2944enddef 2945 2946def Test_finish() 2947 let lines =<< trim END 2948 vim9script 2949 g:res = 'one' 2950 if v:false | finish | endif 2951 g:res = 'two' 2952 finish 2953 g:res = 'three' 2954 END 2955 writefile(lines, 'Xfinished') 2956 source Xfinished 2957 assert_equal('two', g:res) 2958 2959 unlet g:res 2960 delete('Xfinished') 2961enddef 2962 2963def Test_let_func_call() 2964 let lines =<< trim END 2965 vim9script 2966 func GetValue() 2967 if exists('g:count') 2968 let g:count += 1 2969 else 2970 let g:count = 1 2971 endif 2972 return 'this' 2973 endfunc 2974 let val: string = GetValue() 2975 # env var is always a string 2976 let env = $TERM 2977 END 2978 writefile(lines, 'Xfinished') 2979 source Xfinished 2980 # GetValue() is not called during discovery phase 2981 assert_equal(1, g:count) 2982 2983 unlet g:count 2984 delete('Xfinished') 2985enddef 2986 2987def Test_let_missing_type() 2988 let lines =<< trim END 2989 vim9script 2990 let var = g:unknown 2991 END 2992 CheckScriptFailure(lines, 'E121:') 2993 2994 lines =<< trim END 2995 vim9script 2996 let nr: number = 123 2997 let var = nr 2998 END 2999 CheckScriptSuccess(lines) 3000enddef 3001 3002def Test_let_declaration() 3003 let lines =<< trim END 3004 vim9script 3005 let var: string 3006 g:var_uninit = var 3007 var = 'text' 3008 g:var_test = var 3009 # prefixing s: is optional 3010 s:var = 'prefixed' 3011 g:var_prefixed = s:var 3012 3013 let s:other: number 3014 other = 1234 3015 g:other_var = other 3016 3017 # type is inferred 3018 s:dict = {'a': 222} 3019 def GetDictVal(key: any) 3020 g:dict_val = s:dict[key] 3021 enddef 3022 GetDictVal('a') 3023 END 3024 CheckScriptSuccess(lines) 3025 assert_equal('', g:var_uninit) 3026 assert_equal('text', g:var_test) 3027 assert_equal('prefixed', g:var_prefixed) 3028 assert_equal(1234, g:other_var) 3029 assert_equal(222, g:dict_val) 3030 3031 unlet g:var_uninit 3032 unlet g:var_test 3033 unlet g:var_prefixed 3034 unlet g:other_var 3035enddef 3036 3037def Test_let_declaration_fails() 3038 let lines =<< trim END 3039 vim9script 3040 const var: string 3041 END 3042 CheckScriptFailure(lines, 'E1021:') 3043 3044 lines =<< trim END 3045 vim9script 3046 let 9var: string 3047 END 3048 CheckScriptFailure(lines, 'E475:') 3049enddef 3050 3051def Test_let_type_check() 3052 let lines =<< trim END 3053 vim9script 3054 let var: string 3055 var = 1234 3056 END 3057 CheckScriptFailure(lines, 'E1012:') 3058 3059 lines =<< trim END 3060 vim9script 3061 let var:string 3062 END 3063 CheckScriptFailure(lines, 'E1069:') 3064 3065 lines =<< trim END 3066 vim9script 3067 let var: asdf 3068 END 3069 CheckScriptFailure(lines, 'E1010:') 3070 3071 lines =<< trim END 3072 vim9script 3073 let s:l: list<number> 3074 s:l = [] 3075 END 3076 CheckScriptSuccess(lines) 3077 3078 lines =<< trim END 3079 vim9script 3080 let s:d: dict<number> 3081 s:d = {} 3082 END 3083 CheckScriptSuccess(lines) 3084enddef 3085 3086def Test_forward_declaration() 3087 let lines =<< trim END 3088 vim9script 3089 def GetValue(): string 3090 return theVal 3091 enddef 3092 let theVal = 'something' 3093 g:initVal = GetValue() 3094 theVal = 'else' 3095 g:laterVal = GetValue() 3096 END 3097 writefile(lines, 'Xforward') 3098 source Xforward 3099 assert_equal('something', g:initVal) 3100 assert_equal('else', g:laterVal) 3101 3102 unlet g:initVal 3103 unlet g:laterVal 3104 delete('Xforward') 3105enddef 3106 3107def Test_source_vim9_from_legacy() 3108 let legacy_lines =<< trim END 3109 source Xvim9_script.vim 3110 3111 call assert_false(exists('local')) 3112 call assert_false(exists('exported')) 3113 call assert_false(exists('s:exported')) 3114 call assert_equal('global', global) 3115 call assert_equal('global', g:global) 3116 3117 " imported variable becomes script-local 3118 import exported from './Xvim9_script.vim' 3119 call assert_equal('exported', s:exported) 3120 call assert_false(exists('exported')) 3121 3122 " imported function becomes script-local 3123 import GetText from './Xvim9_script.vim' 3124 call assert_equal('text', s:GetText()) 3125 call assert_false(exists('*GetText')) 3126 END 3127 writefile(legacy_lines, 'Xlegacy_script.vim') 3128 3129 let vim9_lines =<< trim END 3130 vim9script 3131 let local = 'local' 3132 g:global = 'global' 3133 export let exported = 'exported' 3134 export def GetText(): string 3135 return 'text' 3136 enddef 3137 END 3138 writefile(vim9_lines, 'Xvim9_script.vim') 3139 3140 source Xlegacy_script.vim 3141 3142 assert_equal('global', g:global) 3143 unlet g:global 3144 3145 delete('Xlegacy_script.vim') 3146 delete('Xvim9_script.vim') 3147enddef 3148 3149func Test_vim9script_not_global() 3150 " check that items defined in Vim9 script are script-local, not global 3151 let vim9lines =<< trim END 3152 vim9script 3153 let var = 'local' 3154 func TheFunc() 3155 echo 'local' 3156 endfunc 3157 def DefFunc() 3158 echo 'local' 3159 enddef 3160 END 3161 call writefile(vim9lines, 'Xvim9script.vim') 3162 source Xvim9script.vim 3163 try 3164 echo g:var 3165 assert_report('did not fail') 3166 catch /E121:/ 3167 " caught 3168 endtry 3169 try 3170 call TheFunc() 3171 assert_report('did not fail') 3172 catch /E117:/ 3173 " caught 3174 endtry 3175 try 3176 call DefFunc() 3177 assert_report('did not fail') 3178 catch /E117:/ 3179 " caught 3180 endtry 3181 3182 call delete('Xvim9script.vim') 3183endfunc 3184 3185def Test_vim9_copen() 3186 # this was giving an error for setting w:quickfix_title 3187 copen 3188 quit 3189enddef 3190 3191" test using a vim9script that is auto-loaded from an autocmd 3192def Test_vim9_autoload() 3193 let lines =<< trim END 3194 vim9script 3195 def foo#test() 3196 echomsg getreg('"') 3197 enddef 3198 END 3199 3200 mkdir('Xdir/autoload', 'p') 3201 writefile(lines, 'Xdir/autoload/foo.vim') 3202 let save_rtp = &rtp 3203 exe 'set rtp^=' .. getcwd() .. '/Xdir' 3204 augroup test 3205 autocmd TextYankPost * call foo#test() 3206 augroup END 3207 3208 normal Y 3209 3210 augroup test 3211 autocmd! 3212 augroup END 3213 delete('Xdir', 'rf') 3214 &rtp = save_rtp 3215enddef 3216 3217def Test_script_var_in_autocmd() 3218 # using a script variable from an autocommand, defined in a :def function in a 3219 # legacy Vim script, cannot check the variable type. 3220 let lines =<< trim END 3221 let s:counter = 1 3222 def s:Func() 3223 au! CursorHold 3224 au CursorHold * s:counter += 1 3225 enddef 3226 call s:Func() 3227 doau CursorHold 3228 call assert_equal(2, s:counter) 3229 au! CursorHold 3230 END 3231 CheckScriptSuccess(lines) 3232enddef 3233 3234def Test_cmdline_win() 3235 # if the Vim syntax highlighting uses Vim9 constructs they can be used from 3236 # the command line window. 3237 mkdir('rtp/syntax', 'p') 3238 let export_lines =<< trim END 3239 vim9script 3240 export let That = 'yes' 3241 END 3242 writefile(export_lines, 'rtp/syntax/Xexport.vim') 3243 let import_lines =<< trim END 3244 vim9script 3245 import That from './Xexport.vim' 3246 END 3247 writefile(import_lines, 'rtp/syntax/vim.vim') 3248 let save_rtp = &rtp 3249 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp 3250 syntax on 3251 augroup CmdWin 3252 autocmd CmdwinEnter * g:got_there = 'yes' 3253 augroup END 3254 # this will open and also close the cmdline window 3255 feedkeys('q:', 'xt') 3256 assert_equal('yes', g:got_there) 3257 3258 augroup CmdWin 3259 au! 3260 augroup END 3261 &rtp = save_rtp 3262 delete('rtp', 'rf') 3263enddef 3264 3265def Test_invalid_sid() 3266 assert_fails('func <SNR>1234_func', 'E123:') 3267 3268 if RunVim([], ['wq Xdidit'], '+"func <SNR>1_func"') 3269 call assert_equal([], readfile('Xdidit')) 3270 endif 3271 delete('Xdidit') 3272enddef 3273 3274" Keep this last, it messes up highlighting. 3275def Test_substitute_cmd() 3276 new 3277 setline(1, 'something') 3278 :substitute(some(other( 3279 assert_equal('otherthing', getline(1)) 3280 bwipe! 3281 3282 # also when the context is Vim9 script 3283 let lines =<< trim END 3284 vim9script 3285 new 3286 setline(1, 'something') 3287 :substitute(some(other( 3288 assert_equal('otherthing', getline(1)) 3289 bwipe! 3290 END 3291 writefile(lines, 'Xvim9lines') 3292 source Xvim9lines 3293 3294 delete('Xvim9lines') 3295enddef 3296 3297" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 3298