blob: f638c84b06e0b997349e6f01cde2563138376e66 [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
360 g:shortlist
361 ->copy()
362 ->setline(1)
363 assert_equal(['1', '2'], getline(1, 2))
364 bwipe!
365 END
366 g:shortlist = [1, 2]
367 CheckDefAndScriptSuccess(lines)
368 unlet g:shortlist
Bram Moolenaare0890d62021-02-17 14:52:14 +0100369
370 new
371 MethodAfterLinebreak('foobar')
372 assert_equal('foobar', getline(1))
373 bwipe!
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100374
375 lines =<< trim END
376 vim9script
377 def Foo(): string
378 return '# some text'
379 enddef
380
381 def Bar(F: func): string
382 return F()
383 enddef
384
385 Foo
386 ->Bar()
387 ->setline(1)
388 END
389 CheckScriptSuccess(lines)
390 assert_equal('# some text', getline(1))
391 bwipe!
Bram Moolenaar5f195932020-07-01 20:07:14 +0200392enddef
393
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +0100394def Test_method_call_whitespace()
395 var lines =<< trim END
396 new
397 var yank = 'text'
398 yank->setline(1)
399 yank ->setline(2)
400 yank-> setline(3)
401 yank -> setline(4)
402 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
403 bwipe!
404 END
405 CheckDefAndScriptSuccess(lines)
406enddef
407
Bram Moolenaar683581e2020-10-22 21:22:58 +0200408def Test_skipped_expr_linebreak()
409 if 0
410 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100411 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200412 endif
413enddef
414
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200415def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100416 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200417 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100418 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200419 test.data
420 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100421 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200422
Bram Moolenaarac564082020-09-27 19:05:33 +0200423 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200424 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100425 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200426 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100427 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200428 END
429 CheckScriptSuccess(lines)
430enddef
431
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200432def Test_bar_after_command()
433 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200434 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200435 redraw | echo x
436 enddef
437 RedrawAndEcho()
438 assert_match('did redraw', Screenline(&lines))
439
Bram Moolenaar788123c2020-07-05 15:32:17 +0200440 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200441 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200442 reg_executing() | echo x
443 enddef
444 CallAndEcho()
445 assert_match('did redraw', Screenline(&lines))
446
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200447 if has('unix')
448 # bar in filter write command does not start new command
449 def WriteToShell()
450 new
451 setline(1, 'some text')
452 w !cat | cat > Xoutfile
453 bwipe!
454 enddef
455 WriteToShell()
456 assert_equal(['some text'], readfile('Xoutfile'))
457 delete('Xoutfile')
458
459 # bar in filter read command does not start new command
460 def ReadFromShell()
461 new
462 r! echo hello there | cat > Xoutfile
463 r !echo again | cat >> Xoutfile
464 bwipe!
465 enddef
466 ReadFromShell()
467 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
468 delete('Xoutfile')
469 endif
470enddef
471
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200472def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100473 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100474 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100475 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200476enddef
477
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100478def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100479 var lines =<< trim END
480 final expected = "\nType Name Content\n c \"c piyo"
481 @a = 'hoge'
482 @b = 'fuga'
483 @c = 'piyo'
484
485 assert_equal(execute('filter /piyo/ registers abc'), expected)
486 END
487 CheckDefAndScriptSuccess(lines)
488enddef
489
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100490def Test_win_command_modifiers()
491 assert_equal(1, winnr('$'))
492
493 set splitright
494 vsplit
495 assert_equal(2, winnr())
496 close
497 aboveleft vsplit
498 assert_equal(1, winnr())
499 close
500 set splitright&
501
502 vsplit
503 assert_equal(1, winnr())
504 close
505 belowright vsplit
506 assert_equal(2, winnr())
507 close
508 rightbelow vsplit
509 assert_equal(2, winnr())
510 close
511
Bram Moolenaar97a19002020-11-01 22:15:44 +0100512 if has('browse')
513 browse set
514 assert_equal('option-window', expand('%'))
515 close
516 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100517
518 vsplit
519 botright split
520 assert_equal(3, winnr())
521 assert_equal(&columns, winwidth(0))
522 close
523 close
524
525 vsplit
526 topleft split
527 assert_equal(1, winnr())
528 assert_equal(&columns, winwidth(0))
529 close
530 close
531
532 gettabinfo()->len()->assert_equal(1)
533 tab split
534 gettabinfo()->len()->assert_equal(2)
535 tabclose
536
537 vertical new
538 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
539 close
540enddef
541
542func Test_command_modifier_confirm()
543 CheckNotGui
544 CheckRunVimInTerminal
545
546 " Test for saving all the modified buffers
547 let lines =<< trim END
548 call setline(1, 'changed')
549 def Getout()
550 confirm write Xfile
551 enddef
552 END
553 call writefile(lines, 'Xconfirmscript')
554 call writefile(['empty'], 'Xfile')
555 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
556 call term_sendkeys(buf, ":call Getout()\n")
557 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
558 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100559 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
560 call term_sendkeys(buf, "\<CR>")
561 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100562 call StopVimInTerminal(buf)
563
564 call assert_equal(['changed'], readfile('Xfile'))
565 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100566 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100567 call delete('Xconfirmscript')
568endfunc
569
570def Test_command_modifiers_keep()
571 if has('unix')
572 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
573 new
574 setline(1, ['one', 'two', 'three'])
575 normal 1Gma
576 normal 2Gmb
577 normal 3Gmc
578 if addRflag
579 set cpo+=R
580 else
581 set cpo-=R
582 endif
583 if keepMarks
584 keepmarks :%!cat
585 else
586 :%!cat
587 endif
588 if hasMarks
589 assert_equal(1, line("'a"))
590 assert_equal(2, line("'b"))
591 assert_equal(3, line("'c"))
592 else
593 assert_equal(0, line("'a"))
594 assert_equal(0, line("'b"))
595 assert_equal(0, line("'c"))
596 endif
597 quit!
598 enddef
599 DoTest(false, false, true)
600 DoTest(true, false, false)
601 DoTest(false, true, true)
602 DoTest(true, true, true)
603 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100604
605 new
606 setline(1, ['one', 'two', 'three', 'four'])
607 assert_equal(4, line("$"))
608 normal 1Gma
609 normal 2Gmb
610 normal 3Gmc
611 lockmarks :1,2!wc
612 # line is deleted, marks don't move
613 assert_equal(3, line("$"))
614 assert_equal('four', getline(3))
615 assert_equal(1, line("'a"))
616 assert_equal(2, line("'b"))
617 assert_equal(3, line("'c"))
618 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100619 endif
620
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100621 edit Xone
622 edit Xtwo
623 assert_equal('Xone', expand('#'))
624 keepalt edit Xthree
625 assert_equal('Xone', expand('#'))
626
627 normal /a*b*
628 assert_equal('a*b*', histget("search"))
629 keeppatterns normal /c*d*
630 assert_equal('a*b*', histget("search"))
631
632 new
633 setline(1, range(10))
634 :10
635 normal gg
636 assert_equal(10, getpos("''")[1])
637 keepjumps normal 5G
638 assert_equal(10, getpos("''")[1])
639 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100640enddef
641
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100642def Test_bar_line_continuation()
643 var lines =<< trim END
644 au BufNewFile Xfile g:readFile = 1
645 | g:readExtra = 2
646 g:readFile = 0
647 g:readExtra = 0
648 edit Xfile
649 assert_equal(1, g:readFile)
650 assert_equal(2, g:readExtra)
651 bwipe!
652 au! BufNewFile
653
654 au BufNewFile Xfile g:readFile = 1
655 | g:readExtra = 2
656 | g:readMore = 3
657 g:readFile = 0
658 g:readExtra = 0
659 g:readMore = 0
660 edit Xfile
661 assert_equal(1, g:readFile)
662 assert_equal(2, g:readExtra)
663 assert_equal(3, g:readMore)
664 bwipe!
665 au! BufNewFile
666 unlet g:readFile
667 unlet g:readExtra
668 unlet g:readMore
669 END
670 CheckDefAndScriptSuccess(lines)
671enddef
672
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100673def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100674 new Xsomefile
675 setline(1, 'changed')
676 var buf = bufnr()
677 hide edit Xotherfile
678 var info = getbufinfo(buf)
679 assert_equal(1, info[0].hidden)
680 assert_equal(1, info[0].changed)
681 edit Xsomefile
682 bwipe!
683
684 au BufNewFile Xfile g:readFile = 1
685 g:readFile = 0
686 edit Xfile
687 assert_equal(1, g:readFile)
688 bwipe!
689 g:readFile = 0
690 noautocmd edit Xfile
691 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100692 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100693 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100694
695 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100696 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100697 bwipe!
698
699 var caught = false
700 try
701 sandbox !ls
702 catch /E48:/
703 caught = true
704 endtry
705 assert_true(caught)
706
707 :8verbose g:verbose_now = &verbose
708 assert_equal(8, g:verbose_now)
709 unlet g:verbose_now
710enddef
711
712def EchoHere()
713 echomsg 'here'
714enddef
715def EchoThere()
716 unsilent echomsg 'there'
717enddef
718
719def Test_modifier_silent_unsilent()
720 echomsg 'last one'
721 silent echomsg "text"
722 assert_equal("\nlast one", execute(':1messages'))
723
724 silent! echoerr "error"
725
726 echomsg 'last one'
727 silent EchoHere()
728 assert_equal("\nlast one", execute(':1messages'))
729
730 silent EchoThere()
731 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100732
733 try
734 silent eval [][0]
735 catch
736 echomsg "caught"
737 endtry
738 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100739enddef
740
Bram Moolenaar36113e42020-11-02 21:08:47 +0100741def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100742 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100743 new
744 setline(1, 'xxx')
745 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
746 assert_equal('', getline(1))
747 bwipe!
748enddef
749
Bram Moolenaarece0b872021-01-08 20:40:45 +0100750def Test_silent_pattern()
751 new
752 silent! :/pat/put _
753 bwipe!
754enddef
755
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200756def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200757 var from = 3
758 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200759 g:val = 111
760 def Increment(nrs: list<number>)
761 for nr in nrs
762 g:val += nr
763 endfor
764 enddef
765 eval range(from, to)
766 ->Increment()
767 assert_equal(111 + 3 + 4 + 5, g:val)
768 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100769
770 var lines =<< trim END
771 vim9script
772 g:caught = 'no'
773 try
774 eval 123 || 0
775 catch
776 g:caught = 'yes'
777 endtry
778 assert_equal('yes', g:caught)
779 unlet g:caught
780 END
781 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200782enddef
783
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200784def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200785 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200786 nnoremap <F3> :echo 'hit F3 #'<CR>
787 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
788 END
789 CheckDefSuccess(lines)
790 CheckScriptSuccess(['vim9script'] + lines)
791enddef
792
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200793def Test_normal_command()
794 new
795 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200796 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200797 try
798 exe "norm! \<C-]>"
799 catch /E433/
800 caught = 2
801 endtry
802 assert_equal(2, caught)
803
804 try
805 exe "norm! 3\<C-]>"
806 catch /E433/
807 caught = 3
808 endtry
809 assert_equal(3, caught)
810 bwipe!
811enddef
812
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200813def Test_put_command()
814 new
815 @p = 'ppp'
816 put p
817 assert_equal('ppp', getline(2))
818
819 put ='below'
820 assert_equal('below', getline(3))
821 put! ='above'
822 assert_equal('above', getline(3))
823 assert_equal('below', getline(4))
824
Bram Moolenaar883cf972021-01-15 18:04:43 +0100825 :2put =['a', 'b', 'c']
826 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
827
Bram Moolenaar08597872020-12-10 19:43:40 +0100828 # compute range at runtime
829 setline(1, range(1, 8))
830 @a = 'aaa'
831 :$-2put a
832 assert_equal('aaa', getline(7))
833
834 setline(1, range(1, 8))
835 :2
836 :+2put! a
837 assert_equal('aaa', getline(4))
838
Bram Moolenaara28639e2021-01-19 22:48:09 +0100839 []->mapnew(() => 0)
840 :$put ='end'
841 assert_equal('end', getline('$'))
842
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200843 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100844
845 CheckDefFailure(['put =xxx'], 'E1001:')
846enddef
847
848def Test_put_with_linebreak()
849 new
850 var lines =<< trim END
851 vim9script
852 pu =split('abc', '\zs')
853 ->join()
854 END
855 CheckScriptSuccess(lines)
856 getline(2)->assert_equal('a b c')
857 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200858enddef
859
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200860def Test_command_star_range()
861 new
862 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
863 setpos("'<", [0, 1, 0, 0])
864 setpos("'>", [0, 3, 0, 0])
865 :*s/\(foo\|bar\)/baz/g
866 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
867
868 bwipe!
869enddef
870
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200871def Test_f_args()
872 var lines =<< trim END
873 vim9script
874
875 func SaveCmdArgs(...)
876 let g:args = a:000
877 endfunc
878
879 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
880
881 TestFArgs
882 assert_equal([], g:args)
883
884 TestFArgs one two three
885 assert_equal(['one', 'two', 'three'], g:args)
886 END
887 CheckScriptSuccess(lines)
888enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200889
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100890def Test_user_command_comment()
891 command -nargs=1 Comd echom <q-args>
892
893 var lines =<< trim END
894 vim9script
895 Comd # comment
896 END
897 CheckScriptSuccess(lines)
898
899 lines =<< trim END
900 vim9script
901 Comd# comment
902 END
903 CheckScriptFailure(lines, 'E1144:')
904
905 delcommand Comd
906enddef
907
Bram Moolenaar95388e32020-11-20 21:07:00 +0100908def Test_star_command()
909 var lines =<< trim END
910 vim9script
911 @s = 'g:success = 8'
912 set cpo+=*
913 exe '*s'
914 assert_equal(8, g:success)
915 unlet g:success
916 set cpo-=*
917 assert_fails("exe '*s'", 'E1050:')
918 END
919 CheckScriptSuccess(lines)
920enddef
921
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100922def Test_cmd_argument_without_colon()
923 new Xfile
924 setline(1, ['a', 'b', 'c', 'd'])
925 write
926 edit +3 %
927 assert_equal(3, getcurpos()[1])
928 edit +/a %
929 assert_equal(1, getcurpos()[1])
930 bwipe
931 delete('Xfile')
932enddef
933
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100934def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100935 command Cmd1 eval 0
936 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100937 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100938 Cmd
939 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100940 CheckDefAndScriptFailure(lines, 'E464:', 1)
941 delcommand Cmd1
942 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100943enddef
944
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100945def Test_command_not_recognized()
946 var lines =<< trim END
947 d.key = 'asdf'
948 END
949 CheckDefFailure(lines, 'E1146:', 1)
950
951 lines =<< trim END
952 d['key'] = 'asdf'
953 END
954 CheckDefFailure(lines, 'E1146:', 1)
955enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200956
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100957def Test_magic_not_used()
958 new
959 for cmd in ['set magic', 'set nomagic']
960 exe cmd
961 setline(1, 'aaa')
962 s/.../bbb/
963 assert_equal('bbb', getline(1))
964 endfor
965
966 set magic
967 setline(1, 'aaa')
968 assert_fails('s/.\M../bbb/', 'E486:')
969 assert_fails('snomagic/.../bbb/', 'E486:')
970 assert_equal('aaa', getline(1))
971
972 bwipe!
973enddef
974
Bram Moolenaar60f63102020-12-21 20:32:43 +0100975def Test_gdefault_not_used()
976 new
977 for cmd in ['set gdefault', 'set nogdefault']
978 exe cmd
979 setline(1, 'aaa')
980 s/./b/
981 assert_equal('baa', getline(1))
982 endfor
983
984 set nogdefault
985 bwipe!
986enddef
987
Bram Moolenaar179eb562020-12-27 18:03:22 +0100988def g:SomeComplFunc(findstart: number, base: string): any
989 if findstart
990 return 0
991 else
992 return ['aaa', 'bbb']
993 endif
994enddef
995
996def Test_insert_complete()
997 # this was running into an error with the matchparen hack
998 new
999 set completefunc=SomeComplFunc
1000 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
1001 assert_equal('aaa', getline(1))
1002
1003 set completefunc=
1004 bwipe!
1005enddef
1006
Bram Moolenaara11919f2021-01-02 19:44:56 +01001007def Test_wincmd()
1008 split
1009 var id1 = win_getid()
1010 if true
1011 try | wincmd w | catch | endtry
1012 endif
1013 assert_notequal(id1, win_getid())
1014 close
1015enddef
1016
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001017def Test_windo_missing_endif()
1018 var lines =<< trim END
1019 windo if 1
1020 END
1021 CheckDefExecFailure(lines, 'E171:', 1)
1022enddef
1023
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001024" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker