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 8source screendump.vim 9 10def Test_range_only() 11 new 12 setline(1, ['blah', 'Blah']) 13 :/Blah/ 14 assert_equal(2, getcurpos()[1]) 15 bwipe! 16 17 # without range commands use current line 18 new 19 setline(1, ['one', 'two', 'three']) 20 :2 21 print 22 assert_equal('two', Screenline(&lines)) 23 :3 24 list 25 assert_equal('three$', Screenline(&lines)) 26 27 # missing command does not print the line 28 var lines =<< trim END 29 vim9script 30 :1| 31 assert_equal('three$', Screenline(&lines)) 32 :| 33 assert_equal('three$', Screenline(&lines)) 34 END 35 CheckScriptSuccess(lines) 36 37 bwipe! 38 39 # won't generate anything 40 if false 41 :123 42 endif 43enddef 44 45let g:alist = [7] 46let g:astring = 'text' 47let g:anumber = 123 48 49def Test_delfunction() 50 # Check function is defined in script namespace 51 CheckScriptSuccess([ 52 'vim9script', 53 'func CheckMe()', 54 ' return 123', 55 'endfunc', 56 'assert_equal(123, s:CheckMe())', 57 ]) 58 59 # Check function in script namespace cannot be deleted 60 CheckScriptFailure([ 61 'vim9script', 62 'func DeleteMe1()', 63 'endfunc', 64 'delfunction DeleteMe1', 65 ], 'E1084:') 66 CheckScriptFailure([ 67 'vim9script', 68 'func DeleteMe2()', 69 'endfunc', 70 'def DoThat()', 71 ' delfunction DeleteMe2', 72 'enddef', 73 'DoThat()', 74 ], 'E1084:') 75 CheckScriptFailure([ 76 'vim9script', 77 'def DeleteMe3()', 78 'enddef', 79 'delfunction DeleteMe3', 80 ], 'E1084:') 81 CheckScriptFailure([ 82 'vim9script', 83 'def DeleteMe4()', 84 'enddef', 85 'def DoThat()', 86 ' delfunction DeleteMe4', 87 'enddef', 88 'DoThat()', 89 ], 'E1084:') 90 91 # Check that global :def function can be replaced and deleted 92 var lines =<< trim END 93 vim9script 94 def g:Global(): string 95 return "yes" 96 enddef 97 assert_equal("yes", g:Global()) 98 def! g:Global(): string 99 return "no" 100 enddef 101 assert_equal("no", g:Global()) 102 delfunc g:Global 103 assert_false(exists('*g:Global')) 104 END 105 CheckScriptSuccess(lines) 106 107 # Check that global function can be replaced by a :def function and deleted 108 lines =<< trim END 109 vim9script 110 func g:Global() 111 return "yes" 112 endfunc 113 assert_equal("yes", g:Global()) 114 def! g:Global(): string 115 return "no" 116 enddef 117 assert_equal("no", g:Global()) 118 delfunc g:Global 119 assert_false(exists('*g:Global')) 120 END 121 CheckScriptSuccess(lines) 122 123 # Check that global :def function can be replaced by a function and deleted 124 lines =<< trim END 125 vim9script 126 def g:Global(): string 127 return "yes" 128 enddef 129 assert_equal("yes", g:Global()) 130 func! g:Global() 131 return "no" 132 endfunc 133 assert_equal("no", g:Global()) 134 delfunc g:Global 135 assert_false(exists('*g:Global')) 136 END 137 CheckScriptSuccess(lines) 138enddef 139 140def Test_wrong_type() 141 CheckDefFailure(['var name: list<nothing>'], 'E1010:') 142 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:') 143 CheckDefFailure(['var name: dict<nothing>'], 'E1010:') 144 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:') 145 146 CheckDefFailure(['var name: dict<number'], 'E1009:') 147 CheckDefFailure(['var name: dict<list<number>'], 'E1009:') 148 149 CheckDefFailure(['var name: ally'], 'E1010:') 150 CheckDefFailure(['var name: bram'], 'E1010:') 151 CheckDefFailure(['var name: cathy'], 'E1010:') 152 CheckDefFailure(['var name: dom'], 'E1010:') 153 CheckDefFailure(['var name: freddy'], 'E1010:') 154 CheckDefFailure(['var name: john'], 'E1010:') 155 CheckDefFailure(['var name: larry'], 'E1010:') 156 CheckDefFailure(['var name: ned'], 'E1010:') 157 CheckDefFailure(['var name: pam'], 'E1010:') 158 CheckDefFailure(['var name: sam'], 'E1010:') 159 CheckDefFailure(['var name: vim'], 'E1010:') 160 161 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:') 162 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:') 163enddef 164 165def Test_script_wrong_type() 166 var lines =<< trim END 167 vim9script 168 var s:dict: dict<string> 169 s:dict['a'] = ['x'] 170 END 171 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3) 172enddef 173 174def Test_const() 175 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:') 176 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:') 177 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:') 178 CheckDefFailure(['final two'], 'E1125:') 179 CheckDefFailure(['final &option'], 'E996:') 180 181 var lines =<< trim END 182 final list = [1, 2, 3] 183 list[0] = 4 184 list->assert_equal([4, 2, 3]) 185 const other = [5, 6, 7] 186 other->assert_equal([5, 6, 7]) 187 188 var varlist = [7, 8] 189 const constlist = [1, varlist, 3] 190 varlist[0] = 77 191 # TODO: does not work yet 192 # constlist[1][1] = 88 193 var cl = constlist[1] 194 cl[1] = 88 195 constlist->assert_equal([1, [77, 88], 3]) 196 197 var vardict = {five: 5, six: 6} 198 const constdict = {one: 1, two: vardict, three: 3} 199 vardict['five'] = 55 200 # TODO: does not work yet 201 # constdict['two']['six'] = 66 202 var cd = constdict['two'] 203 cd['six'] = 66 204 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3}) 205 END 206 CheckDefAndScriptSuccess(lines) 207enddef 208 209def Test_const_bang() 210 var lines =<< trim END 211 const var = 234 212 var = 99 213 END 214 CheckDefExecFailure(lines, 'E1018:', 2) 215 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3) 216 217 lines =<< trim END 218 const ll = [2, 3, 4] 219 ll[0] = 99 220 END 221 CheckDefExecFailure(lines, 'E1119:', 2) 222 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3) 223 224 lines =<< trim END 225 const ll = [2, 3, 4] 226 ll[3] = 99 227 END 228 CheckDefExecFailure(lines, 'E1118:', 2) 229 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3) 230 231 lines =<< trim END 232 const dd = {one: 1, two: 2} 233 dd["one"] = 99 234 END 235 CheckDefExecFailure(lines, 'E1121:', 2) 236 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3) 237 238 lines =<< trim END 239 const dd = {one: 1, two: 2} 240 dd["three"] = 99 241 END 242 CheckDefExecFailure(lines, 'E1120:') 243 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3) 244enddef 245 246def Test_range_no_colon() 247 CheckDefFailure(['%s/a/b/'], 'E1050:') 248 CheckDefFailure(['+ s/a/b/'], 'E1050:') 249 CheckDefFailure(['- s/a/b/'], 'E1050:') 250 CheckDefFailure(['. s/a/b/'], 'E1050:') 251enddef 252 253 254def Test_block() 255 var outer = 1 256 { 257 var inner = 2 258 assert_equal(1, outer) 259 assert_equal(2, inner) 260 } 261 assert_equal(1, outer) 262 263 {|echo 'yes'|} 264enddef 265 266def Test_block_failure() 267 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:') 268 CheckDefFailure(['}'], 'E1025:') 269 CheckDefFailure(['{', 'echo 1'], 'E1026:') 270enddef 271 272def Test_block_local_vars() 273 var lines =<< trim END 274 vim9script 275 v:testing = 1 276 if true 277 var text = ['hello'] 278 def SayHello(): list<string> 279 return text 280 enddef 281 def SetText(v: string) 282 text = [v] 283 enddef 284 endif 285 286 if true 287 var text = ['again'] 288 def SayAgain(): list<string> 289 return text 290 enddef 291 endif 292 293 # test that the "text" variables are not cleaned up 294 test_garbagecollect_now() 295 296 defcompile 297 298 assert_equal(['hello'], SayHello()) 299 assert_equal(['again'], SayAgain()) 300 301 SetText('foobar') 302 assert_equal(['foobar'], SayHello()) 303 304 call writefile(['ok'], 'Xdidit') 305 qall! 306 END 307 308 # need to execute this with a separate Vim instance to avoid the current 309 # context gets garbage collected. 310 writefile(lines, 'Xscript') 311 RunVim([], [], '-S Xscript') 312 assert_equal(['ok'], readfile('Xdidit')) 313 314 delete('Xscript') 315 delete('Xdidit') 316enddef 317 318def Test_block_local_vars_with_func() 319 var lines =<< trim END 320 vim9script 321 if true 322 var foo = 'foo' 323 if true 324 var bar = 'bar' 325 def Func(): list<string> 326 return [foo, bar] 327 enddef 328 endif 329 endif 330 # function is compiled here, after blocks have finished, can still access 331 # "foo" and "bar" 332 assert_equal(['foo', 'bar'], Func()) 333 END 334 CheckScriptSuccess(lines) 335enddef 336 337func g:NoSuchFunc() 338 echo 'none' 339endfunc 340 341def Test_try_catch_throw() 342 var l = [] 343 try # comment 344 add(l, '1') 345 throw 'wrong' 346 add(l, '2') 347 catch # comment 348 add(l, v:exception) 349 finally # comment 350 add(l, '3') 351 endtry # comment 352 assert_equal(['1', 'wrong', '3'], l) 353 354 l = [] 355 try 356 try 357 add(l, '1') 358 throw 'wrong' 359 add(l, '2') 360 catch /right/ 361 add(l, v:exception) 362 endtry 363 catch /wrong/ 364 add(l, 'caught') 365 fina 366 add(l, 'finally') 367 endtry 368 assert_equal(['1', 'caught', 'finally'], l) 369 370 var n: number 371 try 372 n = l[3] 373 catch /E684:/ 374 n = 99 375 endtry 376 assert_equal(99, n) 377 378 var done = 'no' 379 if 0 380 try | catch | endtry 381 else 382 done = 'yes' 383 endif 384 assert_equal('yes', done) 385 386 done = 'no' 387 if 1 388 done = 'yes' 389 else 390 try | catch | endtry 391 done = 'never' 392 endif 393 assert_equal('yes', done) 394 395 if 1 396 else 397 try | catch /pat/ | endtry 398 try | catch /pat/ 399 endtry 400 try 401 catch /pat/ | endtry 402 try 403 catch /pat/ 404 endtry 405 endif 406 407 try 408 # string slice returns a string, not a number 409 n = g:astring[3] 410 catch /E1012:/ 411 n = 77 412 endtry 413 assert_equal(77, n) 414 415 try 416 n = l[g:astring] 417 catch /E1012:/ 418 n = 88 419 endtry 420 assert_equal(88, n) 421 422 try 423 n = s:does_not_exist 424 catch /E121:/ 425 n = 111 426 endtry 427 assert_equal(111, n) 428 429 try 430 n = g:does_not_exist 431 catch /E121:/ 432 n = 121 433 endtry 434 assert_equal(121, n) 435 436 var d = {one: 1} 437 try 438 n = d[g:astring] 439 catch /E716:/ 440 n = 222 441 endtry 442 assert_equal(222, n) 443 444 try 445 n = -g:astring 446 catch /E39:/ 447 n = 233 448 endtry 449 assert_equal(233, n) 450 451 try 452 n = +g:astring 453 catch /E1030:/ 454 n = 244 455 endtry 456 assert_equal(244, n) 457 458 try 459 n = +g:alist 460 catch /E745:/ 461 n = 255 462 endtry 463 assert_equal(255, n) 464 465 var nd: dict<any> 466 try 467 nd = {[g:alist]: 1} 468 catch /E1105:/ 469 n = 266 470 endtry 471 assert_equal(266, n) 472 473 try 474 [n] = [1, 2, 3] 475 catch /E1093:/ 476 n = 277 477 endtry 478 assert_equal(277, n) 479 480 try 481 &ts = g:astring 482 catch /E1012:/ 483 n = 288 484 endtry 485 assert_equal(288, n) 486 487 try 488 &backspace = 'asdf' 489 catch /E474:/ 490 n = 299 491 endtry 492 assert_equal(299, n) 493 494 l = [1] 495 try 496 l[3] = 3 497 catch /E684:/ 498 n = 300 499 endtry 500 assert_equal(300, n) 501 502 try 503 unlet g:does_not_exist 504 catch /E108:/ 505 n = 322 506 endtry 507 assert_equal(322, n) 508 509 try 510 d = {text: 1, [g:astring]: 2} 511 catch /E721:/ 512 n = 333 513 endtry 514 assert_equal(333, n) 515 516 try 517 l = DeletedFunc() 518 catch /E933:/ 519 n = 344 520 endtry 521 assert_equal(344, n) 522 523 try 524 echo len(v:true) 525 catch /E701:/ 526 n = 355 527 endtry 528 assert_equal(355, n) 529 530 var P = function('g:NoSuchFunc') 531 delfunc g:NoSuchFunc 532 try 533 echo P() 534 catch /E117:/ 535 n = 366 536 endtry 537 assert_equal(366, n) 538 539 try 540 echo g:NoSuchFunc() 541 catch /E117:/ 542 n = 377 543 endtry 544 assert_equal(377, n) 545 546 try 547 echo g:alist + 4 548 catch /E745:/ 549 n = 388 550 endtry 551 assert_equal(388, n) 552 553 try 554 echo 4 + g:alist 555 catch /E745:/ 556 n = 399 557 endtry 558 assert_equal(399, n) 559 560 try 561 echo g:alist.member 562 catch /E715:/ 563 n = 400 564 endtry 565 assert_equal(400, n) 566 567 try 568 echo d.member 569 catch /E716:/ 570 n = 411 571 endtry 572 assert_equal(411, n) 573 574 var counter = 0 575 for i in range(4) 576 try 577 eval [][0] 578 catch 579 endtry 580 counter += 1 581 endfor 582 assert_equal(4, counter) 583 584 # return in finally after empty catch 585 def ReturnInFinally(): number 586 try 587 finally 588 return 4 589 endtry 590 return 2 591 enddef 592 assert_equal(4, ReturnInFinally()) 593enddef 594 595" :while at the very start of a function that :continue jumps to 596def TryContinueFunc() 597 while g:Count < 2 598 g:sequence ..= 't' 599 try 600 echoerr 'Test' 601 catch 602 g:Count += 1 603 g:sequence ..= 'c' 604 continue 605 endtry 606 g:sequence ..= 'e' 607 g:Count += 1 608 endwhile 609enddef 610 611def Test_continue_in_try_in_while() 612 g:Count = 0 613 g:sequence = '' 614 TryContinueFunc() 615 assert_equal('tctc', g:sequence) 616 unlet g:Count 617 unlet g:sequence 618enddef 619 620def Test_nocatch_return_in_try() 621 # return in try block returns normally 622 def ReturnInTry(): string 623 try 624 return '"some message"' 625 catch 626 endtry 627 return 'not reached' 628 enddef 629 exe 'echoerr ' .. ReturnInTry() 630enddef 631 632def Test_cnext_works_in_catch() 633 var lines =<< trim END 634 vim9script 635 au BufEnter * eval 0 636 writefile(['text'], 'Xfile1') 637 writefile(['text'], 'Xfile2') 638 var items = [ 639 {lnum: 1, filename: 'Xfile1', valid: true}, 640 {lnum: 1, filename: 'Xfile2', valid: true} 641 ] 642 setqflist([], ' ', {items: items}) 643 cwindow 644 645 def CnextOrCfirst() 646 # if cnext fails, cfirst is used 647 try 648 cnext 649 catch 650 cfirst 651 endtry 652 enddef 653 654 CnextOrCfirst() 655 CnextOrCfirst() 656 writefile([getqflist({idx: 0}).idx], 'Xresult') 657 qall 658 END 659 writefile(lines, 'XCatchCnext') 660 RunVim([], [], '--clean -S XCatchCnext') 661 assert_equal(['1'], readfile('Xresult')) 662 663 delete('Xfile1') 664 delete('Xfile2') 665 delete('XCatchCnext') 666 delete('Xresult') 667enddef 668 669def Test_throw_skipped() 670 if 0 671 throw dontgethere 672 endif 673enddef 674 675def Test_nocatch_throw_silenced() 676 var lines =<< trim END 677 vim9script 678 def Func() 679 throw 'error' 680 enddef 681 silent! Func() 682 END 683 writefile(lines, 'XthrowSilenced') 684 source XthrowSilenced 685 delete('XthrowSilenced') 686enddef 687 688def DeletedFunc(): list<any> 689 return ['delete me'] 690enddef 691defcompile 692delfunc DeletedFunc 693 694def ThrowFromDef() 695 throw "getout" # comment 696enddef 697 698func CatchInFunc() 699 try 700 call ThrowFromDef() 701 catch 702 let g:thrown_func = v:exception 703 endtry 704endfunc 705 706def CatchInDef() 707 try 708 ThrowFromDef() 709 catch 710 g:thrown_def = v:exception 711 endtry 712enddef 713 714def ReturnFinally(): string 715 try 716 return 'intry' 717 finall 718 g:in_finally = 'finally' 719 endtry 720 return 'end' 721enddef 722 723def Test_try_catch_nested() 724 CatchInFunc() 725 assert_equal('getout', g:thrown_func) 726 727 CatchInDef() 728 assert_equal('getout', g:thrown_def) 729 730 assert_equal('intry', ReturnFinally()) 731 assert_equal('finally', g:in_finally) 732enddef 733 734def TryOne(): number 735 try 736 return 0 737 catch 738 endtry 739 return 0 740enddef 741 742def TryTwo(n: number): string 743 try 744 var x = {} 745 catch 746 endtry 747 return 'text' 748enddef 749 750def Test_try_catch_twice() 751 assert_equal('text', TryOne()->TryTwo()) 752enddef 753 754def Test_try_catch_match() 755 var seq = 'a' 756 try 757 throw 'something' 758 catch /nothing/ 759 seq ..= 'x' 760 catch /some/ 761 seq ..= 'b' 762 catch /asdf/ 763 seq ..= 'x' 764 catch ?a\?sdf? 765 seq ..= 'y' 766 finally 767 seq ..= 'c' 768 endtry 769 assert_equal('abc', seq) 770enddef 771 772def Test_try_catch_fails() 773 CheckDefFailure(['catch'], 'E603:') 774 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:') 775 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:') 776 CheckDefFailure(['finally'], 'E606:') 777 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:') 778 CheckDefFailure(['endtry'], 'E602:') 779 CheckDefFailure(['while 1', 'endtry'], 'E170:') 780 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:') 781 CheckDefFailure(['if 1', 'endtry'], 'E171:') 782 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:') 783 784 CheckDefFailure(['throw'], 'E1143:') 785 CheckDefFailure(['throw xxx'], 'E1001:') 786enddef 787 788def Try_catch_skipped() 789 var l = [] 790 try 791 finally 792 endtry 793 794 if 1 795 else 796 try 797 endtry 798 endif 799enddef 800 801" The skipped try/endtry was updating the wrong instruction. 802def Test_try_catch_skipped() 803 var instr = execute('disassemble Try_catch_skipped') 804 assert_match("NEWLIST size 0\n", instr) 805enddef 806 807 808 809def Test_throw_vimscript() 810 # only checks line continuation 811 var lines =<< trim END 812 vim9script 813 try 814 throw 'one' 815 .. 'two' 816 catch 817 assert_equal('onetwo', v:exception) 818 endtry 819 END 820 CheckScriptSuccess(lines) 821 822 lines =<< trim END 823 vim9script 824 @r = '' 825 def Func() 826 throw @r 827 enddef 828 var result = '' 829 try 830 Func() 831 catch /E1129:/ 832 result = 'caught' 833 endtry 834 assert_equal('caught', result) 835 END 836 CheckScriptSuccess(lines) 837enddef 838 839def Test_error_in_nested_function() 840 # an error in a nested :function aborts executing in the calling :def function 841 var lines =<< trim END 842 vim9script 843 def Func() 844 Error() 845 g:test_var = 1 846 enddef 847 func Error() abort 848 eval [][0] 849 endfunc 850 Func() 851 END 852 g:test_var = 0 853 CheckScriptFailure(lines, 'E684:') 854 assert_equal(0, g:test_var) 855enddef 856 857def Test_abort_after_error() 858 var lines =<< trim END 859 vim9script 860 while true 861 echo notfound 862 endwhile 863 g:gotthere = true 864 END 865 g:gotthere = false 866 CheckScriptFailure(lines, 'E121:') 867 assert_false(g:gotthere) 868 unlet g:gotthere 869enddef 870 871def Test_cexpr_vimscript() 872 # only checks line continuation 873 set errorformat=File\ %f\ line\ %l 874 var lines =<< trim END 875 vim9script 876 cexpr 'File' 877 .. ' someFile' .. 878 ' line 19' 879 assert_equal(19, getqflist()[0].lnum) 880 END 881 CheckScriptSuccess(lines) 882 set errorformat& 883enddef 884 885def Test_statusline_syntax() 886 # legacy syntax is used for 'statusline' 887 var lines =<< trim END 888 vim9script 889 func g:Status() 890 return '%{"x" is# "x"}' 891 endfunc 892 set laststatus=2 statusline=%!Status() 893 redrawstatus 894 set laststatus statusline= 895 END 896 CheckScriptSuccess(lines) 897enddef 898 899def Test_list_vimscript() 900 # checks line continuation and comments 901 var lines =<< trim END 902 vim9script 903 var mylist = [ 904 'one', 905 # comment 906 'two', # empty line follows 907 908 'three', 909 ] 910 assert_equal(['one', 'two', 'three'], mylist) 911 END 912 CheckScriptSuccess(lines) 913 914 # check all lines from heredoc are kept 915 lines =<< trim END 916 # comment 1 917 two 918 # comment 3 919 920 five 921 # comment 6 922 END 923 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines) 924 925 lines =<< trim END 926 [{ 927 a: 0}]->string()->assert_equal("[{'a': 0}]") 928 END 929 CheckDefAndScriptSuccess(lines) 930enddef 931 932if has('channel') 933 let someJob = test_null_job() 934 935 def FuncWithError() 936 echomsg g:someJob 937 enddef 938 939 func Test_convert_emsg_to_exception() 940 try 941 call FuncWithError() 942 catch 943 call assert_match('Vim:E908:', v:exception) 944 endtry 945 endfunc 946endif 947 948let s:export_script_lines =<< trim END 949 vim9script 950 var name: string = 'bob' 951 def Concat(arg: string): string 952 return name .. arg 953 enddef 954 g:result = Concat('bie') 955 g:localname = name 956 957 export const CONST = 1234 958 export var exported = 9876 959 export var exp_name = 'John' 960 export def Exported(): string 961 return 'Exported' 962 enddef 963 export final theList = [1] 964END 965 966def Undo_export_script_lines() 967 unlet g:result 968 unlet g:localname 969enddef 970 971def Test_vim9_import_export() 972 var import_script_lines =<< trim END 973 vim9script 974 import {exported, Exported} from './Xexport.vim' 975 g:imported = exported 976 exported += 3 977 g:imported_added = exported 978 g:imported_func = Exported() 979 980 def GetExported(): string 981 var local_dict = {ref: Exported} 982 return local_dict.ref() 983 enddef 984 g:funcref_result = GetExported() 985 986 import {exp_name} from './Xexport.vim' 987 g:imported_name = exp_name 988 exp_name ..= ' Doe' 989 g:imported_name_appended = exp_name 990 g:imported_later = exported 991 992 import theList from './Xexport.vim' 993 theList->add(2) 994 assert_equal([1, 2], theList) 995 END 996 997 writefile(import_script_lines, 'Ximport.vim') 998 writefile(s:export_script_lines, 'Xexport.vim') 999 1000 source Ximport.vim 1001 1002 assert_equal('bobbie', g:result) 1003 assert_equal('bob', g:localname) 1004 assert_equal(9876, g:imported) 1005 assert_equal(9879, g:imported_added) 1006 assert_equal(9879, g:imported_later) 1007 assert_equal('Exported', g:imported_func) 1008 assert_equal('Exported', g:funcref_result) 1009 assert_equal('John', g:imported_name) 1010 assert_equal('John Doe', g:imported_name_appended) 1011 assert_false(exists('g:name')) 1012 1013 Undo_export_script_lines() 1014 unlet g:imported 1015 unlet g:imported_added 1016 unlet g:imported_later 1017 unlet g:imported_func 1018 unlet g:imported_name g:imported_name_appended 1019 delete('Ximport.vim') 1020 1021 # similar, with line breaks 1022 var import_line_break_script_lines =<< trim END 1023 vim9script 1024 import { 1025 exported, 1026 Exported, 1027 } 1028 from 1029 './Xexport.vim' 1030 g:imported = exported 1031 exported += 5 1032 g:imported_added = exported 1033 g:imported_func = Exported() 1034 END 1035 writefile(import_line_break_script_lines, 'Ximport_lbr.vim') 1036 source Ximport_lbr.vim 1037 1038 assert_equal(9876, g:imported) 1039 assert_equal(9881, g:imported_added) 1040 assert_equal('Exported', g:imported_func) 1041 1042 # exported script not sourced again 1043 assert_false(exists('g:result')) 1044 unlet g:imported 1045 unlet g:imported_added 1046 unlet g:imported_func 1047 delete('Ximport_lbr.vim') 1048 1049 # import inside :def function 1050 var import_in_def_lines =<< trim END 1051 vim9script 1052 def ImportInDef() 1053 import exported from './Xexport.vim' 1054 g:imported = exported 1055 exported += 7 1056 g:imported_added = exported 1057 enddef 1058 ImportInDef() 1059 END 1060 writefile(import_in_def_lines, 'Ximport2.vim') 1061 source Ximport2.vim 1062 # TODO: this should be 9879 1063 assert_equal(9876, g:imported) 1064 assert_equal(9883, g:imported_added) 1065 unlet g:imported 1066 unlet g:imported_added 1067 delete('Ximport2.vim') 1068 1069 var import_star_as_lines =<< trim END 1070 vim9script 1071 import * as Export from './Xexport.vim' 1072 def UseExport() 1073 g:imported_def = Export.exported 1074 enddef 1075 g:imported_script = Export.exported 1076 assert_equal(1, exists('Export.exported')) 1077 assert_equal(0, exists('Export.notexported')) 1078 UseExport() 1079 END 1080 writefile(import_star_as_lines, 'Ximport.vim') 1081 source Ximport.vim 1082 assert_equal(9883, g:imported_def) 1083 assert_equal(9883, g:imported_script) 1084 1085 var import_star_as_lines_no_dot =<< trim END 1086 vim9script 1087 import * as Export from './Xexport.vim' 1088 def Func() 1089 var dummy = 1 1090 var imported = Export + dummy 1091 enddef 1092 defcompile 1093 END 1094 writefile(import_star_as_lines_no_dot, 'Ximport.vim') 1095 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func') 1096 1097 var import_star_as_lines_dot_space =<< trim END 1098 vim9script 1099 import * as Export from './Xexport.vim' 1100 def Func() 1101 var imported = Export . exported 1102 enddef 1103 defcompile 1104 END 1105 writefile(import_star_as_lines_dot_space, 'Ximport.vim') 1106 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func') 1107 1108 var import_star_as_duplicated =<< trim END 1109 vim9script 1110 import * as Export from './Xexport.vim' 1111 var some = 'other' 1112 import * as Export from './Xexport.vim' 1113 defcompile 1114 END 1115 writefile(import_star_as_duplicated, 'Ximport.vim') 1116 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim') 1117 1118 var import_star_as_lines_script_no_dot =<< trim END 1119 vim9script 1120 import * as Export from './Xexport.vim' 1121 g:imported_script = Export exported 1122 END 1123 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim') 1124 assert_fails('source Ximport.vim', 'E1029:') 1125 1126 var import_star_as_lines_script_space_after_dot =<< trim END 1127 vim9script 1128 import * as Export from './Xexport.vim' 1129 g:imported_script = Export. exported 1130 END 1131 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim') 1132 assert_fails('source Ximport.vim', 'E1074:') 1133 1134 var import_star_as_lines_missing_name =<< trim END 1135 vim9script 1136 import * as Export from './Xexport.vim' 1137 def Func() 1138 var imported = Export. 1139 enddef 1140 defcompile 1141 END 1142 writefile(import_star_as_lines_missing_name, 'Ximport.vim') 1143 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func') 1144 1145 var import_star_as_lbr_lines =<< trim END 1146 vim9script 1147 import * 1148 as Export 1149 from 1150 './Xexport.vim' 1151 def UseExport() 1152 g:imported = Export.exported 1153 enddef 1154 UseExport() 1155 END 1156 writefile(import_star_as_lbr_lines, 'Ximport.vim') 1157 source Ximport.vim 1158 assert_equal(9883, g:imported) 1159 1160 var import_star_lines =<< trim END 1161 vim9script 1162 import * from './Xexport.vim' 1163 END 1164 writefile(import_star_lines, 'Ximport.vim') 1165 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim') 1166 1167 # try to import something that exists but is not exported 1168 var import_not_exported_lines =<< trim END 1169 vim9script 1170 import name from './Xexport.vim' 1171 END 1172 writefile(import_not_exported_lines, 'Ximport.vim') 1173 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim') 1174 1175 # try to import something that is already defined 1176 var import_already_defined =<< trim END 1177 vim9script 1178 var exported = 'something' 1179 import exported from './Xexport.vim' 1180 END 1181 writefile(import_already_defined, 'Ximport.vim') 1182 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim') 1183 1184 # try to import something that is already defined 1185 import_already_defined =<< trim END 1186 vim9script 1187 var exported = 'something' 1188 import * as exported from './Xexport.vim' 1189 END 1190 writefile(import_already_defined, 'Ximport.vim') 1191 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim') 1192 1193 # try to import something that is already defined 1194 import_already_defined =<< trim END 1195 vim9script 1196 var exported = 'something' 1197 import {exported} from './Xexport.vim' 1198 END 1199 writefile(import_already_defined, 'Ximport.vim') 1200 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim') 1201 1202 # try changing an imported const 1203 var import_assign_to_const =<< trim END 1204 vim9script 1205 import CONST from './Xexport.vim' 1206 def Assign() 1207 CONST = 987 1208 enddef 1209 defcompile 1210 END 1211 writefile(import_assign_to_const, 'Ximport.vim') 1212 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign') 1213 1214 # try changing an imported final 1215 var import_assign_to_final =<< trim END 1216 vim9script 1217 import theList from './Xexport.vim' 1218 def Assign() 1219 theList = [2] 1220 enddef 1221 defcompile 1222 END 1223 writefile(import_assign_to_final, 'Ximport.vim') 1224 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign') 1225 1226 # import a very long name, requires making a copy 1227 var import_long_name_lines =<< trim END 1228 vim9script 1229 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim' 1230 END 1231 writefile(import_long_name_lines, 'Ximport.vim') 1232 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim') 1233 1234 var import_no_from_lines =<< trim END 1235 vim9script 1236 import name './Xexport.vim' 1237 END 1238 writefile(import_no_from_lines, 'Ximport.vim') 1239 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim') 1240 1241 var import_invalid_string_lines =<< trim END 1242 vim9script 1243 import name from Xexport.vim 1244 END 1245 writefile(import_invalid_string_lines, 'Ximport.vim') 1246 assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim') 1247 1248 var import_wrong_name_lines =<< trim END 1249 vim9script 1250 import name from './XnoExport.vim' 1251 END 1252 writefile(import_wrong_name_lines, 'Ximport.vim') 1253 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim') 1254 1255 var import_missing_comma_lines =<< trim END 1256 vim9script 1257 import {exported name} from './Xexport.vim' 1258 END 1259 writefile(import_missing_comma_lines, 'Ximport3.vim') 1260 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim') 1261 1262 delete('Ximport.vim') 1263 delete('Ximport3.vim') 1264 delete('Xexport.vim') 1265 1266 # Check that in a Vim9 script 'cpo' is set to the Vim default. 1267 # Flags added or removed are also applied to the restored value. 1268 set cpo=abcd 1269 var lines =<< trim END 1270 vim9script 1271 g:cpo_in_vim9script = &cpo 1272 set cpo+=f 1273 set cpo-=c 1274 g:cpo_after_vim9script = &cpo 1275 END 1276 writefile(lines, 'Xvim9_script') 1277 source Xvim9_script 1278 assert_equal('fabd', &cpo) 1279 set cpo&vim 1280 assert_equal(&cpo, g:cpo_in_vim9script) 1281 var newcpo = substitute(&cpo, 'c', '', '') .. 'f' 1282 assert_equal(newcpo, g:cpo_after_vim9script) 1283 1284 delete('Xvim9_script') 1285enddef 1286 1287def Test_import_as() 1288 var export_lines =<< trim END 1289 vim9script 1290 export var one = 1 1291 export var yes = 'yes' 1292 END 1293 writefile(export_lines, 'XexportAs') 1294 1295 var import_lines =<< trim END 1296 vim9script 1297 var one = 'notused' 1298 var yes = 777 1299 import one as thatOne from './XexportAs' 1300 assert_equal(1, thatOne) 1301 import yes as yesYes from './XexportAs' 1302 assert_equal('yes', yesYes) 1303 END 1304 CheckScriptSuccess(import_lines) 1305 1306 import_lines =<< trim END 1307 vim9script 1308 import {one as thatOne, yes as yesYes} from './XexportAs' 1309 assert_equal(1, thatOne) 1310 assert_equal('yes', yesYes) 1311 assert_fails('echo one', 'E121:') 1312 assert_fails('echo yes', 'E121:') 1313 END 1314 CheckScriptSuccess(import_lines) 1315 1316 delete('XexportAs') 1317enddef 1318 1319func g:Trigger() 1320 source Ximport.vim 1321 return "echo 'yes'\<CR>" 1322endfunc 1323 1324def Test_import_export_expr_map() 1325 # check that :import and :export work when buffer is locked 1326 var export_lines =<< trim END 1327 vim9script 1328 export def That(): string 1329 return 'yes' 1330 enddef 1331 END 1332 writefile(export_lines, 'Xexport_that.vim') 1333 1334 var import_lines =<< trim END 1335 vim9script 1336 import That from './Xexport_that.vim' 1337 assert_equal('yes', That()) 1338 END 1339 writefile(import_lines, 'Ximport.vim') 1340 1341 nnoremap <expr> trigger g:Trigger() 1342 feedkeys('trigger', "xt") 1343 1344 delete('Xexport_that.vim') 1345 delete('Ximport.vim') 1346 nunmap trigger 1347enddef 1348 1349def Test_import_in_filetype() 1350 # check that :import works when the buffer is locked 1351 mkdir('ftplugin', 'p') 1352 var export_lines =<< trim END 1353 vim9script 1354 export var That = 'yes' 1355 END 1356 writefile(export_lines, 'ftplugin/Xexport_ft.vim') 1357 1358 var import_lines =<< trim END 1359 vim9script 1360 import That from './Xexport_ft.vim' 1361 assert_equal('yes', That) 1362 g:did_load_mytpe = 1 1363 END 1364 writefile(import_lines, 'ftplugin/qf.vim') 1365 1366 var save_rtp = &rtp 1367 &rtp = getcwd() .. ',' .. &rtp 1368 1369 filetype plugin on 1370 copen 1371 assert_equal(1, g:did_load_mytpe) 1372 1373 quit! 1374 delete('Xexport_ft.vim') 1375 delete('ftplugin', 'rf') 1376 &rtp = save_rtp 1377enddef 1378 1379def Test_use_import_in_mapping() 1380 var lines =<< trim END 1381 vim9script 1382 export def Funcx() 1383 g:result = 42 1384 enddef 1385 END 1386 writefile(lines, 'XsomeExport.vim') 1387 lines =<< trim END 1388 vim9script 1389 import Funcx from './XsomeExport.vim' 1390 nnoremap <F3> :call <sid>Funcx()<cr> 1391 END 1392 writefile(lines, 'Xmapscript.vim') 1393 1394 source Xmapscript.vim 1395 feedkeys("\<F3>", "xt") 1396 assert_equal(42, g:result) 1397 1398 unlet g:result 1399 delete('XsomeExport.vim') 1400 delete('Xmapscript.vim') 1401 nunmap <F3> 1402enddef 1403 1404def Test_vim9script_mix() 1405 var lines =<< trim END 1406 if has(g:feature) 1407 " legacy script 1408 let g:legacy = 1 1409 finish 1410 endif 1411 vim9script 1412 g:legacy = 0 1413 END 1414 g:feature = 'eval' 1415 g:legacy = -1 1416 CheckScriptSuccess(lines) 1417 assert_equal(1, g:legacy) 1418 1419 g:feature = 'noteval' 1420 g:legacy = -1 1421 CheckScriptSuccess(lines) 1422 assert_equal(0, g:legacy) 1423enddef 1424 1425def Test_vim9script_fails() 1426 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:') 1427 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:') 1428 CheckScriptFailure(['export var some = 123'], 'E1042:') 1429 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:') 1430 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:') 1431 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:') 1432 1433 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:') 1434 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:') 1435 1436 assert_fails('vim9script', 'E1038:') 1437 assert_fails('export something', 'E1043:') 1438enddef 1439 1440func Test_import_fails_without_script() 1441 CheckRunVimInTerminal 1442 1443 " call indirectly to avoid compilation error for missing functions 1444 call Run_Test_import_fails_on_command_line() 1445endfunc 1446 1447def Run_Test_import_fails_on_command_line() 1448 var export =<< trim END 1449 vim9script 1450 export def Foo(): number 1451 return 0 1452 enddef 1453 END 1454 writefile(export, 'XexportCmd.vim') 1455 1456 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', { 1457 rows: 6, wait_for_ruler: 0}) 1458 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5))) 1459 1460 delete('XexportCmd.vim') 1461 StopVimInTerminal(buf) 1462enddef 1463 1464def Test_vim9script_reload_noclear() 1465 var lines =<< trim END 1466 vim9script 1467 export var exported = 'thexport' 1468 END 1469 writefile(lines, 'XExportReload') 1470 lines =<< trim END 1471 vim9script noclear 1472 g:loadCount += 1 1473 var s:reloaded = 'init' 1474 import exported from './XExportReload' 1475 1476 def Again(): string 1477 return 'again' 1478 enddef 1479 1480 if exists('s:loaded') | finish | endif 1481 var s:loaded = true 1482 1483 var s:notReloaded = 'yes' 1484 s:reloaded = 'first' 1485 def g:Values(): list<string> 1486 return [s:reloaded, s:notReloaded, Again(), Once(), exported] 1487 enddef 1488 1489 def Once(): string 1490 return 'once' 1491 enddef 1492 END 1493 writefile(lines, 'XReloaded') 1494 g:loadCount = 0 1495 source XReloaded 1496 assert_equal(1, g:loadCount) 1497 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values()) 1498 source XReloaded 1499 assert_equal(2, g:loadCount) 1500 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values()) 1501 source XReloaded 1502 assert_equal(3, g:loadCount) 1503 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values()) 1504 1505 delete('XReloaded') 1506 delete('XExportReload') 1507 delfunc g:Values 1508 unlet g:loadCount 1509enddef 1510 1511def Test_vim9script_reload_import() 1512 var lines =<< trim END 1513 vim9script 1514 const var = '' 1515 var valone = 1234 1516 def MyFunc(arg: string) 1517 valone = 5678 1518 enddef 1519 END 1520 var morelines =<< trim END 1521 var valtwo = 222 1522 export def GetValtwo(): number 1523 return valtwo 1524 enddef 1525 END 1526 writefile(lines + morelines, 'Xreload.vim') 1527 source Xreload.vim 1528 source Xreload.vim 1529 source Xreload.vim 1530 1531 var testlines =<< trim END 1532 vim9script 1533 def TheFunc() 1534 import GetValtwo from './Xreload.vim' 1535 assert_equal(222, GetValtwo()) 1536 enddef 1537 TheFunc() 1538 END 1539 writefile(testlines, 'Ximport.vim') 1540 source Ximport.vim 1541 1542 # Test that when not using "morelines" GetValtwo() and valtwo are still 1543 # defined, because import doesn't reload a script. 1544 writefile(lines, 'Xreload.vim') 1545 source Ximport.vim 1546 1547 # cannot declare a var twice 1548 lines =<< trim END 1549 vim9script 1550 var valone = 1234 1551 var valone = 5678 1552 END 1553 writefile(lines, 'Xreload.vim') 1554 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim') 1555 1556 delete('Xreload.vim') 1557 delete('Ximport.vim') 1558enddef 1559 1560" if a script is reloaded with a script-local variable that changed its type, a 1561" compiled function using that variable must fail. 1562def Test_script_reload_change_type() 1563 var lines =<< trim END 1564 vim9script noclear 1565 var str = 'string' 1566 def g:GetStr(): string 1567 return str .. 'xxx' 1568 enddef 1569 END 1570 writefile(lines, 'Xreload.vim') 1571 source Xreload.vim 1572 echo g:GetStr() 1573 1574 lines =<< trim END 1575 vim9script noclear 1576 var str = 1234 1577 END 1578 writefile(lines, 'Xreload.vim') 1579 source Xreload.vim 1580 assert_fails('echo g:GetStr()', 'E1150:') 1581 1582 delfunc g:GetStr 1583 delete('Xreload.vim') 1584enddef 1585 1586" Define CallFunc so that the test can be compiled 1587command CallFunc echo 'nop' 1588 1589def Test_script_reload_from_function() 1590 var lines =<< trim END 1591 vim9script 1592 1593 if exists('g:loaded') 1594 finish 1595 endif 1596 g:loaded = 1 1597 delcommand CallFunc 1598 command CallFunc Func() 1599 def Func() 1600 so XreloadFunc.vim 1601 g:didTheFunc = 1 1602 enddef 1603 END 1604 writefile(lines, 'XreloadFunc.vim') 1605 source XreloadFunc.vim 1606 CallFunc 1607 assert_equal(1, g:didTheFunc) 1608 1609 delete('XreloadFunc.vim') 1610 delcommand CallFunc 1611 unlet g:loaded 1612 unlet g:didTheFunc 1613enddef 1614 1615def Test_script_var_shadows_function() 1616 var lines =<< trim END 1617 vim9script 1618 def Func(): number 1619 return 123 1620 enddef 1621 var Func = 1 1622 END 1623 CheckScriptFailure(lines, 'E1041:', 5) 1624enddef 1625 1626def s:RetSome(): string 1627 return 'some' 1628enddef 1629 1630" Not exported function that is referenced needs to be accessed by the 1631" script-local name. 1632def Test_vim9script_funcref() 1633 var sortlines =<< trim END 1634 vim9script 1635 def Compare(i1: number, i2: number): number 1636 return i2 - i1 1637 enddef 1638 1639 export def FastSort(): list<number> 1640 return range(5)->sort(Compare) 1641 enddef 1642 1643 export def GetString(arg: string): string 1644 return arg 1645 enddef 1646 END 1647 writefile(sortlines, 'Xsort.vim') 1648 1649 var lines =<< trim END 1650 vim9script 1651 import FastSort from './Xsort.vim' 1652 def Test() 1653 g:result = FastSort() 1654 enddef 1655 Test() 1656 1657 # using a function imported with "as" 1658 import * as anAlias from './Xsort.vim' 1659 assert_equal('yes', anAlias.GetString('yes')) 1660 1661 # using the function from a compiled function 1662 def TestMore(): string 1663 var s = s:anAlias.GetString('foo') 1664 return s .. anAlias.GetString('bar') 1665 enddef 1666 assert_equal('foobar', TestMore()) 1667 1668 # error when using a function that isn't exported 1669 assert_fails('anAlias.Compare(1, 2)', 'E1049:') 1670 END 1671 writefile(lines, 'Xscript.vim') 1672 1673 source Xscript.vim 1674 assert_equal([4, 3, 2, 1, 0], g:result) 1675 1676 unlet g:result 1677 delete('Xsort.vim') 1678 delete('Xscript.vim') 1679 1680 var Funcref = function('s:RetSome') 1681 assert_equal('some', Funcref()) 1682enddef 1683 1684" Check that when searching for "FilterFunc" it finds the import in the 1685" script where FastFilter() is called from, both as a string and as a direct 1686" function reference. 1687def Test_vim9script_funcref_other_script() 1688 var filterLines =<< trim END 1689 vim9script 1690 export def FilterFunc(idx: number, val: number): bool 1691 return idx % 2 == 1 1692 enddef 1693 export def FastFilter(): list<number> 1694 return range(10)->filter('FilterFunc') 1695 enddef 1696 export def FastFilterDirect(): list<number> 1697 return range(10)->filter(FilterFunc) 1698 enddef 1699 END 1700 writefile(filterLines, 'Xfilter.vim') 1701 1702 var lines =<< trim END 1703 vim9script 1704 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim' 1705 def Test() 1706 var x: list<number> = FastFilter() 1707 enddef 1708 Test() 1709 def TestDirect() 1710 var x: list<number> = FastFilterDirect() 1711 enddef 1712 TestDirect() 1713 END 1714 CheckScriptSuccess(lines) 1715 delete('Xfilter.vim') 1716enddef 1717 1718def Test_vim9script_reload_delfunc() 1719 var first_lines =<< trim END 1720 vim9script 1721 def FuncYes(): string 1722 return 'yes' 1723 enddef 1724 END 1725 var withno_lines =<< trim END 1726 def FuncNo(): string 1727 return 'no' 1728 enddef 1729 def g:DoCheck(no_exists: bool) 1730 assert_equal('yes', FuncYes()) 1731 assert_equal('no', FuncNo()) 1732 enddef 1733 END 1734 var nono_lines =<< trim END 1735 def g:DoCheck(no_exists: bool) 1736 assert_equal('yes', FuncYes()) 1737 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck') 1738 enddef 1739 END 1740 1741 # FuncNo() is defined 1742 writefile(first_lines + withno_lines, 'Xreloaded.vim') 1743 source Xreloaded.vim 1744 g:DoCheck(true) 1745 1746 # FuncNo() is not redefined 1747 writefile(first_lines + nono_lines, 'Xreloaded.vim') 1748 source Xreloaded.vim 1749 g:DoCheck(false) 1750 1751 # FuncNo() is back 1752 writefile(first_lines + withno_lines, 'Xreloaded.vim') 1753 source Xreloaded.vim 1754 g:DoCheck(false) 1755 1756 delete('Xreloaded.vim') 1757enddef 1758 1759def Test_vim9script_reload_delvar() 1760 # write the script with a script-local variable 1761 var lines =<< trim END 1762 vim9script 1763 var name = 'string' 1764 END 1765 writefile(lines, 'XreloadVar.vim') 1766 source XreloadVar.vim 1767 1768 # now write the script using the same variable locally - works 1769 lines =<< trim END 1770 vim9script 1771 def Func() 1772 var name = 'string' 1773 enddef 1774 END 1775 writefile(lines, 'XreloadVar.vim') 1776 source XreloadVar.vim 1777 1778 delete('XreloadVar.vim') 1779enddef 1780 1781def Test_import_absolute() 1782 var import_lines = [ 1783 'vim9script', 1784 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"', 1785 'def UseExported()', 1786 ' g:imported_abs = exported', 1787 ' exported = 8888', 1788 ' g:imported_after = exported', 1789 'enddef', 1790 'UseExported()', 1791 'g:import_disassembled = execute("disass UseExported")', 1792 ] 1793 writefile(import_lines, 'Ximport_abs.vim') 1794 writefile(s:export_script_lines, 'Xexport_abs.vim') 1795 1796 source Ximport_abs.vim 1797 1798 assert_equal(9876, g:imported_abs) 1799 assert_equal(8888, g:imported_after) 1800 assert_match('<SNR>\d\+_UseExported\_s*' .. 1801 'g:imported_abs = exported\_s*' .. 1802 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' .. 1803 '1 STOREG g:imported_abs\_s*' .. 1804 'exported = 8888\_s*' .. 1805 '2 PUSHNR 8888\_s*' .. 1806 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' .. 1807 'g:imported_after = exported\_s*' .. 1808 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' .. 1809 '5 STOREG g:imported_after', 1810 g:import_disassembled) 1811 1812 Undo_export_script_lines() 1813 unlet g:imported_abs 1814 unlet g:import_disassembled 1815 1816 delete('Ximport_abs.vim') 1817 delete('Xexport_abs.vim') 1818enddef 1819 1820def Test_import_rtp() 1821 var import_lines = [ 1822 'vim9script', 1823 'import exported from "Xexport_rtp.vim"', 1824 'g:imported_rtp = exported', 1825 ] 1826 writefile(import_lines, 'Ximport_rtp.vim') 1827 mkdir('import') 1828 writefile(s:export_script_lines, 'import/Xexport_rtp.vim') 1829 1830 var save_rtp = &rtp 1831 &rtp = getcwd() 1832 source Ximport_rtp.vim 1833 &rtp = save_rtp 1834 1835 assert_equal(9876, g:imported_rtp) 1836 1837 Undo_export_script_lines() 1838 unlet g:imported_rtp 1839 delete('Ximport_rtp.vim') 1840 delete('import', 'rf') 1841enddef 1842 1843def Test_import_compile_error() 1844 var export_lines = [ 1845 'vim9script', 1846 'export def ExpFunc(): string', 1847 ' return notDefined', 1848 'enddef', 1849 ] 1850 writefile(export_lines, 'Xexported.vim') 1851 1852 var import_lines = [ 1853 'vim9script', 1854 'import ExpFunc from "./Xexported.vim"', 1855 'def ImpFunc()', 1856 ' echo ExpFunc()', 1857 'enddef', 1858 'defcompile', 1859 ] 1860 writefile(import_lines, 'Ximport.vim') 1861 1862 try 1863 source Ximport.vim 1864 catch /E1001/ 1865 # Error should be fore the Xexported.vim file. 1866 assert_match('E1001: Variable not found: notDefined', v:exception) 1867 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint) 1868 endtry 1869 1870 delete('Xexported.vim') 1871 delete('Ximport.vim') 1872enddef 1873 1874def Test_func_redefine_error() 1875 var lines = [ 1876 'vim9script', 1877 'def Func()', 1878 ' eval [][0]', 1879 'enddef', 1880 'Func()', 1881 ] 1882 writefile(lines, 'Xtestscript.vim') 1883 1884 for count in range(3) 1885 try 1886 source Xtestscript.vim 1887 catch /E684/ 1888 # function name should contain <SNR> every time 1889 assert_match('E684: list index out of range', v:exception) 1890 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint) 1891 endtry 1892 endfor 1893 1894 delete('Xtestscript.vim') 1895enddef 1896 1897def Test_func_overrules_import_fails() 1898 var export_lines =<< trim END 1899 vim9script 1900 export def Func() 1901 echo 'imported' 1902 enddef 1903 END 1904 writefile(export_lines, 'XexportedFunc.vim') 1905 1906 var lines =<< trim END 1907 vim9script 1908 import Func from './XexportedFunc.vim' 1909 def Func() 1910 echo 'local to function' 1911 enddef 1912 END 1913 CheckScriptFailure(lines, 'E1073:') 1914 1915 lines =<< trim END 1916 vim9script 1917 import Func from './XexportedFunc.vim' 1918 def Outer() 1919 def Func() 1920 echo 'local to function' 1921 enddef 1922 enddef 1923 defcompile 1924 END 1925 CheckScriptFailure(lines, 'E1073:') 1926 1927 delete('XexportedFunc.vim') 1928enddef 1929 1930def Test_func_redefine_fails() 1931 var lines =<< trim END 1932 vim9script 1933 def Func() 1934 echo 'one' 1935 enddef 1936 def Func() 1937 echo 'two' 1938 enddef 1939 END 1940 CheckScriptFailure(lines, 'E1073:') 1941 1942 lines =<< trim END 1943 vim9script 1944 def Foo(): string 1945 return 'foo' 1946 enddef 1947 def Func() 1948 var Foo = {-> 'lambda'} 1949 enddef 1950 defcompile 1951 END 1952 CheckScriptFailure(lines, 'E1073:') 1953enddef 1954 1955def Test_fixed_size_list() 1956 # will be allocated as one piece of memory, check that changes work 1957 var l = [1, 2, 3, 4] 1958 l->remove(0) 1959 l->add(5) 1960 l->insert(99, 1) 1961 assert_equal([2, 99, 3, 4, 5], l) 1962enddef 1963 1964def Test_no_insert_xit() 1965 CheckDefExecFailure(['a = 1'], 'E1100:') 1966 CheckDefExecFailure(['c = 1'], 'E1100:') 1967 CheckDefExecFailure(['i = 1'], 'E1100:') 1968 CheckDefExecFailure(['t = 1'], 'E1100:') 1969 CheckDefExecFailure(['x = 1'], 'E1100:') 1970 1971 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:') 1972 CheckScriptFailure(['vim9script', 'a'], 'E1100:') 1973 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:') 1974 CheckScriptFailure(['vim9script', 'c'], 'E1100:') 1975 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:') 1976 CheckScriptFailure(['vim9script', 'i'], 'E1100:') 1977 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:') 1978 CheckScriptFailure(['vim9script', 'o'], 'E1100:') 1979 CheckScriptFailure(['vim9script', 't'], 'E1100:') 1980 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:') 1981 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:') 1982enddef 1983 1984def IfElse(what: number): string 1985 var res = '' 1986 if what == 1 1987 res = "one" 1988 elseif what == 2 1989 res = "two" 1990 else 1991 res = "three" 1992 endif 1993 return res 1994enddef 1995 1996def Test_if_elseif_else() 1997 assert_equal('one', IfElse(1)) 1998 assert_equal('two', IfElse(2)) 1999 assert_equal('three', IfElse(3)) 2000enddef 2001 2002def Test_if_elseif_else_fails() 2003 CheckDefFailure(['elseif true'], 'E582:') 2004 CheckDefFailure(['else'], 'E581:') 2005 CheckDefFailure(['endif'], 'E580:') 2006 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:') 2007 CheckDefFailure(['if true', 'echo 1'], 'E171:') 2008 2009 var lines =<< trim END 2010 var s = '' 2011 if s = '' 2012 endif 2013 END 2014 CheckDefFailure(lines, 'E488:') 2015 2016 lines =<< trim END 2017 var s = '' 2018 if s == '' 2019 elseif s = '' 2020 endif 2021 END 2022 CheckDefFailure(lines, 'E488:') 2023enddef 2024 2025let g:bool_true = v:true 2026let g:bool_false = v:false 2027 2028def Test_if_const_expr() 2029 var res = false 2030 if true ? true : false 2031 res = true 2032 endif 2033 assert_equal(true, res) 2034 2035 g:glob = 2 2036 if false 2037 execute('g:glob = 3') 2038 endif 2039 assert_equal(2, g:glob) 2040 if true 2041 execute('g:glob = 3') 2042 endif 2043 assert_equal(3, g:glob) 2044 2045 res = false 2046 if g:bool_true ? true : false 2047 res = true 2048 endif 2049 assert_equal(true, res) 2050 2051 res = false 2052 if true ? g:bool_true : false 2053 res = true 2054 endif 2055 assert_equal(true, res) 2056 2057 res = false 2058 if true ? true : g:bool_false 2059 res = true 2060 endif 2061 assert_equal(true, res) 2062 2063 res = false 2064 if true ? false : true 2065 res = true 2066 endif 2067 assert_equal(false, res) 2068 2069 res = false 2070 if false ? false : true 2071 res = true 2072 endif 2073 assert_equal(true, res) 2074 2075 res = false 2076 if false ? true : false 2077 res = true 2078 endif 2079 assert_equal(false, res) 2080 2081 res = false 2082 if has('xyz') ? true : false 2083 res = true 2084 endif 2085 assert_equal(false, res) 2086 2087 res = false 2088 if true && true 2089 res = true 2090 endif 2091 assert_equal(true, res) 2092 2093 res = false 2094 if true && false 2095 res = true 2096 endif 2097 assert_equal(false, res) 2098 2099 res = false 2100 if g:bool_true && false 2101 res = true 2102 endif 2103 assert_equal(false, res) 2104 2105 res = false 2106 if true && g:bool_false 2107 res = true 2108 endif 2109 assert_equal(false, res) 2110 2111 res = false 2112 if false && false 2113 res = true 2114 endif 2115 assert_equal(false, res) 2116 2117 res = false 2118 if true || false 2119 res = true 2120 endif 2121 assert_equal(true, res) 2122 2123 res = false 2124 if g:bool_true || false 2125 res = true 2126 endif 2127 assert_equal(true, res) 2128 2129 res = false 2130 if true || g:bool_false 2131 res = true 2132 endif 2133 assert_equal(true, res) 2134 2135 res = false 2136 if false || false 2137 res = true 2138 endif 2139 assert_equal(false, res) 2140 2141 # with constant "false" expression may be invalid so long as the syntax is OK 2142 if false | eval 0 | endif 2143 if false | eval burp + 234 | endif 2144 if false | echo burp 234 'asd' | endif 2145 if false 2146 burp 2147 endif 2148enddef 2149 2150def Test_if_const_expr_fails() 2151 CheckDefFailure(['if "aaa" == "bbb'], 'E114:') 2152 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:') 2153 CheckDefFailure(["if has('aaa'"], 'E110:') 2154 CheckDefFailure(["if has('aaa') ? true false"], 'E109:') 2155enddef 2156 2157def RunNested(i: number): number 2158 var x: number = 0 2159 if i % 2 2160 if 1 2161 # comment 2162 else 2163 # comment 2164 endif 2165 x += 1 2166 else 2167 x += 1000 2168 endif 2169 return x 2170enddef 2171 2172def Test_nested_if() 2173 assert_equal(1, RunNested(1)) 2174 assert_equal(1000, RunNested(2)) 2175enddef 2176 2177def Test_execute_cmd() 2178 # missing argument is ignored 2179 execute 2180 execute # comment 2181 2182 new 2183 setline(1, 'default') 2184 execute 'setline(1, "execute-string")' 2185 assert_equal('execute-string', getline(1)) 2186 2187 execute "setline(1, 'execute-string')" 2188 assert_equal('execute-string', getline(1)) 2189 2190 var cmd1 = 'setline(1,' 2191 var cmd2 = '"execute-var")' 2192 execute cmd1 cmd2 # comment 2193 assert_equal('execute-var', getline(1)) 2194 2195 execute cmd1 cmd2 '|setline(1, "execute-var-string")' 2196 assert_equal('execute-var-string', getline(1)) 2197 2198 var cmd_first = 'call ' 2199 var cmd_last = 'setline(1, "execute-var-var")' 2200 execute cmd_first .. cmd_last 2201 assert_equal('execute-var-var', getline(1)) 2202 bwipe! 2203 2204 var n = true 2205 execute 'echomsg' (n ? '"true"' : '"no"') 2206 assert_match('^true$', Screenline(&lines)) 2207 2208 echomsg [1, 2, 3] {a: 1, b: 2} 2209 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines)) 2210 2211 CheckDefFailure(['execute xxx'], 'E1001:', 1) 2212 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1) 2213 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1) 2214enddef 2215 2216def Test_execute_cmd_vimscript() 2217 # only checks line continuation 2218 var lines =<< trim END 2219 vim9script 2220 execute 'g:someVar' 2221 .. ' = ' .. 2222 '28' 2223 assert_equal(28, g:someVar) 2224 unlet g:someVar 2225 END 2226 CheckScriptSuccess(lines) 2227enddef 2228 2229def Test_echo_cmd() 2230 echo 'some' # comment 2231 echon 'thing' 2232 assert_match('^something$', Screenline(&lines)) 2233 2234 echo "some" # comment 2235 echon "thing" 2236 assert_match('^something$', Screenline(&lines)) 2237 2238 var str1 = 'some' 2239 var str2 = 'more' 2240 echo str1 str2 2241 assert_match('^some more$', Screenline(&lines)) 2242 2243 CheckDefFailure(['echo "xxx"# comment'], 'E488:') 2244enddef 2245 2246def Test_echomsg_cmd() 2247 echomsg 'some' 'more' # comment 2248 assert_match('^some more$', Screenline(&lines)) 2249 echo 'clear' 2250 :1messages 2251 assert_match('^some more$', Screenline(&lines)) 2252 2253 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:') 2254enddef 2255 2256def Test_echomsg_cmd_vimscript() 2257 # only checks line continuation 2258 var lines =<< trim END 2259 vim9script 2260 echomsg 'here' 2261 .. ' is ' .. 2262 'a message' 2263 assert_match('^here is a message$', Screenline(&lines)) 2264 END 2265 CheckScriptSuccess(lines) 2266enddef 2267 2268def Test_echoerr_cmd() 2269 try 2270 echoerr 'something' 'wrong' # comment 2271 catch 2272 assert_match('something wrong', v:exception) 2273 endtry 2274enddef 2275 2276def Test_echoerr_cmd_vimscript() 2277 # only checks line continuation 2278 var lines =<< trim END 2279 vim9script 2280 try 2281 echoerr 'this' 2282 .. ' is ' .. 2283 'wrong' 2284 catch 2285 assert_match('this is wrong', v:exception) 2286 endtry 2287 END 2288 CheckScriptSuccess(lines) 2289enddef 2290 2291def Test_for_outside_of_function() 2292 var lines =<< trim END 2293 vim9script 2294 new 2295 for var in range(0, 3) 2296 append(line('$'), var) 2297 endfor 2298 assert_equal(['', '0', '1', '2', '3'], getline(1, '$')) 2299 bwipe! 2300 2301 var result = '' 2302 for i in [1, 2, 3] 2303 var loop = ' loop ' .. i 2304 result ..= loop 2305 endfor 2306 assert_equal(' loop 1 loop 2 loop 3', result) 2307 END 2308 writefile(lines, 'Xvim9for.vim') 2309 source Xvim9for.vim 2310 delete('Xvim9for.vim') 2311enddef 2312 2313def Test_for_loop() 2314 var lines =<< trim END 2315 var result = '' 2316 for cnt in range(7) 2317 if cnt == 4 2318 break 2319 endif 2320 if cnt == 2 2321 continue 2322 endif 2323 result ..= cnt .. '_' 2324 endfor 2325 assert_equal('0_1_3_', result) 2326 2327 var concat = '' 2328 for str in eval('["one", "two"]') 2329 concat ..= str 2330 endfor 2331 assert_equal('onetwo', concat) 2332 2333 var total = 0 2334 for nr in 2335 [1, 2, 3] 2336 total += nr 2337 endfor 2338 assert_equal(6, total) 2339 2340 total = 0 2341 for nr 2342 in [1, 2, 3] 2343 total += nr 2344 endfor 2345 assert_equal(6, total) 2346 2347 total = 0 2348 for nr 2349 in 2350 [1, 2, 3] 2351 total += nr 2352 endfor 2353 assert_equal(6, total) 2354 2355 # with type 2356 total = 0 2357 for n: number in [1, 2, 3] 2358 total += n 2359 endfor 2360 assert_equal(6, total) 2361 2362 var chars = '' 2363 for s: string in 'foobar' 2364 chars ..= s 2365 endfor 2366 assert_equal('foobar', chars) 2367 2368 # unpack with type 2369 var res = '' 2370 for [n: number, s: string] in [[1, 'a'], [2, 'b']] 2371 res ..= n .. s 2372 endfor 2373 assert_equal('1a2b', res) 2374 2375 # loop over string 2376 res = '' 2377 for c in 'aéc̀d' 2378 res ..= c .. '-' 2379 endfor 2380 assert_equal('a-é-c̀-d-', res) 2381 2382 res = '' 2383 for c in '' 2384 res ..= c .. '-' 2385 endfor 2386 assert_equal('', res) 2387 2388 res = '' 2389 for c in test_null_string() 2390 res ..= c .. '-' 2391 endfor 2392 assert_equal('', res) 2393 2394 var foo: list<dict<any>> = [ 2395 {a: 'Cat'} 2396 ] 2397 for dd in foo 2398 dd.counter = 12 2399 endfor 2400 assert_equal([{a: 'Cat', counter: 12}], foo) 2401 END 2402 CheckDefAndScriptSuccess(lines) 2403enddef 2404 2405def Test_for_loop_fails() 2406 CheckDefFailure(['for '], 'E1097:') 2407 CheckDefFailure(['for x'], 'E1097:') 2408 CheckDefFailure(['for x in'], 'E1097:') 2409 CheckDefFailure(['for # in range(5)'], 'E690:') 2410 CheckDefFailure(['for i In range(5)'], 'E690:') 2411 CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:') 2412 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:') 2413 delfunc! g:Func 2414 CheckDefFailure(['for i in xxx'], 'E1001:') 2415 CheckDefFailure(['endfor'], 'E588:') 2416 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:') 2417 2418 # wrong type detected at compile time 2419 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported') 2420 2421 # wrong type detected at runtime 2422 g:adict = {a: 1} 2423 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported') 2424 unlet g:adict 2425 2426 var lines =<< trim END 2427 var d: list<dict<any>> = [{a: 0}] 2428 for e in d 2429 e = {a: 0, b: ''} 2430 endfor 2431 END 2432 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3) 2433 2434 lines =<< trim END 2435 for nr: number in ['foo'] 2436 endfor 2437 END 2438 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1) 2439enddef 2440 2441def Test_for_loop_script_var() 2442 # cannot use s:var in a :def function 2443 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:') 2444 2445 # can use s:var in Vim9 script, with or without s: 2446 var lines =<< trim END 2447 vim9script 2448 var total = 0 2449 for s:var in [1, 2, 3] 2450 total += s:var 2451 endfor 2452 assert_equal(6, total) 2453 2454 total = 0 2455 for var in [1, 2, 3] 2456 total += var 2457 endfor 2458 assert_equal(6, total) 2459 END 2460enddef 2461 2462def Test_for_loop_unpack() 2463 var lines =<< trim END 2464 var result = [] 2465 for [v1, v2] in [[1, 2], [3, 4]] 2466 result->add(v1) 2467 result->add(v2) 2468 endfor 2469 assert_equal([1, 2, 3, 4], result) 2470 2471 result = [] 2472 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]] 2473 result->add(v1) 2474 result->add(v2) 2475 result->add(v3) 2476 endfor 2477 assert_equal([1, 2, [], 3, 4, [5, 6]], result) 2478 2479 result = [] 2480 for [&ts, &sw] in [[1, 2], [3, 4]] 2481 result->add(&ts) 2482 result->add(&sw) 2483 endfor 2484 assert_equal([1, 2, 3, 4], result) 2485 2486 var slist: list<string> 2487 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']] 2488 slist->add($LOOPVAR) 2489 slist->add(@r) 2490 slist->add(v:errmsg) 2491 endfor 2492 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist) 2493 2494 slist = [] 2495 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']] 2496 slist->add(g:globalvar) 2497 slist->add(b:bufvar) 2498 slist->add(w:winvar) 2499 slist->add(t:tabvar) 2500 endfor 2501 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist) 2502 unlet! g:globalvar b:bufvar w:winvar t:tabvar 2503 END 2504 CheckDefAndScriptSuccess(lines) 2505 2506 lines =<< trim END 2507 for [v1, v2] in [[1, 2, 3], [3, 4]] 2508 echo v1 v2 2509 endfor 2510 END 2511 CheckDefExecFailure(lines, 'E710:', 1) 2512 2513 lines =<< trim END 2514 for [v1, v2] in [[1], [3, 4]] 2515 echo v1 v2 2516 endfor 2517 END 2518 CheckDefExecFailure(lines, 'E711:', 1) 2519 2520 lines =<< trim END 2521 for [v1, v1] in [[1, 2], [3, 4]] 2522 echo v1 2523 endfor 2524 END 2525 CheckDefExecFailure(lines, 'E1017:', 1) 2526enddef 2527 2528def Test_for_loop_with_try_continue() 2529 var lines =<< trim END 2530 var looped = 0 2531 var cleanup = 0 2532 for i in range(3) 2533 looped += 1 2534 try 2535 eval [][0] 2536 catch 2537 continue 2538 finally 2539 cleanup += 1 2540 endtry 2541 endfor 2542 assert_equal(3, looped) 2543 assert_equal(3, cleanup) 2544 END 2545 CheckDefAndScriptSuccess(lines) 2546enddef 2547 2548def Test_while_loop() 2549 var result = '' 2550 var cnt = 0 2551 while cnt < 555 2552 if cnt == 3 2553 break 2554 endif 2555 cnt += 1 2556 if cnt == 2 2557 continue 2558 endif 2559 result ..= cnt .. '_' 2560 endwhile 2561 assert_equal('1_3_', result) 2562 2563 var s = '' 2564 while s == 'x' # {comment} 2565 endwhile 2566enddef 2567 2568def Test_while_loop_fails() 2569 CheckDefFailure(['while xxx'], 'E1001:') 2570 CheckDefFailure(['endwhile'], 'E588:') 2571 CheckDefFailure(['continue'], 'E586:') 2572 CheckDefFailure(['if true', 'continue'], 'E586:') 2573 CheckDefFailure(['break'], 'E587:') 2574 CheckDefFailure(['if true', 'break'], 'E587:') 2575 CheckDefFailure(['while 1', 'echo 3'], 'E170:') 2576 2577 var lines =<< trim END 2578 var s = '' 2579 while s = '' 2580 endwhile 2581 END 2582 CheckDefFailure(lines, 'E488:') 2583enddef 2584 2585def Test_interrupt_loop() 2586 var caught = false 2587 var x = 0 2588 try 2589 while 1 2590 x += 1 2591 if x == 100 2592 feedkeys("\<C-C>", 'Lt') 2593 endif 2594 endwhile 2595 catch 2596 caught = true 2597 assert_equal(100, x) 2598 endtry 2599 assert_true(caught, 'should have caught an exception') 2600 # consume the CTRL-C 2601 getchar(0) 2602enddef 2603 2604def Test_automatic_line_continuation() 2605 var mylist = [ 2606 'one', 2607 'two', 2608 'three', 2609 ] # comment 2610 assert_equal(['one', 'two', 'three'], mylist) 2611 2612 var mydict = { 2613 ['one']: 1, 2614 ['two']: 2, 2615 ['three']: 2616 3, 2617 } # comment 2618 assert_equal({one: 1, two: 2, three: 3}, mydict) 2619 mydict = { 2620 one: 1, # comment 2621 two: # comment 2622 2, # comment 2623 three: 3 # comment 2624 } 2625 assert_equal({one: 1, two: 2, three: 3}, mydict) 2626 mydict = { 2627 one: 1, 2628 two: 2629 2, 2630 three: 3 2631 } 2632 assert_equal({one: 1, two: 2, three: 3}, mydict) 2633 2634 assert_equal( 2635 ['one', 'two', 'three'], 2636 split('one two three') 2637 ) 2638enddef 2639 2640def Test_vim9_comment() 2641 CheckScriptSuccess([ 2642 'vim9script', 2643 '# something', 2644 '#something', 2645 '#{something', 2646 ]) 2647 2648 split Xfile 2649 CheckScriptSuccess([ 2650 'vim9script', 2651 'edit #something', 2652 ]) 2653 CheckScriptSuccess([ 2654 'vim9script', 2655 'edit #{something', 2656 ]) 2657 close 2658 2659 CheckScriptFailure([ 2660 'vim9script', 2661 ':# something', 2662 ], 'E488:') 2663 CheckScriptFailure([ 2664 '# something', 2665 ], 'E488:') 2666 CheckScriptFailure([ 2667 ':# something', 2668 ], 'E488:') 2669 2670 { # block start 2671 } # block end 2672 CheckDefFailure([ 2673 '{# comment', 2674 ], 'E488:') 2675 CheckDefFailure([ 2676 '{', 2677 '}# comment', 2678 ], 'E488:') 2679 2680 echo "yes" # comment 2681 CheckDefFailure([ 2682 'echo "yes"# comment', 2683 ], 'E488:') 2684 CheckScriptSuccess([ 2685 'vim9script', 2686 'echo "yes" # something', 2687 ]) 2688 CheckScriptFailure([ 2689 'vim9script', 2690 'echo "yes"# something', 2691 ], 'E121:') 2692 CheckScriptFailure([ 2693 'vim9script', 2694 'echo# something', 2695 ], 'E1144:') 2696 CheckScriptFailure([ 2697 'echo "yes" # something', 2698 ], 'E121:') 2699 2700 exe "echo" # comment 2701 CheckDefFailure([ 2702 'exe "echo"# comment', 2703 ], 'E488:') 2704 CheckScriptSuccess([ 2705 'vim9script', 2706 'exe "echo" # something', 2707 ]) 2708 CheckScriptFailure([ 2709 'vim9script', 2710 'exe "echo"# something', 2711 ], 'E121:') 2712 CheckScriptFailure([ 2713 'vim9script', 2714 'exe# something', 2715 ], 'E1144:') 2716 CheckScriptFailure([ 2717 'exe "echo" # something', 2718 ], 'E121:') 2719 2720 CheckDefFailure([ 2721 'try# comment', 2722 ' echo "yes"', 2723 'catch', 2724 'endtry', 2725 ], 'E1144:') 2726 CheckScriptFailure([ 2727 'vim9script', 2728 'try# comment', 2729 'echo "yes"', 2730 ], 'E1144:') 2731 CheckDefFailure([ 2732 'try', 2733 ' throw#comment', 2734 'catch', 2735 'endtry', 2736 ], 'E1144:') 2737 CheckDefFailure([ 2738 'try', 2739 ' throw "yes"#comment', 2740 'catch', 2741 'endtry', 2742 ], 'E488:') 2743 CheckDefFailure([ 2744 'try', 2745 ' echo "yes"', 2746 'catch# comment', 2747 'endtry', 2748 ], 'E1144:') 2749 CheckScriptFailure([ 2750 'vim9script', 2751 'try', 2752 ' echo "yes"', 2753 'catch# comment', 2754 'endtry', 2755 ], 'E1144:') 2756 CheckDefFailure([ 2757 'try', 2758 ' echo "yes"', 2759 'catch /pat/# comment', 2760 'endtry', 2761 ], 'E488:') 2762 CheckDefFailure([ 2763 'try', 2764 'echo "yes"', 2765 'catch', 2766 'endtry# comment', 2767 ], 'E1144:') 2768 CheckScriptFailure([ 2769 'vim9script', 2770 'try', 2771 ' echo "yes"', 2772 'catch', 2773 'endtry# comment', 2774 ], 'E1144:') 2775 2776 CheckScriptSuccess([ 2777 'vim9script', 2778 'hi # comment', 2779 ]) 2780 CheckScriptFailure([ 2781 'vim9script', 2782 'hi# comment', 2783 ], 'E1144:') 2784 CheckScriptSuccess([ 2785 'vim9script', 2786 'hi Search # comment', 2787 ]) 2788 CheckScriptFailure([ 2789 'vim9script', 2790 'hi Search# comment', 2791 ], 'E416:') 2792 CheckScriptSuccess([ 2793 'vim9script', 2794 'hi link This Search # comment', 2795 ]) 2796 CheckScriptFailure([ 2797 'vim9script', 2798 'hi link This That# comment', 2799 ], 'E413:') 2800 CheckScriptSuccess([ 2801 'vim9script', 2802 'hi clear This # comment', 2803 'hi clear # comment', 2804 ]) 2805 # not tested, because it doesn't give an error but a warning: 2806 # hi clear This# comment', 2807 CheckScriptFailure([ 2808 'vim9script', 2809 'hi clear# comment', 2810 ], 'E416:') 2811 2812 CheckScriptSuccess([ 2813 'vim9script', 2814 'hi Group term=bold', 2815 'match Group /todo/ # comment', 2816 ]) 2817 CheckScriptFailure([ 2818 'vim9script', 2819 'hi Group term=bold', 2820 'match Group /todo/# comment', 2821 ], 'E488:') 2822 CheckScriptSuccess([ 2823 'vim9script', 2824 'match # comment', 2825 ]) 2826 CheckScriptFailure([ 2827 'vim9script', 2828 'match# comment', 2829 ], 'E1144:') 2830 CheckScriptSuccess([ 2831 'vim9script', 2832 'match none # comment', 2833 ]) 2834 CheckScriptFailure([ 2835 'vim9script', 2836 'match none# comment', 2837 ], 'E475:') 2838 2839 CheckScriptSuccess([ 2840 'vim9script', 2841 'menutrans clear # comment', 2842 ]) 2843 CheckScriptFailure([ 2844 'vim9script', 2845 'menutrans clear# comment text', 2846 ], 'E474:') 2847 2848 CheckScriptSuccess([ 2849 'vim9script', 2850 'syntax clear # comment', 2851 ]) 2852 CheckScriptFailure([ 2853 'vim9script', 2854 'syntax clear# comment text', 2855 ], 'E28:') 2856 CheckScriptSuccess([ 2857 'vim9script', 2858 'syntax keyword Word some', 2859 'syntax clear Word # comment', 2860 ]) 2861 CheckScriptFailure([ 2862 'vim9script', 2863 'syntax keyword Word some', 2864 'syntax clear Word# comment text', 2865 ], 'E28:') 2866 2867 CheckScriptSuccess([ 2868 'vim9script', 2869 'syntax list # comment', 2870 ]) 2871 CheckScriptFailure([ 2872 'vim9script', 2873 'syntax list# comment text', 2874 ], 'E28:') 2875 2876 CheckScriptSuccess([ 2877 'vim9script', 2878 'syntax match Word /pat/ oneline # comment', 2879 ]) 2880 CheckScriptFailure([ 2881 'vim9script', 2882 'syntax match Word /pat/ oneline# comment', 2883 ], 'E475:') 2884 2885 CheckScriptSuccess([ 2886 'vim9script', 2887 'syntax keyword Word word # comm[ent', 2888 ]) 2889 CheckScriptFailure([ 2890 'vim9script', 2891 'syntax keyword Word word# comm[ent', 2892 ], 'E789:') 2893 2894 CheckScriptSuccess([ 2895 'vim9script', 2896 'syntax match Word /pat/ # comment', 2897 ]) 2898 CheckScriptFailure([ 2899 'vim9script', 2900 'syntax match Word /pat/# comment', 2901 ], 'E402:') 2902 2903 CheckScriptSuccess([ 2904 'vim9script', 2905 'syntax match Word /pat/ contains=Something # comment', 2906 ]) 2907 CheckScriptFailure([ 2908 'vim9script', 2909 'syntax match Word /pat/ contains=Something# comment', 2910 ], 'E475:') 2911 CheckScriptFailure([ 2912 'vim9script', 2913 'syntax match Word /pat/ contains= # comment', 2914 ], 'E406:') 2915 CheckScriptFailure([ 2916 'vim9script', 2917 'syntax match Word /pat/ contains=# comment', 2918 ], 'E475:') 2919 2920 CheckScriptSuccess([ 2921 'vim9script', 2922 'syntax region Word start=/pat/ end=/pat/ # comment', 2923 ]) 2924 CheckScriptFailure([ 2925 'vim9script', 2926 'syntax region Word start=/pat/ end=/pat/# comment', 2927 ], 'E402:') 2928 2929 CheckScriptSuccess([ 2930 'vim9script', 2931 'syntax sync # comment', 2932 ]) 2933 CheckScriptFailure([ 2934 'vim9script', 2935 'syntax sync# comment', 2936 ], 'E404:') 2937 CheckScriptSuccess([ 2938 'vim9script', 2939 'syntax sync ccomment # comment', 2940 ]) 2941 CheckScriptFailure([ 2942 'vim9script', 2943 'syntax sync ccomment# comment', 2944 ], 'E404:') 2945 2946 CheckScriptSuccess([ 2947 'vim9script', 2948 'syntax cluster Some contains=Word # comment', 2949 ]) 2950 CheckScriptFailure([ 2951 'vim9script', 2952 'syntax cluster Some contains=Word# comment', 2953 ], 'E475:') 2954 2955 CheckScriptSuccess([ 2956 'vim9script', 2957 'command Echo echo # comment', 2958 'command Echo # comment', 2959 'delcommand Echo', 2960 ]) 2961 CheckScriptFailure([ 2962 'vim9script', 2963 'command Echo echo# comment', 2964 'Echo', 2965 ], 'E1144:') 2966 delcommand Echo 2967 2968 var curdir = getcwd() 2969 CheckScriptSuccess([ 2970 'command Echo cd " comment', 2971 'Echo', 2972 'delcommand Echo', 2973 ]) 2974 CheckScriptSuccess([ 2975 'vim9script', 2976 'command Echo cd # comment', 2977 'Echo', 2978 'delcommand Echo', 2979 ]) 2980 CheckScriptFailure([ 2981 'vim9script', 2982 'command Echo cd " comment', 2983 'Echo', 2984 ], 'E344:') 2985 delcommand Echo 2986 chdir(curdir) 2987 2988 CheckScriptFailure([ 2989 'vim9script', 2990 'command Echo# comment', 2991 ], 'E182:') 2992 CheckScriptFailure([ 2993 'vim9script', 2994 'command Echo echo', 2995 'command Echo# comment', 2996 ], 'E182:') 2997 delcommand Echo 2998 2999 CheckScriptSuccess([ 3000 'vim9script', 3001 'function # comment', 3002 ]) 3003 CheckScriptFailure([ 3004 'vim9script', 3005 'function " comment', 3006 ], 'E129:') 3007 CheckScriptFailure([ 3008 'vim9script', 3009 'function# comment', 3010 ], 'E1144:') 3011 CheckScriptSuccess([ 3012 'vim9script', 3013 'function CheckScriptSuccess # comment', 3014 ]) 3015 CheckScriptFailure([ 3016 'vim9script', 3017 'function CheckScriptSuccess# comment', 3018 ], 'E488:') 3019 3020 CheckScriptSuccess([ 3021 'vim9script', 3022 'func g:DeleteMeA()', 3023 'endfunc', 3024 'delfunction g:DeleteMeA # comment', 3025 ]) 3026 CheckScriptFailure([ 3027 'vim9script', 3028 'func g:DeleteMeB()', 3029 'endfunc', 3030 'delfunction g:DeleteMeB# comment', 3031 ], 'E488:') 3032 3033 CheckScriptSuccess([ 3034 'vim9script', 3035 'call execute("ls") # comment', 3036 ]) 3037 CheckScriptFailure([ 3038 'vim9script', 3039 'call execute("ls")# comment', 3040 ], 'E488:') 3041 3042 CheckScriptFailure([ 3043 'def Test() " comment', 3044 'enddef', 3045 ], 'E488:') 3046 CheckScriptFailure([ 3047 'vim9script', 3048 'def Test() " comment', 3049 'enddef', 3050 ], 'E488:') 3051 3052 CheckScriptSuccess([ 3053 'func Test() " comment', 3054 'endfunc', 3055 'delfunc Test', 3056 ]) 3057 CheckScriptSuccess([ 3058 'vim9script', 3059 'func Test() " comment', 3060 'endfunc', 3061 ]) 3062 3063 CheckScriptSuccess([ 3064 'def Test() # comment', 3065 'enddef', 3066 ]) 3067 CheckScriptFailure([ 3068 'func Test() # comment', 3069 'endfunc', 3070 ], 'E488:') 3071enddef 3072 3073def Test_vim9_comment_gui() 3074 CheckCanRunGui 3075 3076 CheckScriptFailure([ 3077 'vim9script', 3078 'gui#comment' 3079 ], 'E1144:') 3080 CheckScriptFailure([ 3081 'vim9script', 3082 'gui -f#comment' 3083 ], 'E499:') 3084enddef 3085 3086def Test_vim9_comment_not_compiled() 3087 au TabEnter *.vim g:entered = 1 3088 au TabEnter *.x g:entered = 2 3089 3090 edit test.vim 3091 doautocmd TabEnter #comment 3092 assert_equal(1, g:entered) 3093 3094 doautocmd TabEnter f.x 3095 assert_equal(2, g:entered) 3096 3097 g:entered = 0 3098 doautocmd TabEnter f.x #comment 3099 assert_equal(2, g:entered) 3100 3101 assert_fails('doautocmd Syntax#comment', 'E216:') 3102 3103 au! TabEnter 3104 unlet g:entered 3105 3106 CheckScriptSuccess([ 3107 'vim9script', 3108 'g:var = 123', 3109 'b:var = 456', 3110 'w:var = 777', 3111 't:var = 888', 3112 'unlet g:var w:var # something', 3113 ]) 3114 3115 CheckScriptFailure([ 3116 'vim9script', 3117 'let var = 123', 3118 ], 'E1126: Cannot use :let in Vim9 script') 3119 3120 CheckScriptFailure([ 3121 'vim9script', 3122 'var g:var = 123', 3123 ], 'E1016: Cannot declare a global variable:') 3124 3125 CheckScriptFailure([ 3126 'vim9script', 3127 'var b:var = 123', 3128 ], 'E1016: Cannot declare a buffer variable:') 3129 3130 CheckScriptFailure([ 3131 'vim9script', 3132 'var w:var = 123', 3133 ], 'E1016: Cannot declare a window variable:') 3134 3135 CheckScriptFailure([ 3136 'vim9script', 3137 'var t:var = 123', 3138 ], 'E1016: Cannot declare a tab variable:') 3139 3140 CheckScriptFailure([ 3141 'vim9script', 3142 'var v:version = 123', 3143 ], 'E1016: Cannot declare a v: variable:') 3144 3145 CheckScriptFailure([ 3146 'vim9script', 3147 'var $VARIABLE = "text"', 3148 ], 'E1016: Cannot declare an environment variable:') 3149 3150 CheckScriptFailure([ 3151 'vim9script', 3152 'g:var = 123', 3153 'unlet g:var# comment1', 3154 ], 'E108:') 3155 3156 CheckScriptFailure([ 3157 'let g:var = 123', 3158 'unlet g:var # something', 3159 ], 'E488:') 3160 3161 CheckScriptSuccess([ 3162 'vim9script', 3163 'if 1 # comment2', 3164 ' echo "yes"', 3165 'elseif 2 #comment', 3166 ' echo "no"', 3167 'endif', 3168 ]) 3169 3170 CheckScriptFailure([ 3171 'vim9script', 3172 'if 1# comment3', 3173 ' echo "yes"', 3174 'endif', 3175 ], 'E15:') 3176 3177 CheckScriptFailure([ 3178 'vim9script', 3179 'if 0 # comment4', 3180 ' echo "yes"', 3181 'elseif 2#comment', 3182 ' echo "no"', 3183 'endif', 3184 ], 'E15:') 3185 3186 CheckScriptSuccess([ 3187 'vim9script', 3188 'var v = 1 # comment5', 3189 ]) 3190 3191 CheckScriptFailure([ 3192 'vim9script', 3193 'var v = 1# comment6', 3194 ], 'E15:') 3195 3196 CheckScriptSuccess([ 3197 'vim9script', 3198 'new' 3199 'setline(1, ["# define pat", "last"])', 3200 ':$', 3201 'dsearch /pat/ #comment', 3202 'bwipe!', 3203 ]) 3204 3205 CheckScriptFailure([ 3206 'vim9script', 3207 'new' 3208 'setline(1, ["# define pat", "last"])', 3209 ':$', 3210 'dsearch /pat/#comment', 3211 'bwipe!', 3212 ], 'E488:') 3213 3214 CheckScriptFailure([ 3215 'vim9script', 3216 'func! SomeFunc()', 3217 ], 'E477:') 3218enddef 3219 3220def Test_finish() 3221 var lines =<< trim END 3222 vim9script 3223 g:res = 'one' 3224 if v:false | finish | endif 3225 g:res = 'two' 3226 finish 3227 g:res = 'three' 3228 END 3229 writefile(lines, 'Xfinished') 3230 source Xfinished 3231 assert_equal('two', g:res) 3232 3233 unlet g:res 3234 delete('Xfinished') 3235enddef 3236 3237def Test_forward_declaration() 3238 var lines =<< trim END 3239 vim9script 3240 def GetValue(): string 3241 return theVal 3242 enddef 3243 var theVal = 'something' 3244 g:initVal = GetValue() 3245 theVal = 'else' 3246 g:laterVal = GetValue() 3247 END 3248 writefile(lines, 'Xforward') 3249 source Xforward 3250 assert_equal('something', g:initVal) 3251 assert_equal('else', g:laterVal) 3252 3253 unlet g:initVal 3254 unlet g:laterVal 3255 delete('Xforward') 3256enddef 3257 3258def Test_source_vim9_from_legacy() 3259 var vim9_lines =<< trim END 3260 vim9script 3261 var local = 'local' 3262 g:global = 'global' 3263 export var exported = 'exported' 3264 export def GetText(): string 3265 return 'text' 3266 enddef 3267 END 3268 writefile(vim9_lines, 'Xvim9_script.vim') 3269 3270 var legacy_lines =<< trim END 3271 source Xvim9_script.vim 3272 3273 call assert_false(exists('local')) 3274 call assert_false(exists('exported')) 3275 call assert_false(exists('s:exported')) 3276 call assert_equal('global', global) 3277 call assert_equal('global', g:global) 3278 3279 " imported variable becomes script-local 3280 import exported from './Xvim9_script.vim' 3281 call assert_equal('exported', s:exported) 3282 call assert_false(exists('exported')) 3283 3284 " imported function becomes script-local 3285 import GetText from './Xvim9_script.vim' 3286 call assert_equal('text', s:GetText()) 3287 call assert_false(exists('*GetText')) 3288 END 3289 writefile(legacy_lines, 'Xlegacy_script.vim') 3290 3291 source Xlegacy_script.vim 3292 assert_equal('global', g:global) 3293 unlet g:global 3294 3295 delete('Xlegacy_script.vim') 3296 delete('Xvim9_script.vim') 3297enddef 3298 3299def Test_declare_script_in_func() 3300 var lines =<< trim END 3301 vim9script 3302 func Declare() 3303 let s:local = 123 3304 endfunc 3305 Declare() 3306 assert_equal(123, local) 3307 3308 var error: string 3309 try 3310 local = 'asdf' 3311 catch 3312 error = v:exception 3313 endtry 3314 assert_match('E1012: Type mismatch; expected number but got string', error) 3315 3316 lockvar local 3317 try 3318 local = 999 3319 catch 3320 error = v:exception 3321 endtry 3322 assert_match('E741: Value is locked: local', error) 3323 END 3324 CheckScriptSuccess(lines) 3325enddef 3326 3327 3328func Test_vim9script_not_global() 3329 " check that items defined in Vim9 script are script-local, not global 3330 let vim9lines =<< trim END 3331 vim9script 3332 var name = 'local' 3333 func TheFunc() 3334 echo 'local' 3335 endfunc 3336 def DefFunc() 3337 echo 'local' 3338 enddef 3339 END 3340 call writefile(vim9lines, 'Xvim9script.vim') 3341 source Xvim9script.vim 3342 try 3343 echo g:var 3344 assert_report('did not fail') 3345 catch /E121:/ 3346 " caught 3347 endtry 3348 try 3349 call TheFunc() 3350 assert_report('did not fail') 3351 catch /E117:/ 3352 " caught 3353 endtry 3354 try 3355 call DefFunc() 3356 assert_report('did not fail') 3357 catch /E117:/ 3358 " caught 3359 endtry 3360 3361 call delete('Xvim9script.vim') 3362endfunc 3363 3364def Test_vim9_copen() 3365 # this was giving an error for setting w:quickfix_title 3366 copen 3367 quit 3368enddef 3369 3370" test using an auto-loaded function and variable 3371def Test_vim9_autoload() 3372 var lines =<< trim END 3373 vim9script 3374 def some#gettest(): string 3375 return 'test' 3376 enddef 3377 g:some#name = 'name' 3378 g:some#dict = {key: 'value'} 3379 3380 def some#varargs(a1: string, ...l: list<string>): string 3381 return a1 .. l[0] .. l[1] 3382 enddef 3383 END 3384 3385 mkdir('Xdir/autoload', 'p') 3386 writefile(lines, 'Xdir/autoload/some.vim') 3387 var save_rtp = &rtp 3388 exe 'set rtp^=' .. getcwd() .. '/Xdir' 3389 3390 assert_equal('test', g:some#gettest()) 3391 assert_equal('name', g:some#name) 3392 assert_equal('value', g:some#dict.key) 3393 g:some#other = 'other' 3394 assert_equal('other', g:some#other) 3395 3396 assert_equal('abc', some#varargs('a', 'b', 'c')) 3397 3398 # upper case script name works 3399 lines =<< trim END 3400 vim9script 3401 def Other#getOther(): string 3402 return 'other' 3403 enddef 3404 END 3405 writefile(lines, 'Xdir/autoload/Other.vim') 3406 assert_equal('other', g:Other#getOther()) 3407 3408 delete('Xdir', 'rf') 3409 &rtp = save_rtp 3410enddef 3411 3412" test using a vim9script that is auto-loaded from an autocmd 3413def Test_vim9_aucmd_autoload() 3414 var lines =<< trim END 3415 vim9script 3416 def foo#test() 3417 echomsg getreg('"') 3418 enddef 3419 END 3420 3421 mkdir('Xdir/autoload', 'p') 3422 writefile(lines, 'Xdir/autoload/foo.vim') 3423 var save_rtp = &rtp 3424 exe 'set rtp^=' .. getcwd() .. '/Xdir' 3425 augroup test 3426 autocmd TextYankPost * call foo#test() 3427 augroup END 3428 3429 normal Y 3430 3431 augroup test 3432 autocmd! 3433 augroup END 3434 delete('Xdir', 'rf') 3435 &rtp = save_rtp 3436enddef 3437 3438" This was causing a crash because suppress_errthrow wasn't reset. 3439def Test_vim9_autoload_error() 3440 var lines =<< trim END 3441 vim9script 3442 def crash#func() 3443 try 3444 for x in List() 3445 endfor 3446 catch 3447 endtry 3448 g:ok = true 3449 enddef 3450 fu List() 3451 invalid 3452 endfu 3453 try 3454 alsoinvalid 3455 catch /wontmatch/ 3456 endtry 3457 END 3458 call mkdir('Xruntime/autoload', 'p') 3459 call writefile(lines, 'Xruntime/autoload/crash.vim') 3460 3461 # run in a separate Vim to avoid the side effects of assert_fails() 3462 lines =<< trim END 3463 exe 'set rtp^=' .. getcwd() .. '/Xruntime' 3464 call crash#func() 3465 call writefile(['ok'], 'Xdidit') 3466 qall! 3467 END 3468 writefile(lines, 'Xscript') 3469 RunVim([], [], '-S Xscript') 3470 assert_equal(['ok'], readfile('Xdidit')) 3471 3472 delete('Xdidit') 3473 delete('Xscript') 3474 delete('Xruntime', 'rf') 3475 3476 lines =<< trim END 3477 vim9script 3478 var foo#bar = 'asdf' 3479 END 3480 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2) 3481enddef 3482 3483def Test_script_var_in_autocmd() 3484 # using a script variable from an autocommand, defined in a :def function in a 3485 # legacy Vim script, cannot check the variable type. 3486 var lines =<< trim END 3487 let s:counter = 1 3488 def s:Func() 3489 au! CursorHold 3490 au CursorHold * s:counter += 1 3491 enddef 3492 call s:Func() 3493 doau CursorHold 3494 call assert_equal(2, s:counter) 3495 au! CursorHold 3496 END 3497 CheckScriptSuccess(lines) 3498enddef 3499 3500def Test_cmdline_win() 3501 # if the Vim syntax highlighting uses Vim9 constructs they can be used from 3502 # the command line window. 3503 mkdir('rtp/syntax', 'p') 3504 var export_lines =<< trim END 3505 vim9script 3506 export var That = 'yes' 3507 END 3508 writefile(export_lines, 'rtp/syntax/Xexport.vim') 3509 var import_lines =<< trim END 3510 vim9script 3511 import That from './Xexport.vim' 3512 END 3513 writefile(import_lines, 'rtp/syntax/vim.vim') 3514 var save_rtp = &rtp 3515 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp 3516 syntax on 3517 augroup CmdWin 3518 autocmd CmdwinEnter * g:got_there = 'yes' 3519 augroup END 3520 # this will open and also close the cmdline window 3521 feedkeys('q:', 'xt') 3522 assert_equal('yes', g:got_there) 3523 3524 augroup CmdWin 3525 au! 3526 augroup END 3527 &rtp = save_rtp 3528 delete('rtp', 'rf') 3529enddef 3530 3531def Test_invalid_sid() 3532 assert_fails('func <SNR>1234_func', 'E123:') 3533 3534 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"') 3535 assert_equal([], readfile('Xdidit')) 3536 endif 3537 delete('Xdidit') 3538enddef 3539 3540def Test_restoring_cpo() 3541 writefile(['vim9script', 'set nocp'], 'Xsourced') 3542 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose') 3543 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose') 3544 assert_equal(['done'], readfile('Xdone')) 3545 endif 3546 delete('Xsourced') 3547 delete('Xclose') 3548 delete('Xdone') 3549 3550 writefile(['vim9script'], 'XanotherScript') 3551 set cpo=aABceFsMny> 3552 edit XanotherScript 3553 so % 3554 assert_equal('aABceFsMny>', &cpo) 3555 :1del 3556 w 3557 so % 3558 assert_equal('aABceFsMny>', &cpo) 3559 3560 delete('XanotherScript') 3561 set cpo&vim 3562enddef 3563 3564" Use :function so we can use Check commands 3565func Test_no_redraw_when_restoring_cpo() 3566 CheckScreendump 3567 CheckFeature timers 3568 3569 let lines =<< trim END 3570 vim9script 3571 def script#func() 3572 enddef 3573 END 3574 call mkdir('Xdir/autoload', 'p') 3575 call writefile(lines, 'Xdir/autoload/script.vim') 3576 3577 let lines =<< trim END 3578 vim9script 3579 set cpo+=M 3580 exe 'set rtp^=' .. getcwd() .. '/Xdir' 3581 au CmdlineEnter : ++once timer_start(0, (_) => script#func()) 3582 setline(1, 'some text') 3583 END 3584 call writefile(lines, 'XTest_redraw_cpo') 3585 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6}) 3586 call term_sendkeys(buf, "V:") 3587 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {}) 3588 3589 " clean up 3590 call term_sendkeys(buf, "\<Esc>u") 3591 call StopVimInTerminal(buf) 3592 call delete('XTest_redraw_cpo') 3593 call delete('Xdir', 'rf') 3594endfunc 3595 3596 3597def Test_unset_any_variable() 3598 var lines =<< trim END 3599 var name: any 3600 assert_equal(0, name) 3601 END 3602 CheckDefAndScriptSuccess(lines) 3603enddef 3604 3605func Test_define_func_at_command_line() 3606 CheckRunVimInTerminal 3607 3608 " call indirectly to avoid compilation error for missing functions 3609 call Run_Test_define_func_at_command_line() 3610endfunc 3611 3612def Run_Test_define_func_at_command_line() 3613 # run in a separate Vim instance to avoid the script context 3614 var lines =<< trim END 3615 func CheckAndQuit() 3616 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc') 3617 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd') 3618 endfunc 3619 END 3620 writefile([''], 'Xdidcmd') 3621 writefile(lines, 'XcallFunc') 3622 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6}) 3623 # define Afunc() on the command line 3624 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>") 3625 term_sendkeys(buf, ":call CheckAndQuit()\<CR>") 3626 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd'))) 3627 3628 call StopVimInTerminal(buf) 3629 delete('XcallFunc') 3630 delete('Xdidcmd') 3631enddef 3632 3633def Test_script_var_scope() 3634 var lines =<< trim END 3635 vim9script 3636 if true 3637 if true 3638 var one = 'one' 3639 echo one 3640 endif 3641 echo one 3642 endif 3643 END 3644 CheckScriptFailure(lines, 'E121:', 7) 3645 3646 lines =<< trim END 3647 vim9script 3648 if true 3649 if false 3650 var one = 'one' 3651 echo one 3652 else 3653 var one = 'one' 3654 echo one 3655 endif 3656 echo one 3657 endif 3658 END 3659 CheckScriptFailure(lines, 'E121:', 10) 3660 3661 lines =<< trim END 3662 vim9script 3663 while true 3664 var one = 'one' 3665 echo one 3666 break 3667 endwhile 3668 echo one 3669 END 3670 CheckScriptFailure(lines, 'E121:', 7) 3671 3672 lines =<< trim END 3673 vim9script 3674 for i in range(1) 3675 var one = 'one' 3676 echo one 3677 endfor 3678 echo one 3679 END 3680 CheckScriptFailure(lines, 'E121:', 6) 3681 3682 lines =<< trim END 3683 vim9script 3684 { 3685 var one = 'one' 3686 assert_equal('one', one) 3687 } 3688 assert_false(exists('one')) 3689 assert_false(exists('s:one')) 3690 END 3691 CheckScriptSuccess(lines) 3692 3693 lines =<< trim END 3694 vim9script 3695 { 3696 var one = 'one' 3697 echo one 3698 } 3699 echo one 3700 END 3701 CheckScriptFailure(lines, 'E121:', 6) 3702enddef 3703 3704def Test_catch_exception_in_callback() 3705 var lines =<< trim END 3706 vim9script 3707 def Callback(...l: list<any>) 3708 try 3709 var x: string 3710 var y: string 3711 # this error should be caught with CHECKLEN 3712 [x, y] = [''] 3713 catch 3714 g:caught = 'yes' 3715 endtry 3716 enddef 3717 popup_menu('popup', {callback: Callback}) 3718 feedkeys("\r", 'xt') 3719 END 3720 CheckScriptSuccess(lines) 3721 3722 unlet g:caught 3723enddef 3724 3725def Test_no_unknown_error_after_error() 3726 if !has('unix') || !has('job') 3727 throw 'Skipped: not unix of missing +job feature' 3728 endif 3729 var lines =<< trim END 3730 vim9script 3731 var source: list<number> 3732 def Out_cb(...l: list<any>) 3733 eval [][0] 3734 enddef 3735 def Exit_cb(...l: list<any>) 3736 sleep 1m 3737 source += l 3738 enddef 3739 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'}) 3740 while job_status(myjob) == 'run' 3741 sleep 10m 3742 endwhile 3743 # wait for Exit_cb() to be called 3744 sleep 200m 3745 END 3746 writefile(lines, 'Xdef') 3747 assert_fails('so Xdef', ['E684:', 'E1012:']) 3748 delete('Xdef') 3749enddef 3750 3751def InvokeNormal() 3752 exe "norm! :m+1\r" 3753enddef 3754 3755def Test_invoke_normal_in_visual_mode() 3756 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR> 3757 new 3758 setline(1, ['aaa', 'bbb']) 3759 feedkeys("V\<F3>", 'xt') 3760 assert_equal(['bbb', 'aaa'], getline(1, 2)) 3761 xunmap <F3> 3762enddef 3763 3764def Test_white_space_after_command() 3765 var lines =<< trim END 3766 exit_cb: Func}) 3767 END 3768 CheckDefAndScriptFailure(lines, 'E1144:', 1) 3769 3770 lines =<< trim END 3771 e# 3772 END 3773 CheckDefAndScriptFailure(lines, 'E1144:', 1) 3774enddef 3775 3776def Test_script_var_gone_when_sourced_twice() 3777 var lines =<< trim END 3778 vim9script 3779 if exists('g:guard') 3780 finish 3781 endif 3782 g:guard = 1 3783 var name = 'thename' 3784 def g:GetName(): string 3785 return name 3786 enddef 3787 def g:SetName(arg: string) 3788 name = arg 3789 enddef 3790 END 3791 writefile(lines, 'XscriptTwice.vim') 3792 so XscriptTwice.vim 3793 assert_equal('thename', g:GetName()) 3794 g:SetName('newname') 3795 assert_equal('newname', g:GetName()) 3796 so XscriptTwice.vim 3797 assert_fails('call g:GetName()', 'E1149:') 3798 assert_fails('call g:SetName("x")', 'E1149:') 3799 3800 delfunc g:GetName 3801 delfunc g:SetName 3802 delete('XscriptTwice.vim') 3803 unlet g:guard 3804enddef 3805 3806def Test_import_gone_when_sourced_twice() 3807 var exportlines =<< trim END 3808 vim9script 3809 if exists('g:guard') 3810 finish 3811 endif 3812 g:guard = 1 3813 export var name = 'someName' 3814 END 3815 writefile(exportlines, 'XexportScript.vim') 3816 3817 var lines =<< trim END 3818 vim9script 3819 import name from './XexportScript.vim' 3820 def g:GetName(): string 3821 return name 3822 enddef 3823 END 3824 writefile(lines, 'XscriptImport.vim') 3825 so XscriptImport.vim 3826 assert_equal('someName', g:GetName()) 3827 3828 so XexportScript.vim 3829 assert_fails('call g:GetName()', 'E1149:') 3830 3831 delfunc g:GetName 3832 delete('XexportScript.vim') 3833 delete('XscriptImport.vim') 3834 unlet g:guard 3835enddef 3836 3837def Test_unsupported_commands() 3838 var lines =<< trim END 3839 ka 3840 END 3841 CheckDefAndScriptFailure(lines, 'E1100:') 3842 3843 lines =<< trim END 3844 :1ka 3845 END 3846 CheckDefAndScriptFailure(lines, 'E481:') 3847 3848 lines =<< trim END 3849 t 3850 END 3851 CheckDefFailure(lines, 'E1100:') 3852 CheckScriptFailure(['vim9script'] + lines, 'E1100:') 3853 3854 lines =<< trim END 3855 x 3856 END 3857 CheckDefFailure(lines, 'E1100:') 3858 CheckScriptFailure(['vim9script'] + lines, 'E1100:') 3859 3860 lines =<< trim END 3861 xit 3862 END 3863 CheckDefFailure(lines, 'E1100:') 3864 CheckScriptFailure(['vim9script'] + lines, 'E1100:') 3865enddef 3866 3867def Test_mapping_line_number() 3868 var lines =<< trim END 3869 vim9script 3870 def g:FuncA() 3871 # Some comment 3872 FuncB(0) 3873 enddef 3874 # Some comment 3875 def FuncB( 3876 # Some comment 3877 n: number 3878 ) 3879 exe 'nno ' 3880 # Some comment 3881 .. '<F3> a' 3882 .. 'b' 3883 .. 'c' 3884 enddef 3885 END 3886 CheckScriptSuccess(lines) 3887 var res = execute('verbose nmap <F3>') 3888 assert_match('No mapping found', res) 3889 3890 g:FuncA() 3891 res = execute('verbose nmap <F3>') 3892 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res) 3893 3894 nunmap <F3> 3895 delfunc g:FuncA 3896enddef 3897 3898" Keep this last, it messes up highlighting. 3899def Test_substitute_cmd() 3900 new 3901 setline(1, 'something') 3902 :substitute(some(other( 3903 assert_equal('otherthing', getline(1)) 3904 bwipe! 3905 3906 # also when the context is Vim9 script 3907 var lines =<< trim END 3908 vim9script 3909 new 3910 setline(1, 'something') 3911 :substitute(some(other( 3912 assert_equal('otherthing', getline(1)) 3913 bwipe! 3914 END 3915 writefile(lines, 'Xvim9lines') 3916 source Xvim9lines 3917 3918 delete('Xvim9lines') 3919enddef 3920 3921" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 3922