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'], 'E1016: Cannot declare an environment variable:')
326
327  call CheckDefFailure(['let @~ = 5'], 'E354:')
328  call CheckDefFailure(['let @a = 5'], 'E1066:')
329
330  call CheckDefFailure(['let g:var = 5'], 'E1016: Cannot declare a global variable:')
331  call CheckDefFailure(['let w:var = 5'], 'E1016: Cannot declare a window variable:')
332  call CheckDefFailure(['let b:var = 5'], 'E1016: Cannot declare a buffer variable:')
333  call CheckDefFailure(['let t:var = 5'], 'E1016: Cannot declare a tab variable:')
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_range_no_colon()
472  call CheckDefFailure(['%s/a/b/'], 'E1050:')
473  call CheckDefFailure(['+ s/a/b/'], 'E1050:')
474  call CheckDefFailure(['- s/a/b/'], 'E1050:')
475  call CheckDefFailure(['. s/a/b/'], 'E1050:')
476enddef
477
478
479def Test_block()
480  let outer = 1
481  {
482    let inner = 2
483    assert_equal(1, outer)
484    assert_equal(2, inner)
485  }
486  assert_equal(1, outer)
487enddef
488
489func Test_block_failure()
490  call CheckDefFailure(['{', 'let inner = 1', '}', 'echo inner'], 'E1001:')
491  call CheckDefFailure(['}'], 'E1025:')
492  call CheckDefFailure(['{', 'echo 1'], 'E1026:')
493endfunc
494
495def Test_cmd_modifier()
496  tab echo '0'
497  call CheckDefFailure(['5tab echo 3'], 'E16:')
498enddef
499
500def Test_try_catch()
501  let l = []
502  try # comment
503    add(l, '1')
504    throw 'wrong'
505    add(l, '2')
506  catch # comment
507    add(l, v:exception)
508  finally # comment
509    add(l, '3')
510  endtry # comment
511  assert_equal(['1', 'wrong', '3'], l)
512enddef
513
514def ThrowFromDef()
515  throw "getout" # comment
516enddef
517
518func CatchInFunc()
519  try
520    call ThrowFromDef()
521  catch
522    let g:thrown_func = v:exception
523  endtry
524endfunc
525
526def CatchInDef()
527  try
528    ThrowFromDef()
529  catch
530    g:thrown_def = v:exception
531  endtry
532enddef
533
534def ReturnFinally(): string
535  try
536    return 'intry'
537  finally
538    g:in_finally = 'finally'
539  endtry
540  return 'end'
541enddef
542
543def Test_try_catch_nested()
544  CatchInFunc()
545  assert_equal('getout', g:thrown_func)
546
547  CatchInDef()
548  assert_equal('getout', g:thrown_def)
549
550  assert_equal('intry', ReturnFinally())
551  assert_equal('finally', g:in_finally)
552enddef
553
554def Test_try_catch_match()
555  let seq = 'a'
556  try
557    throw 'something'
558  catch /nothing/
559    seq ..= 'x'
560  catch /some/
561    seq ..= 'b'
562  catch /asdf/
563    seq ..= 'x'
564  catch ?a\?sdf?
565    seq ..= 'y'
566  finally
567    seq ..= 'c'
568  endtry
569  assert_equal('abc', seq)
570enddef
571
572def Test_try_catch_fails()
573  call CheckDefFailure(['catch'], 'E603:')
574  call CheckDefFailure(['try', 'echo 0', 'catch','catch'], 'E1033:')
575  call CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
576  call CheckDefFailure(['finally'], 'E606:')
577  call CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
578  call CheckDefFailure(['endtry'], 'E602:')
579  call CheckDefFailure(['while 1', 'endtry'], 'E170:')
580  call CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
581  call CheckDefFailure(['if 2', 'endtry'], 'E171:')
582  call CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
583
584  call CheckDefFailure(['throw'], 'E1015:')
585  call CheckDefFailure(['throw xxx'], 'E1001:')
586enddef
587
588if has('channel')
589  let someJob = test_null_job()
590
591  def FuncWithError()
592    echomsg g:someJob
593  enddef
594
595  func Test_convert_emsg_to_exception()
596    try
597      call FuncWithError()
598    catch
599      call assert_match('Vim:E908:', v:exception)
600    endtry
601  endfunc
602endif
603
604let s:export_script_lines =<< trim END
605  vim9script
606  let name: string = 'bob'
607  def Concat(arg: string): string
608    return name .. arg
609  enddef
610  g:result = Concat('bie')
611  g:localname = name
612
613  export const CONST = 1234
614  export let exported = 9876
615  export let exp_name = 'John'
616  export def Exported(): string
617    return 'Exported'
618  enddef
619END
620
621def Test_vim9_import_export()
622  let import_script_lines =<< trim END
623    vim9script
624    import {exported, Exported} from './Xexport.vim'
625    g:imported = exported
626    exported += 3
627    g:imported_added = exported
628    g:imported_func = Exported()
629
630    import {exp_name} from './Xexport.vim'
631    g:imported_name = exp_name
632    exp_name ..= ' Doe'
633    g:imported_name_appended = exp_name
634    g:imported_later = exported
635  END
636
637  writefile(import_script_lines, 'Ximport.vim')
638  writefile(s:export_script_lines, 'Xexport.vim')
639
640  source Ximport.vim
641
642  assert_equal('bobbie', g:result)
643  assert_equal('bob', g:localname)
644  assert_equal(9876, g:imported)
645  assert_equal(9879, g:imported_added)
646  assert_equal(9879, g:imported_later)
647  assert_equal('Exported', g:imported_func)
648  assert_equal('John', g:imported_name)
649  assert_equal('John Doe', g:imported_name_appended)
650  assert_false(exists('g:name'))
651
652  unlet g:result
653  unlet g:localname
654  unlet g:imported
655  unlet g:imported_added
656  unlet g:imported_later
657  unlet g:imported_func
658  unlet g:imported_name g:imported_name_appended
659  delete('Ximport.vim')
660
661  let import_in_def_lines =<< trim END
662    vim9script
663    def ImportInDef()
664      import exported from './Xexport.vim'
665      g:imported = exported
666      exported += 7
667      g:imported_added = exported
668    enddef
669    ImportInDef()
670  END
671  writefile(import_in_def_lines, 'Ximport2.vim')
672  source Ximport2.vim
673  " TODO: this should be 9879
674  assert_equal(9876, g:imported)
675  assert_equal(9883, g:imported_added)
676  unlet g:imported
677  unlet g:imported_added
678  delete('Ximport2.vim')
679
680  let import_star_as_lines =<< trim END
681    vim9script
682    import * as Export from './Xexport.vim'
683    def UseExport()
684      g:imported = Export.exported
685    enddef
686    UseExport()
687  END
688  writefile(import_star_as_lines, 'Ximport.vim')
689  source Ximport.vim
690  assert_equal(9883, g:imported)
691
692  let import_star_as_lines_no_dot =<< trim END
693    vim9script
694    import * as Export from './Xexport.vim'
695    def Func()
696      let dummy = 1
697      let imported = Export + dummy
698    enddef
699    defcompile
700  END
701  writefile(import_star_as_lines_no_dot, 'Ximport.vim')
702  assert_fails('source Ximport.vim', 'E1060:')
703
704  let import_star_as_lines_dot_space =<< trim END
705    vim9script
706    import * as Export from './Xexport.vim'
707    def Func()
708      let imported = Export . exported
709    enddef
710    defcompile
711  END
712  writefile(import_star_as_lines_dot_space, 'Ximport.vim')
713  assert_fails('source Ximport.vim', 'E1074:')
714
715  let import_star_as_lines_missing_name =<< trim END
716    vim9script
717    import * as Export from './Xexport.vim'
718    def Func()
719      let imported = Export.
720    enddef
721    defcompile
722  END
723  writefile(import_star_as_lines_missing_name, 'Ximport.vim')
724  assert_fails('source Ximport.vim', 'E1048:')
725
726  let import_star_lines =<< trim END
727    vim9script
728    import * from './Xexport.vim'
729  END
730  writefile(import_star_lines, 'Ximport.vim')
731  assert_fails('source Ximport.vim', 'E1045:')
732
733  " try to import something that exists but is not exported
734  let import_not_exported_lines =<< trim END
735    vim9script
736    import name from './Xexport.vim'
737  END
738  writefile(import_not_exported_lines, 'Ximport.vim')
739  assert_fails('source Ximport.vim', 'E1049:')
740
741  " try to import something that is already defined
742  let import_already_defined =<< trim END
743    vim9script
744    let exported = 'something'
745    import exported from './Xexport.vim'
746  END
747  writefile(import_already_defined, 'Ximport.vim')
748  assert_fails('source Ximport.vim', 'E1073:')
749
750  " try to import something that is already defined
751  import_already_defined =<< trim END
752    vim9script
753    let exported = 'something'
754    import * as exported from './Xexport.vim'
755  END
756  writefile(import_already_defined, 'Ximport.vim')
757  assert_fails('source Ximport.vim', 'E1073:')
758
759  " try to import something that is already defined
760  import_already_defined =<< trim END
761    vim9script
762    let exported = 'something'
763    import {exported} from './Xexport.vim'
764  END
765  writefile(import_already_defined, 'Ximport.vim')
766  assert_fails('source Ximport.vim', 'E1073:')
767
768  " import a very long name, requires making a copy
769  let import_long_name_lines =<< trim END
770    vim9script
771    import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
772  END
773  writefile(import_long_name_lines, 'Ximport.vim')
774  assert_fails('source Ximport.vim', 'E1048:')
775
776  let import_no_from_lines =<< trim END
777    vim9script
778    import name './Xexport.vim'
779  END
780  writefile(import_no_from_lines, 'Ximport.vim')
781  assert_fails('source Ximport.vim', 'E1070:')
782
783  let import_invalid_string_lines =<< trim END
784    vim9script
785    import name from Xexport.vim
786  END
787  writefile(import_invalid_string_lines, 'Ximport.vim')
788  assert_fails('source Ximport.vim', 'E1071:')
789
790  let import_wrong_name_lines =<< trim END
791    vim9script
792    import name from './XnoExport.vim'
793  END
794  writefile(import_wrong_name_lines, 'Ximport.vim')
795  assert_fails('source Ximport.vim', 'E1053:')
796
797  let import_missing_comma_lines =<< trim END
798    vim9script
799    import {exported name} from './Xexport.vim'
800  END
801  writefile(import_missing_comma_lines, 'Ximport3.vim')
802  assert_fails('source Ximport3.vim', 'E1046:')
803
804  delete('Ximport.vim')
805  delete('Ximport3.vim')
806  delete('Xexport.vim')
807
808  " Check that in a Vim9 script 'cpo' is set to the Vim default.
809  set cpo&vi
810  let cpo_before = &cpo
811  let lines =<< trim END
812    vim9script
813    g:cpo_in_vim9script = &cpo
814  END
815  writefile(lines, 'Xvim9_script')
816  source Xvim9_script
817  assert_equal(cpo_before, &cpo)
818  set cpo&vim
819  assert_equal(&cpo, g:cpo_in_vim9script)
820  delete('Xvim9_script')
821enddef
822
823def Test_vim9script_fails()
824  CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
825  CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
826  CheckScriptFailure(['export let some = 123'], 'E1042:')
827  CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
828  CheckScriptFailure(['vim9script', 'export let g:some'], 'E1044:')
829  CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
830
831  CheckScriptFailure(['vim9script', 'let str: string', 'str = 1234'], 'E1013:')
832  CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
833
834  assert_fails('vim9script', 'E1038')
835  assert_fails('export something', 'E1043')
836enddef
837
838func Test_import_fails_without_script()
839  CheckRunVimInTerminal
840
841  " call indirectly to avoid compilation error for missing functions
842  call Run_Test_import_fails_without_script()
843endfunc
844
845def Run_Test_import_fails_without_script()
846  let export =<< trim END
847    vim9script
848    export def Foo(): number
849        return 0
850    enddef
851  END
852  writefile(export, 'Xexport.vim')
853
854  let buf = RunVimInTerminal('-c "import Foo from ''./Xexport.vim''"', #{
855                rows: 6, wait_for_ruler: 0})
856  WaitForAssert({-> assert_match('^E1094:', term_getline(buf, 5))})
857
858  delete('Xexport.vim')
859  StopVimInTerminal(buf)
860enddef
861
862def Test_vim9script_reload_import()
863  let lines =<< trim END
864    vim9script
865    const var = ''
866    let valone = 1234
867    def MyFunc(arg: string)
868       valone = 5678
869    enddef
870  END
871  let morelines =<< trim END
872    let valtwo = 222
873    export def GetValtwo(): number
874      return valtwo
875    enddef
876  END
877  writefile(lines + morelines, 'Xreload.vim')
878  source Xreload.vim
879  source Xreload.vim
880  source Xreload.vim
881
882  let testlines =<< trim END
883    vim9script
884    def TheFunc()
885      import GetValtwo from './Xreload.vim'
886      assert_equal(222, GetValtwo())
887    enddef
888    TheFunc()
889  END
890  writefile(testlines, 'Ximport.vim')
891  source Ximport.vim
892
893  " Test that when not using "morelines" GetValtwo() and valtwo are still
894  " defined, because import doesn't reload a script.
895  writefile(lines, 'Xreload.vim')
896  source Ximport.vim
897
898  " cannot declare a var twice
899  lines =<< trim END
900    vim9script
901    let valone = 1234
902    let valone = 5678
903  END
904  writefile(lines, 'Xreload.vim')
905  assert_fails('source Xreload.vim', 'E1041:')
906
907  delete('Xreload.vim')
908  delete('Ximport.vim')
909enddef
910
911def Test_vim9script_reload_delfunc()
912  let first_lines =<< trim END
913    vim9script
914    def FuncYes(): string
915      return 'yes'
916    enddef
917  END
918  let withno_lines =<< trim END
919    def FuncNo(): string
920      return 'no'
921    enddef
922    def g:DoCheck(no_exists: bool)
923      assert_equal('yes', FuncYes())
924      assert_equal('no', FuncNo())
925    enddef
926  END
927  let nono_lines =<< trim END
928    def g:DoCheck(no_exists: bool)
929      assert_equal('yes', FuncYes())
930      assert_fails('call FuncNo()', 'E117:')
931    enddef
932  END
933
934  # FuncNo() is defined
935  writefile(first_lines + withno_lines, 'Xreloaded.vim')
936  source Xreloaded.vim
937  g:DoCheck(true)
938
939  # FuncNo() is not redefined
940  writefile(first_lines + nono_lines, 'Xreloaded.vim')
941  source Xreloaded.vim
942  g:DoCheck()
943
944  # FuncNo() is back
945  writefile(first_lines + withno_lines, 'Xreloaded.vim')
946  source Xreloaded.vim
947  g:DoCheck()
948
949  delete('Xreloaded.vim')
950enddef
951
952def Test_vim9script_reload_delvar()
953  # write the script with a script-local variable
954  let lines =<< trim END
955    vim9script
956    let var = 'string'
957  END
958  writefile(lines, 'XreloadVar.vim')
959  source XreloadVar.vim
960
961  # now write the script using the same variable locally - works
962  lines =<< trim END
963    vim9script
964    def Func()
965      let var = 'string'
966    enddef
967  END
968  writefile(lines, 'XreloadVar.vim')
969  source XreloadVar.vim
970
971  delete('XreloadVar.vim')
972enddef
973
974def Test_import_absolute()
975  let import_lines = [
976        'vim9script',
977        'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
978        'def UseExported()',
979        '  g:imported_abs = exported',
980        '  exported = 8888',
981        '  g:imported_after = exported',
982        'enddef',
983        'UseExported()',
984        'g:import_disassembled = execute("disass UseExported")',
985        ]
986  writefile(import_lines, 'Ximport_abs.vim')
987  writefile(s:export_script_lines, 'Xexport_abs.vim')
988
989  source Ximport_abs.vim
990
991  assert_equal(9876, g:imported_abs)
992  assert_equal(8888, g:imported_after)
993  assert_match('<SNR>\d\+_UseExported.*' ..
994          'g:imported_abs = exported.*' ..
995          '0 LOADSCRIPT exported from .*Xexport_abs.vim.*' ..
996          '1 STOREG g:imported_abs.*' ..
997          'exported = 8888.*' ..
998          '3 STORESCRIPT exported in .*Xexport_abs.vim.*' ..
999          'g:imported_after = exported.*' ..
1000          '4 LOADSCRIPT exported from .*Xexport_abs.vim.*' ..
1001          '5 STOREG g:imported_after.*',
1002        g:import_disassembled)
1003  unlet g:imported_abs
1004  unlet g:import_disassembled
1005
1006  delete('Ximport_abs.vim')
1007  delete('Xexport_abs.vim')
1008enddef
1009
1010def Test_import_rtp()
1011  let import_lines = [
1012        'vim9script',
1013        'import exported from "Xexport_rtp.vim"',
1014        'g:imported_rtp = exported',
1015        ]
1016  writefile(import_lines, 'Ximport_rtp.vim')
1017  mkdir('import')
1018  writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1019
1020  let save_rtp = &rtp
1021  &rtp = getcwd()
1022  source Ximport_rtp.vim
1023  &rtp = save_rtp
1024
1025  assert_equal(9876, g:imported_rtp)
1026  unlet g:imported_rtp
1027
1028  delete('Ximport_rtp.vim')
1029  delete('import', 'rf')
1030enddef
1031
1032def Test_import_compile_error()
1033  let export_lines = [
1034        'vim9script',
1035        'export def ExpFunc(): string',
1036        '  return notDefined',
1037        'enddef',
1038        ]
1039  writefile(export_lines, 'Xexported.vim')
1040
1041  let import_lines = [
1042        'vim9script',
1043        'import ExpFunc from "./Xexported.vim"',
1044        'def ImpFunc()',
1045        '  echo ExpFunc()',
1046        'enddef',
1047        'defcompile',
1048        ]
1049  writefile(import_lines, 'Ximport.vim')
1050
1051  try
1052    source Ximport.vim
1053  catch /E1001/
1054    " Error should be fore the Xexported.vim file.
1055    assert_match('E1001: variable not found: notDefined', v:exception)
1056    assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1057  endtry
1058
1059  delete('Xexported.vim')
1060  delete('Ximport.vim')
1061enddef
1062
1063def Test_fixed_size_list()
1064  " will be allocated as one piece of memory, check that changes work
1065  let l = [1, 2, 3, 4]
1066  l->remove(0)
1067  l->add(5)
1068  l->insert(99, 1)
1069  assert_equal([2, 99, 3, 4, 5], l)
1070enddef
1071
1072def IfElse(what: number): string
1073  let res = ''
1074  if what == 1
1075    res = "one"
1076  elseif what == 2
1077    res = "two"
1078  else
1079    res = "three"
1080  endif
1081  return res
1082enddef
1083
1084def Test_if_elseif_else()
1085  assert_equal('one', IfElse(1))
1086  assert_equal('two', IfElse(2))
1087  assert_equal('three', IfElse(3))
1088enddef
1089
1090def Test_if_elseif_else_fails()
1091  call CheckDefFailure(['elseif true'], 'E582:')
1092  call CheckDefFailure(['else'], 'E581:')
1093  call CheckDefFailure(['endif'], 'E580:')
1094  call CheckDefFailure(['if true', 'elseif xxx'], 'E1001:')
1095  call CheckDefFailure(['if true', 'echo 1'], 'E171:')
1096enddef
1097
1098let g:bool_true = v:true
1099let g:bool_false = v:false
1100
1101def Test_if_const_expr()
1102  let res = false
1103  if true ? true : false
1104    res = true
1105  endif
1106  assert_equal(true, res)
1107
1108  g:glob = 2
1109  if false
1110    execute('g:glob = 3')
1111  endif
1112  assert_equal(2, g:glob)
1113  if true
1114    execute('g:glob = 3')
1115  endif
1116  assert_equal(3, g:glob)
1117
1118  res = false
1119  if g:bool_true ? true : false
1120    res = true
1121  endif
1122  assert_equal(true, res)
1123
1124  res = false
1125  if true ? g:bool_true : false
1126    res = true
1127  endif
1128  assert_equal(true, res)
1129
1130  res = false
1131  if true ? true : g:bool_false
1132    res = true
1133  endif
1134  assert_equal(true, res)
1135
1136  res = false
1137  if true ? false : true
1138    res = true
1139  endif
1140  assert_equal(false, res)
1141
1142  res = false
1143  if false ? false : true
1144    res = true
1145  endif
1146  assert_equal(true, res)
1147
1148  res = false
1149  if false ? true : false
1150    res = true
1151  endif
1152  assert_equal(false, res)
1153
1154  res = false
1155  if has('xyz') ? true : false
1156    res = true
1157  endif
1158  assert_equal(false, res)
1159
1160  res = false
1161  if true && true
1162    res = true
1163  endif
1164  assert_equal(true, res)
1165
1166  res = false
1167  if true && false
1168    res = true
1169  endif
1170  assert_equal(false, res)
1171
1172  res = false
1173  if g:bool_true && false
1174    res = true
1175  endif
1176  assert_equal(false, res)
1177
1178  res = false
1179  if true && g:bool_false
1180    res = true
1181  endif
1182  assert_equal(false, res)
1183
1184  res = false
1185  if false && false
1186    res = true
1187  endif
1188  assert_equal(false, res)
1189
1190  res = false
1191  if true || false
1192    res = true
1193  endif
1194  assert_equal(true, res)
1195
1196  res = false
1197  if g:bool_true || false
1198    res = true
1199  endif
1200  assert_equal(true, res)
1201
1202  res = false
1203  if true || g:bool_false
1204    res = true
1205  endif
1206  assert_equal(true, res)
1207
1208  res = false
1209  if false || false
1210    res = true
1211  endif
1212  assert_equal(false, res)
1213enddef
1214
1215def Test_if_const_expr_fails()
1216  call CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
1217  call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
1218  call CheckDefFailure(["if has('aaa'"], 'E110:')
1219  call CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
1220enddef
1221
1222def RunNested(i: number): number
1223  let x: number = 0
1224  if i % 2
1225    if 1
1226      " comment
1227    else
1228      " comment
1229    endif
1230    x += 1
1231  else
1232    x += 1000
1233  endif
1234  return x
1235enddef
1236
1237def Test_nested_if()
1238  assert_equal(1, RunNested(1))
1239  assert_equal(1000, RunNested(2))
1240enddef
1241
1242def Test_execute_cmd()
1243  new
1244  setline(1, 'default')
1245  execute 'call setline(1, "execute-string")'
1246  assert_equal('execute-string', getline(1))
1247
1248  execute "call setline(1, 'execute-string')"
1249  assert_equal('execute-string', getline(1))
1250
1251  let cmd1 = 'call setline(1,'
1252  let cmd2 = '"execute-var")'
1253  execute cmd1 cmd2 # comment
1254  assert_equal('execute-var', getline(1))
1255
1256  execute cmd1 cmd2 '|call setline(1, "execute-var-string")'
1257  assert_equal('execute-var-string', getline(1))
1258
1259  let cmd_first = 'call '
1260  let cmd_last = 'setline(1, "execute-var-var")'
1261  execute cmd_first .. cmd_last
1262  assert_equal('execute-var-var', getline(1))
1263  bwipe!
1264
1265  call CheckDefFailure(['execute xxx'], 'E1001:')
1266  call CheckDefFailure(['execute "cmd"# comment'], 'E488:')
1267enddef
1268
1269def Test_echo_cmd()
1270  echo 'some' # comment
1271  echon 'thing'
1272  assert_match('^something$', Screenline(&lines))
1273
1274  echo "some" # comment
1275  echon "thing"
1276  assert_match('^something$', Screenline(&lines))
1277
1278  let str1 = 'some'
1279  let str2 = 'more'
1280  echo str1 str2
1281  assert_match('^some more$', Screenline(&lines))
1282
1283  call CheckDefFailure(['echo "xxx"# comment'], 'E488:')
1284enddef
1285
1286def Test_echomsg_cmd()
1287  echomsg 'some' 'more' # comment
1288  assert_match('^some more$', Screenline(&lines))
1289  echo 'clear'
1290  :1messages
1291  assert_match('^some more$', Screenline(&lines))
1292
1293  call CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
1294enddef
1295
1296def Test_echoerr_cmd()
1297  try
1298    echoerr 'something' 'wrong' # comment
1299  catch
1300    assert_match('something wrong', v:exception)
1301  endtry
1302enddef
1303
1304def Test_for_outside_of_function()
1305  let lines =<< trim END
1306    vim9script
1307    new
1308    for var in range(0, 3)
1309      append(line('$'), var)
1310    endfor
1311    assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
1312    bwipe!
1313  END
1314  writefile(lines, 'Xvim9for.vim')
1315  source Xvim9for.vim
1316  delete('Xvim9for.vim')
1317enddef
1318
1319def Test_for_loop()
1320  let result = ''
1321  for cnt in range(7)
1322    if cnt == 4
1323      break
1324    endif
1325    if cnt == 2
1326      continue
1327    endif
1328    result ..= cnt .. '_'
1329  endfor
1330  assert_equal('0_1_3_', result)
1331enddef
1332
1333def Test_for_loop_fails()
1334  CheckDefFailure(['for # in range(5)'], 'E690:')
1335  CheckDefFailure(['for i In range(5)'], 'E690:')
1336  CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:')
1337  CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
1338  CheckDefFailure(['for i in "text"'], 'E1024:')
1339  CheckDefFailure(['for i in xxx'], 'E1001:')
1340  CheckDefFailure(['endfor'], 'E588:')
1341  CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
1342enddef
1343
1344def Test_while_loop()
1345  let result = ''
1346  let cnt = 0
1347  while cnt < 555
1348    if cnt == 3
1349      break
1350    endif
1351    cnt += 1
1352    if cnt == 2
1353      continue
1354    endif
1355    result ..= cnt .. '_'
1356  endwhile
1357  assert_equal('1_3_', result)
1358enddef
1359
1360def Test_while_loop_fails()
1361  CheckDefFailure(['while xxx'], 'E1001:')
1362  CheckDefFailure(['endwhile'], 'E588:')
1363  CheckDefFailure(['continue'], 'E586:')
1364  CheckDefFailure(['if true', 'continue'], 'E586:')
1365  CheckDefFailure(['break'], 'E587:')
1366  CheckDefFailure(['if true', 'break'], 'E587:')
1367  CheckDefFailure(['while 1', 'echo 3'], 'E170:')
1368enddef
1369
1370def Test_interrupt_loop()
1371  let caught = false
1372  let x = 0
1373  try
1374    while 1
1375      x += 1
1376      if x == 100
1377        feedkeys("\<C-C>", 'Lt')
1378      endif
1379    endwhile
1380  catch
1381    caught = true
1382    assert_equal(100, x)
1383  endtry
1384  assert_true(caught, 'should have caught an exception')
1385enddef
1386
1387def Test_automatic_line_continuation()
1388  let mylist = [
1389      'one',
1390      'two',
1391      'three',
1392      ] " comment
1393  assert_equal(['one', 'two', 'three'], mylist)
1394
1395  let mydict = {
1396      'one': 1,
1397      'two': 2,
1398      'three':
1399          3,
1400      } " comment
1401  assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict)
1402  mydict = #{
1403      one: 1,  # comment
1404      two:     # comment
1405           2,  # comment
1406      three: 3 # comment
1407      }
1408  assert_equal(#{one: 1, two: 2, three: 3}, mydict)
1409  mydict = #{
1410      one: 1,
1411      two:
1412           2,
1413      three: 3
1414      }
1415  assert_equal(#{one: 1, two: 2, three: 3}, mydict)
1416
1417  assert_equal(
1418        ['one', 'two', 'three'],
1419        split('one two three')
1420        )
1421enddef
1422
1423def Test_vim9_comment()
1424  CheckScriptSuccess([
1425      'vim9script',
1426      '# something',
1427      ])
1428  CheckScriptFailure([
1429      'vim9script',
1430      ':# something',
1431      ], 'E488:')
1432  CheckScriptFailure([
1433      '# something',
1434      ], 'E488:')
1435  CheckScriptFailure([
1436      ':# something',
1437      ], 'E488:')
1438
1439  { # block start
1440  } # block end
1441  CheckDefFailure([
1442      '{# comment',
1443      ], 'E488:')
1444  CheckDefFailure([
1445      '{',
1446      '}# comment',
1447      ], 'E488:')
1448
1449  echo "yes" # comment
1450  CheckDefFailure([
1451      'echo "yes"# comment',
1452      ], 'E488:')
1453  CheckScriptSuccess([
1454      'vim9script',
1455      'echo "yes" # something',
1456      ])
1457  CheckScriptFailure([
1458      'vim9script',
1459      'echo "yes"# something',
1460      ], 'E121:')
1461  CheckScriptFailure([
1462      'vim9script',
1463      'echo# something',
1464      ], 'E121:')
1465  CheckScriptFailure([
1466      'echo "yes" # something',
1467      ], 'E121:')
1468
1469  exe "echo" # comment
1470  CheckDefFailure([
1471      'exe "echo"# comment',
1472      ], 'E488:')
1473  CheckScriptSuccess([
1474      'vim9script',
1475      'exe "echo" # something',
1476      ])
1477  CheckScriptFailure([
1478      'vim9script',
1479      'exe "echo"# something',
1480      ], 'E121:')
1481  CheckDefFailure([
1482      'exe # comment',
1483      ], 'E1015:')
1484  CheckScriptFailure([
1485      'vim9script',
1486      'exe# something',
1487      ], 'E121:')
1488  CheckScriptFailure([
1489      'exe "echo" # something',
1490      ], 'E121:')
1491
1492  CheckDefFailure([
1493      'try# comment',
1494      '  echo "yes"',
1495      'catch',
1496      'endtry',
1497      ], 'E488:')
1498  CheckScriptFailure([
1499      'vim9script',
1500      'try# comment',
1501      'echo "yes"',
1502      ], 'E488:')
1503  CheckDefFailure([
1504      'try',
1505      '  throw#comment',
1506      'catch',
1507      'endtry',
1508      ], 'E1015:')
1509  CheckDefFailure([
1510      'try',
1511      '  throw "yes"#comment',
1512      'catch',
1513      'endtry',
1514      ], 'E488:')
1515  CheckDefFailure([
1516      'try',
1517      '  echo "yes"',
1518      'catch# comment',
1519      'endtry',
1520      ], 'E488:')
1521  CheckScriptFailure([
1522      'vim9script',
1523      'try',
1524      '  echo "yes"',
1525      'catch# comment',
1526      'endtry',
1527      ], 'E654:')
1528  CheckDefFailure([
1529      'try',
1530      '  echo "yes"',
1531      'catch /pat/# comment',
1532      'endtry',
1533      ], 'E488:')
1534  CheckDefFailure([
1535      'try',
1536      'echo "yes"',
1537      'catch',
1538      'endtry# comment',
1539      ], 'E488:')
1540  CheckScriptFailure([
1541      'vim9script',
1542      'try',
1543      '  echo "yes"',
1544      'catch',
1545      'endtry# comment',
1546      ], 'E600:')
1547
1548  CheckScriptSuccess([
1549      'vim9script',
1550      'hi # comment',
1551      ])
1552  CheckScriptFailure([
1553      'vim9script',
1554      'hi# comment',
1555      ], 'E416:')
1556  CheckScriptSuccess([
1557      'vim9script',
1558      'hi Search # comment',
1559      ])
1560  CheckScriptFailure([
1561      'vim9script',
1562      'hi Search# comment',
1563      ], 'E416:')
1564  CheckScriptSuccess([
1565      'vim9script',
1566      'hi link This Search # comment',
1567      ])
1568  CheckScriptFailure([
1569      'vim9script',
1570      'hi link This That# comment',
1571      ], 'E413:')
1572  CheckScriptSuccess([
1573      'vim9script',
1574      'hi clear This # comment',
1575      'hi clear # comment',
1576      ])
1577  " not tested, because it doesn't give an error but a warning:
1578  " hi clear This# comment',
1579  CheckScriptFailure([
1580      'vim9script',
1581      'hi clear# comment',
1582      ], 'E416:')
1583
1584  CheckScriptSuccess([
1585      'vim9script',
1586      'hi Group term=bold',
1587      'match Group /todo/ # comment',
1588      ])
1589  CheckScriptFailure([
1590      'vim9script',
1591      'hi Group term=bold',
1592      'match Group /todo/# comment',
1593      ], 'E488:')
1594  CheckScriptSuccess([
1595      'vim9script',
1596      'match # comment',
1597      ])
1598  CheckScriptFailure([
1599      'vim9script',
1600      'match# comment',
1601      ], 'E475:')
1602  CheckScriptSuccess([
1603      'vim9script',
1604      'match none # comment',
1605      ])
1606  CheckScriptFailure([
1607      'vim9script',
1608      'match none# comment',
1609      ], 'E475:')
1610
1611  CheckScriptSuccess([
1612      'vim9script',
1613      'menutrans clear # comment',
1614      ])
1615  CheckScriptFailure([
1616      'vim9script',
1617      'menutrans clear# comment text',
1618      ], 'E474:')
1619
1620  CheckScriptSuccess([
1621      'vim9script',
1622      'syntax clear # comment',
1623      ])
1624  CheckScriptFailure([
1625      'vim9script',
1626      'syntax clear# comment text',
1627      ], 'E28:')
1628  CheckScriptSuccess([
1629      'vim9script',
1630      'syntax keyword Word some',
1631      'syntax clear Word # comment',
1632      ])
1633  CheckScriptFailure([
1634      'vim9script',
1635      'syntax keyword Word some',
1636      'syntax clear Word# comment text',
1637      ], 'E28:')
1638
1639  CheckScriptSuccess([
1640      'vim9script',
1641      'syntax list # comment',
1642      ])
1643  CheckScriptFailure([
1644      'vim9script',
1645      'syntax list# comment text',
1646      ], 'E28:')
1647
1648  CheckScriptSuccess([
1649      'vim9script',
1650      'syntax match Word /pat/ oneline # comment',
1651      ])
1652  CheckScriptFailure([
1653      'vim9script',
1654      'syntax match Word /pat/ oneline# comment',
1655      ], 'E475:')
1656
1657  CheckScriptSuccess([
1658      'vim9script',
1659      'syntax keyword Word word # comm[ent',
1660      ])
1661  CheckScriptFailure([
1662      'vim9script',
1663      'syntax keyword Word word# comm[ent',
1664      ], 'E789:')
1665
1666  CheckScriptSuccess([
1667      'vim9script',
1668      'syntax match Word /pat/ # comment',
1669      ])
1670  CheckScriptFailure([
1671      'vim9script',
1672      'syntax match Word /pat/# comment',
1673      ], 'E402:')
1674
1675  CheckScriptSuccess([
1676      'vim9script',
1677      'syntax match Word /pat/ contains=Something # comment',
1678      ])
1679  CheckScriptFailure([
1680      'vim9script',
1681      'syntax match Word /pat/ contains=Something# comment',
1682      ], 'E475:')
1683  CheckScriptFailure([
1684      'vim9script',
1685      'syntax match Word /pat/ contains= # comment',
1686      ], 'E406:')
1687  CheckScriptFailure([
1688      'vim9script',
1689      'syntax match Word /pat/ contains=# comment',
1690      ], 'E475:')
1691
1692  CheckScriptSuccess([
1693      'vim9script',
1694      'syntax region Word start=/pat/ end=/pat/ # comment',
1695      ])
1696  CheckScriptFailure([
1697      'vim9script',
1698      'syntax region Word start=/pat/ end=/pat/# comment',
1699      ], 'E475:')
1700
1701  CheckScriptSuccess([
1702      'vim9script',
1703      'syntax sync # comment',
1704      ])
1705  CheckScriptFailure([
1706      'vim9script',
1707      'syntax sync# comment',
1708      ], 'E404:')
1709  CheckScriptSuccess([
1710      'vim9script',
1711      'syntax sync ccomment # comment',
1712      ])
1713  CheckScriptFailure([
1714      'vim9script',
1715      'syntax sync ccomment# comment',
1716      ], 'E404:')
1717
1718  CheckScriptSuccess([
1719      'vim9script',
1720      'syntax cluster Some contains=Word # comment',
1721      ])
1722  CheckScriptFailure([
1723      'vim9script',
1724      'syntax cluster Some contains=Word# comment',
1725      ], 'E475:')
1726
1727  CheckScriptSuccess([
1728      'vim9script',
1729      'command Echo echo # comment',
1730      'command Echo # comment',
1731      ])
1732  CheckScriptFailure([
1733      'vim9script',
1734      'command Echo echo# comment',
1735      'Echo',
1736      ], 'E121:')
1737  CheckScriptFailure([
1738      'vim9script',
1739      'command Echo# comment',
1740      ], 'E182:')
1741  CheckScriptFailure([
1742      'vim9script',
1743      'command Echo echo',
1744      'command Echo# comment',
1745      ], 'E182:')
1746
1747  CheckScriptSuccess([
1748      'vim9script',
1749      'function # comment',
1750      ])
1751  CheckScriptFailure([
1752      'vim9script',
1753      'function# comment',
1754      ], 'E129:')
1755  CheckScriptSuccess([
1756      'vim9script',
1757      'function CheckScriptSuccess # comment',
1758      ])
1759  CheckScriptFailure([
1760      'vim9script',
1761      'function CheckScriptSuccess# comment',
1762      ], 'E488:')
1763
1764  CheckScriptSuccess([
1765      'vim9script',
1766      'func g:DeleteMeA()',
1767      'endfunc',
1768      'delfunction g:DeleteMeA # comment',
1769      ])
1770  CheckScriptFailure([
1771      'vim9script',
1772      'func g:DeleteMeB()',
1773      'endfunc',
1774      'delfunction g:DeleteMeB# comment',
1775      ], 'E488:')
1776
1777  CheckScriptSuccess([
1778      'vim9script',
1779      'call execute("ls") # comment',
1780      ])
1781  CheckScriptFailure([
1782      'vim9script',
1783      'call execute("ls")# comment',
1784      ], 'E488:')
1785enddef
1786
1787def Test_vim9_comment_gui()
1788  CheckCanRunGui
1789
1790  CheckScriptFailure([
1791      'vim9script',
1792      'gui#comment'
1793      ], 'E499:')
1794  CheckScriptFailure([
1795      'vim9script',
1796      'gui -f#comment'
1797      ], 'E499:')
1798enddef
1799
1800def Test_vim9_comment_not_compiled()
1801  au TabEnter *.vim g:entered = 1
1802  au TabEnter *.x g:entered = 2
1803
1804  edit test.vim
1805  doautocmd TabEnter #comment
1806  assert_equal(1, g:entered)
1807
1808  doautocmd TabEnter f.x
1809  assert_equal(2, g:entered)
1810
1811  g:entered = 0
1812  doautocmd TabEnter f.x #comment
1813  assert_equal(2, g:entered)
1814
1815  assert_fails('doautocmd Syntax#comment', 'E216:')
1816
1817  au! TabEnter
1818  unlet g:entered
1819
1820  CheckScriptSuccess([
1821      'vim9script',
1822      'g:var = 123',
1823      'b:var = 456',
1824      'w:var = 777',
1825      't:var = 888',
1826      'unlet g:var w:var # something',
1827      ])
1828
1829  CheckScriptFailure([
1830      'vim9script',
1831      'let g:var = 123',
1832      ], 'E1016: Cannot declare a global variable:')
1833
1834  CheckScriptFailure([
1835      'vim9script',
1836      'let b:var = 123',
1837      ], 'E1016: Cannot declare a buffer variable:')
1838
1839  CheckScriptFailure([
1840      'vim9script',
1841      'let w:var = 123',
1842      ], 'E1016: Cannot declare a window variable:')
1843
1844  CheckScriptFailure([
1845      'vim9script',
1846      'let t:var = 123',
1847      ], 'E1016: Cannot declare a tab variable:')
1848
1849  CheckScriptFailure([
1850      'vim9script',
1851      'let v:version = 123',
1852      ], 'E1016: Cannot declare a v: variable:')
1853
1854  CheckScriptFailure([
1855      'vim9script',
1856      'let $VARIABLE = "text"',
1857      ], 'E1016: Cannot declare an environment variable:')
1858
1859  CheckScriptFailure([
1860      'vim9script',
1861      'g:var = 123',
1862      'unlet g:var# comment1',
1863      ], 'E108:')
1864
1865  CheckScriptFailure([
1866      'let g:var = 123',
1867      'unlet g:var # something',
1868      ], 'E488:')
1869
1870  CheckScriptSuccess([
1871      'vim9script',
1872      'if 1 # comment2',
1873      '  echo "yes"',
1874      'elseif 2 #comment',
1875      '  echo "no"',
1876      'endif',
1877      ])
1878
1879  CheckScriptFailure([
1880      'vim9script',
1881      'if 1# comment3',
1882      '  echo "yes"',
1883      'endif',
1884      ], 'E15:')
1885
1886  CheckScriptFailure([
1887      'vim9script',
1888      'if 0 # comment4',
1889      '  echo "yes"',
1890      'elseif 2#comment',
1891      '  echo "no"',
1892      'endif',
1893      ], 'E15:')
1894
1895  CheckScriptSuccess([
1896      'vim9script',
1897      'let v = 1 # comment5',
1898      ])
1899
1900  CheckScriptFailure([
1901      'vim9script',
1902      'let v = 1# comment6',
1903      ], 'E15:')
1904
1905  CheckScriptSuccess([
1906      'vim9script',
1907      'new'
1908      'call setline(1, ["# define pat", "last"])',
1909      ':$',
1910      'dsearch /pat/ #comment',
1911      'bwipe!',
1912      ])
1913
1914  CheckScriptFailure([
1915      'vim9script',
1916      'new'
1917      'call setline(1, ["# define pat", "last"])',
1918      ':$',
1919      'dsearch /pat/#comment',
1920      'bwipe!',
1921      ], 'E488:')
1922
1923  CheckScriptFailure([
1924      'vim9script',
1925      'func! SomeFunc()',
1926      ], 'E477:')
1927enddef
1928
1929def Test_finish()
1930  let lines =<< trim END
1931    vim9script
1932    g:res = 'one'
1933    if v:false | finish | endif
1934    g:res = 'two'
1935    finish
1936    g:res = 'three'
1937  END
1938  writefile(lines, 'Xfinished')
1939  source Xfinished
1940  assert_equal('two', g:res)
1941
1942  unlet g:res
1943  delete('Xfinished')
1944enddef
1945
1946def Test_let_func_call()
1947  let lines =<< trim END
1948    vim9script
1949    func GetValue()
1950      if exists('g:count')
1951        let g:count += 1
1952      else
1953        let g:count = 1
1954      endif
1955      return 'this'
1956    endfunc
1957    let val: string = GetValue()
1958    " env var is always a string
1959    let env = $TERM
1960  END
1961  writefile(lines, 'Xfinished')
1962  source Xfinished
1963  " GetValue() is not called during discovery phase
1964  assert_equal(1, g:count)
1965
1966  unlet g:count
1967  delete('Xfinished')
1968enddef
1969
1970def Test_let_missing_type()
1971  let lines =<< trim END
1972    vim9script
1973    let var = g:unknown
1974  END
1975  CheckScriptFailure(lines, 'E121:')
1976
1977  lines =<< trim END
1978    vim9script
1979    let nr: number = 123
1980    let var = nr
1981  END
1982  CheckScriptSuccess(lines)
1983enddef
1984
1985def Test_let_declaration()
1986  let lines =<< trim END
1987    vim9script
1988    let var: string
1989    g:var_uninit = var
1990    var = 'text'
1991    g:var_test = var
1992    " prefixing s: is optional
1993    s:var = 'prefixed'
1994    g:var_prefixed = s:var
1995
1996    let s:other: number
1997    other = 1234
1998    g:other_var = other
1999  END
2000  CheckScriptSuccess(lines)
2001  assert_equal('', g:var_uninit)
2002  assert_equal('text', g:var_test)
2003  assert_equal('prefixed', g:var_prefixed)
2004  assert_equal(1234, g:other_var)
2005
2006  unlet g:var_uninit
2007  unlet g:var_test
2008  unlet g:var_prefixed
2009  unlet g:other_var
2010enddef
2011
2012def Test_let_declaration_fails()
2013  let lines =<< trim END
2014    vim9script
2015    const var: string
2016  END
2017  CheckScriptFailure(lines, 'E1021:')
2018
2019  lines =<< trim END
2020    vim9script
2021    let 9var: string
2022  END
2023  CheckScriptFailure(lines, 'E475:')
2024enddef
2025
2026def Test_let_type_check()
2027  let lines =<< trim END
2028    vim9script
2029    let var: string
2030    var = 1234
2031  END
2032  CheckScriptFailure(lines, 'E1013:')
2033
2034  lines =<< trim END
2035    vim9script
2036    let var:string
2037  END
2038  CheckScriptFailure(lines, 'E1069:')
2039
2040  lines =<< trim END
2041    vim9script
2042    let var: asdf
2043  END
2044  CheckScriptFailure(lines, 'E1010:')
2045enddef
2046
2047def Test_forward_declaration()
2048  let lines =<< trim END
2049    vim9script
2050    def GetValue(): string
2051      return theVal
2052    enddef
2053    let theVal = 'something'
2054    g:initVal = GetValue()
2055    theVal = 'else'
2056    g:laterVal = GetValue()
2057  END
2058  writefile(lines, 'Xforward')
2059  source Xforward
2060  assert_equal('something', g:initVal)
2061  assert_equal('else', g:laterVal)
2062
2063  unlet g:initVal
2064  unlet g:laterVal
2065  delete('Xforward')
2066enddef
2067
2068def Test_source_vim9_from_legacy()
2069  let legacy_lines =<< trim END
2070    source Xvim9_script.vim
2071
2072    call assert_false(exists('local'))
2073    call assert_false(exists('exported'))
2074    call assert_false(exists('s:exported'))
2075    call assert_equal('global', global)
2076    call assert_equal('global', g:global)
2077
2078    " imported variable becomes script-local
2079    import exported from './Xvim9_script.vim'
2080    call assert_equal('exported', s:exported)
2081    call assert_false(exists('exported'))
2082
2083    " imported function becomes script-local
2084    import GetText from './Xvim9_script.vim'
2085    call assert_equal('text', s:GetText())
2086    call assert_false(exists('*GetText'))
2087  END
2088  writefile(legacy_lines, 'Xlegacy_script.vim')
2089
2090  let vim9_lines =<< trim END
2091    vim9script
2092    let local = 'local'
2093    g:global = 'global'
2094    export let exported = 'exported'
2095    export def GetText(): string
2096       return 'text'
2097    enddef
2098  END
2099  writefile(vim9_lines, 'Xvim9_script.vim')
2100
2101  source Xlegacy_script.vim
2102
2103  assert_equal('global', g:global)
2104"  unlet g:global
2105
2106  delete('Xlegacy_script.vim')
2107  delete('Xvim9_script.vim')
2108enddef
2109
2110" Keep this last, it messes up highlighting.
2111def Test_substitute_cmd()
2112  new
2113  setline(1, 'something')
2114  :substitute(some(other(
2115  assert_equal('otherthing', getline(1))
2116  bwipe!
2117
2118  " also when the context is Vim9 script
2119  let lines =<< trim END
2120    vim9script
2121    new
2122    setline(1, 'something')
2123    :substitute(some(other(
2124    assert_equal('otherthing', getline(1))
2125    bwipe!
2126  END
2127  writefile(lines, 'Xvim9lines')
2128  source Xvim9lines
2129
2130  delete('Xvim9lines')
2131enddef
2132
2133" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
2134