blob: 87832ce2eb01a88dfd44414fa1f248a8f2646563 [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)
Dominique Pelle6d37e8e2021-05-06 17:36:55 +020016 assert_fails('vim9cmd', 'E1164:')
Bram Moolenaar39f3b142021-02-14 12:57:36 +010017enddef
18
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020019def Test_edit_wildcards()
Bram Moolenaarac564082020-09-27 19:05:33 +020020 var filename = 'Xtest'
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020021 edit `=filename`
22 assert_equal('Xtest', bufname())
23
Bram Moolenaarac564082020-09-27 19:05:33 +020024 var filenr = 123
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020025 edit Xtest`=filenr`
26 assert_equal('Xtest123', bufname())
27
28 filenr = 77
29 edit `=filename``=filenr`
30 assert_equal('Xtest77', bufname())
31
32 edit X`=filename`xx`=filenr`yy
33 assert_equal('XXtestxx77yy', bufname())
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010034
35 CheckDefFailure(['edit `=xxx`'], 'E1001:')
36 CheckDefFailure(['edit `="foo"'], 'E1083:')
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020037enddef
38
Bram Moolenaar4389f9c2020-12-27 16:55:11 +010039def Test_expand_alternate_file()
40 var lines =<< trim END
41 edit Xfileone
42 var bone = bufnr()
43 edit Xfiletwo
44 var btwo = bufnr()
45 edit Xfilethree
46 var bthree = bufnr()
47
48 edit #
49 assert_equal(bthree, bufnr())
50 edit %%
51 assert_equal(btwo, bufnr())
52 edit %% # comment
53 assert_equal(bthree, bufnr())
54 edit %%yy
55 assert_equal('Xfiletwoyy', bufname())
56
57 exe "edit %%" .. bone
58 assert_equal(bone, bufnr())
59 exe "edit %%" .. btwo .. "xx"
60 assert_equal('Xfiletwoxx', bufname())
61
62 next Xfileone Xfiletwo Xfilethree
63 assert_equal('Xfileone', argv(0))
64 assert_equal('Xfiletwo', argv(1))
65 assert_equal('Xfilethree', argv(2))
66 next %%%zz
67 assert_equal('Xfileone', argv(0))
68 assert_equal('Xfiletwo', argv(1))
69 assert_equal('Xfilethreezz', argv(2))
70
71 v:oldfiles = ['Xonefile', 'Xtwofile']
72 edit %%<1
73 assert_equal('Xonefile', bufname())
74 edit %%<2
75 assert_equal('Xtwofile', bufname())
76 assert_fails('edit %%<3', 'E684:')
77
78 edit Xfileone.vim
79 edit Xfiletwo
80 edit %%:r
81 assert_equal('Xfileone', bufname())
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +010082
83 assert_false(bufexists('altfoo'))
84 edit altfoo
85 edit bar
86 assert_true(bufexists('altfoo'))
87 assert_true(buflisted('altfoo'))
88 bdel %%
89 assert_true(bufexists('altfoo'))
90 assert_false(buflisted('altfoo'))
91 bwipe! altfoo
92 bwipe! bar
Bram Moolenaar4389f9c2020-12-27 16:55:11 +010093 END
94 CheckDefAndScriptSuccess(lines)
95enddef
96
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +010097def Test_global_backtick_expansion()
98 new
99 setline(1, 'xx')
100 var name = 'foobar'
101 g/^xx/s/.*/`=name`
102 assert_equal('foobar', getline(1))
103 bwipe!
104enddef
105
Bram Moolenaarecac5912021-01-05 19:23:28 +0100106def Test_folddo_backtick_expansion()
107 new
108 var name = 'xxx'
109 folddoopen edit `=name`
110 assert_equal('xxx', bufname())
111 bwipe!
112
113 new
114 setline(1, ['one', 'two'])
115 set nomodified
116 :1,2fold
117 foldclose
118 folddoclose edit `=name`
119 assert_equal('xxx', bufname())
120 bwipe!
121enddef
122
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200123def Test_hardcopy_wildcards()
124 CheckUnix
125 CheckFeature postscript
126
Bram Moolenaarac564082020-09-27 19:05:33 +0200127 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200128 hardcopy > X`=outfile`.ps
129 assert_true(filereadable('Xprint.ps'))
130
131 delete('Xprint.ps')
132enddef
133
134def Test_syn_include_wildcards()
135 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +0200136 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200137 &rtp = '.'
138
Bram Moolenaarac564082020-09-27 19:05:33 +0200139 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200140 syn include @Group Xthe`=fname`.vim
141 assert_match('Found.* contained found', execute('syn list Found'))
142
143 &rtp = save_rtp
144 delete('Xthemine.vim')
145enddef
146
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200147def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200148 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200149 vim9script
150 redir @a
151 echo 'one'
152 .. 'two'
153 redir END
154 assert_equal("\nonetwo", @a)
155 END
156 CheckScriptSuccess(lines)
157
158 lines =<< trim END
159 vim9script
160 redir @a
161 echo 11 +
162 77
163 - 22
164 redir END
165 assert_equal("\n66", @a)
166 END
167 CheckScriptSuccess(lines)
168enddef
169
Bram Moolenaar13106602020-10-04 16:06:05 +0200170def Test_condition_types()
171 var lines =<< trim END
172 if 'text'
173 endif
174 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100175 CheckDefAndScriptFailure(lines, 'E1135:', 1)
Bram Moolenaar13106602020-10-04 16:06:05 +0200176
177 lines =<< trim END
178 if [1]
179 endif
180 END
181 CheckDefFailure(lines, 'E1012:', 1)
182 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
183
184 lines =<< trim END
185 g:cond = 'text'
186 if g:cond
187 endif
188 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100189 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200190
191 lines =<< trim END
192 g:cond = 0
193 if g:cond
194 elseif 'text'
195 endif
196 END
197 CheckDefFailure(lines, 'E1012:', 3)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100198 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
Bram Moolenaar13106602020-10-04 16:06:05 +0200199
200 lines =<< trim END
201 if g:cond
202 elseif [1]
203 endif
204 END
205 CheckDefFailure(lines, 'E1012:', 2)
206 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
207
208 lines =<< trim END
209 g:cond = 'text'
210 if 0
211 elseif g:cond
212 endif
213 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100214 CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
Bram Moolenaar13106602020-10-04 16:06:05 +0200215
216 lines =<< trim END
217 while 'text'
218 endwhile
219 END
220 CheckDefFailure(lines, 'E1012:', 1)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100221 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200222
223 lines =<< trim END
224 while [1]
225 endwhile
226 END
227 CheckDefFailure(lines, 'E1012:', 1)
228 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
229
230 lines =<< trim END
231 g:cond = 'text'
232 while g:cond
233 endwhile
234 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100235 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200236enddef
237
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200238def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200239 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200240 vim9script
241 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200242 true
243 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200244 g:res = 42
245 endif
246 assert_equal(42, g:res)
247 END
248 CheckScriptSuccess(lines)
249 unlet g:res
250
251 lines =<< trim END
252 vim9script
253 if 1 &&
254 0
255 g:res = 0
256 elseif 0 ||
257 0
258 || 1
259 g:res = 12
260 endif
261 assert_equal(12, g:res)
262 END
263 CheckScriptSuccess(lines)
264 unlet g:res
265enddef
266
267def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200268 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200269 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200270 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200271 while nr <
272 10 + 3
273 nr = nr
274 + 4
275 endwhile
276 assert_equal(16, nr)
277 END
278 CheckScriptSuccess(lines)
279
280 lines =<< trim END
281 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200282 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200283 while nr
284 <
285 10
286 +
287 3
288 nr = nr
289 +
290 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200291 endwhile
292 assert_equal(16, nr)
293 END
294 CheckScriptSuccess(lines)
295enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200296
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200297def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200298 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200299 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200300 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200301 for x
302 in
303 [1, 2, 3, 4]
304 nr = nr + x
305 endfor
306 assert_equal(10, nr)
307 END
308 CheckScriptSuccess(lines)
309
310 lines =<< trim END
311 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200312 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200313 for x
314 in
315 [1, 2,
316 3, 4
317 ]
318 nr = nr
319 +
320 x
321 endfor
322 assert_equal(10, nr)
323 END
324 CheckScriptSuccess(lines)
325enddef
326
Bram Moolenaare0890d62021-02-17 14:52:14 +0100327def MethodAfterLinebreak(arg: string)
328 arg
329 ->setline(1)
330enddef
331
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200332def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200333 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200334 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200335 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200336 func RetArg(
337 arg
338 )
339 let s:res = a:arg
340 endfunc
341 [1,
342 2,
343 3]->RetArg()
344 assert_equal([1, 2, 3], res)
345 END
346 CheckScriptSuccess(lines)
Bram Moolenaar148be9b2021-02-02 21:33:52 +0100347
348 lines =<< trim END
349 new
350 var name = [1, 2]
351 name
352 ->copy()
353 ->setline(1)
354 assert_equal(['1', '2'], getline(1, 2))
355 bwipe!
356 END
357 CheckDefAndScriptSuccess(lines)
358
359 lines =<< trim END
360 new
Bram Moolenaar6914e872021-03-06 21:01:09 +0100361 def Foo(): string
362 return 'the text'
363 enddef
364 def Bar(F: func): string
365 return F()
366 enddef
367 def Test()
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100368 Foo ->Bar()
369 ->setline(1)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100370 enddef
371 Test()
372 assert_equal('the text', getline(1))
373 bwipe!
374 END
375 CheckDefAndScriptSuccess(lines)
376
377 lines =<< trim END
378 new
Bram Moolenaar148be9b2021-02-02 21:33:52 +0100379 g:shortlist
380 ->copy()
381 ->setline(1)
382 assert_equal(['1', '2'], getline(1, 2))
383 bwipe!
384 END
385 g:shortlist = [1, 2]
386 CheckDefAndScriptSuccess(lines)
387 unlet g:shortlist
Bram Moolenaare0890d62021-02-17 14:52:14 +0100388
389 new
390 MethodAfterLinebreak('foobar')
391 assert_equal('foobar', getline(1))
392 bwipe!
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100393
394 lines =<< trim END
395 vim9script
396 def Foo(): string
397 return '# some text'
398 enddef
399
400 def Bar(F: func): string
401 return F()
402 enddef
403
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100404 Foo->Bar()
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100405 ->setline(1)
406 END
407 CheckScriptSuccess(lines)
408 assert_equal('# some text', getline(1))
409 bwipe!
Bram Moolenaar5f195932020-07-01 20:07:14 +0200410enddef
411
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +0100412def Test_method_call_whitespace()
413 var lines =<< trim END
414 new
415 var yank = 'text'
416 yank->setline(1)
417 yank ->setline(2)
418 yank-> setline(3)
419 yank -> setline(4)
420 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
421 bwipe!
422 END
423 CheckDefAndScriptSuccess(lines)
424enddef
425
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100426def Test_method_and_user_command()
427 var lines =<< trim END
428 vim9script
429 def Cmd()
430 g:didFunc = 1
431 enddef
432 command Cmd g:didCmd = 1
433 Cmd
434 assert_equal(1, g:didCmd)
435 Cmd()
436 assert_equal(1, g:didFunc)
437 unlet g:didFunc
438 unlet g:didCmd
439
440 def InDefFunc()
441 Cmd
442 assert_equal(1, g:didCmd)
443 Cmd()
444 assert_equal(1, g:didFunc)
445 unlet g:didFunc
446 unlet g:didCmd
447 enddef
448 InDefFunc()
449 END
450 CheckScriptSuccess(lines)
451enddef
452
Bram Moolenaar683581e2020-10-22 21:22:58 +0200453def Test_skipped_expr_linebreak()
454 if 0
455 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100456 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200457 endif
458enddef
459
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200460def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100461 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200462 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100463 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200464 test.data
465 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100466 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200467
Bram Moolenaarac564082020-09-27 19:05:33 +0200468 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200469 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100470 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200471 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100472 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200473 END
474 CheckScriptSuccess(lines)
475enddef
476
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200477def Test_bar_after_command()
478 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200479 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200480 redraw | echo x
481 enddef
482 RedrawAndEcho()
483 assert_match('did redraw', Screenline(&lines))
484
Bram Moolenaar788123c2020-07-05 15:32:17 +0200485 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200486 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200487 reg_executing() | echo x
488 enddef
489 CallAndEcho()
490 assert_match('did redraw', Screenline(&lines))
491
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200492 if has('unix')
493 # bar in filter write command does not start new command
494 def WriteToShell()
495 new
496 setline(1, 'some text')
497 w !cat | cat > Xoutfile
498 bwipe!
499 enddef
500 WriteToShell()
501 assert_equal(['some text'], readfile('Xoutfile'))
502 delete('Xoutfile')
503
504 # bar in filter read command does not start new command
505 def ReadFromShell()
506 new
507 r! echo hello there | cat > Xoutfile
508 r !echo again | cat >> Xoutfile
509 bwipe!
510 enddef
511 ReadFromShell()
512 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
513 delete('Xoutfile')
514 endif
515enddef
516
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200517def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100518 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100519 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100520 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200521enddef
522
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100523def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100524 var lines =<< trim END
525 final expected = "\nType Name Content\n c \"c piyo"
526 @a = 'hoge'
527 @b = 'fuga'
528 @c = 'piyo'
529
530 assert_equal(execute('filter /piyo/ registers abc'), expected)
531 END
532 CheckDefAndScriptSuccess(lines)
533enddef
534
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100535def Test_win_command_modifiers()
536 assert_equal(1, winnr('$'))
537
538 set splitright
539 vsplit
540 assert_equal(2, winnr())
541 close
542 aboveleft vsplit
543 assert_equal(1, winnr())
544 close
545 set splitright&
546
547 vsplit
548 assert_equal(1, winnr())
549 close
550 belowright vsplit
551 assert_equal(2, winnr())
552 close
553 rightbelow vsplit
554 assert_equal(2, winnr())
555 close
556
Bram Moolenaar97a19002020-11-01 22:15:44 +0100557 if has('browse')
558 browse set
559 assert_equal('option-window', expand('%'))
560 close
561 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100562
563 vsplit
564 botright split
565 assert_equal(3, winnr())
566 assert_equal(&columns, winwidth(0))
567 close
568 close
569
570 vsplit
571 topleft split
572 assert_equal(1, winnr())
573 assert_equal(&columns, winwidth(0))
574 close
575 close
576
577 gettabinfo()->len()->assert_equal(1)
578 tab split
579 gettabinfo()->len()->assert_equal(2)
580 tabclose
581
582 vertical new
583 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
584 close
585enddef
586
587func Test_command_modifier_confirm()
588 CheckNotGui
589 CheckRunVimInTerminal
590
591 " Test for saving all the modified buffers
592 let lines =<< trim END
593 call setline(1, 'changed')
594 def Getout()
595 confirm write Xfile
596 enddef
597 END
598 call writefile(lines, 'Xconfirmscript')
599 call writefile(['empty'], 'Xfile')
600 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
601 call term_sendkeys(buf, ":call Getout()\n")
602 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
603 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100604 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
605 call term_sendkeys(buf, "\<CR>")
606 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100607 call StopVimInTerminal(buf)
608
609 call assert_equal(['changed'], readfile('Xfile'))
610 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100611 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100612 call delete('Xconfirmscript')
613endfunc
614
615def Test_command_modifiers_keep()
616 if has('unix')
617 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
618 new
619 setline(1, ['one', 'two', 'three'])
620 normal 1Gma
621 normal 2Gmb
622 normal 3Gmc
623 if addRflag
624 set cpo+=R
625 else
626 set cpo-=R
627 endif
628 if keepMarks
629 keepmarks :%!cat
630 else
631 :%!cat
632 endif
633 if hasMarks
634 assert_equal(1, line("'a"))
635 assert_equal(2, line("'b"))
636 assert_equal(3, line("'c"))
637 else
638 assert_equal(0, line("'a"))
639 assert_equal(0, line("'b"))
640 assert_equal(0, line("'c"))
641 endif
642 quit!
643 enddef
644 DoTest(false, false, true)
645 DoTest(true, false, false)
646 DoTest(false, true, true)
647 DoTest(true, true, true)
648 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100649
650 new
651 setline(1, ['one', 'two', 'three', 'four'])
652 assert_equal(4, line("$"))
653 normal 1Gma
654 normal 2Gmb
655 normal 3Gmc
656 lockmarks :1,2!wc
657 # line is deleted, marks don't move
658 assert_equal(3, line("$"))
659 assert_equal('four', getline(3))
660 assert_equal(1, line("'a"))
661 assert_equal(2, line("'b"))
662 assert_equal(3, line("'c"))
663 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100664 endif
665
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100666 edit Xone
667 edit Xtwo
668 assert_equal('Xone', expand('#'))
669 keepalt edit Xthree
670 assert_equal('Xone', expand('#'))
671
672 normal /a*b*
673 assert_equal('a*b*', histget("search"))
674 keeppatterns normal /c*d*
675 assert_equal('a*b*', histget("search"))
676
677 new
678 setline(1, range(10))
679 :10
680 normal gg
681 assert_equal(10, getpos("''")[1])
682 keepjumps normal 5G
683 assert_equal(10, getpos("''")[1])
684 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100685enddef
686
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100687def Test_bar_line_continuation()
688 var lines =<< trim END
689 au BufNewFile Xfile g:readFile = 1
690 | g:readExtra = 2
691 g:readFile = 0
692 g:readExtra = 0
693 edit Xfile
694 assert_equal(1, g:readFile)
695 assert_equal(2, g:readExtra)
696 bwipe!
697 au! BufNewFile
698
699 au BufNewFile Xfile g:readFile = 1
700 | g:readExtra = 2
701 | g:readMore = 3
702 g:readFile = 0
703 g:readExtra = 0
704 g:readMore = 0
705 edit Xfile
706 assert_equal(1, g:readFile)
707 assert_equal(2, g:readExtra)
708 assert_equal(3, g:readMore)
709 bwipe!
710 au! BufNewFile
711 unlet g:readFile
712 unlet g:readExtra
713 unlet g:readMore
714 END
715 CheckDefAndScriptSuccess(lines)
716enddef
717
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100718def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100719 new Xsomefile
720 setline(1, 'changed')
721 var buf = bufnr()
722 hide edit Xotherfile
723 var info = getbufinfo(buf)
724 assert_equal(1, info[0].hidden)
725 assert_equal(1, info[0].changed)
726 edit Xsomefile
727 bwipe!
728
729 au BufNewFile Xfile g:readFile = 1
730 g:readFile = 0
731 edit Xfile
732 assert_equal(1, g:readFile)
733 bwipe!
734 g:readFile = 0
735 noautocmd edit Xfile
736 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100737 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100738 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100739
740 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100741 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100742 bwipe!
743
744 var caught = false
745 try
746 sandbox !ls
747 catch /E48:/
748 caught = true
749 endtry
750 assert_true(caught)
751
752 :8verbose g:verbose_now = &verbose
753 assert_equal(8, g:verbose_now)
754 unlet g:verbose_now
755enddef
756
757def EchoHere()
758 echomsg 'here'
759enddef
760def EchoThere()
761 unsilent echomsg 'there'
762enddef
763
764def Test_modifier_silent_unsilent()
765 echomsg 'last one'
766 silent echomsg "text"
767 assert_equal("\nlast one", execute(':1messages'))
768
769 silent! echoerr "error"
770
771 echomsg 'last one'
772 silent EchoHere()
773 assert_equal("\nlast one", execute(':1messages'))
774
775 silent EchoThere()
776 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100777
778 try
779 silent eval [][0]
780 catch
781 echomsg "caught"
782 endtry
783 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100784enddef
785
Bram Moolenaar36113e42020-11-02 21:08:47 +0100786def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100787 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100788 new
789 setline(1, 'xxx')
790 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
791 assert_equal('', getline(1))
792 bwipe!
793enddef
794
Bram Moolenaarece0b872021-01-08 20:40:45 +0100795def Test_silent_pattern()
796 new
797 silent! :/pat/put _
798 bwipe!
799enddef
800
Bram Moolenaarfa984412021-03-25 22:15:28 +0100801def Test_useless_command_modifier()
802 g:maybe = true
803 var lines =<< trim END
804 if g:maybe
805 silent endif
806 END
807 CheckDefAndScriptFailure(lines, 'E1176:', 2)
808
809 lines =<< trim END
810 for i in [0]
811 silent endfor
812 END
813 CheckDefAndScriptFailure(lines, 'E1176:', 2)
814
815 lines =<< trim END
816 while g:maybe
817 silent endwhile
818 END
819 CheckDefAndScriptFailure(lines, 'E1176:', 2)
820
821 lines =<< trim END
822 silent try
823 finally
824 endtry
825 END
826 CheckDefAndScriptFailure(lines, 'E1176:', 1)
827
828 lines =<< trim END
829 try
830 silent catch
831 endtry
832 END
833 CheckDefAndScriptFailure(lines, 'E1176:', 2)
834
835 lines =<< trim END
836 try
837 silent finally
838 endtry
839 END
840 CheckDefAndScriptFailure(lines, 'E1176:', 2)
841
842 lines =<< trim END
843 try
844 finally
845 silent endtry
846 END
847 CheckDefAndScriptFailure(lines, 'E1176:', 3)
848enddef
849
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200850def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200851 var from = 3
852 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200853 g:val = 111
854 def Increment(nrs: list<number>)
855 for nr in nrs
856 g:val += nr
857 endfor
858 enddef
859 eval range(from, to)
860 ->Increment()
861 assert_equal(111 + 3 + 4 + 5, g:val)
862 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100863
864 var lines =<< trim END
865 vim9script
866 g:caught = 'no'
867 try
868 eval 123 || 0
869 catch
870 g:caught = 'yes'
871 endtry
872 assert_equal('yes', g:caught)
873 unlet g:caught
874 END
875 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200876enddef
877
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200878def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200879 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200880 nnoremap <F3> :echo 'hit F3 #'<CR>
881 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
882 END
883 CheckDefSuccess(lines)
884 CheckScriptSuccess(['vim9script'] + lines)
885enddef
886
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200887def Test_normal_command()
888 new
889 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200890 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200891 try
892 exe "norm! \<C-]>"
893 catch /E433/
894 caught = 2
895 endtry
896 assert_equal(2, caught)
897
898 try
899 exe "norm! 3\<C-]>"
900 catch /E433/
901 caught = 3
902 endtry
903 assert_equal(3, caught)
904 bwipe!
905enddef
906
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200907def Test_put_command()
908 new
909 @p = 'ppp'
910 put p
911 assert_equal('ppp', getline(2))
912
913 put ='below'
914 assert_equal('below', getline(3))
915 put! ='above'
916 assert_equal('above', getline(3))
917 assert_equal('below', getline(4))
918
Bram Moolenaar883cf972021-01-15 18:04:43 +0100919 :2put =['a', 'b', 'c']
920 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
921
Bram Moolenaar08597872020-12-10 19:43:40 +0100922 # compute range at runtime
923 setline(1, range(1, 8))
924 @a = 'aaa'
925 :$-2put a
926 assert_equal('aaa', getline(7))
927
928 setline(1, range(1, 8))
929 :2
930 :+2put! a
931 assert_equal('aaa', getline(4))
932
Bram Moolenaara28639e2021-01-19 22:48:09 +0100933 []->mapnew(() => 0)
934 :$put ='end'
935 assert_equal('end', getline('$'))
936
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200937 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100938
939 CheckDefFailure(['put =xxx'], 'E1001:')
940enddef
941
942def Test_put_with_linebreak()
943 new
944 var lines =<< trim END
945 vim9script
946 pu =split('abc', '\zs')
947 ->join()
948 END
949 CheckScriptSuccess(lines)
950 getline(2)->assert_equal('a b c')
951 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200952enddef
953
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200954def Test_command_star_range()
955 new
956 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
957 setpos("'<", [0, 1, 0, 0])
958 setpos("'>", [0, 3, 0, 0])
959 :*s/\(foo\|bar\)/baz/g
960 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
961
962 bwipe!
963enddef
964
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200965def Test_f_args()
966 var lines =<< trim END
967 vim9script
968
969 func SaveCmdArgs(...)
970 let g:args = a:000
971 endfunc
972
973 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
974
975 TestFArgs
976 assert_equal([], g:args)
977
978 TestFArgs one two three
979 assert_equal(['one', 'two', 'three'], g:args)
980 END
981 CheckScriptSuccess(lines)
982enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200983
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100984def Test_user_command_comment()
985 command -nargs=1 Comd echom <q-args>
986
987 var lines =<< trim END
Bram Moolenaarb98cec22021-04-25 16:35:55 +0200988 vim9script
989 Comd # comment
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100990 END
991 CheckScriptSuccess(lines)
992
993 lines =<< trim END
Bram Moolenaarb98cec22021-04-25 16:35:55 +0200994 vim9script
995 Comd# comment
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100996 END
997 CheckScriptFailure(lines, 'E1144:')
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100998 delcommand Comd
Bram Moolenaarb98cec22021-04-25 16:35:55 +0200999
1000 lines =<< trim END
1001 vim9script
1002 command Foo echo 'Foo'
1003 Foo3Bar
1004 END
1005 CheckScriptFailure(lines, 'E1144: Command "Foo" is not followed by white space: Foo3Bar')
1006
1007 delcommand Foo
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001008enddef
1009
Bram Moolenaar95388e32020-11-20 21:07:00 +01001010def Test_star_command()
1011 var lines =<< trim END
1012 vim9script
1013 @s = 'g:success = 8'
1014 set cpo+=*
1015 exe '*s'
1016 assert_equal(8, g:success)
1017 unlet g:success
1018 set cpo-=*
1019 assert_fails("exe '*s'", 'E1050:')
1020 END
1021 CheckScriptSuccess(lines)
1022enddef
1023
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001024def Test_cmd_argument_without_colon()
1025 new Xfile
1026 setline(1, ['a', 'b', 'c', 'd'])
1027 write
1028 edit +3 %
1029 assert_equal(3, getcurpos()[1])
1030 edit +/a %
1031 assert_equal(1, getcurpos()[1])
1032 bwipe
1033 delete('Xfile')
1034enddef
1035
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001036def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001037 command Cmd1 eval 0
1038 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001039 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001040 Cmd
1041 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001042 CheckDefAndScriptFailure(lines, 'E464:', 1)
1043 delcommand Cmd1
1044 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01001045enddef
1046
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001047def Test_command_not_recognized()
1048 var lines =<< trim END
1049 d.key = 'asdf'
1050 END
1051 CheckDefFailure(lines, 'E1146:', 1)
1052
1053 lines =<< trim END
1054 d['key'] = 'asdf'
1055 END
1056 CheckDefFailure(lines, 'E1146:', 1)
1057enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001058
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001059def Test_magic_not_used()
1060 new
1061 for cmd in ['set magic', 'set nomagic']
1062 exe cmd
1063 setline(1, 'aaa')
1064 s/.../bbb/
1065 assert_equal('bbb', getline(1))
1066 endfor
1067
1068 set magic
1069 setline(1, 'aaa')
1070 assert_fails('s/.\M../bbb/', 'E486:')
1071 assert_fails('snomagic/.../bbb/', 'E486:')
1072 assert_equal('aaa', getline(1))
1073
1074 bwipe!
1075enddef
1076
Bram Moolenaar60f63102020-12-21 20:32:43 +01001077def Test_gdefault_not_used()
1078 new
1079 for cmd in ['set gdefault', 'set nogdefault']
1080 exe cmd
1081 setline(1, 'aaa')
1082 s/./b/
1083 assert_equal('baa', getline(1))
1084 endfor
1085
1086 set nogdefault
1087 bwipe!
1088enddef
1089
Bram Moolenaar179eb562020-12-27 18:03:22 +01001090def g:SomeComplFunc(findstart: number, base: string): any
1091 if findstart
1092 return 0
1093 else
1094 return ['aaa', 'bbb']
1095 endif
1096enddef
1097
1098def Test_insert_complete()
1099 # this was running into an error with the matchparen hack
1100 new
1101 set completefunc=SomeComplFunc
1102 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
1103 assert_equal('aaa', getline(1))
1104
1105 set completefunc=
1106 bwipe!
1107enddef
1108
Bram Moolenaara11919f2021-01-02 19:44:56 +01001109def Test_wincmd()
1110 split
1111 var id1 = win_getid()
1112 if true
1113 try | wincmd w | catch | endtry
1114 endif
1115 assert_notequal(id1, win_getid())
1116 close
Bram Moolenaar1ff89de2021-03-24 20:08:12 +01001117
1118 split
1119 var id = win_getid()
1120 split
1121 :2wincmd o
1122 assert_equal(id, win_getid())
1123 only
1124
1125 split
1126 split
1127 assert_equal(3, winnr('$'))
1128 :2wincmd c
1129 assert_equal(2, winnr('$'))
1130 only
1131
1132 split
1133 split
1134 assert_equal(3, winnr('$'))
1135 :2wincmd q
1136 assert_equal(2, winnr('$'))
1137 only
Bram Moolenaara11919f2021-01-02 19:44:56 +01001138enddef
1139
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001140def Test_windo_missing_endif()
1141 var lines =<< trim END
1142 windo if 1
1143 END
1144 CheckDefExecFailure(lines, 'E171:', 1)
1145enddef
1146
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02001147let s:theList = [1, 2, 3]
1148
1149def Test_lockvar()
1150 s:theList[1] = 22
1151 assert_equal([1, 22, 3], s:theList)
1152 lockvar s:theList
1153 assert_fails('theList[1] = 77', 'E741:')
1154 unlockvar s:theList
1155 s:theList[1] = 44
1156 assert_equal([1, 44, 3], s:theList)
1157
1158 var lines =<< trim END
1159 vim9script
1160 var theList = [1, 2, 3]
1161 def SetList()
1162 theList[1] = 22
1163 assert_equal([1, 22, 3], theList)
1164 lockvar theList
1165 theList[1] = 77
1166 enddef
1167 SetList()
1168 END
1169 CheckScriptFailure(lines, 'E1119', 4)
1170
1171 lines =<< trim END
1172 var theList = [1, 2, 3]
1173 lockvar theList
1174 END
1175 CheckDefFailure(lines, 'E1178', 2)
1176
1177 lines =<< trim END
1178 var theList = [1, 2, 3]
1179 unlockvar theList
1180 END
1181 CheckDefFailure(lines, 'E1178', 2)
1182enddef
1183
Bram Moolenaar4c137212021-04-19 16:48:48 +02001184def Test_substitute_expr()
1185 var to = 'repl'
1186 new
1187 setline(1, 'one from two')
1188 s/from/\=to
1189 assert_equal('one repl two', getline(1))
1190
1191 setline(1, 'one from two')
1192 s/from/\=to .. '_x'
1193 assert_equal('one repl_x two', getline(1))
1194
1195 setline(1, 'one from two from three')
1196 var also = 'also'
1197 s/from/\=to .. '_' .. also/g#e
1198 assert_equal('one repl_also two repl_also three', getline(1))
1199
Bram Moolenaar8238f082021-04-20 21:10:48 +02001200 setline(1, 'abc abc abc')
1201 for choice in [true, false]
1202 :1s/abc/\=choice ? 'yes' : 'no'/
1203 endfor
1204 assert_equal('yes no abc', getline(1))
1205
Bram Moolenaard386e922021-04-25 14:48:49 +02001206 bwipe!
1207
Bram Moolenaar4c137212021-04-19 16:48:48 +02001208 CheckDefFailure(['s/from/\="x")/'], 'E488:')
1209 CheckDefFailure(['s/from/\="x"/9'], 'E488:')
1210
Bram Moolenaard386e922021-04-25 14:48:49 +02001211 # When calling a function the right instruction list needs to be restored.
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001212 g:cond = true
Bram Moolenaard386e922021-04-25 14:48:49 +02001213 var lines =<< trim END
1214 vim9script
1215 def Foo()
1216 Bar([])
1217 enddef
1218 def Bar(l: list<number>)
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001219 if g:cond
Bram Moolenaard386e922021-04-25 14:48:49 +02001220 s/^/\=Rep()/
1221 for n in l[:]
1222 endfor
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001223 endif
Bram Moolenaard386e922021-04-25 14:48:49 +02001224 enddef
1225 def Rep(): string
1226 return 'rep'
1227 enddef
1228 new
1229 Foo()
1230 assert_equal('rep', getline(1))
1231 bwipe!
1232 END
1233 CheckScriptSuccess(lines)
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001234 unlet g:cond
Bram Moolenaar4c137212021-04-19 16:48:48 +02001235enddef
1236
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001237def Test_redir_to_var()
1238 var result: string
1239 redir => result
1240 echo 'something'
1241 redir END
1242 assert_equal("\nsomething", result)
1243
1244 redir =>> result
1245 echo 'more'
1246 redir END
1247 assert_equal("\nsomething\nmore", result)
1248
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001249 var d: dict<string>
1250 redir => d.redir
1251 echo 'dict'
1252 redir END
1253 assert_equal({redir: "\ndict"}, d)
1254
1255 var l = ['a', 'b', 'c']
1256 redir => l[1]
1257 echo 'list'
1258 redir END
1259 assert_equal(['a', "\nlist", 'c'], l)
1260
1261 var dl = {l: ['x']}
1262 redir => dl.l[0]
1263 echo 'dict-list'
1264 redir END
1265 assert_equal({l: ["\ndict-list"]}, dl)
1266
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001267 redir =>> d.redir
1268 echo 'more'
1269 redir END
1270 assert_equal({redir: "\ndict\nmore"}, d)
1271
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001272 var lines =<< trim END
1273 redir => notexist
1274 END
1275 CheckDefFailure(lines, 'E1089:')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001276
1277 lines =<< trim END
1278 var ls = 'asdf'
1279 redir => ls[1]
1280 redir END
1281 END
1282 CheckDefFailure(lines, 'E1141:')
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001283enddef
1284
Bram Moolenaar68db9962021-05-09 23:19:22 +02001285def Test_echo_void()
1286 var lines =<< trim END
1287 vim9script
1288 def NoReturn()
1289 echo 'nothing'
1290 enddef
1291 echo NoReturn()
1292 END
1293 CheckScriptFailure(lines, 'E1186:', 5)
1294
1295 lines =<< trim END
1296 vim9script
1297 def NoReturn()
1298 echo 'nothing'
1299 enddef
1300 def Try()
1301 echo NoReturn()
1302 enddef
1303 defcompile
1304 END
1305 CheckScriptFailure(lines, 'E1186:', 1)
1306enddef
1307
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02001308
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001309" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker