blob: 42c51b46cdec358f1ae96332b4b17547a30da6e3 [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 Moolenaard2ef6b32020-07-02 21:11:34 +0200326def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200327 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200328 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200329 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200330 func RetArg(
331 arg
332 )
333 let s:res = a:arg
334 endfunc
335 [1,
336 2,
337 3]->RetArg()
338 assert_equal([1, 2, 3], res)
339 END
340 CheckScriptSuccess(lines)
Bram Moolenaar148be9b2021-02-02 21:33:52 +0100341
342 lines =<< trim END
343 new
344 var name = [1, 2]
345 name
346 ->copy()
347 ->setline(1)
348 assert_equal(['1', '2'], getline(1, 2))
349 bwipe!
350 END
351 CheckDefAndScriptSuccess(lines)
352
353 lines =<< trim END
354 new
355 g:shortlist
356 ->copy()
357 ->setline(1)
358 assert_equal(['1', '2'], getline(1, 2))
359 bwipe!
360 END
361 g:shortlist = [1, 2]
362 CheckDefAndScriptSuccess(lines)
363 unlet g:shortlist
Bram Moolenaar5f195932020-07-01 20:07:14 +0200364enddef
365
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +0100366def Test_method_call_whitespace()
367 var lines =<< trim END
368 new
369 var yank = 'text'
370 yank->setline(1)
371 yank ->setline(2)
372 yank-> setline(3)
373 yank -> setline(4)
374 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
375 bwipe!
376 END
377 CheckDefAndScriptSuccess(lines)
378enddef
379
Bram Moolenaar683581e2020-10-22 21:22:58 +0200380def Test_skipped_expr_linebreak()
381 if 0
382 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100383 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200384 endif
385enddef
386
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200387def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100388 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200389 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100390 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200391 test.data
392 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100393 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200394
Bram Moolenaarac564082020-09-27 19:05:33 +0200395 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200396 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100397 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200398 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100399 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200400 END
401 CheckScriptSuccess(lines)
402enddef
403
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200404def Test_bar_after_command()
405 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200406 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200407 redraw | echo x
408 enddef
409 RedrawAndEcho()
410 assert_match('did redraw', Screenline(&lines))
411
Bram Moolenaar788123c2020-07-05 15:32:17 +0200412 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200413 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200414 reg_executing() | echo x
415 enddef
416 CallAndEcho()
417 assert_match('did redraw', Screenline(&lines))
418
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200419 if has('unix')
420 # bar in filter write command does not start new command
421 def WriteToShell()
422 new
423 setline(1, 'some text')
424 w !cat | cat > Xoutfile
425 bwipe!
426 enddef
427 WriteToShell()
428 assert_equal(['some text'], readfile('Xoutfile'))
429 delete('Xoutfile')
430
431 # bar in filter read command does not start new command
432 def ReadFromShell()
433 new
434 r! echo hello there | cat > Xoutfile
435 r !echo again | cat >> Xoutfile
436 bwipe!
437 enddef
438 ReadFromShell()
439 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
440 delete('Xoutfile')
441 endif
442enddef
443
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200444def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100445 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100446 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100447 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200448enddef
449
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100450def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100451 var lines =<< trim END
452 final expected = "\nType Name Content\n c \"c piyo"
453 @a = 'hoge'
454 @b = 'fuga'
455 @c = 'piyo'
456
457 assert_equal(execute('filter /piyo/ registers abc'), expected)
458 END
459 CheckDefAndScriptSuccess(lines)
460enddef
461
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100462def Test_win_command_modifiers()
463 assert_equal(1, winnr('$'))
464
465 set splitright
466 vsplit
467 assert_equal(2, winnr())
468 close
469 aboveleft vsplit
470 assert_equal(1, winnr())
471 close
472 set splitright&
473
474 vsplit
475 assert_equal(1, winnr())
476 close
477 belowright vsplit
478 assert_equal(2, winnr())
479 close
480 rightbelow vsplit
481 assert_equal(2, winnr())
482 close
483
Bram Moolenaar97a19002020-11-01 22:15:44 +0100484 if has('browse')
485 browse set
486 assert_equal('option-window', expand('%'))
487 close
488 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100489
490 vsplit
491 botright split
492 assert_equal(3, winnr())
493 assert_equal(&columns, winwidth(0))
494 close
495 close
496
497 vsplit
498 topleft split
499 assert_equal(1, winnr())
500 assert_equal(&columns, winwidth(0))
501 close
502 close
503
504 gettabinfo()->len()->assert_equal(1)
505 tab split
506 gettabinfo()->len()->assert_equal(2)
507 tabclose
508
509 vertical new
510 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
511 close
512enddef
513
514func Test_command_modifier_confirm()
515 CheckNotGui
516 CheckRunVimInTerminal
517
518 " Test for saving all the modified buffers
519 let lines =<< trim END
520 call setline(1, 'changed')
521 def Getout()
522 confirm write Xfile
523 enddef
524 END
525 call writefile(lines, 'Xconfirmscript')
526 call writefile(['empty'], 'Xfile')
527 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
528 call term_sendkeys(buf, ":call Getout()\n")
529 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
530 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100531 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
532 call term_sendkeys(buf, "\<CR>")
533 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100534 call StopVimInTerminal(buf)
535
536 call assert_equal(['changed'], readfile('Xfile'))
537 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100538 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100539 call delete('Xconfirmscript')
540endfunc
541
542def Test_command_modifiers_keep()
543 if has('unix')
544 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
545 new
546 setline(1, ['one', 'two', 'three'])
547 normal 1Gma
548 normal 2Gmb
549 normal 3Gmc
550 if addRflag
551 set cpo+=R
552 else
553 set cpo-=R
554 endif
555 if keepMarks
556 keepmarks :%!cat
557 else
558 :%!cat
559 endif
560 if hasMarks
561 assert_equal(1, line("'a"))
562 assert_equal(2, line("'b"))
563 assert_equal(3, line("'c"))
564 else
565 assert_equal(0, line("'a"))
566 assert_equal(0, line("'b"))
567 assert_equal(0, line("'c"))
568 endif
569 quit!
570 enddef
571 DoTest(false, false, true)
572 DoTest(true, false, false)
573 DoTest(false, true, true)
574 DoTest(true, true, true)
575 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100576
577 new
578 setline(1, ['one', 'two', 'three', 'four'])
579 assert_equal(4, line("$"))
580 normal 1Gma
581 normal 2Gmb
582 normal 3Gmc
583 lockmarks :1,2!wc
584 # line is deleted, marks don't move
585 assert_equal(3, line("$"))
586 assert_equal('four', getline(3))
587 assert_equal(1, line("'a"))
588 assert_equal(2, line("'b"))
589 assert_equal(3, line("'c"))
590 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100591 endif
592
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100593 edit Xone
594 edit Xtwo
595 assert_equal('Xone', expand('#'))
596 keepalt edit Xthree
597 assert_equal('Xone', expand('#'))
598
599 normal /a*b*
600 assert_equal('a*b*', histget("search"))
601 keeppatterns normal /c*d*
602 assert_equal('a*b*', histget("search"))
603
604 new
605 setline(1, range(10))
606 :10
607 normal gg
608 assert_equal(10, getpos("''")[1])
609 keepjumps normal 5G
610 assert_equal(10, getpos("''")[1])
611 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100612enddef
613
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100614def Test_bar_line_continuation()
615 var lines =<< trim END
616 au BufNewFile Xfile g:readFile = 1
617 | g:readExtra = 2
618 g:readFile = 0
619 g:readExtra = 0
620 edit Xfile
621 assert_equal(1, g:readFile)
622 assert_equal(2, g:readExtra)
623 bwipe!
624 au! BufNewFile
625
626 au BufNewFile Xfile g:readFile = 1
627 | g:readExtra = 2
628 | g:readMore = 3
629 g:readFile = 0
630 g:readExtra = 0
631 g:readMore = 0
632 edit Xfile
633 assert_equal(1, g:readFile)
634 assert_equal(2, g:readExtra)
635 assert_equal(3, g:readMore)
636 bwipe!
637 au! BufNewFile
638 unlet g:readFile
639 unlet g:readExtra
640 unlet g:readMore
641 END
642 CheckDefAndScriptSuccess(lines)
643enddef
644
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100645def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100646 new Xsomefile
647 setline(1, 'changed')
648 var buf = bufnr()
649 hide edit Xotherfile
650 var info = getbufinfo(buf)
651 assert_equal(1, info[0].hidden)
652 assert_equal(1, info[0].changed)
653 edit Xsomefile
654 bwipe!
655
656 au BufNewFile Xfile g:readFile = 1
657 g:readFile = 0
658 edit Xfile
659 assert_equal(1, g:readFile)
660 bwipe!
661 g:readFile = 0
662 noautocmd edit Xfile
663 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100664 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100665 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100666
667 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100668 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100669 bwipe!
670
671 var caught = false
672 try
673 sandbox !ls
674 catch /E48:/
675 caught = true
676 endtry
677 assert_true(caught)
678
679 :8verbose g:verbose_now = &verbose
680 assert_equal(8, g:verbose_now)
681 unlet g:verbose_now
682enddef
683
684def EchoHere()
685 echomsg 'here'
686enddef
687def EchoThere()
688 unsilent echomsg 'there'
689enddef
690
691def Test_modifier_silent_unsilent()
692 echomsg 'last one'
693 silent echomsg "text"
694 assert_equal("\nlast one", execute(':1messages'))
695
696 silent! echoerr "error"
697
698 echomsg 'last one'
699 silent EchoHere()
700 assert_equal("\nlast one", execute(':1messages'))
701
702 silent EchoThere()
703 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100704
705 try
706 silent eval [][0]
707 catch
708 echomsg "caught"
709 endtry
710 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100711enddef
712
Bram Moolenaar36113e42020-11-02 21:08:47 +0100713def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100714 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100715 new
716 setline(1, 'xxx')
717 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
718 assert_equal('', getline(1))
719 bwipe!
720enddef
721
Bram Moolenaarece0b872021-01-08 20:40:45 +0100722def Test_silent_pattern()
723 new
724 silent! :/pat/put _
725 bwipe!
726enddef
727
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200728def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200729 var from = 3
730 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200731 g:val = 111
732 def Increment(nrs: list<number>)
733 for nr in nrs
734 g:val += nr
735 endfor
736 enddef
737 eval range(from, to)
738 ->Increment()
739 assert_equal(111 + 3 + 4 + 5, g:val)
740 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100741
742 var lines =<< trim END
743 vim9script
744 g:caught = 'no'
745 try
746 eval 123 || 0
747 catch
748 g:caught = 'yes'
749 endtry
750 assert_equal('yes', g:caught)
751 unlet g:caught
752 END
753 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200754enddef
755
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200756def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200757 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200758 nnoremap <F3> :echo 'hit F3 #'<CR>
759 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
760 END
761 CheckDefSuccess(lines)
762 CheckScriptSuccess(['vim9script'] + lines)
763enddef
764
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200765def Test_normal_command()
766 new
767 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200768 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200769 try
770 exe "norm! \<C-]>"
771 catch /E433/
772 caught = 2
773 endtry
774 assert_equal(2, caught)
775
776 try
777 exe "norm! 3\<C-]>"
778 catch /E433/
779 caught = 3
780 endtry
781 assert_equal(3, caught)
782 bwipe!
783enddef
784
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200785def Test_put_command()
786 new
787 @p = 'ppp'
788 put p
789 assert_equal('ppp', getline(2))
790
791 put ='below'
792 assert_equal('below', getline(3))
793 put! ='above'
794 assert_equal('above', getline(3))
795 assert_equal('below', getline(4))
796
Bram Moolenaar883cf972021-01-15 18:04:43 +0100797 :2put =['a', 'b', 'c']
798 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
799
Bram Moolenaar08597872020-12-10 19:43:40 +0100800 # compute range at runtime
801 setline(1, range(1, 8))
802 @a = 'aaa'
803 :$-2put a
804 assert_equal('aaa', getline(7))
805
806 setline(1, range(1, 8))
807 :2
808 :+2put! a
809 assert_equal('aaa', getline(4))
810
Bram Moolenaara28639e2021-01-19 22:48:09 +0100811 []->mapnew(() => 0)
812 :$put ='end'
813 assert_equal('end', getline('$'))
814
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200815 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100816
817 CheckDefFailure(['put =xxx'], 'E1001:')
818enddef
819
820def Test_put_with_linebreak()
821 new
822 var lines =<< trim END
823 vim9script
824 pu =split('abc', '\zs')
825 ->join()
826 END
827 CheckScriptSuccess(lines)
828 getline(2)->assert_equal('a b c')
829 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200830enddef
831
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200832def Test_command_star_range()
833 new
834 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
835 setpos("'<", [0, 1, 0, 0])
836 setpos("'>", [0, 3, 0, 0])
837 :*s/\(foo\|bar\)/baz/g
838 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
839
840 bwipe!
841enddef
842
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200843def Test_f_args()
844 var lines =<< trim END
845 vim9script
846
847 func SaveCmdArgs(...)
848 let g:args = a:000
849 endfunc
850
851 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
852
853 TestFArgs
854 assert_equal([], g:args)
855
856 TestFArgs one two three
857 assert_equal(['one', 'two', 'three'], g:args)
858 END
859 CheckScriptSuccess(lines)
860enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200861
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100862def Test_user_command_comment()
863 command -nargs=1 Comd echom <q-args>
864
865 var lines =<< trim END
866 vim9script
867 Comd # comment
868 END
869 CheckScriptSuccess(lines)
870
871 lines =<< trim END
872 vim9script
873 Comd# comment
874 END
875 CheckScriptFailure(lines, 'E1144:')
876
877 delcommand Comd
878enddef
879
Bram Moolenaar95388e32020-11-20 21:07:00 +0100880def Test_star_command()
881 var lines =<< trim END
882 vim9script
883 @s = 'g:success = 8'
884 set cpo+=*
885 exe '*s'
886 assert_equal(8, g:success)
887 unlet g:success
888 set cpo-=*
889 assert_fails("exe '*s'", 'E1050:')
890 END
891 CheckScriptSuccess(lines)
892enddef
893
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100894def Test_cmd_argument_without_colon()
895 new Xfile
896 setline(1, ['a', 'b', 'c', 'd'])
897 write
898 edit +3 %
899 assert_equal(3, getcurpos()[1])
900 edit +/a %
901 assert_equal(1, getcurpos()[1])
902 bwipe
903 delete('Xfile')
904enddef
905
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100906def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100907 command Cmd1 eval 0
908 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100909 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100910 Cmd
911 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100912 CheckDefAndScriptFailure(lines, 'E464:', 1)
913 delcommand Cmd1
914 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100915enddef
916
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100917def Test_command_not_recognized()
918 var lines =<< trim END
919 d.key = 'asdf'
920 END
921 CheckDefFailure(lines, 'E1146:', 1)
922
923 lines =<< trim END
924 d['key'] = 'asdf'
925 END
926 CheckDefFailure(lines, 'E1146:', 1)
927enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200928
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100929def Test_magic_not_used()
930 new
931 for cmd in ['set magic', 'set nomagic']
932 exe cmd
933 setline(1, 'aaa')
934 s/.../bbb/
935 assert_equal('bbb', getline(1))
936 endfor
937
938 set magic
939 setline(1, 'aaa')
940 assert_fails('s/.\M../bbb/', 'E486:')
941 assert_fails('snomagic/.../bbb/', 'E486:')
942 assert_equal('aaa', getline(1))
943
944 bwipe!
945enddef
946
Bram Moolenaar60f63102020-12-21 20:32:43 +0100947def Test_gdefault_not_used()
948 new
949 for cmd in ['set gdefault', 'set nogdefault']
950 exe cmd
951 setline(1, 'aaa')
952 s/./b/
953 assert_equal('baa', getline(1))
954 endfor
955
956 set nogdefault
957 bwipe!
958enddef
959
Bram Moolenaar179eb562020-12-27 18:03:22 +0100960def g:SomeComplFunc(findstart: number, base: string): any
961 if findstart
962 return 0
963 else
964 return ['aaa', 'bbb']
965 endif
966enddef
967
968def Test_insert_complete()
969 # this was running into an error with the matchparen hack
970 new
971 set completefunc=SomeComplFunc
972 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
973 assert_equal('aaa', getline(1))
974
975 set completefunc=
976 bwipe!
977enddef
978
Bram Moolenaara11919f2021-01-02 19:44:56 +0100979def Test_wincmd()
980 split
981 var id1 = win_getid()
982 if true
983 try | wincmd w | catch | endtry
984 endif
985 assert_notequal(id1, win_getid())
986 close
987enddef
988
Bram Moolenaar9567efa2021-01-11 22:16:30 +0100989def Test_windo_missing_endif()
990 var lines =<< trim END
991 windo if 1
992 END
993 CheckDefExecFailure(lines, 'E171:', 1)
994enddef
995
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200996" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker