1" Test commands that are not compiled in a :def function
2
3source check.vim
4source vim9.vim
5source term_util.vim
6source view_util.vim
7
8def Test_edit_wildcards()
9  var filename = 'Xtest'
10  edit `=filename`
11  assert_equal('Xtest', bufname())
12
13  var filenr = 123
14  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
25def Test_hardcopy_wildcards()
26  CheckUnix
27  CheckFeature postscript
28
29  var outfile = 'print'
30  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')
38  var save_rtp = &rtp
39  &rtp = '.'
40
41  var fname = 'mine'
42  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
49def Test_echo_linebreak()
50  var lines =<< trim END
51      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
72def Test_condition_types()
73  var lines =<< trim END
74      if 'text'
75      endif
76  END
77  CheckDefAndScriptFailure(lines, 'E1135:', 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, 'E1135:', 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, 'E1135:', 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, 'E1135:', 3)
117
118  lines =<< trim END
119      while 'text'
120      endwhile
121  END
122  CheckDefFailure(lines, 'E1012:', 1)
123  CheckScriptFailure(['vim9script'] + lines, 'E1135:', 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, 'E1135:', 2)
138enddef
139
140def Test_if_linebreak()
141  var lines =<< trim END
142      vim9script
143      if 1 &&
144            true
145            || 1
146        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()
170  var lines =<< trim END
171      vim9script
172      var nr = 0
173      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
184      var nr = 0
185      while nr
186            <
187              10
188              +
189              3
190            nr = nr
191                  +
192                  4
193      endwhile
194      assert_equal(16, nr)
195  END
196  CheckScriptSuccess(lines)
197enddef
198
199def Test_for_linebreak()
200  var lines =<< trim END
201      vim9script
202      var nr = 0
203      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
214      var nr = 0
215      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
229def Test_method_call_linebreak()
230  var lines =<< trim END
231      vim9script
232      var res = []
233      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
246def Test_skipped_expr_linebreak()
247  if 0
248    var x = []
249               ->map({ -> 0})
250  endif
251enddef
252
253def Test_dict_member()
254   var test: dict<list<number>> = {'data': [3, 1, 2]}
255   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
261  var lines =<< trim END
262      vim9script
263      var test: dict<list<number>> = {'data': [3, 1, 2]}
264      test.data->sort()
265      assert_equal(#{data: [1, 2, 3]}, test)
266  END
267  CheckScriptSuccess(lines)
268enddef
269
270def Test_bar_after_command()
271  def RedrawAndEcho()
272    var x = 'did redraw'
273    redraw | echo x
274  enddef
275  RedrawAndEcho()
276  assert_match('did redraw', Screenline(&lines))
277
278  def CallAndEcho()
279    var x = 'did redraw'
280    reg_executing() | echo x
281  enddef
282  CallAndEcho()
283  assert_match('did redraw', Screenline(&lines))
284
285  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
310def Test_filter_is_not_modifier()
311  var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
312  filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
313  assert_equal([#{x: 3, y: 4}], tags)
314enddef
315
316def Test_command_modifier_filter()
317  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
328def 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  if has('browse')
351    browse set
352    assert_equal('option-window', expand('%'))
353    close
354  endif
355
356  vsplit
357  botright split
358  assert_equal(3, winnr())
359  assert_equal(&columns, winwidth(0))
360  close
361  close
362
363  vsplit
364  topleft split
365  assert_equal(1, winnr())
366  assert_equal(&columns, winwidth(0))
367  close
368  close
369
370  gettabinfo()->len()->assert_equal(1)
371  tab split
372  gettabinfo()->len()->assert_equal(2)
373  tabclose
374
375  vertical new
376  assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
377  close
378enddef
379
380func Test_command_modifier_confirm()
381  CheckNotGui
382  CheckRunVimInTerminal
383
384  " Test for saving all the modified buffers
385  let lines =<< trim END
386    call setline(1, 'changed')
387    def Getout()
388      confirm write Xfile
389    enddef
390  END
391  call writefile(lines, 'Xconfirmscript')
392  call writefile(['empty'], 'Xfile')
393  let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
394  call term_sendkeys(buf, ":call Getout()\n")
395  call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
396  call term_sendkeys(buf, "y")
397  call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
398  call term_sendkeys(buf, "\<CR>")
399  call TermWait(buf)
400  call StopVimInTerminal(buf)
401
402  call assert_equal(['changed'], readfile('Xfile'))
403  call delete('Xfile')
404  call delete('.Xfile.swp')  " in case Vim was killed
405  call delete('Xconfirmscript')
406endfunc
407
408def Test_command_modifiers_keep()
409  if has('unix')
410    def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
411      new
412      setline(1, ['one', 'two', 'three'])
413      normal 1Gma
414      normal 2Gmb
415      normal 3Gmc
416      if addRflag
417        set cpo+=R
418      else
419        set cpo-=R
420      endif
421      if keepMarks
422        keepmarks :%!cat
423      else
424        :%!cat
425      endif
426      if hasMarks
427        assert_equal(1, line("'a"))
428        assert_equal(2, line("'b"))
429        assert_equal(3, line("'c"))
430      else
431        assert_equal(0, line("'a"))
432        assert_equal(0, line("'b"))
433        assert_equal(0, line("'c"))
434      endif
435      quit!
436    enddef
437    DoTest(false, false, true)
438    DoTest(true, false, false)
439    DoTest(false, true, true)
440    DoTest(true, true, true)
441    set cpo&vim
442
443    new
444    setline(1, ['one', 'two', 'three', 'four'])
445    assert_equal(4, line("$"))
446    normal 1Gma
447    normal 2Gmb
448    normal 3Gmc
449    lockmarks :1,2!wc
450    # line is deleted, marks don't move
451    assert_equal(3, line("$"))
452    assert_equal('four', getline(3))
453    assert_equal(1, line("'a"))
454    assert_equal(2, line("'b"))
455    assert_equal(3, line("'c"))
456    quit!
457  endif
458
459  edit Xone
460  edit Xtwo
461  assert_equal('Xone', expand('#'))
462  keepalt edit Xthree
463  assert_equal('Xone', expand('#'))
464
465  normal /a*b*
466  assert_equal('a*b*', histget("search"))
467  keeppatterns normal /c*d*
468  assert_equal('a*b*', histget("search"))
469
470  new
471  setline(1, range(10))
472  :10
473  normal gg
474  assert_equal(10, getpos("''")[1])
475  keepjumps normal 5G
476  assert_equal(10, getpos("''")[1])
477  quit!
478enddef
479
480def Test_command_modifier_other()
481  new Xsomefile
482  setline(1, 'changed')
483  var buf = bufnr()
484  hide edit Xotherfile
485  var info = getbufinfo(buf)
486  assert_equal(1, info[0].hidden)
487  assert_equal(1, info[0].changed)
488  edit Xsomefile
489  bwipe!
490
491  au BufNewFile Xfile g:readFile = 1
492  g:readFile = 0
493  edit Xfile
494  assert_equal(1, g:readFile)
495  bwipe!
496  g:readFile = 0
497  noautocmd edit Xfile
498  assert_equal(0, g:readFile)
499
500  noswapfile edit XnoSwap
501  assert_equal(0, &l:swapfile)
502  bwipe!
503
504  var caught = false
505  try
506    sandbox !ls
507  catch /E48:/
508    caught = true
509  endtry
510  assert_true(caught)
511
512  :8verbose g:verbose_now = &verbose
513  assert_equal(8, g:verbose_now)
514  unlet g:verbose_now
515enddef
516
517def EchoHere()
518  echomsg 'here'
519enddef
520def EchoThere()
521  unsilent echomsg 'there'
522enddef
523
524def Test_modifier_silent_unsilent()
525  echomsg 'last one'
526  silent echomsg "text"
527  assert_equal("\nlast one", execute(':1messages'))
528
529  silent! echoerr "error"
530
531  echomsg 'last one'
532  silent EchoHere()
533  assert_equal("\nlast one", execute(':1messages'))
534
535  silent EchoThere()
536  assert_equal("\nthere", execute(':1messages'))
537enddef
538
539def Test_range_after_command_modifier()
540  CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050:', 2)
541  new
542  setline(1, 'xxx')
543  CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
544  assert_equal('', getline(1))
545  bwipe!
546enddef
547
548def Test_eval_command()
549  var from = 3
550  var to = 5
551  g:val = 111
552  def Increment(nrs: list<number>)
553    for nr in nrs
554      g:val += nr
555    endfor
556  enddef
557  eval range(from, to)
558        ->Increment()
559  assert_equal(111 + 3 + 4 + 5, g:val)
560  unlet g:val
561enddef
562
563def Test_map_command()
564  var lines =<< trim END
565      nnoremap <F3> :echo 'hit F3 #'<CR>
566      assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
567  END
568  CheckDefSuccess(lines)
569  CheckScriptSuccess(['vim9script'] + lines)
570enddef
571
572def Test_normal_command()
573  new
574  setline(1, 'doesnotexist')
575  var caught = 0
576  try
577    exe "norm! \<C-]>"
578  catch /E433/
579    caught = 2
580  endtry
581  assert_equal(2, caught)
582
583  try
584    exe "norm! 3\<C-]>"
585  catch /E433/
586    caught = 3
587  endtry
588  assert_equal(3, caught)
589  bwipe!
590enddef
591
592def Test_put_command()
593  new
594  @p = 'ppp'
595  put p
596  assert_equal('ppp', getline(2))
597
598  put ='below'
599  assert_equal('below', getline(3))
600  put! ='above'
601  assert_equal('above', getline(3))
602  assert_equal('below', getline(4))
603
604  bwipe!
605enddef
606
607def Test_command_star_range()
608  new
609  setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
610  setpos("'<", [0, 1, 0, 0])
611  setpos("'>", [0, 3, 0, 0])
612  :*s/\(foo\|bar\)/baz/g
613  getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
614
615  bwipe!
616enddef
617
618def Test_f_args()
619  var lines =<< trim END
620    vim9script
621
622    func SaveCmdArgs(...)
623      let g:args = a:000
624    endfunc
625
626    command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
627
628    TestFArgs
629    assert_equal([], g:args)
630
631    TestFArgs one two three
632    assert_equal(['one', 'two', 'three'], g:args)
633  END
634  CheckScriptSuccess(lines)
635enddef
636
637def Test_star_command()
638  var lines =<< trim END
639    vim9script
640    @s = 'g:success = 8'
641    set cpo+=*
642    exe '*s'
643    assert_equal(8, g:success)
644    unlet g:success
645    set cpo-=*
646    assert_fails("exe '*s'", 'E1050:')
647  END
648  CheckScriptSuccess(lines)
649enddef
650
651def Test_cmd_argument_without_colon()
652  new Xfile
653  setline(1, ['a', 'b', 'c', 'd'])
654  write
655  edit +3 %
656  assert_equal(3, getcurpos()[1])
657  edit +/a %
658  assert_equal(1, getcurpos()[1])
659  bwipe
660  delete('Xfile')
661enddef
662
663
664" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
665