blob: 8a41004060a758da58e8cd349ba8d5f422d5502a [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)
zeertzjqd3de1782022-09-01 12:58:52 +0100106 horizontal MyCmd
107 call assert_equal('horizontal', g:mods)
108 hor MyCmd
109 call assert_equal('horizontal', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200110
111 aboveleft belowright botright browse confirm hide keepalt keepjumps
zeertzjq9359e8a2022-07-03 13:16:09 +0100112 \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent
113 \ tab topleft unsilent verbose vertical MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200114
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200115 call assert_equal('browse confirm hide keepalt keepjumps ' .
zeertzjq9359e8a2022-07-03 13:16:09 +0100116 \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' .
117 \ 'silent verbose aboveleft belowright botright tab topleft vertical',
118 \ g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200119
120 let g:mods = ''
121 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
122
123 vertical MyQCmd
124 call assert_equal('"vertical" ', g:mods)
125
126 delcommand MyCmd
127 delcommand MyQCmd
128 unlet g:mods
129endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200130
Bram Moolenaare61e5482019-04-27 15:05:12 +0200131func SaveCmdArgs(...)
132 let g:args = a:000
133endfunc
134
135func Test_f_args()
136 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
137
138 TestFArgs
139 call assert_equal([], g:args)
140
141 TestFArgs one two three
142 call assert_equal(['one', 'two', 'three'], g:args)
143
144 TestFArgs one\\two three
145 call assert_equal(['one\two', 'three'], g:args)
146
147 TestFArgs one\ two three
148 call assert_equal(['one two', 'three'], g:args)
149
150 TestFArgs one\"two three
151 call assert_equal(['one\"two', 'three'], g:args)
152
153 delcommand TestFArgs
154endfunc
155
156func Test_q_args()
157 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
158
159 TestQArgs
160 call assert_equal([''], g:args)
161
162 TestQArgs one two three
163 call assert_equal(['one two three'], g:args)
164
165 TestQArgs one\\two three
166 call assert_equal(['one\\two three'], g:args)
167
168 TestQArgs one\ two three
169 call assert_equal(['one\ two three'], g:args)
170
171 TestQArgs one\"two three
172 call assert_equal(['one\"two three'], g:args)
173
174 delcommand TestQArgs
175endfunc
176
177func Test_reg_arg()
178 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
179
180 TestRegArg
181 call assert_equal(['', ''], g:args)
182
183 TestRegArg x
184 call assert_equal(['x', 'x'], g:args)
185
186 delcommand TestRegArg
187endfunc
188
189func Test_no_arg()
190 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
191
192 TestNoArg
193 call assert_equal(['', '<>', '<x>', '<'], g:args)
194
195 TestNoArg one
196 call assert_equal(['one', '<>', '<x>', '<'], g:args)
197
198 delcommand TestNoArg
199endfunc
200
201func Test_range_arg()
202 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
203 new
204 call setline(1, range(100))
205 let lnum = line('.')
206
207 TestRangeArg
208 call assert_equal([0, lnum, lnum], g:args)
209
210 99TestRangeArg
211 call assert_equal([1, 99, 99], g:args)
212
213 88,99TestRangeArg
214 call assert_equal([2, 88, 99], g:args)
215
216 call assert_fails('102TestRangeArg', 'E16:')
217
218 bwipe!
219 delcommand TestRangeArg
220endfunc
221
Bram Moolenaareac784e2016-07-28 22:08:24 +0200222func Test_Ambiguous()
223 command Doit let g:didit = 'yes'
224 command Dothat let g:didthat = 'also'
225 call assert_fails('Do', 'E464:')
226 Doit
227 call assert_equal('yes', g:didit)
228 Dothat
229 call assert_equal('also', g:didthat)
230 unlet g:didit
231 unlet g:didthat
232
233 delcommand Doit
234 Do
235 call assert_equal('also', g:didthat)
236 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200237
238 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200239endfunc
240
Bram Moolenaar55d46912018-12-08 16:03:28 +0100241func Test_redefine_on_reload()
242 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
243 call assert_equal(0, exists(':ExistingCommand'))
244 source Xcommandexists
245 call assert_equal(2, exists(':ExistingCommand'))
246 " Redefining a command when reloading a script is OK.
247 source Xcommandexists
248 call assert_equal(2, exists(':ExistingCommand'))
249
250 " But redefining in another script is not OK.
251 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
252 call assert_fails('source Xcommandexists2', 'E174:')
253 call delete('Xcommandexists2')
254
255 " And defining twice in one script is not OK.
256 delcommand ExistingCommand
257 call assert_equal(0, exists(':ExistingCommand'))
258 call writefile([
259 \ 'command ExistingCommand echo "yes"',
260 \ 'command ExistingCommand echo "no"',
261 \ ], 'Xcommandexists')
262 call assert_fails('source Xcommandexists', 'E174:')
263 call assert_equal(2, exists(':ExistingCommand'))
264
265 call delete('Xcommandexists')
266 delcommand ExistingCommand
267endfunc
268
Bram Moolenaareac784e2016-07-28 22:08:24 +0200269func Test_CmdUndefined()
270 call assert_fails('Doit', 'E492:')
271 au CmdUndefined Doit :command Doit let g:didit = 'yes'
272 Doit
273 call assert_equal('yes', g:didit)
274 delcommand Doit
275
276 call assert_fails('Dothat', 'E492:')
277 au CmdUndefined * let g:didnot = 'yes'
278 call assert_fails('Dothat', 'E492:')
279 call assert_equal('yes', g:didnot)
280endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100281
282func Test_CmdErrors()
283 call assert_fails('com! docmd :', 'E183:')
284 call assert_fails('com! \<Tab> :', 'E182:')
285 call assert_fails('com! _ :', 'E182:')
286 call assert_fails('com! X :', 'E841:')
287 call assert_fails('com! - DoCmd :', 'E175:')
288 call assert_fails('com! -xxx DoCmd :', 'E181:')
289 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200290 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100291 call assert_fails('com! -complete DoCmd :', 'E179:')
292 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
293 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
294 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
295 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
296 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
297 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
298 call assert_fails('com! -count=x DoCmd :', 'E178:')
299 call assert_fails('com! -range=x DoCmd :', 'E178:')
300
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200301 com! -complete=file DoCmd :
302 call assert_match('E1208:', v:warningmsg)
303 let v:warningmsg = ''
304 com! -nargs=0 -complete=file DoCmd :
305 call assert_match('E1208:', v:warningmsg)
306
307 let lines =<< trim END
308 vim9script
309 com! -complete=file DoCmd :
310 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000311 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200312
313 let lines =<< trim END
314 vim9script
315 com! -nargs=0 -complete=file DoCmd :
316 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000317 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200318
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100319 com! -nargs=0 DoCmd :
320 call assert_fails('DoCmd x', 'E488:')
321
322 com! -nargs=1 DoCmd :
323 call assert_fails('DoCmd', 'E471:')
324
325 com! -nargs=+ DoCmd :
326 call assert_fails('DoCmd', 'E471:')
327
328 call assert_fails('com DoCmd :', 'E174:')
329 comclear
330 call assert_fails('delcom DoCmd', 'E184:')
331endfunc
332
333func CustomComplete(A, L, P)
334 return "January\nFebruary\nMars\n"
335endfunc
336
337func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000338 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100339endfunc
340
341func Test_CmdCompletion()
342 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000343 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100344
345 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000346 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100347
348 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
349 call assert_equal('"com -nargs=* + 0 1 ?', @:)
350
351 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100352 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100353
354 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
355 call assert_equal('"com -complete=color command compiler', @:)
356
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000357 " try completion for unsupported argument values
358 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
359 call assert_equal("\"com -newarg=\t", @:)
360
361 " command completion after the name in a user defined command
362 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
363 call assert_equal("\"com MyCmd chistory", @:)
364
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100365 command! DoCmd1 :
366 command! DoCmd2 :
367 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
368 call assert_equal('"com DoCmd1 DoCmd2', @:)
369
370 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
371 call assert_equal('"DoCmd1 DoCmd2', @:)
372
373 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
374 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
375
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000376 " try argument completion for a command without completion
377 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
378 call assert_equal("\"DoCmd1 \t", @:)
379
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100380 delcom DoCmd1
381 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
382 call assert_equal('"delcom DoCmd2', @:)
383
384 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
385 call assert_equal('"com DoCmd2', @:)
386
387 delcom DoCmd2
388 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
389 call assert_equal('"delcom DoC', @:)
390
391 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
392 call assert_equal('"com DoC', @:)
393
Martin Tournoijde69a732021-07-11 14:28:25 +0200394 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100395 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
396 call assert_equal('"DoCmd mswin xterm', @:)
397
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000398 " Test for file name completion
399 com! -nargs=1 -complete=file DoCmd :
400 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
401 call assert_equal('"DoCmd README.txt', @:)
402
403 " Test for buffer name completion
404 com! -nargs=1 -complete=buffer DoCmd :
405 let bnum = bufadd('BufForUserCmd')
406 call setbufvar(bnum, '&buflisted', 1)
407 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
408 call assert_equal('"DoCmd BufForUserCmd', @:)
409 bwipe BufForUserCmd
410 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
411 call assert_equal('"DoCmd BufFor', @:)
412
Martin Tournoijde69a732021-07-11 14:28:25 +0200413 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100414 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
415 call assert_equal('"DoCmd January February Mars', @:)
416
Martin Tournoijde69a732021-07-11 14:28:25 +0200417 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100418 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
419 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
420
Martin Tournoijde69a732021-07-11 14:28:25 +0200421 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100422 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
423
Martin Tournoijde69a732021-07-11 14:28:25 +0200424 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100425 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100426
427 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200428 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100429 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
430
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200431 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200432 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200433 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
434
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000435 " custom completion for a pattern with a backslash
436 let g:ArgLead = ''
437 func! CustCompl(A, L, P)
438 let g:ArgLead = a:A
439 return ['one', 'two', 'three']
440 endfunc
441 com! -nargs=? -complete=customlist,CustCompl DoCmd
442 call feedkeys(":DoCmd a\\\t", 'xt')
443 call assert_equal('a\', g:ArgLead)
444 delfunc CustCompl
445
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100446 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100447endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100448
449func CallExecute(A, L, P)
450 " Drop first '\n'
451 return execute('echo "hi"')[1:]
452endfunc
453
454func Test_use_execute_in_completion()
455 command! -nargs=* -complete=custom,CallExecute DoExec :
456 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
457 call assert_equal('"DoExec hi', @:)
458 delcommand DoExec
459endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100460
461func Test_addr_all()
462 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
463 %DoSomething
464 call assert_equal(1, g:a1)
465 call assert_equal(line('$'), g:a2)
466
467 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
468 args one two three
469 %DoSomething
470 call assert_equal(1, g:a1)
471 call assert_equal(3, g:a2)
472
473 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
474 %DoSomething
475 for low in range(1, bufnr('$'))
476 if buflisted(low)
477 break
478 endif
479 endfor
480 call assert_equal(low, g:a1)
481 call assert_equal(bufnr('$'), g:a2)
482
483 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
484 %DoSomething
485 for low in range(1, bufnr('$'))
486 if bufloaded(low)
487 break
488 endif
489 endfor
490 call assert_equal(low, g:a1)
491 for up in range(bufnr('$'), 1, -1)
492 if bufloaded(up)
493 break
494 endif
495 endfor
496 call assert_equal(up, g:a2)
497
498 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
499 new
500 %DoSomething
501 call assert_equal(1, g:a1)
502 call assert_equal(winnr('$'), g:a2)
503 bwipe
504
505 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
506 tabnew
507 %DoSomething
508 call assert_equal(1, g:a1)
509 call assert_equal(len(gettabinfo()), g:a2)
510 bwipe
511
Bram Moolenaarb7316892019-05-01 18:08:42 +0200512 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100513 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200514 call assert_equal(line('.'), g:a1)
515 call assert_equal(line('.'), g:a2)
516 %DoSomething
517 call assert_equal(1, g:a1)
518 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100519
520 delcommand DoSomething
521endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200522
523func Test_command_list()
524 command! DoCmd :
525 call assert_equal("\n Name Args Address Complete Definition"
526 \ .. "\n DoCmd 0 :",
527 \ execute('command DoCmd'))
528
529 " Test with various -range= and -count= argument values.
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=% DoCmd :
535 call assert_equal("\n Name Args Address Complete Definition"
536 \ .. "\n DoCmd 0 % :",
537 \ execute('command! DoCmd'))
538 command! -range=2 DoCmd :
539 call assert_equal("\n Name Args Address Complete Definition"
540 \ .. "\n DoCmd 0 2 :",
541 \ execute('command DoCmd'))
542 command! -count=2 DoCmd :
543 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200544 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200545 \ execute('command DoCmd'))
546
547 " Test with various -addr= argument values.
548 command! -addr=lines DoCmd :
549 call assert_equal("\n Name Args Address Complete Definition"
550 \ .. "\n DoCmd 0 . :",
551 \ execute('command DoCmd'))
552 command! -addr=arguments DoCmd :
553 call assert_equal("\n Name Args Address Complete Definition"
554 \ .. "\n DoCmd 0 . arg :",
555 \ execute('command DoCmd'))
556 command! -addr=buffers DoCmd :
557 call assert_equal("\n Name Args Address Complete Definition"
558 \ .. "\n DoCmd 0 . buf :",
559 \ execute('command DoCmd'))
560 command! -addr=loaded_buffers DoCmd :
561 call assert_equal("\n Name Args Address Complete Definition"
562 \ .. "\n DoCmd 0 . load :",
563 \ execute('command DoCmd'))
564 command! -addr=windows DoCmd :
565 call assert_equal("\n Name Args Address Complete Definition"
566 \ .. "\n DoCmd 0 . win :",
567 \ execute('command DoCmd'))
568 command! -addr=tabs DoCmd :
569 call assert_equal("\n Name Args Address Complete Definition"
570 \ .. "\n DoCmd 0 . tab :",
571 \ execute('command DoCmd'))
572 command! -addr=other DoCmd :
573 call assert_equal("\n Name Args Address Complete Definition"
574 \ .. "\n DoCmd 0 . ? :",
575 \ execute('command DoCmd'))
576
577 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200578 command! -nargs=1 -complete=arglist 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 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200581 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200582 command! -nargs=* -complete=augroup 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 * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200585 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200586 command! -nargs=? -complete=custom,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 ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200589 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200590 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200591 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200592 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200593 \ execute('command DoCmd'))
594
595 " Test with various -narg= argument values.
596 command! -nargs=0 DoCmd :
597 call assert_equal("\n Name Args Address Complete Definition"
598 \ .. "\n DoCmd 0 :",
599 \ execute('command DoCmd'))
600 command! -nargs=1 DoCmd :
601 call assert_equal("\n Name Args Address Complete Definition"
602 \ .. "\n DoCmd 1 :",
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 command! -nargs=+ DoCmd :
613 call assert_equal("\n Name Args Address Complete Definition"
614 \ .. "\n DoCmd + :",
615 \ execute('command DoCmd'))
616
617 " Test with other arguments.
618 command! -bang DoCmd :
619 call assert_equal("\n Name Args Address Complete Definition"
620 \ .. "\n! DoCmd 0 :",
621 \ execute('command DoCmd'))
622 command! -bar DoCmd :
623 call assert_equal("\n Name Args Address Complete Definition"
624 \ .. "\n| DoCmd 0 :",
625 \ execute('command DoCmd'))
626 command! -register DoCmd :
627 call assert_equal("\n Name Args Address Complete Definition"
628 \ .. "\n\" DoCmd 0 :",
629 \ execute('command DoCmd'))
630 command! -buffer DoCmd :
631 call assert_equal("\n Name Args Address Complete Definition"
632 \ .. "\nb DoCmd 0 :"
633 \ .. "\n\" DoCmd 0 :",
634 \ execute('command DoCmd'))
635 comclear
636
637 " Test with many args.
638 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
639 call assert_equal("\n Name Args Address Complete Definition"
640 \ .. "\n!\"b|DoCmd + 3c win environment :",
641 \ execute('command DoCmd'))
642 comclear
643
644 " Test with special characters in command definition.
645 command! DoCmd :<cr><tab><c-d>
646 call assert_equal("\n Name Args Address Complete Definition"
647 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
648 \ execute('command DoCmd'))
649
650 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000651 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200652 call assert_match("^\n"
653 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000654 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200655 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
656 \ execute('verbose command DoCmd'))
657
658 comclear
659 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
660 call assert_equal("\nNo user-defined commands found", execute('command'))
661endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100662
Bram Moolenaar8b633132020-03-20 18:20:51 +0100663" Test for a custom user completion returning the wrong value type
664func Test_usercmd_custom()
665 func T1(a, c, p)
666 return "a\nb\n"
667 endfunc
668 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaar55e93662022-09-10 13:52:26 +0100669 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 +0200670 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100671 delcommand TCmd1
672 delfunc T1
673
674 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200675 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100676 endfunc
677 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaar55e93662022-09-10 13:52:26 +0100678 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 +0200679 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100680 delcommand TCmd2
681 delfunc T2
682endfunc
683
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200684func Test_usercmd_with_block()
685 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200686 g:didit = 'yes' # comment
687 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200688 g:didmore = 'more'
689 }
690 DoSomething
691 call assert_equal('yes', g:didit)
692 call assert_equal('more', g:didmore)
693 unlet g:didit
694 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200695 delcommand DoSomething
696
697 command DoMap {
698 echo [1, 2, 3]->map((_, v) => v + 1)
699 }
700 DoMap
701 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200702
703 let lines =<< trim END
704 command DoesNotEnd {
705 echo 'hello'
706 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000707 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaar63b91732021-08-05 20:40:03 +0200708
709 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000710 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200711 echo 'hello' | echo 'there'
712 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000713 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200714 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000715 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000716 delcommand HelloThere
717
718 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100719 command EchoCond {
720 const test: string = true
721 ? 'true'
722 : 'false'
723 g:result = test
724 }
725 EchoCond
726 END
727 call v9.CheckScriptSuccess(lines)
728 call assert_equal('true', g:result)
Bram Moolenaara13e7ac2022-05-06 21:24:31 +0100729 unlet g:result
730
731 call feedkeys(":EchoCond\<CR>", 'xt')
732 call assert_equal('true', g:result)
733
Bram Moolenaare442d592022-05-05 12:20:28 +0100734 delcommand EchoCond
735 unlet g:result
736
737 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000738 command BadCommand {
739 echo {
740 'key': 'value',
741 }
742 }
743 BadCommand
744 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000745 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200746endfunc
747
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200748func Test_delcommand_buffer()
749 command Global echo 'global'
750 command -buffer OneBuffer echo 'one'
751 new
752 command -buffer TwoBuffer echo 'two'
753 call assert_equal(0, exists(':OneBuffer'))
754 call assert_equal(2, exists(':Global'))
755 call assert_equal(2, exists(':TwoBuffer'))
756 delcommand -buffer TwoBuffer
757 call assert_equal(0, exists(':TwoBuffer'))
758 call assert_fails('delcommand -buffer Global', 'E1237:')
759 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
760 bwipe!
761 call assert_equal(2, exists(':OneBuffer'))
762 delcommand -buffer OneBuffer
763 call assert_equal(0, exists(':OneBuffer'))
764 call assert_fails('delcommand -buffer Global', 'E1237:')
765 delcommand Global
766 call assert_equal(0, exists(':Global'))
767endfunc
768
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000769def Test_count_with_quotes()
770 command -count GetCount g:nr = <count>
771 execute("GetCount 1'2")
772 assert_equal(12, g:nr)
773 execute("GetCount 1'234'567")
774 assert_equal(1'234'567, g:nr)
775
776 execute("GetCount 1'234'567'890'123'456'789'012")
777 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
778
779 # TODO: test with negative number once this is supported
780
781 assert_fails("GetCount '12", "E488:")
782 assert_fails("GetCount 12'", "E488:")
783 assert_fails("GetCount 1''2", "E488:")
784
785 assert_fails(":1'2GetCount", 'E492:')
786 new
787 setline(1, 'text')
788 normal ma
789 execute(":1, 'aprint")
790 bwipe!
791
792 unlet g:nr
793 delcommand GetCount
794enddef
795
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000796func DefCmd(name)
797 if len(a:name) > 30
798 return
799 endif
800 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
801 echo a:name
802 exe a:name
803endfunc
804
805func Test_recursive_define()
806 call DefCmd('Command')
807
808 let name = 'Command'
809 while len(name) < 30
810 exe 'delcommand ' .. name
811 let name ..= 'x'
812 endwhile
813endfunc
814
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000815" Test for using buffer-local ambiguous user-defined commands
816func Test_buflocal_ambiguous_usercmd()
817 new
818 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
819 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
820
821 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
822 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
823 call assert_equal('"TestCmd ', @:)
824
825 delcommand TestCmd1
826 delcommand TestCmd2
827 bw!
828endfunc
829
830" Test for using a multibyte character in a user command
831func Test_multibyte_in_usercmd()
832 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
833 new
834 call setline(1, "Hello\u3002")
835 SubJapanesePeriodToDot
836 call assert_equal('Hello.', getline(1))
837 bw!
838 delcommand SubJapanesePeriodToDot
839endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000840
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000841" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
842" legacy script.
843func Test_block_declaration_legacy_script()
844 let lines =<< trim END
845 command -range Rename {
846 var save = @a
847 @a = 'something'
848 g:someExpr = @a
849 @a = save
850 }
851 END
852 call writefile(lines, 'Xlegacy')
853 source Xlegacy
854
855 let lines =<< trim END
856 let @a = 'saved'
857 Rename
858 call assert_equal('something', g:someExpr)
859 call assert_equal('saved', @a)
860
861 let g:someExpr = 'xxx'
862 let @a = 'also'
863 Rename
864 call assert_equal('something', g:someExpr)
865 call assert_equal('also', @a)
866 END
867 call writefile(lines, 'Xother')
868 source Xother
869
870 unlet g:someExpr
871 call delete('Xlegacy')
872 call delete('Xother')
873 delcommand Rename
874endfunc
875
876
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100877" vim: shiftwidth=2 sts=2 expandtab