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  delcom DoCmd
368endfunc
369
370func CallExecute(A, L, P)
371  " Drop first '\n'
372  return execute('echo "hi"')[1:]
373endfunc
374
375func Test_use_execute_in_completion()
376  command! -nargs=* -complete=custom,CallExecute DoExec :
377  call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
378  call assert_equal('"DoExec hi', @:)
379  delcommand DoExec
380endfunc
381
382func Test_addr_all()
383  command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
384  %DoSomething
385  call assert_equal(1, g:a1)
386  call assert_equal(line('$'), g:a2)
387
388  command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
389  args one two three
390  %DoSomething
391  call assert_equal(1, g:a1)
392  call assert_equal(3, g:a2)
393
394  command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
395  %DoSomething
396  for low in range(1, bufnr('$'))
397    if buflisted(low)
398      break
399    endif
400  endfor
401  call assert_equal(low, g:a1)
402  call assert_equal(bufnr('$'), g:a2)
403
404  command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
405  %DoSomething
406  for low in range(1, bufnr('$'))
407    if bufloaded(low)
408      break
409    endif
410  endfor
411  call assert_equal(low, g:a1)
412  for up in range(bufnr('$'), 1, -1)
413    if bufloaded(up)
414      break
415    endif
416  endfor
417  call assert_equal(up, g:a2)
418
419  command! -addr=windows DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
420  new
421  %DoSomething
422  call assert_equal(1, g:a1)
423  call assert_equal(winnr('$'), g:a2)
424  bwipe
425
426  command! -addr=tabs DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
427  tabnew
428  %DoSomething
429  call assert_equal(1, g:a1)
430  call assert_equal(len(gettabinfo()), g:a2)
431  bwipe
432
433  command! -addr=other DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
434  DoSomething
435  call assert_equal(line('.'), g:a1)
436  call assert_equal(line('.'), g:a2)
437  %DoSomething
438  call assert_equal(1, g:a1)
439  call assert_equal(line('$'), g:a2)
440
441  delcommand DoSomething
442endfunc
443
444func Test_command_list()
445  command! DoCmd :
446  call assert_equal("\n    Name              Args Address Complete    Definition"
447        \        .. "\n    DoCmd             0                        :",
448        \           execute('command DoCmd'))
449
450  " Test with various -range= and -count= argument values.
451  command! -range DoCmd :
452  call assert_equal("\n    Name              Args Address Complete    Definition"
453        \        .. "\n    DoCmd             0    .                   :",
454        \           execute('command DoCmd'))
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=2 DoCmd :
460  call assert_equal("\n    Name              Args Address Complete    Definition"
461        \        .. "\n    DoCmd             0    2                   :",
462        \           execute('command DoCmd'))
463  command! -count=2 DoCmd :
464  call assert_equal("\n    Name              Args Address Complete    Definition"
465        \        .. "\n    DoCmd             0    2c ?                :",
466        \           execute('command DoCmd'))
467
468  " Test with various -addr= argument values.
469  command! -addr=lines DoCmd :
470  call assert_equal("\n    Name              Args Address Complete    Definition"
471        \        .. "\n    DoCmd             0    .                   :",
472        \           execute('command DoCmd'))
473  command! -addr=arguments DoCmd :
474  call assert_equal("\n    Name              Args Address Complete    Definition"
475        \        .. "\n    DoCmd             0    .  arg              :",
476        \           execute('command DoCmd'))
477  command! -addr=buffers DoCmd :
478  call assert_equal("\n    Name              Args Address Complete    Definition"
479        \        .. "\n    DoCmd             0    .  buf              :",
480        \           execute('command DoCmd'))
481  command! -addr=loaded_buffers DoCmd :
482  call assert_equal("\n    Name              Args Address Complete    Definition"
483        \        .. "\n    DoCmd             0    .  load             :",
484        \           execute('command DoCmd'))
485  command! -addr=windows DoCmd :
486  call assert_equal("\n    Name              Args Address Complete    Definition"
487        \        .. "\n    DoCmd             0    .  win              :",
488        \           execute('command DoCmd'))
489  command! -addr=tabs DoCmd :
490  call assert_equal("\n    Name              Args Address Complete    Definition"
491        \        .. "\n    DoCmd             0    .  tab              :",
492        \           execute('command DoCmd'))
493  command! -addr=other DoCmd :
494  call assert_equal("\n    Name              Args Address Complete    Definition"
495        \        .. "\n    DoCmd             0    .  ?                :",
496        \           execute('command DoCmd'))
497
498  " Test with various -complete= argument values (non-exhaustive list)
499  command! -complete=arglist DoCmd :
500  call assert_equal("\n    Name              Args Address Complete    Definition"
501        \        .. "\n    DoCmd             0            arglist     :",
502        \           execute('command DoCmd'))
503  command! -complete=augroup DoCmd :
504  call assert_equal("\n    Name              Args Address Complete    Definition"
505        \        .. "\n    DoCmd             0            augroup     :",
506        \           execute('command DoCmd'))
507  command! -complete=custom,CustomComplete DoCmd :
508  call assert_equal("\n    Name              Args Address Complete    Definition"
509        \        .. "\n    DoCmd             0            custom      :",
510        \           execute('command DoCmd'))
511  command! -complete=customlist,CustomComplete DoCmd :
512  call assert_equal("\n    Name              Args Address Complete    Definition"
513        \        .. "\n    DoCmd             0            customlist  :",
514        \           execute('command DoCmd'))
515
516  " Test with various -narg= argument values.
517  command! -nargs=0 DoCmd :
518  call assert_equal("\n    Name              Args Address Complete    Definition"
519        \        .. "\n    DoCmd             0                        :",
520        \           execute('command DoCmd'))
521  command! -nargs=1 DoCmd :
522  call assert_equal("\n    Name              Args Address Complete    Definition"
523        \        .. "\n    DoCmd             1                        :",
524        \           execute('command DoCmd'))
525  command! -nargs=* DoCmd :
526  call assert_equal("\n    Name              Args Address Complete    Definition"
527        \        .. "\n    DoCmd             *                        :",
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
538  " Test with other arguments.
539  command! -bang DoCmd :
540  call assert_equal("\n    Name              Args Address Complete    Definition"
541        \        .. "\n!   DoCmd             0                        :",
542        \           execute('command DoCmd'))
543  command! -bar DoCmd :
544  call assert_equal("\n    Name              Args Address Complete    Definition"
545        \        .. "\n|   DoCmd             0                        :",
546        \           execute('command DoCmd'))
547  command! -register DoCmd :
548  call assert_equal("\n    Name              Args Address Complete    Definition"
549        \        .. "\n\"   DoCmd             0                        :",
550        \           execute('command DoCmd'))
551  command! -buffer DoCmd :
552  call assert_equal("\n    Name              Args Address Complete    Definition"
553        \        .. "\nb   DoCmd             0                        :"
554        \        .. "\n\"   DoCmd             0                        :",
555        \           execute('command DoCmd'))
556  comclear
557
558  " Test with many args.
559  command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
560  call assert_equal("\n    Name              Args Address Complete    Definition"
561        \        .. "\n!\"b|DoCmd             +    3c win  environment :",
562        \           execute('command DoCmd'))
563  comclear
564
565  " Test with special characters in command definition.
566  command! DoCmd :<cr><tab><c-d>
567  call assert_equal("\n    Name              Args Address Complete    Definition"
568        \        .. "\n    DoCmd             0                        :<CR><Tab><C-D>",
569        \           execute('command DoCmd'))
570
571  " Test output in verbose mode.
572  command! DoCmd :
573  call assert_match("^\n"
574        \        .. "    Name              Args Address Complete    Definition\n"
575        \        .. "    DoCmd             0                        :\n"
576        \        .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
577        \           execute('verbose command DoCmd'))
578
579  comclear
580  call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
581  call assert_equal("\nNo user-defined commands found", execute('command'))
582endfunc
583
584" Test for a custom user completion returning the wrong value type
585func Test_usercmd_custom()
586  func T1(a, c, p)
587    return "a\nb\n"
588  endfunc
589  command -nargs=* -complete=customlist,T1 TCmd1
590  call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
591  call assert_equal('"TCmd1 ', @:)
592  delcommand TCmd1
593  delfunc T1
594
595  func T2(a, c, p)
596    return {}
597  endfunc
598  command -nargs=* -complete=customlist,T2 TCmd2
599  call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
600  call assert_equal('"TCmd2 ', @:)
601  delcommand TCmd2
602  delfunc T2
603endfunc
604
605" vim: shiftwidth=2 sts=2 expandtab
606