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