1" Test various aspects of the Vim9 script language.
2
3source check.vim
4source term_util.vim
5source view_util.vim
6source vim9.vim
7source shared.vim
8
9def Test_range_only()
10  new
11  setline(1, ['blah', 'Blah'])
12  :/Blah/
13  assert_equal(2, getcurpos()[1])
14  bwipe!
15
16  # without range commands use current line
17  new
18  setline(1, ['one', 'two', 'three'])
19  :2
20  print
21  assert_equal('two', Screenline(&lines))
22  :3
23  list
24  assert_equal('three$', Screenline(&lines))
25  bwipe!
26
27  # won't generate anything
28  if false
29    :123
30  endif
31enddef
32
33let g:alist = [7]
34let g:astring = 'text'
35let g:anumber = 123
36
37def Test_delfunction()
38  # Check function is defined in script namespace
39  CheckScriptSuccess([
40      'vim9script',
41      'func CheckMe()',
42      '  return 123',
43      'endfunc',
44      'assert_equal(123, s:CheckMe())',
45      ])
46
47  # Check function in script namespace cannot be deleted
48  CheckScriptFailure([
49      'vim9script',
50      'func DeleteMe1()',
51      'endfunc',
52      'delfunction DeleteMe1',
53      ], 'E1084:')
54  CheckScriptFailure([
55      'vim9script',
56      'func DeleteMe2()',
57      'endfunc',
58      'def DoThat()',
59      '  delfunction DeleteMe2',
60      'enddef',
61      'DoThat()',
62      ], 'E1084:')
63  CheckScriptFailure([
64      'vim9script',
65      'def DeleteMe3()',
66      'enddef',
67      'delfunction DeleteMe3',
68      ], 'E1084:')
69  CheckScriptFailure([
70      'vim9script',
71      'def DeleteMe4()',
72      'enddef',
73      'def DoThat()',
74      '  delfunction DeleteMe4',
75      'enddef',
76      'DoThat()',
77      ], 'E1084:')
78
79  # Check that global :def function can be replaced and deleted
80  var lines =<< trim END
81      vim9script
82      def g:Global(): string
83        return "yes"
84      enddef
85      assert_equal("yes", g:Global())
86      def! g:Global(): string
87        return "no"
88      enddef
89      assert_equal("no", g:Global())
90      delfunc g:Global
91      assert_false(exists('*g:Global'))
92  END
93  CheckScriptSuccess(lines)
94
95  # Check that global function can be replaced by a :def function and deleted
96  lines =<< trim END
97      vim9script
98      func g:Global()
99        return "yes"
100      endfunc
101      assert_equal("yes", g:Global())
102      def! g:Global(): string
103        return "no"
104      enddef
105      assert_equal("no", g:Global())
106      delfunc g:Global
107      assert_false(exists('*g:Global'))
108  END
109  CheckScriptSuccess(lines)
110
111  # Check that global :def function can be replaced by a function and deleted
112  lines =<< trim END
113      vim9script
114      def g:Global(): string
115        return "yes"
116      enddef
117      assert_equal("yes", g:Global())
118      func! g:Global()
119        return "no"
120      endfunc
121      assert_equal("no", g:Global())
122      delfunc g:Global
123      assert_false(exists('*g:Global'))
124  END
125  CheckScriptSuccess(lines)
126enddef
127
128def Test_wrong_type()
129  CheckDefFailure(['var name: list<nothing>'], 'E1010:')
130  CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
131  CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
132  CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
133
134  CheckDefFailure(['var name: dict<number'], 'E1009:')
135  CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
136
137  CheckDefFailure(['var name: ally'], 'E1010:')
138  CheckDefFailure(['var name: bram'], 'E1010:')
139  CheckDefFailure(['var name: cathy'], 'E1010:')
140  CheckDefFailure(['var name: dom'], 'E1010:')
141  CheckDefFailure(['var name: freddy'], 'E1010:')
142  CheckDefFailure(['var name: john'], 'E1010:')
143  CheckDefFailure(['var name: larry'], 'E1010:')
144  CheckDefFailure(['var name: ned'], 'E1010:')
145  CheckDefFailure(['var name: pam'], 'E1010:')
146  CheckDefFailure(['var name: sam'], 'E1010:')
147  CheckDefFailure(['var name: vim'], 'E1010:')
148
149  CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
150  CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
151enddef
152
153def Test_script_wrong_type()
154  var lines =<< trim END
155      vim9script
156      var s:dict: dict<string>
157      s:dict['a'] = ['x']
158  END
159  CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
160enddef
161
162def Test_const()
163  CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
164  CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
165  CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
166  CheckDefFailure(['final two'], 'E1125:')
167  CheckDefFailure(['final &option'], 'E996:')
168
169  var lines =<< trim END
170    final list = [1, 2, 3]
171    list[0] = 4
172    list->assert_equal([4, 2, 3])
173    const other = [5, 6, 7]
174    other->assert_equal([5, 6, 7])
175
176    var varlist = [7, 8]
177    const constlist = [1, varlist, 3]
178    varlist[0] = 77
179    # TODO: does not work yet
180    # constlist[1][1] = 88
181    var cl = constlist[1]
182    cl[1] = 88
183    constlist->assert_equal([1, [77, 88], 3])
184
185    var vardict = {five: 5, six: 6}
186    const constdict = {one: 1, two: vardict, three: 3}
187    vardict['five'] = 55
188    # TODO: does not work yet
189    # constdict['two']['six'] = 66
190    var cd = constdict['two']
191    cd['six'] = 66
192    constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
193  END
194  CheckDefAndScriptSuccess(lines)
195enddef
196
197def Test_const_bang()
198  var lines =<< trim END
199      const var = 234
200      var = 99
201  END
202  CheckDefExecFailure(lines, 'E1018:', 2)
203  CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
204
205  lines =<< trim END
206      const ll = [2, 3, 4]
207      ll[0] = 99
208  END
209  CheckDefExecFailure(lines, 'E1119:', 2)
210  CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
211
212  lines =<< trim END
213      const ll = [2, 3, 4]
214      ll[3] = 99
215  END
216  CheckDefExecFailure(lines, 'E1118:', 2)
217  CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
218
219  lines =<< trim END
220      const dd = {one: 1, two: 2}
221      dd["one"] = 99
222  END
223  CheckDefExecFailure(lines, 'E1121:', 2)
224  CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
225
226  lines =<< trim END
227      const dd = {one: 1, two: 2}
228      dd["three"] = 99
229  END
230  CheckDefExecFailure(lines, 'E1120:')
231  CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
232enddef
233
234def Test_range_no_colon()
235  CheckDefFailure(['%s/a/b/'], 'E1050:')
236  CheckDefFailure(['+ s/a/b/'], 'E1050:')
237  CheckDefFailure(['- s/a/b/'], 'E1050:')
238  CheckDefFailure(['. s/a/b/'], 'E1050:')
239enddef
240
241
242def Test_block()
243  var outer = 1
244  {
245    var inner = 2
246    assert_equal(1, outer)
247    assert_equal(2, inner)
248  }
249  assert_equal(1, outer)
250enddef
251
252def Test_block_failure()
253  CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
254  CheckDefFailure(['}'], 'E1025:')
255  CheckDefFailure(['{', 'echo 1'], 'E1026:')
256enddef
257
258def Test_block_local_vars()
259  var lines =<< trim END
260      vim9script
261      v:testing = 1
262      if true
263        var text = ['hello']
264        def SayHello(): list<string>
265          return text
266        enddef
267        def SetText(v: string)
268          text = [v]
269        enddef
270      endif
271
272      if true
273        var text = ['again']
274        def SayAgain(): list<string>
275          return text
276        enddef
277      endif
278
279      # test that the "text" variables are not cleaned up
280      test_garbagecollect_now()
281
282      defcompile
283
284      assert_equal(['hello'], SayHello())
285      assert_equal(['again'], SayAgain())
286
287      SetText('foobar')
288      assert_equal(['foobar'], SayHello())
289
290      call writefile(['ok'], 'Xdidit')
291      qall!
292  END
293
294  # need to execute this with a separate Vim instance to avoid the current
295  # context gets garbage collected.
296  writefile(lines, 'Xscript')
297  RunVim([], [], '-S Xscript')
298  assert_equal(['ok'], readfile('Xdidit'))
299
300  delete('Xscript')
301  delete('Xdidit')
302enddef
303
304def Test_block_local_vars_with_func()
305  var lines =<< trim END
306      vim9script
307      if true
308        var foo = 'foo'
309        if true
310          var bar = 'bar'
311          def Func(): list<string>
312            return [foo, bar]
313          enddef
314        endif
315      endif
316      # function is compiled here, after blocks have finished, can still access
317      # "foo" and "bar"
318      assert_equal(['foo', 'bar'], Func())
319  END
320  CheckScriptSuccess(lines)
321enddef
322
323func g:NoSuchFunc()
324  echo 'none'
325endfunc
326
327def Test_try_catch_throw()
328  var l = []
329  try # comment
330    add(l, '1')
331    throw 'wrong'
332    add(l, '2')
333  catch # comment
334    add(l, v:exception)
335  finally # comment
336    add(l, '3')
337  endtry # comment
338  assert_equal(['1', 'wrong', '3'], l)
339
340  l = []
341  try
342    try
343      add(l, '1')
344      throw 'wrong'
345      add(l, '2')
346    catch /right/
347      add(l, v:exception)
348    endtry
349  catch /wrong/
350    add(l, 'caught')
351  fina
352    add(l, 'finally')
353  endtry
354  assert_equal(['1', 'caught', 'finally'], l)
355
356  var n: number
357  try
358    n = l[3]
359  catch /E684:/
360    n = 99
361  endtry
362  assert_equal(99, n)
363
364  var done = 'no'
365  if 0
366    try | catch | endtry
367  else
368    done = 'yes'
369  endif
370  assert_equal('yes', done)
371
372  done = 'no'
373  if 1
374    done = 'yes'
375  else
376    try | catch | endtry
377    done = 'never'
378  endif
379  assert_equal('yes', done)
380
381  if 1
382  else
383    try | catch /pat/ | endtry
384    try | catch /pat/
385    endtry
386    try
387    catch /pat/ | endtry
388    try
389    catch /pat/
390    endtry
391  endif
392
393  try
394    # string slice returns a string, not a number
395    n = g:astring[3]
396  catch /E1012:/
397    n = 77
398  endtry
399  assert_equal(77, n)
400
401  try
402    n = l[g:astring]
403  catch /E1012:/
404    n = 88
405  endtry
406  assert_equal(88, n)
407
408  try
409    n = s:does_not_exist
410  catch /E121:/
411    n = 111
412  endtry
413  assert_equal(111, n)
414
415  try
416    n = g:does_not_exist
417  catch /E121:/
418    n = 121
419  endtry
420  assert_equal(121, n)
421
422  var d = {one: 1}
423  try
424    n = d[g:astring]
425  catch /E716:/
426    n = 222
427  endtry
428  assert_equal(222, n)
429
430  try
431    n = -g:astring
432  catch /E39:/
433    n = 233
434  endtry
435  assert_equal(233, n)
436
437  try
438    n = +g:astring
439  catch /E1030:/
440    n = 244
441  endtry
442  assert_equal(244, n)
443
444  try
445    n = +g:alist
446  catch /E745:/
447    n = 255
448  endtry
449  assert_equal(255, n)
450
451  var nd: dict<any>
452  try
453    nd = {[g:alist]: 1}
454  catch /E1105:/
455    n = 266
456  endtry
457  assert_equal(266, n)
458
459  try
460    [n] = [1, 2, 3]
461  catch /E1093:/
462    n = 277
463  endtry
464  assert_equal(277, n)
465
466  try
467    &ts = g:astring
468  catch /E1012:/
469    n = 288
470  endtry
471  assert_equal(288, n)
472
473  try
474    &backspace = 'asdf'
475  catch /E474:/
476    n = 299
477  endtry
478  assert_equal(299, n)
479
480  l = [1]
481  try
482    l[3] = 3
483  catch /E684:/
484    n = 300
485  endtry
486  assert_equal(300, n)
487
488  try
489    unlet g:does_not_exist
490  catch /E108:/
491    n = 322
492  endtry
493  assert_equal(322, n)
494
495  try
496    d = {text: 1, [g:astring]: 2}
497  catch /E721:/
498    n = 333
499  endtry
500  assert_equal(333, n)
501
502  try
503    l = DeletedFunc()
504  catch /E933:/
505    n = 344
506  endtry
507  assert_equal(344, n)
508
509  try
510    echo len(v:true)
511  catch /E701:/
512    n = 355
513  endtry
514  assert_equal(355, n)
515
516  var P = function('g:NoSuchFunc')
517  delfunc g:NoSuchFunc
518  try
519    echo P()
520  catch /E117:/
521    n = 366
522  endtry
523  assert_equal(366, n)
524
525  try
526    echo g:NoSuchFunc()
527  catch /E117:/
528    n = 377
529  endtry
530  assert_equal(377, n)
531
532  try
533    echo g:alist + 4
534  catch /E745:/
535    n = 388
536  endtry
537  assert_equal(388, n)
538
539  try
540    echo 4 + g:alist
541  catch /E745:/
542    n = 399
543  endtry
544  assert_equal(399, n)
545
546  try
547    echo g:alist.member
548  catch /E715:/
549    n = 400
550  endtry
551  assert_equal(400, n)
552
553  try
554    echo d.member
555  catch /E716:/
556    n = 411
557  endtry
558  assert_equal(411, n)
559
560  var counter = 0
561  for i in range(4)
562    try
563      eval [][0]
564    catch
565    endtry
566    counter += 1
567  endfor
568  assert_equal(4, counter)
569enddef
570
571def Test_cnext_works_in_catch()
572  var lines =<< trim END
573      vim9script
574      au BufEnter * eval 0
575      writefile(['text'], 'Xfile1')
576      writefile(['text'], 'Xfile2')
577      var items = [
578          {lnum: 1, filename: 'Xfile1', valid: true},
579          {lnum: 1, filename: 'Xfile2', valid: true}
580        ]
581      setqflist([], ' ', {items: items})
582      cwindow
583
584      def CnextOrCfirst()
585        # if cnext fails, cfirst is used
586        try
587          cnext
588        catch
589          cfirst
590        endtry
591      enddef
592
593      CnextOrCfirst()
594      CnextOrCfirst()
595      writefile([getqflist({idx: 0}).idx], 'Xresult')
596      qall
597  END
598  writefile(lines, 'XCatchCnext')
599  RunVim([], [], '--clean -S XCatchCnext')
600  assert_equal(['1'], readfile('Xresult'))
601
602  delete('Xfile1')
603  delete('Xfile2')
604  delete('XCatchCnext')
605  delete('Xresult')
606enddef
607
608def Test_throw_skipped()
609  if 0
610    throw dontgethere
611  endif
612enddef
613
614def Test_nocatch_throw_silenced()
615  var lines =<< trim END
616    vim9script
617    def Func()
618      throw 'error'
619    enddef
620    silent! Func()
621  END
622  writefile(lines, 'XthrowSilenced')
623  source XthrowSilenced
624  delete('XthrowSilenced')
625enddef
626
627def DeletedFunc(): list<any>
628  return ['delete me']
629enddef
630defcompile
631delfunc DeletedFunc
632
633def ThrowFromDef()
634  throw "getout" # comment
635enddef
636
637func CatchInFunc()
638  try
639    call ThrowFromDef()
640  catch
641    let g:thrown_func = v:exception
642  endtry
643endfunc
644
645def CatchInDef()
646  try
647    ThrowFromDef()
648  catch
649    g:thrown_def = v:exception
650  endtry
651enddef
652
653def ReturnFinally(): string
654  try
655    return 'intry'
656  finall
657    g:in_finally = 'finally'
658  endtry
659  return 'end'
660enddef
661
662def Test_try_catch_nested()
663  CatchInFunc()
664  assert_equal('getout', g:thrown_func)
665
666  CatchInDef()
667  assert_equal('getout', g:thrown_def)
668
669  assert_equal('intry', ReturnFinally())
670  assert_equal('finally', g:in_finally)
671enddef
672
673def TryOne(): number
674  try
675    return 0
676  catch
677  endtry
678  return 0
679enddef
680
681def TryTwo(n: number): string
682  try
683    var x = {}
684  catch
685  endtry
686  return 'text'
687enddef
688
689def Test_try_catch_twice()
690  assert_equal('text', TryOne()->TryTwo())
691enddef
692
693def Test_try_catch_match()
694  var seq = 'a'
695  try
696    throw 'something'
697  catch /nothing/
698    seq ..= 'x'
699  catch /some/
700    seq ..= 'b'
701  catch /asdf/
702    seq ..= 'x'
703  catch ?a\?sdf?
704    seq ..= 'y'
705  finally
706    seq ..= 'c'
707  endtry
708  assert_equal('abc', seq)
709enddef
710
711def Test_try_catch_fails()
712  CheckDefFailure(['catch'], 'E603:')
713  CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
714  CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
715  CheckDefFailure(['finally'], 'E606:')
716  CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
717  CheckDefFailure(['endtry'], 'E602:')
718  CheckDefFailure(['while 1', 'endtry'], 'E170:')
719  CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
720  CheckDefFailure(['if 1', 'endtry'], 'E171:')
721  CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
722
723  CheckDefFailure(['throw'], 'E1143:')
724  CheckDefFailure(['throw xxx'], 'E1001:')
725enddef
726
727def Test_throw_vimscript()
728  # only checks line continuation
729  var lines =<< trim END
730      vim9script
731      try
732        throw 'one'
733              .. 'two'
734      catch
735        assert_equal('onetwo', v:exception)
736      endtry
737  END
738  CheckScriptSuccess(lines)
739
740  lines =<< trim END
741    vim9script
742    @r = ''
743    def Func()
744      throw @r
745    enddef
746    var result = ''
747    try
748      Func()
749    catch /E1129:/
750      result = 'caught'
751    endtry
752    assert_equal('caught', result)
753  END
754  CheckScriptSuccess(lines)
755enddef
756
757def Test_error_in_nested_function()
758  # an error in a nested :function aborts executin in the calling :def function
759  var lines =<< trim END
760      vim9script
761      def Func()
762        Error()
763        g:test_var = 1
764      enddef
765      func Error() abort
766        eval [][0]
767      endfunc
768      Func()
769  END
770  g:test_var = 0
771  CheckScriptFailure(lines, 'E684:')
772  assert_equal(0, g:test_var)
773enddef
774
775def Test_cexpr_vimscript()
776  # only checks line continuation
777  set errorformat=File\ %f\ line\ %l
778  var lines =<< trim END
779      vim9script
780      cexpr 'File'
781                .. ' someFile' ..
782                   ' line 19'
783      assert_equal(19, getqflist()[0].lnum)
784  END
785  CheckScriptSuccess(lines)
786  set errorformat&
787enddef
788
789def Test_statusline_syntax()
790  # legacy syntax is used for 'statusline'
791  var lines =<< trim END
792      vim9script
793      func g:Status()
794        return '%{"x" is# "x"}'
795      endfunc
796      set laststatus=2 statusline=%!Status()
797      redrawstatus
798      set laststatus statusline=
799  END
800  CheckScriptSuccess(lines)
801enddef
802
803def Test_list_vimscript()
804  # checks line continuation and comments
805  var lines =<< trim END
806      vim9script
807      var mylist = [
808            'one',
809            # comment
810            'two', # empty line follows
811
812            'three',
813            ]
814      assert_equal(['one', 'two', 'three'], mylist)
815  END
816  CheckScriptSuccess(lines)
817
818  # check all lines from heredoc are kept
819  lines =<< trim END
820      # comment 1
821      two
822      # comment 3
823
824      five
825      # comment 6
826  END
827  assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
828
829  lines =<< trim END
830    [{
831      a: 0}]->string()->assert_equal("[{'a': 0}]")
832  END
833  CheckDefAndScriptSuccess(lines)
834enddef
835
836if has('channel')
837  let someJob = test_null_job()
838
839  def FuncWithError()
840    echomsg g:someJob
841  enddef
842
843  func Test_convert_emsg_to_exception()
844    try
845      call FuncWithError()
846    catch
847      call assert_match('Vim:E908:', v:exception)
848    endtry
849  endfunc
850endif
851
852let s:export_script_lines =<< trim END
853  vim9script
854  var name: string = 'bob'
855  def Concat(arg: string): string
856    return name .. arg
857  enddef
858  g:result = Concat('bie')
859  g:localname = name
860
861  export const CONST = 1234
862  export var exported = 9876
863  export var exp_name = 'John'
864  export def Exported(): string
865    return 'Exported'
866  enddef
867END
868
869def Undo_export_script_lines()
870  unlet g:result
871  unlet g:localname
872enddef
873
874def Test_vim9_import_export()
875  var import_script_lines =<< trim END
876    vim9script
877    import {exported, Exported} from './Xexport.vim'
878    g:imported = exported
879    exported += 3
880    g:imported_added = exported
881    g:imported_func = Exported()
882
883    def GetExported(): string
884      var local_dict = {ref: Exported}
885      return local_dict.ref()
886    enddef
887    g:funcref_result = GetExported()
888
889    import {exp_name} from './Xexport.vim'
890    g:imported_name = exp_name
891    exp_name ..= ' Doe'
892    g:imported_name_appended = exp_name
893    g:imported_later = exported
894  END
895
896  writefile(import_script_lines, 'Ximport.vim')
897  writefile(s:export_script_lines, 'Xexport.vim')
898
899  source Ximport.vim
900
901  assert_equal('bobbie', g:result)
902  assert_equal('bob', g:localname)
903  assert_equal(9876, g:imported)
904  assert_equal(9879, g:imported_added)
905  assert_equal(9879, g:imported_later)
906  assert_equal('Exported', g:imported_func)
907  assert_equal('Exported', g:funcref_result)
908  assert_equal('John', g:imported_name)
909  assert_equal('John Doe', g:imported_name_appended)
910  assert_false(exists('g:name'))
911
912  Undo_export_script_lines()
913  unlet g:imported
914  unlet g:imported_added
915  unlet g:imported_later
916  unlet g:imported_func
917  unlet g:imported_name g:imported_name_appended
918  delete('Ximport.vim')
919
920  # similar, with line breaks
921  var import_line_break_script_lines =<< trim END
922    vim9script
923    import {
924        exported,
925        Exported,
926        }
927        from
928        './Xexport.vim'
929    g:imported = exported
930    exported += 5
931    g:imported_added = exported
932    g:imported_func = Exported()
933  END
934  writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
935  source Ximport_lbr.vim
936
937  assert_equal(9876, g:imported)
938  assert_equal(9881, g:imported_added)
939  assert_equal('Exported', g:imported_func)
940
941  # exported script not sourced again
942  assert_false(exists('g:result'))
943  unlet g:imported
944  unlet g:imported_added
945  unlet g:imported_func
946  delete('Ximport_lbr.vim')
947
948  # import inside :def function
949  var import_in_def_lines =<< trim END
950    vim9script
951    def ImportInDef()
952      import exported from './Xexport.vim'
953      g:imported = exported
954      exported += 7
955      g:imported_added = exported
956    enddef
957    ImportInDef()
958  END
959  writefile(import_in_def_lines, 'Ximport2.vim')
960  source Ximport2.vim
961  # TODO: this should be 9879
962  assert_equal(9876, g:imported)
963  assert_equal(9883, g:imported_added)
964  unlet g:imported
965  unlet g:imported_added
966  delete('Ximport2.vim')
967
968  var import_star_as_lines =<< trim END
969    vim9script
970    import * as Export from './Xexport.vim'
971    def UseExport()
972      g:imported = Export.exported
973    enddef
974    UseExport()
975  END
976  writefile(import_star_as_lines, 'Ximport.vim')
977  source Ximport.vim
978  assert_equal(9883, g:imported)
979
980  var import_star_as_lines_no_dot =<< trim END
981    vim9script
982    import * as Export from './Xexport.vim'
983    def Func()
984      var dummy = 1
985      var imported = Export + dummy
986    enddef
987    defcompile
988  END
989  writefile(import_star_as_lines_no_dot, 'Ximport.vim')
990  assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
991
992  var import_star_as_lines_dot_space =<< trim END
993    vim9script
994    import * as Export from './Xexport.vim'
995    def Func()
996      var imported = Export . exported
997    enddef
998    defcompile
999  END
1000  writefile(import_star_as_lines_dot_space, 'Ximport.vim')
1001  assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
1002
1003  var import_star_as_duplicated =<< trim END
1004    vim9script
1005    import * as Export from './Xexport.vim'
1006    var some = 'other'
1007    import * as Export from './Xexport.vim'
1008    defcompile
1009  END
1010  writefile(import_star_as_duplicated, 'Ximport.vim')
1011  assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1012
1013  var import_star_as_lines_missing_name =<< trim END
1014    vim9script
1015    import * as Export from './Xexport.vim'
1016    def Func()
1017      var imported = Export.
1018    enddef
1019    defcompile
1020  END
1021  writefile(import_star_as_lines_missing_name, 'Ximport.vim')
1022  assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
1023
1024  var import_star_as_lbr_lines =<< trim END
1025    vim9script
1026    import *
1027        as Export
1028        from
1029        './Xexport.vim'
1030    def UseExport()
1031      g:imported = Export.exported
1032    enddef
1033    UseExport()
1034  END
1035  writefile(import_star_as_lbr_lines, 'Ximport.vim')
1036  source Ximport.vim
1037  assert_equal(9883, g:imported)
1038
1039  var import_star_lines =<< trim END
1040    vim9script
1041    import * from './Xexport.vim'
1042  END
1043  writefile(import_star_lines, 'Ximport.vim')
1044  assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
1045
1046  # try to import something that exists but is not exported
1047  var import_not_exported_lines =<< trim END
1048    vim9script
1049    import name from './Xexport.vim'
1050  END
1051  writefile(import_not_exported_lines, 'Ximport.vim')
1052  assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
1053
1054  # try to import something that is already defined
1055  var import_already_defined =<< trim END
1056    vim9script
1057    var exported = 'something'
1058    import exported from './Xexport.vim'
1059  END
1060  writefile(import_already_defined, 'Ximport.vim')
1061  assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1062
1063  # try to import something that is already defined
1064  import_already_defined =<< trim END
1065    vim9script
1066    var exported = 'something'
1067    import * as exported from './Xexport.vim'
1068  END
1069  writefile(import_already_defined, 'Ximport.vim')
1070  assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1071
1072  # try to import something that is already defined
1073  import_already_defined =<< trim END
1074    vim9script
1075    var exported = 'something'
1076    import {exported} from './Xexport.vim'
1077  END
1078  writefile(import_already_defined, 'Ximport.vim')
1079  assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1080
1081  # try changing an imported const
1082  var import_assign_to_const =<< trim END
1083    vim9script
1084    import CONST from './Xexport.vim'
1085    def Assign()
1086      CONST = 987
1087    enddef
1088    defcompile
1089  END
1090  writefile(import_assign_to_const, 'Ximport.vim')
1091  assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1092
1093  # import a very long name, requires making a copy
1094  var import_long_name_lines =<< trim END
1095    vim9script
1096    import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1097  END
1098  writefile(import_long_name_lines, 'Ximport.vim')
1099  assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
1100
1101  var import_no_from_lines =<< trim END
1102    vim9script
1103    import name './Xexport.vim'
1104  END
1105  writefile(import_no_from_lines, 'Ximport.vim')
1106  assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
1107
1108  var import_invalid_string_lines =<< trim END
1109    vim9script
1110    import name from Xexport.vim
1111  END
1112  writefile(import_invalid_string_lines, 'Ximport.vim')
1113  assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
1114
1115  var import_wrong_name_lines =<< trim END
1116    vim9script
1117    import name from './XnoExport.vim'
1118  END
1119  writefile(import_wrong_name_lines, 'Ximport.vim')
1120  assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
1121
1122  var import_missing_comma_lines =<< trim END
1123    vim9script
1124    import {exported name} from './Xexport.vim'
1125  END
1126  writefile(import_missing_comma_lines, 'Ximport3.vim')
1127  assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
1128
1129  delete('Ximport.vim')
1130  delete('Ximport3.vim')
1131  delete('Xexport.vim')
1132
1133  # Check that in a Vim9 script 'cpo' is set to the Vim default.
1134  set cpo&vi
1135  var cpo_before = &cpo
1136  var lines =<< trim END
1137    vim9script
1138    g:cpo_in_vim9script = &cpo
1139  END
1140  writefile(lines, 'Xvim9_script')
1141  source Xvim9_script
1142  assert_equal(cpo_before, &cpo)
1143  set cpo&vim
1144  assert_equal(&cpo, g:cpo_in_vim9script)
1145  delete('Xvim9_script')
1146enddef
1147
1148func g:Trigger()
1149  source Ximport.vim
1150  return "echo 'yes'\<CR>"
1151endfunc
1152
1153def Test_import_export_expr_map()
1154  # check that :import and :export work when buffer is locked
1155  var export_lines =<< trim END
1156    vim9script
1157    export def That(): string
1158      return 'yes'
1159    enddef
1160  END
1161  writefile(export_lines, 'Xexport_that.vim')
1162
1163  var import_lines =<< trim END
1164    vim9script
1165    import That from './Xexport_that.vim'
1166    assert_equal('yes', That())
1167  END
1168  writefile(import_lines, 'Ximport.vim')
1169
1170  nnoremap <expr> trigger g:Trigger()
1171  feedkeys('trigger', "xt")
1172
1173  delete('Xexport_that.vim')
1174  delete('Ximport.vim')
1175  nunmap trigger
1176enddef
1177
1178def Test_import_in_filetype()
1179  # check that :import works when the buffer is locked
1180  mkdir('ftplugin', 'p')
1181  var export_lines =<< trim END
1182    vim9script
1183    export var That = 'yes'
1184  END
1185  writefile(export_lines, 'ftplugin/Xexport_ft.vim')
1186
1187  var import_lines =<< trim END
1188    vim9script
1189    import That from './Xexport_ft.vim'
1190    assert_equal('yes', That)
1191    g:did_load_mytpe = 1
1192  END
1193  writefile(import_lines, 'ftplugin/qf.vim')
1194
1195  var save_rtp = &rtp
1196  &rtp = getcwd() .. ',' .. &rtp
1197
1198  filetype plugin on
1199  copen
1200  assert_equal(1, g:did_load_mytpe)
1201
1202  quit!
1203  delete('Xexport_ft.vim')
1204  delete('ftplugin', 'rf')
1205  &rtp = save_rtp
1206enddef
1207
1208def Test_use_import_in_mapping()
1209  var lines =<< trim END
1210      vim9script
1211      export def Funcx()
1212        g:result = 42
1213      enddef
1214  END
1215  writefile(lines, 'XsomeExport.vim')
1216  lines =<< trim END
1217      vim9script
1218      import Funcx from './XsomeExport.vim'
1219      nnoremap <F3> :call <sid>Funcx()<cr>
1220  END
1221  writefile(lines, 'Xmapscript.vim')
1222
1223  source Xmapscript.vim
1224  feedkeys("\<F3>", "xt")
1225  assert_equal(42, g:result)
1226
1227  unlet g:result
1228  delete('XsomeExport.vim')
1229  delete('Xmapscript.vim')
1230  nunmap <F3>
1231enddef
1232
1233def Test_vim9script_fails()
1234  CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1235  CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
1236  CheckScriptFailure(['export var some = 123'], 'E1042:')
1237  CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
1238  CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1239  CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1240
1241  CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
1242  CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1243
1244  assert_fails('vim9script', 'E1038:')
1245  assert_fails('export something', 'E1043:')
1246enddef
1247
1248func Test_import_fails_without_script()
1249  CheckRunVimInTerminal
1250
1251  " call indirectly to avoid compilation error for missing functions
1252  call Run_Test_import_fails_on_command_line()
1253endfunc
1254
1255def Run_Test_import_fails_on_command_line()
1256  var export =<< trim END
1257    vim9script
1258    export def Foo(): number
1259        return 0
1260    enddef
1261  END
1262  writefile(export, 'XexportCmd.vim')
1263
1264  var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
1265                rows: 6, wait_for_ruler: 0})
1266  WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
1267
1268  delete('XexportCmd.vim')
1269  StopVimInTerminal(buf)
1270enddef
1271
1272def Test_vim9script_reload_noclear()
1273  var lines =<< trim END
1274    vim9script
1275    export var exported = 'thexport'
1276  END
1277  writefile(lines, 'XExportReload')
1278  lines =<< trim END
1279    vim9script noclear
1280    g:loadCount += 1
1281    var s:reloaded = 'init'
1282    import exported from './XExportReload'
1283
1284    def Again(): string
1285      return 'again'
1286    enddef
1287
1288    if exists('s:loaded') | finish | endif
1289    var s:loaded = true
1290
1291    var s:notReloaded = 'yes'
1292    s:reloaded = 'first'
1293    def g:Values(): list<string>
1294      return [s:reloaded, s:notReloaded, Again(), Once(), exported]
1295    enddef
1296
1297    def Once(): string
1298      return 'once'
1299    enddef
1300  END
1301  writefile(lines, 'XReloaded')
1302  g:loadCount = 0
1303  source XReloaded
1304  assert_equal(1, g:loadCount)
1305  assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1306  source XReloaded
1307  assert_equal(2, g:loadCount)
1308  assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1309  source XReloaded
1310  assert_equal(3, g:loadCount)
1311  assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1312
1313  delete('XReloaded')
1314  delete('XExportReload')
1315  delfunc g:Values
1316  unlet g:loadCount
1317enddef
1318
1319def Test_vim9script_reload_import()
1320  var lines =<< trim END
1321    vim9script
1322    const var = ''
1323    var valone = 1234
1324    def MyFunc(arg: string)
1325       valone = 5678
1326    enddef
1327  END
1328  var morelines =<< trim END
1329    var valtwo = 222
1330    export def GetValtwo(): number
1331      return valtwo
1332    enddef
1333  END
1334  writefile(lines + morelines, 'Xreload.vim')
1335  source Xreload.vim
1336  source Xreload.vim
1337  source Xreload.vim
1338
1339  var testlines =<< trim END
1340    vim9script
1341    def TheFunc()
1342      import GetValtwo from './Xreload.vim'
1343      assert_equal(222, GetValtwo())
1344    enddef
1345    TheFunc()
1346  END
1347  writefile(testlines, 'Ximport.vim')
1348  source Ximport.vim
1349
1350  # Test that when not using "morelines" GetValtwo() and valtwo are still
1351  # defined, because import doesn't reload a script.
1352  writefile(lines, 'Xreload.vim')
1353  source Ximport.vim
1354
1355  # cannot declare a var twice
1356  lines =<< trim END
1357    vim9script
1358    var valone = 1234
1359    var valone = 5678
1360  END
1361  writefile(lines, 'Xreload.vim')
1362  assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1363
1364  delete('Xreload.vim')
1365  delete('Ximport.vim')
1366enddef
1367
1368" if a script is reloaded with a script-local variable that changed its type, a
1369" compiled function using that variable must fail.
1370def Test_script_reload_change_type()
1371  var lines =<< trim END
1372    vim9script noclear
1373    var str = 'string'
1374    def g:GetStr(): string
1375      return str .. 'xxx'
1376    enddef
1377  END
1378  writefile(lines, 'Xreload.vim')
1379  source Xreload.vim
1380  echo g:GetStr()
1381
1382  lines =<< trim END
1383    vim9script noclear
1384    var str = 1234
1385  END
1386  writefile(lines, 'Xreload.vim')
1387  source Xreload.vim
1388  assert_fails('echo g:GetStr()', 'E1150:')
1389
1390  delfunc g:GetStr
1391  delete('Xreload.vim')
1392enddef
1393
1394def s:RetSome(): string
1395  return 'some'
1396enddef
1397
1398" Not exported function that is referenced needs to be accessed by the
1399" script-local name.
1400def Test_vim9script_funcref()
1401  var sortlines =<< trim END
1402      vim9script
1403      def Compare(i1: number, i2: number): number
1404        return i2 - i1
1405      enddef
1406
1407      export def FastSort(): list<number>
1408        return range(5)->sort(Compare)
1409      enddef
1410  END
1411  writefile(sortlines, 'Xsort.vim')
1412
1413  var lines =<< trim END
1414    vim9script
1415    import FastSort from './Xsort.vim'
1416    def Test()
1417      g:result = FastSort()
1418    enddef
1419    Test()
1420  END
1421  writefile(lines, 'Xscript.vim')
1422
1423  source Xscript.vim
1424  assert_equal([4, 3, 2, 1, 0], g:result)
1425
1426  unlet g:result
1427  delete('Xsort.vim')
1428  delete('Xscript.vim')
1429
1430  var Funcref = function('s:RetSome')
1431  assert_equal('some', Funcref())
1432enddef
1433
1434" Check that when searching for "FilterFunc" it finds the import in the
1435" script where FastFilter() is called from, both as a string and as a direct
1436" function reference.
1437def Test_vim9script_funcref_other_script()
1438  var filterLines =<< trim END
1439    vim9script
1440    export def FilterFunc(idx: number, val: number): bool
1441      return idx % 2 == 1
1442    enddef
1443    export def FastFilter(): list<number>
1444      return range(10)->filter('FilterFunc')
1445    enddef
1446    export def FastFilterDirect(): list<number>
1447      return range(10)->filter(FilterFunc)
1448    enddef
1449  END
1450  writefile(filterLines, 'Xfilter.vim')
1451
1452  var lines =<< trim END
1453    vim9script
1454    import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
1455    def Test()
1456      var x: list<number> = FastFilter()
1457    enddef
1458    Test()
1459    def TestDirect()
1460      var x: list<number> = FastFilterDirect()
1461    enddef
1462    TestDirect()
1463  END
1464  CheckScriptSuccess(lines)
1465  delete('Xfilter.vim')
1466enddef
1467
1468def Test_vim9script_reload_delfunc()
1469  var first_lines =<< trim END
1470    vim9script
1471    def FuncYes(): string
1472      return 'yes'
1473    enddef
1474  END
1475  var withno_lines =<< trim END
1476    def FuncNo(): string
1477      return 'no'
1478    enddef
1479    def g:DoCheck(no_exists: bool)
1480      assert_equal('yes', FuncYes())
1481      assert_equal('no', FuncNo())
1482    enddef
1483  END
1484  var nono_lines =<< trim END
1485    def g:DoCheck(no_exists: bool)
1486      assert_equal('yes', FuncYes())
1487      assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
1488    enddef
1489  END
1490
1491  # FuncNo() is defined
1492  writefile(first_lines + withno_lines, 'Xreloaded.vim')
1493  source Xreloaded.vim
1494  g:DoCheck(true)
1495
1496  # FuncNo() is not redefined
1497  writefile(first_lines + nono_lines, 'Xreloaded.vim')
1498  source Xreloaded.vim
1499  g:DoCheck(false)
1500
1501  # FuncNo() is back
1502  writefile(first_lines + withno_lines, 'Xreloaded.vim')
1503  source Xreloaded.vim
1504  g:DoCheck(false)
1505
1506  delete('Xreloaded.vim')
1507enddef
1508
1509def Test_vim9script_reload_delvar()
1510  # write the script with a script-local variable
1511  var lines =<< trim END
1512    vim9script
1513    var name = 'string'
1514  END
1515  writefile(lines, 'XreloadVar.vim')
1516  source XreloadVar.vim
1517
1518  # now write the script using the same variable locally - works
1519  lines =<< trim END
1520    vim9script
1521    def Func()
1522      var name = 'string'
1523    enddef
1524  END
1525  writefile(lines, 'XreloadVar.vim')
1526  source XreloadVar.vim
1527
1528  delete('XreloadVar.vim')
1529enddef
1530
1531def Test_import_absolute()
1532  var import_lines = [
1533        'vim9script',
1534        'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1535        'def UseExported()',
1536        '  g:imported_abs = exported',
1537        '  exported = 8888',
1538        '  g:imported_after = exported',
1539        'enddef',
1540        'UseExported()',
1541        'g:import_disassembled = execute("disass UseExported")',
1542        ]
1543  writefile(import_lines, 'Ximport_abs.vim')
1544  writefile(s:export_script_lines, 'Xexport_abs.vim')
1545
1546  source Ximport_abs.vim
1547
1548  assert_equal(9876, g:imported_abs)
1549  assert_equal(8888, g:imported_after)
1550  assert_match('<SNR>\d\+_UseExported\_s*' ..
1551          'g:imported_abs = exported\_s*' ..
1552          '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1553          '1 STOREG g:imported_abs\_s*' ..
1554          'exported = 8888\_s*' ..
1555          '2 PUSHNR 8888\_s*' ..
1556          '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1557          'g:imported_after = exported\_s*' ..
1558          '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1559          '5 STOREG g:imported_after',
1560        g:import_disassembled)
1561
1562  Undo_export_script_lines()
1563  unlet g:imported_abs
1564  unlet g:import_disassembled
1565
1566  delete('Ximport_abs.vim')
1567  delete('Xexport_abs.vim')
1568enddef
1569
1570def Test_import_rtp()
1571  var import_lines = [
1572        'vim9script',
1573        'import exported from "Xexport_rtp.vim"',
1574        'g:imported_rtp = exported',
1575        ]
1576  writefile(import_lines, 'Ximport_rtp.vim')
1577  mkdir('import')
1578  writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1579
1580  var save_rtp = &rtp
1581  &rtp = getcwd()
1582  source Ximport_rtp.vim
1583  &rtp = save_rtp
1584
1585  assert_equal(9876, g:imported_rtp)
1586
1587  Undo_export_script_lines()
1588  unlet g:imported_rtp
1589  delete('Ximport_rtp.vim')
1590  delete('import', 'rf')
1591enddef
1592
1593def Test_import_compile_error()
1594  var export_lines = [
1595        'vim9script',
1596        'export def ExpFunc(): string',
1597        '  return notDefined',
1598        'enddef',
1599        ]
1600  writefile(export_lines, 'Xexported.vim')
1601
1602  var import_lines = [
1603        'vim9script',
1604        'import ExpFunc from "./Xexported.vim"',
1605        'def ImpFunc()',
1606        '  echo ExpFunc()',
1607        'enddef',
1608        'defcompile',
1609        ]
1610  writefile(import_lines, 'Ximport.vim')
1611
1612  try
1613    source Ximport.vim
1614  catch /E1001/
1615    # Error should be fore the Xexported.vim file.
1616    assert_match('E1001: Variable not found: notDefined', v:exception)
1617    assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1618  endtry
1619
1620  delete('Xexported.vim')
1621  delete('Ximport.vim')
1622enddef
1623
1624def Test_func_redefine_error()
1625  var lines = [
1626        'vim9script',
1627        'def Func()',
1628        '  eval [][0]',
1629        'enddef',
1630        'Func()',
1631        ]
1632  writefile(lines, 'Xtestscript.vim')
1633
1634  for count in range(3)
1635    try
1636      source Xtestscript.vim
1637    catch /E684/
1638      # function name should contain <SNR> every time
1639      assert_match('E684: list index out of range', v:exception)
1640      assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
1641    endtry
1642  endfor
1643
1644  delete('Xtestscript.vim')
1645enddef
1646
1647def Test_func_overrules_import_fails()
1648  var export_lines =<< trim END
1649      vim9script
1650      export def Func()
1651        echo 'imported'
1652      enddef
1653  END
1654  writefile(export_lines, 'XexportedFunc.vim')
1655
1656  var lines =<< trim END
1657    vim9script
1658    import Func from './XexportedFunc.vim'
1659    def Func()
1660      echo 'local to function'
1661    enddef
1662  END
1663  CheckScriptFailure(lines, 'E1073:')
1664
1665  lines =<< trim END
1666    vim9script
1667    import Func from './XexportedFunc.vim'
1668    def Outer()
1669      def Func()
1670        echo 'local to function'
1671      enddef
1672    enddef
1673    defcompile
1674  END
1675  CheckScriptFailure(lines, 'E1073:')
1676
1677  delete('XexportedFunc.vim')
1678enddef
1679
1680def Test_func_redefine_fails()
1681  var lines =<< trim END
1682    vim9script
1683    def Func()
1684      echo 'one'
1685    enddef
1686    def Func()
1687      echo 'two'
1688    enddef
1689  END
1690  CheckScriptFailure(lines, 'E1073:')
1691
1692  lines =<< trim END
1693    vim9script
1694    def Foo(): string
1695      return 'foo'
1696      enddef
1697    def Func()
1698      var  Foo = {-> 'lambda'}
1699    enddef
1700    defcompile
1701  END
1702  CheckScriptFailure(lines, 'E1073:')
1703enddef
1704
1705def Test_fixed_size_list()
1706  # will be allocated as one piece of memory, check that changes work
1707  var l = [1, 2, 3, 4]
1708  l->remove(0)
1709  l->add(5)
1710  l->insert(99, 1)
1711  assert_equal([2, 99, 3, 4, 5], l)
1712enddef
1713
1714def Test_no_insert_xit()
1715  CheckDefExecFailure(['a = 1'], 'E1100:')
1716  CheckDefExecFailure(['c = 1'], 'E1100:')
1717  CheckDefExecFailure(['i = 1'], 'E1100:')
1718  CheckDefExecFailure(['t = 1'], 'E1100:')
1719  CheckDefExecFailure(['x = 1'], 'E1100:')
1720
1721  CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
1722  CheckScriptFailure(['vim9script', 'a'], 'E1100:')
1723  CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
1724  CheckScriptFailure(['vim9script', 'c'], 'E1100:')
1725  CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
1726  CheckScriptFailure(['vim9script', 'i'], 'E1100:')
1727  CheckScriptFailure(['vim9script', 't'], 'E1100:')
1728  CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
1729  CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
1730enddef
1731
1732def IfElse(what: number): string
1733  var res = ''
1734  if what == 1
1735    res = "one"
1736  elseif what == 2
1737    res = "two"
1738  else
1739    res = "three"
1740  endif
1741  return res
1742enddef
1743
1744def Test_if_elseif_else()
1745  assert_equal('one', IfElse(1))
1746  assert_equal('two', IfElse(2))
1747  assert_equal('three', IfElse(3))
1748enddef
1749
1750def Test_if_elseif_else_fails()
1751  CheckDefFailure(['elseif true'], 'E582:')
1752  CheckDefFailure(['else'], 'E581:')
1753  CheckDefFailure(['endif'], 'E580:')
1754  CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
1755  CheckDefFailure(['if true', 'echo 1'], 'E171:')
1756
1757  var lines =<< trim END
1758      var s = ''
1759      if s = ''
1760      endif
1761  END
1762  CheckDefFailure(lines, 'E488:')
1763
1764  lines =<< trim END
1765      var s = ''
1766      if s == ''
1767      elseif s = ''
1768      endif
1769  END
1770  CheckDefFailure(lines, 'E488:')
1771enddef
1772
1773let g:bool_true = v:true
1774let g:bool_false = v:false
1775
1776def Test_if_const_expr()
1777  var res = false
1778  if true ? true : false
1779    res = true
1780  endif
1781  assert_equal(true, res)
1782
1783  g:glob = 2
1784  if false
1785    execute('g:glob = 3')
1786  endif
1787  assert_equal(2, g:glob)
1788  if true
1789    execute('g:glob = 3')
1790  endif
1791  assert_equal(3, g:glob)
1792
1793  res = false
1794  if g:bool_true ? true : false
1795    res = true
1796  endif
1797  assert_equal(true, res)
1798
1799  res = false
1800  if true ? g:bool_true : false
1801    res = true
1802  endif
1803  assert_equal(true, res)
1804
1805  res = false
1806  if true ? true : g:bool_false
1807    res = true
1808  endif
1809  assert_equal(true, res)
1810
1811  res = false
1812  if true ? false : true
1813    res = true
1814  endif
1815  assert_equal(false, res)
1816
1817  res = false
1818  if false ? false : true
1819    res = true
1820  endif
1821  assert_equal(true, res)
1822
1823  res = false
1824  if false ? true : false
1825    res = true
1826  endif
1827  assert_equal(false, res)
1828
1829  res = false
1830  if has('xyz') ? true : false
1831    res = true
1832  endif
1833  assert_equal(false, res)
1834
1835  res = false
1836  if true && true
1837    res = true
1838  endif
1839  assert_equal(true, res)
1840
1841  res = false
1842  if true && false
1843    res = true
1844  endif
1845  assert_equal(false, res)
1846
1847  res = false
1848  if g:bool_true && false
1849    res = true
1850  endif
1851  assert_equal(false, res)
1852
1853  res = false
1854  if true && g:bool_false
1855    res = true
1856  endif
1857  assert_equal(false, res)
1858
1859  res = false
1860  if false && false
1861    res = true
1862  endif
1863  assert_equal(false, res)
1864
1865  res = false
1866  if true || false
1867    res = true
1868  endif
1869  assert_equal(true, res)
1870
1871  res = false
1872  if g:bool_true || false
1873    res = true
1874  endif
1875  assert_equal(true, res)
1876
1877  res = false
1878  if true || g:bool_false
1879    res = true
1880  endif
1881  assert_equal(true, res)
1882
1883  res = false
1884  if false || false
1885    res = true
1886  endif
1887  assert_equal(false, res)
1888
1889  # with constant "false" expression may be invalid so long as the syntax is OK
1890  if false | eval 0 | endif
1891  if false | eval burp + 234 | endif
1892  if false | echo burp 234 'asd' | endif
1893  if false
1894    burp
1895  endif
1896enddef
1897
1898def Test_if_const_expr_fails()
1899  CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
1900  CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
1901  CheckDefFailure(["if has('aaa'"], 'E110:')
1902  CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
1903enddef
1904
1905def RunNested(i: number): number
1906  var x: number = 0
1907  if i % 2
1908    if 1
1909      # comment
1910    else
1911      # comment
1912    endif
1913    x += 1
1914  else
1915    x += 1000
1916  endif
1917  return x
1918enddef
1919
1920def Test_nested_if()
1921  assert_equal(1, RunNested(1))
1922  assert_equal(1000, RunNested(2))
1923enddef
1924
1925def Test_execute_cmd()
1926  # missing argument is ignored
1927  execute
1928  execute # comment
1929
1930  new
1931  setline(1, 'default')
1932  execute 'setline(1, "execute-string")'
1933  assert_equal('execute-string', getline(1))
1934
1935  execute "setline(1, 'execute-string')"
1936  assert_equal('execute-string', getline(1))
1937
1938  var cmd1 = 'setline(1,'
1939  var cmd2 = '"execute-var")'
1940  execute cmd1 cmd2 # comment
1941  assert_equal('execute-var', getline(1))
1942
1943  execute cmd1 cmd2 '|setline(1, "execute-var-string")'
1944  assert_equal('execute-var-string', getline(1))
1945
1946  var cmd_first = 'call '
1947  var cmd_last = 'setline(1, "execute-var-var")'
1948  execute cmd_first .. cmd_last
1949  assert_equal('execute-var-var', getline(1))
1950  bwipe!
1951
1952  var n = true
1953  execute 'echomsg' (n ? '"true"' : '"no"')
1954  assert_match('^true$', Screenline(&lines))
1955
1956  echomsg [1, 2, 3] {a: 1, b: 2}
1957  assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
1958
1959  CheckDefFailure(['execute xxx'], 'E1001:', 1)
1960  CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
1961  CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
1962enddef
1963
1964def Test_execute_cmd_vimscript()
1965  # only checks line continuation
1966  var lines =<< trim END
1967      vim9script
1968      execute 'g:someVar'
1969                .. ' = ' ..
1970                   '28'
1971      assert_equal(28, g:someVar)
1972      unlet g:someVar
1973  END
1974  CheckScriptSuccess(lines)
1975enddef
1976
1977def Test_echo_cmd()
1978  echo 'some' # comment
1979  echon 'thing'
1980  assert_match('^something$', Screenline(&lines))
1981
1982  echo "some" # comment
1983  echon "thing"
1984  assert_match('^something$', Screenline(&lines))
1985
1986  var str1 = 'some'
1987  var str2 = 'more'
1988  echo str1 str2
1989  assert_match('^some more$', Screenline(&lines))
1990
1991  CheckDefFailure(['echo "xxx"# comment'], 'E488:')
1992enddef
1993
1994def Test_echomsg_cmd()
1995  echomsg 'some' 'more' # comment
1996  assert_match('^some more$', Screenline(&lines))
1997  echo 'clear'
1998  :1messages
1999  assert_match('^some more$', Screenline(&lines))
2000
2001  CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
2002enddef
2003
2004def Test_echomsg_cmd_vimscript()
2005  # only checks line continuation
2006  var lines =<< trim END
2007      vim9script
2008      echomsg 'here'
2009                .. ' is ' ..
2010                   'a message'
2011      assert_match('^here is a message$', Screenline(&lines))
2012  END
2013  CheckScriptSuccess(lines)
2014enddef
2015
2016def Test_echoerr_cmd()
2017  try
2018    echoerr 'something' 'wrong' # comment
2019  catch
2020    assert_match('something wrong', v:exception)
2021  endtry
2022enddef
2023
2024def Test_echoerr_cmd_vimscript()
2025  # only checks line continuation
2026  var lines =<< trim END
2027      vim9script
2028      try
2029        echoerr 'this'
2030                .. ' is ' ..
2031                   'wrong'
2032      catch
2033        assert_match('this is wrong', v:exception)
2034      endtry
2035  END
2036  CheckScriptSuccess(lines)
2037enddef
2038
2039def Test_for_outside_of_function()
2040  var lines =<< trim END
2041    vim9script
2042    new
2043    for var in range(0, 3)
2044      append(line('$'), var)
2045    endfor
2046    assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2047    bwipe!
2048  END
2049  writefile(lines, 'Xvim9for.vim')
2050  source Xvim9for.vim
2051  delete('Xvim9for.vim')
2052enddef
2053
2054def Test_for_loop()
2055  var result = ''
2056  for cnt in range(7)
2057    if cnt == 4
2058      break
2059    endif
2060    if cnt == 2
2061      continue
2062    endif
2063    result ..= cnt .. '_'
2064  endfor
2065  assert_equal('0_1_3_', result)
2066
2067  var concat = ''
2068  for str in eval('["one", "two"]')
2069    concat ..= str
2070  endfor
2071  assert_equal('onetwo', concat)
2072
2073  var total = 0
2074  for nr in
2075      [1, 2, 3]
2076    total += nr
2077  endfor
2078  assert_equal(6, total)
2079
2080  total = 0
2081  for nr
2082    in [1, 2, 3]
2083    total += nr
2084  endfor
2085  assert_equal(6, total)
2086
2087  total = 0
2088  for nr
2089    in
2090    [1, 2, 3]
2091    total += nr
2092  endfor
2093  assert_equal(6, total)
2094
2095  var res = ""
2096  for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2097    res ..= n .. s
2098  endfor
2099  assert_equal('1a2b', res)
2100enddef
2101
2102def Test_for_loop_fails()
2103  CheckDefFailure(['for '], 'E1097:')
2104  CheckDefFailure(['for x'], 'E1097:')
2105  CheckDefFailure(['for x in'], 'E1097:')
2106  CheckDefFailure(['for # in range(5)'], 'E690:')
2107  CheckDefFailure(['for i In range(5)'], 'E690:')
2108  CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:')
2109  CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
2110  delfunc! g:Func
2111  CheckDefFailure(['for i in "text"'], 'E1012:')
2112  CheckDefFailure(['for i in xxx'], 'E1001:')
2113  CheckDefFailure(['endfor'], 'E588:')
2114  CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
2115enddef
2116
2117def Test_for_loop_script_var()
2118  # cannot use s:var in a :def function
2119  CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2120
2121  # can use s:var in Vim9 script, with or without s:
2122  var lines =<< trim END
2123    vim9script
2124    var total = 0
2125    for s:var in [1, 2, 3]
2126      total += s:var
2127    endfor
2128    assert_equal(6, total)
2129
2130    total = 0
2131    for var in [1, 2, 3]
2132      total += var
2133    endfor
2134    assert_equal(6, total)
2135  END
2136enddef
2137
2138def Test_for_loop_unpack()
2139  var lines =<< trim END
2140      var result = []
2141      for [v1, v2] in [[1, 2], [3, 4]]
2142        result->add(v1)
2143        result->add(v2)
2144      endfor
2145      assert_equal([1, 2, 3, 4], result)
2146
2147      result = []
2148      for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2149        result->add(v1)
2150        result->add(v2)
2151        result->add(v3)
2152      endfor
2153      assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2154
2155      result = []
2156      for [&ts, &sw] in [[1, 2], [3, 4]]
2157        result->add(&ts)
2158        result->add(&sw)
2159      endfor
2160      assert_equal([1, 2, 3, 4], result)
2161
2162      var slist: list<string>
2163      for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2164        slist->add($LOOPVAR)
2165        slist->add(@r)
2166        slist->add(v:errmsg)
2167      endfor
2168      assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2169
2170      slist = []
2171      for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2172        slist->add(g:globalvar)
2173        slist->add(b:bufvar)
2174        slist->add(w:winvar)
2175        slist->add(t:tabvar)
2176      endfor
2177      assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
2178      unlet! g:globalvar b:bufvar w:winvar t:tabvar
2179  END
2180  CheckDefAndScriptSuccess(lines)
2181
2182  lines =<< trim END
2183      for [v1, v2] in [[1, 2, 3], [3, 4]]
2184        echo v1 v2
2185      endfor
2186  END
2187  CheckDefExecFailure(lines, 'E710:', 1)
2188
2189  lines =<< trim END
2190      for [v1, v2] in [[1], [3, 4]]
2191        echo v1 v2
2192      endfor
2193  END
2194  CheckDefExecFailure(lines, 'E711:', 1)
2195
2196  lines =<< trim END
2197      for [v1, v1] in [[1, 2], [3, 4]]
2198        echo v1
2199      endfor
2200  END
2201  CheckDefExecFailure(lines, 'E1017:', 1)
2202enddef
2203
2204def Test_for_loop_with_try_continue()
2205  var looped = 0
2206  var cleanup = 0
2207  for i in range(3)
2208    looped += 1
2209    try
2210      eval [][0]
2211    catch
2212      continue
2213    finally
2214      cleanup += 1
2215    endtry
2216  endfor
2217  assert_equal(3, looped)
2218  assert_equal(3, cleanup)
2219enddef
2220
2221def Test_while_loop()
2222  var result = ''
2223  var cnt = 0
2224  while cnt < 555
2225    if cnt == 3
2226      break
2227    endif
2228    cnt += 1
2229    if cnt == 2
2230      continue
2231    endif
2232    result ..= cnt .. '_'
2233  endwhile
2234  assert_equal('1_3_', result)
2235
2236  var s = ''
2237  while s == 'x' #{comment}
2238  endwhile
2239enddef
2240
2241def Test_while_loop_fails()
2242  CheckDefFailure(['while xxx'], 'E1001:')
2243  CheckDefFailure(['endwhile'], 'E588:')
2244  CheckDefFailure(['continue'], 'E586:')
2245  CheckDefFailure(['if true', 'continue'], 'E586:')
2246  CheckDefFailure(['break'], 'E587:')
2247  CheckDefFailure(['if true', 'break'], 'E587:')
2248  CheckDefFailure(['while 1', 'echo 3'], 'E170:')
2249
2250  var lines =<< trim END
2251      var s = ''
2252      while s = ''
2253      endwhile
2254  END
2255  CheckDefFailure(lines, 'E488:')
2256enddef
2257
2258def Test_interrupt_loop()
2259  var caught = false
2260  var x = 0
2261  try
2262    while 1
2263      x += 1
2264      if x == 100
2265        feedkeys("\<C-C>", 'Lt')
2266      endif
2267    endwhile
2268  catch
2269    caught = true
2270    assert_equal(100, x)
2271  endtry
2272  assert_true(caught, 'should have caught an exception')
2273  # consume the CTRL-C
2274  getchar(0)
2275enddef
2276
2277def Test_automatic_line_continuation()
2278  var mylist = [
2279      'one',
2280      'two',
2281      'three',
2282      ] # comment
2283  assert_equal(['one', 'two', 'three'], mylist)
2284
2285  var mydict = {
2286      ['one']: 1,
2287      ['two']: 2,
2288      ['three']:
2289          3,
2290      } # comment
2291  assert_equal({one: 1, two: 2, three: 3}, mydict)
2292  mydict = {
2293      one: 1,  # comment
2294      two:     # comment
2295           2,  # comment
2296      three: 3 # comment
2297      }
2298  assert_equal({one: 1, two: 2, three: 3}, mydict)
2299  mydict = {
2300      one: 1,
2301      two:
2302           2,
2303      three: 3
2304      }
2305  assert_equal({one: 1, two: 2, three: 3}, mydict)
2306
2307  assert_equal(
2308        ['one', 'two', 'three'],
2309        split('one two three')
2310        )
2311enddef
2312
2313def Test_vim9_comment()
2314  CheckScriptSuccess([
2315      'vim9script',
2316      '# something',
2317      '#something',
2318      '#{something',
2319      ])
2320
2321  split Xfile
2322  CheckScriptSuccess([
2323      'vim9script',
2324      'edit #something',
2325      ])
2326  CheckScriptSuccess([
2327      'vim9script',
2328      'edit #{something',
2329      ])
2330  close
2331
2332  CheckScriptFailure([
2333      'vim9script',
2334      ':# something',
2335      ], 'E488:')
2336  CheckScriptFailure([
2337      '# something',
2338      ], 'E488:')
2339  CheckScriptFailure([
2340      ':# something',
2341      ], 'E488:')
2342
2343  { # block start
2344  } # block end
2345  CheckDefFailure([
2346      '{# comment',
2347      ], 'E488:')
2348  CheckDefFailure([
2349      '{',
2350      '}# comment',
2351      ], 'E488:')
2352
2353  echo "yes" # comment
2354  CheckDefFailure([
2355      'echo "yes"# comment',
2356      ], 'E488:')
2357  CheckScriptSuccess([
2358      'vim9script',
2359      'echo "yes" # something',
2360      ])
2361  CheckScriptFailure([
2362      'vim9script',
2363      'echo "yes"# something',
2364      ], 'E121:')
2365  CheckScriptFailure([
2366      'vim9script',
2367      'echo# something',
2368      ], 'E1144:')
2369  CheckScriptFailure([
2370      'echo "yes" # something',
2371      ], 'E121:')
2372
2373  exe "echo" # comment
2374  CheckDefFailure([
2375      'exe "echo"# comment',
2376      ], 'E488:')
2377  CheckScriptSuccess([
2378      'vim9script',
2379      'exe "echo" # something',
2380      ])
2381  CheckScriptFailure([
2382      'vim9script',
2383      'exe "echo"# something',
2384      ], 'E121:')
2385  CheckScriptFailure([
2386      'vim9script',
2387      'exe# something',
2388      ], 'E1144:')
2389  CheckScriptFailure([
2390      'exe "echo" # something',
2391      ], 'E121:')
2392
2393  CheckDefFailure([
2394      'try# comment',
2395      '  echo "yes"',
2396      'catch',
2397      'endtry',
2398      ], 'E1144:')
2399  CheckScriptFailure([
2400      'vim9script',
2401      'try# comment',
2402      'echo "yes"',
2403      ], 'E1144:')
2404  CheckDefFailure([
2405      'try',
2406      '  throw#comment',
2407      'catch',
2408      'endtry',
2409      ], 'E1144:')
2410  CheckDefFailure([
2411      'try',
2412      '  throw "yes"#comment',
2413      'catch',
2414      'endtry',
2415      ], 'E488:')
2416  CheckDefFailure([
2417      'try',
2418      '  echo "yes"',
2419      'catch# comment',
2420      'endtry',
2421      ], 'E1144:')
2422  CheckScriptFailure([
2423      'vim9script',
2424      'try',
2425      '  echo "yes"',
2426      'catch# comment',
2427      'endtry',
2428      ], 'E1144:')
2429  CheckDefFailure([
2430      'try',
2431      '  echo "yes"',
2432      'catch /pat/# comment',
2433      'endtry',
2434      ], 'E488:')
2435  CheckDefFailure([
2436      'try',
2437      'echo "yes"',
2438      'catch',
2439      'endtry# comment',
2440      ], 'E1144:')
2441  CheckScriptFailure([
2442      'vim9script',
2443      'try',
2444      '  echo "yes"',
2445      'catch',
2446      'endtry# comment',
2447      ], 'E1144:')
2448
2449  CheckScriptSuccess([
2450      'vim9script',
2451      'hi # comment',
2452      ])
2453  CheckScriptFailure([
2454      'vim9script',
2455      'hi# comment',
2456      ], 'E1144:')
2457  CheckScriptSuccess([
2458      'vim9script',
2459      'hi Search # comment',
2460      ])
2461  CheckScriptFailure([
2462      'vim9script',
2463      'hi Search# comment',
2464      ], 'E416:')
2465  CheckScriptSuccess([
2466      'vim9script',
2467      'hi link This Search # comment',
2468      ])
2469  CheckScriptFailure([
2470      'vim9script',
2471      'hi link This That# comment',
2472      ], 'E413:')
2473  CheckScriptSuccess([
2474      'vim9script',
2475      'hi clear This # comment',
2476      'hi clear # comment',
2477      ])
2478  # not tested, because it doesn't give an error but a warning:
2479  # hi clear This# comment',
2480  CheckScriptFailure([
2481      'vim9script',
2482      'hi clear# comment',
2483      ], 'E416:')
2484
2485  CheckScriptSuccess([
2486      'vim9script',
2487      'hi Group term=bold',
2488      'match Group /todo/ # comment',
2489      ])
2490  CheckScriptFailure([
2491      'vim9script',
2492      'hi Group term=bold',
2493      'match Group /todo/# comment',
2494      ], 'E488:')
2495  CheckScriptSuccess([
2496      'vim9script',
2497      'match # comment',
2498      ])
2499  CheckScriptFailure([
2500      'vim9script',
2501      'match# comment',
2502      ], 'E1144:')
2503  CheckScriptSuccess([
2504      'vim9script',
2505      'match none # comment',
2506      ])
2507  CheckScriptFailure([
2508      'vim9script',
2509      'match none# comment',
2510      ], 'E475:')
2511
2512  CheckScriptSuccess([
2513      'vim9script',
2514      'menutrans clear # comment',
2515      ])
2516  CheckScriptFailure([
2517      'vim9script',
2518      'menutrans clear# comment text',
2519      ], 'E474:')
2520
2521  CheckScriptSuccess([
2522      'vim9script',
2523      'syntax clear # comment',
2524      ])
2525  CheckScriptFailure([
2526      'vim9script',
2527      'syntax clear# comment text',
2528      ], 'E28:')
2529  CheckScriptSuccess([
2530      'vim9script',
2531      'syntax keyword Word some',
2532      'syntax clear Word # comment',
2533      ])
2534  CheckScriptFailure([
2535      'vim9script',
2536      'syntax keyword Word some',
2537      'syntax clear Word# comment text',
2538      ], 'E28:')
2539
2540  CheckScriptSuccess([
2541      'vim9script',
2542      'syntax list # comment',
2543      ])
2544  CheckScriptFailure([
2545      'vim9script',
2546      'syntax list# comment text',
2547      ], 'E28:')
2548
2549  CheckScriptSuccess([
2550      'vim9script',
2551      'syntax match Word /pat/ oneline # comment',
2552      ])
2553  CheckScriptFailure([
2554      'vim9script',
2555      'syntax match Word /pat/ oneline# comment',
2556      ], 'E475:')
2557
2558  CheckScriptSuccess([
2559      'vim9script',
2560      'syntax keyword Word word # comm[ent',
2561      ])
2562  CheckScriptFailure([
2563      'vim9script',
2564      'syntax keyword Word word# comm[ent',
2565      ], 'E789:')
2566
2567  CheckScriptSuccess([
2568      'vim9script',
2569      'syntax match Word /pat/ # comment',
2570      ])
2571  CheckScriptFailure([
2572      'vim9script',
2573      'syntax match Word /pat/# comment',
2574      ], 'E402:')
2575
2576  CheckScriptSuccess([
2577      'vim9script',
2578      'syntax match Word /pat/ contains=Something # comment',
2579      ])
2580  CheckScriptFailure([
2581      'vim9script',
2582      'syntax match Word /pat/ contains=Something# comment',
2583      ], 'E475:')
2584  CheckScriptFailure([
2585      'vim9script',
2586      'syntax match Word /pat/ contains= # comment',
2587      ], 'E406:')
2588  CheckScriptFailure([
2589      'vim9script',
2590      'syntax match Word /pat/ contains=# comment',
2591      ], 'E475:')
2592
2593  CheckScriptSuccess([
2594      'vim9script',
2595      'syntax region Word start=/pat/ end=/pat/ # comment',
2596      ])
2597  CheckScriptFailure([
2598      'vim9script',
2599      'syntax region Word start=/pat/ end=/pat/# comment',
2600      ], 'E402:')
2601
2602  CheckScriptSuccess([
2603      'vim9script',
2604      'syntax sync # comment',
2605      ])
2606  CheckScriptFailure([
2607      'vim9script',
2608      'syntax sync# comment',
2609      ], 'E404:')
2610  CheckScriptSuccess([
2611      'vim9script',
2612      'syntax sync ccomment # comment',
2613      ])
2614  CheckScriptFailure([
2615      'vim9script',
2616      'syntax sync ccomment# comment',
2617      ], 'E404:')
2618
2619  CheckScriptSuccess([
2620      'vim9script',
2621      'syntax cluster Some contains=Word # comment',
2622      ])
2623  CheckScriptFailure([
2624      'vim9script',
2625      'syntax cluster Some contains=Word# comment',
2626      ], 'E475:')
2627
2628  CheckScriptSuccess([
2629      'vim9script',
2630      'command Echo echo # comment',
2631      'command Echo # comment',
2632      'delcommand Echo',
2633      ])
2634  CheckScriptFailure([
2635      'vim9script',
2636      'command Echo echo# comment',
2637      'Echo',
2638      ], 'E1144:')
2639  delcommand Echo
2640
2641  var curdir = getcwd()
2642  CheckScriptSuccess([
2643      'command Echo cd " comment',
2644      'Echo',
2645      'delcommand Echo',
2646      ])
2647  CheckScriptSuccess([
2648      'vim9script',
2649      'command Echo cd # comment',
2650      'Echo',
2651      'delcommand Echo',
2652      ])
2653  CheckScriptFailure([
2654      'vim9script',
2655      'command Echo cd " comment',
2656      'Echo',
2657      ], 'E344:')
2658  delcommand Echo
2659  chdir(curdir)
2660
2661  CheckScriptFailure([
2662      'vim9script',
2663      'command Echo# comment',
2664      ], 'E182:')
2665  CheckScriptFailure([
2666      'vim9script',
2667      'command Echo echo',
2668      'command Echo# comment',
2669      ], 'E182:')
2670  delcommand Echo
2671
2672  CheckScriptSuccess([
2673      'vim9script',
2674      'function # comment',
2675      ])
2676  CheckScriptFailure([
2677      'vim9script',
2678      'function " comment',
2679      ], 'E129:')
2680  CheckScriptFailure([
2681      'vim9script',
2682      'function# comment',
2683      ], 'E1144:')
2684  CheckScriptSuccess([
2685      'vim9script',
2686      'function CheckScriptSuccess # comment',
2687      ])
2688  CheckScriptFailure([
2689      'vim9script',
2690      'function CheckScriptSuccess# comment',
2691      ], 'E488:')
2692
2693  CheckScriptSuccess([
2694      'vim9script',
2695      'func g:DeleteMeA()',
2696      'endfunc',
2697      'delfunction g:DeleteMeA # comment',
2698      ])
2699  CheckScriptFailure([
2700      'vim9script',
2701      'func g:DeleteMeB()',
2702      'endfunc',
2703      'delfunction g:DeleteMeB# comment',
2704      ], 'E488:')
2705
2706  CheckScriptSuccess([
2707      'vim9script',
2708      'call execute("ls") # comment',
2709      ])
2710  CheckScriptFailure([
2711      'vim9script',
2712      'call execute("ls")# comment',
2713      ], 'E488:')
2714
2715  CheckScriptFailure([
2716      'def Test() " comment',
2717      'enddef',
2718      ], 'E488:')
2719  CheckScriptFailure([
2720      'vim9script',
2721      'def Test() " comment',
2722      'enddef',
2723      ], 'E488:')
2724
2725  CheckScriptSuccess([
2726      'func Test() " comment',
2727      'endfunc',
2728      'delfunc Test',
2729      ])
2730  CheckScriptSuccess([
2731      'vim9script',
2732      'func Test() " comment',
2733      'endfunc',
2734      ])
2735
2736  CheckScriptSuccess([
2737      'def Test() # comment',
2738      'enddef',
2739      ])
2740  CheckScriptFailure([
2741      'func Test() # comment',
2742      'endfunc',
2743      ], 'E488:')
2744enddef
2745
2746def Test_vim9_comment_gui()
2747  CheckCanRunGui
2748
2749  CheckScriptFailure([
2750      'vim9script',
2751      'gui#comment'
2752      ], 'E1144:')
2753  CheckScriptFailure([
2754      'vim9script',
2755      'gui -f#comment'
2756      ], 'E499:')
2757enddef
2758
2759def Test_vim9_comment_not_compiled()
2760  au TabEnter *.vim g:entered = 1
2761  au TabEnter *.x g:entered = 2
2762
2763  edit test.vim
2764  doautocmd TabEnter #comment
2765  assert_equal(1, g:entered)
2766
2767  doautocmd TabEnter f.x
2768  assert_equal(2, g:entered)
2769
2770  g:entered = 0
2771  doautocmd TabEnter f.x #comment
2772  assert_equal(2, g:entered)
2773
2774  assert_fails('doautocmd Syntax#comment', 'E216:')
2775
2776  au! TabEnter
2777  unlet g:entered
2778
2779  CheckScriptSuccess([
2780      'vim9script',
2781      'g:var = 123',
2782      'b:var = 456',
2783      'w:var = 777',
2784      't:var = 888',
2785      'unlet g:var w:var # something',
2786      ])
2787
2788  CheckScriptFailure([
2789      'vim9script',
2790      'let var = 123',
2791      ], 'E1126: Cannot use :let in Vim9 script')
2792
2793  CheckScriptFailure([
2794      'vim9script',
2795      'var g:var = 123',
2796      ], 'E1016: Cannot declare a global variable:')
2797
2798  CheckScriptFailure([
2799      'vim9script',
2800      'var b:var = 123',
2801      ], 'E1016: Cannot declare a buffer variable:')
2802
2803  CheckScriptFailure([
2804      'vim9script',
2805      'var w:var = 123',
2806      ], 'E1016: Cannot declare a window variable:')
2807
2808  CheckScriptFailure([
2809      'vim9script',
2810      'var t:var = 123',
2811      ], 'E1016: Cannot declare a tab variable:')
2812
2813  CheckScriptFailure([
2814      'vim9script',
2815      'var v:version = 123',
2816      ], 'E1016: Cannot declare a v: variable:')
2817
2818  CheckScriptFailure([
2819      'vim9script',
2820      'var $VARIABLE = "text"',
2821      ], 'E1016: Cannot declare an environment variable:')
2822
2823  CheckScriptFailure([
2824      'vim9script',
2825      'g:var = 123',
2826      'unlet g:var# comment1',
2827      ], 'E108:')
2828
2829  CheckScriptFailure([
2830      'let g:var = 123',
2831      'unlet g:var # something',
2832      ], 'E488:')
2833
2834  CheckScriptSuccess([
2835      'vim9script',
2836      'if 1 # comment2',
2837      '  echo "yes"',
2838      'elseif 2 #comment',
2839      '  echo "no"',
2840      'endif',
2841      ])
2842
2843  CheckScriptFailure([
2844      'vim9script',
2845      'if 1# comment3',
2846      '  echo "yes"',
2847      'endif',
2848      ], 'E15:')
2849
2850  CheckScriptFailure([
2851      'vim9script',
2852      'if 0 # comment4',
2853      '  echo "yes"',
2854      'elseif 2#comment',
2855      '  echo "no"',
2856      'endif',
2857      ], 'E15:')
2858
2859  CheckScriptSuccess([
2860      'vim9script',
2861      'var v = 1 # comment5',
2862      ])
2863
2864  CheckScriptFailure([
2865      'vim9script',
2866      'var v = 1# comment6',
2867      ], 'E15:')
2868
2869  CheckScriptSuccess([
2870      'vim9script',
2871      'new'
2872      'setline(1, ["# define pat", "last"])',
2873      ':$',
2874      'dsearch /pat/ #comment',
2875      'bwipe!',
2876      ])
2877
2878  CheckScriptFailure([
2879      'vim9script',
2880      'new'
2881      'setline(1, ["# define pat", "last"])',
2882      ':$',
2883      'dsearch /pat/#comment',
2884      'bwipe!',
2885      ], 'E488:')
2886
2887  CheckScriptFailure([
2888      'vim9script',
2889      'func! SomeFunc()',
2890      ], 'E477:')
2891enddef
2892
2893def Test_finish()
2894  var lines =<< trim END
2895    vim9script
2896    g:res = 'one'
2897    if v:false | finish | endif
2898    g:res = 'two'
2899    finish
2900    g:res = 'three'
2901  END
2902  writefile(lines, 'Xfinished')
2903  source Xfinished
2904  assert_equal('two', g:res)
2905
2906  unlet g:res
2907  delete('Xfinished')
2908enddef
2909
2910def Test_forward_declaration()
2911  var lines =<< trim END
2912    vim9script
2913    def GetValue(): string
2914      return theVal
2915    enddef
2916    var theVal = 'something'
2917    g:initVal = GetValue()
2918    theVal = 'else'
2919    g:laterVal = GetValue()
2920  END
2921  writefile(lines, 'Xforward')
2922  source Xforward
2923  assert_equal('something', g:initVal)
2924  assert_equal('else', g:laterVal)
2925
2926  unlet g:initVal
2927  unlet g:laterVal
2928  delete('Xforward')
2929enddef
2930
2931def Test_source_vim9_from_legacy()
2932  var vim9_lines =<< trim END
2933    vim9script
2934    var local = 'local'
2935    g:global = 'global'
2936    export var exported = 'exported'
2937    export def GetText(): string
2938       return 'text'
2939    enddef
2940  END
2941  writefile(vim9_lines, 'Xvim9_script.vim')
2942
2943  var legacy_lines =<< trim END
2944    source Xvim9_script.vim
2945
2946    call assert_false(exists('local'))
2947    call assert_false(exists('exported'))
2948    call assert_false(exists('s:exported'))
2949    call assert_equal('global', global)
2950    call assert_equal('global', g:global)
2951
2952    " imported variable becomes script-local
2953    import exported from './Xvim9_script.vim'
2954    call assert_equal('exported', s:exported)
2955    call assert_false(exists('exported'))
2956
2957    " imported function becomes script-local
2958    import GetText from './Xvim9_script.vim'
2959    call assert_equal('text', s:GetText())
2960    call assert_false(exists('*GetText'))
2961  END
2962  writefile(legacy_lines, 'Xlegacy_script.vim')
2963
2964  source Xlegacy_script.vim
2965  assert_equal('global', g:global)
2966  unlet g:global
2967
2968  delete('Xlegacy_script.vim')
2969  delete('Xvim9_script.vim')
2970enddef
2971
2972func Test_vim9script_not_global()
2973  " check that items defined in Vim9 script are script-local, not global
2974  let vim9lines =<< trim END
2975    vim9script
2976    var name = 'local'
2977    func TheFunc()
2978      echo 'local'
2979    endfunc
2980    def DefFunc()
2981      echo 'local'
2982    enddef
2983  END
2984  call writefile(vim9lines, 'Xvim9script.vim')
2985  source Xvim9script.vim
2986  try
2987    echo g:var
2988    assert_report('did not fail')
2989  catch /E121:/
2990    " caught
2991  endtry
2992  try
2993    call TheFunc()
2994    assert_report('did not fail')
2995  catch /E117:/
2996    " caught
2997  endtry
2998  try
2999    call DefFunc()
3000    assert_report('did not fail')
3001  catch /E117:/
3002    " caught
3003  endtry
3004
3005  call delete('Xvim9script.vim')
3006endfunc
3007
3008def Test_vim9_copen()
3009  # this was giving an error for setting w:quickfix_title
3010  copen
3011  quit
3012enddef
3013
3014" test using an auto-loaded function and variable
3015def Test_vim9_autoload()
3016  var lines =<< trim END
3017     vim9script
3018     def some#gettest(): string
3019       return 'test'
3020     enddef
3021     g:some#name = 'name'
3022  END
3023
3024  mkdir('Xdir/autoload', 'p')
3025  writefile(lines, 'Xdir/autoload/some.vim')
3026  var save_rtp = &rtp
3027  exe 'set rtp^=' .. getcwd() .. '/Xdir'
3028
3029  assert_equal('test', g:some#gettest())
3030  assert_equal('name', g:some#name)
3031  g:some#other = 'other'
3032  assert_equal('other', g:some#other)
3033
3034  # upper case script name works
3035  lines =<< trim END
3036     vim9script
3037     def Other#getOther(): string
3038       return 'other'
3039     enddef
3040  END
3041  writefile(lines, 'Xdir/autoload/Other.vim')
3042  assert_equal('other', g:Other#getOther())
3043
3044  delete('Xdir', 'rf')
3045  &rtp = save_rtp
3046enddef
3047
3048" test using a vim9script that is auto-loaded from an autocmd
3049def Test_vim9_aucmd_autoload()
3050  var lines =<< trim END
3051     vim9script
3052     def foo#test()
3053         echomsg getreg('"')
3054     enddef
3055  END
3056
3057  mkdir('Xdir/autoload', 'p')
3058  writefile(lines, 'Xdir/autoload/foo.vim')
3059  var save_rtp = &rtp
3060  exe 'set rtp^=' .. getcwd() .. '/Xdir'
3061  augroup test
3062    autocmd TextYankPost * call foo#test()
3063  augroup END
3064
3065  normal Y
3066
3067  augroup test
3068    autocmd!
3069  augroup END
3070  delete('Xdir', 'rf')
3071  &rtp = save_rtp
3072enddef
3073
3074" This was causing a crash because suppress_errthrow wasn't reset.
3075def Test_vim9_autoload_error()
3076  var lines =<< trim END
3077      vim9script
3078      def crash#func()
3079          try
3080              for x in List()
3081              endfor
3082          catch
3083          endtry
3084          g:ok = true
3085      enddef
3086      fu List()
3087          invalid
3088      endfu
3089      try
3090          alsoinvalid
3091      catch /wontmatch/
3092      endtry
3093  END
3094  call mkdir('Xruntime/autoload', 'p')
3095  call writefile(lines, 'Xruntime/autoload/crash.vim')
3096
3097  # run in a separate Vim to avoid the side effects of assert_fails()
3098  lines =<< trim END
3099    exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3100    call crash#func()
3101    call writefile(['ok'], 'Xdidit')
3102    qall!
3103  END
3104  writefile(lines, 'Xscript')
3105  RunVim([], [], '-S Xscript')
3106  assert_equal(['ok'], readfile('Xdidit'))
3107
3108  delete('Xdidit')
3109  delete('Xscript')
3110  delete('Xruntime', 'rf')
3111
3112  lines =<< trim END
3113    vim9script
3114    var foo#bar = 'asdf'
3115  END
3116  CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
3117enddef
3118
3119def Test_script_var_in_autocmd()
3120  # using a script variable from an autocommand, defined in a :def function in a
3121  # legacy Vim script, cannot check the variable type.
3122  var lines =<< trim END
3123    let s:counter = 1
3124    def s:Func()
3125      au! CursorHold
3126      au CursorHold * s:counter += 1
3127    enddef
3128    call s:Func()
3129    doau CursorHold
3130    call assert_equal(2, s:counter)
3131    au! CursorHold
3132  END
3133  CheckScriptSuccess(lines)
3134enddef
3135
3136def Test_cmdline_win()
3137  # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3138  # the command line window.
3139  mkdir('rtp/syntax', 'p')
3140  var export_lines =<< trim END
3141    vim9script
3142    export var That = 'yes'
3143  END
3144  writefile(export_lines, 'rtp/syntax/Xexport.vim')
3145  var import_lines =<< trim END
3146    vim9script
3147    import That from './Xexport.vim'
3148  END
3149  writefile(import_lines, 'rtp/syntax/vim.vim')
3150  var save_rtp = &rtp
3151  &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3152  syntax on
3153  augroup CmdWin
3154    autocmd CmdwinEnter * g:got_there = 'yes'
3155  augroup END
3156  # this will open and also close the cmdline window
3157  feedkeys('q:', 'xt')
3158  assert_equal('yes', g:got_there)
3159
3160  augroup CmdWin
3161    au!
3162  augroup END
3163  &rtp = save_rtp
3164  delete('rtp', 'rf')
3165enddef
3166
3167def Test_invalid_sid()
3168  assert_fails('func <SNR>1234_func', 'E123:')
3169
3170  if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
3171    assert_equal([], readfile('Xdidit'))
3172  endif
3173  delete('Xdidit')
3174enddef
3175
3176def Test_restoring_cpo()
3177  writefile(['vim9script', 'set nocp'], 'Xsourced')
3178  writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3179  if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3180    assert_equal(['done'], readfile('Xdone'))
3181  endif
3182  delete('Xsourced')
3183  delete('Xclose')
3184  delete('Xdone')
3185
3186  writefile(['vim9script'], 'XanotherScript')
3187  set cpo=aABceFsMny>
3188  edit XanotherScript
3189  so %
3190  assert_equal('aABceFsMny>', &cpo)
3191  :1del
3192  w
3193  so %
3194  assert_equal('aABceFsMny>', &cpo)
3195
3196  delete('XanotherScript')
3197  set cpo&vim
3198enddef
3199
3200
3201def Test_unset_any_variable()
3202  var lines =<< trim END
3203    var name: any
3204    assert_equal(0, name)
3205  END
3206  CheckDefAndScriptSuccess(lines)
3207enddef
3208
3209func Test_define_func_at_command_line()
3210  CheckRunVimInTerminal
3211
3212  " call indirectly to avoid compilation error for missing functions
3213  call Run_Test_define_func_at_command_line()
3214endfunc
3215
3216def Run_Test_define_func_at_command_line()
3217  # run in a separate Vim instance to avoid the script context
3218  var lines =<< trim END
3219    func CheckAndQuit()
3220      call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3221      call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3222    endfunc
3223  END
3224  writefile([''], 'Xdidcmd')
3225  writefile(lines, 'XcallFunc')
3226  var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
3227  # define Afunc() on the command line
3228  term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3229  term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
3230  WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
3231
3232  call StopVimInTerminal(buf)
3233  delete('XcallFunc')
3234  delete('Xdidcmd')
3235enddef
3236
3237def Test_script_var_scope()
3238  var lines =<< trim END
3239      vim9script
3240      if true
3241        if true
3242          var one = 'one'
3243          echo one
3244        endif
3245        echo one
3246      endif
3247  END
3248  CheckScriptFailure(lines, 'E121:', 7)
3249
3250  lines =<< trim END
3251      vim9script
3252      if true
3253        if false
3254          var one = 'one'
3255          echo one
3256        else
3257          var one = 'one'
3258          echo one
3259        endif
3260        echo one
3261      endif
3262  END
3263  CheckScriptFailure(lines, 'E121:', 10)
3264
3265  lines =<< trim END
3266      vim9script
3267      while true
3268        var one = 'one'
3269        echo one
3270        break
3271      endwhile
3272      echo one
3273  END
3274  CheckScriptFailure(lines, 'E121:', 7)
3275
3276  lines =<< trim END
3277      vim9script
3278      for i in range(1)
3279        var one = 'one'
3280        echo one
3281      endfor
3282      echo one
3283  END
3284  CheckScriptFailure(lines, 'E121:', 6)
3285
3286  lines =<< trim END
3287      vim9script
3288      {
3289        var one = 'one'
3290        assert_equal('one', one)
3291      }
3292      assert_false(exists('one'))
3293      assert_false(exists('s:one'))
3294  END
3295  CheckScriptSuccess(lines)
3296
3297  lines =<< trim END
3298      vim9script
3299      {
3300        var one = 'one'
3301        echo one
3302      }
3303      echo one
3304  END
3305  CheckScriptFailure(lines, 'E121:', 6)
3306enddef
3307
3308def Test_catch_exception_in_callback()
3309  var lines =<< trim END
3310    vim9script
3311    def Callback(...l: any)
3312      try
3313        var x: string
3314        var y: string
3315        # this error should be caught with CHECKLEN
3316        [x, y] = ['']
3317      catch
3318        g:caught = 'yes'
3319      endtry
3320    enddef
3321    popup_menu('popup', {callback: Callback})
3322    feedkeys("\r", 'xt')
3323  END
3324  CheckScriptSuccess(lines)
3325
3326  unlet g:caught
3327enddef
3328
3329def Test_no_unknown_error_after_error()
3330  if !has('unix') || !has('job')
3331    throw 'Skipped: not unix of missing +job feature'
3332  endif
3333  var lines =<< trim END
3334      vim9script
3335      var source: list<number>
3336      def Out_cb(...l: any)
3337          eval [][0]
3338      enddef
3339      def Exit_cb(...l: any)
3340          sleep 1m
3341          source += l
3342      enddef
3343      var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
3344      while job_status(myjob) == 'run'
3345        sleep 10m
3346      endwhile
3347      # wait for Exit_cb() to be called
3348      sleep 100m
3349  END
3350  writefile(lines, 'Xdef')
3351  assert_fails('so Xdef', ['E684:', 'E1012:'])
3352  delete('Xdef')
3353enddef
3354
3355def InvokeNormal()
3356  exe "norm! :m+1\r"
3357enddef
3358
3359def Test_invoke_normal_in_visual_mode()
3360  xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3361  new
3362  setline(1, ['aaa', 'bbb'])
3363  feedkeys("V\<F3>", 'xt')
3364  assert_equal(['bbb', 'aaa'], getline(1, 2))
3365  xunmap <F3>
3366enddef
3367
3368def Test_white_space_after_command()
3369  var lines =<< trim END
3370    exit_cb: Func})
3371  END
3372  CheckDefAndScriptFailure(lines, 'E1144:', 1)
3373
3374  lines =<< trim END
3375    e#
3376  END
3377  CheckDefAndScriptFailure(lines, 'E1144:', 1)
3378enddef
3379
3380def Test_script_var_gone_when_sourced_twice()
3381  var lines =<< trim END
3382      vim9script
3383      if exists('g:guard')
3384        finish
3385      endif
3386      g:guard = 1
3387      var name = 'thename'
3388      def g:GetName(): string
3389        return name
3390      enddef
3391      def g:SetName(arg: string)
3392        name = arg
3393      enddef
3394  END
3395  writefile(lines, 'XscriptTwice.vim')
3396  so XscriptTwice.vim
3397  assert_equal('thename', g:GetName())
3398  g:SetName('newname')
3399  assert_equal('newname', g:GetName())
3400  so XscriptTwice.vim
3401  assert_fails('call g:GetName()', 'E1149:')
3402  assert_fails('call g:SetName("x")', 'E1149:')
3403
3404  delfunc g:GetName
3405  delfunc g:SetName
3406  delete('XscriptTwice.vim')
3407  unlet g:guard
3408enddef
3409
3410def Test_import_gone_when_sourced_twice()
3411  var exportlines =<< trim END
3412      vim9script
3413      if exists('g:guard')
3414        finish
3415      endif
3416      g:guard = 1
3417      export var name = 'someName'
3418  END
3419  writefile(exportlines, 'XexportScript.vim')
3420
3421  var lines =<< trim END
3422      vim9script
3423      import name from './XexportScript.vim'
3424      def g:GetName(): string
3425        return name
3426      enddef
3427  END
3428  writefile(lines, 'XscriptImport.vim')
3429  so XscriptImport.vim
3430  assert_equal('someName', g:GetName())
3431
3432  so XexportScript.vim
3433  assert_fails('call g:GetName()', 'E1149:')
3434
3435  delfunc g:GetName
3436  delete('XexportScript.vim')
3437  delete('XscriptImport.vim')
3438  unlet g:guard
3439enddef
3440
3441" Keep this last, it messes up highlighting.
3442def Test_substitute_cmd()
3443  new
3444  setline(1, 'something')
3445  :substitute(some(other(
3446  assert_equal('otherthing', getline(1))
3447  bwipe!
3448
3449  # also when the context is Vim9 script
3450  var lines =<< trim END
3451    vim9script
3452    new
3453    setline(1, 'something')
3454    :substitute(some(other(
3455    assert_equal('otherthing', getline(1))
3456    bwipe!
3457  END
3458  writefile(lines, 'Xvim9lines')
3459  source Xvim9lines
3460
3461  delete('Xvim9lines')
3462enddef
3463
3464" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
3465