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