blob: 710af413c58e1728be75e10a3023c346e8b3edcc [file] [log] [blame]
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001" Test commands that are not compiled in a :def function
2
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003source check.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004source vim9.vim
Bram Moolenaare88c8e82020-11-01 17:03:37 +01005source term_util.vim
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007
8def Test_edit_wildcards()
Bram Moolenaarac564082020-09-27 19:05:33 +02009 var filename = 'Xtest'
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010 edit `=filename`
11 assert_equal('Xtest', bufname())
12
Bram Moolenaarac564082020-09-27 19:05:33 +020013 var filenr = 123
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020014 edit Xtest`=filenr`
15 assert_equal('Xtest123', bufname())
16
17 filenr = 77
18 edit `=filename``=filenr`
19 assert_equal('Xtest77', bufname())
20
21 edit X`=filename`xx`=filenr`yy
22 assert_equal('XXtestxx77yy', bufname())
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010023
24 CheckDefFailure(['edit `=xxx`'], 'E1001:')
25 CheckDefFailure(['edit `="foo"'], 'E1083:')
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020026enddef
27
Bram Moolenaar4389f9c2020-12-27 16:55:11 +010028def Test_expand_alternate_file()
29 var lines =<< trim END
30 edit Xfileone
31 var bone = bufnr()
32 edit Xfiletwo
33 var btwo = bufnr()
34 edit Xfilethree
35 var bthree = bufnr()
36
37 edit #
38 assert_equal(bthree, bufnr())
39 edit %%
40 assert_equal(btwo, bufnr())
41 edit %% # comment
42 assert_equal(bthree, bufnr())
43 edit %%yy
44 assert_equal('Xfiletwoyy', bufname())
45
46 exe "edit %%" .. bone
47 assert_equal(bone, bufnr())
48 exe "edit %%" .. btwo .. "xx"
49 assert_equal('Xfiletwoxx', bufname())
50
51 next Xfileone Xfiletwo Xfilethree
52 assert_equal('Xfileone', argv(0))
53 assert_equal('Xfiletwo', argv(1))
54 assert_equal('Xfilethree', argv(2))
55 next %%%zz
56 assert_equal('Xfileone', argv(0))
57 assert_equal('Xfiletwo', argv(1))
58 assert_equal('Xfilethreezz', argv(2))
59
60 v:oldfiles = ['Xonefile', 'Xtwofile']
61 edit %%<1
62 assert_equal('Xonefile', bufname())
63 edit %%<2
64 assert_equal('Xtwofile', bufname())
65 assert_fails('edit %%<3', 'E684:')
66
67 edit Xfileone.vim
68 edit Xfiletwo
69 edit %%:r
70 assert_equal('Xfileone', bufname())
71 END
72 CheckDefAndScriptSuccess(lines)
73enddef
74
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +010075def Test_global_backtick_expansion()
76 new
77 setline(1, 'xx')
78 var name = 'foobar'
79 g/^xx/s/.*/`=name`
80 assert_equal('foobar', getline(1))
81 bwipe!
82enddef
83
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020084def Test_hardcopy_wildcards()
85 CheckUnix
86 CheckFeature postscript
87
Bram Moolenaarac564082020-09-27 19:05:33 +020088 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020089 hardcopy > X`=outfile`.ps
90 assert_true(filereadable('Xprint.ps'))
91
92 delete('Xprint.ps')
93enddef
94
95def Test_syn_include_wildcards()
96 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +020097 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020098 &rtp = '.'
99
Bram Moolenaarac564082020-09-27 19:05:33 +0200100 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200101 syn include @Group Xthe`=fname`.vim
102 assert_match('Found.* contained found', execute('syn list Found'))
103
104 &rtp = save_rtp
105 delete('Xthemine.vim')
106enddef
107
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200108def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200109 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200110 vim9script
111 redir @a
112 echo 'one'
113 .. 'two'
114 redir END
115 assert_equal("\nonetwo", @a)
116 END
117 CheckScriptSuccess(lines)
118
119 lines =<< trim END
120 vim9script
121 redir @a
122 echo 11 +
123 77
124 - 22
125 redir END
126 assert_equal("\n66", @a)
127 END
128 CheckScriptSuccess(lines)
129enddef
130
Bram Moolenaar13106602020-10-04 16:06:05 +0200131def Test_condition_types()
132 var lines =<< trim END
133 if 'text'
134 endif
135 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100136 CheckDefAndScriptFailure(lines, 'E1135:', 1)
Bram Moolenaar13106602020-10-04 16:06:05 +0200137
138 lines =<< trim END
139 if [1]
140 endif
141 END
142 CheckDefFailure(lines, 'E1012:', 1)
143 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
144
145 lines =<< trim END
146 g:cond = 'text'
147 if g:cond
148 endif
149 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100150 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200151
152 lines =<< trim END
153 g:cond = 0
154 if g:cond
155 elseif 'text'
156 endif
157 END
158 CheckDefFailure(lines, 'E1012:', 3)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100159 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
Bram Moolenaar13106602020-10-04 16:06:05 +0200160
161 lines =<< trim END
162 if g:cond
163 elseif [1]
164 endif
165 END
166 CheckDefFailure(lines, 'E1012:', 2)
167 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
168
169 lines =<< trim END
170 g:cond = 'text'
171 if 0
172 elseif g:cond
173 endif
174 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100175 CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
Bram Moolenaar13106602020-10-04 16:06:05 +0200176
177 lines =<< trim END
178 while 'text'
179 endwhile
180 END
181 CheckDefFailure(lines, 'E1012:', 1)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100182 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200183
184 lines =<< trim END
185 while [1]
186 endwhile
187 END
188 CheckDefFailure(lines, 'E1012:', 1)
189 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
190
191 lines =<< trim END
192 g:cond = 'text'
193 while g:cond
194 endwhile
195 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100196 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200197enddef
198
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200199def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200200 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 vim9script
202 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200203 true
204 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200205 g:res = 42
206 endif
207 assert_equal(42, g:res)
208 END
209 CheckScriptSuccess(lines)
210 unlet g:res
211
212 lines =<< trim END
213 vim9script
214 if 1 &&
215 0
216 g:res = 0
217 elseif 0 ||
218 0
219 || 1
220 g:res = 12
221 endif
222 assert_equal(12, g:res)
223 END
224 CheckScriptSuccess(lines)
225 unlet g:res
226enddef
227
228def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200229 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200230 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200231 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200232 while nr <
233 10 + 3
234 nr = nr
235 + 4
236 endwhile
237 assert_equal(16, nr)
238 END
239 CheckScriptSuccess(lines)
240
241 lines =<< trim END
242 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200243 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200244 while nr
245 <
246 10
247 +
248 3
249 nr = nr
250 +
251 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200252 endwhile
253 assert_equal(16, nr)
254 END
255 CheckScriptSuccess(lines)
256enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200257
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200258def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200259 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200260 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200261 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200262 for x
263 in
264 [1, 2, 3, 4]
265 nr = nr + x
266 endfor
267 assert_equal(10, nr)
268 END
269 CheckScriptSuccess(lines)
270
271 lines =<< trim END
272 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200273 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200274 for x
275 in
276 [1, 2,
277 3, 4
278 ]
279 nr = nr
280 +
281 x
282 endfor
283 assert_equal(10, nr)
284 END
285 CheckScriptSuccess(lines)
286enddef
287
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200288def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200289 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200290 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200291 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200292 func RetArg(
293 arg
294 )
295 let s:res = a:arg
296 endfunc
297 [1,
298 2,
299 3]->RetArg()
300 assert_equal([1, 2, 3], res)
301 END
302 CheckScriptSuccess(lines)
303enddef
304
Bram Moolenaar683581e2020-10-22 21:22:58 +0200305def Test_skipped_expr_linebreak()
306 if 0
307 var x = []
308 ->map({ -> 0})
309 endif
310enddef
311
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200312def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100313 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200314 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100315 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200316 test.data
317 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100318 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200319
Bram Moolenaarac564082020-09-27 19:05:33 +0200320 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200321 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100322 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200323 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100324 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200325 END
326 CheckScriptSuccess(lines)
327enddef
328
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200329def Test_bar_after_command()
330 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200331 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200332 redraw | echo x
333 enddef
334 RedrawAndEcho()
335 assert_match('did redraw', Screenline(&lines))
336
Bram Moolenaar788123c2020-07-05 15:32:17 +0200337 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200338 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200339 reg_executing() | echo x
340 enddef
341 CallAndEcho()
342 assert_match('did redraw', Screenline(&lines))
343
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200344 if has('unix')
345 # bar in filter write command does not start new command
346 def WriteToShell()
347 new
348 setline(1, 'some text')
349 w !cat | cat > Xoutfile
350 bwipe!
351 enddef
352 WriteToShell()
353 assert_equal(['some text'], readfile('Xoutfile'))
354 delete('Xoutfile')
355
356 # bar in filter read command does not start new command
357 def ReadFromShell()
358 new
359 r! echo hello there | cat > Xoutfile
360 r !echo again | cat >> Xoutfile
361 bwipe!
362 enddef
363 ReadFromShell()
364 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
365 delete('Xoutfile')
366 endif
367enddef
368
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200369def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100370 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200371 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
Bram Moolenaare0de1712020-12-02 17:36:54 +0100372 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200373enddef
374
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100375def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100376 var lines =<< trim END
377 final expected = "\nType Name Content\n c \"c piyo"
378 @a = 'hoge'
379 @b = 'fuga'
380 @c = 'piyo'
381
382 assert_equal(execute('filter /piyo/ registers abc'), expected)
383 END
384 CheckDefAndScriptSuccess(lines)
385enddef
386
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100387def Test_win_command_modifiers()
388 assert_equal(1, winnr('$'))
389
390 set splitright
391 vsplit
392 assert_equal(2, winnr())
393 close
394 aboveleft vsplit
395 assert_equal(1, winnr())
396 close
397 set splitright&
398
399 vsplit
400 assert_equal(1, winnr())
401 close
402 belowright vsplit
403 assert_equal(2, winnr())
404 close
405 rightbelow vsplit
406 assert_equal(2, winnr())
407 close
408
Bram Moolenaar97a19002020-11-01 22:15:44 +0100409 if has('browse')
410 browse set
411 assert_equal('option-window', expand('%'))
412 close
413 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100414
415 vsplit
416 botright split
417 assert_equal(3, winnr())
418 assert_equal(&columns, winwidth(0))
419 close
420 close
421
422 vsplit
423 topleft split
424 assert_equal(1, winnr())
425 assert_equal(&columns, winwidth(0))
426 close
427 close
428
429 gettabinfo()->len()->assert_equal(1)
430 tab split
431 gettabinfo()->len()->assert_equal(2)
432 tabclose
433
434 vertical new
435 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
436 close
437enddef
438
439func Test_command_modifier_confirm()
440 CheckNotGui
441 CheckRunVimInTerminal
442
443 " Test for saving all the modified buffers
444 let lines =<< trim END
445 call setline(1, 'changed')
446 def Getout()
447 confirm write Xfile
448 enddef
449 END
450 call writefile(lines, 'Xconfirmscript')
451 call writefile(['empty'], 'Xfile')
452 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
453 call term_sendkeys(buf, ":call Getout()\n")
454 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
455 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100456 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
457 call term_sendkeys(buf, "\<CR>")
458 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100459 call StopVimInTerminal(buf)
460
461 call assert_equal(['changed'], readfile('Xfile'))
462 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100463 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100464 call delete('Xconfirmscript')
465endfunc
466
467def Test_command_modifiers_keep()
468 if has('unix')
469 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
470 new
471 setline(1, ['one', 'two', 'three'])
472 normal 1Gma
473 normal 2Gmb
474 normal 3Gmc
475 if addRflag
476 set cpo+=R
477 else
478 set cpo-=R
479 endif
480 if keepMarks
481 keepmarks :%!cat
482 else
483 :%!cat
484 endif
485 if hasMarks
486 assert_equal(1, line("'a"))
487 assert_equal(2, line("'b"))
488 assert_equal(3, line("'c"))
489 else
490 assert_equal(0, line("'a"))
491 assert_equal(0, line("'b"))
492 assert_equal(0, line("'c"))
493 endif
494 quit!
495 enddef
496 DoTest(false, false, true)
497 DoTest(true, false, false)
498 DoTest(false, true, true)
499 DoTest(true, true, true)
500 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100501
502 new
503 setline(1, ['one', 'two', 'three', 'four'])
504 assert_equal(4, line("$"))
505 normal 1Gma
506 normal 2Gmb
507 normal 3Gmc
508 lockmarks :1,2!wc
509 # line is deleted, marks don't move
510 assert_equal(3, line("$"))
511 assert_equal('four', getline(3))
512 assert_equal(1, line("'a"))
513 assert_equal(2, line("'b"))
514 assert_equal(3, line("'c"))
515 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100516 endif
517
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100518 edit Xone
519 edit Xtwo
520 assert_equal('Xone', expand('#'))
521 keepalt edit Xthree
522 assert_equal('Xone', expand('#'))
523
524 normal /a*b*
525 assert_equal('a*b*', histget("search"))
526 keeppatterns normal /c*d*
527 assert_equal('a*b*', histget("search"))
528
529 new
530 setline(1, range(10))
531 :10
532 normal gg
533 assert_equal(10, getpos("''")[1])
534 keepjumps normal 5G
535 assert_equal(10, getpos("''")[1])
536 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100537enddef
538
539def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100540 new Xsomefile
541 setline(1, 'changed')
542 var buf = bufnr()
543 hide edit Xotherfile
544 var info = getbufinfo(buf)
545 assert_equal(1, info[0].hidden)
546 assert_equal(1, info[0].changed)
547 edit Xsomefile
548 bwipe!
549
550 au BufNewFile Xfile g:readFile = 1
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100551 | g:readExtra = 2
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100552 g:readFile = 0
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100553 g:readExtra = 0
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100554 edit Xfile
555 assert_equal(1, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100556 assert_equal(2, g:readExtra)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100557 bwipe!
558 g:readFile = 0
559 noautocmd edit Xfile
560 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100561 au! BufNewFile
562
563 au BufNewFile Xfile g:readFile = 1
564 | g:readExtra = 2
565 | g:readMore = 3
566 g:readFile = 0
567 g:readExtra = 0
568 g:readMore = 0
569 edit Xfile
570 assert_equal(1, g:readFile)
571 assert_equal(2, g:readExtra)
572 assert_equal(3, g:readMore)
573 bwipe!
574 au! BufNewFile
575 unlet g:readFile
576 unlet g:readExtra
577 unlet g:readMore
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100578
579 noswapfile edit XnoSwap
580 assert_equal(0, &l:swapfile)
581 bwipe!
582
583 var caught = false
584 try
585 sandbox !ls
586 catch /E48:/
587 caught = true
588 endtry
589 assert_true(caught)
590
591 :8verbose g:verbose_now = &verbose
592 assert_equal(8, g:verbose_now)
593 unlet g:verbose_now
594enddef
595
596def EchoHere()
597 echomsg 'here'
598enddef
599def EchoThere()
600 unsilent echomsg 'there'
601enddef
602
603def Test_modifier_silent_unsilent()
604 echomsg 'last one'
605 silent echomsg "text"
606 assert_equal("\nlast one", execute(':1messages'))
607
608 silent! echoerr "error"
609
610 echomsg 'last one'
611 silent EchoHere()
612 assert_equal("\nlast one", execute(':1messages'))
613
614 silent EchoThere()
615 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100616
617 try
618 silent eval [][0]
619 catch
620 echomsg "caught"
621 endtry
622 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100623enddef
624
Bram Moolenaar36113e42020-11-02 21:08:47 +0100625def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100626 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100627 new
628 setline(1, 'xxx')
629 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
630 assert_equal('', getline(1))
631 bwipe!
632enddef
633
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200634def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200635 var from = 3
636 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200637 g:val = 111
638 def Increment(nrs: list<number>)
639 for nr in nrs
640 g:val += nr
641 endfor
642 enddef
643 eval range(from, to)
644 ->Increment()
645 assert_equal(111 + 3 + 4 + 5, g:val)
646 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100647
648 var lines =<< trim END
649 vim9script
650 g:caught = 'no'
651 try
652 eval 123 || 0
653 catch
654 g:caught = 'yes'
655 endtry
656 assert_equal('yes', g:caught)
657 unlet g:caught
658 END
659 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200660enddef
661
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200662def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200663 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200664 nnoremap <F3> :echo 'hit F3 #'<CR>
665 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
666 END
667 CheckDefSuccess(lines)
668 CheckScriptSuccess(['vim9script'] + lines)
669enddef
670
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200671def Test_normal_command()
672 new
673 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200674 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200675 try
676 exe "norm! \<C-]>"
677 catch /E433/
678 caught = 2
679 endtry
680 assert_equal(2, caught)
681
682 try
683 exe "norm! 3\<C-]>"
684 catch /E433/
685 caught = 3
686 endtry
687 assert_equal(3, caught)
688 bwipe!
689enddef
690
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200691def Test_put_command()
692 new
693 @p = 'ppp'
694 put p
695 assert_equal('ppp', getline(2))
696
697 put ='below'
698 assert_equal('below', getline(3))
699 put! ='above'
700 assert_equal('above', getline(3))
701 assert_equal('below', getline(4))
702
Bram Moolenaar08597872020-12-10 19:43:40 +0100703 # compute range at runtime
704 setline(1, range(1, 8))
705 @a = 'aaa'
706 :$-2put a
707 assert_equal('aaa', getline(7))
708
709 setline(1, range(1, 8))
710 :2
711 :+2put! a
712 assert_equal('aaa', getline(4))
713
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200714 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100715
716 CheckDefFailure(['put =xxx'], 'E1001:')
717enddef
718
719def Test_put_with_linebreak()
720 new
721 var lines =<< trim END
722 vim9script
723 pu =split('abc', '\zs')
724 ->join()
725 END
726 CheckScriptSuccess(lines)
727 getline(2)->assert_equal('a b c')
728 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200729enddef
730
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200731def Test_command_star_range()
732 new
733 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
734 setpos("'<", [0, 1, 0, 0])
735 setpos("'>", [0, 3, 0, 0])
736 :*s/\(foo\|bar\)/baz/g
737 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
738
739 bwipe!
740enddef
741
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200742def Test_f_args()
743 var lines =<< trim END
744 vim9script
745
746 func SaveCmdArgs(...)
747 let g:args = a:000
748 endfunc
749
750 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
751
752 TestFArgs
753 assert_equal([], g:args)
754
755 TestFArgs one two three
756 assert_equal(['one', 'two', 'three'], g:args)
757 END
758 CheckScriptSuccess(lines)
759enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200760
Bram Moolenaar95388e32020-11-20 21:07:00 +0100761def Test_star_command()
762 var lines =<< trim END
763 vim9script
764 @s = 'g:success = 8'
765 set cpo+=*
766 exe '*s'
767 assert_equal(8, g:success)
768 unlet g:success
769 set cpo-=*
770 assert_fails("exe '*s'", 'E1050:')
771 END
772 CheckScriptSuccess(lines)
773enddef
774
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100775def Test_cmd_argument_without_colon()
776 new Xfile
777 setline(1, ['a', 'b', 'c', 'd'])
778 write
779 edit +3 %
780 assert_equal(3, getcurpos()[1])
781 edit +/a %
782 assert_equal(1, getcurpos()[1])
783 bwipe
784 delete('Xfile')
785enddef
786
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100787def Test_ambiguous_user_cmd()
788 var lines =<< trim END
789 com Cmd1 eval 0
790 com Cmd2 eval 0
791 Cmd
792 END
793 CheckScriptFailure(lines, 'E464:')
794enddef
795
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100796def Test_command_not_recognized()
797 var lines =<< trim END
798 d.key = 'asdf'
799 END
800 CheckDefFailure(lines, 'E1146:', 1)
801
802 lines =<< trim END
803 d['key'] = 'asdf'
804 END
805 CheckDefFailure(lines, 'E1146:', 1)
806enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200807
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100808def Test_magic_not_used()
809 new
810 for cmd in ['set magic', 'set nomagic']
811 exe cmd
812 setline(1, 'aaa')
813 s/.../bbb/
814 assert_equal('bbb', getline(1))
815 endfor
816
817 set magic
818 setline(1, 'aaa')
819 assert_fails('s/.\M../bbb/', 'E486:')
820 assert_fails('snomagic/.../bbb/', 'E486:')
821 assert_equal('aaa', getline(1))
822
823 bwipe!
824enddef
825
Bram Moolenaar60f63102020-12-21 20:32:43 +0100826def Test_gdefault_not_used()
827 new
828 for cmd in ['set gdefault', 'set nogdefault']
829 exe cmd
830 setline(1, 'aaa')
831 s/./b/
832 assert_equal('baa', getline(1))
833 endfor
834
835 set nogdefault
836 bwipe!
837enddef
838
Bram Moolenaar179eb562020-12-27 18:03:22 +0100839def g:SomeComplFunc(findstart: number, base: string): any
840 if findstart
841 return 0
842 else
843 return ['aaa', 'bbb']
844 endif
845enddef
846
847def Test_insert_complete()
848 # this was running into an error with the matchparen hack
849 new
850 set completefunc=SomeComplFunc
851 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
852 assert_equal('aaa', getline(1))
853
854 set completefunc=
855 bwipe!
856enddef
857
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200858" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker