blob: 095158c51754b996844c2a8357172d923ec10ae5 [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())
71 END
72 CheckDefAndScriptSuccess(lines)
73enddef
74
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +010075def Test_global_backtick_expansion()
76 new
77 setline(1, 'xx')
78 var name = 'foobar'
79 g/^xx/s/.*/`=name`
80 assert_equal('foobar', getline(1))
81 bwipe!
82enddef
83
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020084def Test_hardcopy_wildcards()
85 CheckUnix
86 CheckFeature postscript
87
Bram Moolenaarac564082020-09-27 19:05:33 +020088 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020089 hardcopy > X`=outfile`.ps
90 assert_true(filereadable('Xprint.ps'))
91
92 delete('Xprint.ps')
93enddef
94
95def Test_syn_include_wildcards()
96 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +020097 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020098 &rtp = '.'
99
Bram Moolenaarac564082020-09-27 19:05:33 +0200100 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200101 syn include @Group Xthe`=fname`.vim
102 assert_match('Found.* contained found', execute('syn list Found'))
103
104 &rtp = save_rtp
105 delete('Xthemine.vim')
106enddef
107
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200108def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200109 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +0200110 vim9script
111 redir @a
112 echo 'one'
113 .. 'two'
114 redir END
115 assert_equal("\nonetwo", @a)
116 END
117 CheckScriptSuccess(lines)
118
119 lines =<< trim END
120 vim9script
121 redir @a
122 echo 11 +
123 77
124 - 22
125 redir END
126 assert_equal("\n66", @a)
127 END
128 CheckScriptSuccess(lines)
129enddef
130
Bram Moolenaar13106602020-10-04 16:06:05 +0200131def Test_condition_types()
132 var lines =<< trim END
133 if 'text'
134 endif
135 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100136 CheckDefAndScriptFailure(lines, 'E1135:', 1)
Bram Moolenaar13106602020-10-04 16:06:05 +0200137
138 lines =<< trim END
139 if [1]
140 endif
141 END
142 CheckDefFailure(lines, 'E1012:', 1)
143 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
144
145 lines =<< trim END
146 g:cond = 'text'
147 if g:cond
148 endif
149 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100150 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200151
152 lines =<< trim END
153 g:cond = 0
154 if g:cond
155 elseif 'text'
156 endif
157 END
158 CheckDefFailure(lines, 'E1012:', 3)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100159 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
Bram Moolenaar13106602020-10-04 16:06:05 +0200160
161 lines =<< trim END
162 if g:cond
163 elseif [1]
164 endif
165 END
166 CheckDefFailure(lines, 'E1012:', 2)
167 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
168
169 lines =<< trim END
170 g:cond = 'text'
171 if 0
172 elseif g:cond
173 endif
174 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100175 CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
Bram Moolenaar13106602020-10-04 16:06:05 +0200176
177 lines =<< trim END
178 while 'text'
179 endwhile
180 END
181 CheckDefFailure(lines, 'E1012:', 1)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100182 CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200183
184 lines =<< trim END
185 while [1]
186 endwhile
187 END
188 CheckDefFailure(lines, 'E1012:', 1)
189 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
190
191 lines =<< trim END
192 g:cond = 'text'
193 while g:cond
194 endwhile
195 END
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100196 CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
Bram Moolenaar13106602020-10-04 16:06:05 +0200197enddef
198
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200199def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200200 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 vim9script
202 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200203 true
204 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200205 g:res = 42
206 endif
207 assert_equal(42, g:res)
208 END
209 CheckScriptSuccess(lines)
210 unlet g:res
211
212 lines =<< trim END
213 vim9script
214 if 1 &&
215 0
216 g:res = 0
217 elseif 0 ||
218 0
219 || 1
220 g:res = 12
221 endif
222 assert_equal(12, g:res)
223 END
224 CheckScriptSuccess(lines)
225 unlet g:res
226enddef
227
228def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200229 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200230 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200231 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200232 while nr <
233 10 + 3
234 nr = nr
235 + 4
236 endwhile
237 assert_equal(16, nr)
238 END
239 CheckScriptSuccess(lines)
240
241 lines =<< trim END
242 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200243 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200244 while nr
245 <
246 10
247 +
248 3
249 nr = nr
250 +
251 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200252 endwhile
253 assert_equal(16, nr)
254 END
255 CheckScriptSuccess(lines)
256enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200257
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200258def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200259 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200260 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200261 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200262 for x
263 in
264 [1, 2, 3, 4]
265 nr = nr + x
266 endfor
267 assert_equal(10, nr)
268 END
269 CheckScriptSuccess(lines)
270
271 lines =<< trim END
272 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200273 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200274 for x
275 in
276 [1, 2,
277 3, 4
278 ]
279 nr = nr
280 +
281 x
282 endfor
283 assert_equal(10, nr)
284 END
285 CheckScriptSuccess(lines)
286enddef
287
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200288def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200289 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200290 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200291 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200292 func RetArg(
293 arg
294 )
295 let s:res = a:arg
296 endfunc
297 [1,
298 2,
299 3]->RetArg()
300 assert_equal([1, 2, 3], res)
301 END
302 CheckScriptSuccess(lines)
303enddef
304
Bram Moolenaar683581e2020-10-22 21:22:58 +0200305def Test_skipped_expr_linebreak()
306 if 0
307 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100308 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200309 endif
310enddef
311
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200312def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100313 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200314 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100315 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200316 test.data
317 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100318 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200319
Bram Moolenaarac564082020-09-27 19:05:33 +0200320 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200321 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100322 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200323 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100324 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200325 END
326 CheckScriptSuccess(lines)
327enddef
328
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200329def Test_bar_after_command()
330 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200331 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200332 redraw | echo x
333 enddef
334 RedrawAndEcho()
335 assert_match('did redraw', Screenline(&lines))
336
Bram Moolenaar788123c2020-07-05 15:32:17 +0200337 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200338 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200339 reg_executing() | echo x
340 enddef
341 CallAndEcho()
342 assert_match('did redraw', Screenline(&lines))
343
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200344 if has('unix')
345 # bar in filter write command does not start new command
346 def WriteToShell()
347 new
348 setline(1, 'some text')
349 w !cat | cat > Xoutfile
350 bwipe!
351 enddef
352 WriteToShell()
353 assert_equal(['some text'], readfile('Xoutfile'))
354 delete('Xoutfile')
355
356 # bar in filter read command does not start new command
357 def ReadFromShell()
358 new
359 r! echo hello there | cat > Xoutfile
360 r !echo again | cat >> Xoutfile
361 bwipe!
362 enddef
363 ReadFromShell()
364 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
365 delete('Xoutfile')
366 endif
367enddef
368
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200369def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100370 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100371 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100372 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200373enddef
374
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100375def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100376 var lines =<< trim END
377 final expected = "\nType Name Content\n c \"c piyo"
378 @a = 'hoge'
379 @b = 'fuga'
380 @c = 'piyo'
381
382 assert_equal(execute('filter /piyo/ registers abc'), expected)
383 END
384 CheckDefAndScriptSuccess(lines)
385enddef
386
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100387def Test_win_command_modifiers()
388 assert_equal(1, winnr('$'))
389
390 set splitright
391 vsplit
392 assert_equal(2, winnr())
393 close
394 aboveleft vsplit
395 assert_equal(1, winnr())
396 close
397 set splitright&
398
399 vsplit
400 assert_equal(1, winnr())
401 close
402 belowright vsplit
403 assert_equal(2, winnr())
404 close
405 rightbelow vsplit
406 assert_equal(2, winnr())
407 close
408
Bram Moolenaar97a19002020-11-01 22:15:44 +0100409 if has('browse')
410 browse set
411 assert_equal('option-window', expand('%'))
412 close
413 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100414
415 vsplit
416 botright split
417 assert_equal(3, winnr())
418 assert_equal(&columns, winwidth(0))
419 close
420 close
421
422 vsplit
423 topleft split
424 assert_equal(1, winnr())
425 assert_equal(&columns, winwidth(0))
426 close
427 close
428
429 gettabinfo()->len()->assert_equal(1)
430 tab split
431 gettabinfo()->len()->assert_equal(2)
432 tabclose
433
434 vertical new
435 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
436 close
437enddef
438
439func Test_command_modifier_confirm()
440 CheckNotGui
441 CheckRunVimInTerminal
442
443 " Test for saving all the modified buffers
444 let lines =<< trim END
445 call setline(1, 'changed')
446 def Getout()
447 confirm write Xfile
448 enddef
449 END
450 call writefile(lines, 'Xconfirmscript')
451 call writefile(['empty'], 'Xfile')
452 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
453 call term_sendkeys(buf, ":call Getout()\n")
454 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
455 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100456 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
457 call term_sendkeys(buf, "\<CR>")
458 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100459 call StopVimInTerminal(buf)
460
461 call assert_equal(['changed'], readfile('Xfile'))
462 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100463 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100464 call delete('Xconfirmscript')
465endfunc
466
467def Test_command_modifiers_keep()
468 if has('unix')
469 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
470 new
471 setline(1, ['one', 'two', 'three'])
472 normal 1Gma
473 normal 2Gmb
474 normal 3Gmc
475 if addRflag
476 set cpo+=R
477 else
478 set cpo-=R
479 endif
480 if keepMarks
481 keepmarks :%!cat
482 else
483 :%!cat
484 endif
485 if hasMarks
486 assert_equal(1, line("'a"))
487 assert_equal(2, line("'b"))
488 assert_equal(3, line("'c"))
489 else
490 assert_equal(0, line("'a"))
491 assert_equal(0, line("'b"))
492 assert_equal(0, line("'c"))
493 endif
494 quit!
495 enddef
496 DoTest(false, false, true)
497 DoTest(true, false, false)
498 DoTest(false, true, true)
499 DoTest(true, true, true)
500 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100501
502 new
503 setline(1, ['one', 'two', 'three', 'four'])
504 assert_equal(4, line("$"))
505 normal 1Gma
506 normal 2Gmb
507 normal 3Gmc
508 lockmarks :1,2!wc
509 # line is deleted, marks don't move
510 assert_equal(3, line("$"))
511 assert_equal('four', getline(3))
512 assert_equal(1, line("'a"))
513 assert_equal(2, line("'b"))
514 assert_equal(3, line("'c"))
515 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100516 endif
517
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100518 edit Xone
519 edit Xtwo
520 assert_equal('Xone', expand('#'))
521 keepalt edit Xthree
522 assert_equal('Xone', expand('#'))
523
524 normal /a*b*
525 assert_equal('a*b*', histget("search"))
526 keeppatterns normal /c*d*
527 assert_equal('a*b*', histget("search"))
528
529 new
530 setline(1, range(10))
531 :10
532 normal gg
533 assert_equal(10, getpos("''")[1])
534 keepjumps normal 5G
535 assert_equal(10, getpos("''")[1])
536 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100537enddef
538
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100539def Test_bar_line_continuation()
540 var lines =<< trim END
541 au BufNewFile Xfile g:readFile = 1
542 | g:readExtra = 2
543 g:readFile = 0
544 g:readExtra = 0
545 edit Xfile
546 assert_equal(1, g:readFile)
547 assert_equal(2, g:readExtra)
548 bwipe!
549 au! BufNewFile
550
551 au BufNewFile Xfile g:readFile = 1
552 | g:readExtra = 2
553 | g:readMore = 3
554 g:readFile = 0
555 g:readExtra = 0
556 g:readMore = 0
557 edit Xfile
558 assert_equal(1, g:readFile)
559 assert_equal(2, g:readExtra)
560 assert_equal(3, g:readMore)
561 bwipe!
562 au! BufNewFile
563 unlet g:readFile
564 unlet g:readExtra
565 unlet g:readMore
566 END
567 CheckDefAndScriptSuccess(lines)
568enddef
569
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100570def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100571 new Xsomefile
572 setline(1, 'changed')
573 var buf = bufnr()
574 hide edit Xotherfile
575 var info = getbufinfo(buf)
576 assert_equal(1, info[0].hidden)
577 assert_equal(1, info[0].changed)
578 edit Xsomefile
579 bwipe!
580
581 au BufNewFile Xfile g:readFile = 1
582 g:readFile = 0
583 edit Xfile
584 assert_equal(1, g:readFile)
585 bwipe!
586 g:readFile = 0
587 noautocmd edit Xfile
588 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100589 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100590 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100591
592 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100593 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100594 bwipe!
595
596 var caught = false
597 try
598 sandbox !ls
599 catch /E48:/
600 caught = true
601 endtry
602 assert_true(caught)
603
604 :8verbose g:verbose_now = &verbose
605 assert_equal(8, g:verbose_now)
606 unlet g:verbose_now
607enddef
608
609def EchoHere()
610 echomsg 'here'
611enddef
612def EchoThere()
613 unsilent echomsg 'there'
614enddef
615
616def Test_modifier_silent_unsilent()
617 echomsg 'last one'
618 silent echomsg "text"
619 assert_equal("\nlast one", execute(':1messages'))
620
621 silent! echoerr "error"
622
623 echomsg 'last one'
624 silent EchoHere()
625 assert_equal("\nlast one", execute(':1messages'))
626
627 silent EchoThere()
628 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100629
630 try
631 silent eval [][0]
632 catch
633 echomsg "caught"
634 endtry
635 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100636enddef
637
Bram Moolenaar36113e42020-11-02 21:08:47 +0100638def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100639 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100640 new
641 setline(1, 'xxx')
642 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
643 assert_equal('', getline(1))
644 bwipe!
645enddef
646
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200647def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200648 var from = 3
649 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200650 g:val = 111
651 def Increment(nrs: list<number>)
652 for nr in nrs
653 g:val += nr
654 endfor
655 enddef
656 eval range(from, to)
657 ->Increment()
658 assert_equal(111 + 3 + 4 + 5, g:val)
659 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100660
661 var lines =<< trim END
662 vim9script
663 g:caught = 'no'
664 try
665 eval 123 || 0
666 catch
667 g:caught = 'yes'
668 endtry
669 assert_equal('yes', g:caught)
670 unlet g:caught
671 END
672 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200673enddef
674
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200675def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200676 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200677 nnoremap <F3> :echo 'hit F3 #'<CR>
678 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
679 END
680 CheckDefSuccess(lines)
681 CheckScriptSuccess(['vim9script'] + lines)
682enddef
683
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200684def Test_normal_command()
685 new
686 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200687 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200688 try
689 exe "norm! \<C-]>"
690 catch /E433/
691 caught = 2
692 endtry
693 assert_equal(2, caught)
694
695 try
696 exe "norm! 3\<C-]>"
697 catch /E433/
698 caught = 3
699 endtry
700 assert_equal(3, caught)
701 bwipe!
702enddef
703
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200704def Test_put_command()
705 new
706 @p = 'ppp'
707 put p
708 assert_equal('ppp', getline(2))
709
710 put ='below'
711 assert_equal('below', getline(3))
712 put! ='above'
713 assert_equal('above', getline(3))
714 assert_equal('below', getline(4))
715
Bram Moolenaar08597872020-12-10 19:43:40 +0100716 # compute range at runtime
717 setline(1, range(1, 8))
718 @a = 'aaa'
719 :$-2put a
720 assert_equal('aaa', getline(7))
721
722 setline(1, range(1, 8))
723 :2
724 :+2put! a
725 assert_equal('aaa', getline(4))
726
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200727 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100728
729 CheckDefFailure(['put =xxx'], 'E1001:')
730enddef
731
732def Test_put_with_linebreak()
733 new
734 var lines =<< trim END
735 vim9script
736 pu =split('abc', '\zs')
737 ->join()
738 END
739 CheckScriptSuccess(lines)
740 getline(2)->assert_equal('a b c')
741 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200742enddef
743
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200744def Test_command_star_range()
745 new
746 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
747 setpos("'<", [0, 1, 0, 0])
748 setpos("'>", [0, 3, 0, 0])
749 :*s/\(foo\|bar\)/baz/g
750 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
751
752 bwipe!
753enddef
754
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200755def Test_f_args()
756 var lines =<< trim END
757 vim9script
758
759 func SaveCmdArgs(...)
760 let g:args = a:000
761 endfunc
762
763 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
764
765 TestFArgs
766 assert_equal([], g:args)
767
768 TestFArgs one two three
769 assert_equal(['one', 'two', 'three'], g:args)
770 END
771 CheckScriptSuccess(lines)
772enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200773
Bram Moolenaar95388e32020-11-20 21:07:00 +0100774def Test_star_command()
775 var lines =<< trim END
776 vim9script
777 @s = 'g:success = 8'
778 set cpo+=*
779 exe '*s'
780 assert_equal(8, g:success)
781 unlet g:success
782 set cpo-=*
783 assert_fails("exe '*s'", 'E1050:')
784 END
785 CheckScriptSuccess(lines)
786enddef
787
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100788def Test_cmd_argument_without_colon()
789 new Xfile
790 setline(1, ['a', 'b', 'c', 'd'])
791 write
792 edit +3 %
793 assert_equal(3, getcurpos()[1])
794 edit +/a %
795 assert_equal(1, getcurpos()[1])
796 bwipe
797 delete('Xfile')
798enddef
799
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100800def Test_ambiguous_user_cmd()
801 var lines =<< trim END
802 com Cmd1 eval 0
803 com Cmd2 eval 0
804 Cmd
805 END
806 CheckScriptFailure(lines, 'E464:')
807enddef
808
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100809def Test_command_not_recognized()
810 var lines =<< trim END
811 d.key = 'asdf'
812 END
813 CheckDefFailure(lines, 'E1146:', 1)
814
815 lines =<< trim END
816 d['key'] = 'asdf'
817 END
818 CheckDefFailure(lines, 'E1146:', 1)
819enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200820
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100821def Test_magic_not_used()
822 new
823 for cmd in ['set magic', 'set nomagic']
824 exe cmd
825 setline(1, 'aaa')
826 s/.../bbb/
827 assert_equal('bbb', getline(1))
828 endfor
829
830 set magic
831 setline(1, 'aaa')
832 assert_fails('s/.\M../bbb/', 'E486:')
833 assert_fails('snomagic/.../bbb/', 'E486:')
834 assert_equal('aaa', getline(1))
835
836 bwipe!
837enddef
838
Bram Moolenaar60f63102020-12-21 20:32:43 +0100839def Test_gdefault_not_used()
840 new
841 for cmd in ['set gdefault', 'set nogdefault']
842 exe cmd
843 setline(1, 'aaa')
844 s/./b/
845 assert_equal('baa', getline(1))
846 endfor
847
848 set nogdefault
849 bwipe!
850enddef
851
Bram Moolenaar179eb562020-12-27 18:03:22 +0100852def g:SomeComplFunc(findstart: number, base: string): any
853 if findstart
854 return 0
855 else
856 return ['aaa', 'bbb']
857 endif
858enddef
859
860def Test_insert_complete()
861 # this was running into an error with the matchparen hack
862 new
863 set completefunc=SomeComplFunc
864 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
865 assert_equal('aaa', getline(1))
866
867 set completefunc=
868 bwipe!
869enddef
870
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200871" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker