blob: 7153a561d3602731e60cf6029cf30990207d8b7d [file] [log] [blame]
Bram Moolenaar63a60de2016-06-04 22:08:55 +02001" Tests for user defined commands
2
Bram Moolenaar62aec932022-01-29 21:45:34 +00003import './vim9.vim' as v9
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004
Bram Moolenaar63a60de2016-06-04 22:08:55 +02005" Test for <mods> in user defined commands
6function Test_cmdmods()
7 let g:mods = ''
8
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02009 command! -nargs=* MyCmd let g:mods = '<mods>'
Bram Moolenaar63a60de2016-06-04 22:08:55 +020010
11 MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020012 call assert_equal('', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020013 aboveleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020014 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020015 abo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020016 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020017 belowright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020018 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020019 bel MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020020 call assert_equal('belowright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020021 botright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020022 call assert_equal('botright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020023 bo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020024 call assert_equal('botright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020025 browse MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020026 call assert_equal('browse', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020027 bro MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020028 call assert_equal('browse', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020029 confirm MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020030 call assert_equal('confirm', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020031 conf MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020032 call assert_equal('confirm', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020033 hide MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020034 call assert_equal('hide', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020035 hid MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020036 call assert_equal('hide', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020037 keepalt MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020038 call assert_equal('keepalt', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020039 keepa MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020040 call assert_equal('keepalt', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020041 keepjumps MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020042 call assert_equal('keepjumps', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020043 keepj MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020044 call assert_equal('keepjumps', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020045 keepmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020046 call assert_equal('keepmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020047 kee MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020048 call assert_equal('keepmarks', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020049 keeppatterns MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020050 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020051 keepp MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020052 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020053 leftabove MyCmd " results in :aboveleft
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020054 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020055 lefta MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020056 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020057 lockmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020058 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020059 loc MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020060 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020061 " noautocmd MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020062 noswapfile MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020063 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020064 nos MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020065 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020066 rightbelow MyCmd " results in :belowright
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020067 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020068 rightb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020069 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020070 " sandbox MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020071 silent MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020072 call assert_equal('silent', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020073 sil MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020074 call assert_equal('silent', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020075 tab MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020076 call assert_equal('tab', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020077 topleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020078 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020079 to MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020080 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020081 " unsilent MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020082 verbose MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020083 call assert_equal('verbose', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020084 verb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020085 call assert_equal('verbose', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020086 vertical MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020087 call assert_equal('vertical', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020088 vert MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020089 call assert_equal('vertical', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020090
91 aboveleft belowright botright browse confirm hide keepalt keepjumps
92 \ keepmarks keeppatterns lockmarks noswapfile silent tab
93 \ topleft verbose vertical MyCmd
94
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020095 call assert_equal('browse confirm hide keepalt keepjumps ' .
96 \ 'keepmarks keeppatterns lockmarks noswapfile silent ' .
97 \ 'verbose aboveleft belowright botright tab topleft vertical', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020098
99 let g:mods = ''
100 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
101
102 vertical MyQCmd
103 call assert_equal('"vertical" ', g:mods)
104
105 delcommand MyCmd
106 delcommand MyQCmd
107 unlet g:mods
108endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200109
Bram Moolenaare61e5482019-04-27 15:05:12 +0200110func SaveCmdArgs(...)
111 let g:args = a:000
112endfunc
113
114func Test_f_args()
115 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
116
117 TestFArgs
118 call assert_equal([], g:args)
119
120 TestFArgs one two three
121 call assert_equal(['one', 'two', 'three'], g:args)
122
123 TestFArgs one\\two three
124 call assert_equal(['one\two', 'three'], g:args)
125
126 TestFArgs one\ two three
127 call assert_equal(['one two', 'three'], g:args)
128
129 TestFArgs one\"two three
130 call assert_equal(['one\"two', 'three'], g:args)
131
132 delcommand TestFArgs
133endfunc
134
135func Test_q_args()
136 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
137
138 TestQArgs
139 call assert_equal([''], g:args)
140
141 TestQArgs one two three
142 call assert_equal(['one two three'], g:args)
143
144 TestQArgs one\\two three
145 call assert_equal(['one\\two three'], g:args)
146
147 TestQArgs one\ two three
148 call assert_equal(['one\ two three'], g:args)
149
150 TestQArgs one\"two three
151 call assert_equal(['one\"two three'], g:args)
152
153 delcommand TestQArgs
154endfunc
155
156func Test_reg_arg()
157 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
158
159 TestRegArg
160 call assert_equal(['', ''], g:args)
161
162 TestRegArg x
163 call assert_equal(['x', 'x'], g:args)
164
165 delcommand TestRegArg
166endfunc
167
168func Test_no_arg()
169 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
170
171 TestNoArg
172 call assert_equal(['', '<>', '<x>', '<'], g:args)
173
174 TestNoArg one
175 call assert_equal(['one', '<>', '<x>', '<'], g:args)
176
177 delcommand TestNoArg
178endfunc
179
180func Test_range_arg()
181 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
182 new
183 call setline(1, range(100))
184 let lnum = line('.')
185
186 TestRangeArg
187 call assert_equal([0, lnum, lnum], g:args)
188
189 99TestRangeArg
190 call assert_equal([1, 99, 99], g:args)
191
192 88,99TestRangeArg
193 call assert_equal([2, 88, 99], g:args)
194
195 call assert_fails('102TestRangeArg', 'E16:')
196
197 bwipe!
198 delcommand TestRangeArg
199endfunc
200
Bram Moolenaareac784e2016-07-28 22:08:24 +0200201func Test_Ambiguous()
202 command Doit let g:didit = 'yes'
203 command Dothat let g:didthat = 'also'
204 call assert_fails('Do', 'E464:')
205 Doit
206 call assert_equal('yes', g:didit)
207 Dothat
208 call assert_equal('also', g:didthat)
209 unlet g:didit
210 unlet g:didthat
211
212 delcommand Doit
213 Do
214 call assert_equal('also', g:didthat)
215 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200216
217 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200218endfunc
219
Bram Moolenaar55d46912018-12-08 16:03:28 +0100220func Test_redefine_on_reload()
221 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
222 call assert_equal(0, exists(':ExistingCommand'))
223 source Xcommandexists
224 call assert_equal(2, exists(':ExistingCommand'))
225 " Redefining a command when reloading a script is OK.
226 source Xcommandexists
227 call assert_equal(2, exists(':ExistingCommand'))
228
229 " But redefining in another script is not OK.
230 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
231 call assert_fails('source Xcommandexists2', 'E174:')
232 call delete('Xcommandexists2')
233
234 " And defining twice in one script is not OK.
235 delcommand ExistingCommand
236 call assert_equal(0, exists(':ExistingCommand'))
237 call writefile([
238 \ 'command ExistingCommand echo "yes"',
239 \ 'command ExistingCommand echo "no"',
240 \ ], 'Xcommandexists')
241 call assert_fails('source Xcommandexists', 'E174:')
242 call assert_equal(2, exists(':ExistingCommand'))
243
244 call delete('Xcommandexists')
245 delcommand ExistingCommand
246endfunc
247
Bram Moolenaareac784e2016-07-28 22:08:24 +0200248func Test_CmdUndefined()
249 call assert_fails('Doit', 'E492:')
250 au CmdUndefined Doit :command Doit let g:didit = 'yes'
251 Doit
252 call assert_equal('yes', g:didit)
253 delcommand Doit
254
255 call assert_fails('Dothat', 'E492:')
256 au CmdUndefined * let g:didnot = 'yes'
257 call assert_fails('Dothat', 'E492:')
258 call assert_equal('yes', g:didnot)
259endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100260
261func Test_CmdErrors()
262 call assert_fails('com! docmd :', 'E183:')
263 call assert_fails('com! \<Tab> :', 'E182:')
264 call assert_fails('com! _ :', 'E182:')
265 call assert_fails('com! X :', 'E841:')
266 call assert_fails('com! - DoCmd :', 'E175:')
267 call assert_fails('com! -xxx DoCmd :', 'E181:')
268 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200269 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100270 call assert_fails('com! -complete DoCmd :', 'E179:')
271 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
272 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
273 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
274 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
275 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
276 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
277 call assert_fails('com! -count=x DoCmd :', 'E178:')
278 call assert_fails('com! -range=x DoCmd :', 'E178:')
279
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200280 com! -complete=file DoCmd :
281 call assert_match('E1208:', v:warningmsg)
282 let v:warningmsg = ''
283 com! -nargs=0 -complete=file DoCmd :
284 call assert_match('E1208:', v:warningmsg)
285
286 let lines =<< trim END
287 vim9script
288 com! -complete=file DoCmd :
289 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000290 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200291
292 let lines =<< trim END
293 vim9script
294 com! -nargs=0 -complete=file DoCmd :
295 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000296 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200297
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100298 com! -nargs=0 DoCmd :
299 call assert_fails('DoCmd x', 'E488:')
300
301 com! -nargs=1 DoCmd :
302 call assert_fails('DoCmd', 'E471:')
303
304 com! -nargs=+ DoCmd :
305 call assert_fails('DoCmd', 'E471:')
306
307 call assert_fails('com DoCmd :', 'E174:')
308 comclear
309 call assert_fails('delcom DoCmd', 'E184:')
310endfunc
311
312func CustomComplete(A, L, P)
313 return "January\nFebruary\nMars\n"
314endfunc
315
316func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000317 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100318endfunc
319
320func Test_CmdCompletion()
321 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000322 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100323
324 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000325 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100326
327 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
328 call assert_equal('"com -nargs=* + 0 1 ?', @:)
329
330 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100331 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100332
333 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
334 call assert_equal('"com -complete=color command compiler', @:)
335
336 command! DoCmd1 :
337 command! DoCmd2 :
338 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
339 call assert_equal('"com DoCmd1 DoCmd2', @:)
340
341 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
342 call assert_equal('"DoCmd1 DoCmd2', @:)
343
344 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
345 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
346
347 delcom DoCmd1
348 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
349 call assert_equal('"delcom DoCmd2', @:)
350
351 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
352 call assert_equal('"com DoCmd2', @:)
353
354 delcom DoCmd2
355 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
356 call assert_equal('"delcom DoC', @:)
357
358 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
359 call assert_equal('"com DoC', @:)
360
Martin Tournoijde69a732021-07-11 14:28:25 +0200361 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100362 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
363 call assert_equal('"DoCmd mswin xterm', @:)
364
Martin Tournoijde69a732021-07-11 14:28:25 +0200365 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100366 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
367 call assert_equal('"DoCmd January February Mars', @:)
368
Martin Tournoijde69a732021-07-11 14:28:25 +0200369 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100370 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
371 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
372
Martin Tournoijde69a732021-07-11 14:28:25 +0200373 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100374 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
375
Martin Tournoijde69a732021-07-11 14:28:25 +0200376 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100377 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100378
379 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200380 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100381 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
382
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200383 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200384 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200385 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
386
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000387 " custom completion for a pattern with a backslash
388 let g:ArgLead = ''
389 func! CustCompl(A, L, P)
390 let g:ArgLead = a:A
391 return ['one', 'two', 'three']
392 endfunc
393 com! -nargs=? -complete=customlist,CustCompl DoCmd
394 call feedkeys(":DoCmd a\\\t", 'xt')
395 call assert_equal('a\', g:ArgLead)
396 delfunc CustCompl
397
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100398 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100399endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100400
401func CallExecute(A, L, P)
402 " Drop first '\n'
403 return execute('echo "hi"')[1:]
404endfunc
405
406func Test_use_execute_in_completion()
407 command! -nargs=* -complete=custom,CallExecute DoExec :
408 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
409 call assert_equal('"DoExec hi', @:)
410 delcommand DoExec
411endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100412
413func Test_addr_all()
414 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
415 %DoSomething
416 call assert_equal(1, g:a1)
417 call assert_equal(line('$'), g:a2)
418
419 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
420 args one two three
421 %DoSomething
422 call assert_equal(1, g:a1)
423 call assert_equal(3, g:a2)
424
425 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
426 %DoSomething
427 for low in range(1, bufnr('$'))
428 if buflisted(low)
429 break
430 endif
431 endfor
432 call assert_equal(low, g:a1)
433 call assert_equal(bufnr('$'), g:a2)
434
435 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
436 %DoSomething
437 for low in range(1, bufnr('$'))
438 if bufloaded(low)
439 break
440 endif
441 endfor
442 call assert_equal(low, g:a1)
443 for up in range(bufnr('$'), 1, -1)
444 if bufloaded(up)
445 break
446 endif
447 endfor
448 call assert_equal(up, g:a2)
449
450 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
451 new
452 %DoSomething
453 call assert_equal(1, g:a1)
454 call assert_equal(winnr('$'), g:a2)
455 bwipe
456
457 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
458 tabnew
459 %DoSomething
460 call assert_equal(1, g:a1)
461 call assert_equal(len(gettabinfo()), g:a2)
462 bwipe
463
Bram Moolenaarb7316892019-05-01 18:08:42 +0200464 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100465 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200466 call assert_equal(line('.'), g:a1)
467 call assert_equal(line('.'), g:a2)
468 %DoSomething
469 call assert_equal(1, g:a1)
470 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100471
472 delcommand DoSomething
473endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200474
475func Test_command_list()
476 command! DoCmd :
477 call assert_equal("\n Name Args Address Complete Definition"
478 \ .. "\n DoCmd 0 :",
479 \ execute('command DoCmd'))
480
481 " Test with various -range= and -count= argument values.
482 command! -range DoCmd :
483 call assert_equal("\n Name Args Address Complete Definition"
484 \ .. "\n DoCmd 0 . :",
485 \ execute('command DoCmd'))
486 command! -range=% DoCmd :
487 call assert_equal("\n Name Args Address Complete Definition"
488 \ .. "\n DoCmd 0 % :",
489 \ execute('command! DoCmd'))
490 command! -range=2 DoCmd :
491 call assert_equal("\n Name Args Address Complete Definition"
492 \ .. "\n DoCmd 0 2 :",
493 \ execute('command DoCmd'))
494 command! -count=2 DoCmd :
495 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200496 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200497 \ execute('command DoCmd'))
498
499 " Test with various -addr= argument values.
500 command! -addr=lines DoCmd :
501 call assert_equal("\n Name Args Address Complete Definition"
502 \ .. "\n DoCmd 0 . :",
503 \ execute('command DoCmd'))
504 command! -addr=arguments DoCmd :
505 call assert_equal("\n Name Args Address Complete Definition"
506 \ .. "\n DoCmd 0 . arg :",
507 \ execute('command DoCmd'))
508 command! -addr=buffers DoCmd :
509 call assert_equal("\n Name Args Address Complete Definition"
510 \ .. "\n DoCmd 0 . buf :",
511 \ execute('command DoCmd'))
512 command! -addr=loaded_buffers DoCmd :
513 call assert_equal("\n Name Args Address Complete Definition"
514 \ .. "\n DoCmd 0 . load :",
515 \ execute('command DoCmd'))
516 command! -addr=windows DoCmd :
517 call assert_equal("\n Name Args Address Complete Definition"
518 \ .. "\n DoCmd 0 . win :",
519 \ execute('command DoCmd'))
520 command! -addr=tabs DoCmd :
521 call assert_equal("\n Name Args Address Complete Definition"
522 \ .. "\n DoCmd 0 . tab :",
523 \ execute('command DoCmd'))
524 command! -addr=other DoCmd :
525 call assert_equal("\n Name Args Address Complete Definition"
526 \ .. "\n DoCmd 0 . ? :",
527 \ execute('command DoCmd'))
528
529 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200530 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200531 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200532 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200533 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200534 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200535 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200536 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200537 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200538 command! -nargs=? -complete=custom,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200539 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200540 \ .. "\n DoCmd ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200541 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200542 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200543 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200544 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200545 \ execute('command DoCmd'))
546
547 " Test with various -narg= argument values.
548 command! -nargs=0 DoCmd :
549 call assert_equal("\n Name Args Address Complete Definition"
550 \ .. "\n DoCmd 0 :",
551 \ execute('command DoCmd'))
552 command! -nargs=1 DoCmd :
553 call assert_equal("\n Name Args Address Complete Definition"
554 \ .. "\n DoCmd 1 :",
555 \ execute('command DoCmd'))
556 command! -nargs=* DoCmd :
557 call assert_equal("\n Name Args Address Complete Definition"
558 \ .. "\n DoCmd * :",
559 \ execute('command DoCmd'))
560 command! -nargs=? DoCmd :
561 call assert_equal("\n Name Args Address Complete Definition"
562 \ .. "\n DoCmd ? :",
563 \ execute('command DoCmd'))
564 command! -nargs=+ DoCmd :
565 call assert_equal("\n Name Args Address Complete Definition"
566 \ .. "\n DoCmd + :",
567 \ execute('command DoCmd'))
568
569 " Test with other arguments.
570 command! -bang DoCmd :
571 call assert_equal("\n Name Args Address Complete Definition"
572 \ .. "\n! DoCmd 0 :",
573 \ execute('command DoCmd'))
574 command! -bar DoCmd :
575 call assert_equal("\n Name Args Address Complete Definition"
576 \ .. "\n| DoCmd 0 :",
577 \ execute('command DoCmd'))
578 command! -register DoCmd :
579 call assert_equal("\n Name Args Address Complete Definition"
580 \ .. "\n\" DoCmd 0 :",
581 \ execute('command DoCmd'))
582 command! -buffer DoCmd :
583 call assert_equal("\n Name Args Address Complete Definition"
584 \ .. "\nb DoCmd 0 :"
585 \ .. "\n\" DoCmd 0 :",
586 \ execute('command DoCmd'))
587 comclear
588
589 " Test with many args.
590 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
591 call assert_equal("\n Name Args Address Complete Definition"
592 \ .. "\n!\"b|DoCmd + 3c win environment :",
593 \ execute('command DoCmd'))
594 comclear
595
596 " Test with special characters in command definition.
597 command! DoCmd :<cr><tab><c-d>
598 call assert_equal("\n Name Args Address Complete Definition"
599 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
600 \ execute('command DoCmd'))
601
602 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000603 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200604 call assert_match("^\n"
605 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000606 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200607 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
608 \ execute('verbose command DoCmd'))
609
610 comclear
611 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
612 call assert_equal("\nNo user-defined commands found", execute('command'))
613endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100614
Bram Moolenaar8b633132020-03-20 18:20:51 +0100615" Test for a custom user completion returning the wrong value type
616func Test_usercmd_custom()
617 func T1(a, c, p)
618 return "a\nb\n"
619 endfunc
620 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200621 call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
622 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100623 delcommand TCmd1
624 delfunc T1
625
626 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200627 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100628 endfunc
629 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200630 call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
631 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100632 delcommand TCmd2
633 delfunc T2
634endfunc
635
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200636func Test_usercmd_with_block()
637 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200638 g:didit = 'yes' # comment
639 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200640 g:didmore = 'more'
641 }
642 DoSomething
643 call assert_equal('yes', g:didit)
644 call assert_equal('more', g:didmore)
645 unlet g:didit
646 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200647 delcommand DoSomething
648
649 command DoMap {
650 echo [1, 2, 3]->map((_, v) => v + 1)
651 }
652 DoMap
653 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200654
655 let lines =<< trim END
656 command DoesNotEnd {
657 echo 'hello'
658 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000659 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaar63b91732021-08-05 20:40:03 +0200660
661 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000662 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200663 echo 'hello' | echo 'there'
664 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000665 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200666 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000667 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000668 delcommand HelloThere
669
670 let lines =<< trim END
671 command BadCommand {
672 echo {
673 'key': 'value',
674 }
675 }
676 BadCommand
677 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000678 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200679endfunc
680
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200681func Test_delcommand_buffer()
682 command Global echo 'global'
683 command -buffer OneBuffer echo 'one'
684 new
685 command -buffer TwoBuffer echo 'two'
686 call assert_equal(0, exists(':OneBuffer'))
687 call assert_equal(2, exists(':Global'))
688 call assert_equal(2, exists(':TwoBuffer'))
689 delcommand -buffer TwoBuffer
690 call assert_equal(0, exists(':TwoBuffer'))
691 call assert_fails('delcommand -buffer Global', 'E1237:')
692 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
693 bwipe!
694 call assert_equal(2, exists(':OneBuffer'))
695 delcommand -buffer OneBuffer
696 call assert_equal(0, exists(':OneBuffer'))
697 call assert_fails('delcommand -buffer Global', 'E1237:')
698 delcommand Global
699 call assert_equal(0, exists(':Global'))
700endfunc
701
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000702def Test_count_with_quotes()
703 command -count GetCount g:nr = <count>
704 execute("GetCount 1'2")
705 assert_equal(12, g:nr)
706 execute("GetCount 1'234'567")
707 assert_equal(1'234'567, g:nr)
708
709 execute("GetCount 1'234'567'890'123'456'789'012")
710 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
711
712 # TODO: test with negative number once this is supported
713
714 assert_fails("GetCount '12", "E488:")
715 assert_fails("GetCount 12'", "E488:")
716 assert_fails("GetCount 1''2", "E488:")
717
718 assert_fails(":1'2GetCount", 'E492:')
719 new
720 setline(1, 'text')
721 normal ma
722 execute(":1, 'aprint")
723 bwipe!
724
725 unlet g:nr
726 delcommand GetCount
727enddef
728
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000729func DefCmd(name)
730 if len(a:name) > 30
731 return
732 endif
733 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
734 echo a:name
735 exe a:name
736endfunc
737
738func Test_recursive_define()
739 call DefCmd('Command')
740
741 let name = 'Command'
742 while len(name) < 30
743 exe 'delcommand ' .. name
744 let name ..= 'x'
745 endwhile
746endfunc
747
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000748
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100749" vim: shiftwidth=2 sts=2 expandtab