blob: f8cc1f53a1ca67367561a095d2dc853c39853e13 [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)
zeertzjq9359e8a2022-07-03 13:16:09 +010061 noautocmd MyCmd
62 call assert_equal('noautocmd', g:mods)
63 noa MyCmd
64 call assert_equal('noautocmd', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020065 noswapfile MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020066 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020067 nos MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020068 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020069 rightbelow MyCmd " results in :belowright
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020070 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020071 rightb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020072 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020073 " sandbox MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020074 silent MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020075 call assert_equal('silent', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020076 sil MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020077 call assert_equal('silent', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +010078 silent! MyCmd
79 call assert_equal('silent!', g:mods)
80 sil! MyCmd
81 call assert_equal('silent!', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020082 tab MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020083 call assert_equal('tab', g:mods)
zeertzjq208567e2022-10-18 13:11:21 +010084 0tab MyCmd
85 call assert_equal('0tab', g:mods)
86 tab split
87 tab MyCmd
88 call assert_equal('tab', g:mods)
89 1tab MyCmd
90 call assert_equal('1tab', g:mods)
91 tabprev
92 tab MyCmd
93 call assert_equal('tab', g:mods)
94 2tab MyCmd
95 call assert_equal('2tab', g:mods)
96 2tabclose
Bram Moolenaar63a60de2016-06-04 22:08:55 +020097 topleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020098 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020099 to MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200100 call assert_equal('topleft', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +0100101 unsilent MyCmd
102 call assert_equal('unsilent', g:mods)
103 uns MyCmd
104 call assert_equal('unsilent', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200105 verbose MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200106 call assert_equal('verbose', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200107 verb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200108 call assert_equal('verbose', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +0100109 0verbose MyCmd
110 call assert_equal('0verbose', g:mods)
111 3verbose MyCmd
112 call assert_equal('3verbose', g:mods)
113 999verbose MyCmd
114 call assert_equal('999verbose', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200115 vertical MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200116 call assert_equal('vertical', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200117 vert MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200118 call assert_equal('vertical', g:mods)
zeertzjqd3de1782022-09-01 12:58:52 +0100119 horizontal MyCmd
120 call assert_equal('horizontal', g:mods)
121 hor MyCmd
122 call assert_equal('horizontal', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200123
124 aboveleft belowright botright browse confirm hide keepalt keepjumps
zeertzjq9359e8a2022-07-03 13:16:09 +0100125 \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent
126 \ tab topleft unsilent verbose vertical MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200127
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200128 call assert_equal('browse confirm hide keepalt keepjumps ' .
zeertzjq9359e8a2022-07-03 13:16:09 +0100129 \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' .
130 \ 'silent verbose aboveleft belowright botright tab topleft vertical',
131 \ g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200132
133 let g:mods = ''
134 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
135
136 vertical MyQCmd
137 call assert_equal('"vertical" ', g:mods)
138
139 delcommand MyCmd
140 delcommand MyQCmd
141 unlet g:mods
142endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200143
Bram Moolenaare61e5482019-04-27 15:05:12 +0200144func SaveCmdArgs(...)
145 let g:args = a:000
146endfunc
147
148func Test_f_args()
149 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
150
151 TestFArgs
152 call assert_equal([], g:args)
153
154 TestFArgs one two three
155 call assert_equal(['one', 'two', 'three'], g:args)
156
157 TestFArgs one\\two three
158 call assert_equal(['one\two', 'three'], g:args)
159
160 TestFArgs one\ two three
161 call assert_equal(['one two', 'three'], g:args)
162
163 TestFArgs one\"two three
164 call assert_equal(['one\"two', 'three'], g:args)
165
166 delcommand TestFArgs
167endfunc
168
169func Test_q_args()
170 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
171
172 TestQArgs
173 call assert_equal([''], g:args)
174
175 TestQArgs one two three
176 call assert_equal(['one two three'], g:args)
177
178 TestQArgs one\\two three
179 call assert_equal(['one\\two three'], g:args)
180
181 TestQArgs one\ two three
182 call assert_equal(['one\ two three'], g:args)
183
184 TestQArgs one\"two three
185 call assert_equal(['one\"two three'], g:args)
186
187 delcommand TestQArgs
188endfunc
189
190func Test_reg_arg()
191 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
192
193 TestRegArg
194 call assert_equal(['', ''], g:args)
195
196 TestRegArg x
197 call assert_equal(['x', 'x'], g:args)
198
199 delcommand TestRegArg
200endfunc
201
202func Test_no_arg()
203 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
204
205 TestNoArg
206 call assert_equal(['', '<>', '<x>', '<'], g:args)
207
208 TestNoArg one
209 call assert_equal(['one', '<>', '<x>', '<'], g:args)
210
211 delcommand TestNoArg
212endfunc
213
214func Test_range_arg()
215 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
216 new
217 call setline(1, range(100))
218 let lnum = line('.')
219
220 TestRangeArg
221 call assert_equal([0, lnum, lnum], g:args)
222
223 99TestRangeArg
224 call assert_equal([1, 99, 99], g:args)
225
226 88,99TestRangeArg
227 call assert_equal([2, 88, 99], g:args)
228
229 call assert_fails('102TestRangeArg', 'E16:')
230
231 bwipe!
232 delcommand TestRangeArg
233endfunc
234
Bram Moolenaareac784e2016-07-28 22:08:24 +0200235func Test_Ambiguous()
236 command Doit let g:didit = 'yes'
237 command Dothat let g:didthat = 'also'
238 call assert_fails('Do', 'E464:')
239 Doit
240 call assert_equal('yes', g:didit)
241 Dothat
242 call assert_equal('also', g:didthat)
243 unlet g:didit
244 unlet g:didthat
245
246 delcommand Doit
247 Do
248 call assert_equal('also', g:didthat)
249 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200250
251 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200252endfunc
253
Bram Moolenaar55d46912018-12-08 16:03:28 +0100254func Test_redefine_on_reload()
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100255 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100256 call assert_equal(0, exists(':ExistingCommand'))
257 source Xcommandexists
258 call assert_equal(2, exists(':ExistingCommand'))
259 " Redefining a command when reloading a script is OK.
260 source Xcommandexists
261 call assert_equal(2, exists(':ExistingCommand'))
262
263 " But redefining in another script is not OK.
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100264 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100265 call assert_fails('source Xcommandexists2', 'E174:')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100266
267 " And defining twice in one script is not OK.
268 delcommand ExistingCommand
269 call assert_equal(0, exists(':ExistingCommand'))
270 call writefile([
271 \ 'command ExistingCommand echo "yes"',
272 \ 'command ExistingCommand echo "no"',
273 \ ], 'Xcommandexists')
274 call assert_fails('source Xcommandexists', 'E174:')
275 call assert_equal(2, exists(':ExistingCommand'))
276
Bram Moolenaar55d46912018-12-08 16:03:28 +0100277 delcommand ExistingCommand
278endfunc
279
Bram Moolenaareac784e2016-07-28 22:08:24 +0200280func Test_CmdUndefined()
281 call assert_fails('Doit', 'E492:')
282 au CmdUndefined Doit :command Doit let g:didit = 'yes'
283 Doit
284 call assert_equal('yes', g:didit)
285 delcommand Doit
286
287 call assert_fails('Dothat', 'E492:')
288 au CmdUndefined * let g:didnot = 'yes'
289 call assert_fails('Dothat', 'E492:')
290 call assert_equal('yes', g:didnot)
291endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100292
293func Test_CmdErrors()
294 call assert_fails('com! docmd :', 'E183:')
295 call assert_fails('com! \<Tab> :', 'E182:')
296 call assert_fails('com! _ :', 'E182:')
297 call assert_fails('com! X :', 'E841:')
298 call assert_fails('com! - DoCmd :', 'E175:')
299 call assert_fails('com! -xxx DoCmd :', 'E181:')
300 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200301 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100302 call assert_fails('com! -complete DoCmd :', 'E179:')
303 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
304 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
305 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
306 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
307 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
308 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
309 call assert_fails('com! -count=x DoCmd :', 'E178:')
310 call assert_fails('com! -range=x DoCmd :', 'E178:')
311
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200312 com! -complete=file DoCmd :
313 call assert_match('E1208:', v:warningmsg)
314 let v:warningmsg = ''
315 com! -nargs=0 -complete=file DoCmd :
316 call assert_match('E1208:', v:warningmsg)
317
318 let lines =<< trim END
319 vim9script
320 com! -complete=file DoCmd :
321 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000322 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200323
324 let lines =<< trim END
325 vim9script
326 com! -nargs=0 -complete=file DoCmd :
327 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000328 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200329
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100330 com! -nargs=0 DoCmd :
331 call assert_fails('DoCmd x', 'E488:')
332
333 com! -nargs=1 DoCmd :
334 call assert_fails('DoCmd', 'E471:')
335
336 com! -nargs=+ DoCmd :
337 call assert_fails('DoCmd', 'E471:')
338
339 call assert_fails('com DoCmd :', 'E174:')
340 comclear
341 call assert_fails('delcom DoCmd', 'E184:')
342endfunc
343
344func CustomComplete(A, L, P)
345 return "January\nFebruary\nMars\n"
346endfunc
347
348func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000349 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100350endfunc
351
352func Test_CmdCompletion()
353 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000354 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100355
356 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000357 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100358
359 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
360 call assert_equal('"com -nargs=* + 0 1 ?', @:)
361
362 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100363 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100364
365 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
366 call assert_equal('"com -complete=color command compiler', @:)
367
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000368 " try completion for unsupported argument values
369 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
370 call assert_equal("\"com -newarg=\t", @:)
371
372 " command completion after the name in a user defined command
373 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
374 call assert_equal("\"com MyCmd chistory", @:)
375
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100376 command! DoCmd1 :
377 command! DoCmd2 :
378 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
379 call assert_equal('"com DoCmd1 DoCmd2', @:)
380
381 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
382 call assert_equal('"DoCmd1 DoCmd2', @:)
383
384 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
385 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
386
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000387 " try argument completion for a command without completion
388 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
389 call assert_equal("\"DoCmd1 \t", @:)
390
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100391 delcom DoCmd1
392 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
393 call assert_equal('"delcom DoCmd2', @:)
394
395 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
396 call assert_equal('"com DoCmd2', @:)
397
398 delcom DoCmd2
399 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
400 call assert_equal('"delcom DoC', @:)
401
402 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
403 call assert_equal('"com DoC', @:)
404
Martin Tournoijde69a732021-07-11 14:28:25 +0200405 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100406 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
407 call assert_equal('"DoCmd mswin xterm', @:)
408
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000409 " Test for file name completion
410 com! -nargs=1 -complete=file DoCmd :
411 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
412 call assert_equal('"DoCmd README.txt', @:)
413
414 " Test for buffer name completion
415 com! -nargs=1 -complete=buffer DoCmd :
416 let bnum = bufadd('BufForUserCmd')
417 call setbufvar(bnum, '&buflisted', 1)
418 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
419 call assert_equal('"DoCmd BufForUserCmd', @:)
420 bwipe BufForUserCmd
421 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
422 call assert_equal('"DoCmd BufFor', @:)
423
Martin Tournoijde69a732021-07-11 14:28:25 +0200424 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100425 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
426 call assert_equal('"DoCmd January February Mars', @:)
427
Martin Tournoijde69a732021-07-11 14:28:25 +0200428 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100429 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
430 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
431
Martin Tournoijde69a732021-07-11 14:28:25 +0200432 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100433 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
434
Martin Tournoijde69a732021-07-11 14:28:25 +0200435 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100436 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100437
438 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200439 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100440 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
441
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200442 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200443 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200444 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
445
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000446 " custom completion for a pattern with a backslash
447 let g:ArgLead = ''
448 func! CustCompl(A, L, P)
449 let g:ArgLead = a:A
450 return ['one', 'two', 'three']
451 endfunc
452 com! -nargs=? -complete=customlist,CustCompl DoCmd
453 call feedkeys(":DoCmd a\\\t", 'xt')
454 call assert_equal('a\', g:ArgLead)
455 delfunc CustCompl
456
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100457 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100458endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100459
460func CallExecute(A, L, P)
461 " Drop first '\n'
462 return execute('echo "hi"')[1:]
463endfunc
464
465func Test_use_execute_in_completion()
466 command! -nargs=* -complete=custom,CallExecute DoExec :
467 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
468 call assert_equal('"DoExec hi', @:)
469 delcommand DoExec
470endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100471
472func Test_addr_all()
473 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
474 %DoSomething
475 call assert_equal(1, g:a1)
476 call assert_equal(line('$'), g:a2)
477
478 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
479 args one two three
480 %DoSomething
481 call assert_equal(1, g:a1)
482 call assert_equal(3, g:a2)
483
484 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
485 %DoSomething
486 for low in range(1, bufnr('$'))
487 if buflisted(low)
488 break
489 endif
490 endfor
491 call assert_equal(low, g:a1)
492 call assert_equal(bufnr('$'), g:a2)
493
494 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
495 %DoSomething
496 for low in range(1, bufnr('$'))
497 if bufloaded(low)
498 break
499 endif
500 endfor
501 call assert_equal(low, g:a1)
502 for up in range(bufnr('$'), 1, -1)
503 if bufloaded(up)
504 break
505 endif
506 endfor
507 call assert_equal(up, g:a2)
508
509 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
510 new
511 %DoSomething
512 call assert_equal(1, g:a1)
513 call assert_equal(winnr('$'), g:a2)
514 bwipe
515
516 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
517 tabnew
518 %DoSomething
519 call assert_equal(1, g:a1)
520 call assert_equal(len(gettabinfo()), g:a2)
521 bwipe
522
Bram Moolenaarb7316892019-05-01 18:08:42 +0200523 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100524 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200525 call assert_equal(line('.'), g:a1)
526 call assert_equal(line('.'), g:a2)
527 %DoSomething
528 call assert_equal(1, g:a1)
529 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100530
531 delcommand DoSomething
532endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200533
534func Test_command_list()
535 command! DoCmd :
536 call assert_equal("\n Name Args Address Complete Definition"
537 \ .. "\n DoCmd 0 :",
538 \ execute('command DoCmd'))
539
540 " Test with various -range= and -count= argument values.
541 command! -range DoCmd :
542 call assert_equal("\n Name Args Address Complete Definition"
543 \ .. "\n DoCmd 0 . :",
544 \ execute('command DoCmd'))
545 command! -range=% DoCmd :
546 call assert_equal("\n Name Args Address Complete Definition"
547 \ .. "\n DoCmd 0 % :",
548 \ execute('command! DoCmd'))
549 command! -range=2 DoCmd :
550 call assert_equal("\n Name Args Address Complete Definition"
551 \ .. "\n DoCmd 0 2 :",
552 \ execute('command DoCmd'))
553 command! -count=2 DoCmd :
554 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200555 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200556 \ execute('command DoCmd'))
557
558 " Test with various -addr= argument values.
559 command! -addr=lines DoCmd :
560 call assert_equal("\n Name Args Address Complete Definition"
561 \ .. "\n DoCmd 0 . :",
562 \ execute('command DoCmd'))
563 command! -addr=arguments DoCmd :
564 call assert_equal("\n Name Args Address Complete Definition"
565 \ .. "\n DoCmd 0 . arg :",
566 \ execute('command DoCmd'))
567 command! -addr=buffers DoCmd :
568 call assert_equal("\n Name Args Address Complete Definition"
569 \ .. "\n DoCmd 0 . buf :",
570 \ execute('command DoCmd'))
571 command! -addr=loaded_buffers DoCmd :
572 call assert_equal("\n Name Args Address Complete Definition"
573 \ .. "\n DoCmd 0 . load :",
574 \ execute('command DoCmd'))
575 command! -addr=windows DoCmd :
576 call assert_equal("\n Name Args Address Complete Definition"
577 \ .. "\n DoCmd 0 . win :",
578 \ execute('command DoCmd'))
579 command! -addr=tabs DoCmd :
580 call assert_equal("\n Name Args Address Complete Definition"
581 \ .. "\n DoCmd 0 . tab :",
582 \ execute('command DoCmd'))
583 command! -addr=other DoCmd :
584 call assert_equal("\n Name Args Address Complete Definition"
585 \ .. "\n DoCmd 0 . ? :",
586 \ execute('command DoCmd'))
587
588 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200589 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200590 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200591 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200592 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200593 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200594 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200595 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200596 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200597 command! -nargs=? -complete=custom,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200598 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200599 \ .. "\n DoCmd ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200600 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200601 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200602 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200603 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200604 \ execute('command DoCmd'))
605
606 " Test with various -narg= argument values.
607 command! -nargs=0 DoCmd :
608 call assert_equal("\n Name Args Address Complete Definition"
609 \ .. "\n DoCmd 0 :",
610 \ execute('command DoCmd'))
611 command! -nargs=1 DoCmd :
612 call assert_equal("\n Name Args Address Complete Definition"
613 \ .. "\n DoCmd 1 :",
614 \ execute('command DoCmd'))
615 command! -nargs=* DoCmd :
616 call assert_equal("\n Name Args Address Complete Definition"
617 \ .. "\n DoCmd * :",
618 \ execute('command DoCmd'))
619 command! -nargs=? DoCmd :
620 call assert_equal("\n Name Args Address Complete Definition"
621 \ .. "\n DoCmd ? :",
622 \ execute('command DoCmd'))
623 command! -nargs=+ DoCmd :
624 call assert_equal("\n Name Args Address Complete Definition"
625 \ .. "\n DoCmd + :",
626 \ execute('command DoCmd'))
627
628 " Test with other arguments.
629 command! -bang DoCmd :
630 call assert_equal("\n Name Args Address Complete Definition"
631 \ .. "\n! DoCmd 0 :",
632 \ execute('command DoCmd'))
633 command! -bar DoCmd :
634 call assert_equal("\n Name Args Address Complete Definition"
635 \ .. "\n| DoCmd 0 :",
636 \ execute('command DoCmd'))
637 command! -register DoCmd :
638 call assert_equal("\n Name Args Address Complete Definition"
639 \ .. "\n\" DoCmd 0 :",
640 \ execute('command DoCmd'))
641 command! -buffer DoCmd :
642 call assert_equal("\n Name Args Address Complete Definition"
643 \ .. "\nb DoCmd 0 :"
644 \ .. "\n\" DoCmd 0 :",
645 \ execute('command DoCmd'))
646 comclear
647
648 " Test with many args.
649 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
650 call assert_equal("\n Name Args Address Complete Definition"
651 \ .. "\n!\"b|DoCmd + 3c win environment :",
652 \ execute('command DoCmd'))
653 comclear
654
655 " Test with special characters in command definition.
656 command! DoCmd :<cr><tab><c-d>
657 call assert_equal("\n Name Args Address Complete Definition"
658 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
659 \ execute('command DoCmd'))
660
661 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000662 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200663 call assert_match("^\n"
664 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000665 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200666 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
667 \ execute('verbose command DoCmd'))
668
669 comclear
670 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
671 call assert_equal("\nNo user-defined commands found", execute('command'))
672endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100673
Bram Moolenaar8b633132020-03-20 18:20:51 +0100674" Test for a custom user completion returning the wrong value type
675func Test_usercmd_custom()
676 func T1(a, c, p)
677 return "a\nb\n"
678 endfunc
679 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaar55e93662022-09-10 13:52:26 +0100680 call assert_fails('call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", "xt")', 'E1303: Custom list completion function does not return a List but a string')
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200681 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100682 delcommand TCmd1
683 delfunc T1
684
685 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200686 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100687 endfunc
688 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaar55e93662022-09-10 13:52:26 +0100689 call assert_fails('call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", "xt")', 'E1303: Custom list completion function does not return a List but a dict')
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200690 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100691 delcommand TCmd2
692 delfunc T2
693endfunc
694
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200695func Test_usercmd_with_block()
696 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200697 g:didit = 'yes' # comment
698 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200699 g:didmore = 'more'
700 }
701 DoSomething
702 call assert_equal('yes', g:didit)
703 call assert_equal('more', g:didmore)
704 unlet g:didit
705 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200706 delcommand DoSomething
707
708 command DoMap {
709 echo [1, 2, 3]->map((_, v) => v + 1)
710 }
711 DoMap
712 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200713
714 let lines =<< trim END
715 command DoesNotEnd {
716 echo 'hello'
717 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000718 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaar63b91732021-08-05 20:40:03 +0200719
720 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000721 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200722 echo 'hello' | echo 'there'
723 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000724 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200725 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000726 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000727 delcommand HelloThere
728
729 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100730 command EchoCond {
731 const test: string = true
732 ? 'true'
733 : 'false'
734 g:result = test
735 }
736 EchoCond
737 END
738 call v9.CheckScriptSuccess(lines)
739 call assert_equal('true', g:result)
Bram Moolenaara13e7ac2022-05-06 21:24:31 +0100740 unlet g:result
741
742 call feedkeys(":EchoCond\<CR>", 'xt')
743 call assert_equal('true', g:result)
744
Bram Moolenaare442d592022-05-05 12:20:28 +0100745 delcommand EchoCond
746 unlet g:result
747
748 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000749 command BadCommand {
750 echo {
751 'key': 'value',
752 }
753 }
754 BadCommand
755 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000756 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200757endfunc
758
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200759func Test_delcommand_buffer()
760 command Global echo 'global'
761 command -buffer OneBuffer echo 'one'
762 new
763 command -buffer TwoBuffer echo 'two'
764 call assert_equal(0, exists(':OneBuffer'))
765 call assert_equal(2, exists(':Global'))
766 call assert_equal(2, exists(':TwoBuffer'))
767 delcommand -buffer TwoBuffer
768 call assert_equal(0, exists(':TwoBuffer'))
769 call assert_fails('delcommand -buffer Global', 'E1237:')
770 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
771 bwipe!
772 call assert_equal(2, exists(':OneBuffer'))
773 delcommand -buffer OneBuffer
774 call assert_equal(0, exists(':OneBuffer'))
775 call assert_fails('delcommand -buffer Global', 'E1237:')
776 delcommand Global
777 call assert_equal(0, exists(':Global'))
778endfunc
779
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000780def Test_count_with_quotes()
781 command -count GetCount g:nr = <count>
782 execute("GetCount 1'2")
783 assert_equal(12, g:nr)
784 execute("GetCount 1'234'567")
785 assert_equal(1'234'567, g:nr)
786
787 execute("GetCount 1'234'567'890'123'456'789'012")
788 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
789
790 # TODO: test with negative number once this is supported
791
792 assert_fails("GetCount '12", "E488:")
793 assert_fails("GetCount 12'", "E488:")
794 assert_fails("GetCount 1''2", "E488:")
795
796 assert_fails(":1'2GetCount", 'E492:')
797 new
798 setline(1, 'text')
799 normal ma
800 execute(":1, 'aprint")
801 bwipe!
802
803 unlet g:nr
804 delcommand GetCount
805enddef
806
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000807func DefCmd(name)
808 if len(a:name) > 30
809 return
810 endif
811 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
812 echo a:name
813 exe a:name
814endfunc
815
816func Test_recursive_define()
817 call DefCmd('Command')
818
819 let name = 'Command'
820 while len(name) < 30
821 exe 'delcommand ' .. name
822 let name ..= 'x'
823 endwhile
824endfunc
825
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000826" Test for using buffer-local ambiguous user-defined commands
827func Test_buflocal_ambiguous_usercmd()
828 new
829 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
830 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
831
832 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
833 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
834 call assert_equal('"TestCmd ', @:)
835
836 delcommand TestCmd1
837 delcommand TestCmd2
838 bw!
839endfunc
840
841" Test for using a multibyte character in a user command
842func Test_multibyte_in_usercmd()
843 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
844 new
845 call setline(1, "Hello\u3002")
846 SubJapanesePeriodToDot
847 call assert_equal('Hello.', getline(1))
848 bw!
849 delcommand SubJapanesePeriodToDot
850endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000851
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000852" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
853" legacy script.
854func Test_block_declaration_legacy_script()
855 let lines =<< trim END
856 command -range Rename {
857 var save = @a
858 @a = 'something'
859 g:someExpr = @a
860 @a = save
861 }
862 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100863 call writefile(lines, 'Xlegacy', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000864 source Xlegacy
865
866 let lines =<< trim END
867 let @a = 'saved'
868 Rename
869 call assert_equal('something', g:someExpr)
870 call assert_equal('saved', @a)
871
872 let g:someExpr = 'xxx'
873 let @a = 'also'
874 Rename
875 call assert_equal('something', g:someExpr)
876 call assert_equal('also', @a)
877 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100878 call writefile(lines, 'Xother', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000879 source Xother
880
881 unlet g:someExpr
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000882 delcommand Rename
883endfunc
884
885
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100886" vim: shiftwidth=2 sts=2 expandtab