blob: 48bfd5a2521f066831b86e66cad11f45ecd3260a [file] [log] [blame]
Bram Moolenaar63a60de2016-06-04 22:08:55 +02001" Tests for user defined commands
2
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003source vim9.vim
4
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
290 call CheckScriptFailure(lines, 'E1208', 2)
291
292 let lines =<< trim END
293 vim9script
294 com! -nargs=0 -complete=file DoCmd :
295 END
296 call CheckScriptFailure(lines, 'E1208', 2)
297
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)
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100317 return [ "Monday", "Tuesday", "Wednesday", {}]
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100318endfunc
319
320func Test_CmdCompletion()
321 call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
322 call assert_equal('"com -addr bang bar buffer complete count nargs range register', @:)
323
324 call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
325 call assert_equal('"com -nargs=0 -addr bang bar buffer complete count nargs range register', @:)
326
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
336 command! DoCmd1 :
337 command! DoCmd2 :
338 call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
339 call assert_equal('"com DoCmd1 DoCmd2', @:)
340
341 call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
342 call assert_equal('"DoCmd1 DoCmd2', @:)
343
344 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
345 call assert_equal('"delcom DoCmd1 DoCmd2', @:)
346
347 delcom DoCmd1
348 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
349 call assert_equal('"delcom DoCmd2', @:)
350
351 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
352 call assert_equal('"com DoCmd2', @:)
353
354 delcom DoCmd2
355 call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
356 call assert_equal('"delcom DoC', @:)
357
358 call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
359 call assert_equal('"com DoC', @:)
360
Martin Tournoijde69a732021-07-11 14:28:25 +0200361 com! -nargs=1 -complete=behave DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100362 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
363 call assert_equal('"DoCmd mswin xterm', @:)
364
Martin Tournoijde69a732021-07-11 14:28:25 +0200365 com! -nargs=* -complete=custom,CustomComplete DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100366 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
367 call assert_equal('"DoCmd January February Mars', @:)
368
Martin Tournoijde69a732021-07-11 14:28:25 +0200369 com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100370 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
371 call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
372
Martin Tournoijde69a732021-07-11 14:28:25 +0200373 com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100374 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
375
Martin Tournoijde69a732021-07-11 14:28:25 +0200376 com! -nargs=+ -complete=customlist,CustomComp DoCmd :
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100377 call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100378
379 " custom completion without a function
Martin Tournoijde69a732021-07-11 14:28:25 +0200380 com! -nargs=? -complete=custom, DoCmd
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100381 call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
382
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200383 " custom completion failure with the wrong function
Martin Tournoijde69a732021-07-11 14:28:25 +0200384 com! -nargs=? -complete=custom,min DoCmd
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200385 call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
386
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100387 delcom DoCmd
Bram Moolenaar65c836e2017-01-26 22:07:33 +0100388endfunc
Bram Moolenaar20951482017-12-25 13:44:43 +0100389
390func CallExecute(A, L, P)
391 " Drop first '\n'
392 return execute('echo "hi"')[1:]
393endfunc
394
395func Test_use_execute_in_completion()
396 command! -nargs=* -complete=custom,CallExecute DoExec :
397 call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
398 call assert_equal('"DoExec hi', @:)
399 delcommand DoExec
400endfunc
Bram Moolenaar51a74542018-12-02 18:21:49 +0100401
402func Test_addr_all()
403 command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
404 %DoSomething
405 call assert_equal(1, g:a1)
406 call assert_equal(line('$'), g:a2)
407
408 command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
409 args one two three
410 %DoSomething
411 call assert_equal(1, g:a1)
412 call assert_equal(3, g:a2)
413
414 command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
415 %DoSomething
416 for low in range(1, bufnr('$'))
417 if buflisted(low)
418 break
419 endif
420 endfor
421 call assert_equal(low, g:a1)
422 call assert_equal(bufnr('$'), g:a2)
423
424 command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
425 %DoSomething
426 for low in range(1, bufnr('$'))
427 if bufloaded(low)
428 break
429 endif
430 endfor
431 call assert_equal(low, g:a1)
432 for up in range(bufnr('$'), 1, -1)
433 if bufloaded(up)
434 break
435 endif
436 endfor
437 call assert_equal(up, g:a2)
438
439 command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
440 new
441 %DoSomething
442 call assert_equal(1, g:a1)
443 call assert_equal(winnr('$'), g:a2)
444 bwipe
445
446 command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
447 tabnew
448 %DoSomething
449 call assert_equal(1, g:a1)
450 call assert_equal(len(gettabinfo()), g:a2)
451 bwipe
452
Bram Moolenaarb7316892019-05-01 18:08:42 +0200453 command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
Bram Moolenaar51a74542018-12-02 18:21:49 +0100454 DoSomething
Bram Moolenaarb7316892019-05-01 18:08:42 +0200455 call assert_equal(line('.'), g:a1)
456 call assert_equal(line('.'), g:a2)
457 %DoSomething
458 call assert_equal(1, g:a1)
459 call assert_equal(line('$'), g:a2)
Bram Moolenaar51a74542018-12-02 18:21:49 +0100460
461 delcommand DoSomething
462endfunc
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200463
464func Test_command_list()
465 command! DoCmd :
466 call assert_equal("\n Name Args Address Complete Definition"
467 \ .. "\n DoCmd 0 :",
468 \ execute('command DoCmd'))
469
470 " Test with various -range= and -count= argument values.
471 command! -range DoCmd :
472 call assert_equal("\n Name Args Address Complete Definition"
473 \ .. "\n DoCmd 0 . :",
474 \ execute('command DoCmd'))
475 command! -range=% DoCmd :
476 call assert_equal("\n Name Args Address Complete Definition"
477 \ .. "\n DoCmd 0 % :",
478 \ execute('command! DoCmd'))
479 command! -range=2 DoCmd :
480 call assert_equal("\n Name Args Address Complete Definition"
481 \ .. "\n DoCmd 0 2 :",
482 \ execute('command DoCmd'))
483 command! -count=2 DoCmd :
484 call assert_equal("\n Name Args Address Complete Definition"
Bram Moolenaarb7316892019-05-01 18:08:42 +0200485 \ .. "\n DoCmd 0 2c ? :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200486 \ execute('command DoCmd'))
487
488 " Test with various -addr= argument values.
489 command! -addr=lines DoCmd :
490 call assert_equal("\n Name Args Address Complete Definition"
491 \ .. "\n DoCmd 0 . :",
492 \ execute('command DoCmd'))
493 command! -addr=arguments DoCmd :
494 call assert_equal("\n Name Args Address Complete Definition"
495 \ .. "\n DoCmd 0 . arg :",
496 \ execute('command DoCmd'))
497 command! -addr=buffers DoCmd :
498 call assert_equal("\n Name Args Address Complete Definition"
499 \ .. "\n DoCmd 0 . buf :",
500 \ execute('command DoCmd'))
501 command! -addr=loaded_buffers DoCmd :
502 call assert_equal("\n Name Args Address Complete Definition"
503 \ .. "\n DoCmd 0 . load :",
504 \ execute('command DoCmd'))
505 command! -addr=windows DoCmd :
506 call assert_equal("\n Name Args Address Complete Definition"
507 \ .. "\n DoCmd 0 . win :",
508 \ execute('command DoCmd'))
509 command! -addr=tabs DoCmd :
510 call assert_equal("\n Name Args Address Complete Definition"
511 \ .. "\n DoCmd 0 . tab :",
512 \ execute('command DoCmd'))
513 command! -addr=other DoCmd :
514 call assert_equal("\n Name Args Address Complete Definition"
515 \ .. "\n DoCmd 0 . ? :",
516 \ execute('command DoCmd'))
517
518 " Test with various -complete= argument values (non-exhaustive list)
Martin Tournoijde69a732021-07-11 14:28:25 +0200519 command! -nargs=1 -complete=arglist DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200520 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200521 \ .. "\n DoCmd 1 arglist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200522 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200523 command! -nargs=* -complete=augroup DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200524 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200525 \ .. "\n DoCmd * augroup :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200526 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200527 command! -nargs=? -complete=custom,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200528 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200529 \ .. "\n DoCmd ? custom :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200530 \ execute('command DoCmd'))
Martin Tournoijde69a732021-07-11 14:28:25 +0200531 command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200532 call assert_equal("\n Name Args Address Complete Definition"
Martin Tournoijde69a732021-07-11 14:28:25 +0200533 \ .. "\n DoCmd + customlist :",
Bram Moolenaard1f90bb2019-04-25 22:42:07 +0200534 \ execute('command DoCmd'))
535
536 " Test with various -narg= argument values.
537 command! -nargs=0 DoCmd :
538 call assert_equal("\n Name Args Address Complete Definition"
539 \ .. "\n DoCmd 0 :",
540 \ execute('command DoCmd'))
541 command! -nargs=1 DoCmd :
542 call assert_equal("\n Name Args Address Complete Definition"
543 \ .. "\n DoCmd 1 :",
544 \ execute('command DoCmd'))
545 command! -nargs=* DoCmd :
546 call assert_equal("\n Name Args Address Complete Definition"
547 \ .. "\n DoCmd * :",
548 \ execute('command DoCmd'))
549 command! -nargs=? DoCmd :
550 call assert_equal("\n Name Args Address Complete Definition"
551 \ .. "\n DoCmd ? :",
552 \ execute('command DoCmd'))
553 command! -nargs=+ DoCmd :
554 call assert_equal("\n Name Args Address Complete Definition"
555 \ .. "\n DoCmd + :",
556 \ execute('command DoCmd'))
557
558 " Test with other arguments.
559 command! -bang DoCmd :
560 call assert_equal("\n Name Args Address Complete Definition"
561 \ .. "\n! DoCmd 0 :",
562 \ execute('command DoCmd'))
563 command! -bar DoCmd :
564 call assert_equal("\n Name Args Address Complete Definition"
565 \ .. "\n| DoCmd 0 :",
566 \ execute('command DoCmd'))
567 command! -register DoCmd :
568 call assert_equal("\n Name Args Address Complete Definition"
569 \ .. "\n\" DoCmd 0 :",
570 \ execute('command DoCmd'))
571 command! -buffer DoCmd :
572 call assert_equal("\n Name Args Address Complete Definition"
573 \ .. "\nb DoCmd 0 :"
574 \ .. "\n\" DoCmd 0 :",
575 \ execute('command DoCmd'))
576 comclear
577
578 " Test with many args.
579 command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
580 call assert_equal("\n Name Args Address Complete Definition"
581 \ .. "\n!\"b|DoCmd + 3c win environment :",
582 \ execute('command DoCmd'))
583 comclear
584
585 " Test with special characters in command definition.
586 command! DoCmd :<cr><tab><c-d>
587 call assert_equal("\n Name Args Address Complete Definition"
588 \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
589 \ execute('command DoCmd'))
590
591 " Test output in verbose mode.
592 command! DoCmd :
593 call assert_match("^\n"
594 \ .. " Name Args Address Complete Definition\n"
595 \ .. " DoCmd 0 :\n"
596 \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
597 \ execute('verbose command DoCmd'))
598
599 comclear
600 call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
601 call assert_equal("\nNo user-defined commands found", execute('command'))
602endfunc
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100603
Bram Moolenaar8b633132020-03-20 18:20:51 +0100604" Test for a custom user completion returning the wrong value type
605func Test_usercmd_custom()
606 func T1(a, c, p)
607 return "a\nb\n"
608 endfunc
609 command -nargs=* -complete=customlist,T1 TCmd1
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200610 call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
611 call assert_equal('"TCmd1 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100612 delcommand TCmd1
613 delfunc T1
614
615 func T2(a, c, p)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200616 return {}
Bram Moolenaar8b633132020-03-20 18:20:51 +0100617 endfunc
618 command -nargs=* -complete=customlist,T2 TCmd2
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200619 call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
620 call assert_equal('"TCmd2 ', @:)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100621 delcommand TCmd2
622 delfunc T2
623endfunc
624
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200625func Test_usercmd_with_block()
626 command DoSomething {
Bram Moolenaarec1b0962021-09-06 17:10:59 +0200627 g:didit = 'yes' # comment
628 # comment line
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200629 g:didmore = 'more'
630 }
631 DoSomething
632 call assert_equal('yes', g:didit)
633 call assert_equal('more', g:didmore)
634 unlet g:didit
635 unlet g:didmore
Bram Moolenaar68686342021-07-28 15:54:54 +0200636 delcommand DoSomething
637
638 command DoMap {
639 echo [1, 2, 3]->map((_, v) => v + 1)
640 }
641 DoMap
642 delcommand DoMap
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200643
644 let lines =<< trim END
645 command DoesNotEnd {
646 echo 'hello'
647 END
648 call CheckScriptFailure(lines, 'E1026:')
Bram Moolenaar63b91732021-08-05 20:40:03 +0200649
650 let lines =<< trim END
651 command BarCommand {
652 echo 'hello' | echo 'there'
653 }
654 BarCommand
655 END
656 call CheckScriptFailure(lines, 'E1231:')
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200657endfunc
658
Bram Moolenaarbdcba242021-09-12 20:58:02 +0200659func Test_delcommand_buffer()
660 command Global echo 'global'
661 command -buffer OneBuffer echo 'one'
662 new
663 command -buffer TwoBuffer echo 'two'
664 call assert_equal(0, exists(':OneBuffer'))
665 call assert_equal(2, exists(':Global'))
666 call assert_equal(2, exists(':TwoBuffer'))
667 delcommand -buffer TwoBuffer
668 call assert_equal(0, exists(':TwoBuffer'))
669 call assert_fails('delcommand -buffer Global', 'E1237:')
670 call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
671 bwipe!
672 call assert_equal(2, exists(':OneBuffer'))
673 delcommand -buffer OneBuffer
674 call assert_equal(0, exists(':OneBuffer'))
675 call assert_fails('delcommand -buffer Global', 'E1237:')
676 delcommand Global
677 call assert_equal(0, exists(':Global'))
678endfunc
679
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100680" vim: shiftwidth=2 sts=2 expandtab