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