blob: 8c588c55e3c1a795de5f6e8363aca9969254de36 [file] [log] [blame]
Bram Moolenaar63a60de2016-06-04 22:08:55 +02001" Tests for user defined commands
2
Bram Moolenaar62aec932022-01-29 21:45:34 +00003import './vim9.vim' as v9
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004
Bram Moolenaar63a60de2016-06-04 22:08:55 +02005" Test for <mods> in user defined commands
6function Test_cmdmods()
7 let g:mods = ''
8
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02009 command! -nargs=* MyCmd let g:mods = '<mods>'
Bram Moolenaar63a60de2016-06-04 22:08:55 +020010
11 MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020012 call assert_equal('', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020013 aboveleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020014 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020015 abo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020016 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020017 belowright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020018 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020019 bel MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020020 call assert_equal('belowright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020021 botright MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020022 call assert_equal('botright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020023 bo MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020024 call assert_equal('botright', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020025 browse MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020026 call assert_equal('browse', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020027 bro MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020028 call assert_equal('browse', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020029 confirm MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020030 call assert_equal('confirm', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020031 conf MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020032 call assert_equal('confirm', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020033 hide MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020034 call assert_equal('hide', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020035 hid MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020036 call assert_equal('hide', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020037 keepalt MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020038 call assert_equal('keepalt', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020039 keepa MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020040 call assert_equal('keepalt', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020041 keepjumps MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020042 call assert_equal('keepjumps', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020043 keepj MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020044 call assert_equal('keepjumps', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020045 keepmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020046 call assert_equal('keepmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020047 kee MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020048 call assert_equal('keepmarks', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020049 keeppatterns MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020050 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020051 keepp MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020052 call assert_equal('keeppatterns', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020053 leftabove MyCmd " results in :aboveleft
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020054 call assert_equal('aboveleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020055 lefta MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020056 call assert_equal('aboveleft', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020057 lockmarks MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020058 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020059 loc MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020060 call assert_equal('lockmarks', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020061 " noautocmd MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020062 noswapfile MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020063 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020064 nos MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020065 call assert_equal('noswapfile', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020066 rightbelow MyCmd " results in :belowright
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020067 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020068 rightb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020069 call assert_equal('belowright', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020070 " sandbox MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020071 silent MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020072 call assert_equal('silent', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020073 sil MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020074 call assert_equal('silent', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020075 tab MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020076 call assert_equal('tab', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020077 topleft MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020078 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020079 to MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020080 call assert_equal('topleft', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020081 " unsilent MyCmd
Bram Moolenaar63a60de2016-06-04 22:08:55 +020082 verbose MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020083 call assert_equal('verbose', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020084 verb MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020085 call assert_equal('verbose', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020086 vertical MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020087 call assert_equal('vertical', g:mods)
Bram Moolenaar3bcfca32016-07-30 19:39:29 +020088 vert MyCmd
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020089 call assert_equal('vertical', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020090
91 aboveleft belowright botright browse confirm hide keepalt keepjumps
92 \ keepmarks keeppatterns lockmarks noswapfile silent tab
93 \ topleft verbose vertical MyCmd
94
Bram Moolenaar7a1637f2020-04-13 21:16:21 +020095 call assert_equal('browse confirm hide keepalt keepjumps ' .
96 \ 'keepmarks keeppatterns lockmarks noswapfile silent ' .
97 \ 'verbose aboveleft belowright botright tab topleft vertical', g:mods)
Bram Moolenaar63a60de2016-06-04 22:08:55 +020098
99 let g:mods = ''
100 command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
101
102 vertical MyQCmd
103 call assert_equal('"vertical" ', g:mods)
104
105 delcommand MyCmd
106 delcommand MyQCmd
107 unlet g:mods
108endfunction
Bram Moolenaareac784e2016-07-28 22:08:24 +0200109
Bram Moolenaare61e5482019-04-27 15:05:12 +0200110func SaveCmdArgs(...)
111 let g:args = a:000
112endfunc
113
114func Test_f_args()
115 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
116
117 TestFArgs
118 call assert_equal([], g:args)
119
120 TestFArgs one two three
121 call assert_equal(['one', 'two', 'three'], g:args)
122
123 TestFArgs one\\two three
124 call assert_equal(['one\two', 'three'], g:args)
125
126 TestFArgs one\ two three
127 call assert_equal(['one two', 'three'], g:args)
128
129 TestFArgs one\"two three
130 call assert_equal(['one\"two', 'three'], g:args)
131
132 delcommand TestFArgs
133endfunc
134
135func Test_q_args()
136 command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
137
138 TestQArgs
139 call assert_equal([''], g:args)
140
141 TestQArgs one two three
142 call assert_equal(['one two three'], g:args)
143
144 TestQArgs one\\two three
145 call assert_equal(['one\\two three'], g:args)
146
147 TestQArgs one\ two three
148 call assert_equal(['one\ two three'], g:args)
149
150 TestQArgs one\"two three
151 call assert_equal(['one\"two three'], g:args)
152
153 delcommand TestQArgs
154endfunc
155
156func Test_reg_arg()
157 command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
158
159 TestRegArg
160 call assert_equal(['', ''], g:args)
161
162 TestRegArg x
163 call assert_equal(['x', 'x'], g:args)
164
165 delcommand TestRegArg
166endfunc
167
168func Test_no_arg()
169 command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
170
171 TestNoArg
172 call assert_equal(['', '<>', '<x>', '<'], g:args)
173
174 TestNoArg one
175 call assert_equal(['one', '<>', '<x>', '<'], g:args)
176
177 delcommand TestNoArg
178endfunc
179
180func Test_range_arg()
181 command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
182 new
183 call setline(1, range(100))
184 let lnum = line('.')
185
186 TestRangeArg
187 call assert_equal([0, lnum, lnum], g:args)
188
189 99TestRangeArg
190 call assert_equal([1, 99, 99], g:args)
191
192 88,99TestRangeArg
193 call assert_equal([2, 88, 99], g:args)
194
195 call assert_fails('102TestRangeArg', 'E16:')
196
197 bwipe!
198 delcommand TestRangeArg
199endfunc
200
Bram Moolenaareac784e2016-07-28 22:08:24 +0200201func Test_Ambiguous()
202 command Doit let g:didit = 'yes'
203 command Dothat let g:didthat = 'also'
204 call assert_fails('Do', 'E464:')
205 Doit
206 call assert_equal('yes', g:didit)
207 Dothat
208 call assert_equal('also', g:didthat)
209 unlet g:didit
210 unlet g:didthat
211
212 delcommand Doit
213 Do
214 call assert_equal('also', g:didthat)
215 delcommand Dothat
Bram Moolenaare61e5482019-04-27 15:05:12 +0200216
217 call assert_fails("\x4ei\041", ' you demand a ')
Bram Moolenaareac784e2016-07-28 22:08:24 +0200218endfunc
219
Bram Moolenaar55d46912018-12-08 16:03:28 +0100220func Test_redefine_on_reload()
221 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
222 call assert_equal(0, exists(':ExistingCommand'))
223 source Xcommandexists
224 call assert_equal(2, exists(':ExistingCommand'))
225 " Redefining a command when reloading a script is OK.
226 source Xcommandexists
227 call assert_equal(2, exists(':ExistingCommand'))
228
229 " But redefining in another script is not OK.
230 call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
231 call assert_fails('source Xcommandexists2', 'E174:')
232 call delete('Xcommandexists2')
233
234 " And defining twice in one script is not OK.
235 delcommand ExistingCommand
236 call assert_equal(0, exists(':ExistingCommand'))
237 call writefile([
238 \ 'command ExistingCommand echo "yes"',
239 \ 'command ExistingCommand echo "no"',
240 \ ], 'Xcommandexists')
241 call assert_fails('source Xcommandexists', 'E174:')
242 call assert_equal(2, exists(':ExistingCommand'))
243
244 call delete('Xcommandexists')
245 delcommand ExistingCommand
246endfunc
247
Bram Moolenaareac784e2016-07-28 22:08:24 +0200248func Test_CmdUndefined()
249 call assert_fails('Doit', 'E492:')
250 au CmdUndefined Doit :command Doit let g:didit = 'yes'
251 Doit
252 call assert_equal('yes', g:didit)
253 delcommand Doit
254
255 call assert_fails('Dothat', 'E492:')
256 au CmdUndefined * let g:didnot = 'yes'
257 call assert_fails('Dothat', 'E492:')
258 call assert_equal('yes', g:didnot)
259endfunc
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100260
261func Test_CmdErrors()
262 call assert_fails('com! docmd :', 'E183:')
263 call assert_fails('com! \<Tab> :', 'E182:')
264 call assert_fails('com! _ :', 'E182:')
265 call assert_fails('com! X :', 'E841:')
266 call assert_fails('com! - DoCmd :', 'E175:')
267 call assert_fails('com! -xxx DoCmd :', 'E181:')
268 call assert_fails('com! -addr DoCmd :', 'E179:')
Bram Moolenaare61e5482019-04-27 15:05:12 +0200269 call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100270 call assert_fails('com! -complete DoCmd :', 'E179:')
271 call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
272 call assert_fails('com! -complete=custom DoCmd :', 'E467:')
273 call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
274 call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
275 call assert_fails('com! -nargs=x DoCmd :', 'E176:')
276 call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
277 call assert_fails('com! -count=x DoCmd :', 'E178:')
278 call assert_fails('com! -range=x DoCmd :', 'E178:')
279
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200280 com! -complete=file DoCmd :
281 call assert_match('E1208:', v:warningmsg)
282 let v:warningmsg = ''
283 com! -nargs=0 -complete=file DoCmd :
284 call assert_match('E1208:', v:warningmsg)
285
286 let lines =<< trim END
287 vim9script
288 com! -complete=file DoCmd :
289 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000290 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200291
292 let lines =<< trim END
293 vim9script
294 com! -nargs=0 -complete=file DoCmd :
295 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000296 call v9.CheckScriptFailure(lines, 'E1208', 2)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +0200297
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100298 com! -nargs=0 DoCmd :
299 call assert_fails('DoCmd x', 'E488:')
300
301 com! -nargs=1 DoCmd :
302 call assert_fails('DoCmd', 'E471:')
303
304 com! -nargs=+ DoCmd :
305 call assert_fails('DoCmd', 'E471:')
306
307 call assert_fails('com DoCmd :', 'E174:')
308 comclear
309 call assert_fails('delcom DoCmd', 'E184:')
310endfunc
311
312func CustomComplete(A, L, P)
313 return "January\nFebruary\nMars\n"
314endfunc
315
316func CustomCompleteList(A, L, P)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000317 return [ "Monday", "Tuesday", "Wednesday", {}, test_null_string()]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100318endfunc
319
320func Test_CmdCompletion()
321 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000322 call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100323
324 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar326e7da2021-11-12 16:06:03 +0000325 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100326
327 call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
328 call assert_equal('"com -nargs=* + 0 1 ?', @:)
329
330 call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaar51a74542018-12-02 18:21:49 +0100331 call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100332
333 call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
334 call assert_equal('"com -complete=color command compiler', @:)
335
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000336 " try completion for unsupported argument values
337 call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
338 call assert_equal("\"com -newarg=\t", @:)
339
340 " command completion after the name in a user defined command
341 call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
342 call assert_equal("\"com MyCmd chistory", @:)
343
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100344 command! DoCmd1 :
345 command! DoCmd2 :
346 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
347 call assert_equal('"com DoCmd1 DoCmd2', @:)
348
349 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
350 call assert_equal('"DoCmd1 DoCmd2', @:)
351
352 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
353 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
354
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 " try argument completion for a command without completion
356 call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
357 call assert_equal("\"DoCmd1 \t", @:)
358
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100359 delcom DoCmd1
360 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
361 call assert_equal('"delcom DoCmd2', @:)
362
363 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
364 call assert_equal('"com DoCmd2', @:)
365
366 delcom DoCmd2
367 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
368 call assert_equal('"delcom DoC', @:)
369
370 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
371 call assert_equal('"com DoC', @:)
372
Martin Tournoijde69a732021-07-11 14:28:25 +0200373 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100374 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
375 call assert_equal('"DoCmd mswin xterm', @:)
376
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000377 " Test for file name completion
378 com! -nargs=1 -complete=file DoCmd :
379 call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
380 call assert_equal('"DoCmd README.txt', @:)
381
382 " Test for buffer name completion
383 com! -nargs=1 -complete=buffer DoCmd :
384 let bnum = bufadd('BufForUserCmd')
385 call setbufvar(bnum, '&buflisted', 1)
386 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
387 call assert_equal('"DoCmd BufForUserCmd', @:)
388 bwipe BufForUserCmd
389 call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
390 call assert_equal('"DoCmd BufFor', @:)
391
Martin Tournoijde69a732021-07-11 14:28:25 +0200392 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100393 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
394 call assert_equal('"DoCmd January February Mars', @:)
395
Martin Tournoijde69a732021-07-11 14:28:25 +0200396 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100397 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
398 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
399
Martin Tournoijde69a732021-07-11 14:28:25 +0200400 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100401 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
402
Martin Tournoijde69a732021-07-11 14:28:25 +0200403 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100404 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100405
406 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200407 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100408 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
409
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200410 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200411 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200412 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
413
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000414 " custom completion for a pattern with a backslash
415 let g:ArgLead = ''
416 func! CustCompl(A, L, P)
417 let g:ArgLead = a:A
418 return ['one', 'two', 'three']
419 endfunc
420 com! -nargs=? -complete=customlist,CustCompl DoCmd
421 call feedkeys(":DoCmd a\\\t", 'xt')
422 call assert_equal('a\', g:ArgLead)
423 delfunc CustCompl
424
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100425 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100426endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100427
428func CallExecute(A, L, P)
429 " Drop first '\n'
430 return execute('echo "hi"')[1:]
431endfunc
432
433func Test_use_execute_in_completion()
434 command! -nargs=* -complete=custom,CallExecute DoExec :
435 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
436 call assert_equal('"DoExec hi', @:)
437 delcommand DoExec
438endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100439
440func Test_addr_all()
441 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
442 %DoSomething
443 call assert_equal(1, g:a1)
444 call assert_equal(line('$'), g:a2)
445
446 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
447 args one two three
448 %DoSomething
449 call assert_equal(1, g:a1)
450 call assert_equal(3, g:a2)
451
452 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
453 %DoSomething
454 for low in range(1, bufnr('$'))
455 if buflisted(low)
456 break
457 endif
458 endfor
459 call assert_equal(low, g:a1)
460 call assert_equal(bufnr('$'), g:a2)
461
462 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
463 %DoSomething
464 for low in range(1, bufnr('$'))
465 if bufloaded(low)
466 break
467 endif
468 endfor
469 call assert_equal(low, g:a1)
470 for up in range(bufnr('$'), 1, -1)
471 if bufloaded(up)
472 break
473 endif
474 endfor
475 call assert_equal(up, g:a2)
476
477 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
478 new
479 %DoSomething
480 call assert_equal(1, g:a1)
481 call assert_equal(winnr('$'), g:a2)
482 bwipe
483
484 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
485 tabnew
486 %DoSomething
487 call assert_equal(1, g:a1)
488 call assert_equal(len(gettabinfo()), g:a2)
489 bwipe
490
Bram Moolenaarb7316892019-05-01 18:08:42 +0200491 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100492 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200493 call assert_equal(line('.'), g:a1)
494 call assert_equal(line('.'), g:a2)
495 %DoSomething
496 call assert_equal(1, g:a1)
497 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100498
499 delcommand DoSomething
500endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200501
502func Test_command_list()
503 command! DoCmd :
504 call assert_equal("\n Name Args Address Complete Definition"
505 \ .. "\n DoCmd 0 :",
506 \ execute('command DoCmd'))
507
508 " Test with various -range= and -count= argument values.
509 command! -range DoCmd :
510 call assert_equal("\n Name Args Address Complete Definition"
511 \ .. "\n DoCmd 0 . :",
512 \ execute('command DoCmd'))
513 command! -range=% DoCmd :
514 call assert_equal("\n Name Args Address Complete Definition"
515 \ .. "\n DoCmd 0 % :",
516 \ execute('command! DoCmd'))
517 command! -range=2 DoCmd :
518 call assert_equal("\n Name Args Address Complete Definition"
519 \ .. "\n DoCmd 0 2 :",
520 \ execute('command DoCmd'))
521 command! -count=2 DoCmd :
522 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200523 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200524 \ execute('command DoCmd'))
525
526 " Test with various -addr= argument values.
527 command! -addr=lines DoCmd :
528 call assert_equal("\n Name Args Address Complete Definition"
529 \ .. "\n DoCmd 0 . :",
530 \ execute('command DoCmd'))
531 command! -addr=arguments DoCmd :
532 call assert_equal("\n Name Args Address Complete Definition"
533 \ .. "\n DoCmd 0 . arg :",
534 \ execute('command DoCmd'))
535 command! -addr=buffers DoCmd :
536 call assert_equal("\n Name Args Address Complete Definition"
537 \ .. "\n DoCmd 0 . buf :",
538 \ execute('command DoCmd'))
539 command! -addr=loaded_buffers DoCmd :
540 call assert_equal("\n Name Args Address Complete Definition"
541 \ .. "\n DoCmd 0 . load :",
542 \ execute('command DoCmd'))
543 command! -addr=windows DoCmd :
544 call assert_equal("\n Name Args Address Complete Definition"
545 \ .. "\n DoCmd 0 . win :",
546 \ execute('command DoCmd'))
547 command! -addr=tabs DoCmd :
548 call assert_equal("\n Name Args Address Complete Definition"
549 \ .. "\n DoCmd 0 . tab :",
550 \ execute('command DoCmd'))
551 command! -addr=other DoCmd :
552 call assert_equal("\n Name Args Address Complete Definition"
553 \ .. "\n DoCmd 0 . ? :",
554 \ execute('command DoCmd'))
555
556 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200557 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200558 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200559 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200560 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200561 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200562 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200563 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200564 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200565 command! -nargs=? -complete=custom,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200566 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200567 \ .. "\n DoCmd ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200568 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200569 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200570 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200571 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200572 \ execute('command DoCmd'))
573
574 " Test with various -narg= argument values.
575 command! -nargs=0 DoCmd :
576 call assert_equal("\n Name Args Address Complete Definition"
577 \ .. "\n DoCmd 0 :",
578 \ execute('command DoCmd'))
579 command! -nargs=1 DoCmd :
580 call assert_equal("\n Name Args Address Complete Definition"
581 \ .. "\n DoCmd 1 :",
582 \ execute('command DoCmd'))
583 command! -nargs=* DoCmd :
584 call assert_equal("\n Name Args Address Complete Definition"
585 \ .. "\n DoCmd * :",
586 \ execute('command DoCmd'))
587 command! -nargs=? DoCmd :
588 call assert_equal("\n Name Args Address Complete Definition"
589 \ .. "\n DoCmd ? :",
590 \ execute('command DoCmd'))
591 command! -nargs=+ DoCmd :
592 call assert_equal("\n Name Args Address Complete Definition"
593 \ .. "\n DoCmd + :",
594 \ execute('command DoCmd'))
595
596 " Test with other arguments.
597 command! -bang DoCmd :
598 call assert_equal("\n Name Args Address Complete Definition"
599 \ .. "\n! DoCmd 0 :",
600 \ execute('command DoCmd'))
601 command! -bar DoCmd :
602 call assert_equal("\n Name Args Address Complete Definition"
603 \ .. "\n| DoCmd 0 :",
604 \ execute('command DoCmd'))
605 command! -register DoCmd :
606 call assert_equal("\n Name Args Address Complete Definition"
607 \ .. "\n\" DoCmd 0 :",
608 \ execute('command DoCmd'))
609 command! -buffer DoCmd :
610 call assert_equal("\n Name Args Address Complete Definition"
611 \ .. "\nb DoCmd 0 :"
612 \ .. "\n\" DoCmd 0 :",
613 \ execute('command DoCmd'))
614 comclear
615
616 " Test with many args.
617 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
618 call assert_equal("\n Name Args Address Complete Definition"
619 \ .. "\n!\"b|DoCmd + 3c win environment :",
620 \ execute('command DoCmd'))
621 comclear
622
623 " Test with special characters in command definition.
624 command! DoCmd :<cr><tab><c-d>
625 call assert_equal("\n Name Args Address Complete Definition"
626 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
627 \ execute('command DoCmd'))
628
629 " Test output in verbose mode.
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000630 command! -nargs=+ -complete=customlist,SomeFunc DoCmd :ls
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200631 call assert_match("^\n"
632 \ .. " Name Args Address Complete Definition\n"
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000633 \ .. " DoCmd + customlist,SomeFunc :ls\n"
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200634 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
635 \ execute('verbose command DoCmd'))
636
637 comclear
638 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
639 call assert_equal("\nNo user-defined commands found", execute('command'))
640endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100641
Bram Moolenaar8b633132020-03-20 18:20:51 +0100642" Test for a custom user completion returning the wrong value type
643func Test_usercmd_custom()
644 func T1(a, c, p)
645 return "a\nb\n"
646 endfunc
647 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200648 call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
649 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100650 delcommand TCmd1
651 delfunc T1
652
653 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200654 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100655 endfunc
656 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200657 call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
658 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100659 delcommand TCmd2
660 delfunc T2
661endfunc
662
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200663func Test_usercmd_with_block()
664 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200665 g:didit = 'yes' # comment
666 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200667 g:didmore = 'more'
668 }
669 DoSomething
670 call assert_equal('yes', g:didit)
671 call assert_equal('more', g:didmore)
672 unlet g:didit
673 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200674 delcommand DoSomething
675
676 command DoMap {
677 echo [1, 2, 3]->map((_, v) => v + 1)
678 }
679 DoMap
680 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200681
682 let lines =<< trim END
683 command DoesNotEnd {
684 echo 'hello'
685 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000686 call v9.CheckScriptFailure(lines, 'E1026:')
Bram Moolenaar63b91732021-08-05 20:40:03 +0200687
688 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000689 command HelloThere {
Bram Moolenaar63b91732021-08-05 20:40:03 +0200690 echo 'hello' | echo 'there'
691 }
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000692 HelloThere
Bram Moolenaar63b91732021-08-05 20:40:03 +0200693 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000694 call v9.CheckScriptSuccess(lines)
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000695 delcommand HelloThere
696
697 let lines =<< trim END
Bram Moolenaare442d592022-05-05 12:20:28 +0100698 command EchoCond {
699 const test: string = true
700 ? 'true'
701 : 'false'
702 g:result = test
703 }
704 EchoCond
705 END
706 call v9.CheckScriptSuccess(lines)
707 call assert_equal('true', g:result)
708 delcommand EchoCond
709 unlet g:result
710
711 let lines =<< trim END
Bram Moolenaarf87dac02021-12-15 17:53:40 +0000712 command BadCommand {
713 echo {
714 'key': 'value',
715 }
716 }
717 BadCommand
718 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000719 call v9.CheckScriptFailure(lines, 'E1128:')
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200720endfunc
721
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200722func Test_delcommand_buffer()
723 command Global echo 'global'
724 command -buffer OneBuffer echo 'one'
725 new
726 command -buffer TwoBuffer echo 'two'
727 call assert_equal(0, exists(':OneBuffer'))
728 call assert_equal(2, exists(':Global'))
729 call assert_equal(2, exists(':TwoBuffer'))
730 delcommand -buffer TwoBuffer
731 call assert_equal(0, exists(':TwoBuffer'))
732 call assert_fails('delcommand -buffer Global', 'E1237:')
733 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
734 bwipe!
735 call assert_equal(2, exists(':OneBuffer'))
736 delcommand -buffer OneBuffer
737 call assert_equal(0, exists(':OneBuffer'))
738 call assert_fails('delcommand -buffer Global', 'E1237:')
739 delcommand Global
740 call assert_equal(0, exists(':Global'))
741endfunc
742
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000743def Test_count_with_quotes()
744 command -count GetCount g:nr = <count>
745 execute("GetCount 1'2")
746 assert_equal(12, g:nr)
747 execute("GetCount 1'234'567")
748 assert_equal(1'234'567, g:nr)
749
750 execute("GetCount 1'234'567'890'123'456'789'012")
751 assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
752
753 # TODO: test with negative number once this is supported
754
755 assert_fails("GetCount '12", "E488:")
756 assert_fails("GetCount 12'", "E488:")
757 assert_fails("GetCount 1''2", "E488:")
758
759 assert_fails(":1'2GetCount", 'E492:')
760 new
761 setline(1, 'text')
762 normal ma
763 execute(":1, 'aprint")
764 bwipe!
765
766 unlet g:nr
767 delcommand GetCount
768enddef
769
Bram Moolenaar205f29c2021-12-10 21:46:09 +0000770func DefCmd(name)
771 if len(a:name) > 30
772 return
773 endif
774 exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
775 echo a:name
776 exe a:name
777endfunc
778
779func Test_recursive_define()
780 call DefCmd('Command')
781
782 let name = 'Command'
783 while len(name) < 30
784 exe 'delcommand ' .. name
785 let name ..= 'x'
786 endwhile
787endfunc
788
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000789" Test for using buffer-local ambiguous user-defined commands
790func Test_buflocal_ambiguous_usercmd()
791 new
792 command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
793 command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
794
795 call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
796 call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
797 call assert_equal('"TestCmd ', @:)
798
799 delcommand TestCmd1
800 delcommand TestCmd2
801 bw!
802endfunc
803
804" Test for using a multibyte character in a user command
805func Test_multibyte_in_usercmd()
806 command SubJapanesePeriodToDot exe "%s/\u3002/./g"
807 new
808 call setline(1, "Hello\u3002")
809 SubJapanesePeriodToDot
810 call assert_equal('Hello.', getline(1))
811 bw!
812 delcommand SubJapanesePeriodToDot
813endfunc
Bram Moolenaaraf377e32021-11-29 12:12:43 +0000814
Bram Moolenaar98b7fe72022-03-23 21:36:27 +0000815" Declaring a variable in a {} uses Vim9 script rules, even when defined in a
816" legacy script.
817func Test_block_declaration_legacy_script()
818 let lines =<< trim END
819 command -range Rename {
820 var save = @a
821 @a = 'something'
822 g:someExpr = @a
823 @a = save
824 }
825 END
826 call writefile(lines, 'Xlegacy')
827 source Xlegacy
828
829 let lines =<< trim END
830 let @a = 'saved'
831 Rename
832 call assert_equal('something', g:someExpr)
833 call assert_equal('saved', @a)
834
835 let g:someExpr = 'xxx'
836 let @a = 'also'
837 Rename
838 call assert_equal('something', g:someExpr)
839 call assert_equal('also', @a)
840 END
841 call writefile(lines, 'Xother')
842 source Xother
843
844 unlet g:someExpr
845 call delete('Xlegacy')
846 call delete('Xother')
847 delcommand Rename
848endfunc
849
850
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100851" vim: shiftwidth=2 sts=2 expandtab