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