1" Tests for user defined commands 2 3" Test for <mods> in user defined commands 4function Test_cmdmods() 5 let g:mods = '' 6 7 command! -nargs=* MyCmd let g:mods = '<mods>' 8 9 MyCmd 10 call assert_equal('', g:mods) 11 aboveleft MyCmd 12 call assert_equal('aboveleft', g:mods) 13 abo MyCmd 14 call assert_equal('aboveleft', g:mods) 15 belowright MyCmd 16 call assert_equal('belowright', g:mods) 17 bel MyCmd 18 call assert_equal('belowright', g:mods) 19 botright MyCmd 20 call assert_equal('botright', g:mods) 21 bo MyCmd 22 call assert_equal('botright', g:mods) 23 browse MyCmd 24 call assert_equal('browse', g:mods) 25 bro MyCmd 26 call assert_equal('browse', g:mods) 27 confirm MyCmd 28 call assert_equal('confirm', g:mods) 29 conf MyCmd 30 call assert_equal('confirm', g:mods) 31 hide MyCmd 32 call assert_equal('hide', g:mods) 33 hid MyCmd 34 call assert_equal('hide', g:mods) 35 keepalt MyCmd 36 call assert_equal('keepalt', g:mods) 37 keepa MyCmd 38 call assert_equal('keepalt', g:mods) 39 keepjumps MyCmd 40 call assert_equal('keepjumps', g:mods) 41 keepj MyCmd 42 call assert_equal('keepjumps', g:mods) 43 keepmarks MyCmd 44 call assert_equal('keepmarks', g:mods) 45 kee MyCmd 46 call assert_equal('keepmarks', g:mods) 47 keeppatterns MyCmd 48 call assert_equal('keeppatterns', g:mods) 49 keepp MyCmd 50 call assert_equal('keeppatterns', g:mods) 51 leftabove MyCmd " results in :aboveleft 52 call assert_equal('aboveleft', g:mods) 53 lefta MyCmd 54 call assert_equal('aboveleft', g:mods) 55 lockmarks MyCmd 56 call assert_equal('lockmarks', g:mods) 57 loc MyCmd 58 call assert_equal('lockmarks', g:mods) 59 " noautocmd MyCmd 60 noswapfile MyCmd 61 call assert_equal('noswapfile', g:mods) 62 nos MyCmd 63 call assert_equal('noswapfile', g:mods) 64 rightbelow MyCmd " results in :belowright 65 call assert_equal('belowright', g:mods) 66 rightb MyCmd 67 call assert_equal('belowright', g:mods) 68 " sandbox MyCmd 69 silent MyCmd 70 call assert_equal('silent', g:mods) 71 sil MyCmd 72 call assert_equal('silent', g:mods) 73 tab MyCmd 74 call assert_equal('tab', g:mods) 75 topleft MyCmd 76 call assert_equal('topleft', g:mods) 77 to MyCmd 78 call assert_equal('topleft', g:mods) 79 " unsilent MyCmd 80 verbose MyCmd 81 call assert_equal('verbose', g:mods) 82 verb MyCmd 83 call assert_equal('verbose', g:mods) 84 vertical MyCmd 85 call assert_equal('vertical', g:mods) 86 vert MyCmd 87 call assert_equal('vertical', g:mods) 88 89 aboveleft belowright botright browse confirm hide keepalt keepjumps 90 \ keepmarks keeppatterns lockmarks noswapfile silent tab 91 \ topleft verbose vertical MyCmd 92 93 call assert_equal('browse confirm hide keepalt keepjumps ' . 94 \ 'keepmarks keeppatterns lockmarks noswapfile silent ' . 95 \ 'verbose aboveleft belowright botright tab topleft vertical', g:mods) 96 97 let g:mods = '' 98 command! -nargs=* MyQCmd let g:mods .= '<q-mods> ' 99 100 vertical MyQCmd 101 call assert_equal('"vertical" ', g:mods) 102 103 delcommand MyCmd 104 delcommand MyQCmd 105 unlet g:mods 106endfunction 107 108func SaveCmdArgs(...) 109 let g:args = a:000 110endfunc 111 112func Test_f_args() 113 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>) 114 115 TestFArgs 116 call assert_equal([], g:args) 117 118 TestFArgs one two three 119 call assert_equal(['one', 'two', 'three'], g:args) 120 121 TestFArgs one\\two three 122 call assert_equal(['one\two', 'three'], g:args) 123 124 TestFArgs one\ two three 125 call assert_equal(['one two', 'three'], g:args) 126 127 TestFArgs one\"two three 128 call assert_equal(['one\"two', 'three'], g:args) 129 130 delcommand TestFArgs 131endfunc 132 133func Test_q_args() 134 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>) 135 136 TestQArgs 137 call assert_equal([''], g:args) 138 139 TestQArgs one two three 140 call assert_equal(['one two three'], g:args) 141 142 TestQArgs one\\two three 143 call assert_equal(['one\\two three'], g:args) 144 145 TestQArgs one\ two three 146 call assert_equal(['one\ two three'], g:args) 147 148 TestQArgs one\"two three 149 call assert_equal(['one\"two three'], g:args) 150 151 delcommand TestQArgs 152endfunc 153 154func Test_reg_arg() 155 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>") 156 157 TestRegArg 158 call assert_equal(['', ''], g:args) 159 160 TestRegArg x 161 call assert_equal(['x', 'x'], g:args) 162 163 delcommand TestRegArg 164endfunc 165 166func Test_no_arg() 167 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>") 168 169 TestNoArg 170 call assert_equal(['', '<>', '<x>', '<'], g:args) 171 172 TestNoArg one 173 call assert_equal(['one', '<>', '<x>', '<'], g:args) 174 175 delcommand TestNoArg 176endfunc 177 178func Test_range_arg() 179 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>) 180 new 181 call setline(1, range(100)) 182 let lnum = line('.') 183 184 TestRangeArg 185 call assert_equal([0, lnum, lnum], g:args) 186 187 99TestRangeArg 188 call assert_equal([1, 99, 99], g:args) 189 190 88,99TestRangeArg 191 call assert_equal([2, 88, 99], g:args) 192 193 call assert_fails('102TestRangeArg', 'E16:') 194 195 bwipe! 196 delcommand TestRangeArg 197endfunc 198 199func Test_Ambiguous() 200 command Doit let g:didit = 'yes' 201 command Dothat let g:didthat = 'also' 202 call assert_fails('Do', 'E464:') 203 Doit 204 call assert_equal('yes', g:didit) 205 Dothat 206 call assert_equal('also', g:didthat) 207 unlet g:didit 208 unlet g:didthat 209 210 delcommand Doit 211 Do 212 call assert_equal('also', g:didthat) 213 delcommand Dothat 214 215 call assert_fails("\x4ei\041", ' you demand a ') 216endfunc 217 218func Test_redefine_on_reload() 219 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists') 220 call assert_equal(0, exists(':ExistingCommand')) 221 source Xcommandexists 222 call assert_equal(2, exists(':ExistingCommand')) 223 " Redefining a command when reloading a script is OK. 224 source Xcommandexists 225 call assert_equal(2, exists(':ExistingCommand')) 226 227 " But redefining in another script is not OK. 228 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2') 229 call assert_fails('source Xcommandexists2', 'E174:') 230 call delete('Xcommandexists2') 231 232 " And defining twice in one script is not OK. 233 delcommand ExistingCommand 234 call assert_equal(0, exists(':ExistingCommand')) 235 call writefile([ 236 \ 'command ExistingCommand echo "yes"', 237 \ 'command ExistingCommand echo "no"', 238 \ ], 'Xcommandexists') 239 call assert_fails('source Xcommandexists', 'E174:') 240 call assert_equal(2, exists(':ExistingCommand')) 241 242 call delete('Xcommandexists') 243 delcommand ExistingCommand 244endfunc 245 246func Test_CmdUndefined() 247 call assert_fails('Doit', 'E492:') 248 au CmdUndefined Doit :command Doit let g:didit = 'yes' 249 Doit 250 call assert_equal('yes', g:didit) 251 delcommand Doit 252 253 call assert_fails('Dothat', 'E492:') 254 au CmdUndefined * let g:didnot = 'yes' 255 call assert_fails('Dothat', 'E492:') 256 call assert_equal('yes', g:didnot) 257endfunc 258 259func Test_CmdErrors() 260 call assert_fails('com! docmd :', 'E183:') 261 call assert_fails('com! \<Tab> :', 'E182:') 262 call assert_fails('com! _ :', 'E182:') 263 call assert_fails('com! X :', 'E841:') 264 call assert_fails('com! - DoCmd :', 'E175:') 265 call assert_fails('com! -xxx DoCmd :', 'E181:') 266 call assert_fails('com! -addr DoCmd :', 'E179:') 267 call assert_fails('com! -addr=asdf DoCmd :', 'E180:') 268 call assert_fails('com! -complete DoCmd :', 'E179:') 269 call assert_fails('com! -complete=xxx DoCmd :', 'E180:') 270 call assert_fails('com! -complete=custom DoCmd :', 'E467:') 271 call assert_fails('com! -complete=customlist DoCmd :', 'E467:') 272 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:') 273 call assert_fails('com! -complete=file DoCmd :', 'E1208:') 274 call assert_fails('com! -nargs=0 -complete=file DoCmd :', 'E1208:') 275 call assert_fails('com! -nargs=x DoCmd :', 'E176:') 276 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:') 277 call assert_fails('com! -count=x DoCmd :', 'E178:') 278 call assert_fails('com! -range=x DoCmd :', 'E178:') 279 280 com! -nargs=0 DoCmd : 281 call assert_fails('DoCmd x', 'E488:') 282 283 com! -nargs=1 DoCmd : 284 call assert_fails('DoCmd', 'E471:') 285 286 com! -nargs=+ DoCmd : 287 call assert_fails('DoCmd', 'E471:') 288 289 call assert_fails('com DoCmd :', 'E174:') 290 comclear 291 call assert_fails('delcom DoCmd', 'E184:') 292endfunc 293 294func CustomComplete(A, L, P) 295 return "January\nFebruary\nMars\n" 296endfunc 297 298func CustomCompleteList(A, L, P) 299 return [ "Monday", "Tuesday", "Wednesday", {}] 300endfunc 301 302func Test_CmdCompletion() 303 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx') 304 call assert_equal('"com -addr bang bar buffer complete count nargs range register', @:) 305 306 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx') 307 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count nargs range register', @:) 308 309 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx') 310 call assert_equal('"com -nargs=* + 0 1 ?', @:) 311 312 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx') 313 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:) 314 315 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx') 316 call assert_equal('"com -complete=color command compiler', @:) 317 318 command! DoCmd1 : 319 command! DoCmd2 : 320 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx') 321 call assert_equal('"com DoCmd1 DoCmd2', @:) 322 323 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx') 324 call assert_equal('"DoCmd1 DoCmd2', @:) 325 326 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx') 327 call assert_equal('"delcom DoCmd1 DoCmd2', @:) 328 329 delcom DoCmd1 330 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx') 331 call assert_equal('"delcom DoCmd2', @:) 332 333 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx') 334 call assert_equal('"com DoCmd2', @:) 335 336 delcom DoCmd2 337 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx') 338 call assert_equal('"delcom DoC', @:) 339 340 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx') 341 call assert_equal('"com DoC', @:) 342 343 com! -nargs=1 -complete=behave DoCmd : 344 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 345 call assert_equal('"DoCmd mswin xterm', @:) 346 347 com! -nargs=* -complete=custom,CustomComplete DoCmd : 348 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 349 call assert_equal('"DoCmd January February Mars', @:) 350 351 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd : 352 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 353 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:) 354 355 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd : 356 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:') 357 358 com! -nargs=+ -complete=customlist,CustomComp DoCmd : 359 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:') 360 361 " custom completion without a function 362 com! -nargs=? -complete=custom, DoCmd 363 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')") 364 365 " custom completion failure with the wrong function 366 com! -nargs=? -complete=custom,min DoCmd 367 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:') 368 369 delcom DoCmd 370endfunc 371 372func CallExecute(A, L, P) 373 " Drop first '\n' 374 return execute('echo "hi"')[1:] 375endfunc 376 377func Test_use_execute_in_completion() 378 command! -nargs=* -complete=custom,CallExecute DoExec : 379 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx') 380 call assert_equal('"DoExec hi', @:) 381 delcommand DoExec 382endfunc 383 384func Test_addr_all() 385 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2> 386 %DoSomething 387 call assert_equal(1, g:a1) 388 call assert_equal(line('$'), g:a2) 389 390 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2> 391 args one two three 392 %DoSomething 393 call assert_equal(1, g:a1) 394 call assert_equal(3, g:a2) 395 396 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 397 %DoSomething 398 for low in range(1, bufnr('$')) 399 if buflisted(low) 400 break 401 endif 402 endfor 403 call assert_equal(low, g:a1) 404 call assert_equal(bufnr('$'), g:a2) 405 406 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 407 %DoSomething 408 for low in range(1, bufnr('$')) 409 if bufloaded(low) 410 break 411 endif 412 endfor 413 call assert_equal(low, g:a1) 414 for up in range(bufnr('$'), 1, -1) 415 if bufloaded(up) 416 break 417 endif 418 endfor 419 call assert_equal(up, g:a2) 420 421 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2> 422 new 423 %DoSomething 424 call assert_equal(1, g:a1) 425 call assert_equal(winnr('$'), g:a2) 426 bwipe 427 428 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2> 429 tabnew 430 %DoSomething 431 call assert_equal(1, g:a1) 432 call assert_equal(len(gettabinfo()), g:a2) 433 bwipe 434 435 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2> 436 DoSomething 437 call assert_equal(line('.'), g:a1) 438 call assert_equal(line('.'), g:a2) 439 %DoSomething 440 call assert_equal(1, g:a1) 441 call assert_equal(line('$'), g:a2) 442 443 delcommand DoSomething 444endfunc 445 446func Test_command_list() 447 command! DoCmd : 448 call assert_equal("\n Name Args Address Complete Definition" 449 \ .. "\n DoCmd 0 :", 450 \ execute('command DoCmd')) 451 452 " Test with various -range= and -count= argument values. 453 command! -range DoCmd : 454 call assert_equal("\n Name Args Address Complete Definition" 455 \ .. "\n DoCmd 0 . :", 456 \ execute('command DoCmd')) 457 command! -range=% DoCmd : 458 call assert_equal("\n Name Args Address Complete Definition" 459 \ .. "\n DoCmd 0 % :", 460 \ execute('command! DoCmd')) 461 command! -range=2 DoCmd : 462 call assert_equal("\n Name Args Address Complete Definition" 463 \ .. "\n DoCmd 0 2 :", 464 \ execute('command DoCmd')) 465 command! -count=2 DoCmd : 466 call assert_equal("\n Name Args Address Complete Definition" 467 \ .. "\n DoCmd 0 2c ? :", 468 \ execute('command DoCmd')) 469 470 " Test with various -addr= argument values. 471 command! -addr=lines DoCmd : 472 call assert_equal("\n Name Args Address Complete Definition" 473 \ .. "\n DoCmd 0 . :", 474 \ execute('command DoCmd')) 475 command! -addr=arguments DoCmd : 476 call assert_equal("\n Name Args Address Complete Definition" 477 \ .. "\n DoCmd 0 . arg :", 478 \ execute('command DoCmd')) 479 command! -addr=buffers DoCmd : 480 call assert_equal("\n Name Args Address Complete Definition" 481 \ .. "\n DoCmd 0 . buf :", 482 \ execute('command DoCmd')) 483 command! -addr=loaded_buffers DoCmd : 484 call assert_equal("\n Name Args Address Complete Definition" 485 \ .. "\n DoCmd 0 . load :", 486 \ execute('command DoCmd')) 487 command! -addr=windows DoCmd : 488 call assert_equal("\n Name Args Address Complete Definition" 489 \ .. "\n DoCmd 0 . win :", 490 \ execute('command DoCmd')) 491 command! -addr=tabs DoCmd : 492 call assert_equal("\n Name Args Address Complete Definition" 493 \ .. "\n DoCmd 0 . tab :", 494 \ execute('command DoCmd')) 495 command! -addr=other DoCmd : 496 call assert_equal("\n Name Args Address Complete Definition" 497 \ .. "\n DoCmd 0 . ? :", 498 \ execute('command DoCmd')) 499 500 " Test with various -complete= argument values (non-exhaustive list) 501 command! -nargs=1 -complete=arglist DoCmd : 502 call assert_equal("\n Name Args Address Complete Definition" 503 \ .. "\n DoCmd 1 arglist :", 504 \ execute('command DoCmd')) 505 command! -nargs=* -complete=augroup DoCmd : 506 call assert_equal("\n Name Args Address Complete Definition" 507 \ .. "\n DoCmd * augroup :", 508 \ execute('command DoCmd')) 509 command! -nargs=? -complete=custom,CustomComplete DoCmd : 510 call assert_equal("\n Name Args Address Complete Definition" 511 \ .. "\n DoCmd ? custom :", 512 \ execute('command DoCmd')) 513 command! -nargs=+ -complete=customlist,CustomComplete DoCmd : 514 call assert_equal("\n Name Args Address Complete Definition" 515 \ .. "\n DoCmd + customlist :", 516 \ execute('command DoCmd')) 517 518 " Test with various -narg= argument values. 519 command! -nargs=0 DoCmd : 520 call assert_equal("\n Name Args Address Complete Definition" 521 \ .. "\n DoCmd 0 :", 522 \ execute('command DoCmd')) 523 command! -nargs=1 DoCmd : 524 call assert_equal("\n Name Args Address Complete Definition" 525 \ .. "\n DoCmd 1 :", 526 \ execute('command DoCmd')) 527 command! -nargs=* DoCmd : 528 call assert_equal("\n Name Args Address Complete Definition" 529 \ .. "\n DoCmd * :", 530 \ execute('command DoCmd')) 531 command! -nargs=? DoCmd : 532 call assert_equal("\n Name Args Address Complete Definition" 533 \ .. "\n DoCmd ? :", 534 \ execute('command DoCmd')) 535 command! -nargs=+ DoCmd : 536 call assert_equal("\n Name Args Address Complete Definition" 537 \ .. "\n DoCmd + :", 538 \ execute('command DoCmd')) 539 540 " Test with other arguments. 541 command! -bang DoCmd : 542 call assert_equal("\n Name Args Address Complete Definition" 543 \ .. "\n! DoCmd 0 :", 544 \ execute('command DoCmd')) 545 command! -bar DoCmd : 546 call assert_equal("\n Name Args Address Complete Definition" 547 \ .. "\n| DoCmd 0 :", 548 \ execute('command DoCmd')) 549 command! -register DoCmd : 550 call assert_equal("\n Name Args Address Complete Definition" 551 \ .. "\n\" DoCmd 0 :", 552 \ execute('command DoCmd')) 553 command! -buffer DoCmd : 554 call assert_equal("\n Name Args Address Complete Definition" 555 \ .. "\nb DoCmd 0 :" 556 \ .. "\n\" DoCmd 0 :", 557 \ execute('command DoCmd')) 558 comclear 559 560 " Test with many args. 561 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd : 562 call assert_equal("\n Name Args Address Complete Definition" 563 \ .. "\n!\"b|DoCmd + 3c win environment :", 564 \ execute('command DoCmd')) 565 comclear 566 567 " Test with special characters in command definition. 568 command! DoCmd :<cr><tab><c-d> 569 call assert_equal("\n Name Args Address Complete Definition" 570 \ .. "\n DoCmd 0 :<CR><Tab><C-D>", 571 \ execute('command DoCmd')) 572 573 " Test output in verbose mode. 574 command! DoCmd : 575 call assert_match("^\n" 576 \ .. " Name Args Address Complete Definition\n" 577 \ .. " DoCmd 0 :\n" 578 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$", 579 \ execute('verbose command DoCmd')) 580 581 comclear 582 call assert_equal("\nNo user-defined commands found", execute(':command Xxx')) 583 call assert_equal("\nNo user-defined commands found", execute('command')) 584endfunc 585 586" Test for a custom user completion returning the wrong value type 587func Test_usercmd_custom() 588 func T1(a, c, p) 589 return "a\nb\n" 590 endfunc 591 command -nargs=* -complete=customlist,T1 TCmd1 592 call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt') 593 call assert_equal('"TCmd1 ', @:) 594 delcommand TCmd1 595 delfunc T1 596 597 func T2(a, c, p) 598 return {} 599 endfunc 600 command -nargs=* -complete=customlist,T2 TCmd2 601 call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt') 602 call assert_equal('"TCmd2 ', @:) 603 delcommand TCmd2 604 delfunc T2 605endfunc 606 607" vim: shiftwidth=2 sts=2 expandtab 608