blob: 57953ced8a22d71ed279539dad16930b8326ef4c [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 Moolenaarcf2594f2022-11-13 23:30:06 +00005source check.vim
6source screendump.vim
7
Bram Moolenaar63a60de2016-06-04 22:08:55 +02008" Test for <mods> in user defined commands
9function Test_cmdmods()
10 let g:mods = ''
11
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020012 command! -nargs=* MyCmd let g:mods = '<mods>'
Bram Moolenaar63a60de2016-06-04 22:08:55 +020013
14 MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020015 call assert_equal('', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020016 aboveleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020017 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020018 abo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020019 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020020 belowright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020021 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020022 bel MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020023 call assert_equal('belowright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020024 botright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020025 call assert_equal('botright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020026 bo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020027 call assert_equal('botright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020028 browse MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020029 call assert_equal('browse', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020030 bro MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020031 call assert_equal('browse', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020032 confirm MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020033 call assert_equal('confirm', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020034 conf MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020035 call assert_equal('confirm', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020036 hide MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020037 call assert_equal('hide', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020038 hid MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020039 call assert_equal('hide', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020040 keepalt MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020041 call assert_equal('keepalt', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020042 keepa MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020043 call assert_equal('keepalt', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020044 keepjumps MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020045 call assert_equal('keepjumps', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020046 keepj MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020047 call assert_equal('keepjumps', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020048 keepmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020049 call assert_equal('keepmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020050 kee MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020051 call assert_equal('keepmarks', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020052 keeppatterns MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020053 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020054 keepp MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020055 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020056 leftabove MyCmd " results in :aboveleft
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020057 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020058 lefta MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020059 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020060 lockmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020061 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020062 loc MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020063 call assert_equal('lockmarks', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +010064 noautocmd MyCmd
65 call assert_equal('noautocmd', g:mods)
66 noa MyCmd
67 call assert_equal('noautocmd', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020068 noswapfile MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020069 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020070 nos MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020071 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020072 rightbelow MyCmd " results in :belowright
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020073 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020074 rightb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020075 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020076 " sandbox MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020077 silent MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020078 call assert_equal('silent', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020079 sil MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020080 call assert_equal('silent', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +010081 silent! MyCmd
82 call assert_equal('silent!', g:mods)
83 sil! MyCmd
84 call assert_equal('silent!', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020085 tab MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020086 call assert_equal('tab', g:mods)
zeertzjq208567e2022-10-18 13:11:21 +010087 0tab MyCmd
88 call assert_equal('0tab', g:mods)
89 tab split
90 tab MyCmd
91 call assert_equal('tab', g:mods)
92 1tab MyCmd
93 call assert_equal('1tab', g:mods)
94 tabprev
95 tab MyCmd
96 call assert_equal('tab', g:mods)
97 2tab MyCmd
98 call assert_equal('2tab', g:mods)
99 2tabclose
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200100 topleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200101 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200102 to MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200103 call assert_equal('topleft', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +0100104 unsilent MyCmd
105 call assert_equal('unsilent', g:mods)
106 uns MyCmd
107 call assert_equal('unsilent', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200108 verbose MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200109 call assert_equal('verbose', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200110 verb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200111 call assert_equal('verbose', g:mods)
zeertzjq9359e8a2022-07-03 13:16:09 +0100112 0verbose MyCmd
113 call assert_equal('0verbose', g:mods)
114 3verbose MyCmd
115 call assert_equal('3verbose', g:mods)
116 999verbose MyCmd
117 call assert_equal('999verbose', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200118 vertical MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200119 call assert_equal('vertical', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +0200120 vert MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200121 call assert_equal('vertical', g:mods)
zeertzjqd3de1782022-09-01 12:58:52 +0100122 horizontal MyCmd
123 call assert_equal('horizontal', g:mods)
124 hor MyCmd
125 call assert_equal('horizontal', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200126
127 aboveleft belowright botright browse confirm hide keepalt keepjumps
zeertzjq9359e8a2022-07-03 13:16:09 +0100128 \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent
129 \ tab topleft unsilent verbose vertical MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200130
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200131 call assert_equal('browse confirm hide keepalt keepjumps ' .
zeertzjq9359e8a2022-07-03 13:16:09 +0100132 \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' .
133 \ 'silent verbose aboveleft belowright botright tab topleft vertical',
134 \ g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200135
136 let g:mods = ''
137 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
138
139 vertical MyQCmd
140 call assert_equal('"vertical" ', g:mods)
141
142 delcommand MyCmd
143 delcommand MyQCmd
144 unlet g:mods
145endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200146
Bram Moolenaare61e5482019-04-27 15:05:12 +0200147func SaveCmdArgs(...)
148 let g:args = a:000
149endfunc
150
151func Test_f_args()
152 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
153
154 TestFArgs
155 call assert_equal([], g:args)
156
157 TestFArgs one two three
158 call assert_equal(['one', 'two', 'three'], g:args)
159
160 TestFArgs one\\two three
161 call assert_equal(['one\two', 'three'], g:args)
162
163 TestFArgs one\ two three
164 call assert_equal(['one two', 'three'], g:args)
165
166 TestFArgs one\"two three
167 call assert_equal(['one\"two', 'three'], g:args)
168
169 delcommand TestFArgs
170endfunc
171
172func Test_q_args()
173 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
174
175 TestQArgs
176 call assert_equal([''], g:args)
177
178 TestQArgs one two three
179 call assert_equal(['one two three'], g:args)
180
181 TestQArgs one\\two three
182 call assert_equal(['one\\two three'], g:args)
183
184 TestQArgs one\ two three
185 call assert_equal(['one\ two three'], g:args)
186
187 TestQArgs one\"two three
188 call assert_equal(['one\"two three'], g:args)
189
190 delcommand TestQArgs
191endfunc
192
193func Test_reg_arg()
194 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
195
196 TestRegArg
197 call assert_equal(['', ''], g:args)
198
199 TestRegArg x
200 call assert_equal(['x', 'x'], g:args)
201
202 delcommand TestRegArg
203endfunc
204
205func Test_no_arg()
206 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
207
208 TestNoArg
209 call assert_equal(['', '<>', '<x>', '<'], g:args)
210
211 TestNoArg one
212 call assert_equal(['one', '<>', '<x>', '<'], g:args)
213
214 delcommand TestNoArg
215endfunc
216
217func Test_range_arg()
218 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
219 new
220 call setline(1, range(100))
221 let lnum = line('.')
222
223 TestRangeArg
224 call assert_equal([0, lnum, lnum], g:args)
225
226 99TestRangeArg
227 call assert_equal([1, 99, 99], g:args)
228
229 88,99TestRangeArg
230 call assert_equal([2, 88, 99], g:args)
231
232 call assert_fails('102TestRangeArg', 'E16:')
233
234 bwipe!
235 delcommand TestRangeArg
236endfunc
237
Bram Moolenaareac784e2016-07-28 22:08:24 +0200238func Test_Ambiguous()
239 command Doit let g:didit = 'yes'
240 command Dothat let g:didthat = 'also'
241 call assert_fails('Do', 'E464:')
242 Doit
243 call assert_equal('yes', g:didit)
244 Dothat
245 call assert_equal('also', g:didthat)
246 unlet g:didit
247 unlet g:didthat
248
249 delcommand Doit
250 Do
251 call assert_equal('also', g:didthat)
252 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200253
254 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200255endfunc
256
Bram Moolenaar55d46912018-12-08 16:03:28 +0100257func Test_redefine_on_reload()
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100258 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100259 call assert_equal(0, exists(':ExistingCommand'))
260 source Xcommandexists
261 call assert_equal(2, exists(':ExistingCommand'))
262 " Redefining a command when reloading a script is OK.
263 source Xcommandexists
264 call assert_equal(2, exists(':ExistingCommand'))
265
266 " But redefining in another script is not OK.
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100267 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100268 call assert_fails('source Xcommandexists2', 'E174:')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100269
270 " And defining twice in one script is not OK.
271 delcommand ExistingCommand
272 call assert_equal(0, exists(':ExistingCommand'))
273 call writefile([
274 \ 'command ExistingCommand echo "yes"',
275 \ 'command ExistingCommand echo "no"',
276 \ ], 'Xcommandexists')
277 call assert_fails('source Xcommandexists', 'E174:')
278 call assert_equal(2, exists(':ExistingCommand'))
279
Bram Moolenaar55d46912018-12-08 16:03:28 +0100280 delcommand ExistingCommand
281endfunc
282
Bram Moolenaareac784e2016-07-28 22:08:24 +0200283func Test_CmdUndefined()
284 call assert_fails('Doit', 'E492:')
285 au CmdUndefined Doit :command Doit let g:didit = 'yes'
286 Doit
287 call assert_equal('yes', g:didit)
288 delcommand Doit
289
290 call assert_fails('Dothat', 'E492:')
291 au CmdUndefined * let g:didnot = 'yes'
292 call assert_fails('Dothat', 'E492:')
293 call assert_equal('yes', g:didnot)
294endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100295
296func Test_CmdErrors()
297 call assert_fails('com! docmd :', 'E183:')
298 call assert_fails('com! \<Tab> :', 'E182:')
299 call assert_fails('com! _ :', 'E182:')
300 call assert_fails('com! X :', 'E841:')
301 call assert_fails('com! - DoCmd :', 'E175:')
302 call assert_fails('com! -xxx DoCmd :', 'E181:')
303 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200304 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100305 call assert_fails('com! -complete DoCmd :', 'E179:')
306 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
307 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
308 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
309 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
310 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
311 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
312 call assert_fails('com! -count=x DoCmd :', 'E178:')
313 call assert_fails('com! -range=x DoCmd :', 'E178:')
314
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200315 com! -complete=file DoCmd :
316 call assert_match('E1208:', v:warningmsg)
317 let v:warningmsg = ''
318 com! -nargs=0 -complete=file DoCmd :
319 call assert_match('E1208:', v:warningmsg)
320
321 let lines =<< trim END
322 vim9script
323 com! -complete=file DoCmd :
324 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000325 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200326
327 let lines =<< trim END
328 vim9script
329 com! -nargs=0 -complete=file DoCmd :
330 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000331 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200332
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100333 com! -nargs=0 DoCmd :
334 call assert_fails('DoCmd x', 'E488:')
335
336 com! -nargs=1 DoCmd :
337 call assert_fails('DoCmd', 'E471:')
338
339 com! -nargs=+ DoCmd :
340 call assert_fails('DoCmd', 'E471:')
341
342 call assert_fails('com DoCmd :', 'E174:')
343 comclear
344 call assert_fails('delcom DoCmd', 'E184:')
345endfunc
346
347func CustomComplete(A, L, P)
348 return "January\nFebruary\nMars\n"
349endfunc
350
351func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000352 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100353endfunc
354
355func Test_CmdCompletion()
356 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000357 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100358
359 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000360 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100361
362 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
363 call assert_equal('"com -nargs=* + 0 1 ?', @:)
364
365 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100366 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100367
368 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
369 call assert_equal('"com -complete=color command compiler', @:)
370
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000371 " try completion for unsupported argument values
372 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
373 call assert_equal("\"com -newarg=\t", @:)
374
375 " command completion after the name in a user defined command
376 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
377 call assert_equal("\"com MyCmd chistory", @:)
378
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000379 " delete the Check commands to avoid them showing up
380 call feedkeys(":com Check\<C-A>\<C-B>\"\<CR>", 'tx')
381 let cmds = substitute(@:, '"com ', '', '')->split()
382 for cmd in cmds
383 exe 'delcommand ' .. cmd
384 endfor
385 delcommand MissingFeature
386
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100387 command! DoCmd1 :
388 command! DoCmd2 :
389 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
390 call assert_equal('"com DoCmd1 DoCmd2', @:)
391
392 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
393 call assert_equal('"DoCmd1 DoCmd2', @:)
394
395 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
396 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
397
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000398 " try argument completion for a command without completion
399 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
400 call assert_equal("\"DoCmd1 \t", @:)
401
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100402 delcom DoCmd1
403 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
404 call assert_equal('"delcom DoCmd2', @:)
405
406 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
407 call assert_equal('"com DoCmd2', @:)
408
409 delcom DoCmd2
410 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
411 call assert_equal('"delcom DoC', @:)
412
413 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
414 call assert_equal('"com DoC', @:)
415
Martin Tournoijde69a732021-07-11 14:28:25 +0200416 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100417 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
418 call assert_equal('"DoCmd mswin xterm', @:)
419
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000420 " Test for file name completion
421 com! -nargs=1 -complete=file DoCmd :
422 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
423 call assert_equal('"DoCmd README.txt', @:)
424
425 " Test for buffer name completion
426 com! -nargs=1 -complete=buffer DoCmd :
427 let bnum = bufadd('BufForUserCmd')
428 call setbufvar(bnum, '&buflisted', 1)
429 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
430 call assert_equal('"DoCmd BufForUserCmd', @:)
431 bwipe BufForUserCmd
432 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
433 call assert_equal('"DoCmd BufFor', @:)
434
Martin Tournoijde69a732021-07-11 14:28:25 +0200435 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100436 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
437 call assert_equal('"DoCmd January February Mars', @:)
438
Martin Tournoijde69a732021-07-11 14:28:25 +0200439 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100440 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
441 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
442
Martin Tournoijde69a732021-07-11 14:28:25 +0200443 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100444 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
445
Martin Tournoijde69a732021-07-11 14:28:25 +0200446 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100447 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100448
449 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200450 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100451 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
452
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200453 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200454 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200455 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
456
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000457 " custom completion for a pattern with a backslash
458 let g:ArgLead = ''
459 func! CustCompl(A, L, P)
460 let g:ArgLead = a:A
461 return ['one', 'two', 'three']
462 endfunc
463 com! -nargs=? -complete=customlist,CustCompl DoCmd
464 call feedkeys(":DoCmd a\\\t", 'xt')
465 call assert_equal('a\', g:ArgLead)
466 delfunc CustCompl
467
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100468 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100469endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100470
471func CallExecute(A, L, P)
472 " Drop first '\n'
473 return execute('echo "hi"')[1:]
474endfunc
475
476func Test_use_execute_in_completion()
477 command! -nargs=* -complete=custom,CallExecute DoExec :
478 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
479 call assert_equal('"DoExec hi', @:)
480 delcommand DoExec
481endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100482
483func Test_addr_all()
484 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
485 %DoSomething
486 call assert_equal(1, g:a1)
487 call assert_equal(line('$'), g:a2)
488
489 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
490 args one two three
491 %DoSomething
492 call assert_equal(1, g:a1)
493 call assert_equal(3, g:a2)
494
495 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
496 %DoSomething
497 for low in range(1, bufnr('$'))
498 if buflisted(low)
499 break
500 endif
501 endfor
502 call assert_equal(low, g:a1)
503 call assert_equal(bufnr('$'), g:a2)
504
505 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
506 %DoSomething
507 for low in range(1, bufnr('$'))
508 if bufloaded(low)
509 break
510 endif
511 endfor
512 call assert_equal(low, g:a1)
513 for up in range(bufnr('$'), 1, -1)
514 if bufloaded(up)
515 break
516 endif
517 endfor
518 call assert_equal(up, g:a2)
519
520 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
521 new
522 %DoSomething
523 call assert_equal(1, g:a1)
524 call assert_equal(winnr('$'), g:a2)
525 bwipe
526
527 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
528 tabnew
529 %DoSomething
530 call assert_equal(1, g:a1)
531 call assert_equal(len(gettabinfo()), g:a2)
532 bwipe
533
Bram Moolenaarb7316892019-05-01 18:08:42 +0200534 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100535 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200536 call assert_equal(line('.'), g:a1)
537 call assert_equal(line('.'), g:a2)
538 %DoSomething
539 call assert_equal(1, g:a1)
540 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100541
542 delcommand DoSomething
543endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200544
545func Test_command_list()
546 command! DoCmd :
547 call assert_equal("\n Name Args Address Complete Definition"
548 \ .. "\n DoCmd 0 :",
549 \ execute('command DoCmd'))
550
551 " Test with various -range= and -count= argument values.
552 command! -range DoCmd :
553 call assert_equal("\n Name Args Address Complete Definition"
554 \ .. "\n DoCmd 0 . :",
555 \ execute('command DoCmd'))
556 command! -range=% DoCmd :
557 call assert_equal("\n Name Args Address Complete Definition"
558 \ .. "\n DoCmd 0 % :",
559 \ execute('command! DoCmd'))
560 command! -range=2 DoCmd :
561 call assert_equal("\n Name Args Address Complete Definition"
562 \ .. "\n DoCmd 0 2 :",
563 \ execute('command DoCmd'))
564 command! -count=2 DoCmd :
565 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200566 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200567 \ execute('command DoCmd'))
568
569 " Test with various -addr= argument values.
570 command! -addr=lines DoCmd :
571 call assert_equal("\n Name Args Address Complete Definition"
572 \ .. "\n DoCmd 0 . :",
573 \ execute('command DoCmd'))
574 command! -addr=arguments DoCmd :
575 call assert_equal("\n Name Args Address Complete Definition"
576 \ .. "\n DoCmd 0 . arg :",
577 \ execute('command DoCmd'))
578 command! -addr=buffers DoCmd :
579 call assert_equal("\n Name Args Address Complete Definition"
580 \ .. "\n DoCmd 0 . buf :",
581 \ execute('command DoCmd'))
582 command! -addr=loaded_buffers DoCmd :
583 call assert_equal("\n Name Args Address Complete Definition"
584 \ .. "\n DoCmd 0 . load :",
585 \ execute('command DoCmd'))
586 command! -addr=windows DoCmd :
587 call assert_equal("\n Name Args Address Complete Definition"
588 \ .. "\n DoCmd 0 . win :",
589 \ execute('command DoCmd'))
590 command! -addr=tabs DoCmd :
591 call assert_equal("\n Name Args Address Complete Definition"
592 \ .. "\n DoCmd 0 . tab :",
593 \ execute('command DoCmd'))
594 command! -addr=other DoCmd :
595 call assert_equal("\n Name Args Address Complete Definition"
596 \ .. "\n DoCmd 0 . ? :",
597 \ execute('command DoCmd'))
598
599 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200600 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200601 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200602 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200603 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200604 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200605 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200606 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200607 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200608 command! -nargs=? -complete=custom,CustomComplete 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 ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200611 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200612 command! -nargs=+ -complete=customlist,CustomComplete 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 + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200615 \ execute('command DoCmd'))
616
617 " Test with various -narg= argument values.
618 command! -nargs=0 DoCmd :
619 call assert_equal("\n Name Args Address Complete Definition"
620 \ .. "\n DoCmd 0 :",
621 \ execute('command DoCmd'))
622 command! -nargs=1 DoCmd :
623 call assert_equal("\n Name Args Address Complete Definition"
624 \ .. "\n DoCmd 1 :",
625 \ execute('command DoCmd'))
626 command! -nargs=* DoCmd :
627 call assert_equal("\n Name Args Address Complete Definition"
628 \ .. "\n DoCmd * :",
629 \ execute('command DoCmd'))
630 command! -nargs=? DoCmd :
631 call assert_equal("\n Name Args Address Complete Definition"
632 \ .. "\n DoCmd ? :",
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
639 " Test with other arguments.
640 command! -bang DoCmd :
641 call assert_equal("\n Name Args Address Complete Definition"
642 \ .. "\n! DoCmd 0 :",
643 \ execute('command DoCmd'))
644 command! -bar DoCmd :
645 call assert_equal("\n Name Args Address Complete Definition"
646 \ .. "\n| DoCmd 0 :",
647 \ execute('command DoCmd'))
648 command! -register DoCmd :
649 call assert_equal("\n Name Args Address Complete Definition"
650 \ .. "\n\" DoCmd 0 :",
651 \ execute('command DoCmd'))
652 command! -buffer DoCmd :
653 call assert_equal("\n Name Args Address Complete Definition"
654 \ .. "\nb DoCmd 0 :"
655 \ .. "\n\" DoCmd 0 :",
656 \ execute('command DoCmd'))
657 comclear
658
659 " Test with many args.
660 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
661 call assert_equal("\n Name Args Address Complete Definition"
662 \ .. "\n!\"b|DoCmd + 3c win environment :",
663 \ execute('command DoCmd'))
664 comclear
665
666 " Test with special characters in command definition.
667 command! DoCmd :<cr><tab><c-d>
668 call assert_equal("\n Name Args Address Complete Definition"
669 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
670 \ execute('command DoCmd'))
671
672 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000673 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200674 call assert_match("^\n"
675 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000676 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200677 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
678 \ execute('verbose command DoCmd'))
679
680 comclear
681 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
682 call assert_equal("\nNo user-defined commands found", execute('command'))
683endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100684
Bram Moolenaar8b633132020-03-20 18:20:51 +0100685" Test for a custom user completion returning the wrong value type
686func Test_usercmd_custom()
687 func T1(a, c, p)
688 return "a\nb\n"
689 endfunc
690 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaar55e93662022-09-10 13:52:26 +0100691 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 +0200692 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100693 delcommand TCmd1
694 delfunc T1
695
696 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200697 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100698 endfunc
699 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaar55e93662022-09-10 13:52:26 +0100700 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 +0200701 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100702 delcommand TCmd2
703 delfunc T2
704endfunc
705
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200706func Test_usercmd_with_block()
707 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200708 g:didit = 'yes' # comment
709 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200710 g:didmore = 'more'
711 }
712 DoSomething
713 call assert_equal('yes', g:didit)
714 call assert_equal('more', g:didmore)
715 unlet g:didit
716 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200717 delcommand DoSomething
718
719 command DoMap {
720 echo [1, 2, 3]->map((_, v) => v + 1)
721 }
722 DoMap
723 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200724
725 let lines =<< trim END
726 command DoesNotEnd {
727 echo 'hello'
728 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000729 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000730 delcommand DoesNotEnd
Bram Moolenaar63b91732021-08-05 20:40:03 +0200731
732 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000733 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200734 echo 'hello' | echo 'there'
735 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000736 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200737 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000738 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000739 delcommand HelloThere
740
741 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100742 command EchoCond {
743 const test: string = true
744 ? 'true'
745 : 'false'
746 g:result = test
747 }
748 EchoCond
749 END
750 call v9.CheckScriptSuccess(lines)
751 call assert_equal('true', g:result)
Bram Moolenaara13e7ac2022-05-06 21:24:31 +0100752 unlet g:result
753
754 call feedkeys(":EchoCond\<CR>", 'xt')
755 call assert_equal('true', g:result)
756
Bram Moolenaare442d592022-05-05 12:20:28 +0100757 delcommand EchoCond
758 unlet g:result
759
760 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000761 command BadCommand {
762 echo {
763 'key': 'value',
764 }
765 }
766 BadCommand
767 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000768 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000769 delcommand BadCommand
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200770endfunc
771
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200772func Test_delcommand_buffer()
773 command Global echo 'global'
774 command -buffer OneBuffer echo 'one'
775 new
776 command -buffer TwoBuffer echo 'two'
777 call assert_equal(0, exists(':OneBuffer'))
778 call assert_equal(2, exists(':Global'))
779 call assert_equal(2, exists(':TwoBuffer'))
780 delcommand -buffer TwoBuffer
781 call assert_equal(0, exists(':TwoBuffer'))
782 call assert_fails('delcommand -buffer Global', 'E1237:')
783 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
784 bwipe!
785 call assert_equal(2, exists(':OneBuffer'))
786 delcommand -buffer OneBuffer
787 call assert_equal(0, exists(':OneBuffer'))
788 call assert_fails('delcommand -buffer Global', 'E1237:')
789 delcommand Global
790 call assert_equal(0, exists(':Global'))
791endfunc
792
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000793def Test_count_with_quotes()
794 command -count GetCount g:nr = <count>
795 execute("GetCount 1'2")
796 assert_equal(12, g:nr)
797 execute("GetCount 1'234'567")
798 assert_equal(1'234'567, g:nr)
799
800 execute("GetCount 1'234'567'890'123'456'789'012")
801 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
802
803 # TODO: test with negative number once this is supported
804
805 assert_fails("GetCount '12", "E488:")
806 assert_fails("GetCount 12'", "E488:")
807 assert_fails("GetCount 1''2", "E488:")
808
809 assert_fails(":1'2GetCount", 'E492:')
810 new
811 setline(1, 'text')
812 normal ma
813 execute(":1, 'aprint")
814 bwipe!
815
816 unlet g:nr
817 delcommand GetCount
818enddef
819
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000820func DefCmd(name)
821 if len(a:name) > 30
822 return
823 endif
824 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
825 echo a:name
826 exe a:name
827endfunc
828
829func Test_recursive_define()
830 call DefCmd('Command')
831
832 let name = 'Command'
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000833 while len(name) <= 30
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000834 exe 'delcommand ' .. name
835 let name ..= 'x'
836 endwhile
837endfunc
838
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000839" Test for using buffer-local ambiguous user-defined commands
840func Test_buflocal_ambiguous_usercmd()
841 new
842 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
843 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
844
845 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
846 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
847 call assert_equal('"TestCmd ', @:)
848
849 delcommand TestCmd1
850 delcommand TestCmd2
851 bw!
852endfunc
853
854" Test for using a multibyte character in a user command
855func Test_multibyte_in_usercmd()
856 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
857 new
858 call setline(1, "Hello\u3002")
859 SubJapanesePeriodToDot
860 call assert_equal('Hello.', getline(1))
861 bw!
862 delcommand SubJapanesePeriodToDot
863endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000864
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000865" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
866" legacy script.
867func Test_block_declaration_legacy_script()
868 let lines =<< trim END
869 command -range Rename {
870 var save = @a
871 @a = 'something'
872 g:someExpr = @a
873 @a = save
874 }
875 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100876 call writefile(lines, 'Xlegacy', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000877 source Xlegacy
878
879 let lines =<< trim END
880 let @a = 'saved'
881 Rename
882 call assert_equal('something', g:someExpr)
883 call assert_equal('saved', @a)
884
885 let g:someExpr = 'xxx'
886 let @a = 'also'
887 Rename
888 call assert_equal('something', g:someExpr)
889 call assert_equal('also', @a)
890 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100891 call writefile(lines, 'Xother', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000892 source Xother
893
894 unlet g:someExpr
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000895 delcommand Rename
896endfunc
897
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000898func Test_comclear_while_listing()
899 call CheckRunVimInTerminal()
900
901 let lines =<< trim END
902 set nocompatible
903 comclear
904 for i in range(1, 999)
905 exe 'command ' .. 'Foo' .. i .. ' bar'
906 endfor
907 au CmdlineLeave : call timer_start(0, {-> execute('comclear')})
908 END
909 call writefile(lines, 'Xcommandclear', 'D')
910 let buf = RunVimInTerminal('-S Xcommandclear', {'rows': 10})
911
912 " this was using freed memory
913 call term_sendkeys(buf, ":command\<CR>")
914 call TermWait(buf, 50)
915 call term_sendkeys(buf, "j")
916 call TermWait(buf, 50)
917 call term_sendkeys(buf, "G")
918 call term_sendkeys(buf, "\<CR>")
919
920 call StopVimInTerminal(buf)
921endfunc
922
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000923
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100924" vim: shiftwidth=2 sts=2 expandtab