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