blob: 1e1a498bf69553eace039d3bd156f82c88fe5eef [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
Bram Moolenaar39f3b142021-02-14 12:57:36 +01008def Test_vim9cmd()
9 var lines =<< trim END
10 vim9cmd var x = 123
11 let s:y = 'yes'
12 vim9c assert_equal(123, x)
13 vim9cm assert_equal('yes', y)
14 END
15 CheckScriptSuccess(lines)
16enddef
17
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020018def Test_edit_wildcards()
Bram Moolenaarac564082020-09-27 19:05:33 +020019 var filename = 'Xtest'
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020020 edit `=filename`
21 assert_equal('Xtest', bufname())
22
Bram Moolenaarac564082020-09-27 19:05:33 +020023 var filenr = 123
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020024 edit Xtest`=filenr`
25 assert_equal('Xtest123', bufname())
26
27 filenr = 77
28 edit `=filename``=filenr`
29 assert_equal('Xtest77', bufname())
30
31 edit X`=filename`xx`=filenr`yy
32 assert_equal('XXtestxx77yy', bufname())
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010033
34 CheckDefFailure(['edit `=xxx`'], 'E1001:')
35 CheckDefFailure(['edit `="foo"'], 'E1083:')
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020036enddef
37
Bram Moolenaar4389f9c2020-12-27 16:55:11 +010038def Test_expand_alternate_file()
39 var lines =<< trim END
40 edit Xfileone
41 var bone = bufnr()
42 edit Xfiletwo
43 var btwo = bufnr()
44 edit Xfilethree
45 var bthree = bufnr()
46
47 edit #
48 assert_equal(bthree, bufnr())
49 edit %%
50 assert_equal(btwo, bufnr())
51 edit %% # comment
52 assert_equal(bthree, bufnr())
53 edit %%yy
54 assert_equal('Xfiletwoyy', bufname())
55
56 exe "edit %%" .. bone
57 assert_equal(bone, bufnr())
58 exe "edit %%" .. btwo .. "xx"
59 assert_equal('Xfiletwoxx', bufname())
60
61 next Xfileone Xfiletwo Xfilethree
62 assert_equal('Xfileone', argv(0))
63 assert_equal('Xfiletwo', argv(1))
64 assert_equal('Xfilethree', argv(2))
65 next %%%zz
66 assert_equal('Xfileone', argv(0))
67 assert_equal('Xfiletwo', argv(1))
68 assert_equal('Xfilethreezz', argv(2))
69
70 v:oldfiles = ['Xonefile', 'Xtwofile']
71 edit %%<1
72 assert_equal('Xonefile', bufname())
73 edit %%<2
74 assert_equal('Xtwofile', bufname())
75 assert_fails('edit %%<3', 'E684:')
76
77 edit Xfileone.vim
78 edit Xfiletwo
79 edit %%:r
80 assert_equal('Xfileone', bufname())
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +010081
82 assert_false(bufexists('altfoo'))
83 edit altfoo
84 edit bar
85 assert_true(bufexists('altfoo'))
86 assert_true(buflisted('altfoo'))
87 bdel %%
88 assert_true(bufexists('altfoo'))
89 assert_false(buflisted('altfoo'))
90 bwipe! altfoo
91 bwipe! bar
Bram Moolenaar4389f9c2020-12-27 16:55:11 +010092 END
93 CheckDefAndScriptSuccess(lines)
94enddef
95
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +010096def Test_global_backtick_expansion()
97 new
98 setline(1, 'xx')
99 var name = 'foobar'
100 g/^xx/s/.*/`=name`
101 assert_equal('foobar', getline(1))
102 bwipe!
103enddef
104
Bram Moolenaarecac5912021-01-05 19:23:28 +0100105def Test_folddo_backtick_expansion()
106 new
107 var name = 'xxx'
108 folddoopen edit `=name`
109 assert_equal('xxx', bufname())
110 bwipe!
111
112 new
113 setline(1, ['one', 'two'])
114 set nomodified
115 :1,2fold
116 foldclose
117 folddoclose edit `=name`
118 assert_equal('xxx', bufname())
119 bwipe!
120enddef
121
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200122def Test_hardcopy_wildcards()
123 CheckUnix
124 CheckFeature postscript
125
Bram Moolenaarac564082020-09-27 19:05:33 +0200126 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200127 hardcopy > X`=outfile`.ps
128 assert_true(filereadable('Xprint.ps'))
129
130 delete('Xprint.ps')
131enddef
132
133def Test_syn_include_wildcards()
134 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +0200135 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200136 &rtp = '.'
137
Bram Moolenaarac564082020-09-27 19:05:33 +0200138 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200139 syn include @Group Xthe`=fname`.vim
140 assert_match('Found.* contained found', execute('syn list Found'))
141
142 &rtp = save_rtp
143 delete('Xthemine.vim')
144enddef
145
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200146def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200147 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200148 vim9script
149 redir @a
150 echo 'one'
151 .. 'two'
152 redir END
153 assert_equal("\nonetwo", @a)
154 END
155 CheckScriptSuccess(lines)
156
157 lines =<< trim END
158 vim9script
159 redir @a
160 echo 11 +
161 77
162 - 22
163 redir END
164 assert_equal("\n66", @a)
165 END
166 CheckScriptSuccess(lines)
167enddef
168
Bram Moolenaar13106602020-10-04 16:06:05 +0200169def Test_condition_types()
170 var lines =<< trim END
171 if 'text'
172 endif
173 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100174 CheckDefAndScriptFailure(lines, 'E1135:', 1)
Bram Moolenaar13106602020-10-04 16:06:05 +0200175
176 lines =<< trim END
177 if [1]
178 endif
179 END
180 CheckDefFailure(lines, 'E1012:', 1)
181 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
182
183 lines =<< trim END
184 g:cond = 'text'
185 if g:cond
186 endif
187 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100188 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200189
190 lines =<< trim END
191 g:cond = 0
192 if g:cond
193 elseif 'text'
194 endif
195 END
196 CheckDefFailure(lines, 'E1012:', 3)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100197 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
Bram Moolenaar13106602020-10-04 16:06:05 +0200198
199 lines =<< trim END
200 if g:cond
201 elseif [1]
202 endif
203 END
204 CheckDefFailure(lines, 'E1012:', 2)
205 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
206
207 lines =<< trim END
208 g:cond = 'text'
209 if 0
210 elseif g:cond
211 endif
212 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100213 CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
Bram Moolenaar13106602020-10-04 16:06:05 +0200214
215 lines =<< trim END
216 while 'text'
217 endwhile
218 END
219 CheckDefFailure(lines, 'E1012:', 1)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100220 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200221
222 lines =<< trim END
223 while [1]
224 endwhile
225 END
226 CheckDefFailure(lines, 'E1012:', 1)
227 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
228
229 lines =<< trim END
230 g:cond = 'text'
231 while g:cond
232 endwhile
233 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100234 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200235enddef
236
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200237def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200238 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200239 vim9script
240 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200241 true
242 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200243 g:res = 42
244 endif
245 assert_equal(42, g:res)
246 END
247 CheckScriptSuccess(lines)
248 unlet g:res
249
250 lines =<< trim END
251 vim9script
252 if 1 &&
253 0
254 g:res = 0
255 elseif 0 ||
256 0
257 || 1
258 g:res = 12
259 endif
260 assert_equal(12, g:res)
261 END
262 CheckScriptSuccess(lines)
263 unlet g:res
264enddef
265
266def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200267 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200268 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200269 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200270 while nr <
271 10 + 3
272 nr = nr
273 + 4
274 endwhile
275 assert_equal(16, nr)
276 END
277 CheckScriptSuccess(lines)
278
279 lines =<< trim END
280 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200281 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200282 while nr
283 <
284 10
285 +
286 3
287 nr = nr
288 +
289 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200290 endwhile
291 assert_equal(16, nr)
292 END
293 CheckScriptSuccess(lines)
294enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200295
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200296def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200297 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200298 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200299 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200300 for x
301 in
302 [1, 2, 3, 4]
303 nr = nr + x
304 endfor
305 assert_equal(10, nr)
306 END
307 CheckScriptSuccess(lines)
308
309 lines =<< trim END
310 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200311 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200312 for x
313 in
314 [1, 2,
315 3, 4
316 ]
317 nr = nr
318 +
319 x
320 endfor
321 assert_equal(10, nr)
322 END
323 CheckScriptSuccess(lines)
324enddef
325
Bram Moolenaare0890d62021-02-17 14:52:14 +0100326def MethodAfterLinebreak(arg: string)
327 arg
328 ->setline(1)
329enddef
330
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200331def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200332 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200333 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200334 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200335 func RetArg(
336 arg
337 )
338 let s:res = a:arg
339 endfunc
340 [1,
341 2,
342 3]->RetArg()
343 assert_equal([1, 2, 3], res)
344 END
345 CheckScriptSuccess(lines)
Bram Moolenaar148be9b2021-02-02 21:33:52 +0100346
347 lines =<< trim END
348 new
349 var name = [1, 2]
350 name
351 ->copy()
352 ->setline(1)
353 assert_equal(['1', '2'], getline(1, 2))
354 bwipe!
355 END
356 CheckDefAndScriptSuccess(lines)
357
358 lines =<< trim END
359 new
Bram Moolenaar6914e872021-03-06 21:01:09 +0100360 def Foo(): string
361 return 'the text'
362 enddef
363 def Bar(F: func): string
364 return F()
365 enddef
366 def Test()
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100367 Foo ->Bar()
368 ->setline(1)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100369 enddef
370 Test()
371 assert_equal('the text', getline(1))
372 bwipe!
373 END
374 CheckDefAndScriptSuccess(lines)
375
376 lines =<< trim END
377 new
Bram Moolenaar148be9b2021-02-02 21:33:52 +0100378 g:shortlist
379 ->copy()
380 ->setline(1)
381 assert_equal(['1', '2'], getline(1, 2))
382 bwipe!
383 END
384 g:shortlist = [1, 2]
385 CheckDefAndScriptSuccess(lines)
386 unlet g:shortlist
Bram Moolenaare0890d62021-02-17 14:52:14 +0100387
388 new
389 MethodAfterLinebreak('foobar')
390 assert_equal('foobar', getline(1))
391 bwipe!
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100392
393 lines =<< trim END
394 vim9script
395 def Foo(): string
396 return '# some text'
397 enddef
398
399 def Bar(F: func): string
400 return F()
401 enddef
402
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100403 Foo->Bar()
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100404 ->setline(1)
405 END
406 CheckScriptSuccess(lines)
407 assert_equal('# some text', getline(1))
408 bwipe!
Bram Moolenaar5f195932020-07-01 20:07:14 +0200409enddef
410
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +0100411def Test_method_call_whitespace()
412 var lines =<< trim END
413 new
414 var yank = 'text'
415 yank->setline(1)
416 yank ->setline(2)
417 yank-> setline(3)
418 yank -> setline(4)
419 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
420 bwipe!
421 END
422 CheckDefAndScriptSuccess(lines)
423enddef
424
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100425def Test_method_and_user_command()
426 var lines =<< trim END
427 vim9script
428 def Cmd()
429 g:didFunc = 1
430 enddef
431 command Cmd g:didCmd = 1
432 Cmd
433 assert_equal(1, g:didCmd)
434 Cmd()
435 assert_equal(1, g:didFunc)
436 unlet g:didFunc
437 unlet g:didCmd
438
439 def InDefFunc()
440 Cmd
441 assert_equal(1, g:didCmd)
442 Cmd()
443 assert_equal(1, g:didFunc)
444 unlet g:didFunc
445 unlet g:didCmd
446 enddef
447 InDefFunc()
448 END
449 CheckScriptSuccess(lines)
450enddef
451
Bram Moolenaar683581e2020-10-22 21:22:58 +0200452def Test_skipped_expr_linebreak()
453 if 0
454 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100455 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200456 endif
457enddef
458
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200459def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100460 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200461 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100462 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200463 test.data
464 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100465 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200466
Bram Moolenaarac564082020-09-27 19:05:33 +0200467 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200468 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100469 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200470 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100471 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200472 END
473 CheckScriptSuccess(lines)
474enddef
475
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200476def Test_bar_after_command()
477 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200478 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200479 redraw | echo x
480 enddef
481 RedrawAndEcho()
482 assert_match('did redraw', Screenline(&lines))
483
Bram Moolenaar788123c2020-07-05 15:32:17 +0200484 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200485 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200486 reg_executing() | echo x
487 enddef
488 CallAndEcho()
489 assert_match('did redraw', Screenline(&lines))
490
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200491 if has('unix')
492 # bar in filter write command does not start new command
493 def WriteToShell()
494 new
495 setline(1, 'some text')
496 w !cat | cat > Xoutfile
497 bwipe!
498 enddef
499 WriteToShell()
500 assert_equal(['some text'], readfile('Xoutfile'))
501 delete('Xoutfile')
502
503 # bar in filter read command does not start new command
504 def ReadFromShell()
505 new
506 r! echo hello there | cat > Xoutfile
507 r !echo again | cat >> Xoutfile
508 bwipe!
509 enddef
510 ReadFromShell()
511 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
512 delete('Xoutfile')
513 endif
514enddef
515
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200516def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100517 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100518 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100519 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200520enddef
521
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100522def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100523 var lines =<< trim END
524 final expected = "\nType Name Content\n c \"c piyo"
525 @a = 'hoge'
526 @b = 'fuga'
527 @c = 'piyo'
528
529 assert_equal(execute('filter /piyo/ registers abc'), expected)
530 END
531 CheckDefAndScriptSuccess(lines)
532enddef
533
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100534def Test_win_command_modifiers()
535 assert_equal(1, winnr('$'))
536
537 set splitright
538 vsplit
539 assert_equal(2, winnr())
540 close
541 aboveleft vsplit
542 assert_equal(1, winnr())
543 close
544 set splitright&
545
546 vsplit
547 assert_equal(1, winnr())
548 close
549 belowright vsplit
550 assert_equal(2, winnr())
551 close
552 rightbelow vsplit
553 assert_equal(2, winnr())
554 close
555
Bram Moolenaar97a19002020-11-01 22:15:44 +0100556 if has('browse')
557 browse set
558 assert_equal('option-window', expand('%'))
559 close
560 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100561
562 vsplit
563 botright split
564 assert_equal(3, winnr())
565 assert_equal(&columns, winwidth(0))
566 close
567 close
568
569 vsplit
570 topleft split
571 assert_equal(1, winnr())
572 assert_equal(&columns, winwidth(0))
573 close
574 close
575
576 gettabinfo()->len()->assert_equal(1)
577 tab split
578 gettabinfo()->len()->assert_equal(2)
579 tabclose
580
581 vertical new
582 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
583 close
584enddef
585
586func Test_command_modifier_confirm()
587 CheckNotGui
588 CheckRunVimInTerminal
589
590 " Test for saving all the modified buffers
591 let lines =<< trim END
592 call setline(1, 'changed')
593 def Getout()
594 confirm write Xfile
595 enddef
596 END
597 call writefile(lines, 'Xconfirmscript')
598 call writefile(['empty'], 'Xfile')
599 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
600 call term_sendkeys(buf, ":call Getout()\n")
601 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
602 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100603 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
604 call term_sendkeys(buf, "\<CR>")
605 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100606 call StopVimInTerminal(buf)
607
608 call assert_equal(['changed'], readfile('Xfile'))
609 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100610 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100611 call delete('Xconfirmscript')
612endfunc
613
614def Test_command_modifiers_keep()
615 if has('unix')
616 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
617 new
618 setline(1, ['one', 'two', 'three'])
619 normal 1Gma
620 normal 2Gmb
621 normal 3Gmc
622 if addRflag
623 set cpo+=R
624 else
625 set cpo-=R
626 endif
627 if keepMarks
628 keepmarks :%!cat
629 else
630 :%!cat
631 endif
632 if hasMarks
633 assert_equal(1, line("'a"))
634 assert_equal(2, line("'b"))
635 assert_equal(3, line("'c"))
636 else
637 assert_equal(0, line("'a"))
638 assert_equal(0, line("'b"))
639 assert_equal(0, line("'c"))
640 endif
641 quit!
642 enddef
643 DoTest(false, false, true)
644 DoTest(true, false, false)
645 DoTest(false, true, true)
646 DoTest(true, true, true)
647 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100648
649 new
650 setline(1, ['one', 'two', 'three', 'four'])
651 assert_equal(4, line("$"))
652 normal 1Gma
653 normal 2Gmb
654 normal 3Gmc
655 lockmarks :1,2!wc
656 # line is deleted, marks don't move
657 assert_equal(3, line("$"))
658 assert_equal('four', getline(3))
659 assert_equal(1, line("'a"))
660 assert_equal(2, line("'b"))
661 assert_equal(3, line("'c"))
662 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100663 endif
664
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100665 edit Xone
666 edit Xtwo
667 assert_equal('Xone', expand('#'))
668 keepalt edit Xthree
669 assert_equal('Xone', expand('#'))
670
671 normal /a*b*
672 assert_equal('a*b*', histget("search"))
673 keeppatterns normal /c*d*
674 assert_equal('a*b*', histget("search"))
675
676 new
677 setline(1, range(10))
678 :10
679 normal gg
680 assert_equal(10, getpos("''")[1])
681 keepjumps normal 5G
682 assert_equal(10, getpos("''")[1])
683 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100684enddef
685
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100686def Test_bar_line_continuation()
687 var lines =<< trim END
688 au BufNewFile Xfile g:readFile = 1
689 | g:readExtra = 2
690 g:readFile = 0
691 g:readExtra = 0
692 edit Xfile
693 assert_equal(1, g:readFile)
694 assert_equal(2, g:readExtra)
695 bwipe!
696 au! BufNewFile
697
698 au BufNewFile Xfile g:readFile = 1
699 | g:readExtra = 2
700 | g:readMore = 3
701 g:readFile = 0
702 g:readExtra = 0
703 g:readMore = 0
704 edit Xfile
705 assert_equal(1, g:readFile)
706 assert_equal(2, g:readExtra)
707 assert_equal(3, g:readMore)
708 bwipe!
709 au! BufNewFile
710 unlet g:readFile
711 unlet g:readExtra
712 unlet g:readMore
713 END
714 CheckDefAndScriptSuccess(lines)
715enddef
716
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100717def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100718 new Xsomefile
719 setline(1, 'changed')
720 var buf = bufnr()
721 hide edit Xotherfile
722 var info = getbufinfo(buf)
723 assert_equal(1, info[0].hidden)
724 assert_equal(1, info[0].changed)
725 edit Xsomefile
726 bwipe!
727
728 au BufNewFile Xfile g:readFile = 1
729 g:readFile = 0
730 edit Xfile
731 assert_equal(1, g:readFile)
732 bwipe!
733 g:readFile = 0
734 noautocmd edit Xfile
735 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100736 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100737 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100738
739 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100740 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100741 bwipe!
742
743 var caught = false
744 try
745 sandbox !ls
746 catch /E48:/
747 caught = true
748 endtry
749 assert_true(caught)
750
751 :8verbose g:verbose_now = &verbose
752 assert_equal(8, g:verbose_now)
753 unlet g:verbose_now
754enddef
755
756def EchoHere()
757 echomsg 'here'
758enddef
759def EchoThere()
760 unsilent echomsg 'there'
761enddef
762
763def Test_modifier_silent_unsilent()
764 echomsg 'last one'
765 silent echomsg "text"
766 assert_equal("\nlast one", execute(':1messages'))
767
768 silent! echoerr "error"
769
770 echomsg 'last one'
771 silent EchoHere()
772 assert_equal("\nlast one", execute(':1messages'))
773
774 silent EchoThere()
775 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100776
777 try
778 silent eval [][0]
779 catch
780 echomsg "caught"
781 endtry
782 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100783enddef
784
Bram Moolenaar36113e42020-11-02 21:08:47 +0100785def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100786 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100787 new
788 setline(1, 'xxx')
789 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
790 assert_equal('', getline(1))
791 bwipe!
792enddef
793
Bram Moolenaarece0b872021-01-08 20:40:45 +0100794def Test_silent_pattern()
795 new
796 silent! :/pat/put _
797 bwipe!
798enddef
799
Bram Moolenaarfa984412021-03-25 22:15:28 +0100800def Test_useless_command_modifier()
801 g:maybe = true
802 var lines =<< trim END
803 if g:maybe
804 silent endif
805 END
806 CheckDefAndScriptFailure(lines, 'E1176:', 2)
807
808 lines =<< trim END
809 for i in [0]
810 silent endfor
811 END
812 CheckDefAndScriptFailure(lines, 'E1176:', 2)
813
814 lines =<< trim END
815 while g:maybe
816 silent endwhile
817 END
818 CheckDefAndScriptFailure(lines, 'E1176:', 2)
819
820 lines =<< trim END
821 silent try
822 finally
823 endtry
824 END
825 CheckDefAndScriptFailure(lines, 'E1176:', 1)
826
827 lines =<< trim END
828 try
829 silent catch
830 endtry
831 END
832 CheckDefAndScriptFailure(lines, 'E1176:', 2)
833
834 lines =<< trim END
835 try
836 silent finally
837 endtry
838 END
839 CheckDefAndScriptFailure(lines, 'E1176:', 2)
840
841 lines =<< trim END
842 try
843 finally
844 silent endtry
845 END
846 CheckDefAndScriptFailure(lines, 'E1176:', 3)
847enddef
848
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200849def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200850 var from = 3
851 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200852 g:val = 111
853 def Increment(nrs: list<number>)
854 for nr in nrs
855 g:val += nr
856 endfor
857 enddef
858 eval range(from, to)
859 ->Increment()
860 assert_equal(111 + 3 + 4 + 5, g:val)
861 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100862
863 var lines =<< trim END
864 vim9script
865 g:caught = 'no'
866 try
867 eval 123 || 0
868 catch
869 g:caught = 'yes'
870 endtry
871 assert_equal('yes', g:caught)
872 unlet g:caught
873 END
874 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200875enddef
876
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200877def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200878 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200879 nnoremap <F3> :echo 'hit F3 #'<CR>
880 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
881 END
882 CheckDefSuccess(lines)
883 CheckScriptSuccess(['vim9script'] + lines)
884enddef
885
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200886def Test_normal_command()
887 new
888 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200889 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200890 try
891 exe "norm! \<C-]>"
892 catch /E433/
893 caught = 2
894 endtry
895 assert_equal(2, caught)
896
897 try
898 exe "norm! 3\<C-]>"
899 catch /E433/
900 caught = 3
901 endtry
902 assert_equal(3, caught)
903 bwipe!
904enddef
905
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200906def Test_put_command()
907 new
908 @p = 'ppp'
909 put p
910 assert_equal('ppp', getline(2))
911
912 put ='below'
913 assert_equal('below', getline(3))
914 put! ='above'
915 assert_equal('above', getline(3))
916 assert_equal('below', getline(4))
917
Bram Moolenaar883cf972021-01-15 18:04:43 +0100918 :2put =['a', 'b', 'c']
919 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
920
Bram Moolenaar08597872020-12-10 19:43:40 +0100921 # compute range at runtime
922 setline(1, range(1, 8))
923 @a = 'aaa'
924 :$-2put a
925 assert_equal('aaa', getline(7))
926
927 setline(1, range(1, 8))
928 :2
929 :+2put! a
930 assert_equal('aaa', getline(4))
931
Bram Moolenaara28639e2021-01-19 22:48:09 +0100932 []->mapnew(() => 0)
933 :$put ='end'
934 assert_equal('end', getline('$'))
935
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200936 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100937
938 CheckDefFailure(['put =xxx'], 'E1001:')
939enddef
940
941def Test_put_with_linebreak()
942 new
943 var lines =<< trim END
944 vim9script
945 pu =split('abc', '\zs')
946 ->join()
947 END
948 CheckScriptSuccess(lines)
949 getline(2)->assert_equal('a b c')
950 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200951enddef
952
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200953def Test_command_star_range()
954 new
955 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
956 setpos("'<", [0, 1, 0, 0])
957 setpos("'>", [0, 3, 0, 0])
958 :*s/\(foo\|bar\)/baz/g
959 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
960
961 bwipe!
962enddef
963
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200964def Test_f_args()
965 var lines =<< trim END
966 vim9script
967
968 func SaveCmdArgs(...)
969 let g:args = a:000
970 endfunc
971
972 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
973
974 TestFArgs
975 assert_equal([], g:args)
976
977 TestFArgs one two three
978 assert_equal(['one', 'two', 'three'], g:args)
979 END
980 CheckScriptSuccess(lines)
981enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200982
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100983def Test_user_command_comment()
984 command -nargs=1 Comd echom <q-args>
985
986 var lines =<< trim END
Bram Moolenaarb98cec22021-04-25 16:35:55 +0200987 vim9script
988 Comd # comment
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100989 END
990 CheckScriptSuccess(lines)
991
992 lines =<< trim END
Bram Moolenaarb98cec22021-04-25 16:35:55 +0200993 vim9script
994 Comd# comment
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100995 END
996 CheckScriptFailure(lines, 'E1144:')
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100997 delcommand Comd
Bram Moolenaarb98cec22021-04-25 16:35:55 +0200998
999 lines =<< trim END
1000 vim9script
1001 command Foo echo 'Foo'
1002 Foo3Bar
1003 END
1004 CheckScriptFailure(lines, 'E1144: Command "Foo" is not followed by white space: Foo3Bar')
1005
1006 delcommand Foo
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001007enddef
1008
Bram Moolenaar95388e32020-11-20 21:07:00 +01001009def Test_star_command()
1010 var lines =<< trim END
1011 vim9script
1012 @s = 'g:success = 8'
1013 set cpo+=*
1014 exe '*s'
1015 assert_equal(8, g:success)
1016 unlet g:success
1017 set cpo-=*
1018 assert_fails("exe '*s'", 'E1050:')
1019 END
1020 CheckScriptSuccess(lines)
1021enddef
1022
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001023def Test_cmd_argument_without_colon()
1024 new Xfile
1025 setline(1, ['a', 'b', 'c', 'd'])
1026 write
1027 edit +3 %
1028 assert_equal(3, getcurpos()[1])
1029 edit +/a %
1030 assert_equal(1, getcurpos()[1])
1031 bwipe
1032 delete('Xfile')
1033enddef
1034
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001035def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001036 command Cmd1 eval 0
1037 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001038 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001039 Cmd
1040 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001041 CheckDefAndScriptFailure(lines, 'E464:', 1)
1042 delcommand Cmd1
1043 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001044enddef
1045
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001046def Test_command_not_recognized()
1047 var lines =<< trim END
1048 d.key = 'asdf'
1049 END
1050 CheckDefFailure(lines, 'E1146:', 1)
1051
1052 lines =<< trim END
1053 d['key'] = 'asdf'
1054 END
1055 CheckDefFailure(lines, 'E1146:', 1)
1056enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001057
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001058def Test_magic_not_used()
1059 new
1060 for cmd in ['set magic', 'set nomagic']
1061 exe cmd
1062 setline(1, 'aaa')
1063 s/.../bbb/
1064 assert_equal('bbb', getline(1))
1065 endfor
1066
1067 set magic
1068 setline(1, 'aaa')
1069 assert_fails('s/.\M../bbb/', 'E486:')
1070 assert_fails('snomagic/.../bbb/', 'E486:')
1071 assert_equal('aaa', getline(1))
1072
1073 bwipe!
1074enddef
1075
Bram Moolenaar60f63102020-12-21 20:32:43 +01001076def Test_gdefault_not_used()
1077 new
1078 for cmd in ['set gdefault', 'set nogdefault']
1079 exe cmd
1080 setline(1, 'aaa')
1081 s/./b/
1082 assert_equal('baa', getline(1))
1083 endfor
1084
1085 set nogdefault
1086 bwipe!
1087enddef
1088
Bram Moolenaar179eb562020-12-27 18:03:22 +01001089def g:SomeComplFunc(findstart: number, base: string): any
1090 if findstart
1091 return 0
1092 else
1093 return ['aaa', 'bbb']
1094 endif
1095enddef
1096
1097def Test_insert_complete()
1098 # this was running into an error with the matchparen hack
1099 new
1100 set completefunc=SomeComplFunc
1101 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
1102 assert_equal('aaa', getline(1))
1103
1104 set completefunc=
1105 bwipe!
1106enddef
1107
Bram Moolenaara11919f2021-01-02 19:44:56 +01001108def Test_wincmd()
1109 split
1110 var id1 = win_getid()
1111 if true
1112 try | wincmd w | catch | endtry
1113 endif
1114 assert_notequal(id1, win_getid())
1115 close
Bram Moolenaar1ff89de2021-03-24 20:08:12 +01001116
1117 split
1118 var id = win_getid()
1119 split
1120 :2wincmd o
1121 assert_equal(id, win_getid())
1122 only
1123
1124 split
1125 split
1126 assert_equal(3, winnr('$'))
1127 :2wincmd c
1128 assert_equal(2, winnr('$'))
1129 only
1130
1131 split
1132 split
1133 assert_equal(3, winnr('$'))
1134 :2wincmd q
1135 assert_equal(2, winnr('$'))
1136 only
Bram Moolenaara11919f2021-01-02 19:44:56 +01001137enddef
1138
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001139def Test_windo_missing_endif()
1140 var lines =<< trim END
1141 windo if 1
1142 END
1143 CheckDefExecFailure(lines, 'E171:', 1)
1144enddef
1145
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02001146let s:theList = [1, 2, 3]
1147
1148def Test_lockvar()
1149 s:theList[1] = 22
1150 assert_equal([1, 22, 3], s:theList)
1151 lockvar s:theList
1152 assert_fails('theList[1] = 77', 'E741:')
1153 unlockvar s:theList
1154 s:theList[1] = 44
1155 assert_equal([1, 44, 3], s:theList)
1156
1157 var lines =<< trim END
1158 vim9script
1159 var theList = [1, 2, 3]
1160 def SetList()
1161 theList[1] = 22
1162 assert_equal([1, 22, 3], theList)
1163 lockvar theList
1164 theList[1] = 77
1165 enddef
1166 SetList()
1167 END
1168 CheckScriptFailure(lines, 'E1119', 4)
1169
1170 lines =<< trim END
1171 var theList = [1, 2, 3]
1172 lockvar theList
1173 END
1174 CheckDefFailure(lines, 'E1178', 2)
1175
1176 lines =<< trim END
1177 var theList = [1, 2, 3]
1178 unlockvar theList
1179 END
1180 CheckDefFailure(lines, 'E1178', 2)
1181enddef
1182
Bram Moolenaar4c137212021-04-19 16:48:48 +02001183def Test_substitute_expr()
1184 var to = 'repl'
1185 new
1186 setline(1, 'one from two')
1187 s/from/\=to
1188 assert_equal('one repl two', getline(1))
1189
1190 setline(1, 'one from two')
1191 s/from/\=to .. '_x'
1192 assert_equal('one repl_x two', getline(1))
1193
1194 setline(1, 'one from two from three')
1195 var also = 'also'
1196 s/from/\=to .. '_' .. also/g#e
1197 assert_equal('one repl_also two repl_also three', getline(1))
1198
Bram Moolenaar8238f082021-04-20 21:10:48 +02001199 setline(1, 'abc abc abc')
1200 for choice in [true, false]
1201 :1s/abc/\=choice ? 'yes' : 'no'/
1202 endfor
1203 assert_equal('yes no abc', getline(1))
1204
Bram Moolenaard386e922021-04-25 14:48:49 +02001205 bwipe!
1206
Bram Moolenaar4c137212021-04-19 16:48:48 +02001207 CheckDefFailure(['s/from/\="x")/'], 'E488:')
1208 CheckDefFailure(['s/from/\="x"/9'], 'E488:')
1209
Bram Moolenaard386e922021-04-25 14:48:49 +02001210 # When calling a function the right instruction list needs to be restored.
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001211 g:cond = true
Bram Moolenaard386e922021-04-25 14:48:49 +02001212 var lines =<< trim END
1213 vim9script
1214 def Foo()
1215 Bar([])
1216 enddef
1217 def Bar(l: list<number>)
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001218 if g:cond
Bram Moolenaard386e922021-04-25 14:48:49 +02001219 s/^/\=Rep()/
1220 for n in l[:]
1221 endfor
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001222 endif
Bram Moolenaard386e922021-04-25 14:48:49 +02001223 enddef
1224 def Rep(): string
1225 return 'rep'
1226 enddef
1227 new
1228 Foo()
1229 assert_equal('rep', getline(1))
1230 bwipe!
1231 END
1232 CheckScriptSuccess(lines)
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001233 unlet g:cond
Bram Moolenaar4c137212021-04-19 16:48:48 +02001234enddef
1235
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001236def Test_redir_to_var()
1237 var result: string
1238 redir => result
1239 echo 'something'
1240 redir END
1241 assert_equal("\nsomething", result)
1242
1243 redir =>> result
1244 echo 'more'
1245 redir END
1246 assert_equal("\nsomething\nmore", result)
1247
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001248 var d: dict<string>
1249 redir => d.redir
1250 echo 'dict'
1251 redir END
1252 assert_equal({redir: "\ndict"}, d)
1253
1254 var l = ['a', 'b', 'c']
1255 redir => l[1]
1256 echo 'list'
1257 redir END
1258 assert_equal(['a', "\nlist", 'c'], l)
1259
1260 var dl = {l: ['x']}
1261 redir => dl.l[0]
1262 echo 'dict-list'
1263 redir END
1264 assert_equal({l: ["\ndict-list"]}, dl)
1265
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001266 redir =>> d.redir
1267 echo 'more'
1268 redir END
1269 assert_equal({redir: "\ndict\nmore"}, d)
1270
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001271 var lines =<< trim END
1272 redir => notexist
1273 END
1274 CheckDefFailure(lines, 'E1089:')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001275
1276 lines =<< trim END
1277 var ls = 'asdf'
1278 redir => ls[1]
1279 redir END
1280 END
1281 CheckDefFailure(lines, 'E1141:')
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001282enddef
1283
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02001284
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001285" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker