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