blob: 1e1856727a78ca145a381b4691df95daacfc9806 [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:')
zeertzjq33e54302022-12-19 16:49:27 +0000345
346 " These used to leak memory
347 call assert_fails('com! -complete=custom,CustomComplete _ :', 'E182:')
348 call assert_fails('com! -complete=custom,CustomComplete docmd :', 'E183:')
349 call assert_fails('com! -complete=custom,CustomComplete -xxx DoCmd :', 'E181:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100350endfunc
351
352func CustomComplete(A, L, P)
353 return "January\nFebruary\nMars\n"
354endfunc
355
356func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000357 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100358endfunc
359
360func Test_CmdCompletion()
361 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000362 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100363
364 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000365 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100366
367 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
368 call assert_equal('"com -nargs=* + 0 1 ?', @:)
369
370 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100371 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100372
373 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
374 call assert_equal('"com -complete=color command compiler', @:)
375
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000376 " try completion for unsupported argument values
377 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
378 call assert_equal("\"com -newarg=\t", @:)
379
380 " command completion after the name in a user defined command
381 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
382 call assert_equal("\"com MyCmd chistory", @:)
383
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000384 " delete the Check commands to avoid them showing up
385 call feedkeys(":com Check\<C-A>\<C-B>\"\<CR>", 'tx')
386 let cmds = substitute(@:, '"com ', '', '')->split()
387 for cmd in cmds
388 exe 'delcommand ' .. cmd
389 endfor
390 delcommand MissingFeature
391
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100392 command! DoCmd1 :
393 command! DoCmd2 :
394 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
395 call assert_equal('"com DoCmd1 DoCmd2', @:)
396
397 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
398 call assert_equal('"DoCmd1 DoCmd2', @:)
399
400 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
401 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
402
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000403 " try argument completion for a command without completion
404 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
405 call assert_equal("\"DoCmd1 \t", @:)
406
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100407 delcom DoCmd1
408 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
409 call assert_equal('"delcom DoCmd2', @:)
410
411 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
412 call assert_equal('"com DoCmd2', @:)
413
414 delcom DoCmd2
415 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
416 call assert_equal('"delcom DoC', @:)
417
418 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
419 call assert_equal('"com DoC', @:)
420
Martin Tournoijde69a732021-07-11 14:28:25 +0200421 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100422 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
423 call assert_equal('"DoCmd mswin xterm', @:)
424
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000425 " Test for file name completion
426 com! -nargs=1 -complete=file DoCmd :
427 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
428 call assert_equal('"DoCmd README.txt', @:)
429
430 " Test for buffer name completion
431 com! -nargs=1 -complete=buffer DoCmd :
432 let bnum = bufadd('BufForUserCmd')
433 call setbufvar(bnum, '&buflisted', 1)
434 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
435 call assert_equal('"DoCmd BufForUserCmd', @:)
436 bwipe BufForUserCmd
437 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
438 call assert_equal('"DoCmd BufFor', @:)
439
Martin Tournoijde69a732021-07-11 14:28:25 +0200440 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100441 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
442 call assert_equal('"DoCmd January February Mars', @:)
443
Martin Tournoijde69a732021-07-11 14:28:25 +0200444 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100445 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
446 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
447
Martin Tournoijde69a732021-07-11 14:28:25 +0200448 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100449 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
450
Martin Tournoijde69a732021-07-11 14:28:25 +0200451 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100452 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100453
454 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200455 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100456 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
457
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200458 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200459 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200460 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
461
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000462 " custom completion for a pattern with a backslash
463 let g:ArgLead = ''
464 func! CustCompl(A, L, P)
465 let g:ArgLead = a:A
466 return ['one', 'two', 'three']
467 endfunc
468 com! -nargs=? -complete=customlist,CustCompl DoCmd
469 call feedkeys(":DoCmd a\\\t", 'xt')
470 call assert_equal('a\', g:ArgLead)
471 delfunc CustCompl
472
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100473 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100474endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100475
476func CallExecute(A, L, P)
477 " Drop first '\n'
478 return execute('echo "hi"')[1:]
479endfunc
480
481func Test_use_execute_in_completion()
482 command! -nargs=* -complete=custom,CallExecute DoExec :
483 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
484 call assert_equal('"DoExec hi', @:)
485 delcommand DoExec
486endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100487
488func Test_addr_all()
489 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
490 %DoSomething
491 call assert_equal(1, g:a1)
492 call assert_equal(line('$'), g:a2)
493
494 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
495 args one two three
496 %DoSomething
497 call assert_equal(1, g:a1)
498 call assert_equal(3, g:a2)
499
500 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
501 %DoSomething
502 for low in range(1, bufnr('$'))
503 if buflisted(low)
504 break
505 endif
506 endfor
507 call assert_equal(low, g:a1)
508 call assert_equal(bufnr('$'), g:a2)
509
510 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
511 %DoSomething
512 for low in range(1, bufnr('$'))
513 if bufloaded(low)
514 break
515 endif
516 endfor
517 call assert_equal(low, g:a1)
518 for up in range(bufnr('$'), 1, -1)
519 if bufloaded(up)
520 break
521 endif
522 endfor
523 call assert_equal(up, g:a2)
524
525 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
526 new
527 %DoSomething
528 call assert_equal(1, g:a1)
529 call assert_equal(winnr('$'), g:a2)
530 bwipe
531
532 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
533 tabnew
534 %DoSomething
535 call assert_equal(1, g:a1)
536 call assert_equal(len(gettabinfo()), g:a2)
537 bwipe
538
Bram Moolenaarb7316892019-05-01 18:08:42 +0200539 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100540 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200541 call assert_equal(line('.'), g:a1)
542 call assert_equal(line('.'), g:a2)
543 %DoSomething
544 call assert_equal(1, g:a1)
545 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100546
547 delcommand DoSomething
548endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200549
550func Test_command_list()
551 command! DoCmd :
552 call assert_equal("\n Name Args Address Complete Definition"
553 \ .. "\n DoCmd 0 :",
554 \ execute('command DoCmd'))
555
556 " Test with various -range= and -count= argument values.
557 command! -range DoCmd :
558 call assert_equal("\n Name Args Address Complete Definition"
559 \ .. "\n DoCmd 0 . :",
560 \ execute('command DoCmd'))
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=2 DoCmd :
566 call assert_equal("\n Name Args Address Complete Definition"
567 \ .. "\n DoCmd 0 2 :",
568 \ execute('command DoCmd'))
569 command! -count=2 DoCmd :
570 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200571 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200572 \ execute('command DoCmd'))
573
574 " Test with various -addr= argument values.
575 command! -addr=lines DoCmd :
576 call assert_equal("\n Name Args Address Complete Definition"
577 \ .. "\n DoCmd 0 . :",
578 \ execute('command DoCmd'))
579 command! -addr=arguments DoCmd :
580 call assert_equal("\n Name Args Address Complete Definition"
581 \ .. "\n DoCmd 0 . arg :",
582 \ execute('command DoCmd'))
583 command! -addr=buffers DoCmd :
584 call assert_equal("\n Name Args Address Complete Definition"
585 \ .. "\n DoCmd 0 . buf :",
586 \ execute('command DoCmd'))
587 command! -addr=loaded_buffers DoCmd :
588 call assert_equal("\n Name Args Address Complete Definition"
589 \ .. "\n DoCmd 0 . load :",
590 \ execute('command DoCmd'))
591 command! -addr=windows DoCmd :
592 call assert_equal("\n Name Args Address Complete Definition"
593 \ .. "\n DoCmd 0 . win :",
594 \ execute('command DoCmd'))
595 command! -addr=tabs DoCmd :
596 call assert_equal("\n Name Args Address Complete Definition"
597 \ .. "\n DoCmd 0 . tab :",
598 \ execute('command DoCmd'))
599 command! -addr=other DoCmd :
600 call assert_equal("\n Name Args Address Complete Definition"
601 \ .. "\n DoCmd 0 . ? :",
602 \ execute('command DoCmd'))
603
604 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200605 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200606 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200607 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200608 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200609 command! -nargs=* -complete=augroup 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 * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200612 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200613 command! -nargs=? -complete=custom,CustomComplete 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 ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200616 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200617 command! -nargs=+ -complete=customlist,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 + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200620 \ execute('command DoCmd'))
621
622 " Test with various -narg= argument values.
623 command! -nargs=0 DoCmd :
624 call assert_equal("\n Name Args Address Complete Definition"
625 \ .. "\n DoCmd 0 :",
626 \ execute('command DoCmd'))
627 command! -nargs=1 DoCmd :
628 call assert_equal("\n Name Args Address Complete Definition"
629 \ .. "\n DoCmd 1 :",
630 \ execute('command DoCmd'))
631 command! -nargs=* DoCmd :
632 call assert_equal("\n Name Args Address Complete Definition"
633 \ .. "\n DoCmd * :",
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
644 " Test with other arguments.
645 command! -bang DoCmd :
646 call assert_equal("\n Name Args Address Complete Definition"
647 \ .. "\n! DoCmd 0 :",
648 \ execute('command DoCmd'))
649 command! -bar DoCmd :
650 call assert_equal("\n Name Args Address Complete Definition"
651 \ .. "\n| DoCmd 0 :",
652 \ execute('command DoCmd'))
653 command! -register DoCmd :
654 call assert_equal("\n Name Args Address Complete Definition"
655 \ .. "\n\" DoCmd 0 :",
656 \ execute('command DoCmd'))
657 command! -buffer DoCmd :
658 call assert_equal("\n Name Args Address Complete Definition"
659 \ .. "\nb DoCmd 0 :"
660 \ .. "\n\" DoCmd 0 :",
661 \ execute('command DoCmd'))
662 comclear
663
664 " Test with many args.
665 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
666 call assert_equal("\n Name Args Address Complete Definition"
667 \ .. "\n!\"b|DoCmd + 3c win environment :",
668 \ execute('command DoCmd'))
669 comclear
670
671 " Test with special characters in command definition.
672 command! DoCmd :<cr><tab><c-d>
673 call assert_equal("\n Name Args Address Complete Definition"
674 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
675 \ execute('command DoCmd'))
676
677 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000678 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200679 call assert_match("^\n"
680 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000681 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200682 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
683 \ execute('verbose command DoCmd'))
684
685 comclear
686 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
687 call assert_equal("\nNo user-defined commands found", execute('command'))
688endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100689
Bram Moolenaar8b633132020-03-20 18:20:51 +0100690" Test for a custom user completion returning the wrong value type
691func Test_usercmd_custom()
692 func T1(a, c, p)
693 return "a\nb\n"
694 endfunc
695 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaar55e93662022-09-10 13:52:26 +0100696 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 +0200697 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100698 delcommand TCmd1
699 delfunc T1
700
701 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200702 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100703 endfunc
704 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaar55e93662022-09-10 13:52:26 +0100705 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 +0200706 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100707 delcommand TCmd2
708 delfunc T2
709endfunc
710
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200711func Test_usercmd_with_block()
712 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200713 g:didit = 'yes' # comment
714 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200715 g:didmore = 'more'
716 }
717 DoSomething
718 call assert_equal('yes', g:didit)
719 call assert_equal('more', g:didmore)
720 unlet g:didit
721 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200722 delcommand DoSomething
723
724 command DoMap {
725 echo [1, 2, 3]->map((_, v) => v + 1)
726 }
727 DoMap
728 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200729
730 let lines =<< trim END
731 command DoesNotEnd {
732 echo 'hello'
733 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000734 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000735 delcommand DoesNotEnd
Bram Moolenaar63b91732021-08-05 20:40:03 +0200736
737 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000738 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200739 echo 'hello' | echo 'there'
740 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000741 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200742 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000743 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000744 delcommand HelloThere
745
746 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100747 command EchoCond {
748 const test: string = true
749 ? 'true'
750 : 'false'
751 g:result = test
752 }
753 EchoCond
754 END
755 call v9.CheckScriptSuccess(lines)
756 call assert_equal('true', g:result)
Bram Moolenaara13e7ac2022-05-06 21:24:31 +0100757 unlet g:result
758
759 call feedkeys(":EchoCond\<CR>", 'xt')
760 call assert_equal('true', g:result)
761
Bram Moolenaare442d592022-05-05 12:20:28 +0100762 delcommand EchoCond
763 unlet g:result
764
765 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000766 command BadCommand {
767 echo {
768 'key': 'value',
769 }
770 }
771 BadCommand
772 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000773 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000774 delcommand BadCommand
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200775endfunc
776
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200777func Test_delcommand_buffer()
778 command Global echo 'global'
779 command -buffer OneBuffer echo 'one'
780 new
781 command -buffer TwoBuffer echo 'two'
782 call assert_equal(0, exists(':OneBuffer'))
783 call assert_equal(2, exists(':Global'))
784 call assert_equal(2, exists(':TwoBuffer'))
785 delcommand -buffer TwoBuffer
786 call assert_equal(0, exists(':TwoBuffer'))
787 call assert_fails('delcommand -buffer Global', 'E1237:')
788 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
789 bwipe!
790 call assert_equal(2, exists(':OneBuffer'))
791 delcommand -buffer OneBuffer
792 call assert_equal(0, exists(':OneBuffer'))
793 call assert_fails('delcommand -buffer Global', 'E1237:')
794 delcommand Global
795 call assert_equal(0, exists(':Global'))
796endfunc
797
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000798def Test_count_with_quotes()
799 command -count GetCount g:nr = <count>
800 execute("GetCount 1'2")
801 assert_equal(12, g:nr)
802 execute("GetCount 1'234'567")
803 assert_equal(1'234'567, g:nr)
804
805 execute("GetCount 1'234'567'890'123'456'789'012")
806 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
807
808 # TODO: test with negative number once this is supported
809
810 assert_fails("GetCount '12", "E488:")
811 assert_fails("GetCount 12'", "E488:")
812 assert_fails("GetCount 1''2", "E488:")
813
814 assert_fails(":1'2GetCount", 'E492:')
815 new
816 setline(1, 'text')
817 normal ma
818 execute(":1, 'aprint")
819 bwipe!
820
821 unlet g:nr
822 delcommand GetCount
823enddef
824
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000825func DefCmd(name)
826 if len(a:name) > 30
827 return
828 endif
829 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
830 echo a:name
831 exe a:name
832endfunc
833
834func Test_recursive_define()
835 call DefCmd('Command')
836
837 let name = 'Command'
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000838 while len(name) <= 30
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000839 exe 'delcommand ' .. name
840 let name ..= 'x'
841 endwhile
842endfunc
843
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000844" Test for using buffer-local ambiguous user-defined commands
845func Test_buflocal_ambiguous_usercmd()
846 new
847 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
848 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
849
850 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
851 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
852 call assert_equal('"TestCmd ', @:)
853
854 delcommand TestCmd1
855 delcommand TestCmd2
856 bw!
857endfunc
858
zeertzjqb444ee72023-02-20 15:25:13 +0000859" Test for using buffer-local user command from cmdwin.
860func Test_buflocal_usercmd_cmdwin()
861 new
862 command -buffer TestCmd edit Test
863 " This used to crash Vim
864 call assert_fails("norm q::TestCmd\<CR>", 'E11:')
865 bw!
866endfunc
867
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000868" Test for using a multibyte character in a user command
869func Test_multibyte_in_usercmd()
870 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
871 new
872 call setline(1, "Hello\u3002")
873 SubJapanesePeriodToDot
874 call assert_equal('Hello.', getline(1))
875 bw!
876 delcommand SubJapanesePeriodToDot
877endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000878
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000879" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
880" legacy script.
881func Test_block_declaration_legacy_script()
882 let lines =<< trim END
883 command -range Rename {
884 var save = @a
885 @a = 'something'
886 g:someExpr = @a
887 @a = save
888 }
889 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100890 call writefile(lines, 'Xlegacy', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000891 source Xlegacy
892
893 let lines =<< trim END
894 let @a = 'saved'
895 Rename
896 call assert_equal('something', g:someExpr)
897 call assert_equal('saved', @a)
898
899 let g:someExpr = 'xxx'
900 let @a = 'also'
901 Rename
902 call assert_equal('something', g:someExpr)
903 call assert_equal('also', @a)
904 END
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100905 call writefile(lines, 'Xother', 'D')
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000906 source Xother
907
908 unlet g:someExpr
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000909 delcommand Rename
910endfunc
911
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000912func Test_comclear_while_listing()
913 call CheckRunVimInTerminal()
914
915 let lines =<< trim END
916 set nocompatible
917 comclear
918 for i in range(1, 999)
919 exe 'command ' .. 'Foo' .. i .. ' bar'
920 endfor
921 au CmdlineLeave : call timer_start(0, {-> execute('comclear')})
922 END
923 call writefile(lines, 'Xcommandclear', 'D')
924 let buf = RunVimInTerminal('-S Xcommandclear', {'rows': 10})
925
926 " this was using freed memory
927 call term_sendkeys(buf, ":command\<CR>")
928 call TermWait(buf, 50)
929 call term_sendkeys(buf, "j")
930 call TermWait(buf, 50)
931 call term_sendkeys(buf, "G")
932 call term_sendkeys(buf, "\<CR>")
933
934 call StopVimInTerminal(buf)
935endfunc
936
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000937
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100938" vim: shiftwidth=2 sts=2 expandtab