blob: 3b87d367d8e8c5481c0396a71cf605c066c552c7 [file] [log] [blame]
Bram Moolenaar63a60de2016-06-04 22:08:55 +02001" 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
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020011 abo MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020012 belowright MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020013 bel MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020014 botright MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020015 bo MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020016 browse MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020017 bro MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020018 confirm MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020019 conf MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020020 hide MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020021 hid MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020022 keepalt MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020023 keepa MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020024 keepjumps MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020025 keepj MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020026 keepmarks MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020027 kee MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020028 keeppatterns MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020029 keepp MyCmd
30 leftabove MyCmd " results in :aboveleft
31 lefta MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020032 lockmarks MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020033 loc MyCmd
34 " noautocmd MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020035 noswapfile MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020036 nos MyCmd
37 rightbelow MyCmd " results in :belowright
38 rightb MyCmd
39 " sandbox MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020040 silent MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020041 sil MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020042 tab MyCmd
43 topleft MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020044 to MyCmd
45 " unsilent MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020046 verbose MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020047 verb MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020048 vertical MyCmd
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020049 vert MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020050
51 aboveleft belowright botright browse confirm hide keepalt keepjumps
52 \ keepmarks keeppatterns lockmarks noswapfile silent tab
53 \ topleft verbose vertical MyCmd
54
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020055 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 ' .
Bram Moolenaar63a60de2016-06-04 22:08:55 +020062 \ '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
Bram Moolenaareac784e2016-07-28 22:08:24 +020075
76func Test_Ambiguous()
77 command Doit let g:didit = 'yes'
78 command Dothat let g:didthat = 'also'
79 call assert_fails('Do', 'E464:')
80 Doit
81 call assert_equal('yes', g:didit)
82 Dothat
83 call assert_equal('also', g:didthat)
84 unlet g:didit
85 unlet g:didthat
86
87 delcommand Doit
88 Do
89 call assert_equal('also', g:didthat)
90 delcommand Dothat
91endfunc
92
Bram Moolenaar55d46912018-12-08 16:03:28 +010093func Test_redefine_on_reload()
94 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
95 call assert_equal(0, exists(':ExistingCommand'))
96 source Xcommandexists
97 call assert_equal(2, exists(':ExistingCommand'))
98 " Redefining a command when reloading a script is OK.
99 source Xcommandexists
100 call assert_equal(2, exists(':ExistingCommand'))
101
102 " But redefining in another script is not OK.
103 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
104 call assert_fails('source Xcommandexists2', 'E174:')
105 call delete('Xcommandexists2')
106
107 " And defining twice in one script is not OK.
108 delcommand ExistingCommand
109 call assert_equal(0, exists(':ExistingCommand'))
110 call writefile([
111 \ 'command ExistingCommand echo "yes"',
112 \ 'command ExistingCommand echo "no"',
113 \ ], 'Xcommandexists')
114 call assert_fails('source Xcommandexists', 'E174:')
115 call assert_equal(2, exists(':ExistingCommand'))
116
117 call delete('Xcommandexists')
118 delcommand ExistingCommand
119endfunc
120
Bram Moolenaareac784e2016-07-28 22:08:24 +0200121func Test_CmdUndefined()
122 call assert_fails('Doit', 'E492:')
123 au CmdUndefined Doit :command Doit let g:didit = 'yes'
124 Doit
125 call assert_equal('yes', g:didit)
126 delcommand Doit
127
128 call assert_fails('Dothat', 'E492:')
129 au CmdUndefined * let g:didnot = 'yes'
130 call assert_fails('Dothat', 'E492:')
131 call assert_equal('yes', g:didnot)
132endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100133
134func Test_CmdErrors()
135 call assert_fails('com! docmd :', 'E183:')
136 call assert_fails('com! \<Tab> :', 'E182:')
137 call assert_fails('com! _ :', 'E182:')
138 call assert_fails('com! X :', 'E841:')
139 call assert_fails('com! - DoCmd :', 'E175:')
140 call assert_fails('com! -xxx DoCmd :', 'E181:')
141 call assert_fails('com! -addr DoCmd :', 'E179:')
142 call assert_fails('com! -complete DoCmd :', 'E179:')
143 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
144 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
145 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
146 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
147 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
148 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
149 call assert_fails('com! -count=x DoCmd :', 'E178:')
150 call assert_fails('com! -range=x DoCmd :', 'E178:')
151
152 com! -nargs=0 DoCmd :
153 call assert_fails('DoCmd x', 'E488:')
154
155 com! -nargs=1 DoCmd :
156 call assert_fails('DoCmd', 'E471:')
157
158 com! -nargs=+ DoCmd :
159 call assert_fails('DoCmd', 'E471:')
160
161 call assert_fails('com DoCmd :', 'E174:')
162 comclear
163 call assert_fails('delcom DoCmd', 'E184:')
164endfunc
165
166func CustomComplete(A, L, P)
167 return "January\nFebruary\nMars\n"
168endfunc
169
170func CustomCompleteList(A, L, P)
171 return [ "Monday", "Tuesday", "Wednesday" ]
172endfunc
173
174func Test_CmdCompletion()
175 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
176 call assert_equal('"com -addr bang bar buffer complete count nargs range register', @:)
177
178 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
179 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count nargs range register', @:)
180
181 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
182 call assert_equal('"com -nargs=* + 0 1 ?', @:)
183
184 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100185 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100186
187 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
188 call assert_equal('"com -complete=color command compiler', @:)
189
190 command! DoCmd1 :
191 command! DoCmd2 :
192 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
193 call assert_equal('"com DoCmd1 DoCmd2', @:)
194
195 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
196 call assert_equal('"DoCmd1 DoCmd2', @:)
197
198 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
199 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
200
201 delcom DoCmd1
202 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
203 call assert_equal('"delcom DoCmd2', @:)
204
205 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
206 call assert_equal('"com DoCmd2', @:)
207
208 delcom DoCmd2
209 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
210 call assert_equal('"delcom DoC', @:)
211
212 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
213 call assert_equal('"com DoC', @:)
214
215 com! -complete=behave DoCmd :
216 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
217 call assert_equal('"DoCmd mswin xterm', @:)
218
219 " This does not work. Why?
220 "call feedkeys(":DoCmd x\<C-A>\<C-B>\"\<CR>", 'tx')
221 "call assert_equal('"DoCmd xterm', @:)
222
223 com! -complete=custom,CustomComplete DoCmd :
224 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
225 call assert_equal('"DoCmd January February Mars', @:)
226
227 com! -complete=customlist,CustomCompleteList DoCmd :
228 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
229 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
230
231 com! -complete=custom,CustomCompleteList DoCmd :
232 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
233
234 com! -complete=customlist,CustomComp DoCmd :
235 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
236endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100237
238func CallExecute(A, L, P)
239 " Drop first '\n'
240 return execute('echo "hi"')[1:]
241endfunc
242
243func Test_use_execute_in_completion()
244 command! -nargs=* -complete=custom,CallExecute DoExec :
245 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
246 call assert_equal('"DoExec hi', @:)
247 delcommand DoExec
248endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100249
250func Test_addr_all()
251 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
252 %DoSomething
253 call assert_equal(1, g:a1)
254 call assert_equal(line('$'), g:a2)
255
256 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
257 args one two three
258 %DoSomething
259 call assert_equal(1, g:a1)
260 call assert_equal(3, g:a2)
261
262 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
263 %DoSomething
264 for low in range(1, bufnr('$'))
265 if buflisted(low)
266 break
267 endif
268 endfor
269 call assert_equal(low, g:a1)
270 call assert_equal(bufnr('$'), g:a2)
271
272 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
273 %DoSomething
274 for low in range(1, bufnr('$'))
275 if bufloaded(low)
276 break
277 endif
278 endfor
279 call assert_equal(low, g:a1)
280 for up in range(bufnr('$'), 1, -1)
281 if bufloaded(up)
282 break
283 endif
284 endfor
285 call assert_equal(up, g:a2)
286
287 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
288 new
289 %DoSomething
290 call assert_equal(1, g:a1)
291 call assert_equal(winnr('$'), g:a2)
292 bwipe
293
294 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
295 tabnew
296 %DoSomething
297 call assert_equal(1, g:a1)
298 call assert_equal(len(gettabinfo()), g:a2)
299 bwipe
300
301 command! -addr=other DoSomething echo 'nothing'
302 DoSomething
303 call assert_fails('%DoSomething')
304
305 delcommand DoSomething
306endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200307
308func Test_command_list()
309 command! DoCmd :
310 call assert_equal("\n Name Args Address Complete Definition"
311 \ .. "\n DoCmd 0 :",
312 \ execute('command DoCmd'))
313
314 " Test with various -range= and -count= argument values.
315 command! -range DoCmd :
316 call assert_equal("\n Name Args Address Complete Definition"
317 \ .. "\n DoCmd 0 . :",
318 \ execute('command DoCmd'))
319 command! -range=% DoCmd :
320 call assert_equal("\n Name Args Address Complete Definition"
321 \ .. "\n DoCmd 0 % :",
322 \ execute('command! DoCmd'))
323 command! -range=2 DoCmd :
324 call assert_equal("\n Name Args Address Complete Definition"
325 \ .. "\n DoCmd 0 2 :",
326 \ execute('command DoCmd'))
327 command! -count=2 DoCmd :
328 call assert_equal("\n Name Args Address Complete Definition"
329 \ .. "\n DoCmd 0 2c :",
330 \ execute('command DoCmd'))
331
332 " Test with various -addr= argument values.
333 command! -addr=lines DoCmd :
334 call assert_equal("\n Name Args Address Complete Definition"
335 \ .. "\n DoCmd 0 . :",
336 \ execute('command DoCmd'))
337 command! -addr=arguments DoCmd :
338 call assert_equal("\n Name Args Address Complete Definition"
339 \ .. "\n DoCmd 0 . arg :",
340 \ execute('command DoCmd'))
341 command! -addr=buffers DoCmd :
342 call assert_equal("\n Name Args Address Complete Definition"
343 \ .. "\n DoCmd 0 . buf :",
344 \ execute('command DoCmd'))
345 command! -addr=loaded_buffers DoCmd :
346 call assert_equal("\n Name Args Address Complete Definition"
347 \ .. "\n DoCmd 0 . load :",
348 \ execute('command DoCmd'))
349 command! -addr=windows DoCmd :
350 call assert_equal("\n Name Args Address Complete Definition"
351 \ .. "\n DoCmd 0 . win :",
352 \ execute('command DoCmd'))
353 command! -addr=tabs DoCmd :
354 call assert_equal("\n Name Args Address Complete Definition"
355 \ .. "\n DoCmd 0 . tab :",
356 \ execute('command DoCmd'))
357 command! -addr=other DoCmd :
358 call assert_equal("\n Name Args Address Complete Definition"
359 \ .. "\n DoCmd 0 . ? :",
360 \ execute('command DoCmd'))
361
362 " Test with various -complete= argument values (non-exhaustive list)
363 command! -complete=arglist DoCmd :
364 call assert_equal("\n Name Args Address Complete Definition"
365 \ .. "\n DoCmd 0 arglist :",
366 \ execute('command DoCmd'))
367 command! -complete=augroup DoCmd :
368 call assert_equal("\n Name Args Address Complete Definition"
369 \ .. "\n DoCmd 0 augroup :",
370 \ execute('command DoCmd'))
371 command! -complete=custom,CustomComplete DoCmd :
372 call assert_equal("\n Name Args Address Complete Definition"
373 \ .. "\n DoCmd 0 custom :",
374 \ execute('command DoCmd'))
375 command! -complete=customlist,CustomComplete DoCmd :
376 call assert_equal("\n Name Args Address Complete Definition"
377 \ .. "\n DoCmd 0 customlist :",
378 \ execute('command DoCmd'))
379
380 " Test with various -narg= argument values.
381 command! -nargs=0 DoCmd :
382 call assert_equal("\n Name Args Address Complete Definition"
383 \ .. "\n DoCmd 0 :",
384 \ execute('command DoCmd'))
385 command! -nargs=1 DoCmd :
386 call assert_equal("\n Name Args Address Complete Definition"
387 \ .. "\n DoCmd 1 :",
388 \ execute('command DoCmd'))
389 command! -nargs=* DoCmd :
390 call assert_equal("\n Name Args Address Complete Definition"
391 \ .. "\n DoCmd * :",
392 \ execute('command DoCmd'))
393 command! -nargs=? DoCmd :
394 call assert_equal("\n Name Args Address Complete Definition"
395 \ .. "\n DoCmd ? :",
396 \ execute('command DoCmd'))
397 command! -nargs=+ DoCmd :
398 call assert_equal("\n Name Args Address Complete Definition"
399 \ .. "\n DoCmd + :",
400 \ execute('command DoCmd'))
401
402 " Test with other arguments.
403 command! -bang DoCmd :
404 call assert_equal("\n Name Args Address Complete Definition"
405 \ .. "\n! DoCmd 0 :",
406 \ execute('command DoCmd'))
407 command! -bar DoCmd :
408 call assert_equal("\n Name Args Address Complete Definition"
409 \ .. "\n| DoCmd 0 :",
410 \ execute('command DoCmd'))
411 command! -register DoCmd :
412 call assert_equal("\n Name Args Address Complete Definition"
413 \ .. "\n\" DoCmd 0 :",
414 \ execute('command DoCmd'))
415 command! -buffer DoCmd :
416 call assert_equal("\n Name Args Address Complete Definition"
417 \ .. "\nb DoCmd 0 :"
418 \ .. "\n\" DoCmd 0 :",
419 \ execute('command DoCmd'))
420 comclear
421
422 " Test with many args.
423 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
424 call assert_equal("\n Name Args Address Complete Definition"
425 \ .. "\n!\"b|DoCmd + 3c win environment :",
426 \ execute('command DoCmd'))
427 comclear
428
429 " Test with special characters in command definition.
430 command! DoCmd :<cr><tab><c-d>
431 call assert_equal("\n Name Args Address Complete Definition"
432 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
433 \ execute('command DoCmd'))
434
435 " Test output in verbose mode.
436 command! DoCmd :
437 call assert_match("^\n"
438 \ .. " Name Args Address Complete Definition\n"
439 \ .. " DoCmd 0 :\n"
440 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
441 \ execute('verbose command DoCmd'))
442
443 comclear
444 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
445 call assert_equal("\nNo user-defined commands found", execute('command'))
446endfunc