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! -nargs=x DoCmd :', 'E176:')
274  call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
275  call assert_fails('com! -count=x DoCmd :', 'E178:')
276  call assert_fails('com! -range=x DoCmd :', 'E178:')
277
278  com! -nargs=0 DoCmd :
279  call assert_fails('DoCmd x', 'E488:')
280
281  com! -nargs=1 DoCmd :
282  call assert_fails('DoCmd', 'E471:')
283
284  com! -nargs=+ DoCmd :
285  call assert_fails('DoCmd', 'E471:')
286
287  call assert_fails('com DoCmd :', 'E174:')
288  comclear
289  call assert_fails('delcom DoCmd', 'E184:')
290endfunc
291
292func CustomComplete(A, L, P)
293  return "January\nFebruary\nMars\n"
294endfunc
295
296func CustomCompleteList(A, L, P)
297  return [ "Monday", "Tuesday", "Wednesday", {}]
298endfunc
299
300func Test_CmdCompletion()
301  call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
302  call assert_equal('"com -addr bang bar buffer complete count nargs range register', @:)
303
304  call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
305  call assert_equal('"com -nargs=0 -addr bang bar buffer complete count nargs range register', @:)
306
307  call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
308  call assert_equal('"com -nargs=* + 0 1 ?', @:)
309
310  call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
311  call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
312
313  call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
314  call assert_equal('"com -complete=color command compiler', @:)
315
316  command! DoCmd1 :
317  command! DoCmd2 :
318  call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
319  call assert_equal('"com DoCmd1 DoCmd2', @:)
320
321  call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
322  call assert_equal('"DoCmd1 DoCmd2', @:)
323
324  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
325  call assert_equal('"delcom DoCmd1 DoCmd2', @:)
326
327  delcom DoCmd1
328  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
329  call assert_equal('"delcom DoCmd2', @:)
330
331  call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
332  call assert_equal('"com DoCmd2', @:)
333
334  delcom DoCmd2
335  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
336  call assert_equal('"delcom DoC', @:)
337
338  call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
339  call assert_equal('"com DoC', @:)
340
341  com! -complete=behave DoCmd :
342  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
343  call assert_equal('"DoCmd mswin xterm', @:)
344
345  " This does not work. Why?
346  "call feedkeys(":DoCmd x\<C-A>\<C-B>\"\<CR>", 'tx')
347  "call assert_equal('"DoCmd xterm', @:)
348
349  com! -complete=custom,CustomComplete DoCmd :
350  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
351  call assert_equal('"DoCmd January February Mars', @:)
352
353  com! -complete=customlist,CustomCompleteList DoCmd :
354  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
355  call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
356
357  com! -complete=custom,CustomCompleteList DoCmd :
358  call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
359
360  com! -complete=customlist,CustomComp DoCmd :
361  call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
362
363  " custom completion without a function
364  com! -complete=custom, DoCmd
365  call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
366
367  " custom completion failure with the wrong function
368  com! -complete=custom,min DoCmd
369  call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
370
371  delcom DoCmd
372endfunc
373
374func CallExecute(A, L, P)
375  " Drop first '\n'
376  return execute('echo "hi"')[1:]
377endfunc
378
379func Test_use_execute_in_completion()
380  command! -nargs=* -complete=custom,CallExecute DoExec :
381  call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
382  call assert_equal('"DoExec hi', @:)
383  delcommand DoExec
384endfunc
385
386func Test_addr_all()
387  command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
388  %DoSomething
389  call assert_equal(1, g:a1)
390  call assert_equal(line('$'), g:a2)
391
392  command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
393  args one two three
394  %DoSomething
395  call assert_equal(1, g:a1)
396  call assert_equal(3, g:a2)
397
398  command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
399  %DoSomething
400  for low in range(1, bufnr('$'))
401    if buflisted(low)
402      break
403    endif
404  endfor
405  call assert_equal(low, g:a1)
406  call assert_equal(bufnr('$'), g:a2)
407
408  command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
409  %DoSomething
410  for low in range(1, bufnr('$'))
411    if bufloaded(low)
412      break
413    endif
414  endfor
415  call assert_equal(low, g:a1)
416  for up in range(bufnr('$'), 1, -1)
417    if bufloaded(up)
418      break
419    endif
420  endfor
421  call assert_equal(up, g:a2)
422
423  command! -addr=windows DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
424  new
425  %DoSomething
426  call assert_equal(1, g:a1)
427  call assert_equal(winnr('$'), g:a2)
428  bwipe
429
430  command! -addr=tabs DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
431  tabnew
432  %DoSomething
433  call assert_equal(1, g:a1)
434  call assert_equal(len(gettabinfo()), g:a2)
435  bwipe
436
437  command! -addr=other DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
438  DoSomething
439  call assert_equal(line('.'), g:a1)
440  call assert_equal(line('.'), g:a2)
441  %DoSomething
442  call assert_equal(1, g:a1)
443  call assert_equal(line('$'), g:a2)
444
445  delcommand DoSomething
446endfunc
447
448func Test_command_list()
449  command! DoCmd :
450  call assert_equal("\n    Name              Args Address Complete    Definition"
451        \        .. "\n    DoCmd             0                        :",
452        \           execute('command DoCmd'))
453
454  " Test with various -range= and -count= argument values.
455  command! -range DoCmd :
456  call assert_equal("\n    Name              Args Address Complete    Definition"
457        \        .. "\n    DoCmd             0    .                   :",
458        \           execute('command DoCmd'))
459  command! -range=% DoCmd :
460  call assert_equal("\n    Name              Args Address Complete    Definition"
461        \        .. "\n    DoCmd             0    %                   :",
462        \           execute('command! DoCmd'))
463  command! -range=2 DoCmd :
464  call assert_equal("\n    Name              Args Address Complete    Definition"
465        \        .. "\n    DoCmd             0    2                   :",
466        \           execute('command DoCmd'))
467  command! -count=2 DoCmd :
468  call assert_equal("\n    Name              Args Address Complete    Definition"
469        \        .. "\n    DoCmd             0    2c ?                :",
470        \           execute('command DoCmd'))
471
472  " Test with various -addr= argument values.
473  command! -addr=lines DoCmd :
474  call assert_equal("\n    Name              Args Address Complete    Definition"
475        \        .. "\n    DoCmd             0    .                   :",
476        \           execute('command DoCmd'))
477  command! -addr=arguments DoCmd :
478  call assert_equal("\n    Name              Args Address Complete    Definition"
479        \        .. "\n    DoCmd             0    .  arg              :",
480        \           execute('command DoCmd'))
481  command! -addr=buffers DoCmd :
482  call assert_equal("\n    Name              Args Address Complete    Definition"
483        \        .. "\n    DoCmd             0    .  buf              :",
484        \           execute('command DoCmd'))
485  command! -addr=loaded_buffers DoCmd :
486  call assert_equal("\n    Name              Args Address Complete    Definition"
487        \        .. "\n    DoCmd             0    .  load             :",
488        \           execute('command DoCmd'))
489  command! -addr=windows DoCmd :
490  call assert_equal("\n    Name              Args Address Complete    Definition"
491        \        .. "\n    DoCmd             0    .  win              :",
492        \           execute('command DoCmd'))
493  command! -addr=tabs DoCmd :
494  call assert_equal("\n    Name              Args Address Complete    Definition"
495        \        .. "\n    DoCmd             0    .  tab              :",
496        \           execute('command DoCmd'))
497  command! -addr=other DoCmd :
498  call assert_equal("\n    Name              Args Address Complete    Definition"
499        \        .. "\n    DoCmd             0    .  ?                :",
500        \           execute('command DoCmd'))
501
502  " Test with various -complete= argument values (non-exhaustive list)
503  command! -complete=arglist DoCmd :
504  call assert_equal("\n    Name              Args Address Complete    Definition"
505        \        .. "\n    DoCmd             0            arglist     :",
506        \           execute('command DoCmd'))
507  command! -complete=augroup DoCmd :
508  call assert_equal("\n    Name              Args Address Complete    Definition"
509        \        .. "\n    DoCmd             0            augroup     :",
510        \           execute('command DoCmd'))
511  command! -complete=custom,CustomComplete DoCmd :
512  call assert_equal("\n    Name              Args Address Complete    Definition"
513        \        .. "\n    DoCmd             0            custom      :",
514        \           execute('command DoCmd'))
515  command! -complete=customlist,CustomComplete DoCmd :
516  call assert_equal("\n    Name              Args Address Complete    Definition"
517        \        .. "\n    DoCmd             0            customlist  :",
518        \           execute('command DoCmd'))
519
520  " Test with various -narg= argument values.
521  command! -nargs=0 DoCmd :
522  call assert_equal("\n    Name              Args Address Complete    Definition"
523        \        .. "\n    DoCmd             0                        :",
524        \           execute('command DoCmd'))
525  command! -nargs=1 DoCmd :
526  call assert_equal("\n    Name              Args Address Complete    Definition"
527        \        .. "\n    DoCmd             1                        :",
528        \           execute('command DoCmd'))
529  command! -nargs=* DoCmd :
530  call assert_equal("\n    Name              Args Address Complete    Definition"
531        \        .. "\n    DoCmd             *                        :",
532        \           execute('command DoCmd'))
533  command! -nargs=? DoCmd :
534  call assert_equal("\n    Name              Args Address Complete    Definition"
535        \        .. "\n    DoCmd             ?                        :",
536        \           execute('command DoCmd'))
537  command! -nargs=+ DoCmd :
538  call assert_equal("\n    Name              Args Address Complete    Definition"
539        \        .. "\n    DoCmd             +                        :",
540        \           execute('command DoCmd'))
541
542  " Test with other arguments.
543  command! -bang DoCmd :
544  call assert_equal("\n    Name              Args Address Complete    Definition"
545        \        .. "\n!   DoCmd             0                        :",
546        \           execute('command DoCmd'))
547  command! -bar DoCmd :
548  call assert_equal("\n    Name              Args Address Complete    Definition"
549        \        .. "\n|   DoCmd             0                        :",
550        \           execute('command DoCmd'))
551  command! -register DoCmd :
552  call assert_equal("\n    Name              Args Address Complete    Definition"
553        \        .. "\n\"   DoCmd             0                        :",
554        \           execute('command DoCmd'))
555  command! -buffer DoCmd :
556  call assert_equal("\n    Name              Args Address Complete    Definition"
557        \        .. "\nb   DoCmd             0                        :"
558        \        .. "\n\"   DoCmd             0                        :",
559        \           execute('command DoCmd'))
560  comclear
561
562  " Test with many args.
563  command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
564  call assert_equal("\n    Name              Args Address Complete    Definition"
565        \        .. "\n!\"b|DoCmd             +    3c win  environment :",
566        \           execute('command DoCmd'))
567  comclear
568
569  " Test with special characters in command definition.
570  command! DoCmd :<cr><tab><c-d>
571  call assert_equal("\n    Name              Args Address Complete    Definition"
572        \        .. "\n    DoCmd             0                        :<CR><Tab><C-D>",
573        \           execute('command DoCmd'))
574
575  " Test output in verbose mode.
576  command! DoCmd :
577  call assert_match("^\n"
578        \        .. "    Name              Args Address Complete    Definition\n"
579        \        .. "    DoCmd             0                        :\n"
580        \        .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
581        \           execute('verbose command DoCmd'))
582
583  comclear
584  call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
585  call assert_equal("\nNo user-defined commands found", execute('command'))
586endfunc
587
588" Test for a custom user completion returning the wrong value type
589func Test_usercmd_custom()
590  func T1(a, c, p)
591    return "a\nb\n"
592  endfunc
593  command -nargs=* -complete=customlist,T1 TCmd1
594  call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
595  call assert_equal('"TCmd1 ', @:)
596  delcommand TCmd1
597  delfunc T1
598
599  func T2(a, c, p)
600    return {}
601  endfunc
602  command -nargs=* -complete=customlist,T2 TCmd2
603  call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
604  call assert_equal('"TCmd2 ', @:)
605  delcommand TCmd2
606  delfunc T2
607endfunc
608
609" vim: shiftwidth=2 sts=2 expandtab
610