blob: 9c75a91972432e9b531a218d7061cb4f393a5d6f [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)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020084 topleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020085 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020086 to MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020087 call assert_equal('topleft', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +010088 unsilent MyCmd
89 call assert_equal('unsilent', g:mods)
90 uns MyCmd
91 call assert_equal('unsilent', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020092 verbose MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020093 call assert_equal('verbose', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020094 verb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020095 call assert_equal('verbose', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +010096 0verbose MyCmd
97 call assert_equal('0verbose', g:mods)
98 3verbose MyCmd
99 call assert_equal('3verbose', g:mods)
100 999verbose MyCmd
101 call assert_equal('999verbose', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200102 vertical MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200103 call assert_equal('vertical', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200104 vert MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200105 call assert_equal('vertical', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200106
107 aboveleft belowright botright browse confirm hide keepalt keepjumps
zeertzjq9359e8a2022-07-03 13:16:09 +0100108 \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent
109 \ tab topleft unsilent verbose vertical MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200110
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200111 call assert_equal('browse confirm hide keepalt keepjumps ' .
zeertzjq9359e8a2022-07-03 13:16:09 +0100112 \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' .
113 \ 'silent verbose aboveleft belowright botright tab topleft vertical',
114 \ g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200115
116 let g:mods = ''
117 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
118
119 vertical MyQCmd
120 call assert_equal('"vertical" ', g:mods)
121
122 delcommand MyCmd
123 delcommand MyQCmd
124 unlet g:mods
125endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200126
Bram Moolenaare61e5482019-04-27 15:05:12 +0200127func SaveCmdArgs(...)
128 let g:args = a:000
129endfunc
130
131func Test_f_args()
132 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
133
134 TestFArgs
135 call assert_equal([], g:args)
136
137 TestFArgs one two three
138 call assert_equal(['one', 'two', 'three'], g:args)
139
140 TestFArgs one\\two three
141 call assert_equal(['one\two', 'three'], g:args)
142
143 TestFArgs one\ two three
144 call assert_equal(['one two', 'three'], g:args)
145
146 TestFArgs one\"two three
147 call assert_equal(['one\"two', 'three'], g:args)
148
149 delcommand TestFArgs
150endfunc
151
152func Test_q_args()
153 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
154
155 TestQArgs
156 call assert_equal([''], g:args)
157
158 TestQArgs one two three
159 call assert_equal(['one two three'], g:args)
160
161 TestQArgs one\\two three
162 call assert_equal(['one\\two three'], g:args)
163
164 TestQArgs one\ two three
165 call assert_equal(['one\ two three'], g:args)
166
167 TestQArgs one\"two three
168 call assert_equal(['one\"two three'], g:args)
169
170 delcommand TestQArgs
171endfunc
172
173func Test_reg_arg()
174 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
175
176 TestRegArg
177 call assert_equal(['', ''], g:args)
178
179 TestRegArg x
180 call assert_equal(['x', 'x'], g:args)
181
182 delcommand TestRegArg
183endfunc
184
185func Test_no_arg()
186 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
187
188 TestNoArg
189 call assert_equal(['', '<>', '<x>', '<'], g:args)
190
191 TestNoArg one
192 call assert_equal(['one', '<>', '<x>', '<'], g:args)
193
194 delcommand TestNoArg
195endfunc
196
197func Test_range_arg()
198 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
199 new
200 call setline(1, range(100))
201 let lnum = line('.')
202
203 TestRangeArg
204 call assert_equal([0, lnum, lnum], g:args)
205
206 99TestRangeArg
207 call assert_equal([1, 99, 99], g:args)
208
209 88,99TestRangeArg
210 call assert_equal([2, 88, 99], g:args)
211
212 call assert_fails('102TestRangeArg', 'E16:')
213
214 bwipe!
215 delcommand TestRangeArg
216endfunc
217
Bram Moolenaareac784e2016-07-28 22:08:24 +0200218func Test_Ambiguous()
219 command Doit let g:didit = 'yes'
220 command Dothat let g:didthat = 'also'
221 call assert_fails('Do', 'E464:')
222 Doit
223 call assert_equal('yes', g:didit)
224 Dothat
225 call assert_equal('also', g:didthat)
226 unlet g:didit
227 unlet g:didthat
228
229 delcommand Doit
230 Do
231 call assert_equal('also', g:didthat)
232 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200233
234 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200235endfunc
236
Bram Moolenaar55d46912018-12-08 16:03:28 +0100237func Test_redefine_on_reload()
238 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
239 call assert_equal(0, exists(':ExistingCommand'))
240 source Xcommandexists
241 call assert_equal(2, exists(':ExistingCommand'))
242 " Redefining a command when reloading a script is OK.
243 source Xcommandexists
244 call assert_equal(2, exists(':ExistingCommand'))
245
246 " But redefining in another script is not OK.
247 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
248 call assert_fails('source Xcommandexists2', 'E174:')
249 call delete('Xcommandexists2')
250
251 " And defining twice in one script is not OK.
252 delcommand ExistingCommand
253 call assert_equal(0, exists(':ExistingCommand'))
254 call writefile([
255 \ 'command ExistingCommand echo "yes"',
256 \ 'command ExistingCommand echo "no"',
257 \ ], 'Xcommandexists')
258 call assert_fails('source Xcommandexists', 'E174:')
259 call assert_equal(2, exists(':ExistingCommand'))
260
261 call delete('Xcommandexists')
262 delcommand ExistingCommand
263endfunc
264
Bram Moolenaareac784e2016-07-28 22:08:24 +0200265func Test_CmdUndefined()
266 call assert_fails('Doit', 'E492:')
267 au CmdUndefined Doit :command Doit let g:didit = 'yes'
268 Doit
269 call assert_equal('yes', g:didit)
270 delcommand Doit
271
272 call assert_fails('Dothat', 'E492:')
273 au CmdUndefined * let g:didnot = 'yes'
274 call assert_fails('Dothat', 'E492:')
275 call assert_equal('yes', g:didnot)
276endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100277
278func Test_CmdErrors()
279 call assert_fails('com! docmd :', 'E183:')
280 call assert_fails('com! \<Tab> :', 'E182:')
281 call assert_fails('com! _ :', 'E182:')
282 call assert_fails('com! X :', 'E841:')
283 call assert_fails('com! - DoCmd :', 'E175:')
284 call assert_fails('com! -xxx DoCmd :', 'E181:')
285 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200286 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100287 call assert_fails('com! -complete DoCmd :', 'E179:')
288 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
289 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
290 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
291 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
292 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
293 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
294 call assert_fails('com! -count=x DoCmd :', 'E178:')
295 call assert_fails('com! -range=x DoCmd :', 'E178:')
296
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200297 com! -complete=file DoCmd :
298 call assert_match('E1208:', v:warningmsg)
299 let v:warningmsg = ''
300 com! -nargs=0 -complete=file DoCmd :
301 call assert_match('E1208:', v:warningmsg)
302
303 let lines =<< trim END
304 vim9script
305 com! -complete=file DoCmd :
306 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000307 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200308
309 let lines =<< trim END
310 vim9script
311 com! -nargs=0 -complete=file DoCmd :
312 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000313 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200314
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100315 com! -nargs=0 DoCmd :
316 call assert_fails('DoCmd x', 'E488:')
317
318 com! -nargs=1 DoCmd :
319 call assert_fails('DoCmd', 'E471:')
320
321 com! -nargs=+ DoCmd :
322 call assert_fails('DoCmd', 'E471:')
323
324 call assert_fails('com DoCmd :', 'E174:')
325 comclear
326 call assert_fails('delcom DoCmd', 'E184:')
327endfunc
328
329func CustomComplete(A, L, P)
330 return "January\nFebruary\nMars\n"
331endfunc
332
333func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000334 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100335endfunc
336
337func Test_CmdCompletion()
338 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000339 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100340
341 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000342 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100343
344 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
345 call assert_equal('"com -nargs=* + 0 1 ?', @:)
346
347 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100348 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100349
350 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
351 call assert_equal('"com -complete=color command compiler', @:)
352
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000353 " try completion for unsupported argument values
354 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
355 call assert_equal("\"com -newarg=\t", @:)
356
357 " command completion after the name in a user defined command
358 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
359 call assert_equal("\"com MyCmd chistory", @:)
360
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100361 command! DoCmd1 :
362 command! DoCmd2 :
363 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
364 call assert_equal('"com DoCmd1 DoCmd2', @:)
365
366 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
367 call assert_equal('"DoCmd1 DoCmd2', @:)
368
369 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
370 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
371
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000372 " try argument completion for a command without completion
373 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
374 call assert_equal("\"DoCmd1 \t", @:)
375
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100376 delcom DoCmd1
377 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
378 call assert_equal('"delcom DoCmd2', @:)
379
380 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
381 call assert_equal('"com DoCmd2', @:)
382
383 delcom DoCmd2
384 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
385 call assert_equal('"delcom DoC', @:)
386
387 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
388 call assert_equal('"com DoC', @:)
389
Martin Tournoijde69a732021-07-11 14:28:25 +0200390 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100391 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
392 call assert_equal('"DoCmd mswin xterm', @:)
393
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000394 " Test for file name completion
395 com! -nargs=1 -complete=file DoCmd :
396 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
397 call assert_equal('"DoCmd README.txt', @:)
398
399 " Test for buffer name completion
400 com! -nargs=1 -complete=buffer DoCmd :
401 let bnum = bufadd('BufForUserCmd')
402 call setbufvar(bnum, '&buflisted', 1)
403 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
404 call assert_equal('"DoCmd BufForUserCmd', @:)
405 bwipe BufForUserCmd
406 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
407 call assert_equal('"DoCmd BufFor', @:)
408
Martin Tournoijde69a732021-07-11 14:28:25 +0200409 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100410 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
411 call assert_equal('"DoCmd January February Mars', @:)
412
Martin Tournoijde69a732021-07-11 14:28:25 +0200413 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100414 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
415 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
416
Martin Tournoijde69a732021-07-11 14:28:25 +0200417 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100418 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
419
Martin Tournoijde69a732021-07-11 14:28:25 +0200420 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100421 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100422
423 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200424 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100425 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
426
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200427 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200428 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200429 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
430
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000431 " custom completion for a pattern with a backslash
432 let g:ArgLead = ''
433 func! CustCompl(A, L, P)
434 let g:ArgLead = a:A
435 return ['one', 'two', 'three']
436 endfunc
437 com! -nargs=? -complete=customlist,CustCompl DoCmd
438 call feedkeys(":DoCmd a\\\t", 'xt')
439 call assert_equal('a\', g:ArgLead)
440 delfunc CustCompl
441
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100442 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100443endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100444
445func CallExecute(A, L, P)
446 " Drop first '\n'
447 return execute('echo "hi"')[1:]
448endfunc
449
450func Test_use_execute_in_completion()
451 command! -nargs=* -complete=custom,CallExecute DoExec :
452 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
453 call assert_equal('"DoExec hi', @:)
454 delcommand DoExec
455endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100456
457func Test_addr_all()
458 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
459 %DoSomething
460 call assert_equal(1, g:a1)
461 call assert_equal(line('$'), g:a2)
462
463 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
464 args one two three
465 %DoSomething
466 call assert_equal(1, g:a1)
467 call assert_equal(3, g:a2)
468
469 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
470 %DoSomething
471 for low in range(1, bufnr('$'))
472 if buflisted(low)
473 break
474 endif
475 endfor
476 call assert_equal(low, g:a1)
477 call assert_equal(bufnr('$'), g:a2)
478
479 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
480 %DoSomething
481 for low in range(1, bufnr('$'))
482 if bufloaded(low)
483 break
484 endif
485 endfor
486 call assert_equal(low, g:a1)
487 for up in range(bufnr('$'), 1, -1)
488 if bufloaded(up)
489 break
490 endif
491 endfor
492 call assert_equal(up, g:a2)
493
494 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
495 new
496 %DoSomething
497 call assert_equal(1, g:a1)
498 call assert_equal(winnr('$'), g:a2)
499 bwipe
500
501 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
502 tabnew
503 %DoSomething
504 call assert_equal(1, g:a1)
505 call assert_equal(len(gettabinfo()), g:a2)
506 bwipe
507
Bram Moolenaarb7316892019-05-01 18:08:42 +0200508 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100509 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200510 call assert_equal(line('.'), g:a1)
511 call assert_equal(line('.'), g:a2)
512 %DoSomething
513 call assert_equal(1, g:a1)
514 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100515
516 delcommand DoSomething
517endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200518
519func Test_command_list()
520 command! DoCmd :
521 call assert_equal("\n Name Args Address Complete Definition"
522 \ .. "\n DoCmd 0 :",
523 \ execute('command DoCmd'))
524
525 " Test with various -range= and -count= argument values.
526 command! -range DoCmd :
527 call assert_equal("\n Name Args Address Complete Definition"
528 \ .. "\n DoCmd 0 . :",
529 \ execute('command DoCmd'))
530 command! -range=% DoCmd :
531 call assert_equal("\n Name Args Address Complete Definition"
532 \ .. "\n DoCmd 0 % :",
533 \ execute('command! DoCmd'))
534 command! -range=2 DoCmd :
535 call assert_equal("\n Name Args Address Complete Definition"
536 \ .. "\n DoCmd 0 2 :",
537 \ execute('command DoCmd'))
538 command! -count=2 DoCmd :
539 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200540 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200541 \ execute('command DoCmd'))
542
543 " Test with various -addr= argument values.
544 command! -addr=lines DoCmd :
545 call assert_equal("\n Name Args Address Complete Definition"
546 \ .. "\n DoCmd 0 . :",
547 \ execute('command DoCmd'))
548 command! -addr=arguments DoCmd :
549 call assert_equal("\n Name Args Address Complete Definition"
550 \ .. "\n DoCmd 0 . arg :",
551 \ execute('command DoCmd'))
552 command! -addr=buffers DoCmd :
553 call assert_equal("\n Name Args Address Complete Definition"
554 \ .. "\n DoCmd 0 . buf :",
555 \ execute('command DoCmd'))
556 command! -addr=loaded_buffers DoCmd :
557 call assert_equal("\n Name Args Address Complete Definition"
558 \ .. "\n DoCmd 0 . load :",
559 \ execute('command DoCmd'))
560 command! -addr=windows DoCmd :
561 call assert_equal("\n Name Args Address Complete Definition"
562 \ .. "\n DoCmd 0 . win :",
563 \ execute('command DoCmd'))
564 command! -addr=tabs DoCmd :
565 call assert_equal("\n Name Args Address Complete Definition"
566 \ .. "\n DoCmd 0 . tab :",
567 \ execute('command DoCmd'))
568 command! -addr=other DoCmd :
569 call assert_equal("\n Name Args Address Complete Definition"
570 \ .. "\n DoCmd 0 . ? :",
571 \ execute('command DoCmd'))
572
573 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200574 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200575 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200576 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200577 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200578 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200579 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200580 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200581 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200582 command! -nargs=? -complete=custom,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200583 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200584 \ .. "\n DoCmd ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200585 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200586 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200587 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200588 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200589 \ execute('command DoCmd'))
590
591 " Test with various -narg= argument values.
592 command! -nargs=0 DoCmd :
593 call assert_equal("\n Name Args Address Complete Definition"
594 \ .. "\n DoCmd 0 :",
595 \ execute('command DoCmd'))
596 command! -nargs=1 DoCmd :
597 call assert_equal("\n Name Args Address Complete Definition"
598 \ .. "\n DoCmd 1 :",
599 \ execute('command DoCmd'))
600 command! -nargs=* DoCmd :
601 call assert_equal("\n Name Args Address Complete Definition"
602 \ .. "\n DoCmd * :",
603 \ execute('command DoCmd'))
604 command! -nargs=? DoCmd :
605 call assert_equal("\n Name Args Address Complete Definition"
606 \ .. "\n DoCmd ? :",
607 \ execute('command DoCmd'))
608 command! -nargs=+ DoCmd :
609 call assert_equal("\n Name Args Address Complete Definition"
610 \ .. "\n DoCmd + :",
611 \ execute('command DoCmd'))
612
613 " Test with other arguments.
614 command! -bang DoCmd :
615 call assert_equal("\n Name Args Address Complete Definition"
616 \ .. "\n! DoCmd 0 :",
617 \ execute('command DoCmd'))
618 command! -bar DoCmd :
619 call assert_equal("\n Name Args Address Complete Definition"
620 \ .. "\n| DoCmd 0 :",
621 \ execute('command DoCmd'))
622 command! -register DoCmd :
623 call assert_equal("\n Name Args Address Complete Definition"
624 \ .. "\n\" DoCmd 0 :",
625 \ execute('command DoCmd'))
626 command! -buffer DoCmd :
627 call assert_equal("\n Name Args Address Complete Definition"
628 \ .. "\nb DoCmd 0 :"
629 \ .. "\n\" DoCmd 0 :",
630 \ execute('command DoCmd'))
631 comclear
632
633 " Test with many args.
634 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
635 call assert_equal("\n Name Args Address Complete Definition"
636 \ .. "\n!\"b|DoCmd + 3c win environment :",
637 \ execute('command DoCmd'))
638 comclear
639
640 " Test with special characters in command definition.
641 command! DoCmd :<cr><tab><c-d>
642 call assert_equal("\n Name Args Address Complete Definition"
643 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
644 \ execute('command DoCmd'))
645
646 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000647 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200648 call assert_match("^\n"
649 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000650 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200651 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
652 \ execute('verbose command DoCmd'))
653
654 comclear
655 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
656 call assert_equal("\nNo user-defined commands found", execute('command'))
657endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100658
Bram Moolenaar8b633132020-03-20 18:20:51 +0100659" Test for a custom user completion returning the wrong value type
660func Test_usercmd_custom()
661 func T1(a, c, p)
662 return "a\nb\n"
663 endfunc
664 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200665 call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
666 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100667 delcommand TCmd1
668 delfunc T1
669
670 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200671 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100672 endfunc
673 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200674 call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
675 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100676 delcommand TCmd2
677 delfunc T2
678endfunc
679
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200680func Test_usercmd_with_block()
681 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200682 g:didit = 'yes' # comment
683 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200684 g:didmore = 'more'
685 }
686 DoSomething
687 call assert_equal('yes', g:didit)
688 call assert_equal('more', g:didmore)
689 unlet g:didit
690 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200691 delcommand DoSomething
692
693 command DoMap {
694 echo [1, 2, 3]->map((_, v) => v + 1)
695 }
696 DoMap
697 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200698
699 let lines =<< trim END
700 command DoesNotEnd {
701 echo 'hello'
702 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000703 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaar63b91732021-08-05 20:40:03 +0200704
705 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000706 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200707 echo 'hello' | echo 'there'
708 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000709 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200710 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000711 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000712 delcommand HelloThere
713
714 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100715 command EchoCond {
716 const test: string = true
717 ? 'true'
718 : 'false'
719 g:result = test
720 }
721 EchoCond
722 END
723 call v9.CheckScriptSuccess(lines)
724 call assert_equal('true', g:result)
Bram Moolenaara13e7ac2022-05-06 21:24:31 +0100725 unlet g:result
726
727 call feedkeys(":EchoCond\<CR>", 'xt')
728 call assert_equal('true', g:result)
729
Bram Moolenaare442d592022-05-05 12:20:28 +0100730 delcommand EchoCond
731 unlet g:result
732
733 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000734 command BadCommand {
735 echo {
736 'key': 'value',
737 }
738 }
739 BadCommand
740 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000741 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200742endfunc
743
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200744func Test_delcommand_buffer()
745 command Global echo 'global'
746 command -buffer OneBuffer echo 'one'
747 new
748 command -buffer TwoBuffer echo 'two'
749 call assert_equal(0, exists(':OneBuffer'))
750 call assert_equal(2, exists(':Global'))
751 call assert_equal(2, exists(':TwoBuffer'))
752 delcommand -buffer TwoBuffer
753 call assert_equal(0, exists(':TwoBuffer'))
754 call assert_fails('delcommand -buffer Global', 'E1237:')
755 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
756 bwipe!
757 call assert_equal(2, exists(':OneBuffer'))
758 delcommand -buffer OneBuffer
759 call assert_equal(0, exists(':OneBuffer'))
760 call assert_fails('delcommand -buffer Global', 'E1237:')
761 delcommand Global
762 call assert_equal(0, exists(':Global'))
763endfunc
764
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000765def Test_count_with_quotes()
766 command -count GetCount g:nr = <count>
767 execute("GetCount 1'2")
768 assert_equal(12, g:nr)
769 execute("GetCount 1'234'567")
770 assert_equal(1'234'567, g:nr)
771
772 execute("GetCount 1'234'567'890'123'456'789'012")
773 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
774
775 # TODO: test with negative number once this is supported
776
777 assert_fails("GetCount '12", "E488:")
778 assert_fails("GetCount 12'", "E488:")
779 assert_fails("GetCount 1''2", "E488:")
780
781 assert_fails(":1'2GetCount", 'E492:')
782 new
783 setline(1, 'text')
784 normal ma
785 execute(":1, 'aprint")
786 bwipe!
787
788 unlet g:nr
789 delcommand GetCount
790enddef
791
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000792func DefCmd(name)
793 if len(a:name) > 30
794 return
795 endif
796 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
797 echo a:name
798 exe a:name
799endfunc
800
801func Test_recursive_define()
802 call DefCmd('Command')
803
804 let name = 'Command'
805 while len(name) < 30
806 exe 'delcommand ' .. name
807 let name ..= 'x'
808 endwhile
809endfunc
810
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000811" Test for using buffer-local ambiguous user-defined commands
812func Test_buflocal_ambiguous_usercmd()
813 new
814 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
815 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
816
817 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
818 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
819 call assert_equal('"TestCmd ', @:)
820
821 delcommand TestCmd1
822 delcommand TestCmd2
823 bw!
824endfunc
825
826" Test for using a multibyte character in a user command
827func Test_multibyte_in_usercmd()
828 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
829 new
830 call setline(1, "Hello\u3002")
831 SubJapanesePeriodToDot
832 call assert_equal('Hello.', getline(1))
833 bw!
834 delcommand SubJapanesePeriodToDot
835endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000836
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000837" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
838" legacy script.
839func Test_block_declaration_legacy_script()
840 let lines =<< trim END
841 command -range Rename {
842 var save = @a
843 @a = 'something'
844 g:someExpr = @a
845 @a = save
846 }
847 END
848 call writefile(lines, 'Xlegacy')
849 source Xlegacy
850
851 let lines =<< trim END
852 let @a = 'saved'
853 Rename
854 call assert_equal('something', g:someExpr)
855 call assert_equal('saved', @a)
856
857 let g:someExpr = 'xxx'
858 let @a = 'also'
859 Rename
860 call assert_equal('something', g:someExpr)
861 call assert_equal('also', @a)
862 END
863 call writefile(lines, 'Xother')
864 source Xother
865
866 unlet g:someExpr
867 call delete('Xlegacy')
868 call delete('Xother')
869 delcommand Rename
870endfunc
871
872
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100873" vim: shiftwidth=2 sts=2 expandtab