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