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_edit_wildcards() 9 var filename = 'Xtest' 10 edit `=filename` 11 assert_equal('Xtest', bufname()) 12 13 var filenr = 123 14 edit Xtest`=filenr` 15 assert_equal('Xtest123', bufname()) 16 17 filenr = 77 18 edit `=filename``=filenr` 19 assert_equal('Xtest77', bufname()) 20 21 edit X`=filename`xx`=filenr`yy 22 assert_equal('XXtestxx77yy', bufname()) 23 24 CheckDefFailure(['edit `=xxx`'], 'E1001:') 25 CheckDefFailure(['edit `="foo"'], 'E1083:') 26enddef 27 28def Test_expand_alternate_file() 29 var lines =<< trim END 30 edit Xfileone 31 var bone = bufnr() 32 edit Xfiletwo 33 var btwo = bufnr() 34 edit Xfilethree 35 var bthree = bufnr() 36 37 edit # 38 assert_equal(bthree, bufnr()) 39 edit %% 40 assert_equal(btwo, bufnr()) 41 edit %% # comment 42 assert_equal(bthree, bufnr()) 43 edit %%yy 44 assert_equal('Xfiletwoyy', bufname()) 45 46 exe "edit %%" .. bone 47 assert_equal(bone, bufnr()) 48 exe "edit %%" .. btwo .. "xx" 49 assert_equal('Xfiletwoxx', bufname()) 50 51 next Xfileone Xfiletwo Xfilethree 52 assert_equal('Xfileone', argv(0)) 53 assert_equal('Xfiletwo', argv(1)) 54 assert_equal('Xfilethree', argv(2)) 55 next %%%zz 56 assert_equal('Xfileone', argv(0)) 57 assert_equal('Xfiletwo', argv(1)) 58 assert_equal('Xfilethreezz', argv(2)) 59 60 v:oldfiles = ['Xonefile', 'Xtwofile'] 61 edit %%<1 62 assert_equal('Xonefile', bufname()) 63 edit %%<2 64 assert_equal('Xtwofile', bufname()) 65 assert_fails('edit %%<3', 'E684:') 66 67 edit Xfileone.vim 68 edit Xfiletwo 69 edit %%:r 70 assert_equal('Xfileone', bufname()) 71 END 72 CheckDefAndScriptSuccess(lines) 73enddef 74 75def Test_global_backtick_expansion() 76 new 77 setline(1, 'xx') 78 var name = 'foobar' 79 g/^xx/s/.*/`=name` 80 assert_equal('foobar', getline(1)) 81 bwipe! 82enddef 83 84def Test_hardcopy_wildcards() 85 CheckUnix 86 CheckFeature postscript 87 88 var outfile = 'print' 89 hardcopy > X`=outfile`.ps 90 assert_true(filereadable('Xprint.ps')) 91 92 delete('Xprint.ps') 93enddef 94 95def Test_syn_include_wildcards() 96 writefile(['syn keyword Found found'], 'Xthemine.vim') 97 var save_rtp = &rtp 98 &rtp = '.' 99 100 var fname = 'mine' 101 syn include @Group Xthe`=fname`.vim 102 assert_match('Found.* contained found', execute('syn list Found')) 103 104 &rtp = save_rtp 105 delete('Xthemine.vim') 106enddef 107 108def Test_echo_linebreak() 109 var lines =<< trim END 110 vim9script 111 redir @a 112 echo 'one' 113 .. 'two' 114 redir END 115 assert_equal("\nonetwo", @a) 116 END 117 CheckScriptSuccess(lines) 118 119 lines =<< trim END 120 vim9script 121 redir @a 122 echo 11 + 123 77 124 - 22 125 redir END 126 assert_equal("\n66", @a) 127 END 128 CheckScriptSuccess(lines) 129enddef 130 131def Test_condition_types() 132 var lines =<< trim END 133 if 'text' 134 endif 135 END 136 CheckDefAndScriptFailure(lines, 'E1135:', 1) 137 138 lines =<< trim END 139 if [1] 140 endif 141 END 142 CheckDefFailure(lines, 'E1012:', 1) 143 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2) 144 145 lines =<< trim END 146 g:cond = 'text' 147 if g:cond 148 endif 149 END 150 CheckDefExecAndScriptFailure(lines, 'E1135:', 2) 151 152 lines =<< trim END 153 g:cond = 0 154 if g:cond 155 elseif 'text' 156 endif 157 END 158 CheckDefFailure(lines, 'E1012:', 3) 159 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4) 160 161 lines =<< trim END 162 if g:cond 163 elseif [1] 164 endif 165 END 166 CheckDefFailure(lines, 'E1012:', 2) 167 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3) 168 169 lines =<< trim END 170 g:cond = 'text' 171 if 0 172 elseif g:cond 173 endif 174 END 175 CheckDefExecAndScriptFailure(lines, 'E1135:', 3) 176 177 lines =<< trim END 178 while 'text' 179 endwhile 180 END 181 CheckDefFailure(lines, 'E1012:', 1) 182 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2) 183 184 lines =<< trim END 185 while [1] 186 endwhile 187 END 188 CheckDefFailure(lines, 'E1012:', 1) 189 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2) 190 191 lines =<< trim END 192 g:cond = 'text' 193 while g:cond 194 endwhile 195 END 196 CheckDefExecAndScriptFailure(lines, 'E1135:', 2) 197enddef 198 199def Test_if_linebreak() 200 var lines =<< trim END 201 vim9script 202 if 1 && 203 true 204 || 1 205 g:res = 42 206 endif 207 assert_equal(42, g:res) 208 END 209 CheckScriptSuccess(lines) 210 unlet g:res 211 212 lines =<< trim END 213 vim9script 214 if 1 && 215 0 216 g:res = 0 217 elseif 0 || 218 0 219 || 1 220 g:res = 12 221 endif 222 assert_equal(12, g:res) 223 END 224 CheckScriptSuccess(lines) 225 unlet g:res 226enddef 227 228def Test_while_linebreak() 229 var lines =<< trim END 230 vim9script 231 var nr = 0 232 while nr < 233 10 + 3 234 nr = nr 235 + 4 236 endwhile 237 assert_equal(16, nr) 238 END 239 CheckScriptSuccess(lines) 240 241 lines =<< trim END 242 vim9script 243 var nr = 0 244 while nr 245 < 246 10 247 + 248 3 249 nr = nr 250 + 251 4 252 endwhile 253 assert_equal(16, nr) 254 END 255 CheckScriptSuccess(lines) 256enddef 257 258def Test_for_linebreak() 259 var lines =<< trim END 260 vim9script 261 var nr = 0 262 for x 263 in 264 [1, 2, 3, 4] 265 nr = nr + x 266 endfor 267 assert_equal(10, nr) 268 END 269 CheckScriptSuccess(lines) 270 271 lines =<< trim END 272 vim9script 273 var nr = 0 274 for x 275 in 276 [1, 2, 277 3, 4 278 ] 279 nr = nr 280 + 281 x 282 endfor 283 assert_equal(10, nr) 284 END 285 CheckScriptSuccess(lines) 286enddef 287 288def Test_method_call_linebreak() 289 var lines =<< trim END 290 vim9script 291 var res = [] 292 func RetArg( 293 arg 294 ) 295 let s:res = a:arg 296 endfunc 297 [1, 298 2, 299 3]->RetArg() 300 assert_equal([1, 2, 3], res) 301 END 302 CheckScriptSuccess(lines) 303enddef 304 305def Test_skipped_expr_linebreak() 306 if 0 307 var x = [] 308 ->map({ -> 0}) 309 endif 310enddef 311 312def Test_dict_member() 313 var test: dict<list<number>> = {data: [3, 1, 2]} 314 test.data->sort() 315 assert_equal({data: [1, 2, 3]}, test) 316 test.data 317 ->reverse() 318 assert_equal({data: [3, 2, 1]}, test) 319 320 var lines =<< trim END 321 vim9script 322 var test: dict<list<number>> = {data: [3, 1, 2]} 323 test.data->sort() 324 assert_equal({data: [1, 2, 3]}, test) 325 END 326 CheckScriptSuccess(lines) 327enddef 328 329def Test_bar_after_command() 330 def RedrawAndEcho() 331 var x = 'did redraw' 332 redraw | echo x 333 enddef 334 RedrawAndEcho() 335 assert_match('did redraw', Screenline(&lines)) 336 337 def CallAndEcho() 338 var x = 'did redraw' 339 reg_executing() | echo x 340 enddef 341 CallAndEcho() 342 assert_match('did redraw', Screenline(&lines)) 343 344 if has('unix') 345 # bar in filter write command does not start new command 346 def WriteToShell() 347 new 348 setline(1, 'some text') 349 w !cat | cat > Xoutfile 350 bwipe! 351 enddef 352 WriteToShell() 353 assert_equal(['some text'], readfile('Xoutfile')) 354 delete('Xoutfile') 355 356 # bar in filter read command does not start new command 357 def ReadFromShell() 358 new 359 r! echo hello there | cat > Xoutfile 360 r !echo again | cat >> Xoutfile 361 bwipe! 362 enddef 363 ReadFromShell() 364 assert_equal(['hello there', 'again'], readfile('Xoutfile')) 365 delete('Xoutfile') 366 endif 367enddef 368 369def Test_filter_is_not_modifier() 370 var tags = [{a: 1, b: 2}, {x: 3, y: 4}] 371 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 }) 372 assert_equal([{x: 3, y: 4}], tags) 373enddef 374 375def Test_command_modifier_filter() 376 var lines =<< trim END 377 final expected = "\nType Name Content\n c \"c piyo" 378 @a = 'hoge' 379 @b = 'fuga' 380 @c = 'piyo' 381 382 assert_equal(execute('filter /piyo/ registers abc'), expected) 383 END 384 CheckDefAndScriptSuccess(lines) 385enddef 386 387def Test_win_command_modifiers() 388 assert_equal(1, winnr('$')) 389 390 set splitright 391 vsplit 392 assert_equal(2, winnr()) 393 close 394 aboveleft vsplit 395 assert_equal(1, winnr()) 396 close 397 set splitright& 398 399 vsplit 400 assert_equal(1, winnr()) 401 close 402 belowright vsplit 403 assert_equal(2, winnr()) 404 close 405 rightbelow vsplit 406 assert_equal(2, winnr()) 407 close 408 409 if has('browse') 410 browse set 411 assert_equal('option-window', expand('%')) 412 close 413 endif 414 415 vsplit 416 botright split 417 assert_equal(3, winnr()) 418 assert_equal(&columns, winwidth(0)) 419 close 420 close 421 422 vsplit 423 topleft split 424 assert_equal(1, winnr()) 425 assert_equal(&columns, winwidth(0)) 426 close 427 close 428 429 gettabinfo()->len()->assert_equal(1) 430 tab split 431 gettabinfo()->len()->assert_equal(2) 432 tabclose 433 434 vertical new 435 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0)) 436 close 437enddef 438 439func Test_command_modifier_confirm() 440 CheckNotGui 441 CheckRunVimInTerminal 442 443 " Test for saving all the modified buffers 444 let lines =<< trim END 445 call setline(1, 'changed') 446 def Getout() 447 confirm write Xfile 448 enddef 449 END 450 call writefile(lines, 'Xconfirmscript') 451 call writefile(['empty'], 'Xfile') 452 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8}) 453 call term_sendkeys(buf, ":call Getout()\n") 454 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000) 455 call term_sendkeys(buf, "y") 456 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000) 457 call term_sendkeys(buf, "\<CR>") 458 call TermWait(buf) 459 call StopVimInTerminal(buf) 460 461 call assert_equal(['changed'], readfile('Xfile')) 462 call delete('Xfile') 463 call delete('.Xfile.swp') " in case Vim was killed 464 call delete('Xconfirmscript') 465endfunc 466 467def Test_command_modifiers_keep() 468 if has('unix') 469 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool) 470 new 471 setline(1, ['one', 'two', 'three']) 472 normal 1Gma 473 normal 2Gmb 474 normal 3Gmc 475 if addRflag 476 set cpo+=R 477 else 478 set cpo-=R 479 endif 480 if keepMarks 481 keepmarks :%!cat 482 else 483 :%!cat 484 endif 485 if hasMarks 486 assert_equal(1, line("'a")) 487 assert_equal(2, line("'b")) 488 assert_equal(3, line("'c")) 489 else 490 assert_equal(0, line("'a")) 491 assert_equal(0, line("'b")) 492 assert_equal(0, line("'c")) 493 endif 494 quit! 495 enddef 496 DoTest(false, false, true) 497 DoTest(true, false, false) 498 DoTest(false, true, true) 499 DoTest(true, true, true) 500 set cpo&vim 501 502 new 503 setline(1, ['one', 'two', 'three', 'four']) 504 assert_equal(4, line("$")) 505 normal 1Gma 506 normal 2Gmb 507 normal 3Gmc 508 lockmarks :1,2!wc 509 # line is deleted, marks don't move 510 assert_equal(3, line("$")) 511 assert_equal('four', getline(3)) 512 assert_equal(1, line("'a")) 513 assert_equal(2, line("'b")) 514 assert_equal(3, line("'c")) 515 quit! 516 endif 517 518 edit Xone 519 edit Xtwo 520 assert_equal('Xone', expand('#')) 521 keepalt edit Xthree 522 assert_equal('Xone', expand('#')) 523 524 normal /a*b* 525 assert_equal('a*b*', histget("search")) 526 keeppatterns normal /c*d* 527 assert_equal('a*b*', histget("search")) 528 529 new 530 setline(1, range(10)) 531 :10 532 normal gg 533 assert_equal(10, getpos("''")[1]) 534 keepjumps normal 5G 535 assert_equal(10, getpos("''")[1]) 536 quit! 537enddef 538 539def Test_command_modifier_other() 540 new Xsomefile 541 setline(1, 'changed') 542 var buf = bufnr() 543 hide edit Xotherfile 544 var info = getbufinfo(buf) 545 assert_equal(1, info[0].hidden) 546 assert_equal(1, info[0].changed) 547 edit Xsomefile 548 bwipe! 549 550 au BufNewFile Xfile g:readFile = 1 551 | g:readExtra = 2 552 g:readFile = 0 553 g:readExtra = 0 554 edit Xfile 555 assert_equal(1, g:readFile) 556 assert_equal(2, g:readExtra) 557 bwipe! 558 g:readFile = 0 559 noautocmd edit Xfile 560 assert_equal(0, g:readFile) 561 au! BufNewFile 562 563 au BufNewFile Xfile g:readFile = 1 564 | g:readExtra = 2 565 | g:readMore = 3 566 g:readFile = 0 567 g:readExtra = 0 568 g:readMore = 0 569 edit Xfile 570 assert_equal(1, g:readFile) 571 assert_equal(2, g:readExtra) 572 assert_equal(3, g:readMore) 573 bwipe! 574 au! BufNewFile 575 unlet g:readFile 576 unlet g:readExtra 577 unlet g:readMore 578 579 noswapfile edit XnoSwap 580 assert_equal(0, &l:swapfile) 581 bwipe! 582 583 var caught = false 584 try 585 sandbox !ls 586 catch /E48:/ 587 caught = true 588 endtry 589 assert_true(caught) 590 591 :8verbose g:verbose_now = &verbose 592 assert_equal(8, g:verbose_now) 593 unlet g:verbose_now 594enddef 595 596def EchoHere() 597 echomsg 'here' 598enddef 599def EchoThere() 600 unsilent echomsg 'there' 601enddef 602 603def Test_modifier_silent_unsilent() 604 echomsg 'last one' 605 silent echomsg "text" 606 assert_equal("\nlast one", execute(':1messages')) 607 608 silent! echoerr "error" 609 610 echomsg 'last one' 611 silent EchoHere() 612 assert_equal("\nlast one", execute(':1messages')) 613 614 silent EchoThere() 615 assert_equal("\nthere", execute(':1messages')) 616 617 try 618 silent eval [][0] 619 catch 620 echomsg "caught" 621 endtry 622 assert_equal("\ncaught", execute(':1messages')) 623enddef 624 625def Test_range_after_command_modifier() 626 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2) 627 new 628 setline(1, 'xxx') 629 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _']) 630 assert_equal('', getline(1)) 631 bwipe! 632enddef 633 634def Test_eval_command() 635 var from = 3 636 var to = 5 637 g:val = 111 638 def Increment(nrs: list<number>) 639 for nr in nrs 640 g:val += nr 641 endfor 642 enddef 643 eval range(from, to) 644 ->Increment() 645 assert_equal(111 + 3 + 4 + 5, g:val) 646 unlet g:val 647 648 var lines =<< trim END 649 vim9script 650 g:caught = 'no' 651 try 652 eval 123 || 0 653 catch 654 g:caught = 'yes' 655 endtry 656 assert_equal('yes', g:caught) 657 unlet g:caught 658 END 659 CheckScriptSuccess(lines) 660enddef 661 662def Test_map_command() 663 var lines =<< trim END 664 nnoremap <F3> :echo 'hit F3 #'<CR> 665 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n")) 666 END 667 CheckDefSuccess(lines) 668 CheckScriptSuccess(['vim9script'] + lines) 669enddef 670 671def Test_normal_command() 672 new 673 setline(1, 'doesnotexist') 674 var caught = 0 675 try 676 exe "norm! \<C-]>" 677 catch /E433/ 678 caught = 2 679 endtry 680 assert_equal(2, caught) 681 682 try 683 exe "norm! 3\<C-]>" 684 catch /E433/ 685 caught = 3 686 endtry 687 assert_equal(3, caught) 688 bwipe! 689enddef 690 691def Test_put_command() 692 new 693 @p = 'ppp' 694 put p 695 assert_equal('ppp', getline(2)) 696 697 put ='below' 698 assert_equal('below', getline(3)) 699 put! ='above' 700 assert_equal('above', getline(3)) 701 assert_equal('below', getline(4)) 702 703 # compute range at runtime 704 setline(1, range(1, 8)) 705 @a = 'aaa' 706 :$-2put a 707 assert_equal('aaa', getline(7)) 708 709 setline(1, range(1, 8)) 710 :2 711 :+2put! a 712 assert_equal('aaa', getline(4)) 713 714 bwipe! 715 716 CheckDefFailure(['put =xxx'], 'E1001:') 717enddef 718 719def Test_put_with_linebreak() 720 new 721 var lines =<< trim END 722 vim9script 723 pu =split('abc', '\zs') 724 ->join() 725 END 726 CheckScriptSuccess(lines) 727 getline(2)->assert_equal('a b c') 728 bwipe! 729enddef 730 731def Test_command_star_range() 732 new 733 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar']) 734 setpos("'<", [0, 1, 0, 0]) 735 setpos("'>", [0, 3, 0, 0]) 736 :*s/\(foo\|bar\)/baz/g 737 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz']) 738 739 bwipe! 740enddef 741 742def Test_f_args() 743 var lines =<< trim END 744 vim9script 745 746 func SaveCmdArgs(...) 747 let g:args = a:000 748 endfunc 749 750 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>) 751 752 TestFArgs 753 assert_equal([], g:args) 754 755 TestFArgs one two three 756 assert_equal(['one', 'two', 'three'], g:args) 757 END 758 CheckScriptSuccess(lines) 759enddef 760 761def Test_star_command() 762 var lines =<< trim END 763 vim9script 764 @s = 'g:success = 8' 765 set cpo+=* 766 exe '*s' 767 assert_equal(8, g:success) 768 unlet g:success 769 set cpo-=* 770 assert_fails("exe '*s'", 'E1050:') 771 END 772 CheckScriptSuccess(lines) 773enddef 774 775def Test_cmd_argument_without_colon() 776 new Xfile 777 setline(1, ['a', 'b', 'c', 'd']) 778 write 779 edit +3 % 780 assert_equal(3, getcurpos()[1]) 781 edit +/a % 782 assert_equal(1, getcurpos()[1]) 783 bwipe 784 delete('Xfile') 785enddef 786 787def Test_ambiguous_user_cmd() 788 var lines =<< trim END 789 com Cmd1 eval 0 790 com Cmd2 eval 0 791 Cmd 792 END 793 CheckScriptFailure(lines, 'E464:') 794enddef 795 796def Test_command_not_recognized() 797 var lines =<< trim END 798 d.key = 'asdf' 799 END 800 CheckDefFailure(lines, 'E1146:', 1) 801 802 lines =<< trim END 803 d['key'] = 'asdf' 804 END 805 CheckDefFailure(lines, 'E1146:', 1) 806enddef 807 808def Test_magic_not_used() 809 new 810 for cmd in ['set magic', 'set nomagic'] 811 exe cmd 812 setline(1, 'aaa') 813 s/.../bbb/ 814 assert_equal('bbb', getline(1)) 815 endfor 816 817 set magic 818 setline(1, 'aaa') 819 assert_fails('s/.\M../bbb/', 'E486:') 820 assert_fails('snomagic/.../bbb/', 'E486:') 821 assert_equal('aaa', getline(1)) 822 823 bwipe! 824enddef 825 826def Test_gdefault_not_used() 827 new 828 for cmd in ['set gdefault', 'set nogdefault'] 829 exe cmd 830 setline(1, 'aaa') 831 s/./b/ 832 assert_equal('baa', getline(1)) 833 endfor 834 835 set nogdefault 836 bwipe! 837enddef 838 839def g:SomeComplFunc(findstart: number, base: string): any 840 if findstart 841 return 0 842 else 843 return ['aaa', 'bbb'] 844 endif 845enddef 846 847def Test_insert_complete() 848 # this was running into an error with the matchparen hack 849 new 850 set completefunc=SomeComplFunc 851 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx') 852 assert_equal('aaa', getline(1)) 853 854 set completefunc= 855 bwipe! 856enddef 857 858" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 859