blob: 735f48807d81bad3aa3db54fe74adc63e3abbee4 [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 Moolenaar7cebe8b2021-01-23 14:22:16 +0100322def Test_method_call_whitespace()
323 var lines =<< trim END
324 new
325 var yank = 'text'
326 yank->setline(1)
327 yank ->setline(2)
328 yank-> setline(3)
329 yank -> setline(4)
330 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
331 bwipe!
332 END
333 CheckDefAndScriptSuccess(lines)
334enddef
335
Bram Moolenaar683581e2020-10-22 21:22:58 +0200336def Test_skipped_expr_linebreak()
337 if 0
338 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100339 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200340 endif
341enddef
342
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200343def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100344 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200345 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100346 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200347 test.data
348 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100349 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200350
Bram Moolenaarac564082020-09-27 19:05:33 +0200351 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200352 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100353 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200354 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100355 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200356 END
357 CheckScriptSuccess(lines)
358enddef
359
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200360def Test_bar_after_command()
361 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200362 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200363 redraw | echo x
364 enddef
365 RedrawAndEcho()
366 assert_match('did redraw', Screenline(&lines))
367
Bram Moolenaar788123c2020-07-05 15:32:17 +0200368 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200369 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200370 reg_executing() | echo x
371 enddef
372 CallAndEcho()
373 assert_match('did redraw', Screenline(&lines))
374
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200375 if has('unix')
376 # bar in filter write command does not start new command
377 def WriteToShell()
378 new
379 setline(1, 'some text')
380 w !cat | cat > Xoutfile
381 bwipe!
382 enddef
383 WriteToShell()
384 assert_equal(['some text'], readfile('Xoutfile'))
385 delete('Xoutfile')
386
387 # bar in filter read command does not start new command
388 def ReadFromShell()
389 new
390 r! echo hello there | cat > Xoutfile
391 r !echo again | cat >> Xoutfile
392 bwipe!
393 enddef
394 ReadFromShell()
395 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
396 delete('Xoutfile')
397 endif
398enddef
399
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200400def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100401 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100402 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100403 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200404enddef
405
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100406def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100407 var lines =<< trim END
408 final expected = "\nType Name Content\n c \"c piyo"
409 @a = 'hoge'
410 @b = 'fuga'
411 @c = 'piyo'
412
413 assert_equal(execute('filter /piyo/ registers abc'), expected)
414 END
415 CheckDefAndScriptSuccess(lines)
416enddef
417
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100418def Test_win_command_modifiers()
419 assert_equal(1, winnr('$'))
420
421 set splitright
422 vsplit
423 assert_equal(2, winnr())
424 close
425 aboveleft vsplit
426 assert_equal(1, winnr())
427 close
428 set splitright&
429
430 vsplit
431 assert_equal(1, winnr())
432 close
433 belowright vsplit
434 assert_equal(2, winnr())
435 close
436 rightbelow vsplit
437 assert_equal(2, winnr())
438 close
439
Bram Moolenaar97a19002020-11-01 22:15:44 +0100440 if has('browse')
441 browse set
442 assert_equal('option-window', expand('%'))
443 close
444 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100445
446 vsplit
447 botright split
448 assert_equal(3, winnr())
449 assert_equal(&columns, winwidth(0))
450 close
451 close
452
453 vsplit
454 topleft split
455 assert_equal(1, winnr())
456 assert_equal(&columns, winwidth(0))
457 close
458 close
459
460 gettabinfo()->len()->assert_equal(1)
461 tab split
462 gettabinfo()->len()->assert_equal(2)
463 tabclose
464
465 vertical new
466 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
467 close
468enddef
469
470func Test_command_modifier_confirm()
471 CheckNotGui
472 CheckRunVimInTerminal
473
474 " Test for saving all the modified buffers
475 let lines =<< trim END
476 call setline(1, 'changed')
477 def Getout()
478 confirm write Xfile
479 enddef
480 END
481 call writefile(lines, 'Xconfirmscript')
482 call writefile(['empty'], 'Xfile')
483 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
484 call term_sendkeys(buf, ":call Getout()\n")
485 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
486 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100487 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
488 call term_sendkeys(buf, "\<CR>")
489 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100490 call StopVimInTerminal(buf)
491
492 call assert_equal(['changed'], readfile('Xfile'))
493 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100494 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100495 call delete('Xconfirmscript')
496endfunc
497
498def Test_command_modifiers_keep()
499 if has('unix')
500 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
501 new
502 setline(1, ['one', 'two', 'three'])
503 normal 1Gma
504 normal 2Gmb
505 normal 3Gmc
506 if addRflag
507 set cpo+=R
508 else
509 set cpo-=R
510 endif
511 if keepMarks
512 keepmarks :%!cat
513 else
514 :%!cat
515 endif
516 if hasMarks
517 assert_equal(1, line("'a"))
518 assert_equal(2, line("'b"))
519 assert_equal(3, line("'c"))
520 else
521 assert_equal(0, line("'a"))
522 assert_equal(0, line("'b"))
523 assert_equal(0, line("'c"))
524 endif
525 quit!
526 enddef
527 DoTest(false, false, true)
528 DoTest(true, false, false)
529 DoTest(false, true, true)
530 DoTest(true, true, true)
531 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100532
533 new
534 setline(1, ['one', 'two', 'three', 'four'])
535 assert_equal(4, line("$"))
536 normal 1Gma
537 normal 2Gmb
538 normal 3Gmc
539 lockmarks :1,2!wc
540 # line is deleted, marks don't move
541 assert_equal(3, line("$"))
542 assert_equal('four', getline(3))
543 assert_equal(1, line("'a"))
544 assert_equal(2, line("'b"))
545 assert_equal(3, line("'c"))
546 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100547 endif
548
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100549 edit Xone
550 edit Xtwo
551 assert_equal('Xone', expand('#'))
552 keepalt edit Xthree
553 assert_equal('Xone', expand('#'))
554
555 normal /a*b*
556 assert_equal('a*b*', histget("search"))
557 keeppatterns normal /c*d*
558 assert_equal('a*b*', histget("search"))
559
560 new
561 setline(1, range(10))
562 :10
563 normal gg
564 assert_equal(10, getpos("''")[1])
565 keepjumps normal 5G
566 assert_equal(10, getpos("''")[1])
567 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100568enddef
569
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100570def Test_bar_line_continuation()
571 var lines =<< trim END
572 au BufNewFile Xfile g:readFile = 1
573 | g:readExtra = 2
574 g:readFile = 0
575 g:readExtra = 0
576 edit Xfile
577 assert_equal(1, g:readFile)
578 assert_equal(2, g:readExtra)
579 bwipe!
580 au! BufNewFile
581
582 au BufNewFile Xfile g:readFile = 1
583 | g:readExtra = 2
584 | g:readMore = 3
585 g:readFile = 0
586 g:readExtra = 0
587 g:readMore = 0
588 edit Xfile
589 assert_equal(1, g:readFile)
590 assert_equal(2, g:readExtra)
591 assert_equal(3, g:readMore)
592 bwipe!
593 au! BufNewFile
594 unlet g:readFile
595 unlet g:readExtra
596 unlet g:readMore
597 END
598 CheckDefAndScriptSuccess(lines)
599enddef
600
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100601def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100602 new Xsomefile
603 setline(1, 'changed')
604 var buf = bufnr()
605 hide edit Xotherfile
606 var info = getbufinfo(buf)
607 assert_equal(1, info[0].hidden)
608 assert_equal(1, info[0].changed)
609 edit Xsomefile
610 bwipe!
611
612 au BufNewFile Xfile g:readFile = 1
613 g:readFile = 0
614 edit Xfile
615 assert_equal(1, g:readFile)
616 bwipe!
617 g:readFile = 0
618 noautocmd edit Xfile
619 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100620 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100621 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100622
623 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100624 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100625 bwipe!
626
627 var caught = false
628 try
629 sandbox !ls
630 catch /E48:/
631 caught = true
632 endtry
633 assert_true(caught)
634
635 :8verbose g:verbose_now = &verbose
636 assert_equal(8, g:verbose_now)
637 unlet g:verbose_now
638enddef
639
640def EchoHere()
641 echomsg 'here'
642enddef
643def EchoThere()
644 unsilent echomsg 'there'
645enddef
646
647def Test_modifier_silent_unsilent()
648 echomsg 'last one'
649 silent echomsg "text"
650 assert_equal("\nlast one", execute(':1messages'))
651
652 silent! echoerr "error"
653
654 echomsg 'last one'
655 silent EchoHere()
656 assert_equal("\nlast one", execute(':1messages'))
657
658 silent EchoThere()
659 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100660
661 try
662 silent eval [][0]
663 catch
664 echomsg "caught"
665 endtry
666 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100667enddef
668
Bram Moolenaar36113e42020-11-02 21:08:47 +0100669def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100670 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100671 new
672 setline(1, 'xxx')
673 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
674 assert_equal('', getline(1))
675 bwipe!
676enddef
677
Bram Moolenaarece0b872021-01-08 20:40:45 +0100678def Test_silent_pattern()
679 new
680 silent! :/pat/put _
681 bwipe!
682enddef
683
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200684def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200685 var from = 3
686 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200687 g:val = 111
688 def Increment(nrs: list<number>)
689 for nr in nrs
690 g:val += nr
691 endfor
692 enddef
693 eval range(from, to)
694 ->Increment()
695 assert_equal(111 + 3 + 4 + 5, g:val)
696 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100697
698 var lines =<< trim END
699 vim9script
700 g:caught = 'no'
701 try
702 eval 123 || 0
703 catch
704 g:caught = 'yes'
705 endtry
706 assert_equal('yes', g:caught)
707 unlet g:caught
708 END
709 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200710enddef
711
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200712def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200713 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200714 nnoremap <F3> :echo 'hit F3 #'<CR>
715 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
716 END
717 CheckDefSuccess(lines)
718 CheckScriptSuccess(['vim9script'] + lines)
719enddef
720
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200721def Test_normal_command()
722 new
723 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200724 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200725 try
726 exe "norm! \<C-]>"
727 catch /E433/
728 caught = 2
729 endtry
730 assert_equal(2, caught)
731
732 try
733 exe "norm! 3\<C-]>"
734 catch /E433/
735 caught = 3
736 endtry
737 assert_equal(3, caught)
738 bwipe!
739enddef
740
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200741def Test_put_command()
742 new
743 @p = 'ppp'
744 put p
745 assert_equal('ppp', getline(2))
746
747 put ='below'
748 assert_equal('below', getline(3))
749 put! ='above'
750 assert_equal('above', getline(3))
751 assert_equal('below', getline(4))
752
Bram Moolenaar883cf972021-01-15 18:04:43 +0100753 :2put =['a', 'b', 'c']
754 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
755
Bram Moolenaar08597872020-12-10 19:43:40 +0100756 # compute range at runtime
757 setline(1, range(1, 8))
758 @a = 'aaa'
759 :$-2put a
760 assert_equal('aaa', getline(7))
761
762 setline(1, range(1, 8))
763 :2
764 :+2put! a
765 assert_equal('aaa', getline(4))
766
Bram Moolenaara28639e2021-01-19 22:48:09 +0100767 []->mapnew(() => 0)
768 :$put ='end'
769 assert_equal('end', getline('$'))
770
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200771 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100772
773 CheckDefFailure(['put =xxx'], 'E1001:')
774enddef
775
776def Test_put_with_linebreak()
777 new
778 var lines =<< trim END
779 vim9script
780 pu =split('abc', '\zs')
781 ->join()
782 END
783 CheckScriptSuccess(lines)
784 getline(2)->assert_equal('a b c')
785 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200786enddef
787
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200788def Test_command_star_range()
789 new
790 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
791 setpos("'<", [0, 1, 0, 0])
792 setpos("'>", [0, 3, 0, 0])
793 :*s/\(foo\|bar\)/baz/g
794 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
795
796 bwipe!
797enddef
798
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200799def Test_f_args()
800 var lines =<< trim END
801 vim9script
802
803 func SaveCmdArgs(...)
804 let g:args = a:000
805 endfunc
806
807 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
808
809 TestFArgs
810 assert_equal([], g:args)
811
812 TestFArgs one two three
813 assert_equal(['one', 'two', 'three'], g:args)
814 END
815 CheckScriptSuccess(lines)
816enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200817
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100818def Test_user_command_comment()
819 command -nargs=1 Comd echom <q-args>
820
821 var lines =<< trim END
822 vim9script
823 Comd # comment
824 END
825 CheckScriptSuccess(lines)
826
827 lines =<< trim END
828 vim9script
829 Comd# comment
830 END
831 CheckScriptFailure(lines, 'E1144:')
832
833 delcommand Comd
834enddef
835
Bram Moolenaar95388e32020-11-20 21:07:00 +0100836def Test_star_command()
837 var lines =<< trim END
838 vim9script
839 @s = 'g:success = 8'
840 set cpo+=*
841 exe '*s'
842 assert_equal(8, g:success)
843 unlet g:success
844 set cpo-=*
845 assert_fails("exe '*s'", 'E1050:')
846 END
847 CheckScriptSuccess(lines)
848enddef
849
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100850def Test_cmd_argument_without_colon()
851 new Xfile
852 setline(1, ['a', 'b', 'c', 'd'])
853 write
854 edit +3 %
855 assert_equal(3, getcurpos()[1])
856 edit +/a %
857 assert_equal(1, getcurpos()[1])
858 bwipe
859 delete('Xfile')
860enddef
861
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100862def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100863 command Cmd1 eval 0
864 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100865 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100866 Cmd
867 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100868 CheckDefAndScriptFailure(lines, 'E464:', 1)
869 delcommand Cmd1
870 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100871enddef
872
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100873def Test_command_not_recognized()
874 var lines =<< trim END
875 d.key = 'asdf'
876 END
877 CheckDefFailure(lines, 'E1146:', 1)
878
879 lines =<< trim END
880 d['key'] = 'asdf'
881 END
882 CheckDefFailure(lines, 'E1146:', 1)
883enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200884
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100885def Test_magic_not_used()
886 new
887 for cmd in ['set magic', 'set nomagic']
888 exe cmd
889 setline(1, 'aaa')
890 s/.../bbb/
891 assert_equal('bbb', getline(1))
892 endfor
893
894 set magic
895 setline(1, 'aaa')
896 assert_fails('s/.\M../bbb/', 'E486:')
897 assert_fails('snomagic/.../bbb/', 'E486:')
898 assert_equal('aaa', getline(1))
899
900 bwipe!
901enddef
902
Bram Moolenaar60f63102020-12-21 20:32:43 +0100903def Test_gdefault_not_used()
904 new
905 for cmd in ['set gdefault', 'set nogdefault']
906 exe cmd
907 setline(1, 'aaa')
908 s/./b/
909 assert_equal('baa', getline(1))
910 endfor
911
912 set nogdefault
913 bwipe!
914enddef
915
Bram Moolenaar179eb562020-12-27 18:03:22 +0100916def g:SomeComplFunc(findstart: number, base: string): any
917 if findstart
918 return 0
919 else
920 return ['aaa', 'bbb']
921 endif
922enddef
923
924def Test_insert_complete()
925 # this was running into an error with the matchparen hack
926 new
927 set completefunc=SomeComplFunc
928 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
929 assert_equal('aaa', getline(1))
930
931 set completefunc=
932 bwipe!
933enddef
934
Bram Moolenaara11919f2021-01-02 19:44:56 +0100935def Test_wincmd()
936 split
937 var id1 = win_getid()
938 if true
939 try | wincmd w | catch | endtry
940 endif
941 assert_notequal(id1, win_getid())
942 close
943enddef
944
Bram Moolenaar9567efa2021-01-11 22:16:30 +0100945def Test_windo_missing_endif()
946 var lines =<< trim END
947 windo if 1
948 END
949 CheckDefExecFailure(lines, 'E171:', 1)
950enddef
951
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200952" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker