robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
29.2 KiB916 linesraw
1" Tests for user defined commands
2
3source check.vim
4source screendump.vim
5source vim9.vim
6
7" Test for <mods> in user defined commands
8function Test_cmdmods()
9  let g:mods = ''
10
11  command! -nargs=* MyCmd let g:mods = '<mods>'
12
13  MyCmd
14  call assert_equal('', g:mods)
15  aboveleft MyCmd
16  call assert_equal('aboveleft', g:mods)
17  abo MyCmd
18  call assert_equal('aboveleft', g:mods)
19  belowright MyCmd
20  call assert_equal('belowright', g:mods)
21  bel MyCmd
22  call assert_equal('belowright', g:mods)
23  botright MyCmd
24  call assert_equal('botright', g:mods)
25  bo MyCmd
26  call assert_equal('botright', g:mods)
27  browse MyCmd
28  call assert_equal('browse', g:mods)
29  bro MyCmd
30  call assert_equal('browse', g:mods)
31  confirm MyCmd
32  call assert_equal('confirm', g:mods)
33  conf MyCmd
34  call assert_equal('confirm', g:mods)
35  hide MyCmd
36  call assert_equal('hide', g:mods)
37  hid MyCmd
38  call assert_equal('hide', g:mods)
39  keepalt MyCmd
40  call assert_equal('keepalt', g:mods)
41  keepa MyCmd
42  call assert_equal('keepalt', g:mods)
43  keepjumps MyCmd
44  call assert_equal('keepjumps', g:mods)
45  keepj MyCmd
46  call assert_equal('keepjumps', g:mods)
47  keepmarks MyCmd
48  call assert_equal('keepmarks', g:mods)
49  kee MyCmd
50  call assert_equal('keepmarks', g:mods)
51  keeppatterns MyCmd
52  call assert_equal('keeppatterns', g:mods)
53  keepp MyCmd
54  call assert_equal('keeppatterns', g:mods)
55  leftabove MyCmd  " results in :aboveleft
56  call assert_equal('aboveleft', g:mods)
57  lefta MyCmd
58  call assert_equal('aboveleft', g:mods)
59  lockmarks MyCmd
60  call assert_equal('lockmarks', g:mods)
61  loc MyCmd
62  call assert_equal('lockmarks', g:mods)
63  noautocmd MyCmd
64  call assert_equal('noautocmd', g:mods)
65  noa MyCmd
66  call assert_equal('noautocmd', g:mods)
67  noswapfile MyCmd
68  call assert_equal('noswapfile', g:mods)
69  nos MyCmd
70  call assert_equal('noswapfile', g:mods)
71  rightbelow MyCmd " results in :belowright
72  call assert_equal('belowright', g:mods)
73  rightb MyCmd
74  call assert_equal('belowright', g:mods)
75  " sandbox MyCmd
76  silent MyCmd
77  call assert_equal('silent', g:mods)
78  sil MyCmd
79  call assert_equal('silent', g:mods)
80  silent! MyCmd
81  call assert_equal('silent!', g:mods)
82  sil! MyCmd
83  call assert_equal('silent!', g:mods)
84  tab MyCmd
85  call assert_equal('tab', g:mods)
86  0tab MyCmd
87  call assert_equal('0tab', g:mods)
88  tab split
89  tab MyCmd
90  call assert_equal('tab', g:mods)
91  1tab MyCmd
92  call assert_equal('1tab', g:mods)
93  tabprev
94  tab MyCmd
95  call assert_equal('tab', g:mods)
96  2tab MyCmd
97  call assert_equal('2tab', g:mods)
98  2tabclose
99  topleft MyCmd
100  call assert_equal('topleft', g:mods)
101  to MyCmd
102  call assert_equal('topleft', g:mods)
103  unsilent MyCmd
104  call assert_equal('unsilent', g:mods)
105  uns MyCmd
106  call assert_equal('unsilent', g:mods)
107  verbose MyCmd
108  call assert_equal('verbose', g:mods)
109  verb MyCmd
110  call assert_equal('verbose', g:mods)
111  0verbose MyCmd
112  call assert_equal('0verbose', g:mods)
113  3verbose MyCmd
114  call assert_equal('3verbose', g:mods)
115  999verbose MyCmd
116  call assert_equal('999verbose', g:mods)
117  vertical MyCmd
118  call assert_equal('vertical', g:mods)
119  vert MyCmd
120  call assert_equal('vertical', g:mods)
121  horizontal MyCmd
122  call assert_equal('horizontal', g:mods)
123  hor MyCmd
124  call assert_equal('horizontal', g:mods)
125
126  aboveleft belowright botright browse confirm hide keepalt keepjumps
127	      \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent
128	      \ tab topleft unsilent verbose vertical MyCmd
129
130  call assert_equal('browse confirm hide keepalt keepjumps ' .
131      \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' .
132      \ 'silent verbose aboveleft belowright botright tab topleft vertical',
133      \ g:mods)
134
135  let g:mods = ''
136  command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
137
138  vertical MyQCmd
139  call assert_equal('"vertical" ', g:mods)
140
141  delcommand MyCmd
142  delcommand MyQCmd
143  unlet g:mods
144endfunction
145
146func SaveCmdArgs(...)
147  let g:args = a:000
148endfunc
149
150func Test_f_args()
151  command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
152
153  TestFArgs
154  call assert_equal([], g:args)
155
156  TestFArgs one two three
157  call assert_equal(['one', 'two', 'three'], g:args)
158
159  TestFArgs one\\two three
160  call assert_equal(['one\two', 'three'], g:args)
161
162  TestFArgs one\ two three
163  call assert_equal(['one two', 'three'], g:args)
164
165  TestFArgs one\"two three
166  call assert_equal(['one\"two', 'three'], g:args)
167
168  delcommand TestFArgs
169endfunc
170
171func Test_q_args()
172  command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
173
174  TestQArgs
175  call assert_equal([''], g:args)
176
177  TestQArgs one two three
178  call assert_equal(['one two three'], g:args)
179
180  TestQArgs one\\two three
181  call assert_equal(['one\\two three'], g:args)
182
183  TestQArgs one\ two three
184  call assert_equal(['one\ two three'], g:args)
185
186  TestQArgs one\"two three
187  call assert_equal(['one\"two three'], g:args)
188
189  delcommand TestQArgs
190endfunc
191
192func Test_reg_arg()
193  command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
194
195  TestRegArg
196  call assert_equal(['', ''], g:args)
197
198  TestRegArg x
199  call assert_equal(['x', 'x'], g:args)
200
201  delcommand TestRegArg
202endfunc
203
204func Test_no_arg()
205  command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
206
207  TestNoArg
208  call assert_equal(['', '<>', '<x>', '<'], g:args)
209
210  TestNoArg one
211  call assert_equal(['one', '<>', '<x>', '<'], g:args)
212
213  delcommand TestNoArg
214endfunc
215
216func Test_range_arg()
217  command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
218  new
219  call setline(1, range(100))
220  let lnum = line('.')
221
222  TestRangeArg
223  call assert_equal([0, lnum, lnum], g:args)
224
225  99TestRangeArg
226  call assert_equal([1, 99, 99], g:args)
227
228  88,99TestRangeArg
229  call assert_equal([2, 88, 99], g:args)
230
231  call assert_fails('102TestRangeArg', 'E16:')
232
233  bwipe!
234  delcommand TestRangeArg
235endfunc
236
237func Test_Ambiguous()
238  command Doit let g:didit = 'yes'
239  command Dothat let g:didthat = 'also'
240  call assert_fails('Do', 'E464:')
241  Doit
242  call assert_equal('yes', g:didit)
243  Dothat
244  call assert_equal('also', g:didthat)
245  unlet g:didit
246  unlet g:didthat
247
248  delcommand Doit
249  Do
250  call assert_equal('also', g:didthat)
251  delcommand Dothat
252
253  " Nvim removed the ":Ni!" easter egg in 87e107d92.
254  call assert_fails("\x4ei\041", 'E492: Not an editor command: Ni!')
255endfunc
256
257func Test_redefine_on_reload()
258  call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
259  call assert_equal(0, exists(':ExistingCommand'))
260  source Xcommandexists
261  call assert_equal(2, exists(':ExistingCommand'))
262  " Redefining a command when reloading a script is OK.
263  source Xcommandexists
264  call assert_equal(2, exists(':ExistingCommand'))
265
266  " But redefining in another script is not OK.
267  call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
268  call assert_fails('source Xcommandexists2', 'E174:')
269  call delete('Xcommandexists2')
270
271  " And defining twice in one script is not OK.
272  delcommand ExistingCommand
273  call assert_equal(0, exists(':ExistingCommand'))
274  call writefile([
275	\ 'command ExistingCommand echo "yes"',
276	\ 'command ExistingCommand echo "no"',
277	\ ], 'Xcommandexists')
278  call assert_fails('source Xcommandexists', 'E174:')
279  call assert_equal(2, exists(':ExistingCommand'))
280
281  call delete('Xcommandexists')
282  delcommand ExistingCommand
283endfunc
284
285func Test_CmdUndefined()
286  call assert_fails('Doit', 'E492:')
287  au CmdUndefined Doit :command Doit let g:didit = 'yes'
288  Doit
289  call assert_equal('yes', g:didit)
290  delcommand Doit
291
292  call assert_fails('Dothat', 'E492:')
293  au CmdUndefined * let g:didnot = 'yes'
294  call assert_fails('Dothat', 'E492:')
295  call assert_equal('yes', g:didnot)
296endfunc
297
298func Test_CmdErrors()
299  call assert_fails('com! docmd :', 'E183:')
300  call assert_fails('com! \<Tab> :', 'E182:')
301  call assert_fails('com! _ :', 'E182:')
302  call assert_fails('com! - DoCmd :', 'E175:')
303  call assert_fails('com! -xxx DoCmd :', 'E181:')
304  call assert_fails('com! -addr DoCmd :', 'E179:')
305  call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
306  call assert_fails('com! -complete DoCmd :', 'E179:')
307  call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
308  call assert_fails('com! -complete=custom DoCmd :', 'E467:')
309  call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
310  " call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
311  call assert_fails('com! -nargs=x DoCmd :', 'E176:')
312  call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
313  call assert_fails('com! -count=x DoCmd :', 'E178:')
314  call assert_fails('com! -range=x DoCmd :', 'E178:')
315
316  call assert_fails('com! -complete=file DoCmd :', 'E1208:')
317  call assert_fails('com! -nargs=0 -complete=file DoCmd :', 'E1208:')
318
319  let lines =<< trim END
320      vim9script
321      com! -complete=file DoCmd :
322  END
323  call CheckScriptFailure(lines, 'E1208:', 2)
324
325  let lines =<< trim END
326      vim9script
327      com! -nargs=0 -complete=file DoCmd :
328  END
329  call CheckScriptFailure(lines, 'E1208:', 2)
330
331  com! -nargs=0 DoCmd :
332  call assert_fails('DoCmd x', 'E488:')
333
334  com! -nargs=1 DoCmd :
335  call assert_fails('DoCmd', 'E471:')
336
337  com! -nargs=_ DoCmd :
338  call assert_fails('DoCmd', 'E471:')
339
340  com! -nargs=+ DoCmd :
341  call assert_fails('DoCmd', 'E471:')
342
343  call assert_fails('com DoCmd :', 'E174:')
344  comclear
345  call assert_fails('delcom DoCmd', 'E184:')
346
347  " These used to leak memory
348  call assert_fails('com! -complete=custom,CustomComplete _ :', 'E182:')
349  call assert_fails('com! -complete=custom,CustomComplete docmd :', 'E183:')
350  call assert_fails('com! -complete=custom,CustomComplete -xxx DoCmd :', 'E181:')
351endfunc
352
353func CustomComplete(A, L, P)
354  return "January\nFebruary\nMars\n"
355endfunc
356
357func CustomCompleteList(A, L, P)
358  return [ "Monday", "Tuesday", "Wednesday", {}, v:_null_string]
359endfunc
360
361func CustomCompleteListWithSpaces(A, L, P)
362  return [ "Monday Here", "Tuesday There", "Wednesday OK", {}, v:_null_string]
363endfunc
364
365func CustomCompleteListFuzzy(A, L, P)
366  return [ "Monday Here", "Tuesday There", "Wednesday OK", {}, v:_null_string]->matchfuzzy(a:A)
367endfunc
368
369func Test_CmdCompletion()
370  call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
371  call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
372
373  call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
374  call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
375
376  call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
377  call assert_equal('"com -nargs=* + 0 1 ? _', @:)
378
379  call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
380  call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
381
382  call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
383  call assert_equal('"com -complete=color command compiler', @:)
384
385  " try completion for unsupported argument values
386  call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
387  call assert_equal("\"com -newarg=\t", @:)
388
389  " command completion after the name in a user defined command
390  call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
391  call assert_equal("\"com MyCmd chistory", @:)
392
393  " delete the Check commands to avoid them showing up
394  call feedkeys(":com Check\<C-A>\<C-B>\"\<CR>", 'tx')
395  let cmds = substitute(@:, '"com ', '', '')->split()
396  for cmd in cmds
397    exe 'delcommand ' .. cmd
398  endfor
399  delcommand MissingFeature
400
401  command! DoCmd1 :
402  command! DoCmd2 :
403  call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
404  call assert_equal('"com DoCmd1 DoCmd2', @:)
405
406  call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
407  call assert_equal('"DoCmd1 DoCmd2', @:)
408
409  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
410  call assert_equal('"delcom DoCmd1 DoCmd2', @:)
411
412  " try argument completion for a command without completion
413  call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
414  call assert_equal("\"DoCmd1 \t", @:)
415
416  delcom DoCmd1
417  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
418  call assert_equal('"delcom DoCmd2', @:)
419
420  call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
421  call assert_equal('"com DoCmd2', @:)
422
423  delcom DoCmd2
424  call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
425  call assert_equal('"delcom DoC', @:)
426
427  call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
428  call assert_equal('"com DoC', @:)
429
430  " com! -nargs=1 -complete=behave DoCmd :
431  " call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
432  " call assert_equal('"DoCmd mswin xterm', @:)
433
434  " com! -nargs=_ -complete=behave DoCmd :
435  " call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
436  " call assert_equal('"DoCmd mswin xterm', @:)
437
438  com! -nargs=1 -complete=retab DoCmd :
439  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
440  call assert_equal('"DoCmd -indentonly', @:)
441
442  com! -nargs=_ -complete=retab DoCmd :
443  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
444  call assert_equal('"DoCmd -indentonly', @:)
445
446  " Test for file name completion
447  com! -nargs=1 -complete=file DoCmd :
448  call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
449  call assert_equal('"DoCmd README.txt', @:)
450
451  com! -nargs=_ -complete=file DoCmd :
452  call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
453  call assert_equal('"DoCmd README.txt', @:)
454
455  " Test for buffer name completion
456  com! -nargs=1 -complete=buffer DoCmd :
457  let bnum = bufadd('BufForUserCmd')
458  call setbufvar(bnum, '&buflisted', 1)
459  call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
460  call assert_equal('"DoCmd BufForUserCmd', @:)
461  bwipe BufForUserCmd
462  call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
463  call assert_equal('"DoCmd BufFor', @:)
464
465  com! -nargs=_ -complete=buffer DoCmd :
466  let bnum = bufadd('BufForUserCmd')
467  call setbufvar(bnum, '&buflisted', 1)
468  call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
469  call assert_equal('"DoCmd BufForUserCmd', @:)
470  bwipe BufForUserCmd
471  call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
472  call assert_equal('"DoCmd BufFor', @:)
473
474  com! -nargs=* -complete=custom,CustomComplete DoCmd :
475  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
476  call assert_equal('"DoCmd January February Mars', @:)
477
478  com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
479  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
480  call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
481
482  com! -nargs=_ -complete=customlist,CustomCompleteListWithSpaces DoCmd :
483  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
484  call assert_equal('"DoCmd Monday Here Tuesday There Wednesday OK', @:)
485
486  com! -nargs=_ -complete=customlist,CustomCompleteListFuzzy DoCmd :
487  call feedkeys(":DoCmd mo he\<C-A>\<C-B>\"\<CR>", 'tx')
488  call assert_equal('"DoCmd Monday Here', @:)
489
490  com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
491  call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
492
493  com! -nargs=+ -complete=customlist,CustomComp DoCmd :
494  call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
495
496  " custom completion without a function
497  com! -nargs=? -complete=custom, DoCmd
498  call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
499
500  " custom completion failure with the wrong function
501  com! -nargs=? -complete=custom,min DoCmd
502  call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
503
504  " custom completion for a pattern with a backslash
505  let g:ArgLead = ''
506  func! CustCompl(A, L, P)
507    let g:ArgLead = a:A
508    return ['one', 'two', 'three']
509  endfunc
510  com! -nargs=? -complete=customlist,CustCompl DoCmd
511  call feedkeys(":DoCmd a\\\t", 'xt')
512  call assert_equal('a\', g:ArgLead)
513  delfunc CustCompl
514
515  delcom DoCmd
516endfunc
517
518func CallExecute(A, L, P)
519  " Drop first '\n'
520  return execute('echo "hi"')[1:]
521endfunc
522
523func Test_use_execute_in_completion()
524  command! -nargs=* -complete=custom,CallExecute DoExec :
525  call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
526  call assert_equal('"DoExec hi', @:)
527  delcommand DoExec
528endfunc
529
530func Test_addr_all()
531  command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
532  %DoSomething
533  call assert_equal(1, g:a1)
534  call assert_equal(line('$'), g:a2)
535
536  command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
537  args one two three
538  %DoSomething
539  call assert_equal(1, g:a1)
540  call assert_equal(3, g:a2)
541
542  command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
543  %DoSomething
544  for low in range(1, bufnr('$'))
545    if buflisted(low)
546      break
547    endif
548  endfor
549  call assert_equal(low, g:a1)
550  call assert_equal(bufnr('$'), g:a2)
551
552  command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
553  %DoSomething
554  for low in range(1, bufnr('$'))
555    if bufloaded(low)
556      break
557    endif
558  endfor
559  call assert_equal(low, g:a1)
560  for up in range(bufnr('$'), 1, -1)
561    if bufloaded(up)
562      break
563    endif
564  endfor
565  call assert_equal(up, g:a2)
566
567  command! -addr=windows DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
568  new
569  %DoSomething
570  call assert_equal(1, g:a1)
571  call assert_equal(winnr('$'), g:a2)
572  bwipe
573
574  command! -addr=tabs DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
575  tabnew
576  %DoSomething
577  call assert_equal(1, g:a1)
578  call assert_equal(len(gettabinfo()), g:a2)
579  bwipe
580
581  command! -addr=other DoSomething  let g:a1 = <line1> | let g:a2 = <line2>
582  DoSomething
583  call assert_equal(line('.'), g:a1)
584  call assert_equal(line('.'), g:a2)
585  %DoSomething
586  call assert_equal(1, g:a1)
587  call assert_equal(line('$'), g:a2)
588
589  delcommand DoSomething
590endfunc
591
592func Test_nargs_underscore_fargs()
593  " -nargs=_ must behave like -nargs=1 for <f-args>/<q-args>:
594  " the whole argument is one token, whitespace is part of it.
595  let g:res = []
596  com! -nargs=1 DoCmd1 call add(g:res, [<f-args>])
597  com! -nargs=_ DoCmdU call add(g:res, [<f-args>])
598  DoCmd1 a b c
599  DoCmdU a b c
600  call assert_equal([['a b c'], ['a b c']], g:res)
601
602  let g:res = []
603  com! -nargs=_ DoCmdQ call add(g:res, <q-args>)
604  DoCmdQ a b c
605  call assert_equal(['a b c'], g:res)
606
607  delcom DoCmd1
608  delcom DoCmdU
609  delcom DoCmdQ
610  unlet g:res
611endfunc
612
613func Test_command_list()
614  command! DoCmd :
615  call assert_equal("\n    Name              Args Address Complete    Definition"
616        \        .. "\n    DoCmd             0                        :",
617        \           execute('command DoCmd'))
618
619  " Test with various -range= and -count= argument values.
620  command! -range DoCmd :
621  call assert_equal("\n    Name              Args Address Complete    Definition"
622        \        .. "\n    DoCmd             0    .                   :",
623        \           execute('command DoCmd'))
624  command! -range=% DoCmd :
625  call assert_equal("\n    Name              Args Address Complete    Definition"
626        \        .. "\n    DoCmd             0    %                   :",
627        \           execute('command! DoCmd'))
628  command! -range=2 DoCmd :
629  call assert_equal("\n    Name              Args Address Complete    Definition"
630        \        .. "\n    DoCmd             0    2                   :",
631        \           execute('command DoCmd'))
632  command! -count=2 DoCmd :
633  call assert_equal("\n    Name              Args Address Complete    Definition"
634        \        .. "\n    DoCmd             0    2c ?                :",
635        \           execute('command DoCmd'))
636
637  " Test with various -addr= argument values.
638  command! -addr=lines DoCmd :
639  call assert_equal("\n    Name              Args Address Complete    Definition"
640        \        .. "\n    DoCmd             0    .                   :",
641        \           execute('command DoCmd'))
642  command! -addr=arguments DoCmd :
643  call assert_equal("\n    Name              Args Address Complete    Definition"
644        \        .. "\n    DoCmd             0    .  arg              :",
645        \           execute('command DoCmd'))
646  command! -addr=buffers DoCmd :
647  call assert_equal("\n    Name              Args Address Complete    Definition"
648        \        .. "\n    DoCmd             0    .  buf              :",
649        \           execute('command DoCmd'))
650  command! -addr=loaded_buffers DoCmd :
651  call assert_equal("\n    Name              Args Address Complete    Definition"
652        \        .. "\n    DoCmd             0    .  load             :",
653        \           execute('command DoCmd'))
654  command! -addr=windows DoCmd :
655  call assert_equal("\n    Name              Args Address Complete    Definition"
656        \        .. "\n    DoCmd             0    .  win              :",
657        \           execute('command DoCmd'))
658  command! -addr=tabs DoCmd :
659  call assert_equal("\n    Name              Args Address Complete    Definition"
660        \        .. "\n    DoCmd             0    .  tab              :",
661        \           execute('command DoCmd'))
662  command! -addr=other DoCmd :
663  call assert_equal("\n    Name              Args Address Complete    Definition"
664        \        .. "\n    DoCmd             0    .  ?                :",
665        \           execute('command DoCmd'))
666
667  " Test with various -complete= argument values (non-exhaustive list)
668  command! -nargs=1 -complete=arglist DoCmd :
669  call assert_equal("\n    Name              Args Address Complete    Definition"
670        \        .. "\n    DoCmd             1            arglist     :",
671        \           execute('command DoCmd'))
672  command! -nargs=_ -complete=arglist DoCmd :
673  call assert_equal("\n    Name              Args Address Complete    Definition"
674        \        .. "\n    DoCmd             _            arglist     :",
675        \           execute('command DoCmd'))
676  command! -nargs=* -complete=augroup DoCmd :
677  call assert_equal("\n    Name              Args Address Complete    Definition"
678        \        .. "\n    DoCmd             *            augroup     :",
679        \           execute('command DoCmd'))
680  command! -nargs=? -complete=custom,CustomComplete DoCmd :
681  call assert_equal("\n    Name              Args Address Complete    Definition"
682        \        .. "\n    DoCmd             ?            custom      :",
683        \           execute('command DoCmd'))
684  command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
685  call assert_equal("\n    Name              Args Address Complete    Definition"
686        \        .. "\n    DoCmd             +            customlist  :",
687        \           execute('command DoCmd'))
688
689  " Test with various -narg= argument values.
690  command! -nargs=0 DoCmd :
691  call assert_equal("\n    Name              Args Address Complete    Definition"
692        \        .. "\n    DoCmd             0                        :",
693        \           execute('command DoCmd'))
694  command! -nargs=1 DoCmd :
695  call assert_equal("\n    Name              Args Address Complete    Definition"
696        \        .. "\n    DoCmd             1                        :",
697        \           execute('command DoCmd'))
698  command! -nargs=_ DoCmd :
699  call assert_equal("\n    Name              Args Address Complete    Definition"
700        \        .. "\n    DoCmd             _                        :",
701        \           execute('command DoCmd'))
702  command! -nargs=* DoCmd :
703  call assert_equal("\n    Name              Args Address Complete    Definition"
704        \        .. "\n    DoCmd             *                        :",
705        \           execute('command DoCmd'))
706  command! -nargs=? DoCmd :
707  call assert_equal("\n    Name              Args Address Complete    Definition"
708        \        .. "\n    DoCmd             ?                        :",
709        \           execute('command DoCmd'))
710  command! -nargs=+ DoCmd :
711  call assert_equal("\n    Name              Args Address Complete    Definition"
712        \        .. "\n    DoCmd             +                        :",
713        \           execute('command DoCmd'))
714
715  " Test with other arguments.
716  command! -bang DoCmd :
717  call assert_equal("\n    Name              Args Address Complete    Definition"
718        \        .. "\n!   DoCmd             0                        :",
719        \           execute('command DoCmd'))
720  command! -bar DoCmd :
721  call assert_equal("\n    Name              Args Address Complete    Definition"
722        \        .. "\n|   DoCmd             0                        :",
723        \           execute('command DoCmd'))
724  command! -register DoCmd :
725  call assert_equal("\n    Name              Args Address Complete    Definition"
726        \        .. "\n\"   DoCmd             0                        :",
727        \           execute('command DoCmd'))
728  command! -buffer DoCmd :
729  call assert_equal("\n    Name              Args Address Complete    Definition"
730        \        .. "\nb   DoCmd             0                        :"
731        \        .. "\n\"   DoCmd             0                        :",
732        \           execute('command DoCmd'))
733  comclear
734
735  " Test with many args.
736  command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
737  call assert_equal("\n    Name              Args Address Complete    Definition"
738        \        .. "\n!\"b|DoCmd             +    3c win  environment :",
739        \           execute('command DoCmd'))
740  comclear
741
742  " Test with special characters in command definition.
743  command! DoCmd :<cr><tab><c-d>
744  call assert_equal("\n    Name              Args Address Complete    Definition"
745        \        .. "\n    DoCmd             0                        :<CR><Tab><C-D>",
746        \           execute('command DoCmd'))
747
748  " Test output in verbose mode.
749  command! DoCmd :
750  call assert_match("^\n"
751        \        .. "    Name              Args Address Complete    Definition\n"
752        \        .. "    DoCmd             0                        :\n"
753        \        .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
754        \           execute('verbose command DoCmd'))
755
756  comclear
757  call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
758  call assert_equal("\nNo user-defined commands found", execute('command'))
759endfunc
760
761" Test for a custom user completion returning the wrong value type
762func Test_usercmd_custom()
763  func T1(a, c, p)
764    return "a\nb\n"
765  endfunc
766  command -nargs=* -complete=customlist,T1 TCmd1
767  call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
768  call assert_equal('"TCmd1 ', @:)
769  delcommand TCmd1
770  delfunc T1
771
772  func T2(a, c, p)
773    return {}
774  endfunc
775  command -nargs=* -complete=customlist,T2 TCmd2
776  call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
777  call assert_equal('"TCmd2 ', @:)
778  delcommand TCmd2
779  delfunc T2
780endfunc
781
782func Test_delcommand_buffer()
783  command Global echo 'global'
784  command -buffer OneBuffer echo 'one'
785  new
786  command -buffer TwoBuffer echo 'two'
787  call assert_equal(0, exists(':OneBuffer'))
788  call assert_equal(2, exists(':Global'))
789  call assert_equal(2, exists(':TwoBuffer'))
790  delcommand -buffer TwoBuffer
791  call assert_equal(0, exists(':TwoBuffer'))
792  call assert_fails('delcommand -buffer Global', 'E1237:')
793  call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
794  bwipe!
795  call assert_equal(2, exists(':OneBuffer'))
796  delcommand -buffer OneBuffer
797  call assert_equal(0, exists(':OneBuffer'))
798  call assert_fails('delcommand -buffer Global', 'E1237:')
799  delcommand Global
800  call assert_equal(0, exists(':Global'))
801endfunc
802
803func DefCmd(name)
804  if len(a:name) > 30
805    return
806  endif
807  exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
808  echo a:name
809  exe a:name
810endfunc
811
812func Test_recursive_define()
813  call DefCmd('Command')
814
815  let name = 'Command'
816  while len(name) <= 30
817    exe 'delcommand ' .. name
818    let name ..= 'x'
819  endwhile
820endfunc
821
822" Test for using buffer-local ambiguous user-defined commands
823func Test_buflocal_ambiguous_usercmd()
824  new
825  command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
826  command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
827
828  call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
829  call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
830  call assert_equal('"TestCmd ', @:)
831
832  delcommand TestCmd1
833  delcommand TestCmd2
834  bw!
835endfunc
836
837" Test for using buffer-local user command from cmdwin.
838func Test_buflocal_usercmd_cmdwin()
839  throw 'Skipped: Nvim supports cmdwin freedom #40312'
840  new
841  command -buffer TestCmd edit Test
842  " This used to crash Vim
843  call assert_fails("norm q::TestCmd\<CR>", 'E11:')
844  bw!
845endfunc
846
847" Test for using a multibyte character in a user command
848func Test_multibyte_in_usercmd()
849  command SubJapanesePeriodToDot exe "%s/\u3002/./g"
850  new
851  call setline(1, "Hello\u3002")
852  SubJapanesePeriodToDot
853  call assert_equal('Hello.', getline(1))
854  bw!
855  delcommand SubJapanesePeriodToDot
856endfunc
857
858" Test for listing user commands.
859func Test_command_list_0()
860  " Check space padding of attribute and name in command list
861  set vbs&
862  command! ShortCommand echo "ShortCommand"
863  command! VeryMuchLongerCommand echo "VeryMuchLongerCommand"
864
865  redi @"> | com | redi END
866  pu
867
868  let bl = matchbufline(bufnr('%'), "^    ShortCommand      0", 1, '$')
869  call assert_false(bl == [])
870  let bl = matchbufline(bufnr('%'), "^    VeryMuchLongerCommand 0", 1, '$')
871  call assert_false(bl == [])
872
873  bwipe!
874  delcommand ShortCommand
875  delcommand VeryMuchLongerCommand
876endfunc
877
878" Test for a block of ":command" or ":autocmd" in a block that is not
879" executed: the lines up to the "}" are skipped, not parsed as commands.
880func Test_command_block_skipped()
881  throw 'Skipped: Vim9 script is N/A'
882  let lines =<< trim END
883    vim9script
884    def Something(s: string)
885      echo s
886    enddef
887    if 0
888      command! -bar -nargs=? Xfoo {
889        Something(<q-args>)
890      }
891    endif
892    if 0
893      autocmd BufRead *.xyz {
894        Something(<q-args>)
895      }
896    endif
897    while 0
898      command! Xbar {
899        Something('x')
900      }
901    endwhile
902    g:skipped = 'all three'
903  END
904  call writefile(lines, 'Xcmdblock.vim', 'D')
905
906  let g:skipped = ''
907  source Xcmdblock.vim
908  call assert_equal('all three', g:skipped)
909  call assert_equal(0, exists(':Xfoo'))
910  call assert_equal(0, exists(':Xbar'))
911  call assert_notmatch('xyz', execute('autocmd BufRead'))
912
913  unlet g:skipped
914endfunc
915
916" vim: shiftwidth=2 sts=2 expandtab