1" Tests for Vim9 script expressions
2
3source check.vim
4source vim9.vim
5
6let g:cond = v:false
7def FuncOne(arg: number): string
8  return 'yes'
9enddef
10def FuncTwo(arg: number): number
11  return 123
12enddef
13
14" test cond ? expr : expr
15def Test_expr1_trinary()
16  var lines =<< trim END
17      assert_equal('one', true ? 'one' : 'two')
18      assert_equal('one', 1 ?
19                            'one' :
20                            'two')
21      if has('float')
22        assert_equal('one', !!0.1 ? 'one' : 'two')
23      endif
24      assert_equal('one', !!'x' ? 'one' : 'two')
25      assert_equal('one', !!'x'
26                            ? 'one'
27                            : 'two')
28      assert_equal('one', !!0z1234 ? 'one' : 'two')
29      assert_equal('one', !![0] ? 'one' : 'two')
30      assert_equal('one', !!{x: 0} ? 'one' : 'two')
31      var name = 1
32      assert_equal('one', name ? 'one' : 'two')
33
34      assert_equal('two', false ? 'one' : 'two')
35      assert_equal('two', 0 ? 'one' : 'two')
36      if has('float')
37        assert_equal('two', !!0.0 ? 'one' : 'two')
38      endif
39      assert_equal('two', !!'' ? 'one' : 'two')
40      assert_equal('two', !!0z ? 'one' : 'two')
41      assert_equal('two', !![] ? 'one' : 'two')
42      assert_equal('two', !!{} ? 'one' : 'two')
43      name = 0
44      assert_equal('two', name ? 'one' : 'two')
45
46      echo ['a'] + (1 ? ['b'] : ['c']
47                )
48      echo ['a'] + (1 ? ['b'] : ['c'] # comment
49                )
50
51      # with constant condition expression is not evaluated
52      assert_equal('one', 1 ? 'one' : xxx)
53
54      var Some: func = function('len')
55      var Other: func = function('winnr')
56      var Res: func = g:atrue ? Some : Other
57      assert_equal(function('len'), Res)
58
59      var RetOne: func(string): number = function('len')
60      var RetTwo: func(string): number = function('winnr')
61      var RetThat: func = g:atrue ? RetOne : RetTwo
62      assert_equal(function('len'), RetThat)
63
64      var X = FuncOne
65      var Y = FuncTwo
66      var Z = g:cond ? FuncOne : FuncTwo
67      assert_equal(123, Z(3))
68  END
69  CheckDefAndScriptSuccess(lines)
70enddef
71
72def Test_expr1_trinary_vimscript()
73  # check line continuation
74  var lines =<< trim END
75      var name = 1
76      		? 'yes'
77		: 'no'
78      assert_equal('yes', name)
79  END
80  CheckDefAndScriptSuccess(lines)
81
82  lines =<< trim END
83      var name = v:false
84      		? 'yes'
85		: 'no'
86      assert_equal('no', name)
87  END
88  CheckDefAndScriptSuccess(lines)
89
90  lines =<< trim END
91      var name = v:false ?
92      		'yes' :
93		'no'
94      assert_equal('no', name)
95  END
96  CheckDefAndScriptSuccess(lines)
97
98  lines =<< trim END
99      var name = v:false ?  # comment
100      		'yes' :
101                # comment
102		'no' # comment
103      assert_equal('no', name)
104  END
105  CheckDefAndScriptSuccess(lines)
106
107  # check white space
108  lines =<< trim END
109      var name = v:true?1:2
110  END
111  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''?'' at "?1:2"', 1)
112
113  lines =<< trim END
114      var name = v:true? 1 : 2
115  END
116  CheckDefAndScriptFailure(lines, 'E1004:', 1)
117
118  lines =<< trim END
119      var name = v:true ?1 : 2
120  END
121  CheckDefAndScriptFailure(lines, 'E1004:', 1)
122
123  lines =<< trim END
124      var name = v:true ? 1: 2
125  END
126  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after '':'' at ": 2"', 1)
127
128  lines =<< trim END
129      var name = v:true ? 1 :2
130  END
131  CheckDefAndScriptFailure(lines, 'E1004:', 1)
132
133  lines =<< trim END
134      var name = 'x' ? 1 : 2
135  END
136  CheckDefAndScriptFailure(lines, 'E1135:', 1)
137
138  lines =<< trim END
139      var name = [] ? 1 : 2
140  END
141  CheckDefExecAndScriptFailure(lines, 'E745:', 1)
142
143  lines =<< trim END
144      var name = {} ? 1 : 2
145  END
146  CheckDefExecAndScriptFailure(lines, 'E728:', 1)
147
148  # check after failure eval_flags is reset
149  lines =<< trim END
150      try
151        eval('0 ? 1: 2')
152      catch
153      endtry
154      assert_equal(v:true, eval(string(v:true)))
155  END
156  CheckDefAndScriptSuccess(lines)
157
158  lines =<< trim END
159      try
160        eval('0 ? 1 :2')
161      catch
162      endtry
163      assert_equal(v:true, eval(string(v:true)))
164  END
165  CheckDefAndScriptSuccess(lines)
166enddef
167
168func Test_expr1_trinary_fails()
169  call CheckDefAndScriptFailure(["var x = 1 ? 'one'"], "Missing ':' after '?'", 1)
170
171  let msg = "White space required before and after '?'"
172  call CheckDefAndScriptFailure(["var x = 1? 'one' : 'two'"], msg, 1)
173  call CheckDefAndScriptFailure(["var x = 1 ?'one' : 'two'"], msg, 1)
174  call CheckDefAndScriptFailure(["var x = 1?'one' : 'two'"], msg, 1)
175
176  let msg = "White space required before and after ':'"
177  call CheckDefAndScriptFailure(["var x = 1 ? 'one': 'two'"], msg, 1)
178  call CheckDefAndScriptFailure(["var x = 1 ? 'one' :'two'"], msg, 1)
179  call CheckDefAndScriptFailure(["var x = 1 ? 'one':'two'"], msg, 1)
180
181  call CheckDefAndScriptFailure(["var x = 'x' ? 'one' : 'two'"], 'E1135:', 1)
182  call CheckDefAndScriptFailure(["var x = 0z1234 ? 'one' : 'two'"], 'E974:', 1)
183  call CheckDefExecAndScriptFailure(["var x = [] ? 'one' : 'two'"], 'E745:', 1)
184  call CheckDefExecAndScriptFailure(["var x = {} ? 'one' : 'two'"], 'E728:', 1)
185
186  call CheckDefExecFailure(["var x = false ? "], 'E1097:', 3)
187  call CheckScriptFailure(['vim9script', "var x = false ? "], 'E15:', 2)
188  call CheckDefExecFailure(["var x = false ? 'one' : "], 'E1097:', 3)
189  call CheckScriptFailure(['vim9script', "var x = false ? 'one' : "], 'E15:', 2)
190
191  call CheckDefExecAndScriptFailure2(["var x = true ? xxx : 'foo'"], 'E1001:', 'E121:', 1)
192  call CheckDefExecAndScriptFailure2(["var x = false ? 'foo' : xxx"], 'E1001:', 'E121:', 1)
193
194  if has('float')
195    call CheckDefAndScriptFailure(["var x = 0.1 ? 'one' : 'two'"], 'E805:', 1)
196  endif
197
198  " missing argument detected even when common type is used
199  call CheckDefAndScriptFailure([
200	\ 'var X = FuncOne',
201	\ 'var Y = FuncTwo',
202	\ 'var Z = g:cond ? FuncOne : FuncTwo',
203	\ 'Z()'], 'E119:', 4)
204endfunc
205
206def Test_expr1_falsy()
207  var lines =<< trim END
208      assert_equal(v:true, v:true ?? 456)
209      assert_equal(123, 123 ?? 456)
210      assert_equal('yes', 'yes' ?? 456)
211      assert_equal([1], [1] ?? 456)
212      assert_equal({one: 1}, {one: 1} ?? 456)
213      if has('float')
214        assert_equal(0.1, 0.1 ?? 456)
215      endif
216
217      assert_equal(456, v:false ?? 456)
218      assert_equal(456, 0 ?? 456)
219      assert_equal(456, '' ?? 456)
220      assert_equal(456, [] ?? 456)
221      assert_equal(456, {} ?? 456)
222      if has('float')
223        assert_equal(456, 0.0 ?? 456)
224      endif
225  END
226  CheckDefAndScriptSuccess(lines)
227
228  var msg = "White space required before and after '??'"
229  call CheckDefAndScriptFailure(["var x = 1?? 'one' : 'two'"], msg, 1)
230  call CheckDefAndScriptFailure(["var x = 1 ??'one' : 'two'"], msg, 1)
231  call CheckDefAndScriptFailure(["var x = 1??'one' : 'two'"], msg, 1)
232enddef
233
234def Record(val: any): any
235  g:vals->add(val)
236  return val
237enddef
238
239" test ||
240def Test_expr2()
241  var lines =<< trim END
242      assert_equal(true, 1 || 0)
243      assert_equal(true, 0 ||
244                        0 ||
245                        1)
246      assert_equal(true, 0 ||
247			0 ||
248			!!7)
249      assert_equal(false, 0 || 0)
250      assert_equal(false, 0
251                        || 0)
252      assert_equal(false, 0 || false)
253
254      g:vals = []
255      assert_equal(true, Record(1) || Record(3))
256      assert_equal([1], g:vals)
257
258      g:vals = []
259      assert_equal(true, Record(0) || Record(1))
260      assert_equal([0, 1], g:vals)
261
262      g:vals = []
263      assert_equal(true, Record(0) || Record(true))
264      assert_equal([0, true], g:vals)
265
266      g:vals = []
267      assert_equal(true, Record(0)
268                          || Record(1)
269                          || Record(0))
270      assert_equal([0, 1], g:vals)
271
272      g:vals = []
273      assert_equal(true, Record(0)
274			  || Record(true)
275			  || Record(0))
276      assert_equal([0, true], g:vals)
277
278      g:vals = []
279      assert_equal(true, Record(true) || Record(false))
280      assert_equal([true], g:vals)
281
282      g:vals = []
283      assert_equal(false, Record(0) || Record(false) || Record(0))
284      assert_equal([0, false, 0], g:vals)
285
286      g:vals = []
287      var x = 1
288      if x || true
289        g:vals = [1]
290      endif
291      assert_equal([1], g:vals)
292
293      g:vals = []
294      x = 3
295      if true || x
296        g:vals = [1]
297      endif
298      assert_equal([1], g:vals)
299  END
300  CheckDefAndScriptSuccess(lines)
301enddef
302
303def Test_expr2_vimscript()
304  # check line continuation
305  var lines =<< trim END
306      var name = 0
307      		|| 1
308      assert_equal(true, name)
309  END
310  CheckDefAndScriptSuccess(lines)
311
312  lines =<< trim END
313      var name = v:false
314      		|| v:true
315      		|| v:false
316      assert_equal(v:true, name)
317  END
318  CheckDefAndScriptSuccess(lines)
319
320  lines =<< trim END
321      var name = v:false ||
322      		v:true ||
323		v:false
324      assert_equal(v:true, name)
325  END
326  CheckDefAndScriptSuccess(lines)
327
328  lines =<< trim END
329      var name = v:false || # comment
330                # comment
331      		v:true ||
332                # comment
333		v:false # comment
334      assert_equal(v:true, name)
335  END
336  CheckDefAndScriptSuccess(lines)
337
338  # check white space
339  lines =<< trim END
340      var name = v:true||v:true
341  END
342  CheckDefExecAndScriptFailure(lines, 'E1004: White space required before and after ''||'' at "||v:true"', 1)
343
344  lines =<< trim END
345      var name = v:true ||v:true
346  END
347  CheckDefAndScriptFailure(lines, 'E1004:', 1)
348
349  lines =<< trim END
350      var name = v:true|| v:true
351  END
352  CheckDefAndScriptFailure(lines, 'E1004:', 1)
353enddef
354
355def Test_expr2_fails()
356  var msg = "White space required before and after '||'"
357  call CheckDefAndScriptFailure(["var x = 1||2"], msg, 1)
358  call CheckDefAndScriptFailure(["var x = 1 ||2"], msg, 1)
359  call CheckDefAndScriptFailure(["var x = 1|| 2"], msg, 1)
360
361  call CheckDefFailure(["var x = false || "], 'E1097:', 3)
362  call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2)
363
364  # script does not fail, the second expression is skipped
365  call CheckDefFailure(["var x = 1 || xxx"], 'E1001:', 1)
366
367  call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012:', 'E745:', 1)
368
369  call CheckDefAndScriptFailure2(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1)
370
371  # TODO: should fail at compile time
372  call CheckDefExecAndScriptFailure(["var x = 3 || 7"], 'E1023:', 1)
373
374  call CheckDefAndScriptFailure(["if 3"], 'E1023:', 1)
375  call CheckDefExecAndScriptFailure(['var x = 3', 'if x', 'endif'], 'E1023:', 2)
376
377  call CheckDefAndScriptFailure2(["var x = [] || false"], 'E1012: Type mismatch; expected bool but got list<unknown>', 'E745:', 1)
378
379enddef
380
381" test &&
382def Test_expr3()
383  var lines =<< trim END
384      assert_equal(false, 1 && 0)
385      assert_equal(false, 0 &&
386                    0 &&
387                    1)
388      assert_equal(true, 1
389                        && true
390                        && 1)
391      assert_equal(false, 0 && 0)
392      assert_equal(false, 0 && false)
393      assert_equal(true, 1 && true)
394
395      g:vals = []
396      assert_equal(true, Record(true) && Record(1))
397      assert_equal([true, 1], g:vals)
398
399      g:vals = []
400      assert_equal(true, Record(1) && Record(true))
401      assert_equal([1, true], g:vals)
402
403      g:vals = []
404      assert_equal(false, Record(0) && Record(1))
405      assert_equal([0], g:vals)
406
407      g:vals = []
408      assert_equal(false, Record(0) && Record(1) && Record(0))
409      assert_equal([0], g:vals)
410
411      g:vals = []
412      assert_equal(false, Record(0) && Record(4) && Record(0))
413      assert_equal([0], g:vals)
414
415      g:vals = []
416      assert_equal(false, Record(1) && Record(true) && Record(0))
417      assert_equal([1, true, 0], g:vals)
418  END
419  CheckDefAndScriptSuccess(lines)
420enddef
421
422def Test_expr3_vimscript()
423  # check line continuation
424  var lines =<< trim END
425      var name = 0
426      		&& 1
427      assert_equal(false, name)
428  END
429  CheckDefAndScriptSuccess(lines)
430
431  lines =<< trim END
432      var name = v:true
433      		&& v:true
434      		&& v:true
435      assert_equal(v:true, name)
436  END
437  CheckDefAndScriptSuccess(lines)
438
439  lines =<< trim END
440      var name = v:true &&
441      		v:true &&
442      		v:true
443      assert_equal(v:true, name)
444  END
445  CheckDefAndScriptSuccess(lines)
446
447  lines =<< trim END
448      var name = v:true &&  # comment
449                # comment
450      		v:true &&
451                # comment
452      		v:true
453      assert_equal(v:true, name)
454  END
455  CheckDefAndScriptSuccess(lines)
456
457  # check white space
458  lines =<< trim END
459      var name = v:true&&v:true
460  END
461  CheckDefAndScriptFailure(lines, 'E1004:', 1)
462
463  lines =<< trim END
464      var name = v:true &&v:true
465  END
466  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&v:true"', 1)
467
468  lines =<< trim END
469      var name = v:true&& v:true
470  END
471  CheckDefAndScriptFailure(lines, 'E1004:', 1)
472enddef
473
474def Test_expr3_fails()
475  var msg = "White space required before and after '&&'"
476  CheckDefAndScriptFailure(["var x = 1&&2"], msg, 1)
477  CheckDefAndScriptFailure(["var x = 1 &&2"], msg, 1)
478  CheckDefAndScriptFailure(["var x = 1&& 2"], msg, 1)
479
480  g:vals = []
481  CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1)
482
483  CheckDefExecAndScriptFailure(['assert_equal(false, Record(1) && Record(4) && Record(0))'], 'E1023: Using a Number as a Bool: 4', 1)
484
485  var lines =<< trim END
486      if 3
487          && true
488      endif
489  END
490  CheckDefExecAndScriptFailure(lines, 'E1023:', 1)
491
492  lines =<< trim END
493      if 'yes'
494          && true
495      endif
496  END
497  CheckDefAndScriptFailure2(lines, 'E1012:', 'E1135: Using a String as a Bool', 1)
498enddef
499
500" global variables to use for tests with the "any" type
501let atrue = v:true
502let afalse = v:false
503let anone = v:none
504let anull = v:null
505let anint = 10
506let theone = 1
507let thefour = 4
508if has('float')
509  let afloat = 0.1
510endif
511let astring = 'asdf'
512let ablob = 0z01ab
513let alist = [2, 3, 4]
514let adict = #{aaa: 2, bbb: 8}
515
516" test == comperator
517def Test_expr4_equal()
518  var lines =<< trim END
519      var trueVar = true
520      var falseVar = false
521      assert_equal(true, true == true)
522      assert_equal(false, true ==
523                            false)
524      assert_equal(true, true
525                            == trueVar)
526      assert_equal(false, true == falseVar)
527      assert_equal(true, true == g:atrue)
528      assert_equal(false, g:atrue == false)
529
530      assert_equal(true, v:none == v:none)
531      assert_equal(false, v:none == v:null)
532      assert_equal(true, g:anone == v:none)
533      assert_equal(true, null == v:null)
534      assert_equal(true, null == g:anull)
535      assert_equal(false, v:none == g:anull)
536
537      var nr0 = 0
538      var nr61 = 61
539      assert_equal(false, 2 == 0)
540      assert_equal(false, 2 == nr0)
541      assert_equal(true, 61 == 61)
542      assert_equal(true, 61 == nr61)
543      assert_equal(true, g:anint == 10)
544      assert_equal(false, 61 == g:anint)
545
546      if has('float')
547        var ff = 0.3
548        assert_equal(true, ff == 0.3)
549        assert_equal(false, 0.4 == ff)
550        assert_equal(true, 0.1 == g:afloat)
551        assert_equal(false, g:afloat == 0.3)
552
553        ff = 3.0
554        assert_equal(true, ff == 3)
555        assert_equal(true, 3 == ff)
556        ff = 3.1
557        assert_equal(false, ff == 3)
558        assert_equal(false, 3 == ff)
559      endif
560
561      assert_equal(true, 'abc' == 'abc')
562      assert_equal(false, 'xyz' == 'abc')
563      assert_equal(true, g:astring == 'asdf')
564      assert_equal(false, 'xyz' == g:astring)
565
566      assert_equal(false, 'abc' == 'aBc')
567      assert_equal(false, 'abc' ==# 'aBc')
568      assert_equal(true, 'abc' ==? 'aBc')
569
570      assert_equal(false, 'abc' == 'ABC')
571      set ignorecase
572      assert_equal(false, 'abc' == 'ABC')
573      assert_equal(false, 'abc' ==# 'ABC')
574      assert_equal(true, 'abc' ==? 'ABC')
575      set noignorecase
576
577      var bb = 0z3f
578      assert_equal(true, 0z3f == bb)
579      assert_equal(false, bb == 0z4f)
580      assert_equal(true, g:ablob == 0z01ab)
581      assert_equal(false, 0z3f == g:ablob)
582
583      assert_equal(true, [1, 2, 3] == [1, 2, 3])
584      assert_equal(false, [1, 2, 3] == [2, 3, 1])
585      assert_equal(true, [2, 3, 4] == g:alist)
586      assert_equal(false, g:alist == [2, 3, 1])
587      assert_equal(false, [1, 2, 3] == [])
588      assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
589
590      assert_equal(true, {one: 1, two: 2} == {one: 1, two: 2})
591      assert_equal(false, {one: 1, two: 2} == {one: 2, two: 2})
592      assert_equal(false, {one: 1, two: 2} == {two: 2})
593      assert_equal(false, {one: 1, two: 2} == {})
594      assert_equal(true, g:adict == {bbb: 8, aaa: 2})
595      assert_equal(false, {ccc: 9, aaa: 2} == g:adict)
596
597      assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal'))
598      assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is'))
599
600      assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123]))
601      assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123]))
602      assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999]))
603
604      # TODO: this unexpectedly sometimes fails on Appveyor
605      if !has('win32')
606        var OneFunc: func
607        var TwoFunc: func
608        OneFunc = function('len')
609        TwoFunc = function('len')
610        assert_equal(true, OneFunc('abc') == TwoFunc('123'))
611      endif
612  END
613  CheckDefAndScriptSuccess(lines)
614
615  CheckDefAndScriptFailure2(["var x = 'a' == xxx"], 'E1001:', 'E121:', 1)
616  CheckDefFailure(["var x = 'a' == "], 'E1097:', 3)
617  CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2)
618
619  CheckDefExecAndScriptFailure2(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4)
620
621  CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2)
622  CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2)
623  CheckDefExecAndScriptFailure2(["var x: any = 99", 'echo x == true'], 'E1138', 'E1072:', 2)
624  CheckDefExecAndScriptFailure2(["var x: any = 'a'", 'echo x == 99'], 'E1030:', 'E1072:', 2)
625
626  for op in ['>', '>=', '<', '<=', '=~', '!~']
627    CheckDefExecAndScriptFailure([
628        "var a: any = 'a'",
629        'var b: any = true',
630        'echo a ' .. op .. ' b'], 'E1072:', 3)
631  endfor
632enddef
633
634" test != comperator
635def Test_expr4_notequal()
636  var lines =<< trim END
637      var trueVar = true
638      var falseVar = false
639      assert_equal(false, true != true)
640      assert_equal(true, true !=
641                            false)
642      assert_equal(false, true
643                            != trueVar)
644      assert_equal(true, true != falseVar)
645      assert_equal(false, true != g:atrue)
646      assert_equal(true, g:atrue != false)
647
648      assert_equal(false, v:none != v:none)
649      assert_equal(true, v:none != v:null)
650      assert_equal(false, g:anone != v:none)
651      assert_equal(true, v:none != g:anull)
652
653      var nr55 = 55
654      var nr0 = 55
655      assert_equal(true, 2 != 0)
656      assert_equal(true, 2 != nr0)
657      assert_equal(false, 55 != 55)
658      assert_equal(false, 55 != nr55)
659      assert_equal(false, g:anint != 10)
660      assert_equal(true, 61 != g:anint)
661
662      if has('float')
663        var ff = 0.3
664        assert_equal(false, 0.3 != ff)
665        assert_equal(true, 0.4 != ff)
666        assert_equal(false, 0.1 != g:afloat)
667        assert_equal(true, g:afloat != 0.3)
668
669        ff = 3.0
670        assert_equal(false, ff != 3)
671        assert_equal(false, 3 != ff)
672        ff = 3.1
673        assert_equal(true, ff != 3)
674        assert_equal(true, 3 != ff)
675      endif
676
677      assert_equal(false, 'abc' != 'abc')
678      assert_equal(true, 'xyz' != 'abc')
679      assert_equal(false, g:astring != 'asdf')
680      assert_equal(true, 'xyz' != g:astring)
681
682      assert_equal(true, 'abc' != 'ABC')
683      set ignorecase
684      assert_equal(true, 'abc' != 'ABC')
685      assert_equal(true, 'abc' !=# 'ABC')
686      assert_equal(false, 'abc' !=? 'ABC')
687      set noignorecase
688
689      var bb = 0z3f
690      assert_equal(false, 0z3f != bb)
691      assert_equal(true, bb != 0z4f)
692      assert_equal(false, g:ablob != 0z01ab)
693      assert_equal(true, 0z3f != g:ablob)
694
695      assert_equal(false, [1, 2, 3] != [1, 2, 3])
696      assert_equal(true, [1, 2, 3] != [2, 3, 1])
697      assert_equal(false, [2, 3, 4] != g:alist)
698      assert_equal(true, g:alist != [2, 3, 1])
699      assert_equal(true, [1, 2, 3] != [])
700      assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
701
702      assert_equal(false, {one: 1, two: 2} != {one: 1, two: 2})
703      assert_equal(true, {one: 1, two: 2} != {one: 2, two: 2})
704      assert_equal(true, {one: 1, two: 2} != {two: 2})
705      assert_equal(true, {one: 1, two: 2} != {})
706      assert_equal(false, g:adict != {bbb: 8, aaa: 2})
707      assert_equal(true, {ccc: 9, aaa: 2} != g:adict)
708
709      assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal'))
710      assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is'))
711
712      assert_equal(false, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [123]))
713      assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_is', [123]))
714      assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [999]))
715  END
716  CheckDefAndScriptSuccess(lines)
717enddef
718
719" test > comperator
720def Test_expr4_greater()
721  var lines =<< trim END
722      assert_true(2 > 0)
723      assert_true(2 >
724                    1)
725      assert_false(2 > 2)
726      assert_false(2 > 3)
727      var nr2 = 2
728      assert_true(nr2 > 0)
729      assert_true(nr2 >
730                    1)
731      assert_false(nr2 > 2)
732      assert_false(nr2
733                        > 3)
734      if has('float')
735        var ff = 2.0
736        assert_true(ff > 0.0)
737        assert_true(ff > 1.0)
738        assert_false(ff > 2.0)
739        assert_false(ff > 3.0)
740      endif
741  END
742  CheckDefAndScriptSuccess(lines)
743enddef
744
745" test >= comperator
746def Test_expr4_greaterequal()
747  var lines =<< trim END
748      assert_true(2 >= 0)
749      assert_true(2 >=
750                            2)
751      assert_false(2 >= 3)
752      var nr2 = 2
753      assert_true(nr2 >= 0)
754      assert_true(nr2 >= 2)
755      assert_false(nr2 >= 3)
756      if has('float')
757        var ff = 2.0
758        assert_true(ff >= 0.0)
759        assert_true(ff >= 2.0)
760        assert_false(ff >= 3.0)
761      endif
762  END
763  CheckDefAndScriptSuccess(lines)
764enddef
765
766" test < comperator
767def Test_expr4_smaller()
768  var lines =<< trim END
769      assert_false(2 < 0)
770      assert_false(2 <
771                            2)
772      assert_true(2
773                    < 3)
774      var nr2 = 2
775      assert_false(nr2 < 0)
776      assert_false(nr2 < 2)
777      assert_true(nr2 < 3)
778      if has('float')
779        var ff = 2.0
780        assert_false(ff < 0.0)
781        assert_false(ff < 2.0)
782        assert_true(ff < 3.0)
783      endif
784  END
785  CheckDefAndScriptSuccess(lines)
786enddef
787
788" test <= comperator
789def Test_expr4_smallerequal()
790  var lines =<< trim END
791      assert_false(2 <= 0)
792      assert_false(2 <=
793                            1)
794      assert_true(2
795                    <= 2)
796      assert_true(2 <= 3)
797      var nr2 = 2
798      assert_false(nr2 <= 0)
799      assert_false(nr2 <= 1)
800      assert_true(nr2 <= 2)
801      assert_true(nr2 <= 3)
802      if has('float')
803        var ff = 2.0
804        assert_false(ff <= 0.0)
805        assert_false(ff <= 1.0)
806        assert_true(ff <= 2.0)
807        assert_true(ff <= 3.0)
808      endif
809  END
810  CheckDefAndScriptSuccess(lines)
811enddef
812
813" test =~ comperator
814def Test_expr4_match()
815  var lines =<< trim END
816      assert_equal(false, '2' =~ '0')
817      assert_equal(false, ''
818                             =~ '0')
819      assert_equal(true, '2' =~
820                            '[0-9]')
821      set ignorecase
822      assert_equal(false, 'abc' =~ 'ABC')
823      assert_equal(false, 'abc' =~# 'ABC')
824      assert_equal(true, 'abc' =~? 'ABC')
825      set noignorecase
826  END
827  CheckDefAndScriptSuccess(lines)
828enddef
829
830" test !~ comperator
831def Test_expr4_nomatch()
832  var lines =<< trim END
833      assert_equal(true, '2' !~ '0')
834      assert_equal(true, ''
835                            !~ '0')
836      assert_equal(false, '2' !~
837                            '[0-9]')
838  END
839  CheckDefAndScriptSuccess(lines)
840enddef
841
842" test is comperator
843def Test_expr4_is()
844  var lines =<< trim END
845      var mylist = [2]
846      assert_false(mylist is [2])
847      var other = mylist
848      assert_true(mylist is
849                    other)
850
851      var myblob = 0z1234
852      assert_false(myblob
853                            is 0z1234)
854      var otherblob = myblob
855      assert_true(myblob is otherblob)
856  END
857  CheckDefAndScriptSuccess(lines)
858enddef
859
860" test isnot comperator
861def Test_expr4_isnot()
862  var lines =<< trim END
863      var mylist = [2]
864      assert_true('2' isnot '0')
865      assert_true(mylist isnot [2])
866      var other = mylist
867      assert_false(mylist isnot
868                            other)
869
870      var myblob = 0z1234
871      assert_true(myblob
872                    isnot 0z1234)
873      var otherblob = myblob
874      assert_false(myblob isnot otherblob)
875  END
876  CheckDefAndScriptSuccess(lines)
877enddef
878
879def RetVoid()
880  var x = 1
881enddef
882
883def Test_expr4_vim9script()
884  # check line continuation
885  var lines =<< trim END
886      var name = 0
887      		< 1
888      assert_equal(true, name)
889  END
890  CheckDefAndScriptSuccess(lines)
891
892  lines =<< trim END
893      var name = 123
894                # comment
895      		!= 123
896      assert_equal(false, name)
897  END
898  CheckDefAndScriptSuccess(lines)
899
900  lines =<< trim END
901      var name = 123 ==
902      			123
903      assert_equal(true, name)
904  END
905  CheckDefAndScriptSuccess(lines)
906
907  lines =<< trim END
908      var list = [1, 2, 3]
909      var name = list
910      		is list
911      assert_equal(true, name)
912  END
913  CheckDefAndScriptSuccess(lines)
914
915  lines =<< trim END
916      var list = [1, 2, 3]
917      var name = list # comment
918                 # comment
919      		is list
920      assert_equal(true, name)
921  END
922  CheckDefAndScriptSuccess(lines)
923
924  lines =<< trim END
925      var myblob = 0z1234
926      var name = myblob
927      		isnot 0z11
928      assert_equal(true, name)
929  END
930  CheckDefAndScriptSuccess(lines)
931
932  # spot check mismatching types
933  lines =<< trim END
934      echo '' == 0
935  END
936  CheckDefAndScriptFailure(lines, 'E1072:', 1)
937
938  lines =<< trim END
939      echo v:true > v:false
940  END
941  CheckDefAndScriptFailure(lines, 'Cannot compare bool with bool', 1)
942
943  lines =<< trim END
944      echo 123 is 123
945  END
946  CheckDefAndScriptFailure(lines, 'Cannot use "is" with number', 1)
947
948  # check missing white space
949  lines =<< trim END
950    echo 2>3
951  END
952  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''>'' at ">3"', 1)
953
954  lines =<< trim END
955    echo 2 >3
956  END
957  CheckDefAndScriptFailure(lines, 'E1004:', 1)
958
959  lines =<< trim END
960    echo 2> 3
961  END
962  CheckDefAndScriptFailure(lines, 'E1004:', 1)
963
964  lines =<< trim END
965    echo 2!=3
966  END
967  CheckDefAndScriptFailure(lines, 'E1004:', 1)
968
969  lines =<< trim END
970    echo 2 !=3
971  END
972  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''!='' at "!=3"', 1)
973
974  lines =<< trim END
975    echo 2!= 3
976  END
977  CheckDefAndScriptFailure(lines, 'E1004:', 1)
978
979  lines =<< trim END
980    echo len('xxx') == 3
981  END
982  CheckDefAndScriptSuccess(lines)
983
984  lines =<< trim END
985    var line = 'abc'
986    echo line[1] =~ '\w'
987  END
988  CheckDefAndScriptSuccess(lines)
989enddef
990
991func Test_expr4_fails()
992  let msg = "White space required before and after '>'"
993  call CheckDefAndScriptFailure(["var x = 1>2"], msg, 1)
994  call CheckDefAndScriptFailure(["var x = 1 >2"], msg, 1)
995  call CheckDefAndScriptFailure(["var x = 1> 2"], msg, 1)
996
997  let msg = "White space required before and after '=='"
998  call CheckDefAndScriptFailure(["var x = 1==2"], msg, 1)
999  call CheckDefAndScriptFailure(["var x = 1 ==2"], msg, 1)
1000  call CheckDefAndScriptFailure(["var x = 1== 2"], msg, 1)
1001
1002  let msg = "White space required before and after 'is'"
1003  call CheckDefAndScriptFailure(["var x = '1'is'2'"], msg, 1)
1004  call CheckDefAndScriptFailure(["var x = '1' is'2'"], msg, 1)
1005  call CheckDefAndScriptFailure(["var x = '1'is '2'"], msg, 1)
1006
1007  let msg = "White space required before and after 'isnot'"
1008  call CheckDefAndScriptFailure(["var x = '1'isnot'2'"], msg, 1)
1009  call CheckDefAndScriptFailure(["var x = '1' isnot'2'"], msg, 1)
1010  call CheckDefAndScriptFailure(["var x = '1'isnot '2'"], msg, 1)
1011
1012  call CheckDefAndScriptFailure(["var x = 1 is# 2"], 'E15:', 1)
1013  call CheckDefAndScriptFailure(["var x = 1 is? 2"], 'E15:', 1)
1014  call CheckDefAndScriptFailure(["var x = 1 isnot# 2"], 'E15:', 1)
1015  call CheckDefAndScriptFailure(["var x = 1 isnot? 2"], 'E15:', 1)
1016
1017  call CheckDefAndScriptFailure(["var x = 1 == '2'"], 'Cannot compare number with string', 1)
1018  call CheckDefAndScriptFailure(["var x = '1' == 2"], 'Cannot compare string with number', 1)
1019  call CheckDefAndScriptFailure(["var x = 1 == RetVoid()"], 'Cannot compare number with void', 1)
1020  call CheckDefAndScriptFailure(["var x = RetVoid() == 1"], 'Cannot compare void with number', 1)
1021
1022  call CheckDefAndScriptFailure(["var x = true > false"], 'Cannot compare bool with bool', 1)
1023  call CheckDefAndScriptFailure(["var x = true >= false"], 'Cannot compare bool with bool', 1)
1024  call CheckDefAndScriptFailure(["var x = true < false"], 'Cannot compare bool with bool', 1)
1025  call CheckDefAndScriptFailure(["var x = true <= false"], 'Cannot compare bool with bool', 1)
1026  call CheckDefAndScriptFailure(["var x = true =~ false"], 'Cannot compare bool with bool', 1)
1027  call CheckDefAndScriptFailure(["var x = true !~ false"], 'Cannot compare bool with bool', 1)
1028  call CheckDefAndScriptFailure(["var x = true is false"], 'Cannot use "is" with bool', 1)
1029  call CheckDefAndScriptFailure(["var x = true isnot false"], 'Cannot use "isnot" with bool', 1)
1030
1031  call CheckDefAndScriptFailure(["var x = v:none is v:null"], 'Cannot use "is" with special', 1)
1032  call CheckDefAndScriptFailure(["var x = v:none isnot v:null"], 'Cannot use "isnot" with special', 1)
1033  call CheckDefAndScriptFailure(["var x = 123 is 123"], 'Cannot use "is" with number', 1)
1034  call CheckDefAndScriptFailure(["var x = 123 isnot 123"], 'Cannot use "isnot" with number', 1)
1035  if has('float')
1036    call CheckDefAndScriptFailure(["var x = 1.3 is 1.3"], 'Cannot use "is" with float', 1)
1037    call CheckDefAndScriptFailure(["var x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float', 1)
1038  endif
1039
1040  call CheckDefAndScriptFailure(["var x = 0za1 > 0z34"], 'Cannot compare blob with blob', 1)
1041  call CheckDefAndScriptFailure(["var x = 0za1 >= 0z34"], 'Cannot compare blob with blob', 1)
1042  call CheckDefAndScriptFailure(["var x = 0za1 < 0z34"], 'Cannot compare blob with blob', 1)
1043  call CheckDefAndScriptFailure(["var x = 0za1 <= 0z34"], 'Cannot compare blob with blob', 1)
1044  call CheckDefAndScriptFailure(["var x = 0za1 =~ 0z34"], 'Cannot compare blob with blob', 1)
1045  call CheckDefAndScriptFailure(["var x = 0za1 !~ 0z34"], 'Cannot compare blob with blob', 1)
1046
1047  call CheckDefAndScriptFailure(["var x = [13] > [88]"], 'Cannot compare list with list', 1)
1048  call CheckDefAndScriptFailure(["var x = [13] >= [88]"], 'Cannot compare list with list', 1)
1049  call CheckDefAndScriptFailure(["var x = [13] < [88]"], 'Cannot compare list with list', 1)
1050  call CheckDefAndScriptFailure(["var x = [13] <= [88]"], 'Cannot compare list with list', 1)
1051  call CheckDefAndScriptFailure(["var x = [13] =~ [88]"], 'Cannot compare list with list', 1)
1052  call CheckDefAndScriptFailure(["var x = [13] !~ [88]"], 'Cannot compare list with list', 1)
1053
1054  call CheckDefAndScriptFailure(['var j: job', 'var chan: channel', 'var r = j == chan'], 'Cannot compare job with channel', 3)
1055  call CheckDefAndScriptFailure(['var j: job', 'var x: list<any>', 'var r = j == x'], 'Cannot compare job with list', 3)
1056  call CheckDefAndScriptFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3)
1057  call CheckDefAndScriptFailure(['var j: job', 'var Xx: func', 'var r = j == Xx'], 'Cannot compare job with func', 3)
1058endfunc
1059
1060" test addition, subtraction, concatenation
1061def Test_expr5()
1062  var lines =<< trim END
1063      assert_equal(66, 60 + 6)
1064      assert_equal(70, 60 +
1065                            g:anint)
1066      assert_equal(9, g:thefour
1067                            + 5)
1068      assert_equal(14, g:thefour + g:anint)
1069      assert_equal([1, 2, 3, 4], [1] + g:alist)
1070
1071      assert_equal(54, 60 - 6)
1072      assert_equal(50, 60 -
1073                        g:anint)
1074      assert_equal(-1, g:thefour
1075                            - 5)
1076      assert_equal(-6, g:thefour - g:anint)
1077
1078      assert_equal('hello', 'hel' .. 'lo')
1079      assert_equal('hello 123', 'hello ' ..
1080                                            123)
1081      assert_equal('hello 123', 'hello '
1082                                    ..  123)
1083      assert_equal('123 hello', 123 .. ' hello')
1084      assert_equal('123456', 123 .. 456)
1085
1086      assert_equal('atrue', 'a' .. true)
1087      assert_equal('afalse', 'a' .. false)
1088      assert_equal('anull', 'a' .. v:null)
1089      assert_equal('av:none', 'a' .. v:none)
1090      if has('float')
1091        assert_equal('a0.123', 'a' .. 0.123)
1092      endif
1093
1094      assert_equal(3, 1 + [2, 3, 4][0])
1095      assert_equal(5, 2 + {key: 3}['key'])
1096
1097      set digraph
1098      assert_equal('val: true', 'val: ' .. &digraph)
1099      set nodigraph
1100      assert_equal('val: false', 'val: ' .. &digraph)
1101
1102      assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
1103      assert_equal(0z11223344, 0z1122 + 0z3344)
1104      assert_equal(0z112201ab, 0z1122
1105                                    + g:ablob)
1106      assert_equal(0z01ab3344, g:ablob + 0z3344)
1107      assert_equal(0z01ab01ab, g:ablob + g:ablob)
1108
1109      # concatenate non-constant to constant
1110      var save_path = &path
1111      &path = 'b'
1112      assert_equal('ab', 'a' .. &path)
1113      &path = save_path
1114
1115      @b = 'b'
1116      assert_equal('ab', 'a' .. @b)
1117
1118      $ENVVAR = 'env'
1119      assert_equal('aenv', 'a' .. $ENVVAR)
1120
1121      assert_equal('val', '' .. {key: 'val'}['key'])
1122  END
1123  CheckDefAndScriptSuccess(lines)
1124enddef
1125
1126def Test_expr5_vim9script()
1127  # check line continuation
1128  var lines =<< trim END
1129      var name = 11
1130      		+ 77
1131		- 22
1132      assert_equal(66, name)
1133  END
1134  CheckDefAndScriptSuccess(lines)
1135
1136  lines =<< trim END
1137      var name = 11 +
1138		  77 -
1139		  22
1140      assert_equal(66, name)
1141  END
1142  CheckDefAndScriptSuccess(lines)
1143
1144  lines =<< trim END
1145      var name = 11 +  # comment
1146		  77 -
1147                  # comment
1148		  22
1149      assert_equal(66, name)
1150  END
1151  CheckDefAndScriptSuccess(lines)
1152
1153  lines =<< trim END
1154      var name = 'one'
1155      		.. 'two'
1156      assert_equal('onetwo', name)
1157  END
1158  CheckDefAndScriptSuccess(lines)
1159
1160  lines =<< trim END
1161      echo 'abc' is# 'abc'
1162  END
1163  CheckDefAndScriptFailure(lines, 'E15:', 1)
1164
1165  lines =<< trim END
1166      echo {} - 22
1167  END
1168  CheckDefAndScriptFailure2(lines, 'E1036:', 'E728:', 1)
1169
1170  lines =<< trim END
1171      echo [] - 33
1172  END
1173  CheckDefAndScriptFailure2(lines, 'E1036:', 'E745:', 1)
1174
1175  lines =<< trim END
1176      echo 0z1234 - 44
1177  END
1178  CheckDefAndScriptFailure2(lines, 'E1036', 'E974:', 1)
1179
1180  lines =<< trim END
1181      echo 'abc' is? 'abc'
1182  END
1183  CheckDefAndScriptFailure(lines, 'E15:', 1)
1184
1185  lines =<< trim END
1186      echo 'abc' isnot# 'abc'
1187  END
1188  CheckDefAndScriptFailure(lines, 'E15:', 1)
1189
1190  lines =<< trim END
1191      echo 'abc' isnot? 'abc'
1192  END
1193  CheckDefAndScriptFailure(lines, 'E15:', 1)
1194
1195  # check white space
1196  lines =<< trim END
1197      echo 5+6
1198  END
1199  CheckDefAndScriptFailure(lines, 'E1004:', 1)
1200  lines =<< trim END
1201      echo 5 +6
1202  END
1203  CheckDefAndScriptFailure(lines, 'E1004:', 1)
1204
1205  lines =<< trim END
1206      echo 5+ 6
1207  END
1208  CheckDefAndScriptFailure(lines, 'E1004:', 1)
1209
1210  lines =<< trim END
1211      echo 'a'..'b'
1212  END
1213  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at "..''b''"', 1)
1214
1215  lines =<< trim END
1216      echo 'a' ..'b'
1217  END
1218  CheckDefAndScriptFailure(lines, 'E1004:', 1)
1219
1220  lines =<< trim END
1221      echo 'a'.. 'b'
1222  END
1223  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at ".. ''b''"', 1)
1224
1225  lines =<< trim END
1226      echo 'a'
1227          ..'b'
1228      # comment
1229  END
1230  CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''..'' at "..''b''"', 2)
1231
1232  # check invalid string concatenation
1233  lines =<< trim END
1234      echo 'a' .. [1]
1235  END
1236  CheckDefAndScriptFailure2(lines, 'E1105:', 'E730:', 1)
1237
1238  lines =<< trim END
1239      echo 'a' .. {a: 1}
1240  END
1241  CheckDefAndScriptFailure2(lines, 'E1105:', 'E731:', 1)
1242
1243  lines =<< trim END
1244      echo 'a' .. test_void()
1245  END
1246  CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1)
1247
1248  lines =<< trim END
1249      echo 'a' .. 0z33
1250  END
1251  CheckDefAndScriptFailure2(lines, 'E1105:', 'E976:', 1)
1252
1253  lines =<< trim END
1254      echo 'a' .. function('len')
1255  END
1256  CheckDefAndScriptFailure2(lines, 'E1105:', 'E729:', 1)
1257
1258  lines =<< trim END
1259      new
1260      ['']->setline(1)
1261      /pattern
1262
1263      eval 0
1264      bwipe!
1265  END
1266  CheckDefAndScriptFailure(lines, "E1004: White space required before and after '/' at \"/pattern", 3)
1267enddef
1268
1269def Test_expr5_vim9script_channel()
1270  if !has('channel')
1271    MissingFeature 'float'
1272  else
1273    var lines =<< trim END
1274        echo 'a' .. test_null_job()
1275    END
1276    CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1)
1277    lines =<< trim END
1278        echo 'a' .. test_null_channel()
1279    END
1280    CheckDefAndScriptFailure2(lines, 'E1105:', 'E908:', 1)
1281  endif
1282enddef
1283
1284def Test_expr5_float()
1285  if !has('float')
1286    MissingFeature 'float'
1287  else
1288    var lines =<< trim END
1289        assert_equal(66.0, 60.0 + 6.0)
1290        assert_equal(66.0, 60.0 + 6)
1291        assert_equal(66.0, 60 +
1292                             6.0)
1293        assert_equal(5.1, g:afloat
1294                            + 5)
1295        assert_equal(8.1, 8 + g:afloat)
1296        assert_equal(10.1, g:anint + g:afloat)
1297        assert_equal(10.1, g:afloat + g:anint)
1298
1299        assert_equal(54.0, 60.0 - 6.0)
1300        assert_equal(54.0, 60.0
1301                                - 6)
1302        assert_equal(54.0, 60 - 6.0)
1303        assert_equal(-4.9, g:afloat - 5)
1304        assert_equal(7.9, 8 - g:afloat)
1305        assert_equal(9.9, g:anint - g:afloat)
1306        assert_equal(-9.9, g:afloat - g:anint)
1307    END
1308    CheckDefAndScriptSuccess(lines)
1309  endif
1310enddef
1311
1312func Test_expr5_fails()
1313  let msg = "White space required before and after '+'"
1314  call CheckDefAndScriptFailure(["var x = 1+2"], msg, 1)
1315  call CheckDefAndScriptFailure(["var x = 1 +2"], msg, 1)
1316  call CheckDefAndScriptFailure(["var x = 1+ 2"], msg, 1)
1317
1318  let msg = "White space required before and after '-'"
1319  call CheckDefAndScriptFailure(["var x = 1-2"], msg, 1)
1320  call CheckDefAndScriptFailure(["var x = 1 -2"], msg, 1)
1321  call CheckDefAndScriptFailure(["var x = 1- 2"], msg, 1)
1322
1323  let msg = "White space required before and after '..'"
1324  call CheckDefAndScriptFailure(["var x = '1'..'2'"], msg, 1)
1325  call CheckDefAndScriptFailure(["var x = '1' ..'2'"], msg, 1)
1326  call CheckDefAndScriptFailure(["var x = '1'.. '2'"], msg, 1)
1327
1328  call CheckDefAndScriptFailure2(["var x = 0z1122 + 33"], 'E1051:', 'E974:', 1)
1329  call CheckDefAndScriptFailure2(["var x = 0z1122 + [3]"], 'E1051:', 'E974:', 1)
1330  call CheckDefAndScriptFailure2(["var x = 0z1122 + 'asd'"], 'E1051:', 'E974:', 1)
1331  call CheckDefAndScriptFailure2(["var x = 33 + 0z1122"], 'E1051:', 'E974:', 1)
1332  call CheckDefAndScriptFailure2(["var x = [3] + 0z1122"], 'E1051:', 'E745:', 1)
1333  call CheckDefAndScriptFailure2(["var x = 'asdf' + 0z1122"], 'E1051:', 'E1030:', 1)
1334  call CheckDefAndScriptFailure2(["var x = 6 + xxx"], 'E1001:', 'E121:', 1)
1335
1336  call CheckDefAndScriptFailure2(["var x = 'a' .. [1]"], 'E1105:', 'E730:', 1)
1337  call CheckDefAndScriptFailure2(["var x = 'a' .. {a: 1}"], 'E1105:', 'E731:', 1)
1338  call CheckDefAndScriptFailure2(["var x = 'a' .. test_void()"], 'E1105:', 'E908:', 1)
1339  call CheckDefAndScriptFailure2(["var x = 'a' .. 0z32"], 'E1105:', 'E976:', 1)
1340  call CheckDefAndScriptFailure2(["var x = 'a' .. function('len')"], 'E1105:', 'E729:', 1)
1341  call CheckDefAndScriptFailure2(["var x = 'a' .. function('len', ['a'])"], 'E1105:', 'E729:', 1)
1342
1343  call CheckDefAndScriptFailure2(['var x = 1 + v:none'], 'E1051:', 'E611:', 1)
1344  call CheckDefAndScriptFailure2(['var x = 1 + v:null'], 'E1051:', 'E611:', 1)
1345  call CheckDefAndScriptFailure2(['var x = 1 + v:true'], 'E1051:', 'E1138:', 1)
1346  call CheckDefAndScriptFailure2(['var x = 1 + v:false'], 'E1051:', 'E1138:', 1)
1347  call CheckDefAndScriptFailure2(['var x = 1 + true'], 'E1051:', 'E1138:', 1)
1348  call CheckDefAndScriptFailure2(['var x = 1 + false'], 'E1051:', 'E1138:', 1)
1349endfunc
1350
1351func Test_expr5_fails_channel()
1352  CheckFeature channel
1353  call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_job()"], 'E1105:', 'E908:', 1)
1354  call CheckDefAndScriptFailure2(["var x = 'a' .. test_null_channel()"], 'E1105:', 'E908:', 1)
1355endfunc
1356
1357def Test_expr5_list_add()
1358  var lines =<< trim END
1359      # concatenating two lists with same member types is OK
1360      var d = {}
1361      for i in ['a'] + ['b']
1362        d = {[i]: 0}
1363      endfor
1364
1365      # concatenating two lists with different member types results in "any"
1366      var dany = {}
1367      for i in ['a'] + [12]
1368        dany[i] = i
1369      endfor
1370      assert_equal({a: 'a', 12: 12}, dany)
1371
1372      # result of glob() is "any", runtime type check
1373      var sl: list<string> = glob('*.txt', false, true) + ['']
1374  END
1375  CheckDefAndScriptSuccess(lines)
1376enddef
1377
1378" test multiply, divide, modulo
1379def Test_expr6()
1380  var lines =<< trim END
1381      assert_equal(36, 6 * 6)
1382      assert_equal(24, 6 *
1383                            g:thefour)
1384      assert_equal(24, g:thefour
1385                            * 6)
1386      assert_equal(40, g:anint * g:thefour)
1387
1388      assert_equal(10, 60 / 6)
1389      assert_equal(6, 60 /
1390                            g:anint)
1391      assert_equal(1, g:anint / 6)
1392      assert_equal(2, g:anint
1393                            / g:thefour)
1394
1395      assert_equal(5, 11 % 6)
1396      assert_equal(4, g:anint % 6)
1397      assert_equal(3, 13 %
1398                            g:anint)
1399      assert_equal(2, g:anint
1400                            % g:thefour)
1401
1402      assert_equal(4, 6 * 4 / 6)
1403
1404      var x = [2]
1405      var y = [3]
1406      assert_equal(5, x[0] + y[0])
1407      assert_equal(6, x[0] * y[0])
1408      if has('float')
1409        var xf = [2.0]
1410        var yf = [3.0]
1411        assert_equal(5.0, xf[0]
1412                            + yf[0])
1413        assert_equal(6.0, xf[0]
1414                            * yf[0])
1415      endif
1416  END
1417  CheckDefAndScriptSuccess(lines)
1418
1419  CheckDefAndScriptFailure2(["var x = 6 * xxx"], 'E1001:', 'E121:', 1)
1420  CheckDefFailure(["var d = 6 * "], 'E1097:', 3)
1421  CheckScriptFailure(['vim9script', "var d = 6 * "], 'E15:', 2)
1422
1423  CheckDefExecAndScriptFailure(['echo 1 / 0'], 'E1154', 1)
1424  CheckDefExecAndScriptFailure(['echo 1 % 0'], 'E1154', 1)
1425enddef
1426
1427def Test_expr6_vim9script()
1428  # check line continuation
1429  var lines =<< trim END
1430      var name = 11
1431      		* 22
1432		/ 3
1433      assert_equal(80, name)
1434  END
1435  CheckDefAndScriptSuccess(lines)
1436
1437  lines =<< trim END
1438      var name = 25
1439      		% 10
1440      assert_equal(5, name)
1441  END
1442  CheckDefAndScriptSuccess(lines)
1443
1444  lines =<< trim END
1445      var name = 25
1446                # comment
1447
1448                # comment
1449      		% 10
1450      assert_equal(5, name)
1451  END
1452  CheckDefAndScriptSuccess(lines)
1453
1454  lines =<< trim END
1455      var name = 11 *
1456      		22 /
1457		3
1458      assert_equal(80, name)
1459  END
1460  CheckDefAndScriptSuccess(lines)
1461
1462  # check white space
1463  lines =<< trim END
1464      echo 5*6
1465  END
1466  CheckDefAndScriptFailure(lines, 'E1004:', 1)
1467
1468  lines =<< trim END
1469      echo 5 *6
1470  END
1471  CheckDefAndScriptFailure(lines, 'E1004:', 1)
1472
1473  lines =<< trim END
1474      echo 5* 6
1475  END
1476  CheckDefAndScriptFailure(lines, 'E1004:', 1)
1477enddef
1478
1479def Test_expr6_float()
1480  if !has('float')
1481    MissingFeature 'float'
1482  else
1483    var lines =<< trim END
1484        assert_equal(36.0, 6.0 * 6)
1485        assert_equal(36.0, 6 *
1486                               6.0)
1487        assert_equal(36.0, 6.0 * 6.0)
1488        assert_equal(1.0, g:afloat * g:anint)
1489
1490        assert_equal(10.0, 60 / 6.0)
1491        assert_equal(10.0, 60.0 /
1492                            6)
1493        assert_equal(10.0, 60.0 / 6.0)
1494        assert_equal(0.01, g:afloat / g:anint)
1495
1496        assert_equal(4.0, 6.0 * 4 / 6)
1497        assert_equal(4.0, 6 *
1498                            4.0 /
1499                            6)
1500        assert_equal(4.0, 6 * 4 / 6.0)
1501        assert_equal(4.0, 6.0 * 4.0 / 6)
1502        assert_equal(4.0, 6 * 4.0 / 6.0)
1503        assert_equal(4.0, 6.0 * 4 / 6.0)
1504        assert_equal(4.0, 6.0 * 4.0 / 6.0)
1505
1506        assert_equal(4.0, 6.0 * 4.0 / 6.0)
1507    END
1508    CheckDefAndScriptSuccess(lines)
1509  endif
1510enddef
1511
1512func Test_expr6_fails()
1513  let msg = "White space required before and after '*'"
1514  call CheckDefAndScriptFailure(["var x = 1*2"], msg, 1)
1515  call CheckDefAndScriptFailure(["var x = 1 *2"], msg, 1)
1516  call CheckDefAndScriptFailure(["var x = 1* 2"], msg, 1)
1517
1518  let msg = "White space required before and after '/'"
1519  call CheckDefAndScriptFailure(["var x = 1/2"], msg, 1)
1520  call CheckDefAndScriptFailure(["var x = 1 /2"], msg, 1)
1521  call CheckDefAndScriptFailure(["var x = 1/ 2"], msg, 1)
1522
1523  let msg = "White space required before and after '%'"
1524  call CheckDefAndScriptFailure(["var x = 1%2"], msg, 1)
1525  call CheckDefAndScriptFailure(["var x = 1 %2"], msg, 1)
1526  call CheckDefAndScriptFailure(["var x = 1% 2"], msg, 1)
1527
1528  call CheckDefAndScriptFailure2(["var x = '1' * '2'"], 'E1036:', 'E1030:', 1)
1529  call CheckDefAndScriptFailure2(["var x = '1' / '2'"], 'E1036:', 'E1030:', 1)
1530  call CheckDefAndScriptFailure2(["var x = '1' % '2'"], 'E1035:', 'E1030:', 1)
1531
1532  call CheckDefAndScriptFailure2(["var x = 0z01 * 0z12"], 'E1036:', 'E974:', 1)
1533  call CheckDefAndScriptFailure2(["var x = 0z01 / 0z12"], 'E1036:', 'E974:', 1)
1534  call CheckDefAndScriptFailure2(["var x = 0z01 % 0z12"], 'E1035:', 'E974:', 1)
1535
1536  call CheckDefAndScriptFailure2(["var x = [1] * [2]"], 'E1036:', 'E745:', 1)
1537  call CheckDefAndScriptFailure2(["var x = [1] / [2]"], 'E1036:', 'E745:', 1)
1538  call CheckDefAndScriptFailure2(["var x = [1] % [2]"], 'E1035:', 'E745:', 1)
1539
1540  call CheckDefAndScriptFailure2(["var x = {one: 1} * {two: 2}"], 'E1036:', 'E728:', 1)
1541  call CheckDefAndScriptFailure2(["var x = {one: 1} / {two: 2}"], 'E1036:', 'E728:', 1)
1542  call CheckDefAndScriptFailure2(["var x = {one: 1} % {two: 2}"], 'E1035:', 'E728:', 1)
1543
1544  call CheckDefAndScriptFailure2(["var x = 0xff[1]"], 'E1107:', 'E1062:', 1)
1545  if has('float')
1546    call CheckDefAndScriptFailure2(["var x = 0.7[1]"], 'E1107:', 'E806:', 1)
1547  endif
1548endfunc
1549
1550func Test_expr6_float_fails()
1551  CheckFeature float
1552  call CheckDefAndScriptFailure2(["var x = 1.0 % 2"], 'E1035:', 'E804:', 1)
1553endfunc
1554
1555" define here to use old style parsing
1556if has('float')
1557  let g:float_zero = 0.0
1558  let g:float_neg = -9.8
1559  let g:float_big = 9.9e99
1560endif
1561let g:blob_empty = 0z
1562let g:blob_one = 0z01
1563let g:blob_long = 0z0102.0304
1564
1565let g:string_empty = ''
1566let g:string_short = 'x'
1567let g:string_long = 'abcdefghijklm'
1568let g:string_special = "ab\ncd\ref\ekk"
1569
1570let g:special_true = v:true
1571let g:special_false = v:false
1572let g:special_null = v:null
1573let g:special_none = v:none
1574
1575let g:list_empty = []
1576let g:list_mixed = [1, 'b', v:false]
1577
1578let g:dict_empty = {}
1579let g:dict_one = #{one: 1}
1580
1581let $TESTVAR = 'testvar'
1582
1583" type casts
1584def Test_expr7t()
1585  var lines =<< trim END
1586      var ls: list<string> = ['a', <string>g:string_empty]
1587      var ln: list<number> = [<number>g:anint, <number>g:thefour]
1588      var nr = <number>234
1589      assert_equal(234, nr)
1590      var b: bool = <bool>1
1591      assert_equal(true, b)
1592      var text =
1593            <string>
1594              'text'
1595      if false
1596        text = <number>'xxx'
1597      endif
1598  END
1599  CheckDefAndScriptSuccess(lines)
1600
1601  CheckDefAndScriptFailure(["var x = <nr>123"], 'E1010:', 1)
1602  CheckDefFailure(["var x = <number>"], 'E1097:', 3)
1603  CheckDefFailure(["var x = <number>string(1)"], 'E1012:', 1)
1604  CheckScriptFailure(['vim9script', "var x = <number>"], 'E15:', 2)
1605  CheckDefAndScriptFailure(["var x = <number >123"], 'E1068:', 1)
1606  CheckDefAndScriptFailure(["var x = <number 123"], 'E1104:', 1)
1607enddef
1608
1609" test low level expression
1610def Test_expr7_number()
1611  # number constant
1612  var lines =<< trim END
1613      assert_equal(0, 0)
1614      assert_equal(654, 0654)
1615
1616      assert_equal(6, 0x6)
1617      assert_equal(15, 0xf)
1618      assert_equal(255, 0xff)
1619  END
1620  CheckDefAndScriptSuccess(lines)
1621enddef
1622
1623def Test_expr7_float()
1624  # float constant
1625  if !has('float')
1626    MissingFeature 'float'
1627  else
1628    var lines =<< trim END
1629        assert_equal(g:float_zero, .0)
1630        assert_equal(g:float_zero, 0.0)
1631        assert_equal(g:float_neg, -9.8)
1632        assert_equal(g:float_big, 9.9e99)
1633    END
1634    CheckDefAndScriptSuccess(lines)
1635  endif
1636enddef
1637
1638def Test_expr7_blob()
1639  # blob constant
1640  var lines =<< trim END
1641      assert_equal(g:blob_empty, 0z)
1642      assert_equal(g:blob_one, 0z01)
1643      assert_equal(g:blob_long, 0z0102.0304)
1644
1645      var testblob = 0z010203
1646      assert_equal(0x01, testblob[0])
1647      assert_equal(0x02, testblob[1])
1648      assert_equal(0x03, testblob[-1])
1649      assert_equal(0x02, testblob[-2])
1650
1651      assert_equal(0z01, testblob[0 : 0])
1652      assert_equal(0z0102, testblob[0 : 1])
1653      assert_equal(0z010203, testblob[0 : 2])
1654      assert_equal(0z010203, testblob[0 : ])
1655      assert_equal(0z0203, testblob[1 : ])
1656      assert_equal(0z0203, testblob[1 : 2])
1657      assert_equal(0z0203, testblob[1 : -1])
1658      assert_equal(0z03, testblob[-1 : -1])
1659      assert_equal(0z02, testblob[-2 : -2])
1660
1661      # blob slice accepts out of range
1662      assert_equal(0z, testblob[3 : 3])
1663      assert_equal(0z, testblob[0 : -4])
1664  END
1665  CheckDefAndScriptSuccess(lines)
1666
1667  CheckDefAndScriptFailure(["var x = 0z123"], 'E973:', 1)
1668enddef
1669
1670def Test_expr7_string()
1671  # string constant
1672  var lines =<< trim END
1673      assert_equal(g:string_empty, '')
1674      assert_equal(g:string_empty, "")
1675      assert_equal(g:string_short, 'x')
1676      assert_equal(g:string_short, "x")
1677      assert_equal(g:string_long, 'abcdefghijklm')
1678      assert_equal(g:string_long, "abcdefghijklm")
1679      assert_equal(g:string_special, "ab\ncd\ref\ekk")
1680  END
1681  CheckDefAndScriptSuccess(lines)
1682
1683  CheckDefAndScriptFailure(['var x = "abc'], 'E114:', 1)
1684  CheckDefAndScriptFailure(["var x = 'abc"], 'E115:', 1)
1685enddef
1686
1687def Test_expr7_vimvar()
1688  var old: list<string> = v:oldfiles
1689  var compl: dict<any> = v:completed_item
1690
1691  CheckDefFailure(["var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 1)
1692  CheckScriptFailure(['vim9script', 'v:oldfiles = ["foo"]', "var old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 3)
1693  new
1694  exec "normal! afoo fo\<C-N>\<Esc>"
1695  CheckDefExecAndScriptFailure(["var old: dict<number> = v:completed_item"], 'E1012: Type mismatch; expected dict<number> but got dict<string>', 1)
1696  bwipe!
1697enddef
1698
1699def Test_expr7_special()
1700  # special constant
1701  var lines =<< trim END
1702      assert_equal(g:special_true, true)
1703      assert_equal(g:special_false, false)
1704      assert_equal(g:special_true, v:true)
1705      assert_equal(g:special_false, v:false)
1706      assert_equal(v:true, true)
1707      assert_equal(v:false, false)
1708
1709      assert_equal(true, !false)
1710      assert_equal(false, !true)
1711      assert_equal(true, !0)
1712      assert_equal(false, !1)
1713      assert_equal(false, !!false)
1714      assert_equal(true, !!true)
1715      assert_equal(false, !!0)
1716      assert_equal(true, !!1)
1717
1718      var t = true
1719      var f = false
1720      assert_equal(true, t)
1721      assert_equal(false, f)
1722
1723      assert_equal(g:special_null, v:null)
1724      assert_equal(g:special_null, null)
1725      assert_equal(g:special_none, v:none)
1726  END
1727  CheckDefAndScriptSuccess(lines)
1728
1729  CheckDefAndScriptFailure(['v:true = true'], 'E46:', 1)
1730  CheckDefAndScriptFailure(['v:true = false'], 'E46:', 1)
1731  CheckDefAndScriptFailure(['v:false = true'], 'E46:', 1)
1732  CheckDefAndScriptFailure(['v:null = 11'], 'E46:', 1)
1733  CheckDefAndScriptFailure(['v:none = 22'], 'E46:', 1)
1734enddef
1735
1736def Test_expr7_list()
1737  # list
1738  var lines =<< trim END
1739      assert_equal(g:list_empty, [])
1740      assert_equal(g:list_empty, [  ])
1741
1742      var numbers: list<number> = [1, 2, 3]
1743      numbers = [1]
1744      numbers = []
1745
1746      var strings: list<string> = ['a', 'b', 'c']
1747      strings = ['x']
1748      strings = []
1749
1750      var mixed: list<any> = [1, 'b', false,]
1751      assert_equal(g:list_mixed, mixed)
1752      assert_equal('b', mixed[1])
1753
1754      echo [1,
1755            2] [3,
1756                    4]
1757
1758      var llstring: list<list<string>> = [['text'], []]
1759      llstring = [[], ['text']]
1760      llstring = [[], []]
1761  END
1762  CheckDefAndScriptSuccess(lines)
1763
1764  var rangelist: list<number> = range(3)
1765  g:rangelist = range(3)
1766  CheckDefExecAndScriptFailure(["var x: list<string> = g:rangelist"], 'E1012: Type mismatch; expected list<string> but got list<number>', 1)
1767
1768  CheckDefAndScriptFailure2(["var x = 1234[3]"], 'E1107:', 'E1062:', 1)
1769  CheckDefExecAndScriptFailure(["var x = g:anint[3]"], 'E1062:', 1)
1770
1771  CheckDefAndScriptFailure2(["var x = g:list_mixed[xxx]"], 'E1001:', 'E121:', 1)
1772
1773  CheckDefAndScriptFailure(["var x = [1,2,3]"], 'E1069:', 1)
1774  CheckDefAndScriptFailure(["var x = [1 ,2, 3]"], 'E1068:', 1)
1775
1776  CheckDefExecAndScriptFailure(["echo 1", "var x = [][0]", "echo 3"], 'E684:', 2)
1777
1778  CheckDefExecAndScriptFailure2(["var x = g:list_mixed['xx']"], 'E1012:', 'E1030:', 1)
1779  CheckDefFailure(["var x = g:list_mixed["], 'E1097:', 3)
1780  CheckScriptFailure(['vim9script', "var x = g:list_mixed["], 'E15:', 2)
1781  CheckDefFailure(["var x = g:list_mixed[0"], 'E1097:', 3)
1782  CheckScriptFailure(['vim9script', "var x = g:list_mixed[0"], 'E111:', 2)
1783  CheckDefExecAndScriptFailure(["var x = g:list_empty[3]"], 'E684:', 1)
1784  CheckDefExecAndScriptFailure(["var l: list<number> = [234, 'x']"], 'E1012:', 1)
1785  CheckDefExecAndScriptFailure(["var l: list<number> = ['x', 234]"], 'E1012:', 1)
1786  CheckDefExecAndScriptFailure(["var l: list<string> = [234, 'x']"], 'E1012:', 1)
1787  CheckDefExecAndScriptFailure(["var l: list<string> = ['x', 123]"], 'E1012:', 1)
1788
1789  lines =<< trim END
1790      var datalist: list<string>
1791      def Main()
1792        datalist += ['x'.
1793      enddef
1794      Main()
1795  END
1796  CheckDefAndScriptFailure(lines, 'E1127:')
1797
1798  lines =<< trim END
1799      var numbers = [1, 2, 3, 4]
1800      var a = 1
1801      var b = 2
1802  END
1803  CheckDefAndScriptFailure(lines + ['echo numbers[1:b]'],
1804      'E1004: White space required before and after '':'' at ":b]"', 4)
1805  CheckDefAndScriptFailure(lines + ['echo numbers[1: b]'], 'E1004:', 4)
1806  CheckDefAndScriptFailure(lines + ['echo numbers[a :b]'], 'E1004:', 4)
1807enddef
1808
1809def Test_expr7_list_vim9script()
1810  var lines =<< trim END
1811      var l = [
1812		11,
1813		22,
1814		]
1815      assert_equal([11, 22], l)
1816
1817      echo [1,
1818	    2] [3,
1819		    4]
1820
1821      echo [1, # comment
1822            # comment
1823	    2] [3,
1824            # comment
1825		    4]
1826  END
1827  CheckDefAndScriptSuccess(lines)
1828
1829  lines =<< trim END
1830      var l = [11,
1831		22]
1832      assert_equal([11, 22], l)
1833  END
1834  CheckDefAndScriptSuccess(lines)
1835
1836  lines =<< trim END
1837      var l = [11,22]
1838  END
1839  CheckDefAndScriptFailure(lines, 'E1069:', 1)
1840
1841  lines =<< trim END
1842      var l = [11 , 22]
1843  END
1844  CheckDefAndScriptFailure(lines, 'E1068:', 1)
1845
1846  lines =<< trim END
1847    var l: list<number> = [234, 'x']
1848  END
1849  CheckDefAndScriptFailure(lines, 'E1012:', 1)
1850
1851  lines =<< trim END
1852    var l: list<number> = ['x', 234]
1853  END
1854  CheckDefAndScriptFailure(lines, 'E1012:', 1)
1855
1856  lines =<< trim END
1857    var l: list<string> = ['x', 234]
1858  END
1859  CheckDefAndScriptFailure(lines, 'E1012:', 1)
1860
1861  lines =<< trim END
1862    var l: list<string> = [234, 'x']
1863  END
1864  CheckDefAndScriptFailure(lines, 'E1012:', 1)
1865
1866  lines =<< trim END
1867      def Failing()
1868        job_stop()
1869      enddef
1870      var list = [Failing]
1871  END
1872  if has('channel')
1873    CheckDefAndScriptFailure(lines, 'E119:', 0)
1874  else
1875    CheckDefAndScriptFailure(lines, 'E117:', 0)
1876  endif
1877enddef
1878
1879def LambdaWithComments(): func
1880  return (x) =>
1881            # some comment
1882            x == 1
1883            # some comment
1884            ||
1885            x == 2
1886enddef
1887
1888def LambdaUsingArg(x: number): func
1889  return () =>
1890            # some comment
1891            x == 1
1892            # some comment
1893            ||
1894            x == 2
1895enddef
1896
1897def Test_expr7_lambda()
1898  var lines =<< trim END
1899      var La = () => 'result'
1900      # comment
1901      assert_equal('result', La())
1902      assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val))
1903
1904      # line continuation inside lambda with "cond ? expr : expr" works
1905      var ll = range(3)
1906      var dll = mapnew(ll, (k, v) => v % 2 ? {
1907                ['111']: 111 } : {}
1908            )
1909      assert_equal([{}, {111: 111}, {}], dll)
1910
1911      # comment halfway an expression
1912      var Ref = () => 4
1913      # comment
1914      + 6
1915      assert_equal(10, Ref())
1916
1917      ll = range(3)
1918      map(ll, (k, v) => v == 8 || v
1919                    == 9
1920                    || v % 2 ? 111 : 222
1921            )
1922      assert_equal([222, 111, 222], ll)
1923
1924      ll = range(3)
1925      map(ll, (k, v) => v != 8 && v
1926                    != 9
1927                    && v % 2 == 0 ? 111 : 222
1928            )
1929      assert_equal([111, 222, 111], ll)
1930
1931      var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] )
1932      assert_equal([{key: 22}], dl)
1933
1934      dl = [{key: 12}, {['foo']: 34}]
1935      assert_equal([{key: 12}], filter(dl,
1936            (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0))
1937
1938      assert_equal(false, LambdaWithComments()(0))
1939      assert_equal(true, LambdaWithComments()(1))
1940      assert_equal(true, LambdaWithComments()(2))
1941      assert_equal(false, LambdaWithComments()(3))
1942
1943      assert_equal(false, LambdaUsingArg(0)())
1944      assert_equal(true, LambdaUsingArg(1)())
1945
1946      var res = map([1, 2, 3], (i: number, v: number) => i + v)
1947      assert_equal([1, 3, 5], res)
1948  END
1949  CheckDefAndScriptSuccess(lines)
1950
1951  CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:')
1952  CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004: White space required before and after ''=>'' at "=> a + 1"')
1953  CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"], 'E1004:')
1954  CheckDefAndScriptFailure2(["var Ref = (a) =< a + 1"], 'E1001:', 'E121:')
1955  CheckDefAndScriptFailure(["var Ref = (a: int) => a + 1"], 'E1010:')
1956  CheckDefAndScriptFailure(["var Ref = (a): int => a + 1"], 'E1010:')
1957
1958  CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1)
1959  # error is in first line of the lambda
1960  CheckDefAndScriptFailure(["var L = (a) => a + b"], 'E1001:', 0)
1961
1962  assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy'))
1963
1964  CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"], 'E118:')
1965  CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"], 'E118:')
1966  CheckDefAndScriptFailure2(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 'E121:', 1)
1967
1968  CheckDefAndScriptSuccess(['var Fx = (a) => ({k1: 0,', ' k2: 1})'])
1969  CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2)
1970  CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2)
1971
1972  CheckDefAndScriptSuccess(['var Fx = (a) => [0,', ' 1]'])
1973  CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2)
1974
1975  # no error for existing script variable when checking for lambda
1976  lines =<< trim END
1977    var name = 0
1978    eval (name + 2) / 3
1979  END
1980  CheckDefAndScriptSuccess(lines)
1981enddef
1982
1983def Test_expr7_lambda_block()
1984  var lines =<< trim END
1985      var Func = (s: string): string => {
1986                      return 'hello ' .. s
1987                    }
1988      assert_equal('hello there', Func('there'))
1989
1990      var ll = range(3)
1991      var dll = mapnew(ll, (k, v): string => {
1992          if v % 2
1993            return 'yes'
1994          endif
1995          return 'no'
1996        })
1997      assert_equal(['no', 'yes', 'no'], dll)
1998
1999      sandbox var Safe = (nr: number): number => {
2000          return nr + 7
2001        }
2002      assert_equal(10, Safe(3))
2003  END
2004  CheckDefAndScriptSuccess(lines)
2005
2006  lines =<< trim END
2007      map([1, 2], (k, v) => { redrawt })
2008  END
2009  CheckDefAndScriptFailure(lines, 'E488')
2010
2011  lines =<< trim END
2012      var Func = (nr: int) => {
2013              echo nr
2014            }
2015  END
2016  CheckDefAndScriptFailure(lines, 'E1010', 1)
2017
2018  lines =<< trim END
2019      var Func = (nr: number): int => {
2020              return nr
2021            }
2022  END
2023  CheckDefAndScriptFailure(lines, 'E1010', 1)
2024
2025  lines =<< trim END
2026      var Func = (nr: number): int => {
2027              return nr
2028  END
2029  CheckDefAndScriptFailure(lines, 'E1171', 1)  # line nr is function start
2030
2031  lines =<< trim END
2032      var Func = (nr: number): int => {
2033          var ll =<< ENDIT
2034             nothing
2035  END
2036  CheckDefFailure(lines, 'E1145: Missing heredoc end marker: ENDIT', 0)
2037  CheckScriptFailure(['vim9script'] + lines, 'E1145: Missing heredoc end marker: ENDIT', 2)
2038enddef
2039
2040def NewLambdaWithComments(): func
2041  return (x) =>
2042            # some comment
2043            x == 1
2044            # some comment
2045            ||
2046            x == 2
2047enddef
2048
2049def NewLambdaUsingArg(x: number): func
2050  return () =>
2051            # some comment
2052            x == 1
2053            # some comment
2054            ||
2055            x == 2
2056enddef
2057
2058def Test_expr7_new_lambda()
2059  var lines =<< trim END
2060      var La = () => 'result'
2061      assert_equal('result', La())
2062      assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val))
2063
2064      # line continuation inside lambda with "cond ? expr : expr" works
2065      var ll = range(3)
2066      var dll = mapnew(ll, (k, v) => v % 2 ? {
2067                ['111']: 111 } : {}
2068            )
2069      assert_equal([{}, {111: 111}, {}], dll)
2070
2071      ll = range(3)
2072      map(ll, (k, v) => v == 8 || v
2073                    == 9
2074                    || v % 2 ? 111 : 222
2075            )
2076      assert_equal([222, 111, 222], ll)
2077
2078      ll = range(3)
2079      map(ll, (k, v) => v != 8 && v
2080                    != 9
2081                    && v % 2 == 0 ? 111 : 222
2082            )
2083      assert_equal([111, 222, 111], ll)
2084
2085      var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] )
2086      assert_equal([{key: 22}], dl)
2087
2088      dl = [{key: 12}, {['foo']: 34}]
2089      assert_equal([{key: 12}], filter(dl,
2090            (_, v) => has_key(v, 'key') ? v['key'] == 12 : 0))
2091
2092      assert_equal(false, NewLambdaWithComments()(0))
2093      assert_equal(true, NewLambdaWithComments()(1))
2094      assert_equal(true, NewLambdaWithComments()(2))
2095      assert_equal(false, NewLambdaWithComments()(3))
2096
2097      assert_equal(false, NewLambdaUsingArg(0)())
2098      assert_equal(true, NewLambdaUsingArg(1)())
2099
2100      var res = map([1, 2, 3], (i: number, v: number) => i + v)
2101      assert_equal([1, 3, 5], res)
2102
2103      # Lambda returning a dict
2104      var Lmb = () => ({key: 42})
2105      assert_equal({key: 42}, Lmb())
2106
2107      var RefOne: func(number): string = (a: number): string => 'x'
2108      var RefTwo: func(number): any = (a: number): any => 'x'
2109
2110      var Fx = (a) => ({k1: 0,
2111                         k2: 1})
2112      var Fy = (a) => [0,
2113                       1]
2114  END
2115  CheckDefAndScriptSuccess(lines)
2116
2117  CheckDefAndScriptFailure(["var Ref = (a)=>a + 1"], 'E1004:')
2118  CheckDefAndScriptFailure(["var Ref = (a)=> a + 1"], 'E1004:')
2119  CheckDefAndScriptFailure(["var Ref = (a) =>a + 1"],
2120      'E1004: White space required before and after ''=>'' at " =>a + 1"')
2121
2122  CheckDefAndScriptFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:')
2123  CheckDefAndScriptFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:')
2124
2125  CheckDefAndScriptFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1)
2126  # error is in first line of the lambda
2127  CheckDefAndScriptFailure2(["var L = (a) -> a + b"], 'E1001:', 'E121:', 1)
2128
2129  assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy'))
2130
2131  CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x')"],
2132        'E118: Too many arguments for function:')
2133  CheckDefExecFailure(["var s = 'asdf'->((a) => a)('x', 'y')"],
2134        'E118: Too many arguments for function:')
2135  CheckDefFailure(["echo 'asdf'->((a) => a)(x)"], 'E1001:', 1)
2136
2137  CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2)
2138  CheckDefAndScriptFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2)
2139
2140  CheckDefAndScriptFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2)
2141enddef
2142
2143def Test_expr7_lambda_vim9script()
2144  var lines =<< trim END
2145      var v = 10->((a) =>
2146	    a
2147	      + 2
2148            )()
2149      assert_equal(12, v)
2150  END
2151  CheckDefAndScriptSuccess(lines)
2152
2153  # nested lambda with line breaks
2154  lines =<< trim END
2155      search('"', 'cW', 0, 0, () =>
2156	synstack('.', col('.'))
2157          ->map((_, v) => synIDattr(v, 'name'))->len())
2158  END
2159  CheckDefAndScriptSuccess(lines)
2160enddef
2161
2162def Test_expr7_funcref()
2163  var lines =<< trim END
2164      def RetNumber(): number
2165        return 123
2166      enddef
2167      var FuncRef = RetNumber
2168      assert_equal(123, FuncRef())
2169  END
2170  CheckDefAndScriptSuccess(lines)
2171
2172  lines =<< trim END
2173      vim9script
2174      func g:GlobalFunc()
2175        return 'global'
2176      endfunc
2177      func s:ScriptFunc()
2178        return 'script'
2179      endfunc
2180      def Test()
2181        var Ref = g:GlobalFunc
2182        assert_equal('global', Ref())
2183        Ref = GlobalFunc
2184        assert_equal('global', Ref())
2185
2186        Ref = s:ScriptFunc
2187        assert_equal('script', Ref())
2188        Ref = ScriptFunc
2189        assert_equal('script', Ref())
2190      enddef
2191      Test()
2192  END
2193  CheckScriptSuccess(lines)
2194enddef
2195
2196let g:test_space_dict = {'': 'empty', ' ': 'space'}
2197let g:test_hash_dict = #{one: 1, two: 2}
2198
2199def Test_expr7_dict()
2200  # dictionary
2201  var lines =<< trim END
2202      assert_equal(g:dict_empty, {})
2203      assert_equal(g:dict_empty, {  })
2204      assert_equal(g:dict_one, {['one']: 1})
2205      var key = 'one'
2206      var val = 1
2207      assert_equal(g:dict_one, {[key]: val})
2208
2209      var numbers: dict<number> = {a: 1, b: 2, c: 3}
2210      numbers = {a: 1}
2211      numbers = {}
2212
2213      var strings: dict<string> = {a: 'a', b: 'b', c: 'c'}
2214      strings = {a: 'x'}
2215      strings = {}
2216
2217      var dash = {xx-x: 8}
2218      assert_equal({['xx-x']: 8}, dash)
2219
2220      var dnr = {8: 8}
2221      assert_equal({['8']: 8}, dnr)
2222
2223      var mixed: dict<any> = {a: 'a', b: 42}
2224      mixed = {a: 'x'}
2225      mixed = {a: 234}
2226      mixed = {}
2227
2228      var dictlist: dict<list<string>> = {absent: [], present: ['hi']}
2229      dictlist = {absent: ['hi'], present: []}
2230      dictlist = {absent: [], present: []}
2231
2232      var dictdict: dict<dict<string>> = {one: {a: 'text'}, two: {}}
2233      dictdict = {one: {}, two: {a: 'text'}}
2234      dictdict = {one: {}, two: {}}
2235
2236      assert_equal({['']: 0}, {[matchstr('string', 'wont match')]: 0})
2237
2238      assert_equal(g:test_space_dict, {['']: 'empty', [' ']: 'space'})
2239      assert_equal(g:test_hash_dict, {one: 1, two: 2})
2240
2241      assert_equal({['a a']: 1, ['b/c']: 2}, {'a a': 1, "b/c": 2})
2242
2243      var d = {a: () => 3, b: () => 7}
2244      assert_equal(3, d.a())
2245      assert_equal(7, d.b())
2246
2247      var cd = { # comment
2248                key: 'val' # comment
2249               }
2250
2251      # different types used for the key
2252      var dkeys = {['key']: 'string',
2253                   [12]: 'numberexpr',
2254                   34: 'number',
2255                   [true]: 'bool'}
2256      assert_equal('string', dkeys['key'])
2257      assert_equal('numberexpr', dkeys[12])
2258      assert_equal('number', dkeys[34])
2259      assert_equal('bool', dkeys[true])
2260      if has('float')
2261        dkeys = {[1.2]: 'floatexpr', [3.4]: 'float'}
2262        assert_equal('floatexpr', dkeys[1.2])
2263        assert_equal('float', dkeys[3.4])
2264      endif
2265
2266      # automatic conversion from number to string
2267      var n = 123
2268      var dictnr = {[n]: 1}
2269
2270      # comment to start fold is OK
2271      var x1: number #{{ fold
2272      var x2 = 9 #{{ fold
2273  END
2274  CheckDefAndScriptSuccess(lines)
2275
2276  # legacy syntax doesn't work
2277  CheckDefAndScriptFailure(["var x = #{key: 8}"], 'E1170:', 1)
2278  CheckDefAndScriptFailure(["var x = 'a' #{a: 1}"], 'E1170:', 1)
2279  CheckDefAndScriptFailure(["var x = 'a' .. #{a: 1}"], 'E1170:', 1)
2280  CheckDefAndScriptFailure(["var x = true ? #{a: 1}"], 'E1170:', 1)
2281
2282  CheckDefAndScriptFailure(["var x = {a:8}"], 'E1069:', 1)
2283  CheckDefAndScriptFailure(["var x = {a : 8}"], 'E1068:', 1)
2284  CheckDefAndScriptFailure(["var x = {a :8}"], 'E1068:', 1)
2285  CheckDefAndScriptFailure(["var x = {a: 8 , b: 9}"], 'E1068:', 1)
2286  CheckDefAndScriptFailure(["var x = {a: 1,b: 2}"], 'E1069:', 1)
2287
2288  CheckDefAndScriptFailure(["var x = {xxx}"], 'E720:', 1)
2289  CheckDefAndScriptFailure(["var x = {xxx: 1", "var y = 2"], 'E722:', 2)
2290  CheckDefFailure(["var x = {xxx: 1,"], 'E723:', 2)
2291  CheckScriptFailure(['vim9script', "var x = {xxx: 1,"], 'E723:', 2)
2292  CheckDefAndScriptFailure2(["var x = {['a']: xxx}"], 'E1001:', 'E121:', 1)
2293  CheckDefAndScriptFailure(["var x = {a: 1, a: 2}"], 'E721:', 1)
2294  CheckDefExecAndScriptFailure2(["var x = g:anint.member"], 'E715:', 'E15:', 1)
2295  CheckDefExecAndScriptFailure(["var x = g:dict_empty.member"], 'E716:', 1)
2296
2297  CheckDefExecAndScriptFailure(['var x: dict<number> = {a: 234, b: "1"}'], 'E1012:', 1)
2298  CheckDefExecAndScriptFailure(['var x: dict<number> = {a: "x", b: 134}'], 'E1012:', 1)
2299  CheckDefExecAndScriptFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1)
2300  CheckDefExecAndScriptFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1)
2301
2302  # invalid types for the key
2303  CheckDefAndScriptFailure2(["var x = {[[1, 2]]: 0}"], 'E1105:', 'E730:', 1)
2304
2305  CheckDefFailure(['var x = ({'], 'E723:', 2)
2306  CheckScriptFailure(['vim9script', 'var x = ({'], 'E723:', 2)
2307  CheckDefExecAndScriptFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1)
2308enddef
2309
2310def Test_expr7_dict_vim9script()
2311  var lines =<< trim END
2312      var d = {
2313		['one']:
2314		   1,
2315		['two']: 2,
2316		   }
2317      assert_equal({one: 1, two: 2}, d)
2318
2319      d = {  # comment
2320		['one']:
2321                # comment
2322
2323		   1,
2324                # comment
2325                # comment
2326		['two']: 2,
2327		   }
2328      assert_equal({one: 1, two: 2}, d)
2329
2330      var dd = {k: 123->len()}
2331      assert_equal(3, dd.k)
2332  END
2333  CheckDefAndScriptSuccess(lines)
2334
2335  lines =<< trim END
2336      var d = { ["one"]: "one", ["two"]: "two", }
2337      assert_equal({one: 'one', two: 'two'}, d)
2338  END
2339  CheckDefAndScriptSuccess(lines)
2340
2341  lines =<< trim END
2342      var d = {one: 1,
2343		two: 2,
2344	       }
2345      assert_equal({one: 1, two: 2}, d)
2346  END
2347  CheckDefAndScriptSuccess(lines)
2348
2349  lines =<< trim END
2350      var d = {one:1, two: 2}
2351  END
2352  CheckDefAndScriptFailure(lines, 'E1069:', 1)
2353
2354  lines =<< trim END
2355      var d = {one: 1,two: 2}
2356  END
2357  CheckDefAndScriptFailure(lines, 'E1069:', 1)
2358
2359  lines =<< trim END
2360      var d = {one : 1}
2361  END
2362  CheckDefAndScriptFailure(lines, 'E1068:', 1)
2363
2364  lines =<< trim END
2365      var d = {one:1}
2366  END
2367  CheckDefAndScriptFailure(lines, 'E1069:', 1)
2368
2369  lines =<< trim END
2370      var d = {one: 1 , two: 2}
2371  END
2372  CheckDefAndScriptFailure(lines, 'E1068:', 1)
2373
2374  lines =<< trim END
2375    var l: dict<number> = {a: 234, b: 'x'}
2376  END
2377  CheckDefAndScriptFailure(lines, 'E1012:', 1)
2378
2379  lines =<< trim END
2380    var l: dict<number> = {a: 'x', b: 234}
2381  END
2382  CheckDefAndScriptFailure(lines, 'E1012:', 1)
2383
2384  lines =<< trim END
2385    var l: dict<string> = {a: 'x', b: 234}
2386  END
2387  CheckDefAndScriptFailure(lines, 'E1012:', 1)
2388
2389  lines =<< trim END
2390    var l: dict<string> = {a: 234, b: 'x'}
2391  END
2392  CheckDefAndScriptFailure(lines, 'E1012:', 1)
2393
2394  lines =<< trim END
2395    var d = {['a']: 234, ['b': 'x'}
2396  END
2397  CheckDefAndScriptFailure(lines, 'E1139:', 1)
2398
2399  lines =<< trim END
2400    def Func()
2401      var d = {['a']: 234, ['b': 'x'}
2402    enddef
2403    defcompile
2404  END
2405  CheckDefAndScriptFailure(lines, 'E1139:', 0)
2406
2407  lines =<< trim END
2408    var d = {'a':
2409  END
2410  CheckDefFailure(lines, 'E723:', 2)
2411  CheckScriptFailure(['vim9script'] + lines, 'E15:', 2)
2412
2413  lines =<< trim END
2414    def Func()
2415      var d = {'a':
2416    enddef
2417    defcompile
2418  END
2419  CheckDefAndScriptFailure(lines, 'E723:', 0)
2420
2421  lines =<< trim END
2422      def Failing()
2423        job_stop()
2424      enddef
2425      var dict = {name: Failing}
2426  END
2427  if has('channel')
2428    CheckDefAndScriptFailure(lines, 'E119:', 0)
2429  else
2430    CheckDefAndScriptFailure(lines, 'E117:', 0)
2431  endif
2432enddef
2433
2434let g:oneString = 'one'
2435
2436def Test_expr_member()
2437  var lines =<< trim END
2438      assert_equal(1, g:dict_one.one)
2439      var d: dict<number> = g:dict_one
2440      assert_equal(1, d['one'])
2441      assert_equal(1, d[
2442                      'one'
2443                      ])
2444      assert_equal(1, d
2445            .one)
2446      d = {1: 1, _: 2}
2447      assert_equal(1, d
2448            .1)
2449      assert_equal(2, d
2450            ._)
2451
2452      # getting the one member should clear the dict after getting the item
2453      assert_equal('one', {one: 'one'}.one)
2454      assert_equal('one', {one: 'one'}[g:oneString])
2455  END
2456  CheckDefAndScriptSuccess(lines)
2457
2458  CheckDefAndScriptFailure2(["var x = g:dict_one.#$!"], 'E1002:', 'E15:', 1)
2459  CheckDefExecAndScriptFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2)
2460  CheckDefExecAndScriptFailure(["var d: dict<number>", "d = g:list_empty"], 'E1012: Type mismatch; expected dict<number> but got list<unknown>', 2)
2461enddef
2462
2463def Test_expr7_any_index_slice()
2464  var lines =<< trim END
2465    # getting the one member should clear the list only after getting the item
2466    assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1])
2467
2468    # string is permissive, index out of range accepted
2469    g:teststring = 'abcdef'
2470    assert_equal('b', g:teststring[1])
2471    assert_equal('f', g:teststring[-1])
2472    assert_equal('', g:teststring[99])
2473
2474    assert_equal('b', g:teststring[1 : 1])
2475    assert_equal('bcdef', g:teststring[1 :])
2476    assert_equal('abcd', g:teststring[: 3])
2477    assert_equal('cdef', g:teststring[-4 :])
2478    assert_equal('abcdef', g:teststring[-9 :])
2479    assert_equal('abcd', g:teststring[: -3])
2480    assert_equal('', g:teststring[: -9])
2481
2482    # composing characters are included
2483    g:teststring = 'àéû'
2484    assert_equal('à', g:teststring[0])
2485    assert_equal('é', g:teststring[1])
2486    assert_equal('û', g:teststring[2])
2487    assert_equal('', g:teststring[3])
2488    assert_equal('', g:teststring[4])
2489
2490    assert_equal('û', g:teststring[-1])
2491    assert_equal('é', g:teststring[-2])
2492    assert_equal('à', g:teststring[-3])
2493    assert_equal('', g:teststring[-4])
2494    assert_equal('', g:teststring[-5])
2495
2496    assert_equal('à', g:teststring[0 : 0])
2497    assert_equal('é', g:teststring[1 : 1])
2498    assert_equal('àé', g:teststring[0 : 1])
2499    assert_equal('àéû', g:teststring[0 : -1])
2500    assert_equal('àé', g:teststring[0 : -2])
2501    assert_equal('à', g:teststring[0 : -3])
2502    assert_equal('', g:teststring[0 : -4])
2503    assert_equal('', g:teststring[0 : -5])
2504    assert_equal('àéû', g:teststring[ : ])
2505    assert_equal('àéû', g:teststring[0 : ])
2506    assert_equal('éû', g:teststring[1 : ])
2507    assert_equal('û', g:teststring[2 : ])
2508    assert_equal('', g:teststring[3 : ])
2509    assert_equal('', g:teststring[4 : ])
2510
2511    # blob index cannot be out of range
2512    g:testblob = 0z01ab
2513    assert_equal(0x01, g:testblob[0])
2514    assert_equal(0xab, g:testblob[1])
2515    assert_equal(0xab, g:testblob[-1])
2516    assert_equal(0x01, g:testblob[-2])
2517
2518    # blob slice accepts out of range
2519    assert_equal(0z01ab, g:testblob[0 : 1])
2520    assert_equal(0z01, g:testblob[0 : 0])
2521    assert_equal(0z01, g:testblob[-2 : -2])
2522    assert_equal(0zab, g:testblob[1 : 1])
2523    assert_equal(0zab, g:testblob[-1 : -1])
2524    assert_equal(0z, g:testblob[2 : 2])
2525    assert_equal(0z, g:testblob[0 : -3])
2526
2527    # list index cannot be out of range
2528    g:testlist = [0, 1, 2, 3]
2529    assert_equal(0, g:testlist[0])
2530    assert_equal(1, g:testlist[1])
2531    assert_equal(3, g:testlist[3])
2532    assert_equal(3, g:testlist[-1])
2533    assert_equal(0, g:testlist[-4])
2534    assert_equal(1, g:testlist[g:theone])
2535
2536    # list slice accepts out of range
2537    assert_equal([0], g:testlist[0 : 0])
2538    assert_equal([3], g:testlist[3 : 3])
2539    assert_equal([0, 1], g:testlist[0 : 1])
2540    assert_equal([0, 1, 2, 3], g:testlist[0 : 3])
2541    assert_equal([0, 1, 2, 3], g:testlist[0 : 9])
2542    assert_equal([], g:testlist[-1 : 1])
2543    assert_equal([1], g:testlist[-3 : 1])
2544    assert_equal([0, 1], g:testlist[-4 : 1])
2545    assert_equal([0, 1], g:testlist[-9 : 1])
2546    assert_equal([1, 2, 3], g:testlist[1 : -1])
2547    assert_equal([1], g:testlist[1 : -3])
2548    assert_equal([], g:testlist[1 : -4])
2549    assert_equal([], g:testlist[1 : -9])
2550
2551    g:testdict = {a: 1, b: 2}
2552    assert_equal(1, g:testdict['a'])
2553    assert_equal(2, g:testdict['b'])
2554  END
2555
2556  CheckDefAndScriptSuccess(lines)
2557
2558  CheckDefExecAndScriptFailure(['echo g:testblob[2]'], 'E979:', 1)
2559  CheckDefExecAndScriptFailure(['echo g:testblob[-3]'], 'E979:', 1)
2560
2561  CheckDefExecAndScriptFailure(['echo g:testlist[4]'], 'E684: list index out of range: 4', 1)
2562  CheckDefExecAndScriptFailure(['echo g:testlist[-5]'], 'E684:', 1)
2563
2564  CheckDefExecAndScriptFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1)
2565  CheckDefExecAndScriptFailure(['echo g:testdict[1]'], 'E716:', 1)
2566
2567  unlet g:teststring
2568  unlet g:testblob
2569  unlet g:testlist
2570enddef
2571
2572def Test_expr_member_vim9script()
2573  var lines =<< trim END
2574      var d = {one:
2575      		'one',
2576		two: 'two',
2577		1: 1,
2578		_: 2}
2579      assert_equal('one', d.one)
2580      assert_equal('one', d
2581                            .one)
2582      assert_equal(1, d
2583                            .1)
2584      assert_equal(2, d
2585                            ._)
2586      assert_equal('one', d[
2587			    'one'
2588			    ])
2589  END
2590  CheckDefAndScriptSuccess(lines)
2591
2592  lines =<< trim END
2593      var l = [1,
2594		  2,
2595		  3, 4
2596		  ]
2597      assert_equal(2, l[
2598			    1
2599			    ])
2600      assert_equal([2, 3], l[1 : 2])
2601      assert_equal([1, 2, 3], l[
2602				:
2603				2
2604				])
2605      assert_equal([3, 4], l[
2606				2
2607				:
2608				])
2609  END
2610  CheckDefAndScriptSuccess(lines)
2611enddef
2612
2613def SetSomeVar()
2614  b:someVar = &fdm
2615enddef
2616
2617def Test_expr7_option()
2618  var lines =<< trim END
2619      # option
2620      set ts=11
2621      assert_equal(11, &ts)
2622      &ts = 9
2623      assert_equal(9, &ts)
2624      set ts=8
2625      set grepprg=some\ text
2626      assert_equal('some text', &grepprg)
2627      &grepprg = test_null_string()
2628      assert_equal('', &grepprg)
2629      set grepprg&
2630
2631      # check matching type
2632      var bval: bool = &tgc
2633      var nval: number = &ts
2634      var sval: string = &path
2635
2636      # check v_lock is cleared (requires using valgrind, doesn't always show)
2637      SetSomeVar()
2638      b:someVar = 0
2639      unlet b:someVar
2640  END
2641  CheckDefAndScriptSuccess(lines)
2642enddef
2643
2644def Test_expr7_environment()
2645  var lines =<< trim END
2646      # environment variable
2647      assert_equal('testvar', $TESTVAR)
2648      assert_equal('', $ASDF_ASD_XXX)
2649  END
2650  CheckDefAndScriptSuccess(lines)
2651
2652  CheckDefAndScriptFailure2(["var x = $$$"], 'E1002:', 'E15:', 1)
2653enddef
2654
2655def Test_expr7_register()
2656  var lines =<< trim END
2657      @a = 'register a'
2658      assert_equal('register a', @a)
2659
2660      var fname = expand('%')
2661      assert_equal(fname, @%)
2662
2663      feedkeys(":echo 'some'\<CR>", "xt")
2664      assert_equal("echo 'some'", @:)
2665
2666      normal axyz
2667      assert_equal("xyz", @.)
2668
2669      @/ = 'slash'
2670      assert_equal('slash', @/)
2671
2672      @= = 'equal'
2673      assert_equal('equal', @=)
2674  END
2675  CheckDefAndScriptSuccess(lines)
2676
2677  CheckDefAndScriptFailure2(["@. = 'yes'"], 'E354:', 'E488:', 1)
2678enddef
2679
2680" This is slow when run under valgrind.
2681def Test_expr7_namespace()
2682  var lines =<< trim END
2683      g:some_var = 'some'
2684      assert_equal('some', get(g:, 'some_var'))
2685      assert_equal('some', get(g:, 'some_var', 'xxx'))
2686      assert_equal('xxx', get(g:, 'no_var', 'xxx'))
2687      unlet g:some_var
2688
2689      b:some_var = 'some'
2690      assert_equal('some', get(b:, 'some_var'))
2691      assert_equal('some', get(b:, 'some_var', 'xxx'))
2692      assert_equal('xxx', get(b:, 'no_var', 'xxx'))
2693      unlet b:some_var
2694
2695      w:some_var = 'some'
2696      assert_equal('some', get(w:, 'some_var'))
2697      assert_equal('some', get(w:, 'some_var', 'xxx'))
2698      assert_equal('xxx', get(w:, 'no_var', 'xxx'))
2699      unlet w:some_var
2700
2701      t:some_var = 'some'
2702      assert_equal('some', get(t:, 'some_var'))
2703      assert_equal('some', get(t:, 'some_var', 'xxx'))
2704      assert_equal('xxx', get(t:, 'no_var', 'xxx'))
2705      unlet t:some_var
2706
2707      # check using g: in a for loop more than DO_NOT_FREE_CNT times
2708      for i in range(100000)
2709        if has_key(g:, 'does-not-exist')
2710        endif
2711      endfor
2712  END
2713  CheckDefAndScriptSuccess(lines)
2714enddef
2715
2716def Test_expr7_parens()
2717  # (expr)
2718  var lines =<< trim END
2719      assert_equal(4, (6 * 4) / 6)
2720      assert_equal(0, 6 * ( 4 / 6 ))
2721
2722      assert_equal(6, +6)
2723      assert_equal(-6, -6)
2724      assert_equal(false, !-3)
2725      assert_equal(true, !+0)
2726
2727      assert_equal(7, 5 + (
2728                    2))
2729      assert_equal(7, 5 + (
2730                    2
2731                    ))
2732      assert_equal(7, 5 + ( # comment
2733                    2))
2734      assert_equal(7, 5 + ( # comment
2735                    # comment
2736                    2))
2737
2738      var s = (
2739		'one'
2740		..
2741		'two'
2742		)
2743      assert_equal('onetwo', s)
2744  END
2745  CheckDefAndScriptSuccess(lines)
2746enddef
2747
2748def Test_expr7_negate_add()
2749  var lines =<< trim END
2750      assert_equal(-99, -99)
2751      assert_equal(-99, - 99)
2752      assert_equal(99, +99)
2753
2754      var nr = 88
2755      assert_equal(-88, -nr)
2756      assert_equal(-88, - nr)
2757      assert_equal(88, + nr)
2758  END
2759  CheckDefAndScriptSuccess(lines)
2760
2761  lines =<< trim END
2762    var n = 12
2763    echo ++n
2764  END
2765  CheckDefAndScriptFailure(lines, 'E15:')
2766  lines =<< trim END
2767    var n = 12
2768    echo --n
2769  END
2770  CheckDefAndScriptFailure(lines, 'E15:')
2771  lines =<< trim END
2772    var n = 12
2773    echo +-n
2774  END
2775  CheckDefAndScriptFailure(lines, 'E15:')
2776  lines =<< trim END
2777    var n = 12
2778    echo -+n
2779  END
2780  CheckDefAndScriptFailure(lines, 'E15:')
2781  lines =<< trim END
2782    var n = 12
2783    echo - -n
2784  END
2785  CheckDefAndScriptFailure(lines, 'E15:')
2786  lines =<< trim END
2787    var n = 12
2788    echo + +n
2789  END
2790  CheckDefAndScriptFailure(lines, 'E15:')
2791enddef
2792
2793def LegacyReturn(): string
2794  legacy return #{key: 'ok'}.key
2795enddef
2796
2797def Test_expr7_legacy_script()
2798  var lines =<< trim END
2799      let s:legacy = 'legacy'
2800      def GetLocal(): string
2801        return legacy
2802      enddef
2803      def GetLocalPrefix(): string
2804        return s:legacy
2805      enddef
2806      call assert_equal('legacy', GetLocal())
2807      call assert_equal('legacy', GetLocalPrefix())
2808  END
2809  CheckScriptSuccess(lines)
2810
2811  assert_equal('ok', LegacyReturn())
2812
2813  lines =<< trim END
2814      vim9script
2815      def GetNumber(): number
2816          legacy return range(3)->map('v:val + 1')
2817      enddef
2818      echo GetNumber()
2819  END
2820  CheckScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<number>')
2821enddef
2822
2823def Echo(arg: any): string
2824  return arg
2825enddef
2826
2827def s:Echo4Arg(arg: any): string
2828  return arg
2829enddef
2830
2831def Test_expr7_call()
2832  var lines =<< trim END
2833      assert_equal('yes', 'yes'->Echo())
2834      assert_equal(true, !range(5)->empty())
2835      assert_equal([0, 1, 2], 3->range())
2836  END
2837  CheckDefAndScriptSuccess(lines)
2838
2839  assert_equal('yes', 'yes'
2840                        ->s:Echo4Arg())
2841
2842  CheckDefAndScriptFailure(["var x = 'yes'->Echo"], 'E107:', 1)
2843  CheckDefAndScriptFailure2([
2844       "var x = substitute ('x', 'x', 'x', 'x')"
2845       ], 'E1001:', 'E121:', 1)
2846  CheckDefAndScriptFailure2(["var Ref = function('len' [1, 2])"], 'E1123:', 'E116:', 1)
2847
2848  var auto_lines =<< trim END
2849      def g:some#func(): string
2850	return 'found'
2851      enddef
2852  END
2853  mkdir('Xruntime/autoload', 'p')
2854  writefile(auto_lines, 'Xruntime/autoload/some.vim')
2855  var save_rtp = &rtp
2856  &rtp = getcwd() .. '/Xruntime,' .. &rtp
2857  assert_equal('found', g:some#func())
2858  assert_equal('found', some#func())
2859
2860  &rtp = save_rtp
2861  delete('Xruntime', 'rf')
2862enddef
2863
2864def Test_expr7_method_call()
2865  var lines =<< trim END
2866      new
2867      setline(1, ['first', 'last'])
2868      'second'->append(1)
2869      "third"->append(2)
2870      assert_equal(['first', 'second', 'third', 'last'], getline(1, '$'))
2871      bwipe!
2872
2873      var bufnr = bufnr()
2874      var loclist = [{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}]
2875      loclist->setloclist(0)
2876      assert_equal([{bufnr: bufnr,
2877                    lnum: 42,
2878                    col: 17,
2879                    text: 'wrong',
2880                    pattern: '',
2881                    valid: 1,
2882                    vcol: 0,
2883                    nr: 0,
2884                    type: '',
2885                    module: ''}
2886                    ], getloclist(0))
2887
2888      var result: bool = get({n: 0}, 'n', 0)
2889      assert_equal(false, result)
2890
2891      assert_equal('+string+', 'string'->((s) => '+' .. s .. '+')())
2892      assert_equal('-text-', 'text'->((s, c) => c .. s .. c)('-'))
2893
2894      var Join = (l) => join(l, 'x')
2895      assert_equal('axb', ['a', 'b']->(Join)())
2896  END
2897  CheckDefAndScriptSuccess(lines)
2898enddef
2899
2900
2901def Test_expr7_not()
2902  var lines =<< trim END
2903      assert_equal(true, !'')
2904      assert_equal(true, ![])
2905      assert_equal(false, !'asdf')
2906      assert_equal(false, ![2])
2907      assert_equal(true, !!'asdf')
2908      assert_equal(true, !![2])
2909
2910      assert_equal(true, ! false)
2911      assert_equal(true, !! true)
2912      assert_equal(true, ! ! true)
2913      assert_equal(true, !!! false)
2914      assert_equal(true, ! ! ! false)
2915
2916      g:true = true
2917      g:false = false
2918      assert_equal(true, ! g:false)
2919      assert_equal(true, !! g:true)
2920      assert_equal(true, ! ! g:true)
2921      assert_equal(true, !!! g:false)
2922      assert_equal(true, ! ! ! g:false)
2923      unlet g:true
2924      unlet g:false
2925
2926      assert_equal(true, !test_null_partial())
2927      assert_equal(false, !() => 'yes')
2928
2929      assert_equal(true, !test_null_dict())
2930      assert_equal(true, !{})
2931      assert_equal(false, !{yes: 'no'})
2932
2933      if has('channel')
2934	assert_equal(true, !test_null_job())
2935	assert_equal(true, !test_null_channel())
2936      endif
2937
2938      assert_equal(true, !test_null_blob())
2939      assert_equal(true, !0z)
2940      assert_equal(false, !0z01)
2941
2942      assert_equal(true, !test_void())
2943      assert_equal(true, !test_unknown())
2944
2945      assert_equal(false, ![1, 2, 3]->reverse())
2946      assert_equal(true, ![]->reverse())
2947  END
2948  CheckDefAndScriptSuccess(lines)
2949enddef
2950
2951func Test_expr7_fails()
2952  call CheckDefFailure(["var x = (12"], "E1097:", 3)
2953  call CheckScriptFailure(['vim9script', "var x = (12"], 'E110:', 2)
2954
2955  call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1)
2956  call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1)
2957  call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1)
2958  call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E39:", 'E745:', 1)
2959  call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E39:", 'E728:', 1)
2960
2961  call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1)
2962  call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1)
2963
2964  call CheckDefFailure(["var x = [1, 2"], "E697:", 2)
2965  call CheckScriptFailure(['vim9script', "var x = [1, 2"], 'E696:', 2)
2966
2967  call CheckDefAndScriptFailure2(["var x = [notfound]"], "E1001:", 'E121:', 1)
2968
2969  call CheckDefAndScriptFailure2(["var X = () => 123)"], "E488:", 'E15:', 1)
2970  call CheckDefAndScriptFailure(["var x = 123->((x) => x + 5)"], "E107:", 1)
2971
2972  call CheckDefAndScriptFailure(["var x = &notexist"], 'E113:', 1)
2973  call CheckDefAndScriptFailure2(["&grepprg = [343]"], 'E1012:', 'E730:', 1)
2974
2975  call CheckDefExecAndScriptFailure(["echo s:doesnt_exist"], 'E121:', 1)
2976  call CheckDefExecAndScriptFailure(["echo g:doesnt_exist"], 'E121:', 1)
2977
2978  call CheckDefAndScriptFailure2(["echo a:somevar"], 'E1075:', 'E121:', 1)
2979  call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1)
2980  call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1)
2981
2982  call CheckDefExecAndScriptFailure(["var x = +g:astring"], 'E1030:', 1)
2983  call CheckDefExecAndScriptFailure(["var x = +g:ablob"], 'E974:', 1)
2984  call CheckDefExecAndScriptFailure(["var x = +g:alist"], 'E745:', 1)
2985  call CheckDefExecAndScriptFailure(["var x = +g:adict"], 'E728:', 1)
2986
2987  call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E715:', 'E15:', 2)
2988
2989  call CheckDefAndScriptFailure2(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 'E260: Missing name after ->', 1)
2990
2991  call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2)
2992  call CheckScriptFailure(['vim9script', "[1, 2->len()"], 'E696:', 2)
2993
2994  call CheckDefFailure(["{a: 1->len()"], 'E723:', 2)
2995  call CheckScriptFailure(['vim9script', "{a: 1->len()"], 'E722:', 2)
2996
2997  call CheckDefExecFailure(["{['a']: 1->len()"], 'E723:', 2)
2998  call CheckScriptFailure(['vim9script', "{['a']: 1->len()"], 'E722:', 2)
2999endfunc
3000
3001let g:Funcrefs = [function('add')]
3002
3003func CallMe(arg)
3004  return a:arg
3005endfunc
3006
3007func CallMe2(one, two)
3008  return a:one .. a:two
3009endfunc
3010
3011def Test_expr7_trailing()
3012  var lines =<< trim END
3013      # user function call
3014      assert_equal(123, g:CallMe(123))
3015      assert_equal(123, g:CallMe(  123))
3016      assert_equal(123, g:CallMe(123  ))
3017      assert_equal('yesno', g:CallMe2('yes', 'no'))
3018      assert_equal('yesno', g:CallMe2( 'yes', 'no' ))
3019      assert_equal('nothing', g:CallMe('nothing'))
3020
3021      # partial call
3022      var Part = function('g:CallMe')
3023      assert_equal('yes', Part('yes'))
3024
3025      # funcref call, using list index
3026      var l = []
3027      g:Funcrefs[0](l, 2)
3028      assert_equal([2], l)
3029
3030      # method call
3031      l = [2, 5, 6]
3032      l->map((k, v) => k + v)
3033      assert_equal([2, 6, 8], l)
3034
3035      # lambda method call
3036      l = [2, 5]
3037      l->((ll) => add(ll, 8))()
3038      assert_equal([2, 5, 8], l)
3039
3040      # dict member
3041      var d = {key: 123}
3042      assert_equal(123, d.key)
3043  END
3044  CheckDefAndScriptSuccess(lines)
3045enddef
3046
3047def Test_expr7_string_subscript()
3048  var lines =<< trim END
3049    var text = 'abcdef'
3050    assert_equal('f', text[-1])
3051    assert_equal('a', text[0])
3052    assert_equal('e', text[4])
3053    assert_equal('f', text[5])
3054    assert_equal('', text[6])
3055
3056    text = 'ábçdë'
3057    assert_equal('ë', text[-1])
3058    assert_equal('d', text[-2])
3059    assert_equal('ç', text[-3])
3060    assert_equal('b', text[-4])
3061    assert_equal('á', text[-5])
3062    assert_equal('', text[-6])
3063
3064    text = 'ábçdëf'
3065    assert_equal('', text[-999])
3066    assert_equal('f', text[-1])
3067    assert_equal('á', text[0])
3068    assert_equal('b', text[1])
3069    assert_equal('ç', text[2])
3070    assert_equal('d', text[3])
3071    assert_equal('ë', text[4])
3072    assert_equal('f', text[5])
3073    assert_equal('', text[6])
3074    assert_equal('', text[999])
3075
3076    assert_equal('ábçdëf', text[0 : -1])
3077    assert_equal('ábçdëf', text[0 : -1])
3078    assert_equal('ábçdëf', text[0 : -1])
3079    assert_equal('ábçdëf', text[0 : -1])
3080    assert_equal('ábçdëf', text[0
3081                  : -1])
3082    assert_equal('ábçdëf', text[0 :
3083                  -1])
3084    assert_equal('ábçdëf', text[0 : -1
3085                  ])
3086    assert_equal('bçdëf', text[1 : -1])
3087    assert_equal('çdëf', text[2 : -1])
3088    assert_equal('dëf', text[3 : -1])
3089    assert_equal('ëf', text[4 : -1])
3090    assert_equal('f', text[5 : -1])
3091    assert_equal('', text[6 : -1])
3092    assert_equal('', text[999 : -1])
3093
3094    assert_equal('ábçd', text[: 3])
3095    assert_equal('bçdëf', text[1 :])
3096    assert_equal('ábçdëf', text[:])
3097
3098    assert_equal('a', g:astring[0])
3099    assert_equal('sd', g:astring[1 : 2])
3100    assert_equal('asdf', g:astring[:])
3101  END
3102  CheckDefAndScriptSuccess(lines)
3103
3104  lines =<< trim END
3105      var d = 'asdf'[1 :
3106  END
3107  CheckDefFailure(lines, 'E1097:', 3)
3108  CheckScriptFailure(['vim9script'] + lines, 'E15:', 2)
3109
3110  lines =<< trim END
3111      var d = 'asdf'[1 : xxx]
3112  END
3113  CheckDefAndScriptFailure2(lines, 'E1001:', 'E121:', 1)
3114
3115  lines =<< trim END
3116      var d = 'asdf'[1 : 2
3117  END
3118  CheckDefFailure(lines, 'E1097:', 3)
3119  CheckScriptFailure(['vim9script'] + lines, 'E111:', 2)
3120
3121  lines =<< trim END
3122      var d = 'asdf'[1 : 2
3123      echo d
3124  END
3125  CheckDefAndScriptFailure(lines, 'E111:', 2)
3126
3127  lines =<< trim END
3128      var d = 'asdf'['1']
3129      echo d
3130  END
3131  CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1)
3132
3133  lines =<< trim END
3134      var d = 'asdf'['1' : 2]
3135      echo d
3136  END
3137  CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "1"', 1)
3138
3139  lines =<< trim END
3140      var d = 'asdf'[1 : '2']
3141      echo d
3142  END
3143  CheckDefAndScriptFailure2(lines, 'E1012: Type mismatch; expected number but got string', 'E1030: Using a String as a Number: "2"', 1)
3144enddef
3145
3146def Test_expr7_list_subscript()
3147  var lines =<< trim END
3148      var list = [0, 1, 2, 3, 4]
3149      assert_equal(0, list[0])
3150      assert_equal(4, list[4])
3151      assert_equal(4, list[-1])
3152      assert_equal(0, list[-5])
3153
3154      assert_equal([0, 1, 2, 3, 4], list[0 : 4])
3155      assert_equal([0, 1, 2, 3, 4], list[:])
3156      assert_equal([1, 2, 3, 4], list[1 :])
3157      assert_equal([2, 3, 4], list[2 : -1])
3158      assert_equal([4], list[4 : -1])
3159      assert_equal([], list[5 : -1])
3160      assert_equal([], list[999 : -1])
3161      assert_equal([1, 2, 3, 4], list[g:theone : g:thefour])
3162
3163      assert_equal([0, 1, 2, 3], list[0 : 3])
3164      assert_equal([0], list[0 : 0])
3165      assert_equal([0, 1, 2, 3, 4], list[0 : -1])
3166      assert_equal([0, 1, 2], list[0 : -3])
3167      assert_equal([0], list[0 : -5])
3168      assert_equal([], list[0 : -6])
3169      assert_equal([], list[0 : -99])
3170
3171      assert_equal(2, g:alist[0])
3172      assert_equal([2, 3, 4], g:alist[:])
3173  END
3174  CheckDefAndScriptSuccess(lines)
3175
3176  lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]']
3177  CheckDefExecAndScriptFailure2(lines, 'E1012:', 'E1030:', 2)
3178
3179  lines =<< trim END
3180      var ld = []
3181      def Func()
3182        eval ld[0].key
3183      enddef
3184      defcompile
3185  END
3186  CheckDefAndScriptSuccess(lines)
3187enddef
3188
3189def Test_expr7_dict_subscript()
3190  var lines =<< trim END
3191      var l = [{lnum: 2}, {lnum: 1}]
3192      var res = l[0].lnum > l[1].lnum
3193      assert_true(res)
3194
3195      assert_equal(2, g:adict['aaa'])
3196      assert_equal(8, g:adict.bbb)
3197
3198      var dd = {}
3199      def Func1()
3200        eval dd.key1.key2
3201      enddef
3202      def Func2()
3203        eval dd['key1'].key2
3204      enddef
3205      defcompile
3206  END
3207  CheckDefAndScriptSuccess(lines)
3208enddef
3209
3210def Test_expr7_blob_subscript()
3211  var lines =<< trim END
3212      var b = 0z112233
3213      assert_equal(0x11, b[0])
3214      assert_equal(0z112233, b[:])
3215
3216      assert_equal(0x01, g:ablob[0])
3217      assert_equal(0z01ab, g:ablob[:])
3218  END
3219  CheckDefAndScriptSuccess(lines)
3220enddef
3221
3222def Test_expr7_subscript_linebreak()
3223  var lines =<< trim END
3224      var range = range(
3225                    3)
3226      var l = range
3227            ->mapnew('string(v:key)')
3228      assert_equal(['0', '1', '2'], l)
3229
3230      l = range
3231            ->mapnew('string(v:key)')
3232      assert_equal(['0', '1', '2'], l)
3233
3234      l = range # comment
3235            ->mapnew('string(v:key)')
3236      assert_equal(['0', '1', '2'], l)
3237
3238      l = range
3239
3240            ->mapnew('string(v:key)')
3241      assert_equal(['0', '1', '2'], l)
3242
3243      l = range
3244            # comment
3245            ->mapnew('string(v:key)')
3246      assert_equal(['0', '1', '2'], l)
3247
3248      assert_equal('1', l[
3249            1])
3250
3251      var d = {one: 33}
3252      assert_equal(33, d
3253            .one)
3254  END
3255  CheckDefAndScriptSuccess(lines)
3256
3257  lines =<< trim END
3258      var d = {one: 33}
3259      assert_equal(33, d.
3260            one)
3261  END
3262  CheckDefAndScriptFailure2(lines, 'E1127:', 'E116:', 2)
3263enddef
3264
3265func Test_expr7_trailing_fails()
3266  call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8))'], 'E107:', 2)
3267  call CheckDefAndScriptFailure(['var l = [2]', 'l->((ll) => add(ll, 8)) ()'], 'E274:', 2)
3268endfunc
3269
3270func Test_expr_fails()
3271  call CheckDefAndScriptFailure2(["var x = '1'is2"], 'E488:', 'E15:', 1)
3272  call CheckDefAndScriptFailure2(["var x = '1'isnot2"], 'E488:', 'E15:', 1)
3273
3274  call CheckDefAndScriptFailure2(["CallMe ('yes')"], 'E476:', 'E492:', 1)
3275
3276  call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1)
3277
3278  call CheckDefAndScriptFailure2(["v:nosuch += 3"], 'E1001:', 'E121:', 1)
3279  call CheckDefAndScriptFailure(["var v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1)
3280  call CheckDefAndScriptFailure2(["var asdf = v:nosuch"], 'E1001:', 'E121:', 1)
3281
3282  call CheckDefFailure(["echo len('asdf'"], 'E110:', 2)
3283  call CheckScriptFailure(['vim9script', "echo len('asdf'"], 'E116:', 2)
3284
3285  call CheckDefAndScriptFailure2(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 'E117:', 1)
3286  call CheckDefAndScriptFailure(["echo doesnotexist()"], 'E117:', 1)
3287endfunc
3288
3289" vim: shiftwidth=2 sts=2 expandtab
3290