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