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
469if has('channel')
470  let someJob = test_null_job()
471
472  def FuncWithError()
473    echomsg g:someJob
474  enddef
475
476  func Test_convert_emsg_to_exception()
477    try
478      call FuncWithError()
479    catch
480      call assert_match('Vim:E908:', v:exception)
481    endtry
482  endfunc
483endif
484
485let s:export_script_lines =<< trim END
486  vim9script
487  let name: string = 'bob'
488  def Concat(arg: string): string
489    return name .. arg
490  enddef
491  let g:result = Concat('bie')
492  let g:localname = name
493
494  export const CONST = 1234
495  export let exported = 9876
496  export let exp_name = 'John'
497  export def Exported(): string
498    return 'Exported'
499  enddef
500END
501
502def Test_vim9_import_export()
503  let import_script_lines =<< trim END
504    vim9script
505    import {exported, Exported} from './Xexport.vim'
506    g:imported = exported
507    exported += 3
508    g:imported_added = exported
509    g:imported_func = Exported()
510
511    import {exp_name} from './Xexport.vim'
512    g:imported_name = exp_name
513    exp_name ..= ' Doe'
514    g:imported_name_appended = exp_name
515    g:imported_later = exported
516  END
517
518  writefile(import_script_lines, 'Ximport.vim')
519  writefile(s:export_script_lines, 'Xexport.vim')
520
521  source Ximport.vim
522
523  assert_equal('bobbie', g:result)
524  assert_equal('bob', g:localname)
525  assert_equal(9876, g:imported)
526  assert_equal(9879, g:imported_added)
527  assert_equal(9879, g:imported_later)
528  assert_equal('Exported', g:imported_func)
529  assert_equal('John', g:imported_name)
530  assert_equal('John Doe', g:imported_name_appended)
531  assert_false(exists('g:name'))
532
533  unlet g:result
534  unlet g:localname
535  unlet g:imported
536  unlet g:imported_added
537  unlet g:imported_later
538  unlet g:imported_func
539  unlet g:imported_name g:imported_name_appended
540  delete('Ximport.vim')
541
542  let import_in_def_lines =<< trim END
543    vim9script
544    def ImportInDef()
545      import exported from './Xexport.vim'
546      g:imported = exported
547      exported += 7
548      g:imported_added = exported
549    enddef
550    ImportInDef()
551  END
552  writefile(import_in_def_lines, 'Ximport2.vim')
553  source Ximport2.vim
554  " TODO: this should be 9879
555  assert_equal(9876, g:imported)
556  assert_equal(9883, g:imported_added)
557  unlet g:imported
558  unlet g:imported_added
559  delete('Ximport2.vim')
560
561  let import_star_as_lines =<< trim END
562    vim9script
563    import * as Export from './Xexport.vim'
564    def UseExport()
565      g:imported = Export.exported
566    enddef
567    UseExport()
568  END
569  writefile(import_star_as_lines, 'Ximport.vim')
570  source Ximport.vim
571  assert_equal(9883, g:imported)
572
573  let import_star_as_lines_no_dot =<< trim END
574    vim9script
575    import * as Export from './Xexport.vim'
576    def Func()
577      let dummy = 1
578      let imported = Export + dummy
579    enddef
580  END
581  writefile(import_star_as_lines_no_dot, 'Ximport.vim')
582  assert_fails('source Ximport.vim', 'E1060:')
583
584  let import_star_as_lines_dot_space =<< trim END
585    vim9script
586    import * as Export from './Xexport.vim'
587    def Func()
588      let imported = Export . exported
589    enddef
590  END
591  writefile(import_star_as_lines_dot_space, 'Ximport.vim')
592  assert_fails('source Ximport.vim', 'E1074:')
593
594  let import_star_as_lines_missing_name =<< trim END
595    vim9script
596    import * as Export from './Xexport.vim'
597    def Func()
598      let imported = Export.
599    enddef
600  END
601  writefile(import_star_as_lines_missing_name, 'Ximport.vim')
602  assert_fails('source Ximport.vim', 'E1048:')
603
604  let import_star_lines =<< trim END
605    vim9script
606    import * from './Xexport.vim'
607    g:imported = exported
608  END
609  writefile(import_star_lines, 'Ximport.vim')
610  assert_fails('source Ximport.vim', 'E1045:')
611
612  " try to import something that exists but is not exported
613  let import_not_exported_lines =<< trim END
614    vim9script
615    import name from './Xexport.vim'
616  END
617  writefile(import_not_exported_lines, 'Ximport.vim')
618  assert_fails('source Ximport.vim', 'E1049:')
619
620  " try to import something that is already defined
621  let import_already_defined =<< trim END
622    vim9script
623    let exported = 'something'
624    import exported from './Xexport.vim'
625  END
626  writefile(import_already_defined, 'Ximport.vim')
627  assert_fails('source Ximport.vim', 'E1073:')
628
629  " try to import something that is already defined
630  import_already_defined =<< trim END
631    vim9script
632    let exported = 'something'
633    import * as exported from './Xexport.vim'
634  END
635  writefile(import_already_defined, 'Ximport.vim')
636  assert_fails('source Ximport.vim', 'E1073:')
637
638  " try to import something that is already defined
639  import_already_defined =<< trim END
640    vim9script
641    let exported = 'something'
642    import {exported} from './Xexport.vim'
643  END
644  writefile(import_already_defined, 'Ximport.vim')
645  assert_fails('source Ximport.vim', 'E1073:')
646
647  " import a very long name, requires making a copy
648  let import_long_name_lines =<< trim END
649    vim9script
650    import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
651  END
652  writefile(import_long_name_lines, 'Ximport.vim')
653  assert_fails('source Ximport.vim', 'E1048:')
654
655  let import_no_from_lines =<< trim END
656    vim9script
657    import name './Xexport.vim'
658  END
659  writefile(import_no_from_lines, 'Ximport.vim')
660  assert_fails('source Ximport.vim', 'E1070:')
661
662  let import_invalid_string_lines =<< trim END
663    vim9script
664    import name from Xexport.vim
665  END
666  writefile(import_invalid_string_lines, 'Ximport.vim')
667  assert_fails('source Ximport.vim', 'E1071:')
668
669  let import_wrong_name_lines =<< trim END
670    vim9script
671    import name from './XnoExport.vim'
672  END
673  writefile(import_wrong_name_lines, 'Ximport.vim')
674  assert_fails('source Ximport.vim', 'E1053:')
675
676  let import_missing_comma_lines =<< trim END
677    vim9script
678    import {exported name} from './Xexport.vim'
679  END
680  writefile(import_missing_comma_lines, 'Ximport3.vim')
681  assert_fails('source Ximport3.vim', 'E1046:')
682
683  delete('Ximport.vim')
684  delete('Ximport3.vim')
685  delete('Xexport.vim')
686
687  " Check that in a Vim9 script 'cpo' is set to the Vim default.
688  set cpo&vi
689  let cpo_before = &cpo
690  let lines =<< trim END
691    vim9script
692    g:cpo_in_vim9script = &cpo
693  END
694  writefile(lines, 'Xvim9_script')
695  source Xvim9_script
696  assert_equal(cpo_before, &cpo)
697  set cpo&vim
698  assert_equal(&cpo, g:cpo_in_vim9script)
699  delete('Xvim9_script')
700enddef
701
702def Test_vim9script_fails()
703  CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
704  CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
705  CheckScriptFailure(['export let some = 123'], 'E1042:')
706  CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1042:')
707  CheckScriptFailure(['vim9script', 'export let g:some'], 'E1044:')
708  CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
709
710  assert_fails('vim9script', 'E1038')
711  assert_fails('export something', 'E1043')
712enddef
713
714def Test_vim9script_reload_import()
715  let lines =<< trim END
716    vim9script
717    const var = ''
718    let valone = 1234
719    def MyFunc(arg: string)
720       valone = 5678
721    enddef
722  END
723  let morelines =<< trim END
724    let valtwo = 222
725    export def GetValtwo(): number
726      return valtwo
727    enddef
728  END
729  writefile(lines + morelines, 'Xreload.vim')
730  source Xreload.vim
731  source Xreload.vim
732  source Xreload.vim
733
734  let testlines =<< trim END
735    vim9script
736    def TheFunc()
737      import GetValtwo from './Xreload.vim'
738      assert_equal(222, GetValtwo())
739    enddef
740    TheFunc()
741  END
742  writefile(testlines, 'Ximport.vim')
743  source Ximport.vim
744
745  " Test that when not using "morelines" GetValtwo() and valtwo are still
746  " defined, because import doesn't reload a script.
747  writefile(lines, 'Xreload.vim')
748  source Ximport.vim
749
750  " cannot declare a var twice
751  lines =<< trim END
752    vim9script
753    let valone = 1234
754    let valone = 5678
755  END
756  writefile(lines, 'Xreload.vim')
757  assert_fails('source Xreload.vim', 'E1041:')
758
759  delete('Xreload.vim')
760  delete('Ximport.vim')
761enddef
762
763def Test_vim9script_reload_delfunc()
764  let first_lines =<< trim END
765    vim9script
766    def FuncYes(): string
767      return 'yes'
768    enddef
769  END
770  let withno_lines =<< trim END
771    def FuncNo(): string
772      return 'no'
773    enddef
774    def g:DoCheck(no_exists: bool)
775      assert_equal('yes', FuncYes())
776      assert_equal('no', FuncNo())
777    enddef
778  END
779  let nono_lines =<< trim END
780    def g:DoCheck(no_exists: bool)
781      assert_equal('yes', FuncYes())
782      assert_fails('call FuncNo()', 'E117:')
783    enddef
784  END
785
786  # FuncNo() is defined
787  writefile(first_lines + withno_lines, 'Xreloaded.vim')
788  source Xreloaded.vim
789  g:DoCheck(true)
790
791  # FuncNo() is not redefined
792  writefile(first_lines + nono_lines, 'Xreloaded.vim')
793  source Xreloaded.vim
794  g:DoCheck()
795
796  # FuncNo() is back
797  writefile(first_lines + withno_lines, 'Xreloaded.vim')
798  source Xreloaded.vim
799  g:DoCheck()
800
801  delete('Xreloaded.vim')
802enddef
803
804def Test_import_absolute()
805  let import_lines = [
806        'vim9script',
807        'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
808        'def UseExported()',
809        '  g:imported_abs = exported',
810        '  exported = 8888',
811        '  g:imported_after = exported',
812        'enddef',
813        'UseExported()',
814        'g:import_disassembled = execute("disass UseExported")',
815        ]
816  writefile(import_lines, 'Ximport_abs.vim')
817  writefile(s:export_script_lines, 'Xexport_abs.vim')
818
819  source Ximport_abs.vim
820
821  assert_equal(9876, g:imported_abs)
822  assert_equal(8888, g:imported_after)
823  assert_match('<SNR>\d\+_UseExported.*' ..
824          'g:imported_abs = exported.*' ..
825          '0 LOADSCRIPT exported from .*Xexport_abs.vim.*' ..
826          '1 STOREG g:imported_abs.*' ..
827          'exported = 8888.*' ..
828          '3 STORESCRIPT exported in .*Xexport_abs.vim.*' ..
829          'g:imported_after = exported.*' ..
830          '4 LOADSCRIPT exported from .*Xexport_abs.vim.*' ..
831          '5 STOREG g:imported_after.*',
832        g:import_disassembled)
833  unlet g:imported_abs
834  unlet g:import_disassembled
835
836  delete('Ximport_abs.vim')
837  delete('Xexport_abs.vim')
838enddef
839
840def Test_import_rtp()
841  let import_lines = [
842        'vim9script',
843        'import exported from "Xexport_rtp.vim"',
844        'g:imported_rtp = exported',
845        ]
846  writefile(import_lines, 'Ximport_rtp.vim')
847  mkdir('import')
848  writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
849
850  let save_rtp = &rtp
851  &rtp = getcwd()
852  source Ximport_rtp.vim
853  &rtp = save_rtp
854
855  assert_equal(9876, g:imported_rtp)
856  unlet g:imported_rtp
857
858  delete('Ximport_rtp.vim')
859  delete('import/Xexport_rtp.vim')
860  delete('import', 'd')
861enddef
862
863def Test_fixed_size_list()
864  " will be allocated as one piece of memory, check that changes work
865  let l = [1, 2, 3, 4]
866  l->remove(0)
867  l->add(5)
868  l->insert(99, 1)
869  assert_equal([2, 99, 3, 4, 5], l)
870enddef
871
872def IfElse(what: number): string
873  let res = ''
874  if what == 1
875    res = "one"
876  elseif what == 2
877    res = "two"
878  else
879    res = "three"
880  endif
881  return res
882enddef
883
884def Test_if_elseif_else()
885  assert_equal('one', IfElse(1))
886  assert_equal('two', IfElse(2))
887  assert_equal('three', IfElse(3))
888enddef
889
890def Test_if_elseif_else_fails()
891  call CheckDefFailure(['elseif true'], 'E582:')
892  call CheckDefFailure(['else'], 'E581:')
893  call CheckDefFailure(['endif'], 'E580:')
894  call CheckDefFailure(['if true', 'elseif xxx'], 'E1001:')
895  call CheckDefFailure(['if true', 'echo 1'], 'E171:')
896enddef
897
898let g:bool_true = v:true
899let g:bool_false = v:false
900
901def Test_if_const_expr()
902  let res = false
903  if true ? true : false
904    res = true
905  endif
906  assert_equal(true, res)
907
908  g:glob = 2
909  if false
910    execute('let g:glob = 3')
911  endif
912  assert_equal(2, g:glob)
913  if true
914    execute('let g:glob = 3')
915  endif
916  assert_equal(3, g:glob)
917
918  res = false
919  if g:bool_true ? true : false
920    res = true
921  endif
922  assert_equal(true, res)
923
924  res = false
925  if true ? g:bool_true : false
926    res = true
927  endif
928  assert_equal(true, res)
929
930  res = false
931  if true ? true : g:bool_false
932    res = true
933  endif
934  assert_equal(true, res)
935
936  res = false
937  if true ? false : true
938    res = true
939  endif
940  assert_equal(false, res)
941
942  res = false
943  if false ? false : true
944    res = true
945  endif
946  assert_equal(true, res)
947
948  res = false
949  if false ? true : false
950    res = true
951  endif
952  assert_equal(false, res)
953
954  res = false
955  if has('xyz') ? true : false
956    res = true
957  endif
958  assert_equal(false, res)
959
960  res = false
961  if true && true
962    res = true
963  endif
964  assert_equal(true, res)
965
966  res = false
967  if true && false
968    res = true
969  endif
970  assert_equal(false, res)
971
972  res = false
973  if g:bool_true && false
974    res = true
975  endif
976  assert_equal(false, res)
977
978  res = false
979  if true && g:bool_false
980    res = true
981  endif
982  assert_equal(false, res)
983
984  res = false
985  if false && false
986    res = true
987  endif
988  assert_equal(false, res)
989
990  res = false
991  if true || false
992    res = true
993  endif
994  assert_equal(true, res)
995
996  res = false
997  if g:bool_true || false
998    res = true
999  endif
1000  assert_equal(true, res)
1001
1002  res = false
1003  if true || g:bool_false
1004    res = true
1005  endif
1006  assert_equal(true, res)
1007
1008  res = false
1009  if false || false
1010    res = true
1011  endif
1012  assert_equal(false, res)
1013enddef
1014
1015def Test_if_const_expr_fails()
1016  call CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
1017  call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
1018  call CheckDefFailure(["if has('aaa'"], 'E110:')
1019  call CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
1020enddef
1021
1022def Test_execute_cmd()
1023  new
1024  setline(1, 'default')
1025  execute 'call setline(1, "execute-string")'
1026  assert_equal('execute-string', getline(1))
1027
1028  execute "call setline(1, 'execute-string')"
1029  assert_equal('execute-string', getline(1))
1030
1031  let cmd1 = 'call setline(1,'
1032  let cmd2 = '"execute-var")'
1033  execute cmd1 cmd2 # comment
1034  assert_equal('execute-var', getline(1))
1035
1036  execute cmd1 cmd2 '|call setline(1, "execute-var-string")'
1037  assert_equal('execute-var-string', getline(1))
1038
1039  let cmd_first = 'call '
1040  let cmd_last = 'setline(1, "execute-var-var")'
1041  execute cmd_first .. cmd_last
1042  assert_equal('execute-var-var', getline(1))
1043  bwipe!
1044
1045  call CheckDefFailure(['execute xxx'], 'E1001:')
1046  call CheckDefFailure(['execute "cmd"# comment'], 'E488:')
1047enddef
1048
1049def Test_echo_cmd()
1050  echo 'some' # comment
1051  echon 'thing'
1052  assert_match('^something$', Screenline(&lines))
1053
1054  echo "some" # comment
1055  echon "thing"
1056  assert_match('^something$', Screenline(&lines))
1057
1058  let str1 = 'some'
1059  let str2 = 'more'
1060  echo str1 str2
1061  assert_match('^some more$', Screenline(&lines))
1062
1063  call CheckDefFailure(['echo "xxx"# comment'], 'E488:')
1064enddef
1065
1066def Test_echomsg_cmd()
1067  echomsg 'some' 'more' # comment
1068  assert_match('^some more$', Screenline(&lines))
1069  echo 'clear'
1070  1messages
1071  assert_match('^some more$', Screenline(&lines))
1072
1073  call CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
1074enddef
1075
1076def Test_echoerr_cmd()
1077  try
1078    echoerr 'something' 'wrong' # comment
1079  catch
1080    assert_match('something wrong', v:exception)
1081  endtry
1082enddef
1083
1084def Test_for_outside_of_function()
1085  let lines =<< trim END
1086    vim9script
1087    new
1088    for var in range(0, 3)
1089      append(line('$'), var)
1090    endfor
1091    assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
1092    bwipe!
1093  END
1094  writefile(lines, 'Xvim9for.vim')
1095  source Xvim9for.vim
1096  delete('Xvim9for.vim')
1097enddef
1098
1099def Test_for_loop()
1100  let result = ''
1101  for cnt in range(7)
1102    if cnt == 4
1103      break
1104    endif
1105    if cnt == 2
1106      continue
1107    endif
1108    result ..= cnt .. '_'
1109  endfor
1110  assert_equal('0_1_3_', result)
1111enddef
1112
1113def Test_for_loop_fails()
1114  CheckDefFailure(['for # in range(5)'], 'E690:')
1115  CheckDefFailure(['for i In range(5)'], 'E690:')
1116  CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:')
1117  CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef'], 'E1006:')
1118  CheckDefFailure(['for i in "text"'], 'E1024:')
1119  CheckDefFailure(['for i in xxx'], 'E1001:')
1120  CheckDefFailure(['endfor'], 'E588:')
1121  CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
1122enddef
1123
1124def Test_while_loop()
1125  let result = ''
1126  let cnt = 0
1127  while cnt < 555
1128    if cnt == 3
1129      break
1130    endif
1131    cnt += 1
1132    if cnt == 2
1133      continue
1134    endif
1135    result ..= cnt .. '_'
1136  endwhile
1137  assert_equal('1_3_', result)
1138enddef
1139
1140def Test_while_loop_fails()
1141  CheckDefFailure(['while xxx'], 'E1001:')
1142  CheckDefFailure(['endwhile'], 'E588:')
1143  CheckDefFailure(['continue'], 'E586:')
1144  CheckDefFailure(['if true', 'continue'], 'E586:')
1145  CheckDefFailure(['break'], 'E587:')
1146  CheckDefFailure(['if true', 'break'], 'E587:')
1147  CheckDefFailure(['while 1', 'echo 3'], 'E170:')
1148enddef
1149
1150def Test_interrupt_loop()
1151  let caught = false
1152  let x = 0
1153  try
1154    while 1
1155      x += 1
1156      if x == 100
1157        feedkeys("\<C-C>", 'Lt')
1158      endif
1159    endwhile
1160  catch
1161    caught = true
1162    assert_equal(100, x)
1163  endtry
1164  assert_true(caught, 'should have caught an exception')
1165enddef
1166
1167def Test_automatic_line_continuation()
1168  let mylist = [
1169      'one',
1170      'two',
1171      'three',
1172      ] " comment
1173  assert_equal(['one', 'two', 'three'], mylist)
1174
1175  let mydict = {
1176      'one': 1,
1177      'two': 2,
1178      'three':
1179          3,
1180      } " comment
1181  assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict)
1182  mydict = #{
1183      one: 1,  # comment
1184      two:     # comment
1185           2,  # comment
1186      three: 3 # comment
1187      }
1188  assert_equal(#{one: 1, two: 2, three: 3}, mydict)
1189  mydict = #{
1190      one: 1,
1191      two:
1192           2,
1193      three: 3
1194      }
1195  assert_equal(#{one: 1, two: 2, three: 3}, mydict)
1196
1197  assert_equal(
1198        ['one', 'two', 'three'],
1199        split('one two three')
1200        )
1201enddef
1202
1203def Test_vim9_comment()
1204  CheckScriptSuccess([
1205      'vim9script',
1206      '# something',
1207      ])
1208  CheckScriptFailure([
1209      'vim9script',
1210      ':# something',
1211      ], 'E488:')
1212  CheckScriptFailure([
1213      '# something',
1214      ], 'E488:')
1215  CheckScriptFailure([
1216      ':# something',
1217      ], 'E488:')
1218
1219  { # block start
1220  } # block end
1221  CheckDefFailure([
1222      '{# comment',
1223      ], 'E488:')
1224  CheckDefFailure([
1225      '{',
1226      '}# comment',
1227      ], 'E488:')
1228
1229  echo "yes" # comment
1230  CheckDefFailure([
1231      'echo "yes"# comment',
1232      ], 'E488:')
1233  CheckScriptSuccess([
1234      'vim9script',
1235      'echo "yes" # something',
1236      ])
1237  CheckScriptFailure([
1238      'vim9script',
1239      'echo "yes"# something',
1240      ], 'E121:')
1241  CheckScriptFailure([
1242      'vim9script',
1243      'echo# something',
1244      ], 'E121:')
1245  CheckScriptFailure([
1246      'echo "yes" # something',
1247      ], 'E121:')
1248
1249  exe "echo" # comment
1250  CheckDefFailure([
1251      'exe "echo"# comment',
1252      ], 'E488:')
1253  CheckScriptSuccess([
1254      'vim9script',
1255      'exe "echo" # something',
1256      ])
1257  CheckScriptFailure([
1258      'vim9script',
1259      'exe "echo"# something',
1260      ], 'E121:')
1261  CheckDefFailure([
1262      'exe # comment',
1263      ], 'E1015:')
1264  CheckScriptFailure([
1265      'vim9script',
1266      'exe# something',
1267      ], 'E121:')
1268  CheckScriptFailure([
1269      'exe "echo" # something',
1270      ], 'E121:')
1271
1272  CheckDefFailure([
1273      'try# comment',
1274      '  echo "yes"',
1275      'catch',
1276      'endtry',
1277      ], 'E488:')
1278  CheckScriptFailure([
1279      'vim9script',
1280      'try# comment',
1281      'echo "yes"',
1282      ], 'E488:')
1283  CheckDefFailure([
1284      'try',
1285      '  throw#comment',
1286      'catch',
1287      'endtry',
1288      ], 'E1015:')
1289  CheckDefFailure([
1290      'try',
1291      '  throw "yes"#comment',
1292      'catch',
1293      'endtry',
1294      ], 'E488:')
1295  CheckDefFailure([
1296      'try',
1297      '  echo "yes"',
1298      'catch# comment',
1299      'endtry',
1300      ], 'E488:')
1301  CheckScriptFailure([
1302      'vim9script',
1303      'try',
1304      '  echo "yes"',
1305      'catch# comment',
1306      'endtry',
1307      ], 'E654:')
1308  CheckDefFailure([
1309      'try',
1310      '  echo "yes"',
1311      'catch /pat/# comment',
1312      'endtry',
1313      ], 'E488:')
1314  CheckDefFailure([
1315      'try',
1316      'echo "yes"',
1317      'catch',
1318      'endtry# comment',
1319      ], 'E488:')
1320  CheckScriptFailure([
1321      'vim9script',
1322      'try',
1323      '  echo "yes"',
1324      'catch',
1325      'endtry# comment',
1326      ], 'E600:')
1327
1328  CheckScriptSuccess([
1329      'vim9script',
1330      'hi # comment',
1331      ])
1332  CheckScriptFailure([
1333      'vim9script',
1334      'hi# comment',
1335      ], 'E416:')
1336  CheckScriptSuccess([
1337      'vim9script',
1338      'hi Search # comment',
1339      ])
1340  CheckScriptFailure([
1341      'vim9script',
1342      'hi Search# comment',
1343      ], 'E416:')
1344  CheckScriptSuccess([
1345      'vim9script',
1346      'hi link This Search # comment',
1347      ])
1348  CheckScriptFailure([
1349      'vim9script',
1350      'hi link This That# comment',
1351      ], 'E413:')
1352  CheckScriptSuccess([
1353      'vim9script',
1354      'hi clear This # comment',
1355      'hi clear # comment',
1356      ])
1357  " not tested, because it doesn't give an error but a warning:
1358  " hi clear This# comment',
1359  CheckScriptFailure([
1360      'vim9script',
1361      'hi clear# comment',
1362      ], 'E416:')
1363
1364  CheckScriptSuccess([
1365      'vim9script',
1366      'hi Group term=bold',
1367      'match Group /todo/ # comment',
1368      ])
1369  CheckScriptFailure([
1370      'vim9script',
1371      'hi Group term=bold',
1372      'match Group /todo/# comment',
1373      ], 'E488:')
1374  CheckScriptSuccess([
1375      'vim9script',
1376      'match # comment',
1377      ])
1378  CheckScriptFailure([
1379      'vim9script',
1380      'match# comment',
1381      ], 'E475:')
1382  CheckScriptSuccess([
1383      'vim9script',
1384      'match none # comment',
1385      ])
1386  CheckScriptFailure([
1387      'vim9script',
1388      'match none# comment',
1389      ], 'E475:')
1390
1391  CheckScriptSuccess([
1392      'vim9script',
1393      'menutrans clear # comment',
1394      ])
1395  CheckScriptFailure([
1396      'vim9script',
1397      'menutrans clear# comment text',
1398      ], 'E474:')
1399
1400  CheckScriptSuccess([
1401      'vim9script',
1402      'syntax clear # comment',
1403      ])
1404  CheckScriptFailure([
1405      'vim9script',
1406      'syntax clear# comment text',
1407      ], 'E28:')
1408  CheckScriptSuccess([
1409      'vim9script',
1410      'syntax keyword Word some',
1411      'syntax clear Word # comment',
1412      ])
1413  CheckScriptFailure([
1414      'vim9script',
1415      'syntax keyword Word some',
1416      'syntax clear Word# comment text',
1417      ], 'E28:')
1418
1419  CheckScriptSuccess([
1420      'vim9script',
1421      'syntax list # comment',
1422      ])
1423  CheckScriptFailure([
1424      'vim9script',
1425      'syntax list# comment text',
1426      ], 'E28:')
1427
1428  CheckScriptSuccess([
1429      'vim9script',
1430      'syntax match Word /pat/ oneline # comment',
1431      ])
1432  CheckScriptFailure([
1433      'vim9script',
1434      'syntax match Word /pat/ oneline# comment',
1435      ], 'E475:')
1436
1437  CheckScriptSuccess([
1438      'vim9script',
1439      'syntax keyword Word word # comm[ent',
1440      ])
1441  CheckScriptFailure([
1442      'vim9script',
1443      'syntax keyword Word word# comm[ent',
1444      ], 'E789:')
1445
1446  CheckScriptSuccess([
1447      'vim9script',
1448      'syntax match Word /pat/ # comment',
1449      ])
1450  CheckScriptFailure([
1451      'vim9script',
1452      'syntax match Word /pat/# comment',
1453      ], 'E402:')
1454
1455  CheckScriptSuccess([
1456      'vim9script',
1457      'syntax match Word /pat/ contains=Something # comment',
1458      ])
1459  CheckScriptFailure([
1460      'vim9script',
1461      'syntax match Word /pat/ contains=Something# comment',
1462      ], 'E475:')
1463  CheckScriptFailure([
1464      'vim9script',
1465      'syntax match Word /pat/ contains= # comment',
1466      ], 'E406:')
1467  CheckScriptFailure([
1468      'vim9script',
1469      'syntax match Word /pat/ contains=# comment',
1470      ], 'E475:')
1471
1472  CheckScriptSuccess([
1473      'vim9script',
1474      'syntax region Word start=/pat/ end=/pat/ # comment',
1475      ])
1476  CheckScriptFailure([
1477      'vim9script',
1478      'syntax region Word start=/pat/ end=/pat/# comment',
1479      ], 'E475:')
1480
1481  CheckScriptSuccess([
1482      'vim9script',
1483      'syntax sync # comment',
1484      ])
1485  CheckScriptFailure([
1486      'vim9script',
1487      'syntax sync# comment',
1488      ], 'E404:')
1489  CheckScriptSuccess([
1490      'vim9script',
1491      'syntax sync ccomment # comment',
1492      ])
1493  CheckScriptFailure([
1494      'vim9script',
1495      'syntax sync ccomment# comment',
1496      ], 'E404:')
1497
1498  CheckScriptSuccess([
1499      'vim9script',
1500      'syntax cluster Some contains=Word # comment',
1501      ])
1502  CheckScriptFailure([
1503      'vim9script',
1504      'syntax cluster Some contains=Word# comment',
1505      ], 'E475:')
1506
1507  CheckScriptSuccess([
1508      'vim9script',
1509      'command Echo echo # comment',
1510      'command Echo # comment',
1511      ])
1512  CheckScriptFailure([
1513      'vim9script',
1514      'command Echo echo# comment',
1515      'Echo',
1516      ], 'E121:')
1517  CheckScriptFailure([
1518      'vim9script',
1519      'command Echo# comment',
1520      ], 'E182:')
1521  CheckScriptFailure([
1522      'vim9script',
1523      'command Echo echo',
1524      'command Echo# comment',
1525      ], 'E182:')
1526
1527  CheckScriptSuccess([
1528      'vim9script',
1529      'function # comment',
1530      ])
1531  CheckScriptFailure([
1532      'vim9script',
1533      'function# comment',
1534      ], 'E129:')
1535  CheckScriptSuccess([
1536      'vim9script',
1537      'function CheckScriptSuccess # comment',
1538      ])
1539  CheckScriptFailure([
1540      'vim9script',
1541      'function CheckScriptSuccess# comment',
1542      ], 'E488:')
1543
1544  CheckScriptSuccess([
1545      'vim9script',
1546      'func g:DeleteMeA()',
1547      'endfunc',
1548      'delfunction g:DeleteMeA # comment',
1549      ])
1550  CheckScriptFailure([
1551      'vim9script',
1552      'func g:DeleteMeB()',
1553      'endfunc',
1554      'delfunction g:DeleteMeB# comment',
1555      ], 'E488:')
1556
1557  CheckScriptSuccess([
1558      'vim9script',
1559      'call execute("ls") # comment',
1560      ])
1561  CheckScriptFailure([
1562      'vim9script',
1563      'call execute("ls")# comment',
1564      ], 'E488:')
1565enddef
1566
1567def Test_vim9_comment_gui()
1568  CheckCanRunGui
1569
1570  CheckScriptFailure([
1571      'vim9script',
1572      'gui#comment'
1573      ], 'E499:')
1574  CheckScriptFailure([
1575      'vim9script',
1576      'gui -f#comment'
1577      ], 'E499:')
1578enddef
1579
1580def Test_vim9_comment_not_compiled()
1581  au TabEnter *.vim let g:entered = 1
1582  au TabEnter *.x let g:entered = 2
1583
1584  edit test.vim
1585  doautocmd TabEnter #comment
1586  assert_equal(1, g:entered)
1587
1588  doautocmd TabEnter f.x
1589  assert_equal(2, g:entered)
1590
1591  g:entered = 0
1592  doautocmd TabEnter f.x #comment
1593  assert_equal(2, g:entered)
1594
1595  assert_fails('doautocmd Syntax#comment', 'E216:')
1596
1597  au! TabEnter
1598  unlet g:entered
1599
1600  CheckScriptSuccess([
1601      'vim9script',
1602      'let g:var = 123',
1603      'let w:var = 777',
1604      'unlet g:var w:var # something',
1605      ])
1606
1607  CheckScriptFailure([
1608      'vim9script',
1609      'let g:var = 123',
1610      'unlet g:var# comment',
1611      ], 'E108:')
1612
1613  CheckScriptFailure([
1614      'let g:var = 123',
1615      'unlet g:var # something',
1616      ], 'E488:')
1617
1618  CheckScriptSuccess([
1619      'vim9script',
1620      'if 1 # comment',
1621      '  echo "yes"',
1622      'elseif 2 #comment',
1623      '  echo "no"',
1624      'endif',
1625      ])
1626
1627  CheckScriptFailure([
1628      'vim9script',
1629      'if 1# comment',
1630      '  echo "yes"',
1631      'endif',
1632      ], 'E15:')
1633
1634  CheckScriptFailure([
1635      'vim9script',
1636      'if 0 # comment',
1637      '  echo "yes"',
1638      'elseif 2#comment',
1639      '  echo "no"',
1640      'endif',
1641      ], 'E15:')
1642
1643  CheckScriptSuccess([
1644      'vim9script',
1645      'let # comment',
1646      ])
1647
1648  CheckScriptFailure([
1649      'vim9script',
1650      'let# comment',
1651      ], 'E121:')
1652
1653  CheckScriptSuccess([
1654      'vim9script',
1655      'let v:version # comment',
1656      ])
1657
1658  CheckScriptFailure([
1659      'vim9script',
1660      'let v:version# comment',
1661      ], 'E121:')
1662
1663  CheckScriptSuccess([
1664      'vim9script',
1665      'new'
1666      'call setline(1, ["# define pat", "last"])',
1667      '$',
1668      'dsearch /pat/ #comment',
1669      'bwipe!',
1670      ])
1671
1672  CheckScriptFailure([
1673      'vim9script',
1674      'new'
1675      'call setline(1, ["# define pat", "last"])',
1676      '$',
1677      'dsearch /pat/#comment',
1678      'bwipe!',
1679      ], 'E488:')
1680enddef
1681
1682" Keep this last, it messes up highlighting.
1683def Test_substitute_cmd()
1684  new
1685  setline(1, 'something')
1686  :substitute(some(other(
1687  assert_equal('otherthing', getline(1))
1688  bwipe!
1689
1690  " also when the context is Vim9 script
1691  let lines =<< trim END
1692    vim9script
1693    new
1694    setline(1, 'something')
1695    :substitute(some(other(
1696    assert_equal('otherthing', getline(1))
1697    bwipe!
1698  END
1699  writefile(lines, 'Xvim9lines')
1700  source Xvim9lines
1701
1702  delete('Xvim9lines')
1703enddef
1704
1705" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
1706