blob: 636ac2bd15812cd423505a2c80047c823cdc9da9 [file] [log] [blame]
Bram Moolenaar63a60de2016-06-04 22:08:55 +02001" Tests for user defined commands
2
Christian Brabandteb380b92025-07-07 20:53:55 +02003import './util/vim9.vim' as v9
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004
Christian Brabandteb380b92025-07-07 20:53:55 +02005source util/screendump.vim
Bram Moolenaarcf2594f2022-11-13 23:30:06 +00006
Bram Moolenaar63a60de2016-06-04 22:08:55 +02007" Test for <mods> in user defined commands
8function Test_cmdmods()
9 let g:mods = ''
10
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020011 command! -nargs=* MyCmd let g:mods = '<mods>'
Bram Moolenaar63a60de2016-06-04 22:08:55 +020012
13 MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020014 call assert_equal('', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020015 aboveleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020016 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020017 abo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020018 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020019 belowright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020020 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020021 bel MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020022 call assert_equal('belowright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020023 botright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020024 call assert_equal('botright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020025 bo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020026 call assert_equal('botright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020027 browse MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020028 call assert_equal('browse', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020029 bro MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020030 call assert_equal('browse', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020031 confirm MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020032 call assert_equal('confirm', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020033 conf MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020034 call assert_equal('confirm', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020035 hide MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020036 call assert_equal('hide', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020037 hid MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020038 call assert_equal('hide', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020039 keepalt MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020040 call assert_equal('keepalt', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020041 keepa MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020042 call assert_equal('keepalt', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020043 keepjumps MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020044 call assert_equal('keepjumps', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020045 keepj MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020046 call assert_equal('keepjumps', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020047 keepmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020048 call assert_equal('keepmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020049 kee MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020050 call assert_equal('keepmarks', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020051 keeppatterns MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020052 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020053 keepp MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020054 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020055 leftabove MyCmd " results in :aboveleft
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020056 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020057 lefta MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020058 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020059 lockmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020060 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020061 loc MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020062 call assert_equal('lockmarks', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +010063 noautocmd MyCmd
64 call assert_equal('noautocmd', g:mods)
65 noa MyCmd
66 call assert_equal('noautocmd', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020067 noswapfile MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020068 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020069 nos MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020070 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020071 rightbelow MyCmd " results in :belowright
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020072 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020073 rightb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020074 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020075 " sandbox MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020076 silent MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020077 call assert_equal('silent', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020078 sil MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020079 call assert_equal('silent', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +010080 silent! MyCmd
81 call assert_equal('silent!', g:mods)
82 sil! MyCmd
83 call assert_equal('silent!', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020084 tab MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020085 call assert_equal('tab', g:mods)
zeertzjq208567e2022-10-18 13:11:21 +010086 0tab MyCmd
87 call assert_equal('0tab', g:mods)
88 tab split
89 tab MyCmd
90 call assert_equal('tab', g:mods)
91 1tab MyCmd
92 call assert_equal('1tab', g:mods)
93 tabprev
94 tab MyCmd
95 call assert_equal('tab', g:mods)
96 2tab MyCmd
97 call assert_equal('2tab', g:mods)
98 2tabclose
Bram Moolenaar63a60de2016-06-04 22:08:55 +020099 topleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200100 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200101 to MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200102 call assert_equal('topleft', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +0100103 unsilent MyCmd
104 call assert_equal('unsilent', g:mods)
105 uns MyCmd
106 call assert_equal('unsilent', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200107 verbose MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200108 call assert_equal('verbose', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200109 verb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200110 call assert_equal('verbose', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +0100111 0verbose MyCmd
112 call assert_equal('0verbose', g:mods)
113 3verbose MyCmd
114 call assert_equal('3verbose', g:mods)
115 999verbose MyCmd
116 call assert_equal('999verbose', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200117 vertical MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200118 call assert_equal('vertical', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200119 vert MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200120 call assert_equal('vertical', g:mods)
zeertzjqd3de1782022-09-01 12:58:52 +0100121 horizontal MyCmd
122 call assert_equal('horizontal', g:mods)
123 hor MyCmd
124 call assert_equal('horizontal', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200125
126 aboveleft belowright botright browse confirm hide keepalt keepjumps
zeertzjq9359e8a2022-07-03 13:16:09 +0100127 \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent
128 \ tab topleft unsilent verbose vertical MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200129
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200130 call assert_equal('browse confirm hide keepalt keepjumps ' .
zeertzjq9359e8a2022-07-03 13:16:09 +0100131 \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' .
132 \ 'silent verbose aboveleft belowright botright tab topleft vertical',
133 \ g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200134
John Marriott9e795852024-08-20 20:57:23 +0200135 kee keep keepm keepma keepmar keepmarks keepa keepalt keepj keepjumps
136 \ keepp keeppatterns MyCmd
137 call assert_equal('keepalt keepjumps keepmarks keeppatterns', g:mods)
138
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200139 let g:mods = ''
140 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
141
142 vertical MyQCmd
143 call assert_equal('"vertical" ', g:mods)
144
145 delcommand MyCmd
146 delcommand MyQCmd
147 unlet g:mods
148endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200149
Bram Moolenaare61e5482019-04-27 15:05:12 +0200150func SaveCmdArgs(...)
151 let g:args = a:000
152endfunc
153
154func Test_f_args()
155 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
156
157 TestFArgs
158 call assert_equal([], 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 TestFArgs one\ two three
167 call assert_equal(['one two', 'three'], g:args)
168
169 TestFArgs one\"two three
170 call assert_equal(['one\"two', 'three'], g:args)
171
172 delcommand TestFArgs
173endfunc
174
175func Test_q_args()
176 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
177
178 TestQArgs
179 call assert_equal([''], 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 TestQArgs one\ two three
188 call assert_equal(['one\ two three'], g:args)
189
190 TestQArgs one\"two three
191 call assert_equal(['one\"two three'], g:args)
192
193 delcommand TestQArgs
194endfunc
195
196func Test_reg_arg()
197 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
198
199 TestRegArg
200 call assert_equal(['', ''], g:args)
201
202 TestRegArg x
203 call assert_equal(['x', 'x'], g:args)
204
205 delcommand TestRegArg
206endfunc
207
208func Test_no_arg()
209 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
210
211 TestNoArg
212 call assert_equal(['', '<>', '<x>', '<'], g:args)
213
214 TestNoArg one
215 call assert_equal(['one', '<>', '<x>', '<'], g:args)
216
217 delcommand TestNoArg
218endfunc
219
220func Test_range_arg()
221 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
222 new
223 call setline(1, range(100))
224 let lnum = line('.')
225
226 TestRangeArg
227 call assert_equal([0, lnum, lnum], g:args)
228
229 99TestRangeArg
230 call assert_equal([1, 99, 99], g:args)
231
232 88,99TestRangeArg
233 call assert_equal([2, 88, 99], g:args)
234
235 call assert_fails('102TestRangeArg', 'E16:')
236
237 bwipe!
238 delcommand TestRangeArg
239endfunc
240
Bram Moolenaareac784e2016-07-28 22:08:24 +0200241func Test_Ambiguous()
242 command Doit let g:didit = 'yes'
243 command Dothat let g:didthat = 'also'
244 call assert_fails('Do', 'E464:')
245 Doit
246 call assert_equal('yes', g:didit)
247 Dothat
248 call assert_equal('also', g:didthat)
249 unlet g:didit
250 unlet g:didthat
251
252 delcommand Doit
253 Do
254 call assert_equal('also', g:didthat)
255 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200256
257 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200258endfunc
259
Bram Moolenaar55d46912018-12-08 16:03:28 +0100260func Test_redefine_on_reload()
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100261 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100262 call assert_equal(0, exists(':ExistingCommand'))
263 source Xcommandexists
264 call assert_equal(2, exists(':ExistingCommand'))
265 " Redefining a command when reloading a script is OK.
266 source Xcommandexists
267 call assert_equal(2, exists(':ExistingCommand'))
268
269 " But redefining in another script is not OK.
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100270 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100271 call assert_fails('source Xcommandexists2', 'E174:')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100272
273 " And defining twice in one script is not OK.
274 delcommand ExistingCommand
275 call assert_equal(0, exists(':ExistingCommand'))
276 call writefile([
277 \ 'command ExistingCommand echo "yes"',
278 \ 'command ExistingCommand echo "no"',
279 \ ], 'Xcommandexists')
280 call assert_fails('source Xcommandexists', 'E174:')
281 call assert_equal(2, exists(':ExistingCommand'))
282
Bram Moolenaar55d46912018-12-08 16:03:28 +0100283 delcommand ExistingCommand
284endfunc
285
Bram Moolenaareac784e2016-07-28 22:08:24 +0200286func Test_CmdUndefined()
287 call assert_fails('Doit', 'E492:')
288 au CmdUndefined Doit :command Doit let g:didit = 'yes'
289 Doit
290 call assert_equal('yes', g:didit)
291 delcommand Doit
292
293 call assert_fails('Dothat', 'E492:')
294 au CmdUndefined * let g:didnot = 'yes'
295 call assert_fails('Dothat', 'E492:')
296 call assert_equal('yes', g:didnot)
297endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100298
299func Test_CmdErrors()
300 call assert_fails('com! docmd :', 'E183:')
301 call assert_fails('com! \<Tab> :', 'E182:')
302 call assert_fails('com! _ :', 'E182:')
303 call assert_fails('com! X :', 'E841:')
304 call assert_fails('com! - DoCmd :', 'E175:')
305 call assert_fails('com! -xxx DoCmd :', 'E181:')
306 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200307 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100308 call assert_fails('com! -complete DoCmd :', 'E179:')
309 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
310 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
311 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
312 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
313 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
314 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
315 call assert_fails('com! -count=x DoCmd :', 'E178:')
316 call assert_fails('com! -range=x DoCmd :', 'E178:')
317
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200318 com! -complete=file DoCmd :
319 call assert_match('E1208:', v:warningmsg)
320 let v:warningmsg = ''
321 com! -nargs=0 -complete=file DoCmd :
322 call assert_match('E1208:', v:warningmsg)
323
324 let lines =<< trim END
325 vim9script
326 com! -complete=file DoCmd :
327 END
zeertzjq67fe77d2025-04-20 10:21:18 +0200328 call v9.CheckScriptFailure(lines, 'E1208:', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200329
330 let lines =<< trim END
331 vim9script
332 com! -nargs=0 -complete=file DoCmd :
333 END
zeertzjq67fe77d2025-04-20 10:21:18 +0200334 call v9.CheckScriptFailure(lines, 'E1208:', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200335
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100336 com! -nargs=0 DoCmd :
337 call assert_fails('DoCmd x', 'E488:')
338
339 com! -nargs=1 DoCmd :
340 call assert_fails('DoCmd', 'E471:')
341
342 com! -nargs=+ DoCmd :
343 call assert_fails('DoCmd', 'E471:')
344
345 call assert_fails('com DoCmd :', 'E174:')
346 comclear
347 call assert_fails('delcom DoCmd', 'E184:')
zeertzjq33e54302022-12-19 16:49:27 +0000348
349 " These used to leak memory
350 call assert_fails('com! -complete=custom,CustomComplete _ :', 'E182:')
351 call assert_fails('com! -complete=custom,CustomComplete docmd :', 'E183:')
352 call assert_fails('com! -complete=custom,CustomComplete -xxx DoCmd :', 'E181:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100353endfunc
354
355func CustomComplete(A, L, P)
356 return "January\nFebruary\nMars\n"
357endfunc
358
359func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000360 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100361endfunc
362
363func Test_CmdCompletion()
364 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000365 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100366
367 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000368 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100369
370 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
371 call assert_equal('"com -nargs=* + 0 1 ?', @:)
372
373 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100374 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100375
376 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
377 call assert_equal('"com -complete=color command compiler', @:)
378
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000379 " try completion for unsupported argument values
380 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
381 call assert_equal("\"com -newarg=\t", @:)
382
383 " command completion after the name in a user defined command
384 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
385 call assert_equal("\"com MyCmd chistory", @:)
386
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000387 " delete the Check commands to avoid them showing up
388 call feedkeys(":com Check\<C-A>\<C-B>\"\<CR>", 'tx')
389 let cmds = substitute(@:, '"com ', '', '')->split()
390 for cmd in cmds
391 exe 'delcommand ' .. cmd
392 endfor
393 delcommand MissingFeature
394
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100395 command! DoCmd1 :
396 command! DoCmd2 :
397 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
398 call assert_equal('"com DoCmd1 DoCmd2', @:)
399
400 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
401 call assert_equal('"DoCmd1 DoCmd2', @:)
402
403 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
404 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
405
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000406 " try argument completion for a command without completion
407 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
408 call assert_equal("\"DoCmd1 \t", @:)
409
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100410 delcom DoCmd1
411 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
412 call assert_equal('"delcom DoCmd2', @:)
413
414 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
415 call assert_equal('"com DoCmd2', @:)
416
417 delcom DoCmd2
418 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
419 call assert_equal('"delcom DoC', @:)
420
421 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
422 call assert_equal('"com DoC', @:)
423
Martin Tournoijde69a732021-07-11 14:28:25 +0200424 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100425 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
426 call assert_equal('"DoCmd mswin xterm', @:)
427
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000428 " Test for file name completion
429 com! -nargs=1 -complete=file DoCmd :
430 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
431 call assert_equal('"DoCmd README.txt', @:)
432
433 " Test for buffer name completion
434 com! -nargs=1 -complete=buffer DoCmd :
435 let bnum = bufadd('BufForUserCmd')
436 call setbufvar(bnum, '&buflisted', 1)
437 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
438 call assert_equal('"DoCmd BufForUserCmd', @:)
439 bwipe BufForUserCmd
440 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
441 call assert_equal('"DoCmd BufFor', @:)
442
Martin Tournoijde69a732021-07-11 14:28:25 +0200443 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100444 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
445 call assert_equal('"DoCmd January February Mars', @:)
446
Martin Tournoijde69a732021-07-11 14:28:25 +0200447 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100448 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
449 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
450
Martin Tournoijde69a732021-07-11 14:28:25 +0200451 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100452 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
453
Martin Tournoijde69a732021-07-11 14:28:25 +0200454 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100455 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100456
457 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200458 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100459 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
460
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200461 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200462 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200463 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
464
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000465 " custom completion for a pattern with a backslash
466 let g:ArgLead = ''
467 func! CustCompl(A, L, P)
468 let g:ArgLead = a:A
469 return ['one', 'two', 'three']
470 endfunc
471 com! -nargs=? -complete=customlist,CustCompl DoCmd
472 call feedkeys(":DoCmd a\\\t", 'xt')
473 call assert_equal('a\', g:ArgLead)
474 delfunc CustCompl
475
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100476 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100477endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100478
479func CallExecute(A, L, P)
480 " Drop first '\n'
481 return execute('echo "hi"')[1:]
482endfunc
483
484func Test_use_execute_in_completion()
485 command! -nargs=* -complete=custom,CallExecute DoExec :
486 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
487 call assert_equal('"DoExec hi', @:)
488 delcommand DoExec
489endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100490
491func Test_addr_all()
492 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
493 %DoSomething
494 call assert_equal(1, g:a1)
495 call assert_equal(line('$'), g:a2)
496
497 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
498 args one two three
499 %DoSomething
500 call assert_equal(1, g:a1)
501 call assert_equal(3, g:a2)
502
503 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
504 %DoSomething
505 for low in range(1, bufnr('$'))
506 if buflisted(low)
507 break
508 endif
509 endfor
510 call assert_equal(low, g:a1)
511 call assert_equal(bufnr('$'), g:a2)
512
513 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
514 %DoSomething
515 for low in range(1, bufnr('$'))
516 if bufloaded(low)
517 break
518 endif
519 endfor
520 call assert_equal(low, g:a1)
521 for up in range(bufnr('$'), 1, -1)
522 if bufloaded(up)
523 break
524 endif
525 endfor
526 call assert_equal(up, g:a2)
527
528 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
529 new
530 %DoSomething
531 call assert_equal(1, g:a1)
532 call assert_equal(winnr('$'), g:a2)
533 bwipe
534
535 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
536 tabnew
537 %DoSomething
538 call assert_equal(1, g:a1)
539 call assert_equal(len(gettabinfo()), g:a2)
540 bwipe
541
Bram Moolenaarb7316892019-05-01 18:08:42 +0200542 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100543 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200544 call assert_equal(line('.'), g:a1)
545 call assert_equal(line('.'), g:a2)
546 %DoSomething
547 call assert_equal(1, g:a1)
548 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100549
550 delcommand DoSomething
551endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200552
553func Test_command_list()
554 command! DoCmd :
555 call assert_equal("\n Name Args Address Complete Definition"
556 \ .. "\n DoCmd 0 :",
557 \ execute('command DoCmd'))
558
559 " Test with various -range= and -count= argument values.
560 command! -range DoCmd :
561 call assert_equal("\n Name Args Address Complete Definition"
562 \ .. "\n DoCmd 0 . :",
563 \ execute('command DoCmd'))
564 command! -range=% DoCmd :
565 call assert_equal("\n Name Args Address Complete Definition"
566 \ .. "\n DoCmd 0 % :",
567 \ execute('command! DoCmd'))
568 command! -range=2 DoCmd :
569 call assert_equal("\n Name Args Address Complete Definition"
570 \ .. "\n DoCmd 0 2 :",
571 \ execute('command DoCmd'))
572 command! -count=2 DoCmd :
573 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200574 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200575 \ execute('command DoCmd'))
576
577 " Test with various -addr= argument values.
578 command! -addr=lines DoCmd :
579 call assert_equal("\n Name Args Address Complete Definition"
580 \ .. "\n DoCmd 0 . :",
581 \ execute('command DoCmd'))
582 command! -addr=arguments DoCmd :
583 call assert_equal("\n Name Args Address Complete Definition"
584 \ .. "\n DoCmd 0 . arg :",
585 \ execute('command DoCmd'))
586 command! -addr=buffers DoCmd :
587 call assert_equal("\n Name Args Address Complete Definition"
588 \ .. "\n DoCmd 0 . buf :",
589 \ execute('command DoCmd'))
590 command! -addr=loaded_buffers DoCmd :
591 call assert_equal("\n Name Args Address Complete Definition"
592 \ .. "\n DoCmd 0 . load :",
593 \ execute('command DoCmd'))
594 command! -addr=windows DoCmd :
595 call assert_equal("\n Name Args Address Complete Definition"
596 \ .. "\n DoCmd 0 . win :",
597 \ execute('command DoCmd'))
598 command! -addr=tabs DoCmd :
599 call assert_equal("\n Name Args Address Complete Definition"
600 \ .. "\n DoCmd 0 . tab :",
601 \ execute('command DoCmd'))
602 command! -addr=other DoCmd :
603 call assert_equal("\n Name Args Address Complete Definition"
604 \ .. "\n DoCmd 0 . ? :",
605 \ execute('command DoCmd'))
606
607 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200608 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200609 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200610 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200611 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200612 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200613 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200614 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200615 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200616 command! -nargs=? -complete=custom,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200617 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200618 \ .. "\n DoCmd ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200619 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200620 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200621 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200622 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200623 \ execute('command DoCmd'))
624
625 " Test with various -narg= argument values.
626 command! -nargs=0 DoCmd :
627 call assert_equal("\n Name Args Address Complete Definition"
628 \ .. "\n DoCmd 0 :",
629 \ execute('command DoCmd'))
630 command! -nargs=1 DoCmd :
631 call assert_equal("\n Name Args Address Complete Definition"
632 \ .. "\n DoCmd 1 :",
633 \ execute('command DoCmd'))
634 command! -nargs=* DoCmd :
635 call assert_equal("\n Name Args Address Complete Definition"
636 \ .. "\n DoCmd * :",
637 \ execute('command DoCmd'))
638 command! -nargs=? DoCmd :
639 call assert_equal("\n Name Args Address Complete Definition"
640 \ .. "\n DoCmd ? :",
641 \ execute('command DoCmd'))
642 command! -nargs=+ DoCmd :
643 call assert_equal("\n Name Args Address Complete Definition"
644 \ .. "\n DoCmd + :",
645 \ execute('command DoCmd'))
646
647 " Test with other arguments.
648 command! -bang DoCmd :
649 call assert_equal("\n Name Args Address Complete Definition"
650 \ .. "\n! DoCmd 0 :",
651 \ execute('command DoCmd'))
652 command! -bar DoCmd :
653 call assert_equal("\n Name Args Address Complete Definition"
654 \ .. "\n| DoCmd 0 :",
655 \ execute('command DoCmd'))
656 command! -register DoCmd :
657 call assert_equal("\n Name Args Address Complete Definition"
658 \ .. "\n\" DoCmd 0 :",
659 \ execute('command DoCmd'))
660 command! -buffer DoCmd :
661 call assert_equal("\n Name Args Address Complete Definition"
662 \ .. "\nb DoCmd 0 :"
663 \ .. "\n\" DoCmd 0 :",
664 \ execute('command DoCmd'))
665 comclear
666
667 " Test with many args.
668 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
669 call assert_equal("\n Name Args Address Complete Definition"
670 \ .. "\n!\"b|DoCmd + 3c win environment :",
671 \ execute('command DoCmd'))
672 comclear
673
674 " Test with special characters in command definition.
675 command! DoCmd :<cr><tab><c-d>
676 call assert_equal("\n Name Args Address Complete Definition"
677 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
678 \ execute('command DoCmd'))
679
680 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000681 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200682 call assert_match("^\n"
683 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000684 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200685 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
686 \ execute('verbose command DoCmd'))
687
688 comclear
689 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
690 call assert_equal("\nNo user-defined commands found", execute('command'))
691endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100692
Bram Moolenaar8b633132020-03-20 18:20:51 +0100693" Test for a custom user completion returning the wrong value type
694func Test_usercmd_custom()
695 func T1(a, c, p)
696 return "a\nb\n"
697 endfunc
698 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaar55e93662022-09-10 13:52:26 +0100699 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 +0200700 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100701 delcommand TCmd1
702 delfunc T1
703
704 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200705 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100706 endfunc
707 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaar55e93662022-09-10 13:52:26 +0100708 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 +0200709 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100710 delcommand TCmd2
711 delfunc T2
712endfunc
713
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200714func Test_usercmd_with_block()
715 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200716 g:didit = 'yes' # comment
717 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200718 g:didmore = 'more'
719 }
720 DoSomething
721 call assert_equal('yes', g:didit)
722 call assert_equal('more', g:didmore)
723 unlet g:didit
724 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200725 delcommand DoSomething
726
727 command DoMap {
728 echo [1, 2, 3]->map((_, v) => v + 1)
729 }
730 DoMap
731 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200732
733 let lines =<< trim END
734 command DoesNotEnd {
735 echo 'hello'
736 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000737 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000738 delcommand DoesNotEnd
Bram Moolenaar63b91732021-08-05 20:40:03 +0200739
740 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000741 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200742 echo 'hello' | echo 'there'
743 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000744 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200745 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000746 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000747 delcommand HelloThere
748
749 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100750 command EchoCond {
751 const test: string = true
752 ? 'true'
753 : 'false'
754 g:result = test
755 }
756 EchoCond
757 END
758 call v9.CheckScriptSuccess(lines)
759 call assert_equal('true', g:result)
Bram Moolenaara13e7ac2022-05-06 21:24:31 +0100760 unlet g:result
761
762 call feedkeys(":EchoCond\<CR>", 'xt')
763 call assert_equal('true', g:result)
764
Bram Moolenaare442d592022-05-05 12:20:28 +0100765 delcommand EchoCond
766 unlet g:result
767
768 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000769 command BadCommand {
770 echo {
771 'key': 'value',
772 }
773 }
774 BadCommand
775 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000776 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000777 delcommand BadCommand
Christian Brabandt00cb2472023-09-05 20:46:25 +0200778
779 let lines =<< trim END
780 vim9script
781 command Cmd {
782 g:result = [1,
783 2]
784 }
785 Cmd
786 END
787 call v9.CheckScriptSuccess(lines)
788 call assert_equal([1, 2], g:result)
789 delcommand Cmd
790 unlet! g:result
791
792 let lines =<< trim END
793 vim9script
794 command Cmd {
795 g:result = and(0x80,
796 0x80)
797 }
798 Cmd
799 END
800 call v9.CheckScriptSuccess(lines)
801 call assert_equal(128, g:result)
802 delcommand Cmd
803 unlet! g:result
804
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200805endfunc
806
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200807func Test_delcommand_buffer()
808 command Global echo 'global'
809 command -buffer OneBuffer echo 'one'
810 new
811 command -buffer TwoBuffer echo 'two'
812 call assert_equal(0, exists(':OneBuffer'))
813 call assert_equal(2, exists(':Global'))
814 call assert_equal(2, exists(':TwoBuffer'))
815 delcommand -buffer TwoBuffer
816 call assert_equal(0, exists(':TwoBuffer'))
817 call assert_fails('delcommand -buffer Global', 'E1237:')
818 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
819 bwipe!
820 call assert_equal(2, exists(':OneBuffer'))
821 delcommand -buffer OneBuffer
822 call assert_equal(0, exists(':OneBuffer'))
823 call assert_fails('delcommand -buffer Global', 'E1237:')
824 delcommand Global
825 call assert_equal(0, exists(':Global'))
826endfunc
827
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000828def Test_count_with_quotes()
829 command -count GetCount g:nr = <count>
830 execute("GetCount 1'2")
831 assert_equal(12, g:nr)
832 execute("GetCount 1'234'567")
833 assert_equal(1'234'567, g:nr)
834
835 execute("GetCount 1'234'567'890'123'456'789'012")
836 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
837
838 # TODO: test with negative number once this is supported
839
840 assert_fails("GetCount '12", "E488:")
841 assert_fails("GetCount 12'", "E488:")
842 assert_fails("GetCount 1''2", "E488:")
843
844 assert_fails(":1'2GetCount", 'E492:')
845 new
846 setline(1, 'text')
847 normal ma
848 execute(":1, 'aprint")
849 bwipe!
850
851 unlet g:nr
852 delcommand GetCount
853enddef
854
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000855func DefCmd(name)
856 if len(a:name) > 30
857 return
858 endif
859 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
860 echo a:name
861 exe a:name
862endfunc
863
864func Test_recursive_define()
865 call DefCmd('Command')
866
867 let name = 'Command'
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000868 while len(name) <= 30
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000869 exe 'delcommand ' .. name
870 let name ..= 'x'
871 endwhile
872endfunc
873
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000874" Test for using buffer-local ambiguous user-defined commands
875func Test_buflocal_ambiguous_usercmd()
876 new
877 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
878 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
879
880 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
881 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
882 call assert_equal('"TestCmd ', @:)
883
884 delcommand TestCmd1
885 delcommand TestCmd2
886 bw!
887endfunc
888
zeertzjqb444ee72023-02-20 15:25:13 +0000889" Test for using buffer-local user command from cmdwin.
890func Test_buflocal_usercmd_cmdwin()
891 new
892 command -buffer TestCmd edit Test
893 " This used to crash Vim
894 call assert_fails("norm q::TestCmd\<CR>", 'E11:')
895 bw!
896endfunc
897
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000898" Test for using a multibyte character in a user command
899func Test_multibyte_in_usercmd()
900 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
901 new
902 call setline(1, "Hello\u3002")
903 SubJapanesePeriodToDot
904 call assert_equal('Hello.', getline(1))
905 bw!
906 delcommand SubJapanesePeriodToDot
907endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000908
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000909" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
910" legacy script.
911func Test_block_declaration_legacy_script()
912 let lines =<< trim END
913 command -range Rename {
914 var save = @a
915 @a = 'something'
916 g:someExpr = @a
917 @a = save
918 }
919 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100920 call writefile(lines, 'Xlegacy', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000921 source Xlegacy
922
923 let lines =<< trim END
924 let @a = 'saved'
925 Rename
926 call assert_equal('something', g:someExpr)
927 call assert_equal('saved', @a)
928
929 let g:someExpr = 'xxx'
930 let @a = 'also'
931 Rename
932 call assert_equal('something', g:someExpr)
933 call assert_equal('also', @a)
934 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100935 call writefile(lines, 'Xother', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000936 source Xother
937
938 unlet g:someExpr
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000939 delcommand Rename
940endfunc
941
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000942func Test_comclear_while_listing()
943 call CheckRunVimInTerminal()
944
945 let lines =<< trim END
946 set nocompatible
947 comclear
948 for i in range(1, 999)
949 exe 'command ' .. 'Foo' .. i .. ' bar'
950 endfor
951 au CmdlineLeave : call timer_start(0, {-> execute('comclear')})
952 END
953 call writefile(lines, 'Xcommandclear', 'D')
954 let buf = RunVimInTerminal('-S Xcommandclear', {'rows': 10})
955
956 " this was using freed memory
957 call term_sendkeys(buf, ":command\<CR>")
958 call TermWait(buf, 50)
959 call term_sendkeys(buf, "j")
960 call TermWait(buf, 50)
961 call term_sendkeys(buf, "G")
962 call term_sendkeys(buf, "\<CR>")
963
964 call StopVimInTerminal(buf)
965endfunc
966
Mike Williams0174d8f2025-06-08 15:41:52 +0200967" Test for listing user commands.
968func Test_command_list_0()
969 " Check space padding of attribute and name in command list
970 set vbs&
971 command! ShortCommand echo "ShortCommand"
972 command! VeryMuchLongerCommand echo "VeryMuchLongerCommand"
973
974 redi @"> | com | redi END
975 pu
976
977 let bl = matchbufline(bufnr('%'), "^ ShortCommand 0", 1, '$')
978 call assert_false(bl == [])
979 let bl = matchbufline(bufnr('%'), "^ VeryMuchLongerCommand 0", 1, '$')
980 call assert_false(bl == [])
981
982 bwipe!
983 delcommand ShortCommand
984 delcommand VeryMuchLongerCommand
985endfunc
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000986
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100987" vim: shiftwidth=2 sts=2 expandtab