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