blob: fe08b9585c02ba4c7ca5710a70095db2e0102b59 [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
John Marriott9e795852024-08-20 20:57:23 +0200136 kee keep keepm keepma keepmar keepmarks keepa keepalt keepj keepjumps
137 \ keepp keeppatterns MyCmd
138 call assert_equal('keepalt keepjumps keepmarks keeppatterns', g:mods)
139
Bram Moolenaar63a60de2016-06-04 22:08:55 +0200140 let g:mods = ''
141 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
142
143 vertical MyQCmd
144 call assert_equal('"vertical" ', g:mods)
145
146 delcommand MyCmd
147 delcommand MyQCmd
148 unlet g:mods
149endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200150
Bram Moolenaare61e5482019-04-27 15:05:12 +0200151func SaveCmdArgs(...)
152 let g:args = a:000
153endfunc
154
155func Test_f_args()
156 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
157
158 TestFArgs
159 call assert_equal([], g:args)
160
161 TestFArgs one two three
162 call assert_equal(['one', 'two', 'three'], g:args)
163
164 TestFArgs one\\two three
165 call assert_equal(['one\two', 'three'], g:args)
166
167 TestFArgs one\ two three
168 call assert_equal(['one two', 'three'], g:args)
169
170 TestFArgs one\"two three
171 call assert_equal(['one\"two', 'three'], g:args)
172
173 delcommand TestFArgs
174endfunc
175
176func Test_q_args()
177 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
178
179 TestQArgs
180 call assert_equal([''], g:args)
181
182 TestQArgs one two three
183 call assert_equal(['one two three'], g:args)
184
185 TestQArgs one\\two three
186 call assert_equal(['one\\two three'], g:args)
187
188 TestQArgs one\ two three
189 call assert_equal(['one\ two three'], g:args)
190
191 TestQArgs one\"two three
192 call assert_equal(['one\"two three'], g:args)
193
194 delcommand TestQArgs
195endfunc
196
197func Test_reg_arg()
198 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
199
200 TestRegArg
201 call assert_equal(['', ''], g:args)
202
203 TestRegArg x
204 call assert_equal(['x', 'x'], g:args)
205
206 delcommand TestRegArg
207endfunc
208
209func Test_no_arg()
210 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
211
212 TestNoArg
213 call assert_equal(['', '<>', '<x>', '<'], g:args)
214
215 TestNoArg one
216 call assert_equal(['one', '<>', '<x>', '<'], g:args)
217
218 delcommand TestNoArg
219endfunc
220
221func Test_range_arg()
222 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
223 new
224 call setline(1, range(100))
225 let lnum = line('.')
226
227 TestRangeArg
228 call assert_equal([0, lnum, lnum], g:args)
229
230 99TestRangeArg
231 call assert_equal([1, 99, 99], g:args)
232
233 88,99TestRangeArg
234 call assert_equal([2, 88, 99], g:args)
235
236 call assert_fails('102TestRangeArg', 'E16:')
237
238 bwipe!
239 delcommand TestRangeArg
240endfunc
241
Bram Moolenaareac784e2016-07-28 22:08:24 +0200242func Test_Ambiguous()
243 command Doit let g:didit = 'yes'
244 command Dothat let g:didthat = 'also'
245 call assert_fails('Do', 'E464:')
246 Doit
247 call assert_equal('yes', g:didit)
248 Dothat
249 call assert_equal('also', g:didthat)
250 unlet g:didit
251 unlet g:didthat
252
253 delcommand Doit
254 Do
255 call assert_equal('also', g:didthat)
256 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200257
258 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200259endfunc
260
Bram Moolenaar55d46912018-12-08 16:03:28 +0100261func Test_redefine_on_reload()
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100262 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100263 call assert_equal(0, exists(':ExistingCommand'))
264 source Xcommandexists
265 call assert_equal(2, exists(':ExistingCommand'))
266 " Redefining a command when reloading a script is OK.
267 source Xcommandexists
268 call assert_equal(2, exists(':ExistingCommand'))
269
270 " But redefining in another script is not OK.
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100271 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2', 'D')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100272 call assert_fails('source Xcommandexists2', 'E174:')
Bram Moolenaar55d46912018-12-08 16:03:28 +0100273
274 " And defining twice in one script is not OK.
275 delcommand ExistingCommand
276 call assert_equal(0, exists(':ExistingCommand'))
277 call writefile([
278 \ 'command ExistingCommand echo "yes"',
279 \ 'command ExistingCommand echo "no"',
280 \ ], 'Xcommandexists')
281 call assert_fails('source Xcommandexists', 'E174:')
282 call assert_equal(2, exists(':ExistingCommand'))
283
Bram Moolenaar55d46912018-12-08 16:03:28 +0100284 delcommand ExistingCommand
285endfunc
286
Bram Moolenaareac784e2016-07-28 22:08:24 +0200287func Test_CmdUndefined()
288 call assert_fails('Doit', 'E492:')
289 au CmdUndefined Doit :command Doit let g:didit = 'yes'
290 Doit
291 call assert_equal('yes', g:didit)
292 delcommand Doit
293
294 call assert_fails('Dothat', 'E492:')
295 au CmdUndefined * let g:didnot = 'yes'
296 call assert_fails('Dothat', 'E492:')
297 call assert_equal('yes', g:didnot)
298endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100299
300func Test_CmdErrors()
301 call assert_fails('com! docmd :', 'E183:')
302 call assert_fails('com! \<Tab> :', 'E182:')
303 call assert_fails('com! _ :', 'E182:')
304 call assert_fails('com! X :', 'E841:')
305 call assert_fails('com! - DoCmd :', 'E175:')
306 call assert_fails('com! -xxx DoCmd :', 'E181:')
307 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200308 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100309 call assert_fails('com! -complete DoCmd :', 'E179:')
310 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
311 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
312 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
313 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
314 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
315 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
316 call assert_fails('com! -count=x DoCmd :', 'E178:')
317 call assert_fails('com! -range=x DoCmd :', 'E178:')
318
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200319 com! -complete=file DoCmd :
320 call assert_match('E1208:', v:warningmsg)
321 let v:warningmsg = ''
322 com! -nargs=0 -complete=file DoCmd :
323 call assert_match('E1208:', v:warningmsg)
324
325 let lines =<< trim END
326 vim9script
327 com! -complete=file DoCmd :
328 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000329 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200330
331 let lines =<< trim END
332 vim9script
333 com! -nargs=0 -complete=file DoCmd :
334 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000335 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200336
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100337 com! -nargs=0 DoCmd :
338 call assert_fails('DoCmd x', 'E488:')
339
340 com! -nargs=1 DoCmd :
341 call assert_fails('DoCmd', 'E471:')
342
343 com! -nargs=+ DoCmd :
344 call assert_fails('DoCmd', 'E471:')
345
346 call assert_fails('com DoCmd :', 'E174:')
347 comclear
348 call assert_fails('delcom DoCmd', 'E184:')
zeertzjq33e54302022-12-19 16:49:27 +0000349
350 " These used to leak memory
351 call assert_fails('com! -complete=custom,CustomComplete _ :', 'E182:')
352 call assert_fails('com! -complete=custom,CustomComplete docmd :', 'E183:')
353 call assert_fails('com! -complete=custom,CustomComplete -xxx DoCmd :', 'E181:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100354endfunc
355
356func CustomComplete(A, L, P)
357 return "January\nFebruary\nMars\n"
358endfunc
359
360func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000361 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100362endfunc
363
364func Test_CmdCompletion()
365 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000366 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100367
368 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000369 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100370
371 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
372 call assert_equal('"com -nargs=* + 0 1 ?', @:)
373
374 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100375 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100376
377 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
378 call assert_equal('"com -complete=color command compiler', @:)
379
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000380 " try completion for unsupported argument values
381 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
382 call assert_equal("\"com -newarg=\t", @:)
383
384 " command completion after the name in a user defined command
385 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
386 call assert_equal("\"com MyCmd chistory", @:)
387
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000388 " delete the Check commands to avoid them showing up
389 call feedkeys(":com Check\<C-A>\<C-B>\"\<CR>", 'tx')
390 let cmds = substitute(@:, '"com ', '', '')->split()
391 for cmd in cmds
392 exe 'delcommand ' .. cmd
393 endfor
394 delcommand MissingFeature
395
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100396 command! DoCmd1 :
397 command! DoCmd2 :
398 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
399 call assert_equal('"com DoCmd1 DoCmd2', @:)
400
401 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
402 call assert_equal('"DoCmd1 DoCmd2', @:)
403
404 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
405 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
406
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000407 " try argument completion for a command without completion
408 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
409 call assert_equal("\"DoCmd1 \t", @:)
410
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100411 delcom DoCmd1
412 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
413 call assert_equal('"delcom DoCmd2', @:)
414
415 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
416 call assert_equal('"com DoCmd2', @:)
417
418 delcom DoCmd2
419 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
420 call assert_equal('"delcom DoC', @:)
421
422 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
423 call assert_equal('"com DoC', @:)
424
Martin Tournoijde69a732021-07-11 14:28:25 +0200425 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100426 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
427 call assert_equal('"DoCmd mswin xterm', @:)
428
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000429 " Test for file name completion
430 com! -nargs=1 -complete=file DoCmd :
431 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
432 call assert_equal('"DoCmd README.txt', @:)
433
434 " Test for buffer name completion
435 com! -nargs=1 -complete=buffer DoCmd :
436 let bnum = bufadd('BufForUserCmd')
437 call setbufvar(bnum, '&buflisted', 1)
438 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
439 call assert_equal('"DoCmd BufForUserCmd', @:)
440 bwipe BufForUserCmd
441 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
442 call assert_equal('"DoCmd BufFor', @:)
443
Martin Tournoijde69a732021-07-11 14:28:25 +0200444 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100445 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
446 call assert_equal('"DoCmd January February Mars', @:)
447
Martin Tournoijde69a732021-07-11 14:28:25 +0200448 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100449 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
450 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
451
Martin Tournoijde69a732021-07-11 14:28:25 +0200452 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100453 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
454
Martin Tournoijde69a732021-07-11 14:28:25 +0200455 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100456 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100457
458 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200459 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100460 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
461
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200462 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200463 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200464 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
465
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000466 " custom completion for a pattern with a backslash
467 let g:ArgLead = ''
468 func! CustCompl(A, L, P)
469 let g:ArgLead = a:A
470 return ['one', 'two', 'three']
471 endfunc
472 com! -nargs=? -complete=customlist,CustCompl DoCmd
473 call feedkeys(":DoCmd a\\\t", 'xt')
474 call assert_equal('a\', g:ArgLead)
475 delfunc CustCompl
476
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100477 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100478endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100479
480func CallExecute(A, L, P)
481 " Drop first '\n'
482 return execute('echo "hi"')[1:]
483endfunc
484
485func Test_use_execute_in_completion()
486 command! -nargs=* -complete=custom,CallExecute DoExec :
487 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
488 call assert_equal('"DoExec hi', @:)
489 delcommand DoExec
490endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100491
492func Test_addr_all()
493 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
494 %DoSomething
495 call assert_equal(1, g:a1)
496 call assert_equal(line('$'), g:a2)
497
498 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
499 args one two three
500 %DoSomething
501 call assert_equal(1, g:a1)
502 call assert_equal(3, g:a2)
503
504 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
505 %DoSomething
506 for low in range(1, bufnr('$'))
507 if buflisted(low)
508 break
509 endif
510 endfor
511 call assert_equal(low, g:a1)
512 call assert_equal(bufnr('$'), g:a2)
513
514 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
515 %DoSomething
516 for low in range(1, bufnr('$'))
517 if bufloaded(low)
518 break
519 endif
520 endfor
521 call assert_equal(low, g:a1)
522 for up in range(bufnr('$'), 1, -1)
523 if bufloaded(up)
524 break
525 endif
526 endfor
527 call assert_equal(up, g:a2)
528
529 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
530 new
531 %DoSomething
532 call assert_equal(1, g:a1)
533 call assert_equal(winnr('$'), g:a2)
534 bwipe
535
536 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
537 tabnew
538 %DoSomething
539 call assert_equal(1, g:a1)
540 call assert_equal(len(gettabinfo()), g:a2)
541 bwipe
542
Bram Moolenaarb7316892019-05-01 18:08:42 +0200543 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100544 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200545 call assert_equal(line('.'), g:a1)
546 call assert_equal(line('.'), g:a2)
547 %DoSomething
548 call assert_equal(1, g:a1)
549 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100550
551 delcommand DoSomething
552endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200553
554func Test_command_list()
555 command! DoCmd :
556 call assert_equal("\n Name Args Address Complete Definition"
557 \ .. "\n DoCmd 0 :",
558 \ execute('command DoCmd'))
559
560 " Test with various -range= and -count= argument values.
561 command! -range DoCmd :
562 call assert_equal("\n Name Args Address Complete Definition"
563 \ .. "\n DoCmd 0 . :",
564 \ execute('command DoCmd'))
565 command! -range=% DoCmd :
566 call assert_equal("\n Name Args Address Complete Definition"
567 \ .. "\n DoCmd 0 % :",
568 \ execute('command! DoCmd'))
569 command! -range=2 DoCmd :
570 call assert_equal("\n Name Args Address Complete Definition"
571 \ .. "\n DoCmd 0 2 :",
572 \ execute('command DoCmd'))
573 command! -count=2 DoCmd :
574 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200575 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200576 \ execute('command DoCmd'))
577
578 " Test with various -addr= argument values.
579 command! -addr=lines DoCmd :
580 call assert_equal("\n Name Args Address Complete Definition"
581 \ .. "\n DoCmd 0 . :",
582 \ execute('command DoCmd'))
583 command! -addr=arguments DoCmd :
584 call assert_equal("\n Name Args Address Complete Definition"
585 \ .. "\n DoCmd 0 . arg :",
586 \ execute('command DoCmd'))
587 command! -addr=buffers DoCmd :
588 call assert_equal("\n Name Args Address Complete Definition"
589 \ .. "\n DoCmd 0 . buf :",
590 \ execute('command DoCmd'))
591 command! -addr=loaded_buffers DoCmd :
592 call assert_equal("\n Name Args Address Complete Definition"
593 \ .. "\n DoCmd 0 . load :",
594 \ execute('command DoCmd'))
595 command! -addr=windows DoCmd :
596 call assert_equal("\n Name Args Address Complete Definition"
597 \ .. "\n DoCmd 0 . win :",
598 \ execute('command DoCmd'))
599 command! -addr=tabs DoCmd :
600 call assert_equal("\n Name Args Address Complete Definition"
601 \ .. "\n DoCmd 0 . tab :",
602 \ execute('command DoCmd'))
603 command! -addr=other DoCmd :
604 call assert_equal("\n Name Args Address Complete Definition"
605 \ .. "\n DoCmd 0 . ? :",
606 \ execute('command DoCmd'))
607
608 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200609 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200610 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200611 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200612 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200613 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200614 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200615 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200616 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200617 command! -nargs=? -complete=custom,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200618 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200619 \ .. "\n DoCmd ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200620 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200621 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200622 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200623 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200624 \ execute('command DoCmd'))
625
626 " Test with various -narg= argument values.
627 command! -nargs=0 DoCmd :
628 call assert_equal("\n Name Args Address Complete Definition"
629 \ .. "\n DoCmd 0 :",
630 \ execute('command DoCmd'))
631 command! -nargs=1 DoCmd :
632 call assert_equal("\n Name Args Address Complete Definition"
633 \ .. "\n DoCmd 1 :",
634 \ execute('command DoCmd'))
635 command! -nargs=* DoCmd :
636 call assert_equal("\n Name Args Address Complete Definition"
637 \ .. "\n DoCmd * :",
638 \ execute('command DoCmd'))
639 command! -nargs=? DoCmd :
640 call assert_equal("\n Name Args Address Complete Definition"
641 \ .. "\n DoCmd ? :",
642 \ execute('command DoCmd'))
643 command! -nargs=+ DoCmd :
644 call assert_equal("\n Name Args Address Complete Definition"
645 \ .. "\n DoCmd + :",
646 \ execute('command DoCmd'))
647
648 " Test with other arguments.
649 command! -bang DoCmd :
650 call assert_equal("\n Name Args Address Complete Definition"
651 \ .. "\n! DoCmd 0 :",
652 \ execute('command DoCmd'))
653 command! -bar DoCmd :
654 call assert_equal("\n Name Args Address Complete Definition"
655 \ .. "\n| DoCmd 0 :",
656 \ execute('command DoCmd'))
657 command! -register DoCmd :
658 call assert_equal("\n Name Args Address Complete Definition"
659 \ .. "\n\" DoCmd 0 :",
660 \ execute('command DoCmd'))
661 command! -buffer DoCmd :
662 call assert_equal("\n Name Args Address Complete Definition"
663 \ .. "\nb DoCmd 0 :"
664 \ .. "\n\" DoCmd 0 :",
665 \ execute('command DoCmd'))
666 comclear
667
668 " Test with many args.
669 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
670 call assert_equal("\n Name Args Address Complete Definition"
671 \ .. "\n!\"b|DoCmd + 3c win environment :",
672 \ execute('command DoCmd'))
673 comclear
674
675 " Test with special characters in command definition.
676 command! DoCmd :<cr><tab><c-d>
677 call assert_equal("\n Name Args Address Complete Definition"
678 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
679 \ execute('command DoCmd'))
680
681 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000682 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200683 call assert_match("^\n"
684 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000685 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200686 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
687 \ execute('verbose command DoCmd'))
688
689 comclear
690 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
691 call assert_equal("\nNo user-defined commands found", execute('command'))
692endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100693
Bram Moolenaar8b633132020-03-20 18:20:51 +0100694" Test for a custom user completion returning the wrong value type
695func Test_usercmd_custom()
696 func T1(a, c, p)
697 return "a\nb\n"
698 endfunc
699 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaar55e93662022-09-10 13:52:26 +0100700 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 +0200701 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100702 delcommand TCmd1
703 delfunc T1
704
705 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200706 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100707 endfunc
708 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaar55e93662022-09-10 13:52:26 +0100709 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 +0200710 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100711 delcommand TCmd2
712 delfunc T2
713endfunc
714
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200715func Test_usercmd_with_block()
716 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200717 g:didit = 'yes' # comment
718 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200719 g:didmore = 'more'
720 }
721 DoSomething
722 call assert_equal('yes', g:didit)
723 call assert_equal('more', g:didmore)
724 unlet g:didit
725 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200726 delcommand DoSomething
727
728 command DoMap {
729 echo [1, 2, 3]->map((_, v) => v + 1)
730 }
731 DoMap
732 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200733
734 let lines =<< trim END
735 command DoesNotEnd {
736 echo 'hello'
737 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000738 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000739 delcommand DoesNotEnd
Bram Moolenaar63b91732021-08-05 20:40:03 +0200740
741 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000742 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200743 echo 'hello' | echo 'there'
744 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000745 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200746 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000747 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000748 delcommand HelloThere
749
750 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100751 command EchoCond {
752 const test: string = true
753 ? 'true'
754 : 'false'
755 g:result = test
756 }
757 EchoCond
758 END
759 call v9.CheckScriptSuccess(lines)
760 call assert_equal('true', g:result)
Bram Moolenaara13e7ac2022-05-06 21:24:31 +0100761 unlet g:result
762
763 call feedkeys(":EchoCond\<CR>", 'xt')
764 call assert_equal('true', g:result)
765
Bram Moolenaare442d592022-05-05 12:20:28 +0100766 delcommand EchoCond
767 unlet g:result
768
769 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000770 command BadCommand {
771 echo {
772 'key': 'value',
773 }
774 }
775 BadCommand
776 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000777 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000778 delcommand BadCommand
Christian Brabandt00cb2472023-09-05 20:46:25 +0200779
780 let lines =<< trim END
781 vim9script
782 command Cmd {
783 g:result = [1,
784 2]
785 }
786 Cmd
787 END
788 call v9.CheckScriptSuccess(lines)
789 call assert_equal([1, 2], g:result)
790 delcommand Cmd
791 unlet! g:result
792
793 let lines =<< trim END
794 vim9script
795 command Cmd {
796 g:result = and(0x80,
797 0x80)
798 }
799 Cmd
800 END
801 call v9.CheckScriptSuccess(lines)
802 call assert_equal(128, g:result)
803 delcommand Cmd
804 unlet! g:result
805
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200806endfunc
807
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200808func Test_delcommand_buffer()
809 command Global echo 'global'
810 command -buffer OneBuffer echo 'one'
811 new
812 command -buffer TwoBuffer echo 'two'
813 call assert_equal(0, exists(':OneBuffer'))
814 call assert_equal(2, exists(':Global'))
815 call assert_equal(2, exists(':TwoBuffer'))
816 delcommand -buffer TwoBuffer
817 call assert_equal(0, exists(':TwoBuffer'))
818 call assert_fails('delcommand -buffer Global', 'E1237:')
819 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
820 bwipe!
821 call assert_equal(2, exists(':OneBuffer'))
822 delcommand -buffer OneBuffer
823 call assert_equal(0, exists(':OneBuffer'))
824 call assert_fails('delcommand -buffer Global', 'E1237:')
825 delcommand Global
826 call assert_equal(0, exists(':Global'))
827endfunc
828
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000829def Test_count_with_quotes()
830 command -count GetCount g:nr = <count>
831 execute("GetCount 1'2")
832 assert_equal(12, g:nr)
833 execute("GetCount 1'234'567")
834 assert_equal(1'234'567, g:nr)
835
836 execute("GetCount 1'234'567'890'123'456'789'012")
837 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
838
839 # TODO: test with negative number once this is supported
840
841 assert_fails("GetCount '12", "E488:")
842 assert_fails("GetCount 12'", "E488:")
843 assert_fails("GetCount 1''2", "E488:")
844
845 assert_fails(":1'2GetCount", 'E492:')
846 new
847 setline(1, 'text')
848 normal ma
849 execute(":1, 'aprint")
850 bwipe!
851
852 unlet g:nr
853 delcommand GetCount
854enddef
855
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000856func DefCmd(name)
857 if len(a:name) > 30
858 return
859 endif
860 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
861 echo a:name
862 exe a:name
863endfunc
864
865func Test_recursive_define()
866 call DefCmd('Command')
867
868 let name = 'Command'
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000869 while len(name) <= 30
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000870 exe 'delcommand ' .. name
871 let name ..= 'x'
872 endwhile
873endfunc
874
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000875" Test for using buffer-local ambiguous user-defined commands
876func Test_buflocal_ambiguous_usercmd()
877 new
878 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
879 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
880
881 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
882 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
883 call assert_equal('"TestCmd ', @:)
884
885 delcommand TestCmd1
886 delcommand TestCmd2
887 bw!
888endfunc
889
zeertzjqb444ee72023-02-20 15:25:13 +0000890" Test for using buffer-local user command from cmdwin.
891func Test_buflocal_usercmd_cmdwin()
892 new
893 command -buffer TestCmd edit Test
894 " This used to crash Vim
895 call assert_fails("norm q::TestCmd\<CR>", 'E11:')
896 bw!
897endfunc
898
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000899" Test for using a multibyte character in a user command
900func Test_multibyte_in_usercmd()
901 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
902 new
903 call setline(1, "Hello\u3002")
904 SubJapanesePeriodToDot
905 call assert_equal('Hello.', getline(1))
906 bw!
907 delcommand SubJapanesePeriodToDot
908endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000909
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000910" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
911" legacy script.
912func Test_block_declaration_legacy_script()
913 let lines =<< trim END
914 command -range Rename {
915 var save = @a
916 @a = 'something'
917 g:someExpr = @a
918 @a = save
919 }
920 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100921 call writefile(lines, 'Xlegacy', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000922 source Xlegacy
923
924 let lines =<< trim END
925 let @a = 'saved'
926 Rename
927 call assert_equal('something', g:someExpr)
928 call assert_equal('saved', @a)
929
930 let g:someExpr = 'xxx'
931 let @a = 'also'
932 Rename
933 call assert_equal('something', g:someExpr)
934 call assert_equal('also', @a)
935 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100936 call writefile(lines, 'Xother', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000937 source Xother
938
939 unlet g:someExpr
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000940 delcommand Rename
941endfunc
942
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000943func Test_comclear_while_listing()
944 call CheckRunVimInTerminal()
945
946 let lines =<< trim END
947 set nocompatible
948 comclear
949 for i in range(1, 999)
950 exe 'command ' .. 'Foo' .. i .. ' bar'
951 endfor
952 au CmdlineLeave : call timer_start(0, {-> execute('comclear')})
953 END
954 call writefile(lines, 'Xcommandclear', 'D')
955 let buf = RunVimInTerminal('-S Xcommandclear', {'rows': 10})
956
957 " this was using freed memory
958 call term_sendkeys(buf, ":command\<CR>")
959 call TermWait(buf, 50)
960 call term_sendkeys(buf, "j")
961 call TermWait(buf, 50)
962 call term_sendkeys(buf, "G")
963 call term_sendkeys(buf, "\<CR>")
964
965 call StopVimInTerminal(buf)
966endfunc
967
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000968
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100969" vim: shiftwidth=2 sts=2 expandtab