1" Test commands that are not compiled in a :def function 2 3source check.vim 4source vim9.vim 5source term_util.vim 6source view_util.vim 7 8def Test_vim9cmd() 9 var lines =<< trim END 10 vim9cmd var x = 123 11 let s:y = 'yes' 12 vim9c assert_equal(123, x) 13 vim9cm assert_equal('yes', y) 14 END 15 CheckScriptSuccess(lines) 16 assert_fails('vim9cmd', 'E1164:') 17enddef 18 19def Test_edit_wildcards() 20 var filename = 'Xtest' 21 edit `=filename` 22 assert_equal('Xtest', bufname()) 23 24 var filenr = 123 25 edit Xtest`=filenr` 26 assert_equal('Xtest123', bufname()) 27 28 filenr = 77 29 edit `=filename``=filenr` 30 assert_equal('Xtest77', bufname()) 31 32 edit X`=filename`xx`=filenr`yy 33 assert_equal('XXtestxx77yy', bufname()) 34 35 CheckDefFailure(['edit `=xxx`'], 'E1001:') 36 CheckDefFailure(['edit `="foo"'], 'E1083:') 37 38 var files = ['file 1', 'file%2', 'file# 3'] 39 args `=files` 40 assert_equal(files, argv()) 41enddef 42 43def Test_expand_alternate_file() 44 var lines =<< trim END 45 edit Xfileone 46 var bone = bufnr() 47 edit Xfiletwo 48 var btwo = bufnr() 49 edit Xfilethree 50 var bthree = bufnr() 51 52 edit # 53 assert_equal(bthree, bufnr()) 54 edit %% 55 assert_equal(btwo, bufnr()) 56 edit %% # comment 57 assert_equal(bthree, bufnr()) 58 edit %%yy 59 assert_equal('Xfiletwoyy', bufname()) 60 61 exe "edit %%" .. bone 62 assert_equal(bone, bufnr()) 63 exe "edit %%" .. btwo .. "xx" 64 assert_equal('Xfiletwoxx', bufname()) 65 66 next Xfileone Xfiletwo Xfilethree 67 assert_equal('Xfileone', argv(0)) 68 assert_equal('Xfiletwo', argv(1)) 69 assert_equal('Xfilethree', argv(2)) 70 next %%%zz 71 assert_equal('Xfileone', argv(0)) 72 assert_equal('Xfiletwo', argv(1)) 73 assert_equal('Xfilethreezz', argv(2)) 74 75 v:oldfiles = ['Xonefile', 'Xtwofile'] 76 edit %%<1 77 assert_equal('Xonefile', bufname()) 78 edit %%<2 79 assert_equal('Xtwofile', bufname()) 80 assert_fails('edit %%<3', 'E684:') 81 82 edit Xfileone.vim 83 edit Xfiletwo 84 edit %%:r 85 assert_equal('Xfileone', bufname()) 86 87 assert_false(bufexists('altfoo')) 88 edit altfoo 89 edit bar 90 assert_true(bufexists('altfoo')) 91 assert_true(buflisted('altfoo')) 92 bdel %% 93 assert_true(bufexists('altfoo')) 94 assert_false(buflisted('altfoo')) 95 bwipe! altfoo 96 bwipe! bar 97 END 98 CheckDefAndScriptSuccess(lines) 99enddef 100 101def Test_global_backtick_expansion() 102 new 103 setline(1, 'xx') 104 var name = 'foobar' 105 g/^xx/s/.*/`=name` 106 assert_equal('foobar', getline(1)) 107 bwipe! 108enddef 109 110def Test_folddo_backtick_expansion() 111 new 112 var name = 'xxx' 113 folddoopen edit `=name` 114 assert_equal('xxx', bufname()) 115 bwipe! 116 117 new 118 setline(1, ['one', 'two']) 119 set nomodified 120 :1,2fold 121 foldclose 122 folddoclose edit `=name` 123 assert_equal('xxx', bufname()) 124 bwipe! 125enddef 126 127def Test_hardcopy_wildcards() 128 CheckUnix 129 CheckFeature postscript 130 131 var outfile = 'print' 132 hardcopy > X`=outfile`.ps 133 assert_true(filereadable('Xprint.ps')) 134 135 delete('Xprint.ps') 136enddef 137 138def Test_syn_include_wildcards() 139 writefile(['syn keyword Found found'], 'Xthemine.vim') 140 var save_rtp = &rtp 141 &rtp = '.' 142 143 var fname = 'mine' 144 syn include @Group Xthe`=fname`.vim 145 assert_match('Found.* contained found', execute('syn list Found')) 146 147 &rtp = save_rtp 148 delete('Xthemine.vim') 149enddef 150 151def Test_echo_linebreak() 152 var lines =<< trim END 153 vim9script 154 redir @a 155 echo 'one' 156 .. 'two' 157 redir END 158 assert_equal("\nonetwo", @a) 159 END 160 CheckScriptSuccess(lines) 161 162 lines =<< trim END 163 vim9script 164 redir @a 165 echo 11 + 166 77 167 - 22 168 redir END 169 assert_equal("\n66", @a) 170 END 171 CheckScriptSuccess(lines) 172enddef 173 174def Test_condition_types() 175 var lines =<< trim END 176 if 'text' 177 endif 178 END 179 CheckDefAndScriptFailure(lines, 'E1135:', 1) 180 181 lines =<< trim END 182 if [1] 183 endif 184 END 185 CheckDefFailure(lines, 'E1012:', 1) 186 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2) 187 188 lines =<< trim END 189 g:cond = 'text' 190 if g:cond 191 endif 192 END 193 CheckDefExecAndScriptFailure(lines, 'E1135:', 2) 194 195 lines =<< trim END 196 g:cond = 0 197 if g:cond 198 elseif 'text' 199 endif 200 END 201 CheckDefFailure(lines, 'E1012:', 3) 202 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4) 203 204 lines =<< trim END 205 if g:cond 206 elseif [1] 207 endif 208 END 209 CheckDefFailure(lines, 'E1012:', 2) 210 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3) 211 212 lines =<< trim END 213 g:cond = 'text' 214 if 0 215 elseif g:cond 216 endif 217 END 218 CheckDefExecAndScriptFailure(lines, 'E1135:', 3) 219 220 lines =<< trim END 221 while 'text' 222 endwhile 223 END 224 CheckDefFailure(lines, 'E1012:', 1) 225 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2) 226 227 lines =<< trim END 228 while [1] 229 endwhile 230 END 231 CheckDefFailure(lines, 'E1012:', 1) 232 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2) 233 234 lines =<< trim END 235 g:cond = 'text' 236 while g:cond 237 endwhile 238 END 239 CheckDefExecAndScriptFailure(lines, 'E1135:', 2) 240enddef 241 242def Test_if_linebreak() 243 var lines =<< trim END 244 vim9script 245 if 1 && 246 true 247 || 1 248 g:res = 42 249 endif 250 assert_equal(42, g:res) 251 END 252 CheckScriptSuccess(lines) 253 unlet g:res 254 255 lines =<< trim END 256 vim9script 257 if 1 && 258 0 259 g:res = 0 260 elseif 0 || 261 0 262 || 1 263 g:res = 12 264 endif 265 assert_equal(12, g:res) 266 END 267 CheckScriptSuccess(lines) 268 unlet g:res 269enddef 270 271def Test_while_linebreak() 272 var lines =<< trim END 273 vim9script 274 var nr = 0 275 while nr < 276 10 + 3 277 nr = nr 278 + 4 279 endwhile 280 assert_equal(16, nr) 281 END 282 CheckScriptSuccess(lines) 283 284 lines =<< trim END 285 vim9script 286 var nr = 0 287 while nr 288 < 289 10 290 + 291 3 292 nr = nr 293 + 294 4 295 endwhile 296 assert_equal(16, nr) 297 END 298 CheckScriptSuccess(lines) 299enddef 300 301def Test_for_linebreak() 302 var lines =<< trim END 303 vim9script 304 var nr = 0 305 for x 306 in 307 [1, 2, 3, 4] 308 nr = nr + x 309 endfor 310 assert_equal(10, nr) 311 END 312 CheckScriptSuccess(lines) 313 314 lines =<< trim END 315 vim9script 316 var nr = 0 317 for x 318 in 319 [1, 2, 320 3, 4 321 ] 322 nr = nr 323 + 324 x 325 endfor 326 assert_equal(10, nr) 327 END 328 CheckScriptSuccess(lines) 329enddef 330 331def MethodAfterLinebreak(arg: string) 332 arg 333 ->setline(1) 334enddef 335 336def Test_method_call_linebreak() 337 var lines =<< trim END 338 vim9script 339 var res = [] 340 func RetArg( 341 arg 342 ) 343 let s:res = a:arg 344 endfunc 345 [1, 346 2, 347 3]->RetArg() 348 assert_equal([1, 2, 3], res) 349 END 350 CheckScriptSuccess(lines) 351 352 lines =<< trim END 353 new 354 var name = [1, 2] 355 name 356 ->copy() 357 ->setline(1) 358 assert_equal(['1', '2'], getline(1, 2)) 359 bwipe! 360 END 361 CheckDefAndScriptSuccess(lines) 362 363 lines =<< trim END 364 new 365 def Foo(): string 366 return 'the text' 367 enddef 368 def Bar(F: func): string 369 return F() 370 enddef 371 def Test() 372 Foo ->Bar() 373 ->setline(1) 374 enddef 375 Test() 376 assert_equal('the text', getline(1)) 377 bwipe! 378 END 379 CheckDefAndScriptSuccess(lines) 380 381 lines =<< trim END 382 new 383 g:shortlist 384 ->copy() 385 ->setline(1) 386 assert_equal(['1', '2'], getline(1, 2)) 387 bwipe! 388 END 389 g:shortlist = [1, 2] 390 CheckDefAndScriptSuccess(lines) 391 unlet g:shortlist 392 393 new 394 MethodAfterLinebreak('foobar') 395 assert_equal('foobar', getline(1)) 396 bwipe! 397 398 lines =<< trim END 399 vim9script 400 def Foo(): string 401 return '# some text' 402 enddef 403 404 def Bar(F: func): string 405 return F() 406 enddef 407 408 Foo->Bar() 409 ->setline(1) 410 END 411 CheckScriptSuccess(lines) 412 assert_equal('# some text', getline(1)) 413 bwipe! 414enddef 415 416def Test_method_call_whitespace() 417 var lines =<< trim END 418 new 419 var yank = 'text' 420 yank->setline(1) 421 yank ->setline(2) 422 yank-> setline(3) 423 yank -> setline(4) 424 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4)) 425 bwipe! 426 END 427 CheckDefAndScriptSuccess(lines) 428enddef 429 430def Test_method_and_user_command() 431 var lines =<< trim END 432 vim9script 433 def Cmd() 434 g:didFunc = 1 435 enddef 436 command Cmd g:didCmd = 1 437 Cmd 438 assert_equal(1, g:didCmd) 439 Cmd() 440 assert_equal(1, g:didFunc) 441 unlet g:didFunc 442 unlet g:didCmd 443 444 def InDefFunc() 445 Cmd 446 assert_equal(1, g:didCmd) 447 Cmd() 448 assert_equal(1, g:didFunc) 449 unlet g:didFunc 450 unlet g:didCmd 451 enddef 452 InDefFunc() 453 END 454 CheckScriptSuccess(lines) 455enddef 456 457def Test_skipped_expr_linebreak() 458 if 0 459 var x = [] 460 ->map(() => 0) 461 endif 462enddef 463 464def Test_dict_member() 465 var test: dict<list<number>> = {data: [3, 1, 2]} 466 test.data->sort() 467 assert_equal({data: [1, 2, 3]}, test) 468 test.data 469 ->reverse() 470 assert_equal({data: [3, 2, 1]}, test) 471 472 var lines =<< trim END 473 vim9script 474 var test: dict<list<number>> = {data: [3, 1, 2]} 475 test.data->sort() 476 assert_equal({data: [1, 2, 3]}, test) 477 END 478 CheckScriptSuccess(lines) 479enddef 480 481def Test_bar_after_command() 482 def RedrawAndEcho() 483 var x = 'did redraw' 484 redraw | echo x 485 enddef 486 RedrawAndEcho() 487 assert_match('did redraw', Screenline(&lines)) 488 489 def CallAndEcho() 490 var x = 'did redraw' 491 reg_executing() | echo x 492 enddef 493 CallAndEcho() 494 assert_match('did redraw', Screenline(&lines)) 495 496 if has('unix') 497 # bar in filter write command does not start new command 498 def WriteToShell() 499 new 500 setline(1, 'some text') 501 w !cat | cat > Xoutfile 502 bwipe! 503 enddef 504 WriteToShell() 505 assert_equal(['some text'], readfile('Xoutfile')) 506 delete('Xoutfile') 507 508 # bar in filter read command does not start new command 509 def ReadFromShell() 510 new 511 r! echo hello there | cat > Xoutfile 512 r !echo again | cat >> Xoutfile 513 bwipe! 514 enddef 515 ReadFromShell() 516 assert_equal(['hello there', 'again'], readfile('Xoutfile')) 517 delete('Xoutfile') 518 endif 519enddef 520 521def Test_filter_is_not_modifier() 522 var tags = [{a: 1, b: 2}, {x: 3, y: 4}] 523 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 ) 524 assert_equal([{x: 3, y: 4}], tags) 525enddef 526 527def Test_command_modifier_filter() 528 var lines =<< trim END 529 final expected = "\nType Name Content\n c \"c piyo" 530 @a = 'hoge' 531 @b = 'fuga' 532 @c = 'piyo' 533 534 assert_equal(execute('filter /piyo/ registers abc'), expected) 535 END 536 CheckDefAndScriptSuccess(lines) 537enddef 538 539def Test_win_command_modifiers() 540 assert_equal(1, winnr('$')) 541 542 set splitright 543 vsplit 544 assert_equal(2, winnr()) 545 close 546 aboveleft vsplit 547 assert_equal(1, winnr()) 548 close 549 set splitright& 550 551 vsplit 552 assert_equal(1, winnr()) 553 close 554 belowright vsplit 555 assert_equal(2, winnr()) 556 close 557 rightbelow vsplit 558 assert_equal(2, winnr()) 559 close 560 561 if has('browse') 562 browse set 563 assert_equal('option-window', expand('%')) 564 close 565 endif 566 567 vsplit 568 botright split 569 assert_equal(3, winnr()) 570 assert_equal(&columns, winwidth(0)) 571 close 572 close 573 574 vsplit 575 topleft split 576 assert_equal(1, winnr()) 577 assert_equal(&columns, winwidth(0)) 578 close 579 close 580 581 gettabinfo()->len()->assert_equal(1) 582 tab split 583 gettabinfo()->len()->assert_equal(2) 584 tabclose 585 586 vertical new 587 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0)) 588 close 589enddef 590 591func Test_command_modifier_confirm() 592 CheckNotGui 593 CheckRunVimInTerminal 594 595 " Test for saving all the modified buffers 596 let lines =<< trim END 597 call setline(1, 'changed') 598 def Getout() 599 confirm write Xfile 600 enddef 601 END 602 call writefile(lines, 'Xconfirmscript') 603 call writefile(['empty'], 'Xfile') 604 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8}) 605 call term_sendkeys(buf, ":call Getout()\n") 606 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000) 607 call term_sendkeys(buf, "y") 608 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000) 609 call term_sendkeys(buf, "\<CR>") 610 call TermWait(buf) 611 call StopVimInTerminal(buf) 612 613 call assert_equal(['changed'], readfile('Xfile')) 614 call delete('Xfile') 615 call delete('.Xfile.swp') " in case Vim was killed 616 call delete('Xconfirmscript') 617endfunc 618 619def Test_command_modifiers_keep() 620 if has('unix') 621 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool) 622 new 623 setline(1, ['one', 'two', 'three']) 624 normal 1Gma 625 normal 2Gmb 626 normal 3Gmc 627 if addRflag 628 set cpo+=R 629 else 630 set cpo-=R 631 endif 632 if keepMarks 633 keepmarks :%!cat 634 else 635 :%!cat 636 endif 637 if hasMarks 638 assert_equal(1, line("'a")) 639 assert_equal(2, line("'b")) 640 assert_equal(3, line("'c")) 641 else 642 assert_equal(0, line("'a")) 643 assert_equal(0, line("'b")) 644 assert_equal(0, line("'c")) 645 endif 646 quit! 647 enddef 648 DoTest(false, false, true) 649 DoTest(true, false, false) 650 DoTest(false, true, true) 651 DoTest(true, true, true) 652 set cpo&vim 653 654 new 655 setline(1, ['one', 'two', 'three', 'four']) 656 assert_equal(4, line("$")) 657 normal 1Gma 658 normal 2Gmb 659 normal 3Gmc 660 lockmarks :1,2!wc 661 # line is deleted, marks don't move 662 assert_equal(3, line("$")) 663 assert_equal('four', getline(3)) 664 assert_equal(1, line("'a")) 665 assert_equal(2, line("'b")) 666 assert_equal(3, line("'c")) 667 quit! 668 endif 669 670 edit Xone 671 edit Xtwo 672 assert_equal('Xone', expand('#')) 673 keepalt edit Xthree 674 assert_equal('Xone', expand('#')) 675 676 normal /a*b* 677 assert_equal('a*b*', histget("search")) 678 keeppatterns normal /c*d* 679 assert_equal('a*b*', histget("search")) 680 681 new 682 setline(1, range(10)) 683 :10 684 normal gg 685 assert_equal(10, getpos("''")[1]) 686 keepjumps normal 5G 687 assert_equal(10, getpos("''")[1]) 688 quit! 689enddef 690 691def Test_bar_line_continuation() 692 var lines =<< trim END 693 au BufNewFile Xfile g:readFile = 1 694 | g:readExtra = 2 695 g:readFile = 0 696 g:readExtra = 0 697 edit Xfile 698 assert_equal(1, g:readFile) 699 assert_equal(2, g:readExtra) 700 bwipe! 701 au! BufNewFile 702 703 au BufNewFile Xfile g:readFile = 1 704 | g:readExtra = 2 705 | g:readMore = 3 706 g:readFile = 0 707 g:readExtra = 0 708 g:readMore = 0 709 edit Xfile 710 assert_equal(1, g:readFile) 711 assert_equal(2, g:readExtra) 712 assert_equal(3, g:readMore) 713 bwipe! 714 au! BufNewFile 715 unlet g:readFile 716 unlet g:readExtra 717 unlet g:readMore 718 END 719 CheckDefAndScriptSuccess(lines) 720enddef 721 722def Test_command_modifier_other() 723 new Xsomefile 724 setline(1, 'changed') 725 var buf = bufnr() 726 hide edit Xotherfile 727 var info = getbufinfo(buf) 728 assert_equal(1, info[0].hidden) 729 assert_equal(1, info[0].changed) 730 edit Xsomefile 731 bwipe! 732 733 au BufNewFile Xfile g:readFile = 1 734 g:readFile = 0 735 edit Xfile 736 assert_equal(1, g:readFile) 737 bwipe! 738 g:readFile = 0 739 noautocmd edit Xfile 740 assert_equal(0, g:readFile) 741 au! BufNewFile 742 unlet g:readFile 743 744 noswapfile edit XnoSwap 745 assert_equal(false, &l:swapfile) 746 bwipe! 747 748 var caught = false 749 try 750 sandbox !ls 751 catch /E48:/ 752 caught = true 753 endtry 754 assert_true(caught) 755 756 :8verbose g:verbose_now = &verbose 757 assert_equal(8, g:verbose_now) 758 unlet g:verbose_now 759enddef 760 761def EchoHere() 762 echomsg 'here' 763enddef 764def EchoThere() 765 unsilent echomsg 'there' 766enddef 767 768def Test_modifier_silent_unsilent() 769 echomsg 'last one' 770 silent echomsg "text" 771 assert_equal("\nlast one", execute(':1messages')) 772 773 silent! echoerr "error" 774 775 echomsg 'last one' 776 silent EchoHere() 777 assert_equal("\nlast one", execute(':1messages')) 778 779 silent EchoThere() 780 assert_equal("\nthere", execute(':1messages')) 781 782 try 783 silent eval [][0] 784 catch 785 echomsg "caught" 786 endtry 787 assert_equal("\ncaught", execute(':1messages')) 788enddef 789 790def Test_range_after_command_modifier() 791 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2) 792 new 793 setline(1, 'xxx') 794 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _']) 795 assert_equal('', getline(1)) 796 bwipe! 797enddef 798 799def Test_silent_pattern() 800 new 801 silent! :/pat/put _ 802 bwipe! 803enddef 804 805def Test_useless_command_modifier() 806 g:maybe = true 807 var lines =<< trim END 808 if g:maybe 809 silent endif 810 END 811 CheckDefAndScriptFailure(lines, 'E1176:', 2) 812 813 lines =<< trim END 814 for i in [0] 815 silent endfor 816 END 817 CheckDefAndScriptFailure(lines, 'E1176:', 2) 818 819 lines =<< trim END 820 while g:maybe 821 silent endwhile 822 END 823 CheckDefAndScriptFailure(lines, 'E1176:', 2) 824 825 lines =<< trim END 826 silent try 827 finally 828 endtry 829 END 830 CheckDefAndScriptFailure(lines, 'E1176:', 1) 831 832 lines =<< trim END 833 try 834 silent catch 835 endtry 836 END 837 CheckDefAndScriptFailure(lines, 'E1176:', 2) 838 839 lines =<< trim END 840 try 841 silent finally 842 endtry 843 END 844 CheckDefAndScriptFailure(lines, 'E1176:', 2) 845 846 lines =<< trim END 847 try 848 finally 849 silent endtry 850 END 851 CheckDefAndScriptFailure(lines, 'E1176:', 3) 852enddef 853 854def Test_eval_command() 855 var from = 3 856 var to = 5 857 g:val = 111 858 def Increment(nrs: list<number>) 859 for nr in nrs 860 g:val += nr 861 endfor 862 enddef 863 eval range(from, to) 864 ->Increment() 865 assert_equal(111 + 3 + 4 + 5, g:val) 866 unlet g:val 867 868 var lines =<< trim END 869 vim9script 870 g:caught = 'no' 871 try 872 eval 123 || 0 873 catch 874 g:caught = 'yes' 875 endtry 876 assert_equal('yes', g:caught) 877 unlet g:caught 878 END 879 CheckScriptSuccess(lines) 880enddef 881 882def Test_map_command() 883 var lines =<< trim END 884 nnoremap <F3> :echo 'hit F3 #'<CR> 885 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n")) 886 END 887 CheckDefSuccess(lines) 888 CheckScriptSuccess(['vim9script'] + lines) 889enddef 890 891def Test_normal_command() 892 new 893 setline(1, 'doesnotexist') 894 var caught = 0 895 try 896 exe "norm! \<C-]>" 897 catch /E433/ 898 caught = 2 899 endtry 900 assert_equal(2, caught) 901 902 try 903 exe "norm! 3\<C-]>" 904 catch /E433/ 905 caught = 3 906 endtry 907 assert_equal(3, caught) 908 bwipe! 909enddef 910 911def Test_put_command() 912 new 913 @p = 'ppp' 914 put p 915 assert_equal('ppp', getline(2)) 916 917 put ='below' 918 assert_equal('below', getline(3)) 919 put! ='above' 920 assert_equal('above', getline(3)) 921 assert_equal('below', getline(4)) 922 923 :2put =['a', 'b', 'c'] 924 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6)) 925 926 # compute range at runtime 927 setline(1, range(1, 8)) 928 @a = 'aaa' 929 :$-2put a 930 assert_equal('aaa', getline(7)) 931 932 setline(1, range(1, 8)) 933 :2 934 :+2put! a 935 assert_equal('aaa', getline(4)) 936 937 []->mapnew(() => 0) 938 :$put ='end' 939 assert_equal('end', getline('$')) 940 941 bwipe! 942 943 CheckDefFailure(['put =xxx'], 'E1001:') 944enddef 945 946def Test_put_with_linebreak() 947 new 948 var lines =<< trim END 949 vim9script 950 pu =split('abc', '\zs') 951 ->join() 952 END 953 CheckScriptSuccess(lines) 954 getline(2)->assert_equal('a b c') 955 bwipe! 956enddef 957 958def Test_command_star_range() 959 new 960 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar']) 961 setpos("'<", [0, 1, 0, 0]) 962 setpos("'>", [0, 3, 0, 0]) 963 :*s/\(foo\|bar\)/baz/g 964 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz']) 965 966 bwipe! 967enddef 968 969def Test_f_args() 970 var lines =<< trim END 971 vim9script 972 973 func SaveCmdArgs(...) 974 let g:args = a:000 975 endfunc 976 977 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>) 978 979 TestFArgs 980 assert_equal([], g:args) 981 982 TestFArgs one two three 983 assert_equal(['one', 'two', 'three'], g:args) 984 END 985 CheckScriptSuccess(lines) 986enddef 987 988def Test_user_command_comment() 989 command -nargs=1 Comd echom <q-args> 990 991 var lines =<< trim END 992 vim9script 993 Comd # comment 994 END 995 CheckScriptSuccess(lines) 996 997 lines =<< trim END 998 vim9script 999 Comd# comment 1000 END 1001 CheckScriptFailure(lines, 'E1144:') 1002 delcommand Comd 1003 1004 lines =<< trim END 1005 vim9script 1006 command Foo echo 'Foo' 1007 Foo3Bar 1008 END 1009 CheckScriptFailure(lines, 'E1144: Command "Foo" is not followed by white space: Foo3Bar') 1010 1011 delcommand Foo 1012enddef 1013 1014def Test_star_command() 1015 var lines =<< trim END 1016 vim9script 1017 @s = 'g:success = 8' 1018 set cpo+=* 1019 exe '*s' 1020 assert_equal(8, g:success) 1021 unlet g:success 1022 set cpo-=* 1023 assert_fails("exe '*s'", 'E1050:') 1024 END 1025 CheckScriptSuccess(lines) 1026enddef 1027 1028def Test_cmd_argument_without_colon() 1029 new Xfile 1030 setline(1, ['a', 'b', 'c', 'd']) 1031 write 1032 edit +3 % 1033 assert_equal(3, getcurpos()[1]) 1034 edit +/a % 1035 assert_equal(1, getcurpos()[1]) 1036 bwipe 1037 delete('Xfile') 1038enddef 1039 1040def Test_ambiguous_user_cmd() 1041 command Cmd1 eval 0 1042 command Cmd2 eval 0 1043 var lines =<< trim END 1044 Cmd 1045 END 1046 CheckDefAndScriptFailure(lines, 'E464:', 1) 1047 delcommand Cmd1 1048 delcommand Cmd2 1049enddef 1050 1051def Test_command_not_recognized() 1052 var lines =<< trim END 1053 d.key = 'asdf' 1054 END 1055 CheckDefFailure(lines, 'E1146:', 1) 1056 1057 lines =<< trim END 1058 d['key'] = 'asdf' 1059 END 1060 CheckDefFailure(lines, 'E1146:', 1) 1061enddef 1062 1063def Test_magic_not_used() 1064 new 1065 for cmd in ['set magic', 'set nomagic'] 1066 exe cmd 1067 setline(1, 'aaa') 1068 s/.../bbb/ 1069 assert_equal('bbb', getline(1)) 1070 endfor 1071 1072 set magic 1073 setline(1, 'aaa') 1074 assert_fails('s/.\M../bbb/', 'E486:') 1075 assert_fails('snomagic/.../bbb/', 'E486:') 1076 assert_equal('aaa', getline(1)) 1077 1078 bwipe! 1079enddef 1080 1081def Test_gdefault_not_used() 1082 new 1083 for cmd in ['set gdefault', 'set nogdefault'] 1084 exe cmd 1085 setline(1, 'aaa') 1086 s/./b/ 1087 assert_equal('baa', getline(1)) 1088 endfor 1089 1090 set nogdefault 1091 bwipe! 1092enddef 1093 1094def g:SomeComplFunc(findstart: number, base: string): any 1095 if findstart 1096 return 0 1097 else 1098 return ['aaa', 'bbb'] 1099 endif 1100enddef 1101 1102def Test_insert_complete() 1103 # this was running into an error with the matchparen hack 1104 new 1105 set completefunc=SomeComplFunc 1106 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx') 1107 assert_equal('aaa', getline(1)) 1108 1109 set completefunc= 1110 bwipe! 1111enddef 1112 1113def Test_wincmd() 1114 split 1115 var id1 = win_getid() 1116 if true 1117 try | wincmd w | catch | endtry 1118 endif 1119 assert_notequal(id1, win_getid()) 1120 close 1121 1122 split 1123 var id = win_getid() 1124 split 1125 :2wincmd o 1126 assert_equal(id, win_getid()) 1127 only 1128 1129 split 1130 split 1131 assert_equal(3, winnr('$')) 1132 :2wincmd c 1133 assert_equal(2, winnr('$')) 1134 only 1135 1136 split 1137 split 1138 assert_equal(3, winnr('$')) 1139 :2wincmd q 1140 assert_equal(2, winnr('$')) 1141 only 1142enddef 1143 1144def Test_windo_missing_endif() 1145 var lines =<< trim END 1146 windo if 1 1147 END 1148 CheckDefExecFailure(lines, 'E171:', 1) 1149enddef 1150 1151let s:theList = [1, 2, 3] 1152 1153def Test_lockvar() 1154 s:theList[1] = 22 1155 assert_equal([1, 22, 3], s:theList) 1156 lockvar s:theList 1157 assert_fails('theList[1] = 77', 'E741:') 1158 unlockvar s:theList 1159 s:theList[1] = 44 1160 assert_equal([1, 44, 3], s:theList) 1161 1162 var lines =<< trim END 1163 vim9script 1164 var theList = [1, 2, 3] 1165 def SetList() 1166 theList[1] = 22 1167 assert_equal([1, 22, 3], theList) 1168 lockvar theList 1169 theList[1] = 77 1170 enddef 1171 SetList() 1172 END 1173 CheckScriptFailure(lines, 'E1119', 4) 1174 1175 lines =<< trim END 1176 var theList = [1, 2, 3] 1177 lockvar theList 1178 END 1179 CheckDefFailure(lines, 'E1178', 2) 1180 1181 lines =<< trim END 1182 var theList = [1, 2, 3] 1183 unlockvar theList 1184 END 1185 CheckDefFailure(lines, 'E1178', 2) 1186enddef 1187 1188def Test_substitute_expr() 1189 var to = 'repl' 1190 new 1191 setline(1, 'one from two') 1192 s/from/\=to 1193 assert_equal('one repl two', getline(1)) 1194 1195 setline(1, 'one from two') 1196 s/from/\=to .. '_x' 1197 assert_equal('one repl_x two', getline(1)) 1198 1199 setline(1, 'one from two from three') 1200 var also = 'also' 1201 s/from/\=to .. '_' .. also/g#e 1202 assert_equal('one repl_also two repl_also three', getline(1)) 1203 1204 setline(1, 'abc abc abc') 1205 for choice in [true, false] 1206 :1s/abc/\=choice ? 'yes' : 'no'/ 1207 endfor 1208 assert_equal('yes no abc', getline(1)) 1209 1210 bwipe! 1211 1212 CheckDefFailure(['s/from/\="x")/'], 'E488:') 1213 CheckDefFailure(['s/from/\="x"/9'], 'E488:') 1214 1215 # When calling a function the right instruction list needs to be restored. 1216 g:cond = true 1217 var lines =<< trim END 1218 vim9script 1219 def Foo() 1220 Bar([]) 1221 enddef 1222 def Bar(l: list<number>) 1223 if g:cond 1224 s/^/\=Rep()/ 1225 for n in l[:] 1226 endfor 1227 endif 1228 enddef 1229 def Rep(): string 1230 return 'rep' 1231 enddef 1232 new 1233 Foo() 1234 assert_equal('rep', getline(1)) 1235 bwipe! 1236 END 1237 CheckScriptSuccess(lines) 1238 unlet g:cond 1239enddef 1240 1241def Test_redir_to_var() 1242 var result: string 1243 redir => result 1244 echo 'something' 1245 redir END 1246 assert_equal("\nsomething", result) 1247 1248 redir =>> result 1249 echo 'more' 1250 redir END 1251 assert_equal("\nsomething\nmore", result) 1252 1253 var d: dict<string> 1254 redir => d.redir 1255 echo 'dict' 1256 redir END 1257 assert_equal({redir: "\ndict"}, d) 1258 1259 var l = ['a', 'b', 'c'] 1260 redir => l[1] 1261 echo 'list' 1262 redir END 1263 assert_equal(['a', "\nlist", 'c'], l) 1264 1265 var dl = {l: ['x']} 1266 redir => dl.l[0] 1267 echo 'dict-list' 1268 redir END 1269 assert_equal({l: ["\ndict-list"]}, dl) 1270 1271 redir =>> d.redir 1272 echo 'more' 1273 redir END 1274 assert_equal({redir: "\ndict\nmore"}, d) 1275 1276 var lines =<< trim END 1277 redir => notexist 1278 END 1279 CheckDefFailure(lines, 'E1089:') 1280 1281 lines =<< trim END 1282 var ls = 'asdf' 1283 redir => ls[1] 1284 redir END 1285 END 1286 CheckDefFailure(lines, 'E1141:') 1287enddef 1288 1289def Test_echo_void() 1290 var lines =<< trim END 1291 vim9script 1292 def NoReturn() 1293 echo 'nothing' 1294 enddef 1295 echo NoReturn() 1296 END 1297 CheckScriptFailure(lines, 'E1186:', 5) 1298 1299 lines =<< trim END 1300 vim9script 1301 def NoReturn() 1302 echo 'nothing' 1303 enddef 1304 def Try() 1305 echo NoReturn() 1306 enddef 1307 defcompile 1308 END 1309 CheckScriptFailure(lines, 'E1186:', 1) 1310enddef 1311 1312 1313" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 1314