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_vim9cmd()
9  var lines =<< trim END
10    vim9cmd var x = 123
11    let s:y = 'yes'
12    vim9c assert_equal(123, x)
13    vim9cm assert_equal('yes', y)
14  END
15  CheckScriptSuccess(lines)
16
17  assert_fails('vim9cmd', 'E1164:')
18  assert_fails('legacy', 'E1234:')
19  assert_fails('vim9cmd echo "con" . "cat"', 'E15:')
20
21  lines =<< trim END
22      let str = 'con'
23      vim9cmd str .= 'cat'
24  END
25  CheckScriptFailure(lines, 'E492:')
26
27  lines =<< trim END
28      vim9script
29      legacy echo "con" . "cat"
30      legacy let str = 'con'
31      legacy let str .= 'cat'
32  END
33  CheckScriptSuccess(lines)
34
35  lines =<< trim END
36      vim9script
37      def Foo()
38        g:found_bar = "bar"
39      enddef
40      nmap ,; :vim9cmd <SID>Foo()<CR>
41  END
42  CheckScriptSuccess(lines)
43
44  feedkeys(',;', 'xt')
45  assert_equal("bar", g:found_bar)
46  nunmap ,;
47  unlet g:found_bar
48
49  lines =<< trim END
50      vim9script
51      legacy echo 1'000
52  END
53  CheckScriptFailure(lines, 'E115:')
54
55  if has('float')
56    lines =<< trim END
57        vim9script
58        echo .10
59    END
60    CheckScriptSuccess(lines)
61    lines =<< trim END
62        vim9cmd echo .10
63    END
64    CheckScriptSuccess(lines)
65    lines =<< trim END
66        vim9script
67        legacy echo .10
68    END
69    CheckScriptFailure(lines, 'E15:')
70  endif
71
72  echo v:version
73  assert_fails('vim9cmd echo version', 'E121:')
74  lines =<< trim END
75      vim9script
76      echo version
77  END
78  CheckScriptFailure(lines, 'E121:')
79  lines =<< trim END
80      vim9script
81      legacy echo version
82  END
83  CheckScriptSuccess(lines)
84enddef
85
86def Test_edit_wildcards()
87  var filename = 'Xtest'
88  edit `=filename`
89  assert_equal('Xtest', bufname())
90
91  var filenr = 123
92  edit Xtest`=filenr`
93  assert_equal('Xtest123', bufname())
94
95  filenr = 77
96  edit `=filename``=filenr`
97  assert_equal('Xtest77', bufname())
98
99  edit X`=filename`xx`=filenr`yy
100  assert_equal('XXtestxx77yy', bufname())
101
102  CheckDefFailure(['edit `=xxx`'], 'E1001:')
103  CheckDefFailure(['edit `="foo"'], 'E1083:')
104
105  var files = ['file 1', 'file%2', 'file# 3']
106  args `=files`
107  assert_equal(files, argv())
108enddef
109
110def Test_expand_alternate_file()
111  var lines =<< trim END
112    edit Xfileone
113    var bone = bufnr()
114    edit Xfiletwo
115    var btwo = bufnr()
116    edit Xfilethree
117    var bthree = bufnr()
118
119    edit #
120    assert_equal(bthree, bufnr())
121    edit %%
122    assert_equal(btwo, bufnr())
123    edit %% # comment
124    assert_equal(bthree, bufnr())
125    edit %%yy
126    assert_equal('Xfiletwoyy', bufname())
127
128    exe "edit %%" .. bone
129    assert_equal(bone, bufnr())
130    exe "edit %%" .. btwo .. "xx"
131    assert_equal('Xfiletwoxx', bufname())
132
133    next Xfileone Xfiletwo Xfilethree
134    assert_equal('Xfileone', argv(0))
135    assert_equal('Xfiletwo', argv(1))
136    assert_equal('Xfilethree', argv(2))
137    next %%%zz
138    assert_equal('Xfileone', argv(0))
139    assert_equal('Xfiletwo', argv(1))
140    assert_equal('Xfilethreezz', argv(2))
141
142    v:oldfiles = ['Xonefile', 'Xtwofile']
143    edit %%<1
144    assert_equal('Xonefile', bufname())
145    edit %%<2
146    assert_equal('Xtwofile', bufname())
147    assert_fails('edit %%<3', 'E684:')
148
149    edit Xfileone.vim
150    edit Xfiletwo
151    edit %%:r
152    assert_equal('Xfileone', bufname())
153
154    assert_false(bufexists('altfoo'))
155    edit altfoo
156    edit bar
157    assert_true(bufexists('altfoo'))
158    assert_true(buflisted('altfoo'))
159    bdel %%
160    assert_true(bufexists('altfoo'))
161    assert_false(buflisted('altfoo'))
162    bwipe! altfoo
163    bwipe! bar
164  END
165  CheckDefAndScriptSuccess(lines)
166enddef
167
168def Test_global_backtick_expansion()
169  new
170  setline(1, 'xx')
171  var name = 'foobar'
172  g/^xx/s/.*/`=name`
173  assert_equal('foobar', getline(1))
174  bwipe!
175enddef
176
177def Test_folddo_backtick_expansion()
178  new
179  var name = 'xxx'
180  folddoopen edit `=name`
181  assert_equal('xxx', bufname())
182  bwipe!
183
184  new
185  setline(1, ['one', 'two'])
186  set nomodified
187  :1,2fold
188  foldclose
189  folddoclose edit `=name`
190  assert_equal('xxx', bufname())
191  bwipe!
192enddef
193
194def Test_hardcopy_wildcards()
195  CheckUnix
196  CheckFeature postscript
197
198  var outfile = 'print'
199  hardcopy > X`=outfile`.ps
200  assert_true(filereadable('Xprint.ps'))
201
202  delete('Xprint.ps')
203enddef
204
205def Test_syn_include_wildcards()
206  writefile(['syn keyword Found found'], 'Xthemine.vim')
207  var save_rtp = &rtp
208  &rtp = '.'
209
210  var fname = 'mine'
211  syn include @Group Xthe`=fname`.vim
212  assert_match('Found.* contained found', execute('syn list Found'))
213
214  &rtp = save_rtp
215  delete('Xthemine.vim')
216enddef
217
218def Test_echo_linebreak()
219  var lines =<< trim END
220      vim9script
221      redir @a
222      echo 'one'
223            .. 'two'
224      redir END
225      assert_equal("\nonetwo", @a)
226  END
227  CheckScriptSuccess(lines)
228
229  lines =<< trim END
230      vim9script
231      redir @a
232      echo 11 +
233            77
234            - 22
235      redir END
236      assert_equal("\n66", @a)
237  END
238  CheckScriptSuccess(lines)
239enddef
240
241def Test_condition_types()
242  var lines =<< trim END
243      if 'text'
244      endif
245  END
246  CheckDefAndScriptFailure(lines, 'E1135:', 1)
247
248  lines =<< trim END
249      if [1]
250      endif
251  END
252  CheckDefFailure(lines, 'E1012:', 1)
253  CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
254
255  lines =<< trim END
256      g:cond = 'text'
257      if g:cond
258      endif
259  END
260  CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
261
262  lines =<< trim END
263      g:cond = 0
264      if g:cond
265      elseif 'text'
266      endif
267  END
268  CheckDefFailure(lines, 'E1012:', 3)
269  CheckScriptFailure(['vim9script'] + lines, 'E1135:', 4)
270
271  lines =<< trim END
272      if g:cond
273      elseif [1]
274      endif
275  END
276  CheckDefFailure(lines, 'E1012:', 2)
277  CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
278
279  lines =<< trim END
280      g:cond = 'text'
281      if 0
282      elseif g:cond
283      endif
284  END
285  CheckDefExecAndScriptFailure(lines, 'E1135:', 3)
286
287  lines =<< trim END
288      while 'text'
289      endwhile
290  END
291  CheckDefFailure(lines, 'E1012:', 1)
292  CheckScriptFailure(['vim9script'] + lines, 'E1135:', 2)
293
294  lines =<< trim END
295      while [1]
296      endwhile
297  END
298  CheckDefFailure(lines, 'E1012:', 1)
299  CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
300
301  lines =<< trim END
302      g:cond = 'text'
303      while g:cond
304      endwhile
305  END
306  CheckDefExecAndScriptFailure(lines, 'E1135:', 2)
307enddef
308
309def Test_if_linebreak()
310  var lines =<< trim END
311      vim9script
312      if 1 &&
313            true
314            || 1
315        g:res = 42
316      endif
317      assert_equal(42, g:res)
318  END
319  CheckScriptSuccess(lines)
320  unlet g:res
321
322  lines =<< trim END
323      vim9script
324      if 1 &&
325            0
326        g:res = 0
327      elseif 0 ||
328              0
329              || 1
330        g:res = 12
331      endif
332      assert_equal(12, g:res)
333  END
334  CheckScriptSuccess(lines)
335  unlet g:res
336enddef
337
338def Test_while_linebreak()
339  var lines =<< trim END
340      vim9script
341      var nr = 0
342      while nr <
343              10 + 3
344            nr = nr
345                  + 4
346      endwhile
347      assert_equal(16, nr)
348  END
349  CheckScriptSuccess(lines)
350
351  lines =<< trim END
352      vim9script
353      var nr = 0
354      while nr
355            <
356              10
357              +
358              3
359            nr = nr
360                  +
361                  4
362      endwhile
363      assert_equal(16, nr)
364  END
365  CheckScriptSuccess(lines)
366enddef
367
368def Test_for_linebreak()
369  var lines =<< trim END
370      vim9script
371      var nr = 0
372      for x
373            in
374              [1, 2, 3, 4]
375          nr = nr + x
376      endfor
377      assert_equal(10, nr)
378  END
379  CheckScriptSuccess(lines)
380
381  lines =<< trim END
382      vim9script
383      var nr = 0
384      for x
385            in
386              [1, 2,
387                  3, 4
388                  ]
389          nr = nr
390                 +
391                  x
392      endfor
393      assert_equal(10, nr)
394  END
395  CheckScriptSuccess(lines)
396enddef
397
398def MethodAfterLinebreak(arg: string)
399  arg
400    ->setline(1)
401enddef
402
403def Test_method_call_linebreak()
404  var lines =<< trim END
405      vim9script
406      var res = []
407      func RetArg(
408            arg
409            )
410            let s:res = a:arg
411      endfunc
412      [1,
413          2,
414          3]->RetArg()
415      assert_equal([1, 2, 3], res)
416  END
417  CheckScriptSuccess(lines)
418
419  lines =<< trim END
420      new
421      var name = [1, 2]
422      name
423          ->copy()
424          ->setline(1)
425      assert_equal(['1', '2'], getline(1, 2))
426      bwipe!
427  END
428  CheckDefAndScriptSuccess(lines)
429
430  lines =<< trim END
431      new
432      def Foo(): string
433        return 'the text'
434      enddef
435      def Bar(F: func): string
436        return F()
437      enddef
438      def Test()
439        Foo  ->Bar()
440             ->setline(1)
441      enddef
442      Test()
443      assert_equal('the text', getline(1))
444      bwipe!
445  END
446  CheckDefAndScriptSuccess(lines)
447
448  lines =<< trim END
449      new
450      g:shortlist
451          ->copy()
452          ->setline(1)
453      assert_equal(['1', '2'], getline(1, 2))
454      bwipe!
455  END
456  g:shortlist = [1, 2]
457  CheckDefAndScriptSuccess(lines)
458  unlet g:shortlist
459
460  new
461  MethodAfterLinebreak('foobar')
462  assert_equal('foobar', getline(1))
463  bwipe!
464
465  lines =<< trim END
466      vim9script
467      def Foo(): string
468          return '# some text'
469      enddef
470
471      def Bar(F: func): string
472          return F()
473      enddef
474
475      Foo->Bar()
476         ->setline(1)
477  END
478  CheckScriptSuccess(lines)
479  assert_equal('# some text', getline(1))
480  bwipe!
481enddef
482
483def Test_method_call_whitespace()
484  var lines =<< trim END
485    new
486    var yank = 'text'
487    yank->setline(1)
488    yank  ->setline(2)
489    yank->  setline(3)
490    yank  ->  setline(4)
491    assert_equal(['text', 'text', 'text', 'text'], getline(1, 4))
492    bwipe!
493  END
494  CheckDefAndScriptSuccess(lines)
495enddef
496
497def Test_method_and_user_command()
498  var lines =<< trim END
499      vim9script
500      def Cmd()
501        g:didFunc = 1
502      enddef
503      command Cmd g:didCmd = 1
504      Cmd
505      assert_equal(1, g:didCmd)
506      Cmd()
507      assert_equal(1, g:didFunc)
508      unlet g:didFunc
509      unlet g:didCmd
510
511      def InDefFunc()
512        Cmd
513        assert_equal(1, g:didCmd)
514        Cmd()
515        assert_equal(1, g:didFunc)
516        unlet g:didFunc
517        unlet g:didCmd
518      enddef
519      InDefFunc()
520  END
521  CheckScriptSuccess(lines)
522enddef
523
524def Test_option_use_linebreak()
525  var lines =<< trim END
526      new
527      &matchpairs = '(:)'
528      &matchpairs->setline(1)
529      &matchpairs = '[:]'
530      &matchpairs   ->setline(2)
531      &matchpairs = '{:}'
532      &matchpairs
533          ->setline(3)
534      assert_equal(['(:)', '[:]', '{:}'], getline(1, '$'))
535      bwipe!
536  END
537  CheckDefAndScriptSuccess(lines)
538enddef
539
540def Test_register_use_linebreak()
541  var lines =<< trim END
542      new
543      @a = 'one'
544      @a->setline(1)
545      @b = 'two'
546      @b   ->setline(2)
547      @c = 'three'
548      @c
549          ->setline(3)
550      assert_equal(['one', 'two', 'three'], getline(1, '$'))
551      bwipe!
552  END
553  CheckDefAndScriptSuccess(lines)
554enddef
555
556def Test_skipped_expr_linebreak()
557  if 0
558    var x = []
559               ->map(() => 0)
560  endif
561enddef
562
563def Test_dict_member()
564   var test: dict<list<number>> = {data: [3, 1, 2]}
565   test.data->sort()
566   assert_equal({data: [1, 2, 3]}, test)
567   test.data
568      ->reverse()
569   assert_equal({data: [3, 2, 1]}, test)
570
571  var lines =<< trim END
572      vim9script
573      var test: dict<list<number>> = {data: [3, 1, 2]}
574      test.data->sort()
575      assert_equal({data: [1, 2, 3]}, test)
576  END
577  CheckScriptSuccess(lines)
578enddef
579
580def Test_bar_after_command()
581  def RedrawAndEcho()
582    var x = 'did redraw'
583    redraw | echo x
584  enddef
585  RedrawAndEcho()
586  assert_match('did redraw', Screenline(&lines))
587
588  def CallAndEcho()
589    var x = 'did redraw'
590    reg_executing() | echo x
591  enddef
592  CallAndEcho()
593  assert_match('did redraw', Screenline(&lines))
594
595  if has('unix')
596    # bar in filter write command does not start new command
597    def WriteToShell()
598      new
599      setline(1, 'some text')
600      w !cat | cat > Xoutfile
601      bwipe!
602    enddef
603    WriteToShell()
604    assert_equal(['some text'], readfile('Xoutfile'))
605    delete('Xoutfile')
606
607    # bar in filter read command does not start new command
608    def ReadFromShell()
609      new
610      r! echo hello there | cat > Xoutfile
611      r !echo again | cat >> Xoutfile
612      bwipe!
613    enddef
614    ReadFromShell()
615    assert_equal(['hello there', 'again'], readfile('Xoutfile'))
616    delete('Xoutfile')
617  endif
618enddef
619
620def Test_filter_is_not_modifier()
621  var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
622  filter(tags, ( _, v) => has_key(v, 'x') ? 1 : 0 )
623  assert_equal([{x: 3, y: 4}], tags)
624enddef
625
626def Test_command_modifier_filter()
627  var lines =<< trim END
628    final expected = "\nType Name Content\n  c  \"c   piyo"
629    @a = 'hoge'
630    @b = 'fuga'
631    @c = 'piyo'
632
633    assert_equal(execute('filter /piyo/ registers abc'), expected)
634  END
635  CheckDefAndScriptSuccess(lines)
636
637  # also do this compiled
638  lines =<< trim END
639      @a = 'very specific z3d37dh234 string'
640      filter z3d37dh234 registers
641      assert_match('very specific z3d37dh234 string', Screenline(&lines))
642  END
643  CheckDefAndScriptSuccess(lines)
644enddef
645
646def Test_win_command_modifiers()
647  assert_equal(1, winnr('$'))
648
649  set splitright
650  vsplit
651  assert_equal(2, winnr())
652  close
653  aboveleft vsplit
654  assert_equal(1, winnr())
655  close
656  set splitright&
657
658  vsplit
659  assert_equal(1, winnr())
660  close
661  belowright vsplit
662  assert_equal(2, winnr())
663  close
664  rightbelow vsplit
665  assert_equal(2, winnr())
666  close
667
668  if has('browse')
669    browse set
670    assert_equal('option-window', expand('%'))
671    close
672  endif
673
674  vsplit
675  botright split
676  assert_equal(3, winnr())
677  assert_equal(&columns, winwidth(0))
678  close
679  close
680
681  vsplit
682  topleft split
683  assert_equal(1, winnr())
684  assert_equal(&columns, winwidth(0))
685  close
686  close
687
688  gettabinfo()->len()->assert_equal(1)
689  tab split
690  gettabinfo()->len()->assert_equal(2)
691  tabclose
692
693  vertical new
694  assert_inrange(&columns / 2 - 2, &columns / 2 + 1, winwidth(0))
695  close
696enddef
697
698func Test_command_modifier_confirm()
699  CheckNotGui
700  CheckRunVimInTerminal
701
702  " Test for saving all the modified buffers
703  let lines =<< trim END
704    call setline(1, 'changed')
705    def Getout()
706      confirm write Xfile
707    enddef
708  END
709  call writefile(lines, 'Xconfirmscript')
710  call writefile(['empty'], 'Xfile')
711  let buf = RunVimInTerminal('-S Xconfirmscript', {'rows': 8})
712  call term_sendkeys(buf, ":call Getout()\n")
713  call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
714  call term_sendkeys(buf, "y")
715  call WaitForAssert({-> assert_match('(Y)es, \[N\]o: ', term_getline(buf, 8))}, 1000)
716  call term_sendkeys(buf, "\<CR>")
717  call TermWait(buf)
718  call StopVimInTerminal(buf)
719
720  call assert_equal(['changed'], readfile('Xfile'))
721  call delete('Xfile')
722  call delete('.Xfile.swp')  " in case Vim was killed
723  call delete('Xconfirmscript')
724endfunc
725
726def Test_command_modifiers_keep()
727  if has('unix')
728    def DoTest(addRflag: bool, keepMarks: bool, hasMarks: bool)
729      new
730      setline(1, ['one', 'two', 'three'])
731      normal 1Gma
732      normal 2Gmb
733      normal 3Gmc
734      if addRflag
735        set cpo+=R
736      else
737        set cpo-=R
738      endif
739      if keepMarks
740        keepmarks :%!cat
741      else
742        :%!cat
743      endif
744      if hasMarks
745        assert_equal(1, line("'a"))
746        assert_equal(2, line("'b"))
747        assert_equal(3, line("'c"))
748      else
749        assert_equal(0, line("'a"))
750        assert_equal(0, line("'b"))
751        assert_equal(0, line("'c"))
752      endif
753      quit!
754    enddef
755    DoTest(false, false, true)
756    DoTest(true, false, false)
757    DoTest(false, true, true)
758    DoTest(true, true, true)
759    set cpo&vim
760
761    new
762    setline(1, ['one', 'two', 'three', 'four'])
763    assert_equal(4, line("$"))
764    normal 1Gma
765    normal 2Gmb
766    normal 3Gmc
767    lockmarks :1,2!wc
768    # line is deleted, marks don't move
769    assert_equal(3, line("$"))
770    assert_equal('four', getline(3))
771    assert_equal(1, line("'a"))
772    assert_equal(2, line("'b"))
773    assert_equal(3, line("'c"))
774    quit!
775  endif
776
777  edit Xone
778  edit Xtwo
779  assert_equal('Xone', expand('#'))
780  keepalt edit Xthree
781  assert_equal('Xone', expand('#'))
782
783  normal /a*b*
784  assert_equal('a*b*', histget("search"))
785  keeppatterns normal /c*d*
786  assert_equal('a*b*', histget("search"))
787
788  new
789  setline(1, range(10))
790  :10
791  normal gg
792  assert_equal(10, getpos("''")[1])
793  keepjumps normal 5G
794  assert_equal(10, getpos("''")[1])
795  quit!
796enddef
797
798def Test_bar_line_continuation()
799  var lines =<< trim END
800      au BufNewFile Xfile g:readFile = 1
801          | g:readExtra = 2
802      g:readFile = 0
803      g:readExtra = 0
804      edit Xfile
805      assert_equal(1, g:readFile)
806      assert_equal(2, g:readExtra)
807      bwipe!
808      au! BufNewFile
809
810      au BufNewFile Xfile g:readFile = 1
811          | g:readExtra = 2
812          | g:readMore = 3
813      g:readFile = 0
814      g:readExtra = 0
815      g:readMore = 0
816      edit Xfile
817      assert_equal(1, g:readFile)
818      assert_equal(2, g:readExtra)
819      assert_equal(3, g:readMore)
820      bwipe!
821      au! BufNewFile
822      unlet g:readFile
823      unlet g:readExtra
824      unlet g:readMore
825  END
826  CheckDefAndScriptSuccess(lines)
827enddef
828
829def Test_command_modifier_other()
830  new Xsomefile
831  setline(1, 'changed')
832  var buf = bufnr()
833  hide edit Xotherfile
834  var info = getbufinfo(buf)
835  assert_equal(1, info[0].hidden)
836  assert_equal(1, info[0].changed)
837  edit Xsomefile
838  bwipe!
839
840  au BufNewFile Xfile g:readFile = 1
841  g:readFile = 0
842  edit Xfile
843  assert_equal(1, g:readFile)
844  bwipe!
845  g:readFile = 0
846  noautocmd edit Xfile
847  assert_equal(0, g:readFile)
848  au! BufNewFile
849  unlet g:readFile
850
851  noswapfile edit XnoSwap
852  assert_equal(false, &l:swapfile)
853  bwipe!
854
855  var caught = false
856  try
857    sandbox !ls
858  catch /E48:/
859    caught = true
860  endtry
861  assert_true(caught)
862
863  :8verbose g:verbose_now = &verbose
864  assert_equal(8, g:verbose_now)
865  unlet g:verbose_now
866enddef
867
868def EchoHere()
869  echomsg 'here'
870enddef
871def EchoThere()
872  unsilent echomsg 'there'
873enddef
874
875def Test_modifier_silent_unsilent()
876  echomsg 'last one'
877  silent echomsg "text"
878  assert_equal("\nlast one", execute(':1messages'))
879
880  silent! echoerr "error"
881
882  echomsg 'last one'
883  silent EchoHere()
884  assert_equal("\nlast one", execute(':1messages'))
885
886  silent EchoThere()
887  assert_equal("\nthere", execute(':1messages'))
888
889  try
890    silent eval [][0]
891  catch
892    echomsg "caught"
893  endtry
894  assert_equal("\ncaught", execute(':1messages'))
895
896  var lines =<< trim END
897      vim9script
898      set history=11
899      silent! while 0
900        set history=22
901      silent! endwhile
902      assert_equal(11, &history)
903      set history&
904  END
905  CheckScriptSuccess(lines)
906enddef
907
908def Test_range_after_command_modifier()
909  CheckScriptFailure(['vim9script', 'silent keepjump 1d _'], 'E1050: Colon required before a range: 1d _', 2)
910  new
911  setline(1, 'xxx')
912  CheckScriptSuccess(['vim9script', 'silent keepjump :1d _'])
913  assert_equal('', getline(1))
914  bwipe!
915enddef
916
917def Test_silent_pattern()
918  new
919  silent! :/pat/put _
920  bwipe!
921enddef
922
923def Test_useless_command_modifier()
924  g:maybe = true
925  var lines =<< trim END
926      if g:maybe
927      silent endif
928  END
929  CheckDefAndScriptFailure(lines, 'E1176:', 2)
930
931  lines =<< trim END
932      for i in [0]
933      silent endfor
934  END
935  CheckDefFailure(lines, 'E1176:', 2)
936  CheckScriptSuccess(['vim9script'] + lines)
937
938  lines =<< trim END
939      while g:maybe
940      silent endwhile
941  END
942  CheckDefFailure(lines, 'E1176:', 2)
943  g:maybe = false
944  CheckScriptSuccess(['vim9script'] + lines)
945
946  lines =<< trim END
947      silent try
948      finally
949      endtry
950  END
951  CheckDefAndScriptFailure(lines, 'E1176:', 1)
952
953  lines =<< trim END
954      try
955      silent catch
956      endtry
957  END
958  CheckDefAndScriptFailure(lines, 'E1176:', 2)
959
960  lines =<< trim END
961      try
962      silent finally
963      endtry
964  END
965  CheckDefAndScriptFailure(lines, 'E1176:', 2)
966
967  lines =<< trim END
968      try
969      finally
970      silent endtry
971  END
972  CheckDefAndScriptFailure(lines, 'E1176:', 3)
973enddef
974
975def Test_eval_command()
976  var from = 3
977  var to = 5
978  g:val = 111
979  def Increment(nrs: list<number>)
980    for nr in nrs
981      g:val += nr
982    endfor
983  enddef
984  eval range(from, to)
985        ->Increment()
986  assert_equal(111 + 3 + 4 + 5, g:val)
987  unlet g:val
988
989  var lines =<< trim END
990    vim9script
991    g:caught = 'no'
992    try
993      eval 123 || 0
994    catch
995      g:caught = 'yes'
996    endtry
997    assert_equal('yes', g:caught)
998    unlet g:caught
999  END
1000  CheckScriptSuccess(lines)
1001enddef
1002
1003def Test_map_command()
1004  var lines =<< trim END
1005      nnoremap <F3> :echo 'hit F3 #'<CR>
1006      assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
1007  END
1008  CheckDefSuccess(lines)
1009  CheckScriptSuccess(['vim9script'] + lines)
1010enddef
1011
1012def Test_normal_command()
1013  new
1014  setline(1, 'doesnotexist')
1015  var caught = 0
1016  try
1017    exe "norm! \<C-]>"
1018  catch /E433/
1019    caught = 2
1020  endtry
1021  assert_equal(2, caught)
1022
1023  try
1024    exe "norm! 3\<C-]>"
1025  catch /E433/
1026    caught = 3
1027  endtry
1028  assert_equal(3, caught)
1029  bwipe!
1030enddef
1031
1032def Test_put_command()
1033  new
1034  @p = 'ppp'
1035  put p
1036  assert_equal('ppp', getline(2))
1037
1038  put ='below'
1039  assert_equal('below', getline(3))
1040  put! ='above'
1041  assert_equal('above', getline(3))
1042  assert_equal('below', getline(4))
1043
1044  :2put =['a', 'b', 'c']
1045  assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
1046
1047  # compute range at runtime
1048  setline(1, range(1, 8))
1049  @a = 'aaa'
1050  :$-2put a
1051  assert_equal('aaa', getline(7))
1052
1053  setline(1, range(1, 8))
1054  :2
1055  :+2put! a
1056  assert_equal('aaa', getline(4))
1057
1058  []->mapnew(() => 0)
1059  :$put ='end'
1060  assert_equal('end', getline('$'))
1061
1062  bwipe!
1063
1064  CheckDefFailure(['put =xxx'], 'E1001:')
1065enddef
1066
1067def Test_put_with_linebreak()
1068  new
1069  var lines =<< trim END
1070    vim9script
1071    pu =split('abc', '\zs')
1072            ->join()
1073  END
1074  CheckScriptSuccess(lines)
1075  getline(2)->assert_equal('a b c')
1076  bwipe!
1077enddef
1078
1079def Test_command_star_range()
1080  new
1081  setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
1082  setpos("'<", [0, 1, 0, 0])
1083  setpos("'>", [0, 3, 0, 0])
1084  :*s/\(foo\|bar\)/baz/g
1085  getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
1086
1087  bwipe!
1088enddef
1089
1090def Test_f_args()
1091  var lines =<< trim END
1092    vim9script
1093
1094    func SaveCmdArgs(...)
1095      let g:args = a:000
1096    endfunc
1097
1098    command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
1099
1100    TestFArgs
1101    assert_equal([], g:args)
1102
1103    TestFArgs one two three
1104    assert_equal(['one', 'two', 'three'], g:args)
1105  END
1106  CheckScriptSuccess(lines)
1107enddef
1108
1109def Test_user_command_comment()
1110  command -nargs=1 Comd echom <q-args>
1111
1112  var lines =<< trim END
1113      vim9script
1114      Comd # comment
1115  END
1116  CheckScriptSuccess(lines)
1117
1118  lines =<< trim END
1119      vim9script
1120      Comd# comment
1121  END
1122  CheckScriptFailure(lines, 'E1144:')
1123  delcommand Comd
1124
1125  lines =<< trim END
1126      vim9script
1127      command Foo echo 'Foo'
1128      Foo3Bar
1129  END
1130  CheckScriptFailure(lines, 'E1144: Command "Foo" is not followed by white space: Foo3Bar')
1131
1132  delcommand Foo
1133enddef
1134
1135def Test_star_command()
1136  var lines =<< trim END
1137    vim9script
1138    @s = 'g:success = 8'
1139    set cpo+=*
1140    exe '*s'
1141    assert_equal(8, g:success)
1142    unlet g:success
1143    set cpo-=*
1144    assert_fails("exe '*s'", 'E1050:')
1145  END
1146  CheckScriptSuccess(lines)
1147enddef
1148
1149def Test_cmd_argument_without_colon()
1150  new Xfile
1151  setline(1, ['a', 'b', 'c', 'd'])
1152  write
1153  edit +3 %
1154  assert_equal(3, getcurpos()[1])
1155  edit +/a %
1156  assert_equal(1, getcurpos()[1])
1157  bwipe
1158  delete('Xfile')
1159enddef
1160
1161def Test_ambiguous_user_cmd()
1162  command Cmd1 eval 0
1163  command Cmd2 eval 0
1164  var lines =<< trim END
1165      Cmd
1166  END
1167  CheckDefAndScriptFailure(lines, 'E464:', 1)
1168  delcommand Cmd1
1169  delcommand Cmd2
1170enddef
1171
1172def Test_command_not_recognized()
1173  var lines =<< trim END
1174    d.key = 'asdf'
1175  END
1176  CheckDefFailure(lines, 'E1146:', 1)
1177
1178  lines =<< trim END
1179    d['key'] = 'asdf'
1180  END
1181  CheckDefFailure(lines, 'E1146:', 1)
1182enddef
1183
1184def Test_magic_not_used()
1185  new
1186  for cmd in ['set magic', 'set nomagic']
1187    exe cmd
1188    setline(1, 'aaa')
1189    s/.../bbb/
1190    assert_equal('bbb', getline(1))
1191  endfor
1192
1193  set magic
1194  setline(1, 'aaa')
1195  assert_fails('s/.\M../bbb/', 'E486:')
1196  assert_fails('snomagic/.../bbb/', 'E486:')
1197  assert_equal('aaa', getline(1))
1198
1199  bwipe!
1200enddef
1201
1202def Test_gdefault_not_used()
1203  new
1204  for cmd in ['set gdefault', 'set nogdefault']
1205    exe cmd
1206    setline(1, 'aaa')
1207    s/./b/
1208    assert_equal('baa', getline(1))
1209  endfor
1210
1211  set nogdefault
1212  bwipe!
1213enddef
1214
1215def g:SomeComplFunc(findstart: number, base: string): any
1216  if findstart
1217    return 0
1218  else
1219    return ['aaa', 'bbb']
1220  endif
1221enddef
1222
1223def Test_insert_complete()
1224  # this was running into an error with the matchparen hack
1225  new
1226  set completefunc=SomeComplFunc
1227  feedkeys("i\<c-x>\<c-u>\<Esc>", 'ntx')
1228  assert_equal('aaa', getline(1))
1229
1230  set completefunc=
1231  bwipe!
1232enddef
1233
1234def Test_wincmd()
1235  split
1236  var id1 = win_getid()
1237  if true
1238    try | wincmd w | catch | endtry
1239  endif
1240  assert_notequal(id1, win_getid())
1241  close
1242
1243  split
1244  var id = win_getid()
1245  split
1246  :2wincmd o
1247  assert_equal(id, win_getid())
1248  only
1249
1250  split
1251  split
1252  assert_equal(3, winnr('$'))
1253  :2wincmd c
1254  assert_equal(2, winnr('$'))
1255  only
1256
1257  split
1258  split
1259  assert_equal(3, winnr('$'))
1260  :2wincmd q
1261  assert_equal(2, winnr('$'))
1262  only
1263enddef
1264
1265def Test_windo_missing_endif()
1266  var lines =<< trim END
1267      windo if 1
1268  END
1269  CheckDefExecFailure(lines, 'E171:', 1)
1270enddef
1271
1272let s:theList = [1, 2, 3]
1273
1274def Test_lockvar()
1275  s:theList[1] = 22
1276  assert_equal([1, 22, 3], s:theList)
1277  lockvar s:theList
1278  assert_fails('theList[1] = 77', 'E741:')
1279  unlockvar s:theList
1280  s:theList[1] = 44
1281  assert_equal([1, 44, 3], s:theList)
1282
1283  var d = {a: 1, b: 2}
1284  d.a = 3
1285  d.b = 4
1286  assert_equal({a: 3, b: 4}, d)
1287  lockvar d.a
1288  d.b = 5
1289  var ex = ''
1290  try
1291    d.a = 6
1292  catch
1293    ex = v:exception
1294  endtry
1295  assert_match('E1121:', ex)
1296  unlockvar d.a
1297  d.a = 7
1298  assert_equal({a: 7, b: 5}, d)
1299
1300  var lines =<< trim END
1301      vim9script
1302      var theList = [1, 2, 3]
1303      def SetList()
1304        theList[1] = 22
1305        assert_equal([1, 22, 3], theList)
1306        lockvar theList
1307        theList[1] = 77
1308      enddef
1309      SetList()
1310  END
1311  CheckScriptFailure(lines, 'E1119', 4)
1312
1313  lines =<< trim END
1314      var theList = [1, 2, 3]
1315      lockvar theList
1316  END
1317  CheckDefFailure(lines, 'E1178', 2)
1318
1319  lines =<< trim END
1320      var theList = [1, 2, 3]
1321      unlockvar theList
1322  END
1323  CheckDefFailure(lines, 'E1178', 2)
1324enddef
1325
1326def Test_substitute_expr()
1327  var to = 'repl'
1328  new
1329  setline(1, 'one from two')
1330  s/from/\=to
1331  assert_equal('one repl two', getline(1))
1332
1333  setline(1, 'one from two')
1334  s/from/\=to .. '_x'
1335  assert_equal('one repl_x two', getline(1))
1336
1337  setline(1, 'one from two from three')
1338  var also = 'also'
1339  s/from/\=to .. '_' .. also/g#e
1340  assert_equal('one repl_also two repl_also three', getline(1))
1341
1342  setline(1, 'abc abc abc')
1343  for choice in [true, false]
1344    :1s/abc/\=choice ? 'yes' : 'no'/
1345  endfor
1346  assert_equal('yes no abc', getline(1))
1347
1348  bwipe!
1349
1350  CheckDefFailure(['s/from/\="x")/'], 'E488:')
1351  CheckDefFailure(['s/from/\="x"/9'], 'E488:')
1352
1353  # When calling a function the right instruction list needs to be restored.
1354  g:cond = true
1355  var lines =<< trim END
1356      vim9script
1357      def Foo()
1358          Bar([])
1359      enddef
1360      def Bar(l: list<number>)
1361        if g:cond
1362          s/^/\=Rep()/
1363          for n in l[:]
1364          endfor
1365        endif
1366      enddef
1367      def Rep(): string
1368          return 'rep'
1369      enddef
1370      new
1371      Foo()
1372      assert_equal('rep', getline(1))
1373      bwipe!
1374  END
1375  CheckScriptSuccess(lines)
1376  unlet g:cond
1377
1378  # List results in multiple lines
1379  new
1380  setline(1, 'some text here')
1381  s/text/\=['aaa', 'bbb', 'ccc']/
1382  assert_equal(['some aaa', 'bbb', 'ccc', ' here'], getline(1, '$'))
1383  bwipe!
1384enddef
1385
1386def Test_redir_to_var()
1387  var result: string
1388  redir => result
1389    echo 'something'
1390  redir END
1391  assert_equal("\nsomething", result)
1392
1393  redir =>> result
1394    echo 'more'
1395  redir END
1396  assert_equal("\nsomething\nmore", result)
1397
1398  var d: dict<string>
1399  redir => d.redir
1400    echo 'dict'
1401  redir END
1402  assert_equal({redir: "\ndict"}, d)
1403
1404  var l = ['a', 'b', 'c']
1405  redir => l[1]
1406    echo 'list'
1407  redir END
1408  assert_equal(['a', "\nlist", 'c'], l)
1409
1410  var dl = {l: ['x']}
1411  redir => dl.l[0]
1412    echo 'dict-list'
1413  redir END
1414  assert_equal({l: ["\ndict-list"]}, dl)
1415
1416  redir =>> d.redir
1417    echo 'more'
1418  redir END
1419  assert_equal({redir: "\ndict\nmore"}, d)
1420
1421  var lines =<< trim END
1422    redir => notexist
1423  END
1424  CheckDefFailure(lines, 'E1089:')
1425
1426  lines =<< trim END
1427    var ls = 'asdf'
1428    redir => ls[1]
1429    redir END
1430  END
1431  CheckDefFailure(lines, 'E1141:')
1432enddef
1433
1434def Test_echo_void()
1435  var lines =<< trim END
1436      vim9script
1437      def NoReturn()
1438        echo 'nothing'
1439      enddef
1440      echo NoReturn()
1441  END
1442  CheckScriptFailure(lines, 'E1186:', 5)
1443
1444  lines =<< trim END
1445      vim9script
1446      def NoReturn()
1447        echo 'nothing'
1448      enddef
1449      def Try()
1450        echo NoReturn()
1451      enddef
1452      defcompile
1453  END
1454  CheckScriptFailure(lines, 'E1186:', 1)
1455enddef
1456
1457def Test_cmdwin_block()
1458  augroup justTesting
1459    autocmd BufEnter * {
1460      echomsg 'in block'
1461    }
1462  augroup END
1463  feedkeys('q:', 'xt')
1464  redraw
1465  feedkeys("aclose\<CR>", 'xt')
1466
1467  au! justTesting
1468enddef
1469
1470
1471" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
1472