blob: 2f8fd71e95393abe4f2d84504b9463a7b461b5fd [file] [log] [blame]
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001" Test commands that are not compiled in a :def function
2
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003source check.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004source vim9.vim
Bram Moolenaare88c8e82020-11-01 17:03:37 +01005source term_util.vim
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007
8def Test_edit_wildcards()
Bram Moolenaarac564082020-09-27 19:05:33 +02009 var filename = 'Xtest'
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010 edit `=filename`
11 assert_equal('Xtest', bufname())
12
Bram Moolenaarac564082020-09-27 19:05:33 +020013 var filenr = 123
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020014 edit Xtest`=filenr`
15 assert_equal('Xtest123', bufname())
16
17 filenr = 77
18 edit `=filename``=filenr`
19 assert_equal('Xtest77', bufname())
20
21 edit X`=filename`xx`=filenr`yy
22 assert_equal('XXtestxx77yy', bufname())
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010023
24 CheckDefFailure(['edit `=xxx`'], 'E1001:')
25 CheckDefFailure(['edit `="foo"'], 'E1083:')
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020026enddef
27
Bram Moolenaar4389f9c2020-12-27 16:55:11 +010028def Test_expand_alternate_file()
29 var lines =<< trim END
30 edit Xfileone
31 var bone = bufnr()
32 edit Xfiletwo
33 var btwo = bufnr()
34 edit Xfilethree
35 var bthree = bufnr()
36
37 edit #
38 assert_equal(bthree, bufnr())
39 edit %%
40 assert_equal(btwo, bufnr())
41 edit %% # comment
42 assert_equal(bthree, bufnr())
43 edit %%yy
44 assert_equal('Xfiletwoyy', bufname())
45
46 exe "edit %%" .. bone
47 assert_equal(bone, bufnr())
48 exe "edit %%" .. btwo .. "xx"
49 assert_equal('Xfiletwoxx', bufname())
50
51 next Xfileone Xfiletwo Xfilethree
52 assert_equal('Xfileone', argv(0))
53 assert_equal('Xfiletwo', argv(1))
54 assert_equal('Xfilethree', argv(2))
55 next %%%zz
56 assert_equal('Xfileone', argv(0))
57 assert_equal('Xfiletwo', argv(1))
58 assert_equal('Xfilethreezz', argv(2))
59
60 v:oldfiles = ['Xonefile', 'Xtwofile']
61 edit %%<1
62 assert_equal('Xonefile', bufname())
63 edit %%<2
64 assert_equal('Xtwofile', bufname())
65 assert_fails('edit %%<3', 'E684:')
66
67 edit Xfileone.vim
68 edit Xfiletwo
69 edit %%:r
70 assert_equal('Xfileone', bufname())
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +010071
72 assert_false(bufexists('altfoo'))
73 edit altfoo
74 edit bar
75 assert_true(bufexists('altfoo'))
76 assert_true(buflisted('altfoo'))
77 bdel %%
78 assert_true(bufexists('altfoo'))
79 assert_false(buflisted('altfoo'))
80 bwipe! altfoo
81 bwipe! bar
Bram Moolenaar4389f9c2020-12-27 16:55:11 +010082 END
83 CheckDefAndScriptSuccess(lines)
84enddef
85
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +010086def Test_global_backtick_expansion()
87 new
88 setline(1, 'xx')
89 var name = 'foobar'
90 g/^xx/s/.*/`=name`
91 assert_equal('foobar', getline(1))
92 bwipe!
93enddef
94
Bram Moolenaarecac5912021-01-05 19:23:28 +010095def Test_folddo_backtick_expansion()
96 new
97 var name = 'xxx'
98 folddoopen edit `=name`
99 assert_equal('xxx', bufname())
100 bwipe!
101
102 new
103 setline(1, ['one', 'two'])
104 set nomodified
105 :1,2fold
106 foldclose
107 folddoclose edit `=name`
108 assert_equal('xxx', bufname())
109 bwipe!
110enddef
111
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200112def Test_hardcopy_wildcards()
113 CheckUnix
114 CheckFeature postscript
115
Bram Moolenaarac564082020-09-27 19:05:33 +0200116 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200117 hardcopy > X`=outfile`.ps
118 assert_true(filereadable('Xprint.ps'))
119
120 delete('Xprint.ps')
121enddef
122
123def Test_syn_include_wildcards()
124 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +0200125 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200126 &rtp = '.'
127
Bram Moolenaarac564082020-09-27 19:05:33 +0200128 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200129 syn include @Group Xthe`=fname`.vim
130 assert_match('Found.* contained found', execute('syn list Found'))
131
132 &rtp = save_rtp
133 delete('Xthemine.vim')
134enddef
135
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200136def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200137 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200138 vim9script
139 redir @a
140 echo 'one'
141 .. 'two'
142 redir END
143 assert_equal("\nonetwo", @a)
144 END
145 CheckScriptSuccess(lines)
146
147 lines =<< trim END
148 vim9script
149 redir @a
150 echo 11 +
151 77
152 - 22
153 redir END
154 assert_equal("\n66", @a)
155 END
156 CheckScriptSuccess(lines)
157enddef
158
Bram Moolenaar13106602020-10-04 16:06:05 +0200159def Test_condition_types()
160 var lines =<< trim END
161 if 'text'
162 endif
163 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100164 CheckDefAndScriptFailure(lines, 'E1135:', 1)
Bram Moolenaar13106602020-10-04 16:06:05 +0200165
166 lines =<< trim END
167 if [1]
168 endif
169 END
170 CheckDefFailure(lines, 'E1012:', 1)
171 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
172
173 lines =<< trim END
174 g:cond = 'text'
175 if g:cond
176 endif
177 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100178 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200179
180 lines =<< trim END
181 g:cond = 0
182 if g:cond
183 elseif 'text'
184 endif
185 END
186 CheckDefFailure(lines, 'E1012:', 3)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100187 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
Bram Moolenaar13106602020-10-04 16:06:05 +0200188
189 lines =<< trim END
190 if g:cond
191 elseif [1]
192 endif
193 END
194 CheckDefFailure(lines, 'E1012:', 2)
195 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
196
197 lines =<< trim END
198 g:cond = 'text'
199 if 0
200 elseif g:cond
201 endif
202 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100203 CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
Bram Moolenaar13106602020-10-04 16:06:05 +0200204
205 lines =<< trim END
206 while 'text'
207 endwhile
208 END
209 CheckDefFailure(lines, 'E1012:', 1)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100210 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200211
212 lines =<< trim END
213 while [1]
214 endwhile
215 END
216 CheckDefFailure(lines, 'E1012:', 1)
217 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
218
219 lines =<< trim END
220 g:cond = 'text'
221 while g:cond
222 endwhile
223 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100224 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200225enddef
226
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200227def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200228 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200229 vim9script
230 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200231 true
232 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200233 g:res = 42
234 endif
235 assert_equal(42, g:res)
236 END
237 CheckScriptSuccess(lines)
238 unlet g:res
239
240 lines =<< trim END
241 vim9script
242 if 1 &&
243 0
244 g:res = 0
245 elseif 0 ||
246 0
247 || 1
248 g:res = 12
249 endif
250 assert_equal(12, g:res)
251 END
252 CheckScriptSuccess(lines)
253 unlet g:res
254enddef
255
256def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200257 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200258 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200259 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200260 while nr <
261 10 + 3
262 nr = nr
263 + 4
264 endwhile
265 assert_equal(16, nr)
266 END
267 CheckScriptSuccess(lines)
268
269 lines =<< trim END
270 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200271 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200272 while nr
273 <
274 10
275 +
276 3
277 nr = nr
278 +
279 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200280 endwhile
281 assert_equal(16, nr)
282 END
283 CheckScriptSuccess(lines)
284enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200285
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200286def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200287 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200288 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200289 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200290 for x
291 in
292 [1, 2, 3, 4]
293 nr = nr + x
294 endfor
295 assert_equal(10, nr)
296 END
297 CheckScriptSuccess(lines)
298
299 lines =<< trim END
300 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200301 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200302 for x
303 in
304 [1, 2,
305 3, 4
306 ]
307 nr = nr
308 +
309 x
310 endfor
311 assert_equal(10, nr)
312 END
313 CheckScriptSuccess(lines)
314enddef
315
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200316def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200317 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200318 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200319 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200320 func RetArg(
321 arg
322 )
323 let s:res = a:arg
324 endfunc
325 [1,
326 2,
327 3]->RetArg()
328 assert_equal([1, 2, 3], res)
329 END
330 CheckScriptSuccess(lines)
331enddef
332
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +0100333def Test_method_call_whitespace()
334 var lines =<< trim END
335 new
336 var yank = 'text'
337 yank->setline(1)
338 yank ->setline(2)
339 yank-> setline(3)
340 yank -> setline(4)
341 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
342 bwipe!
343 END
344 CheckDefAndScriptSuccess(lines)
345enddef
346
Bram Moolenaar683581e2020-10-22 21:22:58 +0200347def Test_skipped_expr_linebreak()
348 if 0
349 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100350 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200351 endif
352enddef
353
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200354def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100355 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200356 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100357 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200358 test.data
359 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100360 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200361
Bram Moolenaarac564082020-09-27 19:05:33 +0200362 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200363 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100364 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200365 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100366 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200367 END
368 CheckScriptSuccess(lines)
369enddef
370
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200371def Test_bar_after_command()
372 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200373 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200374 redraw | echo x
375 enddef
376 RedrawAndEcho()
377 assert_match('did redraw', Screenline(&lines))
378
Bram Moolenaar788123c2020-07-05 15:32:17 +0200379 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200380 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200381 reg_executing() | echo x
382 enddef
383 CallAndEcho()
384 assert_match('did redraw', Screenline(&lines))
385
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200386 if has('unix')
387 # bar in filter write command does not start new command
388 def WriteToShell()
389 new
390 setline(1, 'some text')
391 w !cat | cat > Xoutfile
392 bwipe!
393 enddef
394 WriteToShell()
395 assert_equal(['some text'], readfile('Xoutfile'))
396 delete('Xoutfile')
397
398 # bar in filter read command does not start new command
399 def ReadFromShell()
400 new
401 r! echo hello there | cat > Xoutfile
402 r !echo again | cat >> Xoutfile
403 bwipe!
404 enddef
405 ReadFromShell()
406 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
407 delete('Xoutfile')
408 endif
409enddef
410
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200411def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100412 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100413 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100414 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200415enddef
416
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100417def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100418 var lines =<< trim END
419 final expected = "\nType Name Content\n c \"c piyo"
420 @a = 'hoge'
421 @b = 'fuga'
422 @c = 'piyo'
423
424 assert_equal(execute('filter /piyo/ registers abc'), expected)
425 END
426 CheckDefAndScriptSuccess(lines)
427enddef
428
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100429def Test_win_command_modifiers()
430 assert_equal(1, winnr('$'))
431
432 set splitright
433 vsplit
434 assert_equal(2, winnr())
435 close
436 aboveleft vsplit
437 assert_equal(1, winnr())
438 close
439 set splitright&
440
441 vsplit
442 assert_equal(1, winnr())
443 close
444 belowright vsplit
445 assert_equal(2, winnr())
446 close
447 rightbelow vsplit
448 assert_equal(2, winnr())
449 close
450
Bram Moolenaar97a19002020-11-01 22:15:44 +0100451 if has('browse')
452 browse set
453 assert_equal('option-window', expand('%'))
454 close
455 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100456
457 vsplit
458 botright split
459 assert_equal(3, winnr())
460 assert_equal(&columns, winwidth(0))
461 close
462 close
463
464 vsplit
465 topleft split
466 assert_equal(1, winnr())
467 assert_equal(&columns, winwidth(0))
468 close
469 close
470
471 gettabinfo()->len()->assert_equal(1)
472 tab split
473 gettabinfo()->len()->assert_equal(2)
474 tabclose
475
476 vertical new
477 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
478 close
479enddef
480
481func Test_command_modifier_confirm()
482 CheckNotGui
483 CheckRunVimInTerminal
484
485 " Test for saving all the modified buffers
486 let lines =<< trim END
487 call setline(1, 'changed')
488 def Getout()
489 confirm write Xfile
490 enddef
491 END
492 call writefile(lines, 'Xconfirmscript')
493 call writefile(['empty'], 'Xfile')
494 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
495 call term_sendkeys(buf, ":call Getout()\n")
496 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
497 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100498 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
499 call term_sendkeys(buf, "\<CR>")
500 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100501 call StopVimInTerminal(buf)
502
503 call assert_equal(['changed'], readfile('Xfile'))
504 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100505 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100506 call delete('Xconfirmscript')
507endfunc
508
509def Test_command_modifiers_keep()
510 if has('unix')
511 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
512 new
513 setline(1, ['one', 'two', 'three'])
514 normal 1Gma
515 normal 2Gmb
516 normal 3Gmc
517 if addRflag
518 set cpo+=R
519 else
520 set cpo-=R
521 endif
522 if keepMarks
523 keepmarks :%!cat
524 else
525 :%!cat
526 endif
527 if hasMarks
528 assert_equal(1, line("'a"))
529 assert_equal(2, line("'b"))
530 assert_equal(3, line("'c"))
531 else
532 assert_equal(0, line("'a"))
533 assert_equal(0, line("'b"))
534 assert_equal(0, line("'c"))
535 endif
536 quit!
537 enddef
538 DoTest(false, false, true)
539 DoTest(true, false, false)
540 DoTest(false, true, true)
541 DoTest(true, true, true)
542 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100543
544 new
545 setline(1, ['one', 'two', 'three', 'four'])
546 assert_equal(4, line("$"))
547 normal 1Gma
548 normal 2Gmb
549 normal 3Gmc
550 lockmarks :1,2!wc
551 # line is deleted, marks don't move
552 assert_equal(3, line("$"))
553 assert_equal('four', getline(3))
554 assert_equal(1, line("'a"))
555 assert_equal(2, line("'b"))
556 assert_equal(3, line("'c"))
557 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100558 endif
559
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100560 edit Xone
561 edit Xtwo
562 assert_equal('Xone', expand('#'))
563 keepalt edit Xthree
564 assert_equal('Xone', expand('#'))
565
566 normal /a*b*
567 assert_equal('a*b*', histget("search"))
568 keeppatterns normal /c*d*
569 assert_equal('a*b*', histget("search"))
570
571 new
572 setline(1, range(10))
573 :10
574 normal gg
575 assert_equal(10, getpos("''")[1])
576 keepjumps normal 5G
577 assert_equal(10, getpos("''")[1])
578 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100579enddef
580
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100581def Test_bar_line_continuation()
582 var lines =<< trim END
583 au BufNewFile Xfile g:readFile = 1
584 | g:readExtra = 2
585 g:readFile = 0
586 g:readExtra = 0
587 edit Xfile
588 assert_equal(1, g:readFile)
589 assert_equal(2, g:readExtra)
590 bwipe!
591 au! BufNewFile
592
593 au BufNewFile Xfile g:readFile = 1
594 | g:readExtra = 2
595 | g:readMore = 3
596 g:readFile = 0
597 g:readExtra = 0
598 g:readMore = 0
599 edit Xfile
600 assert_equal(1, g:readFile)
601 assert_equal(2, g:readExtra)
602 assert_equal(3, g:readMore)
603 bwipe!
604 au! BufNewFile
605 unlet g:readFile
606 unlet g:readExtra
607 unlet g:readMore
608 END
609 CheckDefAndScriptSuccess(lines)
610enddef
611
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100612def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100613 new Xsomefile
614 setline(1, 'changed')
615 var buf = bufnr()
616 hide edit Xotherfile
617 var info = getbufinfo(buf)
618 assert_equal(1, info[0].hidden)
619 assert_equal(1, info[0].changed)
620 edit Xsomefile
621 bwipe!
622
623 au BufNewFile Xfile g:readFile = 1
624 g:readFile = 0
625 edit Xfile
626 assert_equal(1, g:readFile)
627 bwipe!
628 g:readFile = 0
629 noautocmd edit Xfile
630 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100631 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100632 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100633
634 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100635 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100636 bwipe!
637
638 var caught = false
639 try
640 sandbox !ls
641 catch /E48:/
642 caught = true
643 endtry
644 assert_true(caught)
645
646 :8verbose g:verbose_now = &verbose
647 assert_equal(8, g:verbose_now)
648 unlet g:verbose_now
649enddef
650
651def EchoHere()
652 echomsg 'here'
653enddef
654def EchoThere()
655 unsilent echomsg 'there'
656enddef
657
658def Test_modifier_silent_unsilent()
659 echomsg 'last one'
660 silent echomsg "text"
661 assert_equal("\nlast one", execute(':1messages'))
662
663 silent! echoerr "error"
664
665 echomsg 'last one'
666 silent EchoHere()
667 assert_equal("\nlast one", execute(':1messages'))
668
669 silent EchoThere()
670 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100671
672 try
673 silent eval [][0]
674 catch
675 echomsg "caught"
676 endtry
677 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100678enddef
679
Bram Moolenaar36113e42020-11-02 21:08:47 +0100680def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100681 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100682 new
683 setline(1, 'xxx')
684 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
685 assert_equal('', getline(1))
686 bwipe!
687enddef
688
Bram Moolenaarece0b872021-01-08 20:40:45 +0100689def Test_silent_pattern()
690 new
691 silent! :/pat/put _
692 bwipe!
693enddef
694
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200695def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200696 var from = 3
697 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200698 g:val = 111
699 def Increment(nrs: list<number>)
700 for nr in nrs
701 g:val += nr
702 endfor
703 enddef
704 eval range(from, to)
705 ->Increment()
706 assert_equal(111 + 3 + 4 + 5, g:val)
707 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100708
709 var lines =<< trim END
710 vim9script
711 g:caught = 'no'
712 try
713 eval 123 || 0
714 catch
715 g:caught = 'yes'
716 endtry
717 assert_equal('yes', g:caught)
718 unlet g:caught
719 END
720 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200721enddef
722
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200723def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200724 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200725 nnoremap <F3> :echo 'hit F3 #'<CR>
726 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
727 END
728 CheckDefSuccess(lines)
729 CheckScriptSuccess(['vim9script'] + lines)
730enddef
731
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200732def Test_normal_command()
733 new
734 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200735 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200736 try
737 exe "norm! \<C-]>"
738 catch /E433/
739 caught = 2
740 endtry
741 assert_equal(2, caught)
742
743 try
744 exe "norm! 3\<C-]>"
745 catch /E433/
746 caught = 3
747 endtry
748 assert_equal(3, caught)
749 bwipe!
750enddef
751
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200752def Test_put_command()
753 new
754 @p = 'ppp'
755 put p
756 assert_equal('ppp', getline(2))
757
758 put ='below'
759 assert_equal('below', getline(3))
760 put! ='above'
761 assert_equal('above', getline(3))
762 assert_equal('below', getline(4))
763
Bram Moolenaar883cf972021-01-15 18:04:43 +0100764 :2put =['a', 'b', 'c']
765 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
766
Bram Moolenaar08597872020-12-10 19:43:40 +0100767 # compute range at runtime
768 setline(1, range(1, 8))
769 @a = 'aaa'
770 :$-2put a
771 assert_equal('aaa', getline(7))
772
773 setline(1, range(1, 8))
774 :2
775 :+2put! a
776 assert_equal('aaa', getline(4))
777
Bram Moolenaara28639e2021-01-19 22:48:09 +0100778 []->mapnew(() => 0)
779 :$put ='end'
780 assert_equal('end', getline('$'))
781
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200782 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100783
784 CheckDefFailure(['put =xxx'], 'E1001:')
785enddef
786
787def Test_put_with_linebreak()
788 new
789 var lines =<< trim END
790 vim9script
791 pu =split('abc', '\zs')
792 ->join()
793 END
794 CheckScriptSuccess(lines)
795 getline(2)->assert_equal('a b c')
796 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200797enddef
798
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200799def Test_command_star_range()
800 new
801 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
802 setpos("'<", [0, 1, 0, 0])
803 setpos("'>", [0, 3, 0, 0])
804 :*s/\(foo\|bar\)/baz/g
805 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
806
807 bwipe!
808enddef
809
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200810def Test_f_args()
811 var lines =<< trim END
812 vim9script
813
814 func SaveCmdArgs(...)
815 let g:args = a:000
816 endfunc
817
818 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
819
820 TestFArgs
821 assert_equal([], g:args)
822
823 TestFArgs one two three
824 assert_equal(['one', 'two', 'three'], g:args)
825 END
826 CheckScriptSuccess(lines)
827enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200828
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100829def Test_user_command_comment()
830 command -nargs=1 Comd echom <q-args>
831
832 var lines =<< trim END
833 vim9script
834 Comd # comment
835 END
836 CheckScriptSuccess(lines)
837
838 lines =<< trim END
839 vim9script
840 Comd# comment
841 END
842 CheckScriptFailure(lines, 'E1144:')
843
844 delcommand Comd
845enddef
846
Bram Moolenaar95388e32020-11-20 21:07:00 +0100847def Test_star_command()
848 var lines =<< trim END
849 vim9script
850 @s = 'g:success = 8'
851 set cpo+=*
852 exe '*s'
853 assert_equal(8, g:success)
854 unlet g:success
855 set cpo-=*
856 assert_fails("exe '*s'", 'E1050:')
857 END
858 CheckScriptSuccess(lines)
859enddef
860
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100861def Test_cmd_argument_without_colon()
862 new Xfile
863 setline(1, ['a', 'b', 'c', 'd'])
864 write
865 edit +3 %
866 assert_equal(3, getcurpos()[1])
867 edit +/a %
868 assert_equal(1, getcurpos()[1])
869 bwipe
870 delete('Xfile')
871enddef
872
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100873def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100874 command Cmd1 eval 0
875 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100876 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100877 Cmd
878 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100879 CheckDefAndScriptFailure(lines, 'E464:', 1)
880 delcommand Cmd1
881 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100882enddef
883
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100884def Test_command_not_recognized()
885 var lines =<< trim END
886 d.key = 'asdf'
887 END
888 CheckDefFailure(lines, 'E1146:', 1)
889
890 lines =<< trim END
891 d['key'] = 'asdf'
892 END
893 CheckDefFailure(lines, 'E1146:', 1)
894enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200895
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100896def Test_magic_not_used()
897 new
898 for cmd in ['set magic', 'set nomagic']
899 exe cmd
900 setline(1, 'aaa')
901 s/.../bbb/
902 assert_equal('bbb', getline(1))
903 endfor
904
905 set magic
906 setline(1, 'aaa')
907 assert_fails('s/.\M../bbb/', 'E486:')
908 assert_fails('snomagic/.../bbb/', 'E486:')
909 assert_equal('aaa', getline(1))
910
911 bwipe!
912enddef
913
Bram Moolenaar60f63102020-12-21 20:32:43 +0100914def Test_gdefault_not_used()
915 new
916 for cmd in ['set gdefault', 'set nogdefault']
917 exe cmd
918 setline(1, 'aaa')
919 s/./b/
920 assert_equal('baa', getline(1))
921 endfor
922
923 set nogdefault
924 bwipe!
925enddef
926
Bram Moolenaar179eb562020-12-27 18:03:22 +0100927def g:SomeComplFunc(findstart: number, base: string): any
928 if findstart
929 return 0
930 else
931 return ['aaa', 'bbb']
932 endif
933enddef
934
935def Test_insert_complete()
936 # this was running into an error with the matchparen hack
937 new
938 set completefunc=SomeComplFunc
939 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
940 assert_equal('aaa', getline(1))
941
942 set completefunc=
943 bwipe!
944enddef
945
Bram Moolenaara11919f2021-01-02 19:44:56 +0100946def Test_wincmd()
947 split
948 var id1 = win_getid()
949 if true
950 try | wincmd w | catch | endtry
951 endif
952 assert_notequal(id1, win_getid())
953 close
954enddef
955
Bram Moolenaar9567efa2021-01-11 22:16:30 +0100956def Test_windo_missing_endif()
957 var lines =<< trim END
958 windo if 1
959 END
960 CheckDefExecFailure(lines, 'E171:', 1)
961enddef
962
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200963" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker