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())
23
24  CheckDefFailure(['edit `=xxx`'], 'E1001:')
25  CheckDefFailure(['edit `="foo"'], 'E1083:')
26enddef
27
28def Test_hardcopy_wildcards()
29  CheckUnix
30  CheckFeature postscript
31
32  var outfile = 'print'
33  hardcopy > X`=outfile`.ps
34  assert_true(filereadable('Xprint.ps'))
35
36  delete('Xprint.ps')
37enddef
38
39def Test_syn_include_wildcards()
40  writefile(['syn keyword Found found'], 'Xthemine.vim')
41  var save_rtp = &rtp
42  &rtp = '.'
43
44  var fname = 'mine'
45  syn include @Group Xthe`=fname`.vim
46  assert_match('Found.* contained found', execute('syn list Found'))
47
48  &rtp = save_rtp
49  delete('Xthemine.vim')
50enddef
51
52def Test_echo_linebreak()
53  var lines =<< trim END
54      vim9script
55      redir @a
56      echo 'one'
57            .. 'two'
58      redir END
59      assert_equal("\nonetwo", @a)
60  END
61  CheckScriptSuccess(lines)
62
63  lines =<< trim END
64      vim9script
65      redir @a
66      echo 11 +
67            77
68            - 22
69      redir END
70      assert_equal("\n66", @a)
71  END
72  CheckScriptSuccess(lines)
73enddef
74
75def Test_condition_types()
76  var lines =<< trim END
77      if 'text'
78      endif
79  END
80  CheckDefAndScriptFailure(lines, 'E1135:', 1)
81
82  lines =<< trim END
83      if [1]
84      endif
85  END
86  CheckDefFailure(lines, 'E1012:', 1)
87  CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
88
89  lines =<< trim END
90      g:cond = 'text'
91      if g:cond
92      endif
93  END
94  CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
95
96  lines =<< trim END
97      g:cond = 0
98      if g:cond
99      elseif 'text'
100      endif
101  END
102  CheckDefFailure(lines, 'E1012:', 3)
103  CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
104
105  lines =<< trim END
106      if g:cond
107      elseif [1]
108      endif
109  END
110  CheckDefFailure(lines, 'E1012:', 2)
111  CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
112
113  lines =<< trim END
114      g:cond = 'text'
115      if 0
116      elseif g:cond
117      endif
118  END
119  CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
120
121  lines =<< trim END
122      while 'text'
123      endwhile
124  END
125  CheckDefFailure(lines, 'E1012:', 1)
126  CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
127
128  lines =<< trim END
129      while [1]
130      endwhile
131  END
132  CheckDefFailure(lines, 'E1012:', 1)
133  CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
134
135  lines =<< trim END
136      g:cond = 'text'
137      while g:cond
138      endwhile
139  END
140  CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
141enddef
142
143def Test_if_linebreak()
144  var lines =<< trim END
145      vim9script
146      if 1 &&
147            true
148            || 1
149        g:res = 42
150      endif
151      assert_equal(42, g:res)
152  END
153  CheckScriptSuccess(lines)
154  unlet g:res
155
156  lines =<< trim END
157      vim9script
158      if 1 &&
159            0
160        g:res = 0
161      elseif 0 ||
162              0
163              || 1
164        g:res = 12
165      endif
166      assert_equal(12, g:res)
167  END
168  CheckScriptSuccess(lines)
169  unlet g:res
170enddef
171
172def Test_while_linebreak()
173  var lines =<< trim END
174      vim9script
175      var nr = 0
176      while nr <
177              10 + 3
178            nr = nr
179                  + 4
180      endwhile
181      assert_equal(16, nr)
182  END
183  CheckScriptSuccess(lines)
184
185  lines =<< trim END
186      vim9script
187      var nr = 0
188      while nr
189            <
190              10
191              +
192              3
193            nr = nr
194                  +
195                  4
196      endwhile
197      assert_equal(16, nr)
198  END
199  CheckScriptSuccess(lines)
200enddef
201
202def Test_for_linebreak()
203  var lines =<< trim END
204      vim9script
205      var nr = 0
206      for x
207            in
208              [1, 2, 3, 4]
209          nr = nr + x
210      endfor
211      assert_equal(10, nr)
212  END
213  CheckScriptSuccess(lines)
214
215  lines =<< trim END
216      vim9script
217      var nr = 0
218      for x
219            in
220              [1, 2,
221                  3, 4
222                  ]
223          nr = nr
224                 +
225                  x
226      endfor
227      assert_equal(10, nr)
228  END
229  CheckScriptSuccess(lines)
230enddef
231
232def Test_method_call_linebreak()
233  var lines =<< trim END
234      vim9script
235      var res = []
236      func RetArg(
237            arg
238            )
239            let s:res = a:arg
240      endfunc
241      [1,
242          2,
243          3]->RetArg()
244      assert_equal([1, 2, 3], res)
245  END
246  CheckScriptSuccess(lines)
247enddef
248
249def Test_skipped_expr_linebreak()
250  if 0
251    var x = []
252               ->map({ -> 0})
253  endif
254enddef
255
256def Test_dict_member()
257   var test: dict<list<number>> = {data: [3, 1, 2]}
258   test.data->sort()
259   assert_equal({data: [1, 2, 3]}, test)
260   test.data
261      ->reverse()
262   assert_equal({data: [3, 2, 1]}, test)
263
264  var lines =<< trim END
265      vim9script
266      var test: dict<list<number>> = {data: [3, 1, 2]}
267      test.data->sort()
268      assert_equal({data: [1, 2, 3]}, test)
269  END
270  CheckScriptSuccess(lines)
271enddef
272
273def Test_bar_after_command()
274  def RedrawAndEcho()
275    var x = 'did redraw'
276    redraw | echo x
277  enddef
278  RedrawAndEcho()
279  assert_match('did redraw', Screenline(&lines))
280
281  def CallAndEcho()
282    var x = 'did redraw'
283    reg_executing() | echo x
284  enddef
285  CallAndEcho()
286  assert_match('did redraw', Screenline(&lines))
287
288  if has('unix')
289    # bar in filter write command does not start new command
290    def WriteToShell()
291      new
292      setline(1, 'some text')
293      w !cat | cat > Xoutfile
294      bwipe!
295    enddef
296    WriteToShell()
297    assert_equal(['some text'], readfile('Xoutfile'))
298    delete('Xoutfile')
299
300    # bar in filter read command does not start new command
301    def ReadFromShell()
302      new
303      r! echo hello there | cat > Xoutfile
304      r !echo again | cat >> Xoutfile
305      bwipe!
306    enddef
307    ReadFromShell()
308    assert_equal(['hello there', 'again'], readfile('Xoutfile'))
309    delete('Xoutfile')
310  endif
311enddef
312
313def Test_filter_is_not_modifier()
314  var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
315  filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
316  assert_equal([{x: 3, y: 4}], tags)
317enddef
318
319def Test_command_modifier_filter()
320  var lines =<< trim END
321    final expected = "\nType Name Content\n  c  \"c   piyo"
322    @a = 'hoge'
323    @b = 'fuga'
324    @c = 'piyo'
325
326    assert_equal(execute('filter /piyo/ registers abc'), expected)
327  END
328  CheckDefAndScriptSuccess(lines)
329enddef
330
331def Test_win_command_modifiers()
332  assert_equal(1, winnr('$'))
333
334  set splitright
335  vsplit
336  assert_equal(2, winnr())
337  close
338  aboveleft vsplit
339  assert_equal(1, winnr())
340  close
341  set splitright&
342
343  vsplit
344  assert_equal(1, winnr())
345  close
346  belowright vsplit
347  assert_equal(2, winnr())
348  close
349  rightbelow vsplit
350  assert_equal(2, winnr())
351  close
352
353  if has('browse')
354    browse set
355    assert_equal('option-window', expand('%'))
356    close
357  endif
358
359  vsplit
360  botright split
361  assert_equal(3, winnr())
362  assert_equal(&columns, winwidth(0))
363  close
364  close
365
366  vsplit
367  topleft split
368  assert_equal(1, winnr())
369  assert_equal(&columns, winwidth(0))
370  close
371  close
372
373  gettabinfo()->len()->assert_equal(1)
374  tab split
375  gettabinfo()->len()->assert_equal(2)
376  tabclose
377
378  vertical new
379  assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
380  close
381enddef
382
383func Test_command_modifier_confirm()
384  CheckNotGui
385  CheckRunVimInTerminal
386
387  " Test for saving all the modified buffers
388  let lines =<< trim END
389    call setline(1, 'changed')
390    def Getout()
391      confirm write Xfile
392    enddef
393  END
394  call writefile(lines, 'Xconfirmscript')
395  call writefile(['empty'], 'Xfile')
396  let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
397  call term_sendkeys(buf, ":call Getout()\n")
398  call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
399  call term_sendkeys(buf, "y")
400  call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
401  call term_sendkeys(buf, "\<CR>")
402  call TermWait(buf)
403  call StopVimInTerminal(buf)
404
405  call assert_equal(['changed'], readfile('Xfile'))
406  call delete('Xfile')
407  call delete('.Xfile.swp')  " in case Vim was killed
408  call delete('Xconfirmscript')
409endfunc
410
411def Test_command_modifiers_keep()
412  if has('unix')
413    def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
414      new
415      setline(1, ['one', 'two', 'three'])
416      normal 1Gma
417      normal 2Gmb
418      normal 3Gmc
419      if addRflag
420        set cpo+=R
421      else
422        set cpo-=R
423      endif
424      if keepMarks
425        keepmarks :%!cat
426      else
427        :%!cat
428      endif
429      if hasMarks
430        assert_equal(1, line("'a"))
431        assert_equal(2, line("'b"))
432        assert_equal(3, line("'c"))
433      else
434        assert_equal(0, line("'a"))
435        assert_equal(0, line("'b"))
436        assert_equal(0, line("'c"))
437      endif
438      quit!
439    enddef
440    DoTest(false, false, true)
441    DoTest(true, false, false)
442    DoTest(false, true, true)
443    DoTest(true, true, true)
444    set cpo&vim
445
446    new
447    setline(1, ['one', 'two', 'three', 'four'])
448    assert_equal(4, line("$"))
449    normal 1Gma
450    normal 2Gmb
451    normal 3Gmc
452    lockmarks :1,2!wc
453    # line is deleted, marks don't move
454    assert_equal(3, line("$"))
455    assert_equal('four', getline(3))
456    assert_equal(1, line("'a"))
457    assert_equal(2, line("'b"))
458    assert_equal(3, line("'c"))
459    quit!
460  endif
461
462  edit Xone
463  edit Xtwo
464  assert_equal('Xone', expand('#'))
465  keepalt edit Xthree
466  assert_equal('Xone', expand('#'))
467
468  normal /a*b*
469  assert_equal('a*b*', histget("search"))
470  keeppatterns normal /c*d*
471  assert_equal('a*b*', histget("search"))
472
473  new
474  setline(1, range(10))
475  :10
476  normal gg
477  assert_equal(10, getpos("''")[1])
478  keepjumps normal 5G
479  assert_equal(10, getpos("''")[1])
480  quit!
481enddef
482
483def Test_command_modifier_other()
484  new Xsomefile
485  setline(1, 'changed')
486  var buf = bufnr()
487  hide edit Xotherfile
488  var info = getbufinfo(buf)
489  assert_equal(1, info[0].hidden)
490  assert_equal(1, info[0].changed)
491  edit Xsomefile
492  bwipe!
493
494  au BufNewFile Xfile g:readFile = 1
495  g:readFile = 0
496  edit Xfile
497  assert_equal(1, g:readFile)
498  bwipe!
499  g:readFile = 0
500  noautocmd edit Xfile
501  assert_equal(0, g:readFile)
502
503  noswapfile edit XnoSwap
504  assert_equal(0, &l:swapfile)
505  bwipe!
506
507  var caught = false
508  try
509    sandbox !ls
510  catch /E48:/
511    caught = true
512  endtry
513  assert_true(caught)
514
515  :8verbose g:verbose_now = &verbose
516  assert_equal(8, g:verbose_now)
517  unlet g:verbose_now
518enddef
519
520def EchoHere()
521  echomsg 'here'
522enddef
523def EchoThere()
524  unsilent echomsg 'there'
525enddef
526
527def Test_modifier_silent_unsilent()
528  echomsg 'last one'
529  silent echomsg "text"
530  assert_equal("\nlast one", execute(':1messages'))
531
532  silent! echoerr "error"
533
534  echomsg 'last one'
535  silent EchoHere()
536  assert_equal("\nlast one", execute(':1messages'))
537
538  silent EchoThere()
539  assert_equal("\nthere", execute(':1messages'))
540enddef
541
542def Test_range_after_command_modifier()
543  CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050:', 2)
544  new
545  setline(1, 'xxx')
546  CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
547  assert_equal('', getline(1))
548  bwipe!
549enddef
550
551def Test_eval_command()
552  var from = 3
553  var to = 5
554  g:val = 111
555  def Increment(nrs: list<number>)
556    for nr in nrs
557      g:val += nr
558    endfor
559  enddef
560  eval range(from, to)
561        ->Increment()
562  assert_equal(111 + 3 + 4 + 5, g:val)
563  unlet g:val
564
565  var lines =<< trim END
566    vim9script
567    g:caught = 'no'
568    try
569      eval 123 || 0
570    catch
571      g:caught = 'yes'
572    endtry
573    assert_equal('yes', g:caught)
574    unlet g:caught
575  END
576  CheckScriptSuccess(lines)
577enddef
578
579def Test_map_command()
580  var lines =<< trim END
581      nnoremap <F3> :echo 'hit F3 #'<CR>
582      assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
583  END
584  CheckDefSuccess(lines)
585  CheckScriptSuccess(['vim9script'] + lines)
586enddef
587
588def Test_normal_command()
589  new
590  setline(1, 'doesnotexist')
591  var caught = 0
592  try
593    exe "norm! \<C-]>"
594  catch /E433/
595    caught = 2
596  endtry
597  assert_equal(2, caught)
598
599  try
600    exe "norm! 3\<C-]>"
601  catch /E433/
602    caught = 3
603  endtry
604  assert_equal(3, caught)
605  bwipe!
606enddef
607
608def Test_put_command()
609  new
610  @p = 'ppp'
611  put p
612  assert_equal('ppp', getline(2))
613
614  put ='below'
615  assert_equal('below', getline(3))
616  put! ='above'
617  assert_equal('above', getline(3))
618  assert_equal('below', getline(4))
619
620  # compute range at runtime
621  setline(1, range(1, 8))
622  @a = 'aaa'
623  :$-2put a
624  assert_equal('aaa', getline(7))
625
626  setline(1, range(1, 8))
627  :2
628  :+2put! a
629  assert_equal('aaa', getline(4))
630
631  bwipe!
632
633  CheckDefFailure(['put =xxx'], 'E1001:')
634enddef
635
636def Test_put_with_linebreak()
637  new
638  var lines =<< trim END
639    vim9script
640    pu =split('abc', '\zs')
641            ->join()
642  END
643  CheckScriptSuccess(lines)
644  getline(2)->assert_equal('a b c')
645  bwipe!
646enddef
647
648def Test_command_star_range()
649  new
650  setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
651  setpos("'<", [0, 1, 0, 0])
652  setpos("'>", [0, 3, 0, 0])
653  :*s/\(foo\|bar\)/baz/g
654  getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
655
656  bwipe!
657enddef
658
659def Test_f_args()
660  var lines =<< trim END
661    vim9script
662
663    func SaveCmdArgs(...)
664      let g:args = a:000
665    endfunc
666
667    command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
668
669    TestFArgs
670    assert_equal([], g:args)
671
672    TestFArgs one two three
673    assert_equal(['one', 'two', 'three'], g:args)
674  END
675  CheckScriptSuccess(lines)
676enddef
677
678def Test_star_command()
679  var lines =<< trim END
680    vim9script
681    @s = 'g:success = 8'
682    set cpo+=*
683    exe '*s'
684    assert_equal(8, g:success)
685    unlet g:success
686    set cpo-=*
687    assert_fails("exe '*s'", 'E1050:')
688  END
689  CheckScriptSuccess(lines)
690enddef
691
692def Test_cmd_argument_without_colon()
693  new Xfile
694  setline(1, ['a', 'b', 'c', 'd'])
695  write
696  edit +3 %
697  assert_equal(3, getcurpos()[1])
698  edit +/a %
699  assert_equal(1, getcurpos()[1])
700  bwipe
701  delete('Xfile')
702enddef
703
704def Test_ambiguous_user_cmd()
705  var lines =<< trim END
706      com Cmd1 eval 0
707      com Cmd2 eval 0
708      Cmd
709  END
710  CheckScriptFailure(lines, 'E464:')
711enddef
712
713
714" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
715