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_command_modifier_other()
540  new Xsomefile
541  setline(1, 'changed')
542  var buf = bufnr()
543  hide edit Xotherfile
544  var info = getbufinfo(buf)
545  assert_equal(1, info[0].hidden)
546  assert_equal(1, info[0].changed)
547  edit Xsomefile
548  bwipe!
549
550  au BufNewFile Xfile g:readFile = 1
551  g:readFile = 0
552  edit Xfile
553  assert_equal(1, g:readFile)
554  bwipe!
555  g:readFile = 0
556  noautocmd edit Xfile
557  assert_equal(0, g:readFile)
558
559  noswapfile edit XnoSwap
560  assert_equal(0, &l:swapfile)
561  bwipe!
562
563  var caught = false
564  try
565    sandbox !ls
566  catch /E48:/
567    caught = true
568  endtry
569  assert_true(caught)
570
571  :8verbose g:verbose_now = &verbose
572  assert_equal(8, g:verbose_now)
573  unlet g:verbose_now
574enddef
575
576def EchoHere()
577  echomsg 'here'
578enddef
579def EchoThere()
580  unsilent echomsg 'there'
581enddef
582
583def Test_modifier_silent_unsilent()
584  echomsg 'last one'
585  silent echomsg "text"
586  assert_equal("\nlast one", execute(':1messages'))
587
588  silent! echoerr "error"
589
590  echomsg 'last one'
591  silent EchoHere()
592  assert_equal("\nlast one", execute(':1messages'))
593
594  silent EchoThere()
595  assert_equal("\nthere", execute(':1messages'))
596
597  try
598    silent eval [][0]
599  catch
600    echomsg "caught"
601  endtry
602  assert_equal("\ncaught", execute(':1messages'))
603enddef
604
605def Test_range_after_command_modifier()
606  CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
607  new
608  setline(1, 'xxx')
609  CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
610  assert_equal('', getline(1))
611  bwipe!
612enddef
613
614def Test_eval_command()
615  var from = 3
616  var to = 5
617  g:val = 111
618  def Increment(nrs: list<number>)
619    for nr in nrs
620      g:val += nr
621    endfor
622  enddef
623  eval range(from, to)
624        ->Increment()
625  assert_equal(111 + 3 + 4 + 5, g:val)
626  unlet g:val
627
628  var lines =<< trim END
629    vim9script
630    g:caught = 'no'
631    try
632      eval 123 || 0
633    catch
634      g:caught = 'yes'
635    endtry
636    assert_equal('yes', g:caught)
637    unlet g:caught
638  END
639  CheckScriptSuccess(lines)
640enddef
641
642def Test_map_command()
643  var lines =<< trim END
644      nnoremap <F3> :echo 'hit F3 #'<CR>
645      assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
646  END
647  CheckDefSuccess(lines)
648  CheckScriptSuccess(['vim9script'] + lines)
649enddef
650
651def Test_normal_command()
652  new
653  setline(1, 'doesnotexist')
654  var caught = 0
655  try
656    exe "norm! \<C-]>"
657  catch /E433/
658    caught = 2
659  endtry
660  assert_equal(2, caught)
661
662  try
663    exe "norm! 3\<C-]>"
664  catch /E433/
665    caught = 3
666  endtry
667  assert_equal(3, caught)
668  bwipe!
669enddef
670
671def Test_put_command()
672  new
673  @p = 'ppp'
674  put p
675  assert_equal('ppp', getline(2))
676
677  put ='below'
678  assert_equal('below', getline(3))
679  put! ='above'
680  assert_equal('above', getline(3))
681  assert_equal('below', getline(4))
682
683  # compute range at runtime
684  setline(1, range(1, 8))
685  @a = 'aaa'
686  :$-2put a
687  assert_equal('aaa', getline(7))
688
689  setline(1, range(1, 8))
690  :2
691  :+2put! a
692  assert_equal('aaa', getline(4))
693
694  bwipe!
695
696  CheckDefFailure(['put =xxx'], 'E1001:')
697enddef
698
699def Test_put_with_linebreak()
700  new
701  var lines =<< trim END
702    vim9script
703    pu =split('abc', '\zs')
704            ->join()
705  END
706  CheckScriptSuccess(lines)
707  getline(2)->assert_equal('a b c')
708  bwipe!
709enddef
710
711def Test_command_star_range()
712  new
713  setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
714  setpos("'<", [0, 1, 0, 0])
715  setpos("'>", [0, 3, 0, 0])
716  :*s/\(foo\|bar\)/baz/g
717  getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
718
719  bwipe!
720enddef
721
722def Test_f_args()
723  var lines =<< trim END
724    vim9script
725
726    func SaveCmdArgs(...)
727      let g:args = a:000
728    endfunc
729
730    command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
731
732    TestFArgs
733    assert_equal([], g:args)
734
735    TestFArgs one two three
736    assert_equal(['one', 'two', 'three'], g:args)
737  END
738  CheckScriptSuccess(lines)
739enddef
740
741def Test_star_command()
742  var lines =<< trim END
743    vim9script
744    @s = 'g:success = 8'
745    set cpo+=*
746    exe '*s'
747    assert_equal(8, g:success)
748    unlet g:success
749    set cpo-=*
750    assert_fails("exe '*s'", 'E1050:')
751  END
752  CheckScriptSuccess(lines)
753enddef
754
755def Test_cmd_argument_without_colon()
756  new Xfile
757  setline(1, ['a', 'b', 'c', 'd'])
758  write
759  edit +3 %
760  assert_equal(3, getcurpos()[1])
761  edit +/a %
762  assert_equal(1, getcurpos()[1])
763  bwipe
764  delete('Xfile')
765enddef
766
767def Test_ambiguous_user_cmd()
768  var lines =<< trim END
769      com Cmd1 eval 0
770      com Cmd2 eval 0
771      Cmd
772  END
773  CheckScriptFailure(lines, 'E464:')
774enddef
775
776def Test_command_not_recognized()
777  var lines =<< trim END
778    d.key = 'asdf'
779  END
780  CheckDefFailure(lines, 'E1146:', 1)
781
782  lines =<< trim END
783    d['key'] = 'asdf'
784  END
785  CheckDefFailure(lines, 'E1146:', 1)
786enddef
787
788def Test_magic_not_used()
789  new
790  for cmd in ['set magic', 'set nomagic']
791    exe cmd
792    setline(1, 'aaa')
793    s/.../bbb/
794    assert_equal('bbb', getline(1))
795  endfor
796
797  set magic
798  setline(1, 'aaa')
799  assert_fails('s/.\M../bbb/', 'E486:')
800  assert_fails('snomagic/.../bbb/', 'E486:')
801  assert_equal('aaa', getline(1))
802
803  bwipe!
804enddef
805
806def Test_gdefault_not_used()
807  new
808  for cmd in ['set gdefault', 'set nogdefault']
809    exe cmd
810    setline(1, 'aaa')
811    s/./b/
812    assert_equal('baa', getline(1))
813  endfor
814
815  set nogdefault
816  bwipe!
817enddef
818
819def g:SomeComplFunc(findstart: number, base: string): any
820  if findstart
821    return 0
822  else
823    return ['aaa', 'bbb']
824  endif
825enddef
826
827def Test_insert_complete()
828  # this was running into an error with the matchparen hack
829  new
830  set completefunc=SomeComplFunc
831  feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
832  assert_equal('aaa', getline(1))
833
834  set completefunc=
835  bwipe!
836enddef
837
838" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
839