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_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
75def 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
84def Test_hardcopy_wildcards()
85  CheckUnix
86  CheckFeature postscript
87
88  var outfile = 'print'
89  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')
97  var save_rtp = &rtp
98  &rtp = '.'
99
100  var fname = 'mine'
101  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
108def Test_echo_linebreak()
109  var lines =<< trim END
110      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
131def Test_condition_types()
132  var lines =<< trim END
133      if 'text'
134      endif
135  END
136  CheckDefAndScriptFailure(lines, 'E1135:', 1)
137
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
150  CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
151
152  lines =<< trim END
153      g:cond = 0
154      if g:cond
155      elseif 'text'
156      endif
157  END
158  CheckDefFailure(lines, 'E1012:', 3)
159  CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
160
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
175  CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
176
177  lines =<< trim END
178      while 'text'
179      endwhile
180  END
181  CheckDefFailure(lines, 'E1012:', 1)
182  CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
183
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
196  CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
197enddef
198
199def Test_if_linebreak()
200  var lines =<< trim END
201      vim9script
202      if 1 &&
203            true
204            || 1
205        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()
229  var lines =<< trim END
230      vim9script
231      var nr = 0
232      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
243      var nr = 0
244      while nr
245            <
246              10
247              +
248              3
249            nr = nr
250                  +
251                  4
252      endwhile
253      assert_equal(16, nr)
254  END
255  CheckScriptSuccess(lines)
256enddef
257
258def Test_for_linebreak()
259  var lines =<< trim END
260      vim9script
261      var nr = 0
262      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
273      var nr = 0
274      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
288def Test_method_call_linebreak()
289  var lines =<< trim END
290      vim9script
291      var res = []
292      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
305def Test_skipped_expr_linebreak()
306  if 0
307    var x = []
308               ->map(() => 0)
309  endif
310enddef
311
312def Test_dict_member()
313   var test: dict<list<number>> = {data: [3, 1, 2]}
314   test.data->sort()
315   assert_equal({data: [1, 2, 3]}, test)
316   test.data
317      ->reverse()
318   assert_equal({data: [3, 2, 1]}, test)
319
320  var lines =<< trim END
321      vim9script
322      var test: dict<list<number>> = {data: [3, 1, 2]}
323      test.data->sort()
324      assert_equal({data: [1, 2, 3]}, test)
325  END
326  CheckScriptSuccess(lines)
327enddef
328
329def Test_bar_after_command()
330  def RedrawAndEcho()
331    var x = 'did redraw'
332    redraw | echo x
333  enddef
334  RedrawAndEcho()
335  assert_match('did redraw', Screenline(&lines))
336
337  def CallAndEcho()
338    var x = 'did redraw'
339    reg_executing() | echo x
340  enddef
341  CallAndEcho()
342  assert_match('did redraw', Screenline(&lines))
343
344  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
369def Test_filter_is_not_modifier()
370  var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
371  filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
372  assert_equal([{x: 3, y: 4}], tags)
373enddef
374
375def Test_command_modifier_filter()
376  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
387def 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
409  if has('browse')
410    browse set
411    assert_equal('option-window', expand('%'))
412    close
413  endif
414
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")
456  call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
457  call term_sendkeys(buf, "\<CR>")
458  call TermWait(buf)
459  call StopVimInTerminal(buf)
460
461  call assert_equal(['changed'], readfile('Xfile'))
462  call delete('Xfile')
463  call delete('.Xfile.swp')  " in case Vim was killed
464  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
501
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!
516  endif
517
518  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!
537enddef
538
539def 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
570def Test_command_modifier_other()
571  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)
589  au! BufNewFile
590  unlet g:readFile
591
592  noswapfile edit XnoSwap
593  assert_equal(false, &l:swapfile)
594  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'))
629
630  try
631    silent eval [][0]
632  catch
633    echomsg "caught"
634  endtry
635  assert_equal("\ncaught", execute(':1messages'))
636enddef
637
638def Test_range_after_command_modifier()
639  CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
640  new
641  setline(1, 'xxx')
642  CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
643  assert_equal('', getline(1))
644  bwipe!
645enddef
646
647def Test_eval_command()
648  var from = 3
649  var to = 5
650  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
660
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)
673enddef
674
675def Test_map_command()
676  var lines =<< trim END
677      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
684def Test_normal_command()
685  new
686  setline(1, 'doesnotexist')
687  var caught = 0
688  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
704def 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
716  # 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
727  bwipe!
728
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!
742enddef
743
744def 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
755def 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
773
774def 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
788def 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
800def 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
809def 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
820
821def 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
839def 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
852def 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
871" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
872