1" Test various aspects of the Vim9 script language.
2
3source check.vim
4source term_util.vim
5source view_util.vim
6source vim9.vim
7
8def Test_syntax()
9  let var = 234
10  let other: list<string> = ['asdf']
11enddef
12
13let s:appendToMe = 'xxx'
14let s:addToMe = 111
15let g:existing = 'yes'
16let g:inc_counter = 1
17let $SOME_ENV_VAR = 'some'
18let g:alist = [7]
19let g:astring = 'text'
20
21def Test_assignment()
22  let bool1: bool = true
23  assert_equal(v:true, bool1)
24  let bool2: bool = false
25  assert_equal(v:false, bool2)
26
27  call CheckDefFailure(['let x:string'], 'E1069:')
28  call CheckDefFailure(['let x:string = "x"'], 'E1069:')
29  call CheckDefFailure(['let a:string = "x"'], 'E1069:')
30
31  let a: number = 6
32  assert_equal(6, a)
33
34  if has('channel')
35    let chan1: channel
36    let job1: job
37    let job2: job = job_start('willfail')
38  endif
39  if has('float')
40    let float1: float = 3.4
41  endif
42  let Funky1: func
43  let Funky2: func = function('len')
44  let Party2: func = funcref('g:Test_syntax')
45
46  g:newvar = 'new'
47  assert_equal('new', g:newvar)
48
49  assert_equal('yes', g:existing)
50  g:existing = 'no'
51  assert_equal('no', g:existing)
52
53  v:char = 'abc'
54  assert_equal('abc', v:char)
55
56  $ENVVAR = 'foobar'
57  assert_equal('foobar', $ENVVAR)
58  $ENVVAR = ''
59
60  s:appendToMe ..= 'yyy'
61  assert_equal('xxxyyy', s:appendToMe)
62  s:addToMe += 222
63  assert_equal(333, s:addToMe)
64  s:newVar = 'new'
65  assert_equal('new', s:newVar)
66
67  set ts=7
68  &ts += 1
69  assert_equal(8, &ts)
70  &ts -= 3
71  assert_equal(5, &ts)
72  &ts *= 2
73  assert_equal(10, &ts)
74  &ts /= 3
75  assert_equal(3, &ts)
76  set ts=10
77  &ts %= 4
78  assert_equal(2, &ts)
79  call CheckDefFailure(['&notex += 3'], 'E113:')
80  call CheckDefFailure(['&ts ..= "xxx"'], 'E1019:')
81  call CheckDefFailure(['&ts = [7]'], 'E1013:')
82  call CheckDefExecFailure(['&ts = g:alist'], 'E1029: Expected number but got list')
83  call CheckDefFailure(['&ts = "xx"'], 'E1013:')
84  call CheckDefExecFailure(['&ts = g:astring'], 'E1029: Expected number but got string')
85  call CheckDefFailure(['&path += 3'], 'E1013:')
86  call CheckDefExecFailure(['&bs = "asdf"'], 'E474:')
87  # test freeing ISN_STOREOPT
88  call CheckDefFailure(['&ts = 3', 'let asdf'], 'E1022:')
89  &ts = 8
90
91  g:inc_counter += 1
92  assert_equal(2, g:inc_counter)
93
94  $SOME_ENV_VAR ..= 'more'
95  assert_equal('somemore', $SOME_ENV_VAR)
96  call CheckDefFailure(['$SOME_ENV_VAR += "more"'], 'E1013:')
97  call CheckDefFailure(['$SOME_ENV_VAR += 123'], 'E1013:')
98
99  @a = 'areg'
100  @a ..= 'add'
101  assert_equal('aregadd', @a)
102  call CheckDefFailure(['@a += "more"'], 'E1013:')
103  call CheckDefFailure(['@a += 123'], 'E1013:')
104
105  v:errmsg = 'none'
106  v:errmsg ..= 'again'
107  assert_equal('noneagain', v:errmsg)
108  call CheckDefFailure(['v:errmsg += "more"'], 'E1013:')
109  call CheckDefFailure(['v:errmsg += 123'], 'E1013:')
110enddef
111
112def Test_vim9_single_char_vars()
113  let lines =<< trim END
114      vim9script
115
116      " single character variable declarations work
117      let a: string
118      let b: number
119      let l: list<any>
120      let s: string
121      let t: number
122      let v: number
123      let w: number
124
125      " script-local variables can be used without s: prefix
126      a = 'script-a'
127      b = 111
128      l = [1, 2, 3]
129      s = 'script-s'
130      t = 222
131      v = 333
132      w = 444
133
134      assert_equal('script-a', a)
135      assert_equal(111, b)
136      assert_equal([1, 2, 3], l)
137      assert_equal('script-s', s)
138      assert_equal(222, t)
139      assert_equal(333, v)
140      assert_equal(444, w)
141  END
142  writefile(lines, 'Xsinglechar')
143  source Xsinglechar
144  delete('Xsinglechar')
145enddef
146
147def Test_assignment_list()
148  let list1: list<bool> = [false, true, false]
149  let list2: list<number> = [1, 2, 3]
150  let list3: list<string> = ['sdf', 'asdf']
151  let list4: list<any> = ['yes', true, 1234]
152  let list5: list<blob> = [0z01, 0z02]
153
154  let listS: list<string> = []
155  let listN: list<number> = []
156
157  assert_equal([1, 2, 3], list2)
158  list2[-1] = 99
159  assert_equal([1, 2, 99], list2)
160  list2[-2] = 88
161  assert_equal([1, 88, 99], list2)
162  list2[-3] = 77
163  assert_equal([77, 88, 99], list2)
164  call CheckDefExecFailure(['let ll = [1, 2, 3]', 'll[-4] = 6'], 'E684:')
165  call CheckDefExecFailure(['let [v1, v2] = [1, 2]'], 'E1092:')
166
167  # type becomes list<any>
168  let somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c']
169enddef
170
171def Test_assignment_dict()
172  let dict1: dict<bool> = #{one: false, two: true}
173  let dict2: dict<number> = #{one: 1, two: 2}
174  let dict3: dict<string> = #{key: 'value'}
175  let dict4: dict<any> = #{one: 1, two: '2'}
176  let dict5: dict<blob> = #{one: 0z01, two: 0z02}
177
178  " overwrite
179  dict3['key'] = 'another'
180
181  call CheckDefExecFailure(['let dd = {}', 'dd[""] = 6'], 'E713:')
182
183  # type becomes dict<any>
184  let somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'}
185enddef
186
187def Test_assignment_local()
188  " Test in a separated file in order not to the current buffer/window/tab is
189  " changed.
190  let script_lines: list<string> =<< trim END
191    let b:existing = 'yes'
192    let w:existing = 'yes'
193    let t:existing = 'yes'
194
195    def Test_assignment_local_internal()
196      b:newvar = 'new'
197      assert_equal('new', b:newvar)
198      assert_equal('yes', b:existing)
199      b:existing = 'no'
200      assert_equal('no', b:existing)
201      b:existing ..= 'NO'
202      assert_equal('noNO', b:existing)
203
204      w:newvar = 'new'
205      assert_equal('new', w:newvar)
206      assert_equal('yes', w:existing)
207      w:existing = 'no'
208      assert_equal('no', w:existing)
209      w:existing ..= 'NO'
210      assert_equal('noNO', w:existing)
211
212      t:newvar = 'new'
213      assert_equal('new', t:newvar)
214      assert_equal('yes', t:existing)
215      t:existing = 'no'
216      assert_equal('no', t:existing)
217      t:existing ..= 'NO'
218      assert_equal('noNO', t:existing)
219    enddef
220    call Test_assignment_local_internal()
221  END
222  call CheckScriptSuccess(script_lines)
223enddef
224
225def Test_assignment_default()
226
227  # Test default values.
228  let thebool: bool
229  assert_equal(v:false, thebool)
230
231  let thenumber: number
232  assert_equal(0, thenumber)
233
234  if has('float')
235    let thefloat: float
236    assert_equal(0.0, thefloat)
237  endif
238
239  let thestring: string
240  assert_equal('', thestring)
241
242  let theblob: blob
243  assert_equal(0z, theblob)
244
245  let Thefunc: func
246  assert_equal(test_null_function(), Thefunc)
247
248  let thelist: list<any>
249  assert_equal([], thelist)
250
251  let thedict: dict<any>
252  assert_equal({}, thedict)
253
254  if has('channel')
255    let thejob: job
256    assert_equal(test_null_job(), thejob)
257
258    let thechannel: channel
259    assert_equal(test_null_channel(), thechannel)
260
261    if has('unix') && executable('cat')
262      " check with non-null job and channel, types must match
263      thejob = job_start("cat ", #{})
264      thechannel = job_getchannel(thejob)
265      job_stop(thejob, 'kill')
266    endif
267  endif
268
269  let nr = 1234 | nr = 5678
270  assert_equal(5678, nr)
271enddef
272
273def Test_assignment_var_list()
274  let v1: string
275  let v2: string
276  let vrem: list<string>
277  [v1] = ['aaa']
278  assert_equal('aaa', v1)
279
280  [v1, v2] = ['one', 'two']
281  assert_equal('one', v1)
282  assert_equal('two', v2)
283
284  [v1, v2; vrem] = ['one', 'two']
285  assert_equal('one', v1)
286  assert_equal('two', v2)
287  assert_equal([], vrem)
288
289  [v1, v2; vrem] = ['one', 'two', 'three']
290  assert_equal('one', v1)
291  assert_equal('two', v2)
292  assert_equal(['three'], vrem)
293enddef
294
295def Mess(): string
296  v:foldstart = 123
297  return 'xxx'
298enddef
299
300def Test_assignment_failure()
301  call CheckDefFailure(['let var=234'], 'E1004:')
302  call CheckDefFailure(['let var =234'], 'E1004:')
303  call CheckDefFailure(['let var= 234'], 'E1004:')
304
305  call CheckDefFailure(['let true = 1'], 'E1034:')
306  call CheckDefFailure(['let false = 1'], 'E1034:')
307
308  call CheckDefFailure(['[a; b; c] = g:list'], 'E452:')
309  call CheckDefExecFailure(['let a: number',
310                            '[a] = test_null_list()'], 'E1093:')
311  call CheckDefExecFailure(['let a: number',
312                            '[a] = []'], 'E1093:')
313  call CheckDefExecFailure(['let x: number',
314                            'let y: number',
315                            '[x, y] = [1]'], 'E1093:')
316  call CheckDefExecFailure(['let x: number',
317                            'let y: number',
318                            'let z: list<number>',
319                            '[x, y; z] = [1]'], 'E1093:')
320
321  call CheckDefFailure(['let somevar'], "E1022:")
322  call CheckDefFailure(['let &option'], 'E1052:')
323  call CheckDefFailure(['&g:option = 5'], 'E113:')
324
325  call CheckDefFailure(['let $VAR = 5'], 'E1065:')
326
327  call CheckDefFailure(['let @~ = 5'], 'E354:')
328  call CheckDefFailure(['let @a = 5'], 'E1066:')
329
330  call CheckDefFailure(['let g:var = 5'], 'E1016:')
331  call CheckDefFailure(['let w:var = 5'], 'E1079:')
332  call CheckDefFailure(['let b:var = 5'], 'E1078:')
333  call CheckDefFailure(['let t:var = 5'], 'E1080:')
334
335  call CheckDefFailure(['let anr = 4', 'anr ..= "text"'], 'E1019:')
336  call CheckDefFailure(['let xnr += 4'], 'E1020:')
337
338  call CheckScriptFailure(['vim9script', 'def Func()', 'let dummy = s:notfound', 'enddef', 'defcompile'], 'E1050:')
339
340  call CheckDefFailure(['let var: list<string> = [123]'], 'expected list<string> but got list<number>')
341  call CheckDefFailure(['let var: list<number> = ["xx"]'], 'expected list<number> but got list<string>')
342
343  call CheckDefFailure(['let var: dict<string> = #{key: 123}'], 'expected dict<string> but got dict<number>')
344  call CheckDefFailure(['let var: dict<number> = #{key: "xx"}'], 'expected dict<number> but got dict<string>')
345
346  call CheckDefFailure(['let var = feedkeys("0")'], 'E1031:')
347  call CheckDefFailure(['let var: number = feedkeys("0")'], 'expected number but got void')
348
349  call CheckDefFailure(['let var: dict <number>'], 'E1068:')
350  call CheckDefFailure(['let var: dict<number'], 'E1009:')
351
352  call assert_fails('s/^/\=Mess()/n', 'E794:')
353  call CheckDefFailure(['let var: dict<number'], 'E1009:')
354enddef
355
356def Test_unlet()
357  g:somevar = 'yes'
358  assert_true(exists('g:somevar'))
359  unlet g:somevar
360  assert_false(exists('g:somevar'))
361  unlet! g:somevar
362
363  call CheckScriptFailure([
364        'vim9script',
365        'let svar = 123',
366        'unlet svar',
367        ], 'E1081:')
368  call CheckScriptFailure([
369        'vim9script',
370        'let svar = 123',
371        'unlet s:svar',
372        ], 'E1081:')
373  call CheckScriptFailure([
374        'vim9script',
375        'let svar = 123',
376        'def Func()',
377        '  unlet svar',
378        'enddef',
379        'defcompile',
380        ], 'E1081:')
381  call CheckScriptFailure([
382        'vim9script',
383        'let svar = 123',
384        'def Func()',
385        '  unlet s:svar',
386        'enddef',
387        'defcompile',
388        ], 'E1081:')
389
390  $ENVVAR = 'foobar'
391  assert_equal('foobar', $ENVVAR)
392  unlet $ENVVAR
393  assert_equal('', $ENVVAR)
394enddef
395
396def Test_delfunction()
397  " Check function is defined in script namespace
398  CheckScriptSuccess([
399      'vim9script',
400      'func CheckMe()',
401      '  return 123',
402      'endfunc',
403      'assert_equal(123, s:CheckMe())',
404      ])
405
406  " Check function in script namespace cannot be deleted
407  CheckScriptFailure([
408      'vim9script',
409      'func DeleteMe1()',
410      'endfunc',
411      'delfunction DeleteMe1',
412      ], 'E1084:')
413  CheckScriptFailure([
414      'vim9script',
415      'func DeleteMe2()',
416      'endfunc',
417      'def DoThat()',
418      '  delfunction DeleteMe2',
419      'enddef',
420      'DoThat()',
421      ], 'E1084:')
422  CheckScriptFailure([
423      'vim9script',
424      'def DeleteMe3()',
425      'enddef',
426      'delfunction DeleteMe3',
427      ], 'E1084:')
428  CheckScriptFailure([
429      'vim9script',
430      'def DeleteMe4()',
431      'enddef',
432      'def DoThat()',
433      '  delfunction DeleteMe4',
434      'enddef',
435      'DoThat()',
436      ], 'E1084:')
437enddef
438
439func Test_wrong_type()
440  call CheckDefFailure(['let var: list<nothing>'], 'E1010:')
441  call CheckDefFailure(['let var: list<list<nothing>>'], 'E1010:')
442  call CheckDefFailure(['let var: dict<nothing>'], 'E1010:')
443  call CheckDefFailure(['let var: dict<dict<nothing>>'], 'E1010:')
444
445  call CheckDefFailure(['let var: dict<number'], 'E1009:')
446  call CheckDefFailure(['let var: dict<list<number>'], 'E1009:')
447
448  call CheckDefFailure(['let var: ally'], 'E1010:')
449  call CheckDefFailure(['let var: bram'], 'E1010:')
450  call CheckDefFailure(['let var: cathy'], 'E1010:')
451  call CheckDefFailure(['let var: dom'], 'E1010:')
452  call CheckDefFailure(['let var: freddy'], 'E1010:')
453  call CheckDefFailure(['let var: john'], 'E1010:')
454  call CheckDefFailure(['let var: larry'], 'E1010:')
455  call CheckDefFailure(['let var: ned'], 'E1010:')
456  call CheckDefFailure(['let var: pam'], 'E1010:')
457  call CheckDefFailure(['let var: sam'], 'E1010:')
458  call CheckDefFailure(['let var: vim'], 'E1010:')
459
460  call CheckDefFailure(['let Ref: number', 'Ref()'], 'E1085:')
461  call CheckDefFailure(['let Ref: string', 'let res = Ref()'], 'E1085:')
462endfunc
463
464func Test_const()
465  call CheckDefFailure(['const var = 234', 'var = 99'], 'E1018:')
466  call CheckDefFailure(['const one = 234', 'let one = 99'], 'E1017:')
467  call CheckDefFailure(['const two'], 'E1021:')
468  call CheckDefFailure(['const &option'], 'E996:')
469endfunc
470
471def Test_block()
472  let outer = 1
473  {
474    let inner = 2
475    assert_equal(1, outer)
476    assert_equal(2, inner)
477  }
478  assert_equal(1, outer)
479enddef
480
481func Test_block_failure()
482  call CheckDefFailure(['{', 'let inner = 1', '}', 'echo inner'], 'E1001:')
483  call CheckDefFailure(['}'], 'E1025:')
484  call CheckDefFailure(['{', 'echo 1'], 'E1026:')
485endfunc
486
487def Test_cmd_modifier()
488  tab echo '0'
489  call CheckDefFailure(['5tab echo 3'], 'E16:')
490enddef
491
492def Test_try_catch()
493  let l = []
494  try # comment
495    add(l, '1')
496    throw 'wrong'
497    add(l, '2')
498  catch # comment
499    add(l, v:exception)
500  finally # comment
501    add(l, '3')
502  endtry # comment
503  assert_equal(['1', 'wrong', '3'], l)
504enddef
505
506def ThrowFromDef()
507  throw "getout" # comment
508enddef
509
510func CatchInFunc()
511  try
512    call ThrowFromDef()
513  catch
514    let g:thrown_func = v:exception
515  endtry
516endfunc
517
518def CatchInDef()
519  try
520    ThrowFromDef()
521  catch
522    g:thrown_def = v:exception
523  endtry
524enddef
525
526def ReturnFinally(): string
527  try
528    return 'intry'
529  finally
530    g:in_finally = 'finally'
531  endtry
532  return 'end'
533enddef
534
535def Test_try_catch_nested()
536  CatchInFunc()
537  assert_equal('getout', g:thrown_func)
538
539  CatchInDef()
540  assert_equal('getout', g:thrown_def)
541
542  assert_equal('intry', ReturnFinally())
543  assert_equal('finally', g:in_finally)
544enddef
545
546def Test_try_catch_match()
547  let seq = 'a'
548  try
549    throw 'something'
550  catch /nothing/
551    seq ..= 'x'
552  catch /some/
553    seq ..= 'b'
554  catch /asdf/
555    seq ..= 'x'
556  catch ?a\?sdf?
557    seq ..= 'y'
558  finally
559    seq ..= 'c'
560  endtry
561  assert_equal('abc', seq)
562enddef
563
564def Test_try_catch_fails()
565  call CheckDefFailure(['catch'], 'E603:')
566  call CheckDefFailure(['try', 'echo 0', 'catch','catch'], 'E1033:')
567  call CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
568  call CheckDefFailure(['finally'], 'E606:')
569  call CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
570  call CheckDefFailure(['endtry'], 'E602:')
571  call CheckDefFailure(['while 1', 'endtry'], 'E170:')
572  call CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
573  call CheckDefFailure(['if 2', 'endtry'], 'E171:')
574  call CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
575
576  call CheckDefFailure(['throw'], 'E1015:')
577  call CheckDefFailure(['throw xxx'], 'E1001:')
578enddef
579
580if has('channel')
581  let someJob = test_null_job()
582
583  def FuncWithError()
584    echomsg g:someJob
585  enddef
586
587  func Test_convert_emsg_to_exception()
588    try
589      call FuncWithError()
590    catch
591      call assert_match('Vim:E908:', v:exception)
592    endtry
593  endfunc
594endif
595
596let s:export_script_lines =<< trim END
597  vim9script
598  let name: string = 'bob'
599  def Concat(arg: string): string
600    return name .. arg
601  enddef
602  g:result = Concat('bie')
603  g:localname = name
604
605  export const CONST = 1234
606  export let exported = 9876
607  export let exp_name = 'John'
608  export def Exported(): string
609    return 'Exported'
610  enddef
611END
612
613def Test_vim9_import_export()
614  let import_script_lines =<< trim END
615    vim9script
616    import {exported, Exported} from './Xexport.vim'
617    g:imported = exported
618    exported += 3
619    g:imported_added = exported
620    g:imported_func = Exported()
621
622    import {exp_name} from './Xexport.vim'
623    g:imported_name = exp_name
624    exp_name ..= ' Doe'
625    g:imported_name_appended = exp_name
626    g:imported_later = exported
627  END
628
629  writefile(import_script_lines, 'Ximport.vim')
630  writefile(s:export_script_lines, 'Xexport.vim')
631
632  source Ximport.vim
633
634  assert_equal('bobbie', g:result)
635  assert_equal('bob', g:localname)
636  assert_equal(9876, g:imported)
637  assert_equal(9879, g:imported_added)
638  assert_equal(9879, g:imported_later)
639  assert_equal('Exported', g:imported_func)
640  assert_equal('John', g:imported_name)
641  assert_equal('John Doe', g:imported_name_appended)
642  assert_false(exists('g:name'))
643
644  unlet g:result
645  unlet g:localname
646  unlet g:imported
647  unlet g:imported_added
648  unlet g:imported_later
649  unlet g:imported_func
650  unlet g:imported_name g:imported_name_appended
651  delete('Ximport.vim')
652
653  let import_in_def_lines =<< trim END
654    vim9script
655    def ImportInDef()
656      import exported from './Xexport.vim'
657      g:imported = exported
658      exported += 7
659      g:imported_added = exported
660    enddef
661    ImportInDef()
662  END
663  writefile(import_in_def_lines, 'Ximport2.vim')
664  source Ximport2.vim
665  " TODO: this should be 9879
666  assert_equal(9876, g:imported)
667  assert_equal(9883, g:imported_added)
668  unlet g:imported
669  unlet g:imported_added
670  delete('Ximport2.vim')
671
672  let import_star_as_lines =<< trim END
673    vim9script
674    import * as Export from './Xexport.vim'
675    def UseExport()
676      g:imported = Export.exported
677    enddef
678    UseExport()
679  END
680  writefile(import_star_as_lines, 'Ximport.vim')
681  source Ximport.vim
682  assert_equal(9883, g:imported)
683
684  let import_star_as_lines_no_dot =<< trim END
685    vim9script
686    import * as Export from './Xexport.vim'
687    def Func()
688      let dummy = 1
689      let imported = Export + dummy
690    enddef
691    defcompile
692  END
693  writefile(import_star_as_lines_no_dot, 'Ximport.vim')
694  assert_fails('source Ximport.vim', 'E1060:')
695
696  let import_star_as_lines_dot_space =<< trim END
697    vim9script
698    import * as Export from './Xexport.vim'
699    def Func()
700      let imported = Export . exported
701    enddef
702    defcompile
703  END
704  writefile(import_star_as_lines_dot_space, 'Ximport.vim')
705  assert_fails('source Ximport.vim', 'E1074:')
706
707  let import_star_as_lines_missing_name =<< trim END
708    vim9script
709    import * as Export from './Xexport.vim'
710    def Func()
711      let imported = Export.
712    enddef
713    defcompile
714  END
715  writefile(import_star_as_lines_missing_name, 'Ximport.vim')
716  assert_fails('source Ximport.vim', 'E1048:')
717
718  let import_star_lines =<< trim END
719    vim9script
720    import * from './Xexport.vim'
721  END
722  writefile(import_star_lines, 'Ximport.vim')
723  assert_fails('source Ximport.vim', 'E1045:')
724
725  " try to import something that exists but is not exported
726  let import_not_exported_lines =<< trim END
727    vim9script
728    import name from './Xexport.vim'
729  END
730  writefile(import_not_exported_lines, 'Ximport.vim')
731  assert_fails('source Ximport.vim', 'E1049:')
732
733  " try to import something that is already defined
734  let import_already_defined =<< trim END
735    vim9script
736    let exported = 'something'
737    import exported from './Xexport.vim'
738  END
739  writefile(import_already_defined, 'Ximport.vim')
740  assert_fails('source Ximport.vim', 'E1073:')
741
742  " try to import something that is already defined
743  import_already_defined =<< trim END
744    vim9script
745    let exported = 'something'
746    import * as exported from './Xexport.vim'
747  END
748  writefile(import_already_defined, 'Ximport.vim')
749  assert_fails('source Ximport.vim', 'E1073:')
750
751  " try to import something that is already defined
752  import_already_defined =<< trim END
753    vim9script
754    let exported = 'something'
755    import {exported} from './Xexport.vim'
756  END
757  writefile(import_already_defined, 'Ximport.vim')
758  assert_fails('source Ximport.vim', 'E1073:')
759
760  " import a very long name, requires making a copy
761  let import_long_name_lines =<< trim END
762    vim9script
763    import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
764  END
765  writefile(import_long_name_lines, 'Ximport.vim')
766  assert_fails('source Ximport.vim', 'E1048:')
767
768  let import_no_from_lines =<< trim END
769    vim9script
770    import name './Xexport.vim'
771  END
772  writefile(import_no_from_lines, 'Ximport.vim')
773  assert_fails('source Ximport.vim', 'E1070:')
774
775  let import_invalid_string_lines =<< trim END
776    vim9script
777    import name from Xexport.vim
778  END
779  writefile(import_invalid_string_lines, 'Ximport.vim')
780  assert_fails('source Ximport.vim', 'E1071:')
781
782  let import_wrong_name_lines =<< trim END
783    vim9script
784    import name from './XnoExport.vim'
785  END
786  writefile(import_wrong_name_lines, 'Ximport.vim')
787  assert_fails('source Ximport.vim', 'E1053:')
788
789  let import_missing_comma_lines =<< trim END
790    vim9script
791    import {exported name} from './Xexport.vim'
792  END
793  writefile(import_missing_comma_lines, 'Ximport3.vim')
794  assert_fails('source Ximport3.vim', 'E1046:')
795
796  delete('Ximport.vim')
797  delete('Ximport3.vim')
798  delete('Xexport.vim')
799
800  " Check that in a Vim9 script 'cpo' is set to the Vim default.
801  set cpo&vi
802  let cpo_before = &cpo
803  let lines =<< trim END
804    vim9script
805    g:cpo_in_vim9script = &cpo
806  END
807  writefile(lines, 'Xvim9_script')
808  source Xvim9_script
809  assert_equal(cpo_before, &cpo)
810  set cpo&vim
811  assert_equal(&cpo, g:cpo_in_vim9script)
812  delete('Xvim9_script')
813enddef
814
815def Test_vim9script_fails()
816  CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
817  CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
818  CheckScriptFailure(['export let some = 123'], 'E1042:')
819  CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
820  CheckScriptFailure(['vim9script', 'export let g:some'], 'E1044:')
821  CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
822
823  CheckScriptFailure(['vim9script', 'let str: string', 'str = 1234'], 'E1013:')
824  CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
825
826  assert_fails('vim9script', 'E1038')
827  assert_fails('export something', 'E1043')
828enddef
829
830func Test_import_fails_without_script()
831  CheckRunVimInTerminal
832
833  " call indirectly to avoid compilation error for missing functions
834  call Run_Test_import_fails_without_script()
835endfunc
836
837def Run_Test_import_fails_without_script()
838  let export =<< trim END
839    vim9script
840    export def Foo(): number
841        return 0
842    enddef
843  END
844  writefile(export, 'Xexport.vim')
845
846  let buf = RunVimInTerminal('-c "import Foo from ''./Xexport.vim''"', #{
847                rows: 6, wait_for_ruler: 0})
848  WaitForAssert({-> assert_match('^E1094:', term_getline(buf, 5))})
849
850  delete('Xexport.vim')
851  StopVimInTerminal(buf)
852enddef
853
854def Test_vim9script_reload_import()
855  let lines =<< trim END
856    vim9script
857    const var = ''
858    let valone = 1234
859    def MyFunc(arg: string)
860       valone = 5678
861    enddef
862  END
863  let morelines =<< trim END
864    let valtwo = 222
865    export def GetValtwo(): number
866      return valtwo
867    enddef
868  END
869  writefile(lines + morelines, 'Xreload.vim')
870  source Xreload.vim
871  source Xreload.vim
872  source Xreload.vim
873
874  let testlines =<< trim END
875    vim9script
876    def TheFunc()
877      import GetValtwo from './Xreload.vim'
878      assert_equal(222, GetValtwo())
879    enddef
880    TheFunc()
881  END
882  writefile(testlines, 'Ximport.vim')
883  source Ximport.vim
884
885  " Test that when not using "morelines" GetValtwo() and valtwo are still
886  " defined, because import doesn't reload a script.
887  writefile(lines, 'Xreload.vim')
888  source Ximport.vim
889
890  " cannot declare a var twice
891  lines =<< trim END
892    vim9script
893    let valone = 1234
894    let valone = 5678
895  END
896  writefile(lines, 'Xreload.vim')
897  assert_fails('source Xreload.vim', 'E1041:')
898
899  delete('Xreload.vim')
900  delete('Ximport.vim')
901enddef
902
903def Test_vim9script_reload_delfunc()
904  let first_lines =<< trim END
905    vim9script
906    def FuncYes(): string
907      return 'yes'
908    enddef
909  END
910  let withno_lines =<< trim END
911    def FuncNo(): string
912      return 'no'
913    enddef
914    def g:DoCheck(no_exists: bool)
915      assert_equal('yes', FuncYes())
916      assert_equal('no', FuncNo())
917    enddef
918  END
919  let nono_lines =<< trim END
920    def g:DoCheck(no_exists: bool)
921      assert_equal('yes', FuncYes())
922      assert_fails('call FuncNo()', 'E117:')
923    enddef
924  END
925
926  # FuncNo() is defined
927  writefile(first_lines + withno_lines, 'Xreloaded.vim')
928  source Xreloaded.vim
929  g:DoCheck(true)
930
931  # FuncNo() is not redefined
932  writefile(first_lines + nono_lines, 'Xreloaded.vim')
933  source Xreloaded.vim
934  g:DoCheck()
935
936  # FuncNo() is back
937  writefile(first_lines + withno_lines, 'Xreloaded.vim')
938  source Xreloaded.vim
939  g:DoCheck()
940
941  delete('Xreloaded.vim')
942enddef
943
944def Test_vim9script_reload_delvar()
945  # write the script with a script-local variable
946  let lines =<< trim END
947    vim9script
948    let var = 'string'
949  END
950  writefile(lines, 'XreloadVar.vim')
951  source XreloadVar.vim
952
953  # now write the script using the same variable locally - works
954  lines =<< trim END
955    vim9script
956    def Func()
957      let var = 'string'
958    enddef
959  END
960  writefile(lines, 'XreloadVar.vim')
961  source XreloadVar.vim
962
963  delete('XreloadVar.vim')
964enddef
965
966def Test_import_absolute()
967  let import_lines = [
968        'vim9script',
969        'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
970        'def UseExported()',
971        '  g:imported_abs = exported',
972        '  exported = 8888',
973        '  g:imported_after = exported',
974        'enddef',
975        'UseExported()',
976        'g:import_disassembled = execute("disass UseExported")',
977        ]
978  writefile(import_lines, 'Ximport_abs.vim')
979  writefile(s:export_script_lines, 'Xexport_abs.vim')
980
981  source Ximport_abs.vim
982
983  assert_equal(9876, g:imported_abs)
984  assert_equal(8888, g:imported_after)
985  assert_match('<SNR>\d\+_UseExported.*' ..
986          'g:imported_abs = exported.*' ..
987          '0 LOADSCRIPT exported from .*Xexport_abs.vim.*' ..
988          '1 STOREG g:imported_abs.*' ..
989          'exported = 8888.*' ..
990          '3 STORESCRIPT exported in .*Xexport_abs.vim.*' ..
991          'g:imported_after = exported.*' ..
992          '4 LOADSCRIPT exported from .*Xexport_abs.vim.*' ..
993          '5 STOREG g:imported_after.*',
994        g:import_disassembled)
995  unlet g:imported_abs
996  unlet g:import_disassembled
997
998  delete('Ximport_abs.vim')
999  delete('Xexport_abs.vim')
1000enddef
1001
1002def Test_import_rtp()
1003  let import_lines = [
1004        'vim9script',
1005        'import exported from "Xexport_rtp.vim"',
1006        'g:imported_rtp = exported',
1007        ]
1008  writefile(import_lines, 'Ximport_rtp.vim')
1009  mkdir('import')
1010  writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1011
1012  let save_rtp = &rtp
1013  &rtp = getcwd()
1014  source Ximport_rtp.vim
1015  &rtp = save_rtp
1016
1017  assert_equal(9876, g:imported_rtp)
1018  unlet g:imported_rtp
1019
1020  delete('Ximport_rtp.vim')
1021  delete('import', 'rf')
1022enddef
1023
1024def Test_import_compile_error()
1025  let export_lines = [
1026        'vim9script',
1027        'export def ExpFunc(): string',
1028        '  return notDefined',
1029        'enddef',
1030        ]
1031  writefile(export_lines, 'Xexported.vim')
1032
1033  let import_lines = [
1034        'vim9script',
1035        'import ExpFunc from "./Xexported.vim"',
1036        'def ImpFunc()',
1037        '  echo ExpFunc()',
1038        'enddef',
1039        'defcompile',
1040        ]
1041  writefile(import_lines, 'Ximport.vim')
1042
1043  try
1044    source Ximport.vim
1045  catch /E1001/
1046    " Error should be fore the Xexported.vim file.
1047    assert_match('E1001: variable not found: notDefined', v:exception)
1048    assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1049  endtry
1050
1051  delete('Xexported.vim')
1052  delete('Ximport.vim')
1053enddef
1054
1055def Test_fixed_size_list()
1056  " will be allocated as one piece of memory, check that changes work
1057  let l = [1, 2, 3, 4]
1058  l->remove(0)
1059  l->add(5)
1060  l->insert(99, 1)
1061  assert_equal([2, 99, 3, 4, 5], l)
1062enddef
1063
1064def IfElse(what: number): string
1065  let res = ''
1066  if what == 1
1067    res = "one"
1068  elseif what == 2
1069    res = "two"
1070  else
1071    res = "three"
1072  endif
1073  return res
1074enddef
1075
1076def Test_if_elseif_else()
1077  assert_equal('one', IfElse(1))
1078  assert_equal('two', IfElse(2))
1079  assert_equal('three', IfElse(3))
1080enddef
1081
1082def Test_if_elseif_else_fails()
1083  call CheckDefFailure(['elseif true'], 'E582:')
1084  call CheckDefFailure(['else'], 'E581:')
1085  call CheckDefFailure(['endif'], 'E580:')
1086  call CheckDefFailure(['if true', 'elseif xxx'], 'E1001:')
1087  call CheckDefFailure(['if true', 'echo 1'], 'E171:')
1088enddef
1089
1090let g:bool_true = v:true
1091let g:bool_false = v:false
1092
1093def Test_if_const_expr()
1094  let res = false
1095  if true ? true : false
1096    res = true
1097  endif
1098  assert_equal(true, res)
1099
1100  g:glob = 2
1101  if false
1102    execute('let g:glob = 3')
1103  endif
1104  assert_equal(2, g:glob)
1105  if true
1106    execute('let g:glob = 3')
1107  endif
1108  assert_equal(3, g:glob)
1109
1110  res = false
1111  if g:bool_true ? true : false
1112    res = true
1113  endif
1114  assert_equal(true, res)
1115
1116  res = false
1117  if true ? g:bool_true : false
1118    res = true
1119  endif
1120  assert_equal(true, res)
1121
1122  res = false
1123  if true ? true : g:bool_false
1124    res = true
1125  endif
1126  assert_equal(true, res)
1127
1128  res = false
1129  if true ? false : true
1130    res = true
1131  endif
1132  assert_equal(false, res)
1133
1134  res = false
1135  if false ? false : true
1136    res = true
1137  endif
1138  assert_equal(true, res)
1139
1140  res = false
1141  if false ? true : false
1142    res = true
1143  endif
1144  assert_equal(false, res)
1145
1146  res = false
1147  if has('xyz') ? true : false
1148    res = true
1149  endif
1150  assert_equal(false, res)
1151
1152  res = false
1153  if true && true
1154    res = true
1155  endif
1156  assert_equal(true, res)
1157
1158  res = false
1159  if true && false
1160    res = true
1161  endif
1162  assert_equal(false, res)
1163
1164  res = false
1165  if g:bool_true && false
1166    res = true
1167  endif
1168  assert_equal(false, res)
1169
1170  res = false
1171  if true && g:bool_false
1172    res = true
1173  endif
1174  assert_equal(false, res)
1175
1176  res = false
1177  if false && false
1178    res = true
1179  endif
1180  assert_equal(false, res)
1181
1182  res = false
1183  if true || false
1184    res = true
1185  endif
1186  assert_equal(true, res)
1187
1188  res = false
1189  if g:bool_true || false
1190    res = true
1191  endif
1192  assert_equal(true, res)
1193
1194  res = false
1195  if true || g:bool_false
1196    res = true
1197  endif
1198  assert_equal(true, res)
1199
1200  res = false
1201  if false || false
1202    res = true
1203  endif
1204  assert_equal(false, res)
1205enddef
1206
1207def Test_if_const_expr_fails()
1208  call CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
1209  call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
1210  call CheckDefFailure(["if has('aaa'"], 'E110:')
1211  call CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
1212enddef
1213
1214def RunNested(i: number): number
1215  let x: number = 0
1216  if i % 2
1217    if 1
1218      " comment
1219    else
1220      " comment
1221    endif
1222    x += 1
1223  else
1224    x += 1000
1225  endif
1226  return x
1227enddef
1228
1229def Test_nested_if()
1230  assert_equal(1, RunNested(1))
1231  assert_equal(1000, RunNested(2))
1232enddef
1233
1234def Test_execute_cmd()
1235  new
1236  setline(1, 'default')
1237  execute 'call setline(1, "execute-string")'
1238  assert_equal('execute-string', getline(1))
1239
1240  execute "call setline(1, 'execute-string')"
1241  assert_equal('execute-string', getline(1))
1242
1243  let cmd1 = 'call setline(1,'
1244  let cmd2 = '"execute-var")'
1245  execute cmd1 cmd2 # comment
1246  assert_equal('execute-var', getline(1))
1247
1248  execute cmd1 cmd2 '|call setline(1, "execute-var-string")'
1249  assert_equal('execute-var-string', getline(1))
1250
1251  let cmd_first = 'call '
1252  let cmd_last = 'setline(1, "execute-var-var")'
1253  execute cmd_first .. cmd_last
1254  assert_equal('execute-var-var', getline(1))
1255  bwipe!
1256
1257  call CheckDefFailure(['execute xxx'], 'E1001:')
1258  call CheckDefFailure(['execute "cmd"# comment'], 'E488:')
1259enddef
1260
1261def Test_echo_cmd()
1262  echo 'some' # comment
1263  echon 'thing'
1264  assert_match('^something$', Screenline(&lines))
1265
1266  echo "some" # comment
1267  echon "thing"
1268  assert_match('^something$', Screenline(&lines))
1269
1270  let str1 = 'some'
1271  let str2 = 'more'
1272  echo str1 str2
1273  assert_match('^some more$', Screenline(&lines))
1274
1275  call CheckDefFailure(['echo "xxx"# comment'], 'E488:')
1276enddef
1277
1278def Test_echomsg_cmd()
1279  echomsg 'some' 'more' # comment
1280  assert_match('^some more$', Screenline(&lines))
1281  echo 'clear'
1282  1messages
1283  assert_match('^some more$', Screenline(&lines))
1284
1285  call CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
1286enddef
1287
1288def Test_echoerr_cmd()
1289  try
1290    echoerr 'something' 'wrong' # comment
1291  catch
1292    assert_match('something wrong', v:exception)
1293  endtry
1294enddef
1295
1296def Test_for_outside_of_function()
1297  let lines =<< trim END
1298    vim9script
1299    new
1300    for var in range(0, 3)
1301      append(line('$'), var)
1302    endfor
1303    assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
1304    bwipe!
1305  END
1306  writefile(lines, 'Xvim9for.vim')
1307  source Xvim9for.vim
1308  delete('Xvim9for.vim')
1309enddef
1310
1311def Test_for_loop()
1312  let result = ''
1313  for cnt in range(7)
1314    if cnt == 4
1315      break
1316    endif
1317    if cnt == 2
1318      continue
1319    endif
1320    result ..= cnt .. '_'
1321  endfor
1322  assert_equal('0_1_3_', result)
1323enddef
1324
1325def Test_for_loop_fails()
1326  CheckDefFailure(['for # in range(5)'], 'E690:')
1327  CheckDefFailure(['for i In range(5)'], 'E690:')
1328  CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:')
1329  CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
1330  CheckDefFailure(['for i in "text"'], 'E1024:')
1331  CheckDefFailure(['for i in xxx'], 'E1001:')
1332  CheckDefFailure(['endfor'], 'E588:')
1333  CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
1334enddef
1335
1336def Test_while_loop()
1337  let result = ''
1338  let cnt = 0
1339  while cnt < 555
1340    if cnt == 3
1341      break
1342    endif
1343    cnt += 1
1344    if cnt == 2
1345      continue
1346    endif
1347    result ..= cnt .. '_'
1348  endwhile
1349  assert_equal('1_3_', result)
1350enddef
1351
1352def Test_while_loop_fails()
1353  CheckDefFailure(['while xxx'], 'E1001:')
1354  CheckDefFailure(['endwhile'], 'E588:')
1355  CheckDefFailure(['continue'], 'E586:')
1356  CheckDefFailure(['if true', 'continue'], 'E586:')
1357  CheckDefFailure(['break'], 'E587:')
1358  CheckDefFailure(['if true', 'break'], 'E587:')
1359  CheckDefFailure(['while 1', 'echo 3'], 'E170:')
1360enddef
1361
1362def Test_interrupt_loop()
1363  let caught = false
1364  let x = 0
1365  try
1366    while 1
1367      x += 1
1368      if x == 100
1369        feedkeys("\<C-C>", 'Lt')
1370      endif
1371    endwhile
1372  catch
1373    caught = true
1374    assert_equal(100, x)
1375  endtry
1376  assert_true(caught, 'should have caught an exception')
1377enddef
1378
1379def Test_automatic_line_continuation()
1380  let mylist = [
1381      'one',
1382      'two',
1383      'three',
1384      ] " comment
1385  assert_equal(['one', 'two', 'three'], mylist)
1386
1387  let mydict = {
1388      'one': 1,
1389      'two': 2,
1390      'three':
1391          3,
1392      } " comment
1393  assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict)
1394  mydict = #{
1395      one: 1,  # comment
1396      two:     # comment
1397           2,  # comment
1398      three: 3 # comment
1399      }
1400  assert_equal(#{one: 1, two: 2, three: 3}, mydict)
1401  mydict = #{
1402      one: 1,
1403      two:
1404           2,
1405      three: 3
1406      }
1407  assert_equal(#{one: 1, two: 2, three: 3}, mydict)
1408
1409  assert_equal(
1410        ['one', 'two', 'three'],
1411        split('one two three')
1412        )
1413enddef
1414
1415def Test_vim9_comment()
1416  CheckScriptSuccess([
1417      'vim9script',
1418      '# something',
1419      ])
1420  CheckScriptFailure([
1421      'vim9script',
1422      ':# something',
1423      ], 'E488:')
1424  CheckScriptFailure([
1425      '# something',
1426      ], 'E488:')
1427  CheckScriptFailure([
1428      ':# something',
1429      ], 'E488:')
1430
1431  { # block start
1432  } # block end
1433  CheckDefFailure([
1434      '{# comment',
1435      ], 'E488:')
1436  CheckDefFailure([
1437      '{',
1438      '}# comment',
1439      ], 'E488:')
1440
1441  echo "yes" # comment
1442  CheckDefFailure([
1443      'echo "yes"# comment',
1444      ], 'E488:')
1445  CheckScriptSuccess([
1446      'vim9script',
1447      'echo "yes" # something',
1448      ])
1449  CheckScriptFailure([
1450      'vim9script',
1451      'echo "yes"# something',
1452      ], 'E121:')
1453  CheckScriptFailure([
1454      'vim9script',
1455      'echo# something',
1456      ], 'E121:')
1457  CheckScriptFailure([
1458      'echo "yes" # something',
1459      ], 'E121:')
1460
1461  exe "echo" # comment
1462  CheckDefFailure([
1463      'exe "echo"# comment',
1464      ], 'E488:')
1465  CheckScriptSuccess([
1466      'vim9script',
1467      'exe "echo" # something',
1468      ])
1469  CheckScriptFailure([
1470      'vim9script',
1471      'exe "echo"# something',
1472      ], 'E121:')
1473  CheckDefFailure([
1474      'exe # comment',
1475      ], 'E1015:')
1476  CheckScriptFailure([
1477      'vim9script',
1478      'exe# something',
1479      ], 'E121:')
1480  CheckScriptFailure([
1481      'exe "echo" # something',
1482      ], 'E121:')
1483
1484  CheckDefFailure([
1485      'try# comment',
1486      '  echo "yes"',
1487      'catch',
1488      'endtry',
1489      ], 'E488:')
1490  CheckScriptFailure([
1491      'vim9script',
1492      'try# comment',
1493      'echo "yes"',
1494      ], 'E488:')
1495  CheckDefFailure([
1496      'try',
1497      '  throw#comment',
1498      'catch',
1499      'endtry',
1500      ], 'E1015:')
1501  CheckDefFailure([
1502      'try',
1503      '  throw "yes"#comment',
1504      'catch',
1505      'endtry',
1506      ], 'E488:')
1507  CheckDefFailure([
1508      'try',
1509      '  echo "yes"',
1510      'catch# comment',
1511      'endtry',
1512      ], 'E488:')
1513  CheckScriptFailure([
1514      'vim9script',
1515      'try',
1516      '  echo "yes"',
1517      'catch# comment',
1518      'endtry',
1519      ], 'E654:')
1520  CheckDefFailure([
1521      'try',
1522      '  echo "yes"',
1523      'catch /pat/# comment',
1524      'endtry',
1525      ], 'E488:')
1526  CheckDefFailure([
1527      'try',
1528      'echo "yes"',
1529      'catch',
1530      'endtry# comment',
1531      ], 'E488:')
1532  CheckScriptFailure([
1533      'vim9script',
1534      'try',
1535      '  echo "yes"',
1536      'catch',
1537      'endtry# comment',
1538      ], 'E600:')
1539
1540  CheckScriptSuccess([
1541      'vim9script',
1542      'hi # comment',
1543      ])
1544  CheckScriptFailure([
1545      'vim9script',
1546      'hi# comment',
1547      ], 'E416:')
1548  CheckScriptSuccess([
1549      'vim9script',
1550      'hi Search # comment',
1551      ])
1552  CheckScriptFailure([
1553      'vim9script',
1554      'hi Search# comment',
1555      ], 'E416:')
1556  CheckScriptSuccess([
1557      'vim9script',
1558      'hi link This Search # comment',
1559      ])
1560  CheckScriptFailure([
1561      'vim9script',
1562      'hi link This That# comment',
1563      ], 'E413:')
1564  CheckScriptSuccess([
1565      'vim9script',
1566      'hi clear This # comment',
1567      'hi clear # comment',
1568      ])
1569  " not tested, because it doesn't give an error but a warning:
1570  " hi clear This# comment',
1571  CheckScriptFailure([
1572      'vim9script',
1573      'hi clear# comment',
1574      ], 'E416:')
1575
1576  CheckScriptSuccess([
1577      'vim9script',
1578      'hi Group term=bold',
1579      'match Group /todo/ # comment',
1580      ])
1581  CheckScriptFailure([
1582      'vim9script',
1583      'hi Group term=bold',
1584      'match Group /todo/# comment',
1585      ], 'E488:')
1586  CheckScriptSuccess([
1587      'vim9script',
1588      'match # comment',
1589      ])
1590  CheckScriptFailure([
1591      'vim9script',
1592      'match# comment',
1593      ], 'E475:')
1594  CheckScriptSuccess([
1595      'vim9script',
1596      'match none # comment',
1597      ])
1598  CheckScriptFailure([
1599      'vim9script',
1600      'match none# comment',
1601      ], 'E475:')
1602
1603  CheckScriptSuccess([
1604      'vim9script',
1605      'menutrans clear # comment',
1606      ])
1607  CheckScriptFailure([
1608      'vim9script',
1609      'menutrans clear# comment text',
1610      ], 'E474:')
1611
1612  CheckScriptSuccess([
1613      'vim9script',
1614      'syntax clear # comment',
1615      ])
1616  CheckScriptFailure([
1617      'vim9script',
1618      'syntax clear# comment text',
1619      ], 'E28:')
1620  CheckScriptSuccess([
1621      'vim9script',
1622      'syntax keyword Word some',
1623      'syntax clear Word # comment',
1624      ])
1625  CheckScriptFailure([
1626      'vim9script',
1627      'syntax keyword Word some',
1628      'syntax clear Word# comment text',
1629      ], 'E28:')
1630
1631  CheckScriptSuccess([
1632      'vim9script',
1633      'syntax list # comment',
1634      ])
1635  CheckScriptFailure([
1636      'vim9script',
1637      'syntax list# comment text',
1638      ], 'E28:')
1639
1640  CheckScriptSuccess([
1641      'vim9script',
1642      'syntax match Word /pat/ oneline # comment',
1643      ])
1644  CheckScriptFailure([
1645      'vim9script',
1646      'syntax match Word /pat/ oneline# comment',
1647      ], 'E475:')
1648
1649  CheckScriptSuccess([
1650      'vim9script',
1651      'syntax keyword Word word # comm[ent',
1652      ])
1653  CheckScriptFailure([
1654      'vim9script',
1655      'syntax keyword Word word# comm[ent',
1656      ], 'E789:')
1657
1658  CheckScriptSuccess([
1659      'vim9script',
1660      'syntax match Word /pat/ # comment',
1661      ])
1662  CheckScriptFailure([
1663      'vim9script',
1664      'syntax match Word /pat/# comment',
1665      ], 'E402:')
1666
1667  CheckScriptSuccess([
1668      'vim9script',
1669      'syntax match Word /pat/ contains=Something # comment',
1670      ])
1671  CheckScriptFailure([
1672      'vim9script',
1673      'syntax match Word /pat/ contains=Something# comment',
1674      ], 'E475:')
1675  CheckScriptFailure([
1676      'vim9script',
1677      'syntax match Word /pat/ contains= # comment',
1678      ], 'E406:')
1679  CheckScriptFailure([
1680      'vim9script',
1681      'syntax match Word /pat/ contains=# comment',
1682      ], 'E475:')
1683
1684  CheckScriptSuccess([
1685      'vim9script',
1686      'syntax region Word start=/pat/ end=/pat/ # comment',
1687      ])
1688  CheckScriptFailure([
1689      'vim9script',
1690      'syntax region Word start=/pat/ end=/pat/# comment',
1691      ], 'E475:')
1692
1693  CheckScriptSuccess([
1694      'vim9script',
1695      'syntax sync # comment',
1696      ])
1697  CheckScriptFailure([
1698      'vim9script',
1699      'syntax sync# comment',
1700      ], 'E404:')
1701  CheckScriptSuccess([
1702      'vim9script',
1703      'syntax sync ccomment # comment',
1704      ])
1705  CheckScriptFailure([
1706      'vim9script',
1707      'syntax sync ccomment# comment',
1708      ], 'E404:')
1709
1710  CheckScriptSuccess([
1711      'vim9script',
1712      'syntax cluster Some contains=Word # comment',
1713      ])
1714  CheckScriptFailure([
1715      'vim9script',
1716      'syntax cluster Some contains=Word# comment',
1717      ], 'E475:')
1718
1719  CheckScriptSuccess([
1720      'vim9script',
1721      'command Echo echo # comment',
1722      'command Echo # comment',
1723      ])
1724  CheckScriptFailure([
1725      'vim9script',
1726      'command Echo echo# comment',
1727      'Echo',
1728      ], 'E121:')
1729  CheckScriptFailure([
1730      'vim9script',
1731      'command Echo# comment',
1732      ], 'E182:')
1733  CheckScriptFailure([
1734      'vim9script',
1735      'command Echo echo',
1736      'command Echo# comment',
1737      ], 'E182:')
1738
1739  CheckScriptSuccess([
1740      'vim9script',
1741      'function # comment',
1742      ])
1743  CheckScriptFailure([
1744      'vim9script',
1745      'function# comment',
1746      ], 'E129:')
1747  CheckScriptSuccess([
1748      'vim9script',
1749      'function CheckScriptSuccess # comment',
1750      ])
1751  CheckScriptFailure([
1752      'vim9script',
1753      'function CheckScriptSuccess# comment',
1754      ], 'E488:')
1755
1756  CheckScriptSuccess([
1757      'vim9script',
1758      'func g:DeleteMeA()',
1759      'endfunc',
1760      'delfunction g:DeleteMeA # comment',
1761      ])
1762  CheckScriptFailure([
1763      'vim9script',
1764      'func g:DeleteMeB()',
1765      'endfunc',
1766      'delfunction g:DeleteMeB# comment',
1767      ], 'E488:')
1768
1769  CheckScriptSuccess([
1770      'vim9script',
1771      'call execute("ls") # comment',
1772      ])
1773  CheckScriptFailure([
1774      'vim9script',
1775      'call execute("ls")# comment',
1776      ], 'E488:')
1777enddef
1778
1779def Test_vim9_comment_gui()
1780  CheckCanRunGui
1781
1782  CheckScriptFailure([
1783      'vim9script',
1784      'gui#comment'
1785      ], 'E499:')
1786  CheckScriptFailure([
1787      'vim9script',
1788      'gui -f#comment'
1789      ], 'E499:')
1790enddef
1791
1792def Test_vim9_comment_not_compiled()
1793  au TabEnter *.vim let g:entered = 1
1794  au TabEnter *.x let g:entered = 2
1795
1796  edit test.vim
1797  doautocmd TabEnter #comment
1798  assert_equal(1, g:entered)
1799
1800  doautocmd TabEnter f.x
1801  assert_equal(2, g:entered)
1802
1803  g:entered = 0
1804  doautocmd TabEnter f.x #comment
1805  assert_equal(2, g:entered)
1806
1807  assert_fails('doautocmd Syntax#comment', 'E216:')
1808
1809  au! TabEnter
1810  unlet g:entered
1811
1812  CheckScriptSuccess([
1813      'vim9script',
1814      'let g:var = 123',
1815      'let w:var = 777',
1816      'unlet g:var w:var # something',
1817      ])
1818
1819  CheckScriptFailure([
1820      'vim9script',
1821      'let g:var = 123',
1822      'unlet g:var# comment1',
1823      ], 'E108:')
1824
1825  CheckScriptFailure([
1826      'let g:var = 123',
1827      'unlet g:var # something',
1828      ], 'E488:')
1829
1830  CheckScriptSuccess([
1831      'vim9script',
1832      'if 1 # comment2',
1833      '  echo "yes"',
1834      'elseif 2 #comment',
1835      '  echo "no"',
1836      'endif',
1837      ])
1838
1839  CheckScriptFailure([
1840      'vim9script',
1841      'if 1# comment3',
1842      '  echo "yes"',
1843      'endif',
1844      ], 'E15:')
1845
1846  CheckScriptFailure([
1847      'vim9script',
1848      'if 0 # comment4',
1849      '  echo "yes"',
1850      'elseif 2#comment',
1851      '  echo "no"',
1852      'endif',
1853      ], 'E15:')
1854
1855  CheckScriptSuccess([
1856      'vim9script',
1857      'let v = 1 # comment5',
1858      ])
1859
1860  CheckScriptFailure([
1861      'vim9script',
1862      'let v = 1# comment6',
1863      ], 'E15:')
1864
1865  CheckScriptSuccess([
1866      'vim9script',
1867      'new'
1868      'call setline(1, ["# define pat", "last"])',
1869      '$',
1870      'dsearch /pat/ #comment',
1871      'bwipe!',
1872      ])
1873
1874  CheckScriptFailure([
1875      'vim9script',
1876      'new'
1877      'call setline(1, ["# define pat", "last"])',
1878      '$',
1879      'dsearch /pat/#comment',
1880      'bwipe!',
1881      ], 'E488:')
1882
1883  CheckScriptFailure([
1884      'vim9script',
1885      'func! SomeFunc()',
1886      ], 'E477:')
1887enddef
1888
1889def Test_finish()
1890  let lines =<< trim END
1891    vim9script
1892    let g:res = 'one'
1893    if v:false | finish | endif
1894    let g:res = 'two'
1895    finish
1896    let g:res = 'three'
1897  END
1898  writefile(lines, 'Xfinished')
1899  source Xfinished
1900  assert_equal('two', g:res)
1901
1902  unlet g:res
1903  delete('Xfinished')
1904enddef
1905
1906def Test_let_func_call()
1907  let lines =<< trim END
1908    vim9script
1909    func GetValue()
1910      if exists('g:count')
1911        let g:count += 1
1912      else
1913        let g:count = 1
1914      endif
1915      return 'this'
1916    endfunc
1917    let val: string = GetValue()
1918    " env var is always a string
1919    let env = $TERM
1920  END
1921  writefile(lines, 'Xfinished')
1922  source Xfinished
1923  " GetValue() is not called during discovery phase
1924  assert_equal(1, g:count)
1925
1926  unlet g:count
1927  delete('Xfinished')
1928enddef
1929
1930def Test_let_missing_type()
1931  let lines =<< trim END
1932    vim9script
1933    let var = g:unknown
1934  END
1935  CheckScriptFailure(lines, 'E121:')
1936
1937  lines =<< trim END
1938    vim9script
1939    let nr: number = 123
1940    let var = nr
1941  END
1942  CheckScriptSuccess(lines)
1943enddef
1944
1945def Test_let_declaration()
1946  let lines =<< trim END
1947    vim9script
1948    let var: string
1949    g:var_uninit = var
1950    var = 'text'
1951    g:var_test = var
1952    " prefixing s: is optional
1953    s:var = 'prefixed'
1954    g:var_prefixed = s:var
1955
1956    let s:other: number
1957    other = 1234
1958    g:other_var = other
1959  END
1960  CheckScriptSuccess(lines)
1961  assert_equal('', g:var_uninit)
1962  assert_equal('text', g:var_test)
1963  assert_equal('prefixed', g:var_prefixed)
1964  assert_equal(1234, g:other_var)
1965
1966  unlet g:var_uninit
1967  unlet g:var_test
1968  unlet g:var_prefixed
1969  unlet g:other_var
1970enddef
1971
1972def Test_let_declaration_fails()
1973  let lines =<< trim END
1974    vim9script
1975    const var: string
1976  END
1977  CheckScriptFailure(lines, 'E1021:')
1978
1979  lines =<< trim END
1980    vim9script
1981    let 9var: string
1982  END
1983  CheckScriptFailure(lines, 'E475:')
1984enddef
1985
1986def Test_let_type_check()
1987  let lines =<< trim END
1988    vim9script
1989    let var: string
1990    var = 1234
1991  END
1992  CheckScriptFailure(lines, 'E1013:')
1993
1994  lines =<< trim END
1995    vim9script
1996    let var:string
1997  END
1998  CheckScriptFailure(lines, 'E1069:')
1999
2000  lines =<< trim END
2001    vim9script
2002    let var: asdf
2003  END
2004  CheckScriptFailure(lines, 'E1010:')
2005enddef
2006
2007def Test_forward_declaration()
2008  let lines =<< trim END
2009    vim9script
2010    def GetValue(): string
2011      return theVal
2012    enddef
2013    let theVal = 'something'
2014    g:initVal = GetValue()
2015    theVal = 'else'
2016    g:laterVal = GetValue()
2017  END
2018  writefile(lines, 'Xforward')
2019  source Xforward
2020  assert_equal('something', g:initVal)
2021  assert_equal('else', g:laterVal)
2022
2023  unlet g:initVal
2024  unlet g:laterVal
2025  delete('Xforward')
2026enddef
2027
2028def Test_source_vim9_from_legacy()
2029  let legacy_lines =<< trim END
2030    source Xvim9_script.vim
2031
2032    call assert_false(exists('local'))
2033    call assert_false(exists('exported'))
2034    call assert_false(exists('s:exported'))
2035    call assert_equal('global', global)
2036    call assert_equal('global', g:global)
2037
2038    " imported variable becomes script-local
2039    import exported from './Xvim9_script.vim'
2040    call assert_equal('exported', s:exported)
2041    call assert_false(exists('exported'))
2042
2043    " imported function becomes script-local
2044    import GetText from './Xvim9_script.vim'
2045    call assert_equal('text', s:GetText())
2046    call assert_false(exists('*GetText'))
2047  END
2048  writefile(legacy_lines, 'Xlegacy_script.vim')
2049
2050  let vim9_lines =<< trim END
2051    vim9script
2052    let local = 'local'
2053    g:global = 'global'
2054    export let exported = 'exported'
2055    export def GetText(): string
2056       return 'text'
2057    enddef
2058  END
2059  writefile(vim9_lines, 'Xvim9_script.vim')
2060
2061  source Xlegacy_script.vim
2062
2063  assert_equal('global', g:global)
2064"  unlet g:global
2065
2066  delete('Xlegacy_script.vim')
2067  delete('Xvim9_script.vim')
2068enddef
2069
2070" Keep this last, it messes up highlighting.
2071def Test_substitute_cmd()
2072  new
2073  setline(1, 'something')
2074  :substitute(some(other(
2075  assert_equal('otherthing', getline(1))
2076  bwipe!
2077
2078  " also when the context is Vim9 script
2079  let lines =<< trim END
2080    vim9script
2081    new
2082    setline(1, 'something')
2083    :substitute(some(other(
2084    assert_equal('otherthing', getline(1))
2085    bwipe!
2086  END
2087  writefile(lines, 'Xvim9lines')
2088  source Xvim9lines
2089
2090  delete('Xvim9lines')
2091enddef
2092
2093" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
2094