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')) 810enddef 811 812def Test_range_after_command_modifier() 813 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2) 814 new 815 setline(1, 'xxx') 816 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _']) 817 assert_equal('', getline(1)) 818 bwipe! 819enddef 820 821def Test_silent_pattern() 822 new 823 silent! :/pat/put _ 824 bwipe! 825enddef 826 827def Test_useless_command_modifier() 828 g:maybe = true 829 var lines =<< trim END 830 if g:maybe 831 silent endif 832 END 833 CheckDefAndScriptFailure(lines, 'E1176:', 2) 834 835 lines =<< trim END 836 for i in [0] 837 silent endfor 838 END 839 CheckDefAndScriptFailure(lines, 'E1176:', 2) 840 841 lines =<< trim END 842 while g:maybe 843 silent endwhile 844 END 845 CheckDefAndScriptFailure(lines, 'E1176:', 2) 846 847 lines =<< trim END 848 silent try 849 finally 850 endtry 851 END 852 CheckDefAndScriptFailure(lines, 'E1176:', 1) 853 854 lines =<< trim END 855 try 856 silent catch 857 endtry 858 END 859 CheckDefAndScriptFailure(lines, 'E1176:', 2) 860 861 lines =<< trim END 862 try 863 silent finally 864 endtry 865 END 866 CheckDefAndScriptFailure(lines, 'E1176:', 2) 867 868 lines =<< trim END 869 try 870 finally 871 silent endtry 872 END 873 CheckDefAndScriptFailure(lines, 'E1176:', 3) 874enddef 875 876def Test_eval_command() 877 var from = 3 878 var to = 5 879 g:val = 111 880 def Increment(nrs: list<number>) 881 for nr in nrs 882 g:val += nr 883 endfor 884 enddef 885 eval range(from, to) 886 ->Increment() 887 assert_equal(111 + 3 + 4 + 5, g:val) 888 unlet g:val 889 890 var lines =<< trim END 891 vim9script 892 g:caught = 'no' 893 try 894 eval 123 || 0 895 catch 896 g:caught = 'yes' 897 endtry 898 assert_equal('yes', g:caught) 899 unlet g:caught 900 END 901 CheckScriptSuccess(lines) 902enddef 903 904def Test_map_command() 905 var lines =<< trim END 906 nnoremap <F3> :echo 'hit F3 #'<CR> 907 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n")) 908 END 909 CheckDefSuccess(lines) 910 CheckScriptSuccess(['vim9script'] + lines) 911enddef 912 913def Test_normal_command() 914 new 915 setline(1, 'doesnotexist') 916 var caught = 0 917 try 918 exe "norm! \<C-]>" 919 catch /E433/ 920 caught = 2 921 endtry 922 assert_equal(2, caught) 923 924 try 925 exe "norm! 3\<C-]>" 926 catch /E433/ 927 caught = 3 928 endtry 929 assert_equal(3, caught) 930 bwipe! 931enddef 932 933def Test_put_command() 934 new 935 @p = 'ppp' 936 put p 937 assert_equal('ppp', getline(2)) 938 939 put ='below' 940 assert_equal('below', getline(3)) 941 put! ='above' 942 assert_equal('above', getline(3)) 943 assert_equal('below', getline(4)) 944 945 :2put =['a', 'b', 'c'] 946 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6)) 947 948 # compute range at runtime 949 setline(1, range(1, 8)) 950 @a = 'aaa' 951 :$-2put a 952 assert_equal('aaa', getline(7)) 953 954 setline(1, range(1, 8)) 955 :2 956 :+2put! a 957 assert_equal('aaa', getline(4)) 958 959 []->mapnew(() => 0) 960 :$put ='end' 961 assert_equal('end', getline('$')) 962 963 bwipe! 964 965 CheckDefFailure(['put =xxx'], 'E1001:') 966enddef 967 968def Test_put_with_linebreak() 969 new 970 var lines =<< trim END 971 vim9script 972 pu =split('abc', '\zs') 973 ->join() 974 END 975 CheckScriptSuccess(lines) 976 getline(2)->assert_equal('a b c') 977 bwipe! 978enddef 979 980def Test_command_star_range() 981 new 982 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar']) 983 setpos("'<", [0, 1, 0, 0]) 984 setpos("'>", [0, 3, 0, 0]) 985 :*s/\(foo\|bar\)/baz/g 986 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz']) 987 988 bwipe! 989enddef 990 991def Test_f_args() 992 var lines =<< trim END 993 vim9script 994 995 func SaveCmdArgs(...) 996 let g:args = a:000 997 endfunc 998 999 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>) 1000 1001 TestFArgs 1002 assert_equal([], g:args) 1003 1004 TestFArgs one two three 1005 assert_equal(['one', 'two', 'three'], g:args) 1006 END 1007 CheckScriptSuccess(lines) 1008enddef 1009 1010def Test_user_command_comment() 1011 command -nargs=1 Comd echom <q-args> 1012 1013 var lines =<< trim END 1014 vim9script 1015 Comd # comment 1016 END 1017 CheckScriptSuccess(lines) 1018 1019 lines =<< trim END 1020 vim9script 1021 Comd# comment 1022 END 1023 CheckScriptFailure(lines, 'E1144:') 1024 delcommand Comd 1025 1026 lines =<< trim END 1027 vim9script 1028 command Foo echo 'Foo' 1029 Foo3Bar 1030 END 1031 CheckScriptFailure(lines, 'E1144: Command "Foo" is not followed by white space: Foo3Bar') 1032 1033 delcommand Foo 1034enddef 1035 1036def Test_star_command() 1037 var lines =<< trim END 1038 vim9script 1039 @s = 'g:success = 8' 1040 set cpo+=* 1041 exe '*s' 1042 assert_equal(8, g:success) 1043 unlet g:success 1044 set cpo-=* 1045 assert_fails("exe '*s'", 'E1050:') 1046 END 1047 CheckScriptSuccess(lines) 1048enddef 1049 1050def Test_cmd_argument_without_colon() 1051 new Xfile 1052 setline(1, ['a', 'b', 'c', 'd']) 1053 write 1054 edit +3 % 1055 assert_equal(3, getcurpos()[1]) 1056 edit +/a % 1057 assert_equal(1, getcurpos()[1]) 1058 bwipe 1059 delete('Xfile') 1060enddef 1061 1062def Test_ambiguous_user_cmd() 1063 command Cmd1 eval 0 1064 command Cmd2 eval 0 1065 var lines =<< trim END 1066 Cmd 1067 END 1068 CheckDefAndScriptFailure(lines, 'E464:', 1) 1069 delcommand Cmd1 1070 delcommand Cmd2 1071enddef 1072 1073def Test_command_not_recognized() 1074 var lines =<< trim END 1075 d.key = 'asdf' 1076 END 1077 CheckDefFailure(lines, 'E1146:', 1) 1078 1079 lines =<< trim END 1080 d['key'] = 'asdf' 1081 END 1082 CheckDefFailure(lines, 'E1146:', 1) 1083enddef 1084 1085def Test_magic_not_used() 1086 new 1087 for cmd in ['set magic', 'set nomagic'] 1088 exe cmd 1089 setline(1, 'aaa') 1090 s/.../bbb/ 1091 assert_equal('bbb', getline(1)) 1092 endfor 1093 1094 set magic 1095 setline(1, 'aaa') 1096 assert_fails('s/.\M../bbb/', 'E486:') 1097 assert_fails('snomagic/.../bbb/', 'E486:') 1098 assert_equal('aaa', getline(1)) 1099 1100 bwipe! 1101enddef 1102 1103def Test_gdefault_not_used() 1104 new 1105 for cmd in ['set gdefault', 'set nogdefault'] 1106 exe cmd 1107 setline(1, 'aaa') 1108 s/./b/ 1109 assert_equal('baa', getline(1)) 1110 endfor 1111 1112 set nogdefault 1113 bwipe! 1114enddef 1115 1116def g:SomeComplFunc(findstart: number, base: string): any 1117 if findstart 1118 return 0 1119 else 1120 return ['aaa', 'bbb'] 1121 endif 1122enddef 1123 1124def Test_insert_complete() 1125 # this was running into an error with the matchparen hack 1126 new 1127 set completefunc=SomeComplFunc 1128 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx') 1129 assert_equal('aaa', getline(1)) 1130 1131 set completefunc= 1132 bwipe! 1133enddef 1134 1135def Test_wincmd() 1136 split 1137 var id1 = win_getid() 1138 if true 1139 try | wincmd w | catch | endtry 1140 endif 1141 assert_notequal(id1, win_getid()) 1142 close 1143 1144 split 1145 var id = win_getid() 1146 split 1147 :2wincmd o 1148 assert_equal(id, win_getid()) 1149 only 1150 1151 split 1152 split 1153 assert_equal(3, winnr('$')) 1154 :2wincmd c 1155 assert_equal(2, winnr('$')) 1156 only 1157 1158 split 1159 split 1160 assert_equal(3, winnr('$')) 1161 :2wincmd q 1162 assert_equal(2, winnr('$')) 1163 only 1164enddef 1165 1166def Test_windo_missing_endif() 1167 var lines =<< trim END 1168 windo if 1 1169 END 1170 CheckDefExecFailure(lines, 'E171:', 1) 1171enddef 1172 1173let s:theList = [1, 2, 3] 1174 1175def Test_lockvar() 1176 s:theList[1] = 22 1177 assert_equal([1, 22, 3], s:theList) 1178 lockvar s:theList 1179 assert_fails('theList[1] = 77', 'E741:') 1180 unlockvar s:theList 1181 s:theList[1] = 44 1182 assert_equal([1, 44, 3], s:theList) 1183 1184 var lines =<< trim END 1185 vim9script 1186 var theList = [1, 2, 3] 1187 def SetList() 1188 theList[1] = 22 1189 assert_equal([1, 22, 3], theList) 1190 lockvar theList 1191 theList[1] = 77 1192 enddef 1193 SetList() 1194 END 1195 CheckScriptFailure(lines, 'E1119', 4) 1196 1197 lines =<< trim END 1198 var theList = [1, 2, 3] 1199 lockvar theList 1200 END 1201 CheckDefFailure(lines, 'E1178', 2) 1202 1203 lines =<< trim END 1204 var theList = [1, 2, 3] 1205 unlockvar theList 1206 END 1207 CheckDefFailure(lines, 'E1178', 2) 1208enddef 1209 1210def Test_substitute_expr() 1211 var to = 'repl' 1212 new 1213 setline(1, 'one from two') 1214 s/from/\=to 1215 assert_equal('one repl two', getline(1)) 1216 1217 setline(1, 'one from two') 1218 s/from/\=to .. '_x' 1219 assert_equal('one repl_x two', getline(1)) 1220 1221 setline(1, 'one from two from three') 1222 var also = 'also' 1223 s/from/\=to .. '_' .. also/g#e 1224 assert_equal('one repl_also two repl_also three', getline(1)) 1225 1226 setline(1, 'abc abc abc') 1227 for choice in [true, false] 1228 :1s/abc/\=choice ? 'yes' : 'no'/ 1229 endfor 1230 assert_equal('yes no abc', getline(1)) 1231 1232 bwipe! 1233 1234 CheckDefFailure(['s/from/\="x")/'], 'E488:') 1235 CheckDefFailure(['s/from/\="x"/9'], 'E488:') 1236 1237 # When calling a function the right instruction list needs to be restored. 1238 g:cond = true 1239 var lines =<< trim END 1240 vim9script 1241 def Foo() 1242 Bar([]) 1243 enddef 1244 def Bar(l: list<number>) 1245 if g:cond 1246 s/^/\=Rep()/ 1247 for n in l[:] 1248 endfor 1249 endif 1250 enddef 1251 def Rep(): string 1252 return 'rep' 1253 enddef 1254 new 1255 Foo() 1256 assert_equal('rep', getline(1)) 1257 bwipe! 1258 END 1259 CheckScriptSuccess(lines) 1260 unlet g:cond 1261 1262 # List results in multiple lines 1263 new 1264 setline(1, 'some text here') 1265 s/text/\=['aaa', 'bbb', 'ccc']/ 1266 assert_equal(['some aaa', 'bbb', 'ccc', ' here'], getline(1, '$')) 1267 bwipe! 1268enddef 1269 1270def Test_redir_to_var() 1271 var result: string 1272 redir => result 1273 echo 'something' 1274 redir END 1275 assert_equal("\nsomething", result) 1276 1277 redir =>> result 1278 echo 'more' 1279 redir END 1280 assert_equal("\nsomething\nmore", result) 1281 1282 var d: dict<string> 1283 redir => d.redir 1284 echo 'dict' 1285 redir END 1286 assert_equal({redir: "\ndict"}, d) 1287 1288 var l = ['a', 'b', 'c'] 1289 redir => l[1] 1290 echo 'list' 1291 redir END 1292 assert_equal(['a', "\nlist", 'c'], l) 1293 1294 var dl = {l: ['x']} 1295 redir => dl.l[0] 1296 echo 'dict-list' 1297 redir END 1298 assert_equal({l: ["\ndict-list"]}, dl) 1299 1300 redir =>> d.redir 1301 echo 'more' 1302 redir END 1303 assert_equal({redir: "\ndict\nmore"}, d) 1304 1305 var lines =<< trim END 1306 redir => notexist 1307 END 1308 CheckDefFailure(lines, 'E1089:') 1309 1310 lines =<< trim END 1311 var ls = 'asdf' 1312 redir => ls[1] 1313 redir END 1314 END 1315 CheckDefFailure(lines, 'E1141:') 1316enddef 1317 1318def Test_echo_void() 1319 var lines =<< trim END 1320 vim9script 1321 def NoReturn() 1322 echo 'nothing' 1323 enddef 1324 echo NoReturn() 1325 END 1326 CheckScriptFailure(lines, 'E1186:', 5) 1327 1328 lines =<< trim END 1329 vim9script 1330 def NoReturn() 1331 echo 'nothing' 1332 enddef 1333 def Try() 1334 echo NoReturn() 1335 enddef 1336 defcompile 1337 END 1338 CheckScriptFailure(lines, 'E1186:', 1) 1339enddef 1340 1341def Test_cmdwin_block() 1342 augroup justTesting 1343 autocmd BufEnter * { 1344 echomsg 'in block' 1345 } 1346 augroup END 1347 feedkeys('q:', 'xt') 1348 redraw 1349 feedkeys("aclose\<CR>", 'xt') 1350 1351 au! justTesting 1352enddef 1353 1354 1355" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 1356