blob: a87d9b25a8b35d10ace98d9a558245cb8df9e7fe [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())
23enddef
24
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020025def Test_hardcopy_wildcards()
26 CheckUnix
27 CheckFeature postscript
28
Bram Moolenaarac564082020-09-27 19:05:33 +020029 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020030 hardcopy > X`=outfile`.ps
31 assert_true(filereadable('Xprint.ps'))
32
33 delete('Xprint.ps')
34enddef
35
36def Test_syn_include_wildcards()
37 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +020038 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020039 &rtp = '.'
40
Bram Moolenaarac564082020-09-27 19:05:33 +020041 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020042 syn include @Group Xthe`=fname`.vim
43 assert_match('Found.* contained found', execute('syn list Found'))
44
45 &rtp = save_rtp
46 delete('Xthemine.vim')
47enddef
48
Bram Moolenaar7e8967f2020-06-27 21:56:17 +020049def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +020050 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +020051 vim9script
52 redir @a
53 echo 'one'
54 .. 'two'
55 redir END
56 assert_equal("\nonetwo", @a)
57 END
58 CheckScriptSuccess(lines)
59
60 lines =<< trim END
61 vim9script
62 redir @a
63 echo 11 +
64 77
65 - 22
66 redir END
67 assert_equal("\n66", @a)
68 END
69 CheckScriptSuccess(lines)
70enddef
71
Bram Moolenaar13106602020-10-04 16:06:05 +020072def Test_condition_types()
73 var lines =<< trim END
74 if 'text'
75 endif
76 END
77 CheckDefAndScriptFailure(lines, 'E1030:', 1)
78
79 lines =<< trim END
80 if [1]
81 endif
82 END
83 CheckDefFailure(lines, 'E1012:', 1)
84 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
85
86 lines =<< trim END
87 g:cond = 'text'
88 if g:cond
89 endif
90 END
91 CheckDefExecAndScriptFailure(lines, 'E1030:', 2)
92
93 lines =<< trim END
94 g:cond = 0
95 if g:cond
96 elseif 'text'
97 endif
98 END
99 CheckDefFailure(lines, 'E1012:', 3)
100 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 4)
101
102 lines =<< trim END
103 if g:cond
104 elseif [1]
105 endif
106 END
107 CheckDefFailure(lines, 'E1012:', 2)
108 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
109
110 lines =<< trim END
111 g:cond = 'text'
112 if 0
113 elseif g:cond
114 endif
115 END
116 CheckDefExecAndScriptFailure(lines, 'E1030:', 3)
117
118 lines =<< trim END
119 while 'text'
120 endwhile
121 END
122 CheckDefFailure(lines, 'E1012:', 1)
123 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 2)
124
125 lines =<< trim END
126 while [1]
127 endwhile
128 END
129 CheckDefFailure(lines, 'E1012:', 1)
130 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
131
132 lines =<< trim END
133 g:cond = 'text'
134 while g:cond
135 endwhile
136 END
137 CheckDefExecAndScriptFailure(lines, 'E1030:', 2)
138enddef
139
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200140def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200141 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200142 vim9script
143 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200144 true
145 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200146 g:res = 42
147 endif
148 assert_equal(42, g:res)
149 END
150 CheckScriptSuccess(lines)
151 unlet g:res
152
153 lines =<< trim END
154 vim9script
155 if 1 &&
156 0
157 g:res = 0
158 elseif 0 ||
159 0
160 || 1
161 g:res = 12
162 endif
163 assert_equal(12, g:res)
164 END
165 CheckScriptSuccess(lines)
166 unlet g:res
167enddef
168
169def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200170 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200171 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200172 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200173 while nr <
174 10 + 3
175 nr = nr
176 + 4
177 endwhile
178 assert_equal(16, nr)
179 END
180 CheckScriptSuccess(lines)
181
182 lines =<< trim END
183 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200184 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200185 while nr
186 <
187 10
188 +
189 3
190 nr = nr
191 +
192 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200193 endwhile
194 assert_equal(16, nr)
195 END
196 CheckScriptSuccess(lines)
197enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200198
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200199def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200200 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200201 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200202 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200203 for x
204 in
205 [1, 2, 3, 4]
206 nr = nr + x
207 endfor
208 assert_equal(10, nr)
209 END
210 CheckScriptSuccess(lines)
211
212 lines =<< trim END
213 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200214 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200215 for x
216 in
217 [1, 2,
218 3, 4
219 ]
220 nr = nr
221 +
222 x
223 endfor
224 assert_equal(10, nr)
225 END
226 CheckScriptSuccess(lines)
227enddef
228
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200229def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200230 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200231 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200232 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200233 func RetArg(
234 arg
235 )
236 let s:res = a:arg
237 endfunc
238 [1,
239 2,
240 3]->RetArg()
241 assert_equal([1, 2, 3], res)
242 END
243 CheckScriptSuccess(lines)
244enddef
245
Bram Moolenaar683581e2020-10-22 21:22:58 +0200246def Test_skipped_expr_linebreak()
247 if 0
248 var x = []
249 ->map({ -> 0})
250 endif
251enddef
252
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200253def Test_dict_member()
Bram Moolenaarac564082020-09-27 19:05:33 +0200254 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200255 test.data->sort()
256 assert_equal(#{data: [1, 2, 3]}, test)
257 test.data
258 ->reverse()
259 assert_equal(#{data: [3, 2, 1]}, test)
260
Bram Moolenaarac564082020-09-27 19:05:33 +0200261 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200262 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200263 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200264 test.data->sort()
265 assert_equal(#{data: [1, 2, 3]}, test)
266 END
267 CheckScriptSuccess(lines)
268enddef
269
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200270def Test_bar_after_command()
271 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200272 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200273 redraw | echo x
274 enddef
275 RedrawAndEcho()
276 assert_match('did redraw', Screenline(&lines))
277
Bram Moolenaar788123c2020-07-05 15:32:17 +0200278 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200279 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200280 reg_executing() | echo x
281 enddef
282 CallAndEcho()
283 assert_match('did redraw', Screenline(&lines))
284
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200285 if has('unix')
286 # bar in filter write command does not start new command
287 def WriteToShell()
288 new
289 setline(1, 'some text')
290 w !cat | cat > Xoutfile
291 bwipe!
292 enddef
293 WriteToShell()
294 assert_equal(['some text'], readfile('Xoutfile'))
295 delete('Xoutfile')
296
297 # bar in filter read command does not start new command
298 def ReadFromShell()
299 new
300 r! echo hello there | cat > Xoutfile
301 r !echo again | cat >> Xoutfile
302 bwipe!
303 enddef
304 ReadFromShell()
305 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
306 delete('Xoutfile')
307 endif
308enddef
309
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200310def Test_filter_is_not_modifier()
Bram Moolenaarac564082020-09-27 19:05:33 +0200311 var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200312 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
313 assert_equal([#{x: 3, y: 4}], tags)
314enddef
315
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100316def Test_command_modifier_filter()
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100317 var lines =<< trim END
318 final expected = "\nType Name Content\n c \"c piyo"
319 @a = 'hoge'
320 @b = 'fuga'
321 @c = 'piyo'
322
323 assert_equal(execute('filter /piyo/ registers abc'), expected)
324 END
325 CheckDefAndScriptSuccess(lines)
326enddef
327
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100328def Test_win_command_modifiers()
329 assert_equal(1, winnr('$'))
330
331 set splitright
332 vsplit
333 assert_equal(2, winnr())
334 close
335 aboveleft vsplit
336 assert_equal(1, winnr())
337 close
338 set splitright&
339
340 vsplit
341 assert_equal(1, winnr())
342 close
343 belowright vsplit
344 assert_equal(2, winnr())
345 close
346 rightbelow vsplit
347 assert_equal(2, winnr())
348 close
349
350 browse set
351 assert_equal('option-window', expand('%'))
352 close
353
354 vsplit
355 botright split
356 assert_equal(3, winnr())
357 assert_equal(&columns, winwidth(0))
358 close
359 close
360
361 vsplit
362 topleft split
363 assert_equal(1, winnr())
364 assert_equal(&columns, winwidth(0))
365 close
366 close
367
368 gettabinfo()->len()->assert_equal(1)
369 tab split
370 gettabinfo()->len()->assert_equal(2)
371 tabclose
372
373 vertical new
374 assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
375 close
376enddef
377
378func Test_command_modifier_confirm()
379 CheckNotGui
380 CheckRunVimInTerminal
381
382 " Test for saving all the modified buffers
383 let lines =<< trim END
384 call setline(1, 'changed')
385 def Getout()
386 confirm write Xfile
387 enddef
388 END
389 call writefile(lines, 'Xconfirmscript')
390 call writefile(['empty'], 'Xfile')
391 let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
392 call term_sendkeys(buf, ":call Getout()\n")
393 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
394 call term_sendkeys(buf, "y")
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100395 call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
396 call term_sendkeys(buf, "\<CR>")
397 call TermWait(buf)
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100398 call StopVimInTerminal(buf)
399
400 call assert_equal(['changed'], readfile('Xfile'))
401 call delete('Xfile')
Bram Moolenaar645cd3e2020-11-01 20:04:57 +0100402 call delete('.Xfile.swp') " in case Vim was killed
Bram Moolenaare88c8e82020-11-01 17:03:37 +0100403 call delete('Xconfirmscript')
404endfunc
405
406def Test_command_modifiers_keep()
407 if has('unix')
408 def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
409 new
410 setline(1, ['one', 'two', 'three'])
411 normal 1Gma
412 normal 2Gmb
413 normal 3Gmc
414 if addRflag
415 set cpo+=R
416 else
417 set cpo-=R
418 endif
419 if keepMarks
420 keepmarks :%!cat
421 else
422 :%!cat
423 endif
424 if hasMarks
425 assert_equal(1, line("'a"))
426 assert_equal(2, line("'b"))
427 assert_equal(3, line("'c"))
428 else
429 assert_equal(0, line("'a"))
430 assert_equal(0, line("'b"))
431 assert_equal(0, line("'c"))
432 endif
433 quit!
434 enddef
435 DoTest(false, false, true)
436 DoTest(true, false, false)
437 DoTest(false, true, true)
438 DoTest(true, true, true)
439 set cpo&vim
440 endif
441
442 # TODO
443 # lockmarks
444 # keepalt
445 # keeppatterns
446 # keepjumps
447enddef
448
449def Test_command_modifier_other()
450 # TODO
451 # hide
452 # noautocmd
453 # noswapfile
454 # sandbox
455 # silent
456 # silent!
457 # unsilent
458 # verbose
459enddef
460
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200461def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200462 var from = 3
463 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200464 g:val = 111
465 def Increment(nrs: list<number>)
466 for nr in nrs
467 g:val += nr
468 endfor
469 enddef
470 eval range(from, to)
471 ->Increment()
472 assert_equal(111 + 3 + 4 + 5, g:val)
473 unlet g:val
474enddef
475
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200476def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200477 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200478 nnoremap <F3> :echo 'hit F3 #'<CR>
479 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
480 END
481 CheckDefSuccess(lines)
482 CheckScriptSuccess(['vim9script'] + lines)
483enddef
484
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200485def Test_normal_command()
486 new
487 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200488 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200489 try
490 exe "norm! \<C-]>"
491 catch /E433/
492 caught = 2
493 endtry
494 assert_equal(2, caught)
495
496 try
497 exe "norm! 3\<C-]>"
498 catch /E433/
499 caught = 3
500 endtry
501 assert_equal(3, caught)
502 bwipe!
503enddef
504
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200505def Test_put_command()
506 new
507 @p = 'ppp'
508 put p
509 assert_equal('ppp', getline(2))
510
511 put ='below'
512 assert_equal('below', getline(3))
513 put! ='above'
514 assert_equal('above', getline(3))
515 assert_equal('below', getline(4))
516
517 bwipe!
518enddef
519
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200520def Test_command_star_range()
521 new
522 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
523 setpos("'<", [0, 1, 0, 0])
524 setpos("'>", [0, 3, 0, 0])
525 :*s/\(foo\|bar\)/baz/g
526 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
527
528 bwipe!
529enddef
530
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200531def Test_f_args()
532 var lines =<< trim END
533 vim9script
534
535 func SaveCmdArgs(...)
536 let g:args = a:000
537 endfunc
538
539 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
540
541 TestFArgs
542 assert_equal([], g:args)
543
544 TestFArgs one two three
545 assert_equal(['one', 'two', 'three'], g:args)
546 END
547 CheckScriptSuccess(lines)
548enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200549
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200550def Test_modifier_silent()
551 echomsg 'last one'
552 silent echomsg "text"
553 redir => g:testmsg
554 :1messages
555 redir END
556 assert_equal("\nlast one", g:testmsg)
557 unlet g:testmsg
558
559 silent! echoerr "error"
560enddef
561
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200562
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200563" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker