blob: 12b9bf191b0fb4792e0f6a3384489eee7d6613db [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 Moolenaarecac5912021-01-05 19:23:28 +010084def Test_folddo_backtick_expansion()
85 new
86 var name = 'xxx'
87 folddoopen edit `=name`
88 assert_equal('xxx', bufname())
89 bwipe!
90
91 new
92 setline(1, ['one', 'two'])
93 set nomodified
94 :1,2fold
95 foldclose
96 folddoclose edit `=name`
97 assert_equal('xxx', bufname())
98 bwipe!
99enddef
100
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200101def Test_hardcopy_wildcards()
102 CheckUnix
103 CheckFeature postscript
104
Bram Moolenaarac564082020-09-27 19:05:33 +0200105 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200106 hardcopy > X`=outfile`.ps
107 assert_true(filereadable('Xprint.ps'))
108
109 delete('Xprint.ps')
110enddef
111
112def Test_syn_include_wildcards()
113 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +0200114 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200115 &rtp = '.'
116
Bram Moolenaarac564082020-09-27 19:05:33 +0200117 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200118 syn include @Group Xthe`=fname`.vim
119 assert_match('Found.* contained found', execute('syn list Found'))
120
121 &rtp = save_rtp
122 delete('Xthemine.vim')
123enddef
124
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200125def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200126 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200127 vim9script
128 redir @a
129 echo 'one'
130 .. 'two'
131 redir END
132 assert_equal("\nonetwo", @a)
133 END
134 CheckScriptSuccess(lines)
135
136 lines =<< trim END
137 vim9script
138 redir @a
139 echo 11 +
140 77
141 - 22
142 redir END
143 assert_equal("\n66", @a)
144 END
145 CheckScriptSuccess(lines)
146enddef
147
Bram Moolenaar13106602020-10-04 16:06:05 +0200148def Test_condition_types()
149 var lines =<< trim END
150 if 'text'
151 endif
152 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100153 CheckDefAndScriptFailure(lines, 'E1135:', 1)
Bram Moolenaar13106602020-10-04 16:06:05 +0200154
155 lines =<< trim END
156 if [1]
157 endif
158 END
159 CheckDefFailure(lines, 'E1012:', 1)
160 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
161
162 lines =<< trim END
163 g:cond = 'text'
164 if g:cond
165 endif
166 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100167 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200168
169 lines =<< trim END
170 g:cond = 0
171 if g:cond
172 elseif 'text'
173 endif
174 END
175 CheckDefFailure(lines, 'E1012:', 3)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100176 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
Bram Moolenaar13106602020-10-04 16:06:05 +0200177
178 lines =<< trim END
179 if g:cond
180 elseif [1]
181 endif
182 END
183 CheckDefFailure(lines, 'E1012:', 2)
184 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
185
186 lines =<< trim END
187 g:cond = 'text'
188 if 0
189 elseif g:cond
190 endif
191 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100192 CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
Bram Moolenaar13106602020-10-04 16:06:05 +0200193
194 lines =<< trim END
195 while 'text'
196 endwhile
197 END
198 CheckDefFailure(lines, 'E1012:', 1)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100199 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200200
201 lines =<< trim END
202 while [1]
203 endwhile
204 END
205 CheckDefFailure(lines, 'E1012:', 1)
206 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
207
208 lines =<< trim END
209 g:cond = 'text'
210 while g:cond
211 endwhile
212 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100213 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200214enddef
215
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200216def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200217 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200218 vim9script
219 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200220 true
221 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200222 g:res = 42
223 endif
224 assert_equal(42, g:res)
225 END
226 CheckScriptSuccess(lines)
227 unlet g:res
228
229 lines =<< trim END
230 vim9script
231 if 1 &&
232 0
233 g:res = 0
234 elseif 0 ||
235 0
236 || 1
237 g:res = 12
238 endif
239 assert_equal(12, g:res)
240 END
241 CheckScriptSuccess(lines)
242 unlet g:res
243enddef
244
245def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200246 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200247 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200248 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200249 while nr <
250 10 + 3
251 nr = nr
252 + 4
253 endwhile
254 assert_equal(16, nr)
255 END
256 CheckScriptSuccess(lines)
257
258 lines =<< trim END
259 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200260 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200261 while nr
262 <
263 10
264 +
265 3
266 nr = nr
267 +
268 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200269 endwhile
270 assert_equal(16, nr)
271 END
272 CheckScriptSuccess(lines)
273enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200274
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200275def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200276 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200277 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200278 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200279 for x
280 in
281 [1, 2, 3, 4]
282 nr = nr + x
283 endfor
284 assert_equal(10, nr)
285 END
286 CheckScriptSuccess(lines)
287
288 lines =<< trim END
289 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200290 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200291 for x
292 in
293 [1, 2,
294 3, 4
295 ]
296 nr = nr
297 +
298 x
299 endfor
300 assert_equal(10, nr)
301 END
302 CheckScriptSuccess(lines)
303enddef
304
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200305def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200306 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200307 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200308 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200309 func RetArg(
310 arg
311 )
312 let s:res = a:arg
313 endfunc
314 [1,
315 2,
316 3]->RetArg()
317 assert_equal([1, 2, 3], res)
318 END
319 CheckScriptSuccess(lines)
320enddef
321
Bram Moolenaar683581e2020-10-22 21:22:58 +0200322def Test_skipped_expr_linebreak()
323 if 0
324 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100325 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200326 endif
327enddef
328
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200329def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100330 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200331 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100332 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200333 test.data
334 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100335 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200336
Bram Moolenaarac564082020-09-27 19:05:33 +0200337 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200338 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100339 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200340 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100341 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200342 END
343 CheckScriptSuccess(lines)
344enddef
345
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200346def Test_bar_after_command()
347 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200348 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200349 redraw | echo x
350 enddef
351 RedrawAndEcho()
352 assert_match('did redraw', Screenline(&lines))
353
Bram Moolenaar788123c2020-07-05 15:32:17 +0200354 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200355 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200356 reg_executing() | echo x
357 enddef
358 CallAndEcho()
359 assert_match('did redraw', Screenline(&lines))
360
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200361 if has('unix')
362 # bar in filter write command does not start new command
363 def WriteToShell()
364 new
365 setline(1, 'some text')
366 w !cat | cat > Xoutfile
367 bwipe!
368 enddef
369 WriteToShell()
370 assert_equal(['some text'], readfile('Xoutfile'))
371 delete('Xoutfile')
372
373 # bar in filter read command does not start new command
374 def ReadFromShell()
375 new
376 r! echo hello there | cat > Xoutfile
377 r !echo again | cat >> Xoutfile
378 bwipe!
379 enddef
380 ReadFromShell()
381 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
382 delete('Xoutfile')
383 endif
384enddef
385
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200386def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100387 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100388 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100389 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200390enddef
391
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100392def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100393 var lines =<< trim END
394 final expected = "\nType Name Content\n c \"c piyo"
395 @a = 'hoge'
396 @b = 'fuga'
397 @c = 'piyo'
398
399 assert_equal(execute('filter /piyo/ registers abc'), expected)
400 END
401 CheckDefAndScriptSuccess(lines)
402enddef
403
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100404def Test_win_command_modifiers()
405 assert_equal(1, winnr('$'))
406
407 set splitright
408 vsplit
409 assert_equal(2, winnr())
410 close
411 aboveleft vsplit
412 assert_equal(1, winnr())
413 close
414 set splitright&
415
416 vsplit
417 assert_equal(1, winnr())
418 close
419 belowright vsplit
420 assert_equal(2, winnr())
421 close
422 rightbelow vsplit
423 assert_equal(2, winnr())
424 close
425
Bram Moolenaar97a19002020-11-01 22:15:44 +0100426 if has('browse')
427 browse set
428 assert_equal('option-window', expand('%'))
429 close
430 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100431
432 vsplit
433 botright split
434 assert_equal(3, winnr())
435 assert_equal(&columns, winwidth(0))
436 close
437 close
438
439 vsplit
440 topleft split
441 assert_equal(1, winnr())
442 assert_equal(&columns, winwidth(0))
443 close
444 close
445
446 gettabinfo()->len()->assert_equal(1)
447 tab split
448 gettabinfo()->len()->assert_equal(2)
449 tabclose
450
451 vertical new
452 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
453 close
454enddef
455
456func Test_command_modifier_confirm()
457 CheckNotGui
458 CheckRunVimInTerminal
459
460 " Test for saving all the modified buffers
461 let lines =<< trim END
462 call setline(1, 'changed')
463 def Getout()
464 confirm write Xfile
465 enddef
466 END
467 call writefile(lines, 'Xconfirmscript')
468 call writefile(['empty'], 'Xfile')
469 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
470 call term_sendkeys(buf, ":call Getout()\n")
471 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
472 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100473 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
474 call term_sendkeys(buf, "\<CR>")
475 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100476 call StopVimInTerminal(buf)
477
478 call assert_equal(['changed'], readfile('Xfile'))
479 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100480 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100481 call delete('Xconfirmscript')
482endfunc
483
484def Test_command_modifiers_keep()
485 if has('unix')
486 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
487 new
488 setline(1, ['one', 'two', 'three'])
489 normal 1Gma
490 normal 2Gmb
491 normal 3Gmc
492 if addRflag
493 set cpo+=R
494 else
495 set cpo-=R
496 endif
497 if keepMarks
498 keepmarks :%!cat
499 else
500 :%!cat
501 endif
502 if hasMarks
503 assert_equal(1, line("'a"))
504 assert_equal(2, line("'b"))
505 assert_equal(3, line("'c"))
506 else
507 assert_equal(0, line("'a"))
508 assert_equal(0, line("'b"))
509 assert_equal(0, line("'c"))
510 endif
511 quit!
512 enddef
513 DoTest(false, false, true)
514 DoTest(true, false, false)
515 DoTest(false, true, true)
516 DoTest(true, true, true)
517 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100518
519 new
520 setline(1, ['one', 'two', 'three', 'four'])
521 assert_equal(4, line("$"))
522 normal 1Gma
523 normal 2Gmb
524 normal 3Gmc
525 lockmarks :1,2!wc
526 # line is deleted, marks don't move
527 assert_equal(3, line("$"))
528 assert_equal('four', getline(3))
529 assert_equal(1, line("'a"))
530 assert_equal(2, line("'b"))
531 assert_equal(3, line("'c"))
532 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100533 endif
534
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100535 edit Xone
536 edit Xtwo
537 assert_equal('Xone', expand('#'))
538 keepalt edit Xthree
539 assert_equal('Xone', expand('#'))
540
541 normal /a*b*
542 assert_equal('a*b*', histget("search"))
543 keeppatterns normal /c*d*
544 assert_equal('a*b*', histget("search"))
545
546 new
547 setline(1, range(10))
548 :10
549 normal gg
550 assert_equal(10, getpos("''")[1])
551 keepjumps normal 5G
552 assert_equal(10, getpos("''")[1])
553 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100554enddef
555
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100556def Test_bar_line_continuation()
557 var lines =<< trim END
558 au BufNewFile Xfile g:readFile = 1
559 | g:readExtra = 2
560 g:readFile = 0
561 g:readExtra = 0
562 edit Xfile
563 assert_equal(1, g:readFile)
564 assert_equal(2, g:readExtra)
565 bwipe!
566 au! BufNewFile
567
568 au BufNewFile Xfile g:readFile = 1
569 | g:readExtra = 2
570 | g:readMore = 3
571 g:readFile = 0
572 g:readExtra = 0
573 g:readMore = 0
574 edit Xfile
575 assert_equal(1, g:readFile)
576 assert_equal(2, g:readExtra)
577 assert_equal(3, g:readMore)
578 bwipe!
579 au! BufNewFile
580 unlet g:readFile
581 unlet g:readExtra
582 unlet g:readMore
583 END
584 CheckDefAndScriptSuccess(lines)
585enddef
586
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100587def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100588 new Xsomefile
589 setline(1, 'changed')
590 var buf = bufnr()
591 hide edit Xotherfile
592 var info = getbufinfo(buf)
593 assert_equal(1, info[0].hidden)
594 assert_equal(1, info[0].changed)
595 edit Xsomefile
596 bwipe!
597
598 au BufNewFile Xfile g:readFile = 1
599 g:readFile = 0
600 edit Xfile
601 assert_equal(1, g:readFile)
602 bwipe!
603 g:readFile = 0
604 noautocmd edit Xfile
605 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100606 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100607 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100608
609 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100610 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100611 bwipe!
612
613 var caught = false
614 try
615 sandbox !ls
616 catch /E48:/
617 caught = true
618 endtry
619 assert_true(caught)
620
621 :8verbose g:verbose_now = &verbose
622 assert_equal(8, g:verbose_now)
623 unlet g:verbose_now
624enddef
625
626def EchoHere()
627 echomsg 'here'
628enddef
629def EchoThere()
630 unsilent echomsg 'there'
631enddef
632
633def Test_modifier_silent_unsilent()
634 echomsg 'last one'
635 silent echomsg "text"
636 assert_equal("\nlast one", execute(':1messages'))
637
638 silent! echoerr "error"
639
640 echomsg 'last one'
641 silent EchoHere()
642 assert_equal("\nlast one", execute(':1messages'))
643
644 silent EchoThere()
645 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100646
647 try
648 silent eval [][0]
649 catch
650 echomsg "caught"
651 endtry
652 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100653enddef
654
Bram Moolenaar36113e42020-11-02 21:08:47 +0100655def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100656 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100657 new
658 setline(1, 'xxx')
659 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
660 assert_equal('', getline(1))
661 bwipe!
662enddef
663
Bram Moolenaarece0b872021-01-08 20:40:45 +0100664def Test_silent_pattern()
665 new
666 silent! :/pat/put _
667 bwipe!
668enddef
669
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200670def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200671 var from = 3
672 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200673 g:val = 111
674 def Increment(nrs: list<number>)
675 for nr in nrs
676 g:val += nr
677 endfor
678 enddef
679 eval range(from, to)
680 ->Increment()
681 assert_equal(111 + 3 + 4 + 5, g:val)
682 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100683
684 var lines =<< trim END
685 vim9script
686 g:caught = 'no'
687 try
688 eval 123 || 0
689 catch
690 g:caught = 'yes'
691 endtry
692 assert_equal('yes', g:caught)
693 unlet g:caught
694 END
695 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200696enddef
697
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200698def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200699 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200700 nnoremap <F3> :echo 'hit F3 #'<CR>
701 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
702 END
703 CheckDefSuccess(lines)
704 CheckScriptSuccess(['vim9script'] + lines)
705enddef
706
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200707def Test_normal_command()
708 new
709 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200710 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200711 try
712 exe "norm! \<C-]>"
713 catch /E433/
714 caught = 2
715 endtry
716 assert_equal(2, caught)
717
718 try
719 exe "norm! 3\<C-]>"
720 catch /E433/
721 caught = 3
722 endtry
723 assert_equal(3, caught)
724 bwipe!
725enddef
726
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200727def Test_put_command()
728 new
729 @p = 'ppp'
730 put p
731 assert_equal('ppp', getline(2))
732
733 put ='below'
734 assert_equal('below', getline(3))
735 put! ='above'
736 assert_equal('above', getline(3))
737 assert_equal('below', getline(4))
738
Bram Moolenaar883cf972021-01-15 18:04:43 +0100739 :2put =['a', 'b', 'c']
740 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
741
Bram Moolenaar08597872020-12-10 19:43:40 +0100742 # compute range at runtime
743 setline(1, range(1, 8))
744 @a = 'aaa'
745 :$-2put a
746 assert_equal('aaa', getline(7))
747
748 setline(1, range(1, 8))
749 :2
750 :+2put! a
751 assert_equal('aaa', getline(4))
752
Bram Moolenaara28639e2021-01-19 22:48:09 +0100753 []->mapnew(() => 0)
754 :$put ='end'
755 assert_equal('end', getline('$'))
756
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200757 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100758
759 CheckDefFailure(['put =xxx'], 'E1001:')
760enddef
761
762def Test_put_with_linebreak()
763 new
764 var lines =<< trim END
765 vim9script
766 pu =split('abc', '\zs')
767 ->join()
768 END
769 CheckScriptSuccess(lines)
770 getline(2)->assert_equal('a b c')
771 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200772enddef
773
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200774def Test_command_star_range()
775 new
776 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
777 setpos("'<", [0, 1, 0, 0])
778 setpos("'>", [0, 3, 0, 0])
779 :*s/\(foo\|bar\)/baz/g
780 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
781
782 bwipe!
783enddef
784
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200785def Test_f_args()
786 var lines =<< trim END
787 vim9script
788
789 func SaveCmdArgs(...)
790 let g:args = a:000
791 endfunc
792
793 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
794
795 TestFArgs
796 assert_equal([], g:args)
797
798 TestFArgs one two three
799 assert_equal(['one', 'two', 'three'], g:args)
800 END
801 CheckScriptSuccess(lines)
802enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200803
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100804def Test_user_command_comment()
805 command -nargs=1 Comd echom <q-args>
806
807 var lines =<< trim END
808 vim9script
809 Comd # comment
810 END
811 CheckScriptSuccess(lines)
812
813 lines =<< trim END
814 vim9script
815 Comd# comment
816 END
817 CheckScriptFailure(lines, 'E1144:')
818
819 delcommand Comd
820enddef
821
Bram Moolenaar95388e32020-11-20 21:07:00 +0100822def Test_star_command()
823 var lines =<< trim END
824 vim9script
825 @s = 'g:success = 8'
826 set cpo+=*
827 exe '*s'
828 assert_equal(8, g:success)
829 unlet g:success
830 set cpo-=*
831 assert_fails("exe '*s'", 'E1050:')
832 END
833 CheckScriptSuccess(lines)
834enddef
835
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100836def Test_cmd_argument_without_colon()
837 new Xfile
838 setline(1, ['a', 'b', 'c', 'd'])
839 write
840 edit +3 %
841 assert_equal(3, getcurpos()[1])
842 edit +/a %
843 assert_equal(1, getcurpos()[1])
844 bwipe
845 delete('Xfile')
846enddef
847
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100848def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100849 command Cmd1 eval 0
850 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100851 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100852 Cmd
853 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100854 CheckDefAndScriptFailure(lines, 'E464:', 1)
855 delcommand Cmd1
856 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100857enddef
858
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100859def Test_command_not_recognized()
860 var lines =<< trim END
861 d.key = 'asdf'
862 END
863 CheckDefFailure(lines, 'E1146:', 1)
864
865 lines =<< trim END
866 d['key'] = 'asdf'
867 END
868 CheckDefFailure(lines, 'E1146:', 1)
869enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200870
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100871def Test_magic_not_used()
872 new
873 for cmd in ['set magic', 'set nomagic']
874 exe cmd
875 setline(1, 'aaa')
876 s/.../bbb/
877 assert_equal('bbb', getline(1))
878 endfor
879
880 set magic
881 setline(1, 'aaa')
882 assert_fails('s/.\M../bbb/', 'E486:')
883 assert_fails('snomagic/.../bbb/', 'E486:')
884 assert_equal('aaa', getline(1))
885
886 bwipe!
887enddef
888
Bram Moolenaar60f63102020-12-21 20:32:43 +0100889def Test_gdefault_not_used()
890 new
891 for cmd in ['set gdefault', 'set nogdefault']
892 exe cmd
893 setline(1, 'aaa')
894 s/./b/
895 assert_equal('baa', getline(1))
896 endfor
897
898 set nogdefault
899 bwipe!
900enddef
901
Bram Moolenaar179eb562020-12-27 18:03:22 +0100902def g:SomeComplFunc(findstart: number, base: string): any
903 if findstart
904 return 0
905 else
906 return ['aaa', 'bbb']
907 endif
908enddef
909
910def Test_insert_complete()
911 # this was running into an error with the matchparen hack
912 new
913 set completefunc=SomeComplFunc
914 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
915 assert_equal('aaa', getline(1))
916
917 set completefunc=
918 bwipe!
919enddef
920
Bram Moolenaara11919f2021-01-02 19:44:56 +0100921def Test_wincmd()
922 split
923 var id1 = win_getid()
924 if true
925 try | wincmd w | catch | endtry
926 endif
927 assert_notequal(id1, win_getid())
928 close
929enddef
930
Bram Moolenaar9567efa2021-01-11 22:16:30 +0100931def Test_windo_missing_endif()
932 var lines =<< trim END
933 windo if 1
934 END
935 CheckDefExecFailure(lines, 'E171:', 1)
936enddef
937
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200938" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker