blob: ef0a5fe2e887ee79856d310e4396bc5e548721fb [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)
Bram Moolenaar148be9b2021-02-02 21:33:52 +0100331
332 lines =<< trim END
333 new
334 var name = [1, 2]
335 name
336 ->copy()
337 ->setline(1)
338 assert_equal(['1', '2'], getline(1, 2))
339 bwipe!
340 END
341 CheckDefAndScriptSuccess(lines)
342
343 lines =<< trim END
344 new
345 g:shortlist
346 ->copy()
347 ->setline(1)
348 assert_equal(['1', '2'], getline(1, 2))
349 bwipe!
350 END
351 g:shortlist = [1, 2]
352 CheckDefAndScriptSuccess(lines)
353 unlet g:shortlist
Bram Moolenaar5f195932020-07-01 20:07:14 +0200354enddef
355
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +0100356def Test_method_call_whitespace()
357 var lines =<< trim END
358 new
359 var yank = 'text'
360 yank->setline(1)
361 yank ->setline(2)
362 yank-> setline(3)
363 yank -> setline(4)
364 assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
365 bwipe!
366 END
367 CheckDefAndScriptSuccess(lines)
368enddef
369
Bram Moolenaar683581e2020-10-22 21:22:58 +0200370def Test_skipped_expr_linebreak()
371 if 0
372 var x = []
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100373 ->map(() => 0)
Bram Moolenaar683581e2020-10-22 21:22:58 +0200374 endif
375enddef
376
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200377def Test_dict_member()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100378 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200379 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100380 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200381 test.data
382 ->reverse()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100383 assert_equal({data: [3, 2, 1]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200384
Bram Moolenaarac564082020-09-27 19:05:33 +0200385 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200386 vim9script
Bram Moolenaare0de1712020-12-02 17:36:54 +0100387 var test: dict<list<number>> = {data: [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200388 test.data->sort()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100389 assert_equal({data: [1, 2, 3]}, test)
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200390 END
391 CheckScriptSuccess(lines)
392enddef
393
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200394def Test_bar_after_command()
395 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200396 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200397 redraw | echo x
398 enddef
399 RedrawAndEcho()
400 assert_match('did redraw', Screenline(&lines))
401
Bram Moolenaar788123c2020-07-05 15:32:17 +0200402 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200403 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200404 reg_executing() | echo x
405 enddef
406 CallAndEcho()
407 assert_match('did redraw', Screenline(&lines))
408
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200409 if has('unix')
410 # bar in filter write command does not start new command
411 def WriteToShell()
412 new
413 setline(1, 'some text')
414 w !cat | cat > Xoutfile
415 bwipe!
416 enddef
417 WriteToShell()
418 assert_equal(['some text'], readfile('Xoutfile'))
419 delete('Xoutfile')
420
421 # bar in filter read command does not start new command
422 def ReadFromShell()
423 new
424 r! echo hello there | cat > Xoutfile
425 r !echo again | cat >> Xoutfile
426 bwipe!
427 enddef
428 ReadFromShell()
429 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
430 delete('Xoutfile')
431 endif
432enddef
433
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200434def Test_filter_is_not_modifier()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100435 var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100436 filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
Bram Moolenaare0de1712020-12-02 17:36:54 +0100437 assert_equal([{x: 3, y: 4}], tags)
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200438enddef
439
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100440def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100441 var lines =<< trim END
442 final expected = "\nType Name Content\n c \"c piyo"
443 @a = 'hoge'
444 @b = 'fuga'
445 @c = 'piyo'
446
447 assert_equal(execute('filter /piyo/ registers abc'), expected)
448 END
449 CheckDefAndScriptSuccess(lines)
450enddef
451
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100452def Test_win_command_modifiers()
453 assert_equal(1, winnr('$'))
454
455 set splitright
456 vsplit
457 assert_equal(2, winnr())
458 close
459 aboveleft vsplit
460 assert_equal(1, winnr())
461 close
462 set splitright&
463
464 vsplit
465 assert_equal(1, winnr())
466 close
467 belowright vsplit
468 assert_equal(2, winnr())
469 close
470 rightbelow vsplit
471 assert_equal(2, winnr())
472 close
473
Bram Moolenaar97a19002020-11-01 22:15:44 +0100474 if has('browse')
475 browse set
476 assert_equal('option-window', expand('%'))
477 close
478 endif
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100479
480 vsplit
481 botright split
482 assert_equal(3, winnr())
483 assert_equal(&columns, winwidth(0))
484 close
485 close
486
487 vsplit
488 topleft split
489 assert_equal(1, winnr())
490 assert_equal(&columns, winwidth(0))
491 close
492 close
493
494 gettabinfo()->len()->assert_equal(1)
495 tab split
496 gettabinfo()->len()->assert_equal(2)
497 tabclose
498
499 vertical new
500 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
501 close
502enddef
503
504func Test_command_modifier_confirm()
505 CheckNotGui
506 CheckRunVimInTerminal
507
508 " Test for saving all the modified buffers
509 let lines =<< trim END
510 call setline(1, 'changed')
511 def Getout()
512 confirm write Xfile
513 enddef
514 END
515 call writefile(lines, 'Xconfirmscript')
516 call writefile(['empty'], 'Xfile')
517 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
518 call term_sendkeys(buf, ":call Getout()\n")
519 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
520 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100521 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
522 call term_sendkeys(buf, "\<CR>")
523 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100524 call StopVimInTerminal(buf)
525
526 call assert_equal(['changed'], readfile('Xfile'))
527 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100528 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100529 call delete('Xconfirmscript')
530endfunc
531
532def Test_command_modifiers_keep()
533 if has('unix')
534 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
535 new
536 setline(1, ['one', 'two', 'three'])
537 normal 1Gma
538 normal 2Gmb
539 normal 3Gmc
540 if addRflag
541 set cpo+=R
542 else
543 set cpo-=R
544 endif
545 if keepMarks
546 keepmarks :%!cat
547 else
548 :%!cat
549 endif
550 if hasMarks
551 assert_equal(1, line("'a"))
552 assert_equal(2, line("'b"))
553 assert_equal(3, line("'c"))
554 else
555 assert_equal(0, line("'a"))
556 assert_equal(0, line("'b"))
557 assert_equal(0, line("'c"))
558 endif
559 quit!
560 enddef
561 DoTest(false, false, true)
562 DoTest(true, false, false)
563 DoTest(false, true, true)
564 DoTest(true, true, true)
565 set cpo&vim
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100566
567 new
568 setline(1, ['one', 'two', 'three', 'four'])
569 assert_equal(4, line("$"))
570 normal 1Gma
571 normal 2Gmb
572 normal 3Gmc
573 lockmarks :1,2!wc
574 # line is deleted, marks don't move
575 assert_equal(3, line("$"))
576 assert_equal('four', getline(3))
577 assert_equal(1, line("'a"))
578 assert_equal(2, line("'b"))
579 assert_equal(3, line("'c"))
580 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100581 endif
582
Bram Moolenaarf65b35b2020-11-04 18:02:44 +0100583 edit Xone
584 edit Xtwo
585 assert_equal('Xone', expand('#'))
586 keepalt edit Xthree
587 assert_equal('Xone', expand('#'))
588
589 normal /a*b*
590 assert_equal('a*b*', histget("search"))
591 keeppatterns normal /c*d*
592 assert_equal('a*b*', histget("search"))
593
594 new
595 setline(1, range(10))
596 :10
597 normal gg
598 assert_equal(10, getpos("''")[1])
599 keepjumps normal 5G
600 assert_equal(10, getpos("''")[1])
601 quit!
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100602enddef
603
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100604def Test_bar_line_continuation()
605 var lines =<< trim END
606 au BufNewFile Xfile g:readFile = 1
607 | g:readExtra = 2
608 g:readFile = 0
609 g:readExtra = 0
610 edit Xfile
611 assert_equal(1, g:readFile)
612 assert_equal(2, g:readExtra)
613 bwipe!
614 au! BufNewFile
615
616 au BufNewFile Xfile g:readFile = 1
617 | g:readExtra = 2
618 | g:readMore = 3
619 g:readFile = 0
620 g:readExtra = 0
621 g:readMore = 0
622 edit Xfile
623 assert_equal(1, g:readFile)
624 assert_equal(2, g:readExtra)
625 assert_equal(3, g:readMore)
626 bwipe!
627 au! BufNewFile
628 unlet g:readFile
629 unlet g:readExtra
630 unlet g:readMore
631 END
632 CheckDefAndScriptSuccess(lines)
633enddef
634
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100635def Test_command_modifier_other()
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100636 new Xsomefile
637 setline(1, 'changed')
638 var buf = bufnr()
639 hide edit Xotherfile
640 var info = getbufinfo(buf)
641 assert_equal(1, info[0].hidden)
642 assert_equal(1, info[0].changed)
643 edit Xsomefile
644 bwipe!
645
646 au BufNewFile Xfile g:readFile = 1
647 g:readFile = 0
648 edit Xfile
649 assert_equal(1, g:readFile)
650 bwipe!
651 g:readFile = 0
652 noautocmd edit Xfile
653 assert_equal(0, g:readFile)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100654 au! BufNewFile
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100655 unlet g:readFile
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100656
657 noswapfile edit XnoSwap
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100658 assert_equal(false, &l:swapfile)
Bram Moolenaar66669fc2020-11-04 18:53:35 +0100659 bwipe!
660
661 var caught = false
662 try
663 sandbox !ls
664 catch /E48:/
665 caught = true
666 endtry
667 assert_true(caught)
668
669 :8verbose g:verbose_now = &verbose
670 assert_equal(8, g:verbose_now)
671 unlet g:verbose_now
672enddef
673
674def EchoHere()
675 echomsg 'here'
676enddef
677def EchoThere()
678 unsilent echomsg 'there'
679enddef
680
681def Test_modifier_silent_unsilent()
682 echomsg 'last one'
683 silent echomsg "text"
684 assert_equal("\nlast one", execute(':1messages'))
685
686 silent! echoerr "error"
687
688 echomsg 'last one'
689 silent EchoHere()
690 assert_equal("\nlast one", execute(':1messages'))
691
692 silent EchoThere()
693 assert_equal("\nthere", execute(':1messages'))
Bram Moolenaar20a76292020-12-25 19:47:24 +0100694
695 try
696 silent eval [][0]
697 catch
698 echomsg "caught"
699 endtry
700 assert_equal("\ncaught", execute(':1messages'))
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100701enddef
702
Bram Moolenaar36113e42020-11-02 21:08:47 +0100703def Test_range_after_command_modifier()
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +0100704 CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
Bram Moolenaar36113e42020-11-02 21:08:47 +0100705 new
706 setline(1, 'xxx')
707 CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
708 assert_equal('', getline(1))
709 bwipe!
710enddef
711
Bram Moolenaarece0b872021-01-08 20:40:45 +0100712def Test_silent_pattern()
713 new
714 silent! :/pat/put _
715 bwipe!
716enddef
717
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200718def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200719 var from = 3
720 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200721 g:val = 111
722 def Increment(nrs: list<number>)
723 for nr in nrs
724 g:val += nr
725 endfor
726 enddef
727 eval range(from, to)
728 ->Increment()
729 assert_equal(111 + 3 + 4 + 5, g:val)
730 unlet g:val
Bram Moolenaard0fe6202020-12-05 17:11:12 +0100731
732 var lines =<< trim END
733 vim9script
734 g:caught = 'no'
735 try
736 eval 123 || 0
737 catch
738 g:caught = 'yes'
739 endtry
740 assert_equal('yes', g:caught)
741 unlet g:caught
742 END
743 CheckScriptSuccess(lines)
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200744enddef
745
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200746def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200747 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200748 nnoremap <F3> :echo 'hit F3 #'<CR>
749 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
750 END
751 CheckDefSuccess(lines)
752 CheckScriptSuccess(['vim9script'] + lines)
753enddef
754
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200755def Test_normal_command()
756 new
757 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200758 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200759 try
760 exe "norm! \<C-]>"
761 catch /E433/
762 caught = 2
763 endtry
764 assert_equal(2, caught)
765
766 try
767 exe "norm! 3\<C-]>"
768 catch /E433/
769 caught = 3
770 endtry
771 assert_equal(3, caught)
772 bwipe!
773enddef
774
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200775def Test_put_command()
776 new
777 @p = 'ppp'
778 put p
779 assert_equal('ppp', getline(2))
780
781 put ='below'
782 assert_equal('below', getline(3))
783 put! ='above'
784 assert_equal('above', getline(3))
785 assert_equal('below', getline(4))
786
Bram Moolenaar883cf972021-01-15 18:04:43 +0100787 :2put =['a', 'b', 'c']
788 assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
789
Bram Moolenaar08597872020-12-10 19:43:40 +0100790 # compute range at runtime
791 setline(1, range(1, 8))
792 @a = 'aaa'
793 :$-2put a
794 assert_equal('aaa', getline(7))
795
796 setline(1, range(1, 8))
797 :2
798 :+2put! a
799 assert_equal('aaa', getline(4))
800
Bram Moolenaara28639e2021-01-19 22:48:09 +0100801 []->mapnew(() => 0)
802 :$put ='end'
803 assert_equal('end', getline('$'))
804
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200805 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +0100806
807 CheckDefFailure(['put =xxx'], 'E1001:')
808enddef
809
810def Test_put_with_linebreak()
811 new
812 var lines =<< trim END
813 vim9script
814 pu =split('abc', '\zs')
815 ->join()
816 END
817 CheckScriptSuccess(lines)
818 getline(2)->assert_equal('a b c')
819 bwipe!
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200820enddef
821
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200822def Test_command_star_range()
823 new
824 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
825 setpos("'<", [0, 1, 0, 0])
826 setpos("'>", [0, 3, 0, 0])
827 :*s/\(foo\|bar\)/baz/g
828 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
829
830 bwipe!
831enddef
832
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200833def Test_f_args()
834 var lines =<< trim END
835 vim9script
836
837 func SaveCmdArgs(...)
838 let g:args = a:000
839 endfunc
840
841 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
842
843 TestFArgs
844 assert_equal([], g:args)
845
846 TestFArgs one two three
847 assert_equal(['one', 'two', 'three'], g:args)
848 END
849 CheckScriptSuccess(lines)
850enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200851
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100852def Test_user_command_comment()
853 command -nargs=1 Comd echom <q-args>
854
855 var lines =<< trim END
856 vim9script
857 Comd # comment
858 END
859 CheckScriptSuccess(lines)
860
861 lines =<< trim END
862 vim9script
863 Comd# comment
864 END
865 CheckScriptFailure(lines, 'E1144:')
866
867 delcommand Comd
868enddef
869
Bram Moolenaar95388e32020-11-20 21:07:00 +0100870def Test_star_command()
871 var lines =<< trim END
872 vim9script
873 @s = 'g:success = 8'
874 set cpo+=*
875 exe '*s'
876 assert_equal(8, g:success)
877 unlet g:success
878 set cpo-=*
879 assert_fails("exe '*s'", 'E1050:')
880 END
881 CheckScriptSuccess(lines)
882enddef
883
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100884def Test_cmd_argument_without_colon()
885 new Xfile
886 setline(1, ['a', 'b', 'c', 'd'])
887 write
888 edit +3 %
889 assert_equal(3, getcurpos()[1])
890 edit +/a %
891 assert_equal(1, getcurpos()[1])
892 bwipe
893 delete('Xfile')
894enddef
895
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100896def Test_ambiguous_user_cmd()
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100897 command Cmd1 eval 0
898 command Cmd2 eval 0
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100899 var lines =<< trim END
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100900 Cmd
901 END
Bram Moolenaard1510ee2021-01-04 16:15:58 +0100902 CheckDefAndScriptFailure(lines, 'E464:', 1)
903 delcommand Cmd1
904 delcommand Cmd2
Bram Moolenaar1c0aa972020-12-16 21:43:54 +0100905enddef
906
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100907def Test_command_not_recognized()
908 var lines =<< trim END
909 d.key = 'asdf'
910 END
911 CheckDefFailure(lines, 'E1146:', 1)
912
913 lines =<< trim END
914 d['key'] = 'asdf'
915 END
916 CheckDefFailure(lines, 'E1146:', 1)
917enddef
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200918
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100919def Test_magic_not_used()
920 new
921 for cmd in ['set magic', 'set nomagic']
922 exe cmd
923 setline(1, 'aaa')
924 s/.../bbb/
925 assert_equal('bbb', getline(1))
926 endfor
927
928 set magic
929 setline(1, 'aaa')
930 assert_fails('s/.\M../bbb/', 'E486:')
931 assert_fails('snomagic/.../bbb/', 'E486:')
932 assert_equal('aaa', getline(1))
933
934 bwipe!
935enddef
936
Bram Moolenaar60f63102020-12-21 20:32:43 +0100937def Test_gdefault_not_used()
938 new
939 for cmd in ['set gdefault', 'set nogdefault']
940 exe cmd
941 setline(1, 'aaa')
942 s/./b/
943 assert_equal('baa', getline(1))
944 endfor
945
946 set nogdefault
947 bwipe!
948enddef
949
Bram Moolenaar179eb562020-12-27 18:03:22 +0100950def g:SomeComplFunc(findstart: number, base: string): any
951 if findstart
952 return 0
953 else
954 return ['aaa', 'bbb']
955 endif
956enddef
957
958def Test_insert_complete()
959 # this was running into an error with the matchparen hack
960 new
961 set completefunc=SomeComplFunc
962 feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
963 assert_equal('aaa', getline(1))
964
965 set completefunc=
966 bwipe!
967enddef
968
Bram Moolenaara11919f2021-01-02 19:44:56 +0100969def Test_wincmd()
970 split
971 var id1 = win_getid()
972 if true
973 try | wincmd w | catch | endtry
974 endif
975 assert_notequal(id1, win_getid())
976 close
977enddef
978
Bram Moolenaar9567efa2021-01-11 22:16:30 +0100979def Test_windo_missing_endif()
980 var lines =<< trim END
981 windo if 1
982 END
983 CheckDefExecFailure(lines, 'E171:', 1)
984enddef
985
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200986" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker