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:') 330endfunc 331 332func CallExecute(A, L, P) 333 " Drop first '\n' 334 return execute('echo "hi"')[1:] 335endfunc 336 337func Test_use_execute_in_completion() 338 command! -nargs=* -complete=custom,CallExecute DoExec : 339 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx') 340 call assert_equal('"DoExec hi', @:) 341 delcommand DoExec 342endfunc 343 344func Test_addr_all() 345 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2> 346 %DoSomething 347 call assert_equal(1, g:a1) 348 call assert_equal(line('$'), g:a2) 349 350 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2> 351 args one two three 352 %DoSomething 353 call assert_equal(1, g:a1) 354 call assert_equal(3, g:a2) 355 356 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 357 %DoSomething 358 for low in range(1, bufnr('$')) 359 if buflisted(low) 360 break 361 endif 362 endfor 363 call assert_equal(low, g:a1) 364 call assert_equal(bufnr('$'), g:a2) 365 366 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 367 %DoSomething 368 for low in range(1, bufnr('$')) 369 if bufloaded(low) 370 break 371 endif 372 endfor 373 call assert_equal(low, g:a1) 374 for up in range(bufnr('$'), 1, -1) 375 if bufloaded(up) 376 break 377 endif 378 endfor 379 call assert_equal(up, g:a2) 380 381 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2> 382 new 383 %DoSomething 384 call assert_equal(1, g:a1) 385 call assert_equal(winnr('$'), g:a2) 386 bwipe 387 388 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2> 389 tabnew 390 %DoSomething 391 call assert_equal(1, g:a1) 392 call assert_equal(len(gettabinfo()), g:a2) 393 bwipe 394 395 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2> 396 DoSomething 397 call assert_equal(line('.'), g:a1) 398 call assert_equal(line('.'), g:a2) 399 %DoSomething 400 call assert_equal(1, g:a1) 401 call assert_equal(line('$'), g:a2) 402 403 delcommand DoSomething 404endfunc 405 406func Test_command_list() 407 command! DoCmd : 408 call assert_equal("\n Name Args Address Complete Definition" 409 \ .. "\n DoCmd 0 :", 410 \ execute('command DoCmd')) 411 412 " Test with various -range= and -count= argument values. 413 command! -range DoCmd : 414 call assert_equal("\n Name Args Address Complete Definition" 415 \ .. "\n DoCmd 0 . :", 416 \ execute('command DoCmd')) 417 command! -range=% DoCmd : 418 call assert_equal("\n Name Args Address Complete Definition" 419 \ .. "\n DoCmd 0 % :", 420 \ execute('command! DoCmd')) 421 command! -range=2 DoCmd : 422 call assert_equal("\n Name Args Address Complete Definition" 423 \ .. "\n DoCmd 0 2 :", 424 \ execute('command DoCmd')) 425 command! -count=2 DoCmd : 426 call assert_equal("\n Name Args Address Complete Definition" 427 \ .. "\n DoCmd 0 2c ? :", 428 \ execute('command DoCmd')) 429 430 " Test with various -addr= argument values. 431 command! -addr=lines DoCmd : 432 call assert_equal("\n Name Args Address Complete Definition" 433 \ .. "\n DoCmd 0 . :", 434 \ execute('command DoCmd')) 435 command! -addr=arguments DoCmd : 436 call assert_equal("\n Name Args Address Complete Definition" 437 \ .. "\n DoCmd 0 . arg :", 438 \ execute('command DoCmd')) 439 command! -addr=buffers DoCmd : 440 call assert_equal("\n Name Args Address Complete Definition" 441 \ .. "\n DoCmd 0 . buf :", 442 \ execute('command DoCmd')) 443 command! -addr=loaded_buffers DoCmd : 444 call assert_equal("\n Name Args Address Complete Definition" 445 \ .. "\n DoCmd 0 . load :", 446 \ execute('command DoCmd')) 447 command! -addr=windows DoCmd : 448 call assert_equal("\n Name Args Address Complete Definition" 449 \ .. "\n DoCmd 0 . win :", 450 \ execute('command DoCmd')) 451 command! -addr=tabs DoCmd : 452 call assert_equal("\n Name Args Address Complete Definition" 453 \ .. "\n DoCmd 0 . tab :", 454 \ execute('command DoCmd')) 455 command! -addr=other DoCmd : 456 call assert_equal("\n Name Args Address Complete Definition" 457 \ .. "\n DoCmd 0 . ? :", 458 \ execute('command DoCmd')) 459 460 " Test with various -complete= argument values (non-exhaustive list) 461 command! -complete=arglist DoCmd : 462 call assert_equal("\n Name Args Address Complete Definition" 463 \ .. "\n DoCmd 0 arglist :", 464 \ execute('command DoCmd')) 465 command! -complete=augroup DoCmd : 466 call assert_equal("\n Name Args Address Complete Definition" 467 \ .. "\n DoCmd 0 augroup :", 468 \ execute('command DoCmd')) 469 command! -complete=custom,CustomComplete DoCmd : 470 call assert_equal("\n Name Args Address Complete Definition" 471 \ .. "\n DoCmd 0 custom :", 472 \ execute('command DoCmd')) 473 command! -complete=customlist,CustomComplete DoCmd : 474 call assert_equal("\n Name Args Address Complete Definition" 475 \ .. "\n DoCmd 0 customlist :", 476 \ execute('command DoCmd')) 477 478 " Test with various -narg= argument values. 479 command! -nargs=0 DoCmd : 480 call assert_equal("\n Name Args Address Complete Definition" 481 \ .. "\n DoCmd 0 :", 482 \ execute('command DoCmd')) 483 command! -nargs=1 DoCmd : 484 call assert_equal("\n Name Args Address Complete Definition" 485 \ .. "\n DoCmd 1 :", 486 \ execute('command DoCmd')) 487 command! -nargs=* DoCmd : 488 call assert_equal("\n Name Args Address Complete Definition" 489 \ .. "\n DoCmd * :", 490 \ execute('command DoCmd')) 491 command! -nargs=? DoCmd : 492 call assert_equal("\n Name Args Address Complete Definition" 493 \ .. "\n DoCmd ? :", 494 \ execute('command DoCmd')) 495 command! -nargs=+ DoCmd : 496 call assert_equal("\n Name Args Address Complete Definition" 497 \ .. "\n DoCmd + :", 498 \ execute('command DoCmd')) 499 500 " Test with other arguments. 501 command! -bang DoCmd : 502 call assert_equal("\n Name Args Address Complete Definition" 503 \ .. "\n! DoCmd 0 :", 504 \ execute('command DoCmd')) 505 command! -bar DoCmd : 506 call assert_equal("\n Name Args Address Complete Definition" 507 \ .. "\n| DoCmd 0 :", 508 \ execute('command DoCmd')) 509 command! -register DoCmd : 510 call assert_equal("\n Name Args Address Complete Definition" 511 \ .. "\n\" DoCmd 0 :", 512 \ execute('command DoCmd')) 513 command! -buffer DoCmd : 514 call assert_equal("\n Name Args Address Complete Definition" 515 \ .. "\nb DoCmd 0 :" 516 \ .. "\n\" DoCmd 0 :", 517 \ execute('command DoCmd')) 518 comclear 519 520 " Test with many args. 521 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd : 522 call assert_equal("\n Name Args Address Complete Definition" 523 \ .. "\n!\"b|DoCmd + 3c win environment :", 524 \ execute('command DoCmd')) 525 comclear 526 527 " Test with special characters in command definition. 528 command! DoCmd :<cr><tab><c-d> 529 call assert_equal("\n Name Args Address Complete Definition" 530 \ .. "\n DoCmd 0 :<CR><Tab><C-D>", 531 \ execute('command DoCmd')) 532 533 " Test output in verbose mode. 534 command! DoCmd : 535 call assert_match("^\n" 536 \ .. " Name Args Address Complete Definition\n" 537 \ .. " DoCmd 0 :\n" 538 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$", 539 \ execute('verbose command DoCmd')) 540 541 comclear 542 call assert_equal("\nNo user-defined commands found", execute(':command Xxx')) 543 call assert_equal("\nNo user-defined commands found", execute('command')) 544endfunc 545