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