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