blob: 7f87e27aeed2f839f65bb0d28130bd16e35c6a4b [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
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02007 command! -nargs=* MyCmd let g:mods = '<mods>'
Bram Moolenaar63a60de2016-06-04 22:08:55 +02008
9 MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020010 call assert_equal('', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020011 aboveleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020012 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020013 abo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020014 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020015 belowright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020016 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020017 bel MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020018 call assert_equal('belowright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020019 botright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020020 call assert_equal('botright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020021 bo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020022 call assert_equal('botright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020023 browse MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020024 call assert_equal('browse', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020025 bro MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020026 call assert_equal('browse', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020027 confirm MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020028 call assert_equal('confirm', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020029 conf MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020030 call assert_equal('confirm', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020031 hide MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020032 call assert_equal('hide', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020033 hid MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020034 call assert_equal('hide', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020035 keepalt MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020036 call assert_equal('keepalt', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020037 keepa MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020038 call assert_equal('keepalt', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020039 keepjumps MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020040 call assert_equal('keepjumps', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020041 keepj MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020042 call assert_equal('keepjumps', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020043 keepmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020044 call assert_equal('keepmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020045 kee MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020046 call assert_equal('keepmarks', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020047 keeppatterns MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020048 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020049 keepp MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020050 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020051 leftabove MyCmd " results in :aboveleft
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020052 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020053 lefta MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020054 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020055 lockmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020056 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020057 loc MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020058 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020059 " noautocmd MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020060 noswapfile MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020061 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020062 nos MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020063 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020064 rightbelow MyCmd " results in :belowright
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020065 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020066 rightb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020067 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020068 " sandbox MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020069 silent MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020070 call assert_equal('silent', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020071 sil MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020072 call assert_equal('silent', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020073 tab MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020074 call assert_equal('tab', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020075 topleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020076 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020077 to MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020078 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020079 " unsilent MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020080 verbose MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020081 call assert_equal('verbose', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020082 verb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020083 call assert_equal('verbose', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020084 vertical MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020085 call assert_equal('vertical', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020086 vert MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020087 call assert_equal('vertical', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020088
89 aboveleft belowright botright browse confirm hide keepalt keepjumps
90 \ keepmarks keeppatterns lockmarks noswapfile silent tab
91 \ topleft verbose vertical MyCmd
92
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020093 call assert_equal('browse confirm hide keepalt keepjumps ' .
94 \ 'keepmarks keeppatterns lockmarks noswapfile silent ' .
95 \ 'verbose aboveleft belowright botright tab topleft vertical', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020096
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
Bram Moolenaareac784e2016-07-28 22:08:24 +0200107
Bram Moolenaare61e5482019-04-27 15:05:12 +0200108func 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
Bram Moolenaareac784e2016-07-28 22:08:24 +0200199func 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
Bram Moolenaare61e5482019-04-27 15:05:12 +0200214
215 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200216endfunc
217
Bram Moolenaar55d46912018-12-08 16:03:28 +0100218func 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
Bram Moolenaareac784e2016-07-28 22:08:24 +0200246func 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
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100258
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:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200267 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100268 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)
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100297 return [ "Monday", "Tuesday", "Wednesday", {}]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100298endfunc
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')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100311 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100312
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:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100362
363 " custom completion without a function
364 com! -complete=custom, DoCmd
365 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
366
367 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100368endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100369
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
Bram Moolenaar51a74542018-12-02 18:21:49 +0100381
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
Bram Moolenaarb7316892019-05-01 18:08:42 +0200433 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100434 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200435 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)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100440
441 delcommand DoSomething
442endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200443
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"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200465 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200466 \ 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
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100583
Bram Moolenaar8b633132020-03-20 18:20:51 +0100584" 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
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200590 call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
591 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100592 delcommand TCmd1
593 delfunc T1
594
595 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200596 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100597 endfunc
598 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200599 call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
600 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100601 delcommand TCmd2
602 delfunc T2
603endfunc
604
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100605" vim: shiftwidth=2 sts=2 expandtab