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