test_usercommands.vim (26472B)
1 " Tests for user defined commands 2 3 source check.vim 4 source screendump.vim 5 source vim9.vim 6 7 " Test for <mods> in user defined commands 8 function Test_cmdmods() 9 let g:mods = '' 10 11 command! -nargs=* MyCmd let g:mods = '<mods>' 12 13 MyCmd 14 call assert_equal('', g:mods) 15 aboveleft MyCmd 16 call assert_equal('aboveleft', g:mods) 17 abo MyCmd 18 call assert_equal('aboveleft', g:mods) 19 belowright MyCmd 20 call assert_equal('belowright', g:mods) 21 bel MyCmd 22 call assert_equal('belowright', g:mods) 23 botright MyCmd 24 call assert_equal('botright', g:mods) 25 bo MyCmd 26 call assert_equal('botright', g:mods) 27 browse MyCmd 28 call assert_equal('browse', g:mods) 29 bro MyCmd 30 call assert_equal('browse', g:mods) 31 confirm MyCmd 32 call assert_equal('confirm', g:mods) 33 conf MyCmd 34 call assert_equal('confirm', g:mods) 35 hide MyCmd 36 call assert_equal('hide', g:mods) 37 hid MyCmd 38 call assert_equal('hide', g:mods) 39 keepalt MyCmd 40 call assert_equal('keepalt', g:mods) 41 keepa MyCmd 42 call assert_equal('keepalt', g:mods) 43 keepjumps MyCmd 44 call assert_equal('keepjumps', g:mods) 45 keepj MyCmd 46 call assert_equal('keepjumps', g:mods) 47 keepmarks MyCmd 48 call assert_equal('keepmarks', g:mods) 49 kee MyCmd 50 call assert_equal('keepmarks', g:mods) 51 keeppatterns MyCmd 52 call assert_equal('keeppatterns', g:mods) 53 keepp MyCmd 54 call assert_equal('keeppatterns', g:mods) 55 leftabove MyCmd " results in :aboveleft 56 call assert_equal('aboveleft', g:mods) 57 lefta MyCmd 58 call assert_equal('aboveleft', g:mods) 59 lockmarks MyCmd 60 call assert_equal('lockmarks', g:mods) 61 loc MyCmd 62 call assert_equal('lockmarks', g:mods) 63 noautocmd MyCmd 64 call assert_equal('noautocmd', g:mods) 65 noa MyCmd 66 call assert_equal('noautocmd', g:mods) 67 noswapfile MyCmd 68 call assert_equal('noswapfile', g:mods) 69 nos MyCmd 70 call assert_equal('noswapfile', g:mods) 71 rightbelow MyCmd " results in :belowright 72 call assert_equal('belowright', g:mods) 73 rightb MyCmd 74 call assert_equal('belowright', g:mods) 75 " sandbox MyCmd 76 silent MyCmd 77 call assert_equal('silent', g:mods) 78 sil MyCmd 79 call assert_equal('silent', g:mods) 80 silent! MyCmd 81 call assert_equal('silent!', g:mods) 82 sil! MyCmd 83 call assert_equal('silent!', g:mods) 84 tab MyCmd 85 call assert_equal('tab', g:mods) 86 0tab MyCmd 87 call assert_equal('0tab', g:mods) 88 tab split 89 tab MyCmd 90 call assert_equal('tab', g:mods) 91 1tab MyCmd 92 call assert_equal('1tab', g:mods) 93 tabprev 94 tab MyCmd 95 call assert_equal('tab', g:mods) 96 2tab MyCmd 97 call assert_equal('2tab', g:mods) 98 2tabclose 99 topleft MyCmd 100 call assert_equal('topleft', g:mods) 101 to MyCmd 102 call assert_equal('topleft', g:mods) 103 unsilent MyCmd 104 call assert_equal('unsilent', g:mods) 105 uns MyCmd 106 call assert_equal('unsilent', g:mods) 107 verbose MyCmd 108 call assert_equal('verbose', g:mods) 109 verb MyCmd 110 call assert_equal('verbose', g:mods) 111 0verbose MyCmd 112 call assert_equal('0verbose', g:mods) 113 3verbose MyCmd 114 call assert_equal('3verbose', g:mods) 115 999verbose MyCmd 116 call assert_equal('999verbose', g:mods) 117 vertical MyCmd 118 call assert_equal('vertical', g:mods) 119 vert MyCmd 120 call assert_equal('vertical', g:mods) 121 horizontal MyCmd 122 call assert_equal('horizontal', g:mods) 123 hor MyCmd 124 call assert_equal('horizontal', g:mods) 125 126 aboveleft belowright botright browse confirm hide keepalt keepjumps 127 \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent 128 \ tab topleft unsilent verbose vertical MyCmd 129 130 call assert_equal('browse confirm hide keepalt keepjumps ' . 131 \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' . 132 \ 'silent verbose aboveleft belowright botright tab topleft vertical', 133 \ g:mods) 134 135 let g:mods = '' 136 command! -nargs=* MyQCmd let g:mods .= '<q-mods> ' 137 138 vertical MyQCmd 139 call assert_equal('"vertical" ', g:mods) 140 141 delcommand MyCmd 142 delcommand MyQCmd 143 unlet g:mods 144 endfunction 145 146 func SaveCmdArgs(...) 147 let g:args = a:000 148 endfunc 149 150 func Test_f_args() 151 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>) 152 153 TestFArgs 154 call assert_equal([], g:args) 155 156 TestFArgs one two three 157 call assert_equal(['one', 'two', 'three'], g:args) 158 159 TestFArgs one\\two three 160 call assert_equal(['one\two', 'three'], g:args) 161 162 TestFArgs one\ two three 163 call assert_equal(['one two', 'three'], g:args) 164 165 TestFArgs one\"two three 166 call assert_equal(['one\"two', 'three'], g:args) 167 168 delcommand TestFArgs 169 endfunc 170 171 func Test_q_args() 172 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>) 173 174 TestQArgs 175 call assert_equal([''], g:args) 176 177 TestQArgs one two three 178 call assert_equal(['one two three'], g:args) 179 180 TestQArgs one\\two three 181 call assert_equal(['one\\two three'], g:args) 182 183 TestQArgs one\ two three 184 call assert_equal(['one\ two three'], g:args) 185 186 TestQArgs one\"two three 187 call assert_equal(['one\"two three'], g:args) 188 189 delcommand TestQArgs 190 endfunc 191 192 func Test_reg_arg() 193 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>") 194 195 TestRegArg 196 call assert_equal(['', ''], g:args) 197 198 TestRegArg x 199 call assert_equal(['x', 'x'], g:args) 200 201 delcommand TestRegArg 202 endfunc 203 204 func Test_no_arg() 205 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>") 206 207 TestNoArg 208 call assert_equal(['', '<>', '<x>', '<'], g:args) 209 210 TestNoArg one 211 call assert_equal(['one', '<>', '<x>', '<'], g:args) 212 213 delcommand TestNoArg 214 endfunc 215 216 func Test_range_arg() 217 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>) 218 new 219 call setline(1, range(100)) 220 let lnum = line('.') 221 222 TestRangeArg 223 call assert_equal([0, lnum, lnum], g:args) 224 225 99TestRangeArg 226 call assert_equal([1, 99, 99], g:args) 227 228 88,99TestRangeArg 229 call assert_equal([2, 88, 99], g:args) 230 231 call assert_fails('102TestRangeArg', 'E16:') 232 233 bwipe! 234 delcommand TestRangeArg 235 endfunc 236 237 func Test_Ambiguous() 238 command Doit let g:didit = 'yes' 239 command Dothat let g:didthat = 'also' 240 call assert_fails('Do', 'E464:') 241 Doit 242 call assert_equal('yes', g:didit) 243 Dothat 244 call assert_equal('also', g:didthat) 245 unlet g:didit 246 unlet g:didthat 247 248 delcommand Doit 249 Do 250 call assert_equal('also', g:didthat) 251 delcommand Dothat 252 253 " Nvim removed the ":Ni!" easter egg in 87e107d92. 254 call assert_fails("\x4ei\041", 'E492: Not an editor command: Ni!') 255 endfunc 256 257 func Test_redefine_on_reload() 258 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists') 259 call assert_equal(0, exists(':ExistingCommand')) 260 source Xcommandexists 261 call assert_equal(2, exists(':ExistingCommand')) 262 " Redefining a command when reloading a script is OK. 263 source Xcommandexists 264 call assert_equal(2, exists(':ExistingCommand')) 265 266 " But redefining in another script is not OK. 267 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2') 268 call assert_fails('source Xcommandexists2', 'E174:') 269 call delete('Xcommandexists2') 270 271 " And defining twice in one script is not OK. 272 delcommand ExistingCommand 273 call assert_equal(0, exists(':ExistingCommand')) 274 call writefile([ 275 \ 'command ExistingCommand echo "yes"', 276 \ 'command ExistingCommand echo "no"', 277 \ ], 'Xcommandexists') 278 call assert_fails('source Xcommandexists', 'E174:') 279 call assert_equal(2, exists(':ExistingCommand')) 280 281 call delete('Xcommandexists') 282 delcommand ExistingCommand 283 endfunc 284 285 func Test_CmdUndefined() 286 call assert_fails('Doit', 'E492:') 287 au CmdUndefined Doit :command Doit let g:didit = 'yes' 288 Doit 289 call assert_equal('yes', g:didit) 290 delcommand Doit 291 292 call assert_fails('Dothat', 'E492:') 293 au CmdUndefined * let g:didnot = 'yes' 294 call assert_fails('Dothat', 'E492:') 295 call assert_equal('yes', g:didnot) 296 endfunc 297 298 func Test_CmdErrors() 299 call assert_fails('com! docmd :', 'E183:') 300 call assert_fails('com! \<Tab> :', 'E182:') 301 call assert_fails('com! _ :', 'E182:') 302 call assert_fails('com! - DoCmd :', 'E175:') 303 call assert_fails('com! -xxx DoCmd :', 'E181:') 304 call assert_fails('com! -addr DoCmd :', 'E179:') 305 call assert_fails('com! -addr=asdf DoCmd :', 'E180:') 306 call assert_fails('com! -complete DoCmd :', 'E179:') 307 call assert_fails('com! -complete=xxx DoCmd :', 'E180:') 308 call assert_fails('com! -complete=custom DoCmd :', 'E467:') 309 call assert_fails('com! -complete=customlist DoCmd :', 'E467:') 310 " call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:') 311 call assert_fails('com! -nargs=x DoCmd :', 'E176:') 312 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:') 313 call assert_fails('com! -count=x DoCmd :', 'E178:') 314 call assert_fails('com! -range=x DoCmd :', 'E178:') 315 316 call assert_fails('com! -complete=file DoCmd :', 'E1208:') 317 call assert_fails('com! -nargs=0 -complete=file DoCmd :', 'E1208:') 318 319 let lines =<< trim END 320 vim9script 321 com! -complete=file DoCmd : 322 END 323 call CheckScriptFailure(lines, 'E1208:', 2) 324 325 let lines =<< trim END 326 vim9script 327 com! -nargs=0 -complete=file DoCmd : 328 END 329 call CheckScriptFailure(lines, 'E1208:', 2) 330 331 com! -nargs=0 DoCmd : 332 call assert_fails('DoCmd x', 'E488:') 333 334 com! -nargs=1 DoCmd : 335 call assert_fails('DoCmd', 'E471:') 336 337 com! -nargs=+ DoCmd : 338 call assert_fails('DoCmd', 'E471:') 339 340 call assert_fails('com DoCmd :', 'E174:') 341 comclear 342 call assert_fails('delcom DoCmd', 'E184:') 343 344 " These used to leak memory 345 call assert_fails('com! -complete=custom,CustomComplete _ :', 'E182:') 346 call assert_fails('com! -complete=custom,CustomComplete docmd :', 'E183:') 347 call assert_fails('com! -complete=custom,CustomComplete -xxx DoCmd :', 'E181:') 348 endfunc 349 350 func CustomComplete(A, L, P) 351 return "January\nFebruary\nMars\n" 352 endfunc 353 354 func CustomCompleteList(A, L, P) 355 return [ "Monday", "Tuesday", "Wednesday", {}, v:_null_string] 356 endfunc 357 358 func Test_CmdCompletion() 359 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx') 360 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:) 361 362 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx') 363 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:) 364 365 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx') 366 call assert_equal('"com -nargs=* + 0 1 ?', @:) 367 368 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx') 369 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:) 370 371 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx') 372 call assert_equal('"com -complete=color command compiler', @:) 373 374 " try completion for unsupported argument values 375 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx') 376 call assert_equal("\"com -newarg=\t", @:) 377 378 " command completion after the name in a user defined command 379 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx') 380 call assert_equal("\"com MyCmd chistory", @:) 381 382 " delete the Check commands to avoid them showing up 383 call feedkeys(":com Check\<C-A>\<C-B>\"\<CR>", 'tx') 384 let cmds = substitute(@:, '"com ', '', '')->split() 385 for cmd in cmds 386 exe 'delcommand ' .. cmd 387 endfor 388 delcommand MissingFeature 389 390 command! DoCmd1 : 391 command! DoCmd2 : 392 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx') 393 call assert_equal('"com DoCmd1 DoCmd2', @:) 394 395 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx') 396 call assert_equal('"DoCmd1 DoCmd2', @:) 397 398 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx') 399 call assert_equal('"delcom DoCmd1 DoCmd2', @:) 400 401 " try argument completion for a command without completion 402 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx') 403 call assert_equal("\"DoCmd1 \t", @:) 404 405 delcom DoCmd1 406 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx') 407 call assert_equal('"delcom DoCmd2', @:) 408 409 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx') 410 call assert_equal('"com DoCmd2', @:) 411 412 delcom DoCmd2 413 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx') 414 call assert_equal('"delcom DoC', @:) 415 416 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx') 417 call assert_equal('"com DoC', @:) 418 419 " com! -nargs=1 -complete=behave DoCmd : 420 " call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 421 " call assert_equal('"DoCmd mswin xterm', @:) 422 423 com! -nargs=1 -complete=retab DoCmd : 424 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 425 call assert_equal('"DoCmd -indentonly', @:) 426 427 " Test for file name completion 428 com! -nargs=1 -complete=file DoCmd : 429 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx') 430 call assert_equal('"DoCmd README.txt', @:) 431 432 " Test for buffer name completion 433 com! -nargs=1 -complete=buffer DoCmd : 434 let bnum = bufadd('BufForUserCmd') 435 call setbufvar(bnum, '&buflisted', 1) 436 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx') 437 call assert_equal('"DoCmd BufForUserCmd', @:) 438 bwipe BufForUserCmd 439 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx') 440 call assert_equal('"DoCmd BufFor', @:) 441 442 com! -nargs=* -complete=custom,CustomComplete DoCmd : 443 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 444 call assert_equal('"DoCmd January February Mars', @:) 445 446 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd : 447 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 448 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:) 449 450 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd : 451 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:') 452 453 com! -nargs=+ -complete=customlist,CustomComp DoCmd : 454 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:') 455 456 " custom completion without a function 457 com! -nargs=? -complete=custom, DoCmd 458 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')") 459 460 " custom completion failure with the wrong function 461 com! -nargs=? -complete=custom,min DoCmd 462 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:') 463 464 " custom completion for a pattern with a backslash 465 let g:ArgLead = '' 466 func! CustCompl(A, L, P) 467 let g:ArgLead = a:A 468 return ['one', 'two', 'three'] 469 endfunc 470 com! -nargs=? -complete=customlist,CustCompl DoCmd 471 call feedkeys(":DoCmd a\\\t", 'xt') 472 call assert_equal('a\', g:ArgLead) 473 delfunc CustCompl 474 475 delcom DoCmd 476 endfunc 477 478 func CallExecute(A, L, P) 479 " Drop first '\n' 480 return execute('echo "hi"')[1:] 481 endfunc 482 483 func Test_use_execute_in_completion() 484 command! -nargs=* -complete=custom,CallExecute DoExec : 485 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx') 486 call assert_equal('"DoExec hi', @:) 487 delcommand DoExec 488 endfunc 489 490 func Test_addr_all() 491 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2> 492 %DoSomething 493 call assert_equal(1, g:a1) 494 call assert_equal(line('$'), g:a2) 495 496 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2> 497 args one two three 498 %DoSomething 499 call assert_equal(1, g:a1) 500 call assert_equal(3, g:a2) 501 502 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 503 %DoSomething 504 for low in range(1, bufnr('$')) 505 if buflisted(low) 506 break 507 endif 508 endfor 509 call assert_equal(low, g:a1) 510 call assert_equal(bufnr('$'), g:a2) 511 512 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 513 %DoSomething 514 for low in range(1, bufnr('$')) 515 if bufloaded(low) 516 break 517 endif 518 endfor 519 call assert_equal(low, g:a1) 520 for up in range(bufnr('$'), 1, -1) 521 if bufloaded(up) 522 break 523 endif 524 endfor 525 call assert_equal(up, g:a2) 526 527 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2> 528 new 529 %DoSomething 530 call assert_equal(1, g:a1) 531 call assert_equal(winnr('$'), g:a2) 532 bwipe 533 534 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2> 535 tabnew 536 %DoSomething 537 call assert_equal(1, g:a1) 538 call assert_equal(len(gettabinfo()), g:a2) 539 bwipe 540 541 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2> 542 DoSomething 543 call assert_equal(line('.'), g:a1) 544 call assert_equal(line('.'), g:a2) 545 %DoSomething 546 call assert_equal(1, g:a1) 547 call assert_equal(line('$'), g:a2) 548 549 delcommand DoSomething 550 endfunc 551 552 func Test_command_list() 553 command! DoCmd : 554 call assert_equal("\n Name Args Address Complete Definition" 555 \ .. "\n DoCmd 0 :", 556 \ execute('command DoCmd')) 557 558 " Test with various -range= and -count= argument values. 559 command! -range DoCmd : 560 call assert_equal("\n Name Args Address Complete Definition" 561 \ .. "\n DoCmd 0 . :", 562 \ execute('command DoCmd')) 563 command! -range=% DoCmd : 564 call assert_equal("\n Name Args Address Complete Definition" 565 \ .. "\n DoCmd 0 % :", 566 \ execute('command! DoCmd')) 567 command! -range=2 DoCmd : 568 call assert_equal("\n Name Args Address Complete Definition" 569 \ .. "\n DoCmd 0 2 :", 570 \ execute('command DoCmd')) 571 command! -count=2 DoCmd : 572 call assert_equal("\n Name Args Address Complete Definition" 573 \ .. "\n DoCmd 0 2c ? :", 574 \ execute('command DoCmd')) 575 576 " Test with various -addr= argument values. 577 command! -addr=lines DoCmd : 578 call assert_equal("\n Name Args Address Complete Definition" 579 \ .. "\n DoCmd 0 . :", 580 \ execute('command DoCmd')) 581 command! -addr=arguments DoCmd : 582 call assert_equal("\n Name Args Address Complete Definition" 583 \ .. "\n DoCmd 0 . arg :", 584 \ execute('command DoCmd')) 585 command! -addr=buffers DoCmd : 586 call assert_equal("\n Name Args Address Complete Definition" 587 \ .. "\n DoCmd 0 . buf :", 588 \ execute('command DoCmd')) 589 command! -addr=loaded_buffers DoCmd : 590 call assert_equal("\n Name Args Address Complete Definition" 591 \ .. "\n DoCmd 0 . load :", 592 \ execute('command DoCmd')) 593 command! -addr=windows DoCmd : 594 call assert_equal("\n Name Args Address Complete Definition" 595 \ .. "\n DoCmd 0 . win :", 596 \ execute('command DoCmd')) 597 command! -addr=tabs DoCmd : 598 call assert_equal("\n Name Args Address Complete Definition" 599 \ .. "\n DoCmd 0 . tab :", 600 \ execute('command DoCmd')) 601 command! -addr=other DoCmd : 602 call assert_equal("\n Name Args Address Complete Definition" 603 \ .. "\n DoCmd 0 . ? :", 604 \ execute('command DoCmd')) 605 606 " Test with various -complete= argument values (non-exhaustive list) 607 command! -nargs=1 -complete=arglist DoCmd : 608 call assert_equal("\n Name Args Address Complete Definition" 609 \ .. "\n DoCmd 1 arglist :", 610 \ execute('command DoCmd')) 611 command! -nargs=* -complete=augroup DoCmd : 612 call assert_equal("\n Name Args Address Complete Definition" 613 \ .. "\n DoCmd * augroup :", 614 \ execute('command DoCmd')) 615 command! -nargs=? -complete=custom,CustomComplete DoCmd : 616 call assert_equal("\n Name Args Address Complete Definition" 617 \ .. "\n DoCmd ? custom :", 618 \ execute('command DoCmd')) 619 command! -nargs=+ -complete=customlist,CustomComplete DoCmd : 620 call assert_equal("\n Name Args Address Complete Definition" 621 \ .. "\n DoCmd + customlist :", 622 \ execute('command DoCmd')) 623 624 " Test with various -narg= argument values. 625 command! -nargs=0 DoCmd : 626 call assert_equal("\n Name Args Address Complete Definition" 627 \ .. "\n DoCmd 0 :", 628 \ execute('command DoCmd')) 629 command! -nargs=1 DoCmd : 630 call assert_equal("\n Name Args Address Complete Definition" 631 \ .. "\n DoCmd 1 :", 632 \ execute('command DoCmd')) 633 command! -nargs=* DoCmd : 634 call assert_equal("\n Name Args Address Complete Definition" 635 \ .. "\n DoCmd * :", 636 \ execute('command DoCmd')) 637 command! -nargs=? DoCmd : 638 call assert_equal("\n Name Args Address Complete Definition" 639 \ .. "\n DoCmd ? :", 640 \ execute('command DoCmd')) 641 command! -nargs=+ DoCmd : 642 call assert_equal("\n Name Args Address Complete Definition" 643 \ .. "\n DoCmd + :", 644 \ execute('command DoCmd')) 645 646 " Test with other arguments. 647 command! -bang DoCmd : 648 call assert_equal("\n Name Args Address Complete Definition" 649 \ .. "\n! DoCmd 0 :", 650 \ execute('command DoCmd')) 651 command! -bar DoCmd : 652 call assert_equal("\n Name Args Address Complete Definition" 653 \ .. "\n| DoCmd 0 :", 654 \ execute('command DoCmd')) 655 command! -register DoCmd : 656 call assert_equal("\n Name Args Address Complete Definition" 657 \ .. "\n\" DoCmd 0 :", 658 \ execute('command DoCmd')) 659 command! -buffer DoCmd : 660 call assert_equal("\n Name Args Address Complete Definition" 661 \ .. "\nb DoCmd 0 :" 662 \ .. "\n\" DoCmd 0 :", 663 \ execute('command DoCmd')) 664 comclear 665 666 " Test with many args. 667 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd : 668 call assert_equal("\n Name Args Address Complete Definition" 669 \ .. "\n!\"b|DoCmd + 3c win environment :", 670 \ execute('command DoCmd')) 671 comclear 672 673 " Test with special characters in command definition. 674 command! DoCmd :<cr><tab><c-d> 675 call assert_equal("\n Name Args Address Complete Definition" 676 \ .. "\n DoCmd 0 :<CR><Tab><C-D>", 677 \ execute('command DoCmd')) 678 679 " Test output in verbose mode. 680 command! DoCmd : 681 call assert_match("^\n" 682 \ .. " Name Args Address Complete Definition\n" 683 \ .. " DoCmd 0 :\n" 684 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$", 685 \ execute('verbose command DoCmd')) 686 687 comclear 688 call assert_equal("\nNo user-defined commands found", execute(':command Xxx')) 689 call assert_equal("\nNo user-defined commands found", execute('command')) 690 endfunc 691 692 " Test for a custom user completion returning the wrong value type 693 func Test_usercmd_custom() 694 func T1(a, c, p) 695 return "a\nb\n" 696 endfunc 697 command -nargs=* -complete=customlist,T1 TCmd1 698 call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt') 699 call assert_equal('"TCmd1 ', @:) 700 delcommand TCmd1 701 delfunc T1 702 703 func T2(a, c, p) 704 return {} 705 endfunc 706 command -nargs=* -complete=customlist,T2 TCmd2 707 call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt') 708 call assert_equal('"TCmd2 ', @:) 709 delcommand TCmd2 710 delfunc T2 711 endfunc 712 713 func Test_delcommand_buffer() 714 command Global echo 'global' 715 command -buffer OneBuffer echo 'one' 716 new 717 command -buffer TwoBuffer echo 'two' 718 call assert_equal(0, exists(':OneBuffer')) 719 call assert_equal(2, exists(':Global')) 720 call assert_equal(2, exists(':TwoBuffer')) 721 delcommand -buffer TwoBuffer 722 call assert_equal(0, exists(':TwoBuffer')) 723 call assert_fails('delcommand -buffer Global', 'E1237:') 724 call assert_fails('delcommand -buffer OneBuffer', 'E1237:') 725 bwipe! 726 call assert_equal(2, exists(':OneBuffer')) 727 delcommand -buffer OneBuffer 728 call assert_equal(0, exists(':OneBuffer')) 729 call assert_fails('delcommand -buffer Global', 'E1237:') 730 delcommand Global 731 call assert_equal(0, exists(':Global')) 732 endfunc 733 734 func DefCmd(name) 735 if len(a:name) > 30 736 return 737 endif 738 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")' 739 echo a:name 740 exe a:name 741 endfunc 742 743 func Test_recursive_define() 744 call DefCmd('Command') 745 746 let name = 'Command' 747 while len(name) <= 30 748 exe 'delcommand ' .. name 749 let name ..= 'x' 750 endwhile 751 endfunc 752 753 " Test for using buffer-local ambiguous user-defined commands 754 func Test_buflocal_ambiguous_usercmd() 755 new 756 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello" 757 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World" 758 759 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:') 760 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt') 761 call assert_equal('"TestCmd ', @:) 762 763 delcommand TestCmd1 764 delcommand TestCmd2 765 bw! 766 endfunc 767 768 " Test for using buffer-local user command from cmdwin. 769 func Test_buflocal_usercmd_cmdwin() 770 new 771 command -buffer TestCmd edit Test 772 " This used to crash Vim 773 call assert_fails("norm q::TestCmd\<CR>", 'E11:') 774 bw! 775 endfunc 776 777 " Test for using a multibyte character in a user command 778 func Test_multibyte_in_usercmd() 779 command SubJapanesePeriodToDot exe "%s/\u3002/./g" 780 new 781 call setline(1, "Hello\u3002") 782 SubJapanesePeriodToDot 783 call assert_equal('Hello.', getline(1)) 784 bw! 785 delcommand SubJapanesePeriodToDot 786 endfunc 787 788 " Test for listing user commands. 789 func Test_command_list_0() 790 " Check space padding of attribute and name in command list 791 set vbs& 792 command! ShortCommand echo "ShortCommand" 793 command! VeryMuchLongerCommand echo "VeryMuchLongerCommand" 794 795 redi @"> | com | redi END 796 pu 797 798 let bl = matchbufline(bufnr('%'), "^ ShortCommand 0", 1, '$') 799 call assert_false(bl == []) 800 let bl = matchbufline(bufnr('%'), "^ VeryMuchLongerCommand 0", 1, '$') 801 call assert_false(bl == []) 802 803 bwipe! 804 delcommand ShortCommand 805 delcommand VeryMuchLongerCommand 806 endfunc 807 808 " vim: shiftwidth=2 sts=2 expandtab