1" Tests for Vim9 script expressions
2
3source check.vim
4source vim9.vim
5
6
7let g:cond = v:false
8def FuncOne(arg: number): string
9  return 'yes'
10enddef
11def FuncTwo(arg: number): number
12  return 123
13enddef
14
15" test cond ? expr : expr
16def Test_expr1()
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  let var = 1
32  assert_equal('one', var ? '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  var = 0
44  assert_equal('two', var ? 'one' : 'two')
45
46  # with constant condition expression is not evaluated
47  assert_equal('one', 1 ? 'one' : xxx)
48
49  let Some: func = function('len')
50  let Other: func = function('winnr')
51  let Res: func = g:atrue ? Some : Other
52  assert_equal(function('len'), Res)
53
54  let RetOne: func(string): number = function('len')
55  let RetTwo: func(string): number = function('winnr')
56  let RetThat: func = g:atrue ? RetOne : RetTwo
57  assert_equal(function('len'), RetThat)
58
59  let X = FuncOne
60  let Y = FuncTwo
61  let Z = g:cond ? FuncOne : FuncTwo
62  assert_equal(123, Z(3))
63enddef
64
65def Test_expr1_vimscript()
66  # check line continuation
67  let lines =<< trim END
68      vim9script
69      let var = 1
70      		? 'yes'
71		: 'no'
72      assert_equal('yes', var)
73  END
74  CheckScriptSuccess(lines)
75
76  lines =<< trim END
77      vim9script
78      let var = v:false
79      		? 'yes'
80		: 'no'
81      assert_equal('no', var)
82  END
83  CheckScriptSuccess(lines)
84
85  lines =<< trim END
86      vim9script
87      let var = v:false ?
88      		'yes' :
89		'no'
90      assert_equal('no', var)
91  END
92  CheckScriptSuccess(lines)
93
94  # check white space
95  lines =<< trim END
96      vim9script
97      let var = v:true?1:2
98  END
99  CheckScriptFailure(lines, 'E1004:', 2)
100  lines =<< trim END
101      vim9script
102      let var = v:true? 1 : 2
103  END
104  CheckScriptFailure(lines, 'E1004:', 2)
105  lines =<< trim END
106      vim9script
107      let var = v:true ?1 : 2
108  END
109  CheckScriptFailure(lines, 'E1004:', 2)
110  lines =<< trim END
111      vim9script
112      let var = v:true ? 1: 2
113  END
114  CheckScriptFailure(lines, 'E1004:', 2)
115  lines =<< trim END
116      vim9script
117      let var = v:true ? 1 :2
118  END
119  CheckScriptFailure(lines, 'E1004:', 2)
120
121  # check after failure eval_flags is reset
122  lines =<< trim END
123      vim9script
124      try
125        eval('0 ? 1: 2')
126      catch
127      endtry
128      assert_equal(v:true, eval(string(v:true)))
129  END
130  CheckScriptSuccess(lines)
131
132  lines =<< trim END
133      vim9script
134      try
135        eval('0 ? 1 :2')
136      catch
137      endtry
138      assert_equal(v:true, eval(string(v:true)))
139  END
140  CheckScriptSuccess(lines)
141enddef
142
143func Test_expr1_fails()
144  call CheckDefFailure(["let x = 1 ? 'one'"], "Missing ':' after '?'", 1)
145
146  let msg = "White space required before and after '?'"
147  call CheckDefFailure(["let x = 1? 'one' : 'two'"], msg, 1)
148  call CheckDefFailure(["let x = 1 ?'one' : 'two'"], msg, 1)
149  call CheckDefFailure(["let x = 1?'one' : 'two'"], msg, 1)
150
151  let msg = "White space required before and after ':'"
152  call CheckDefFailure(["let x = 1 ? 'one': 'two'"], msg, 1)
153  call CheckDefFailure(["let x = 1 ? 'one' :'two'"], msg, 1)
154  call CheckDefFailure(["let x = 1 ? 'one':'two'"], msg, 1)
155
156  " missing argument detected even when common type is used
157  call CheckDefFailure([
158	\ 'let X = FuncOne',
159	\ 'let Y = FuncTwo',
160	\ 'let Z = g:cond ? FuncOne : FuncTwo',
161	\ 'Z()'], 'E119:', 4)
162endfunc
163
164" TODO: define inside test function
165def Record(val: any): any
166  g:vals->add(val)
167  return val
168enddef
169
170" test ||
171def Test_expr2()
172  assert_equal(2, 2 || 0)
173  assert_equal(7, 0 ||
174		    0 ||
175		    7)
176  assert_equal(0, 0 || 0)
177  assert_equal(0, 0
178  		    || 0)
179  assert_equal('', 0 || '')
180
181  g:vals = []
182  assert_equal(3, Record(3) || Record(1))
183  assert_equal([3], g:vals)
184
185  g:vals = []
186  assert_equal(5, Record(0) || Record(5))
187  assert_equal([0, 5], g:vals)
188
189  g:vals = []
190  assert_equal(4, Record(0)
191		      || Record(4)
192		      || Record(0))
193  assert_equal([0, 4], g:vals)
194
195  g:vals = []
196  assert_equal(0, Record([]) || Record('') || Record(0))
197  assert_equal([[], '', 0], g:vals)
198enddef
199
200def Test_expr2_vimscript()
201  # check line continuation
202  let lines =<< trim END
203      vim9script
204      let var = 0
205      		|| 1
206      assert_equal(1, var)
207  END
208  CheckScriptSuccess(lines)
209
210  lines =<< trim END
211      vim9script
212      let var = v:false
213      		|| v:true
214      		|| v:false
215      assert_equal(v:true, var)
216  END
217  CheckScriptSuccess(lines)
218
219  lines =<< trim END
220      vim9script
221      let var = v:false ||
222      		v:true ||
223		v:false
224      assert_equal(v:true, var)
225  END
226  CheckScriptSuccess(lines)
227
228  # check white space
229  lines =<< trim END
230      vim9script
231      let var = v:true||v:true
232  END
233  CheckScriptFailure(lines, 'E1004:', 2)
234  lines =<< trim END
235      vim9script
236      let var = v:true ||v:true
237  END
238  CheckScriptFailure(lines, 'E1004:', 2)
239  lines =<< trim END
240      vim9script
241      let var = v:true|| v:true
242  END
243  CheckScriptFailure(lines, 'E1004:', 2)
244
245  # check keeping the value
246  lines =<< trim END
247      vim9script
248      assert_equal(2, 2 || 0)
249      assert_equal(7, 0 ||
250			0 ||
251			7)
252      assert_equal(0, 0 || 0)
253      assert_equal(0, 0
254			|| 0)
255      assert_equal('', 0 || '')
256
257      g:vals = []
258      assert_equal(3, Record(3) || Record(1))
259      assert_equal([3], g:vals)
260
261      g:vals = []
262      assert_equal(5, Record(0) || Record(5))
263      assert_equal([0, 5], g:vals)
264
265      g:vals = []
266      assert_equal(4, Record(0)
267			  || Record(4)
268			  || Record(0))
269      assert_equal([0, 4], g:vals)
270
271      g:vals = []
272      assert_equal(0, Record([]) || Record('') || Record(0))
273      assert_equal([[], '', 0], g:vals)
274  END
275  CheckScriptSuccess(lines)
276enddef
277
278func Test_expr2_fails()
279  let msg = "White space required before and after '||'"
280  call CheckDefFailure(["let x = 1||2"], msg, 1)
281  call CheckDefFailure(["let x = 1 ||2"], msg, 1)
282  call CheckDefFailure(["let x = 1|| 2"], msg, 1)
283
284  call CheckDefFailure(["let x = 1 || xxx"], 'E1001:', 1)
285endfunc
286
287" test &&
288def Test_expr3()
289  assert_equal(0, 2 && 0)
290  assert_equal(0, 0 &&
291		0 &&
292		7)
293  assert_equal(7, 2
294  		    && 3
295		    && 7)
296  assert_equal(0, 0 && 0)
297  assert_equal(0, 0 && '')
298  assert_equal('', 8 && '')
299
300  g:vals = []
301  assert_equal(1, Record(3) && Record(1))
302  assert_equal([3, 1], g:vals)
303
304  g:vals = []
305  assert_equal(0, Record(0) && Record(5))
306  assert_equal([0], g:vals)
307
308  g:vals = []
309  assert_equal(0, Record(0) && Record(4) && Record(0))
310  assert_equal([0], g:vals)
311
312  g:vals = []
313  assert_equal(0, Record(8) && Record(4) && Record(0))
314  assert_equal([8, 4, 0], g:vals)
315
316  g:vals = []
317  assert_equal(0, Record([1]) && Record('z') && Record(0))
318  assert_equal([[1], 'z', 0], g:vals)
319enddef
320
321def Test_expr3_vimscript()
322  # check line continuation
323  let lines =<< trim END
324      vim9script
325      let var = 0
326      		&& 1
327      assert_equal(0, var)
328  END
329  CheckScriptSuccess(lines)
330
331  lines =<< trim END
332      vim9script
333      let var = v:true
334      		&& v:true
335      		&& v:true
336      assert_equal(v:true, var)
337  END
338  CheckScriptSuccess(lines)
339
340  lines =<< trim END
341      vim9script
342      let var = v:true &&
343      		v:true &&
344      		v:true
345      assert_equal(v:true, var)
346  END
347  CheckScriptSuccess(lines)
348
349  # check white space
350  lines =<< trim END
351      vim9script
352      let var = v:true&&v:true
353  END
354  CheckScriptFailure(lines, 'E1004:', 2)
355  lines =<< trim END
356      vim9script
357      let var = v:true &&v:true
358  END
359  CheckScriptFailure(lines, 'E1004:', 2)
360  lines =<< trim END
361      vim9script
362      let var = v:true&& v:true
363  END
364  CheckScriptFailure(lines, 'E1004:', 2)
365
366  # check keeping the value
367  lines =<< trim END
368      vim9script
369      assert_equal(0, 2 && 0)
370      assert_equal(0, 0 &&
371		    0 &&
372		    7)
373      assert_equal(7, 2
374			&& 3
375			&& 7)
376      assert_equal(0, 0 && 0)
377      assert_equal(0, 0 && '')
378      assert_equal('', 8 && '')
379
380      g:vals = []
381      assert_equal(1, Record(3) && Record(1))
382      assert_equal([3, 1], g:vals)
383
384      g:vals = []
385      assert_equal(0, Record(0) && Record(5))
386      assert_equal([0], g:vals)
387
388      g:vals = []
389      assert_equal(0, Record(0) && Record(4) && Record(0))
390      assert_equal([0], g:vals)
391
392      g:vals = []
393      assert_equal(0, Record(8) && Record(4) && Record(0))
394      assert_equal([8, 4, 0], g:vals)
395
396      g:vals = []
397      assert_equal(0, Record([1]) && Record('z') && Record(0))
398      assert_equal([[1], 'z', 0], g:vals)
399  END
400  CheckScriptSuccess(lines)
401enddef
402
403func Test_expr3_fails()
404  let msg = "White space required before and after '&&'"
405  call CheckDefFailure(["let x = 1&&2"], msg, 1)
406  call CheckDefFailure(["let x = 1 &&2"], msg, 1)
407  call CheckDefFailure(["let x = 1&& 2"], msg, 1)
408endfunc
409
410" global variables to use for tests with the "any" type
411let atrue = v:true
412let afalse = v:false
413let anone = v:none
414let anull = v:null
415let anint = 10
416let theone = 1
417let thefour = 4
418if has('float')
419  let afloat = 0.1
420endif
421let astring = 'asdf'
422let ablob = 0z01ab
423let alist = [2, 3, 4]
424let adict = #{aaa: 2, bbb: 8}
425
426" test == comperator
427def Test_expr4_equal()
428  let trueVar = true
429  let falseVar = false
430  assert_equal(true, true == true)
431  assert_equal(false, true ==
432			false)
433  assert_equal(true, true
434			== trueVar)
435  assert_equal(false, true == falseVar)
436  assert_equal(true, true == g:atrue)
437  assert_equal(false, g:atrue == false)
438
439  assert_equal(true, v:none == v:none)
440  assert_equal(false, v:none == v:null)
441  assert_equal(true, g:anone == v:none)
442  assert_equal(false, v:none == g:anull)
443
444  let nr0 = 0
445  let nr61 = 61
446  assert_equal(false, 2 == 0)
447  assert_equal(false, 2 == nr0)
448  assert_equal(true, 61 == 61)
449  assert_equal(true, 61 == nr61)
450  assert_equal(true, g:anint == 10)
451  assert_equal(false, 61 == g:anint)
452
453  if has('float')
454    let ff = 0.3
455    assert_equal(true, ff == 0.3)
456    assert_equal(false, 0.4 == ff)
457    assert_equal(true, 0.1 == g:afloat)
458    assert_equal(false, g:afloat == 0.3)
459
460    ff = 3.0
461    assert_equal(true, ff == 3)
462    assert_equal(true, 3 == ff)
463    ff = 3.1
464    assert_equal(false, ff == 3)
465    assert_equal(false, 3 == ff)
466  endif
467
468  assert_equal(true, 'abc' == 'abc')
469  assert_equal(false, 'xyz' == 'abc')
470  assert_equal(true, g:astring == 'asdf')
471  assert_equal(false, 'xyz' == g:astring)
472
473  assert_equal(false, 'abc' == 'aBc')
474  assert_equal(false, 'abc' ==# 'aBc')
475  assert_equal(true, 'abc' ==? 'aBc')
476
477  assert_equal(false, 'abc' == 'ABC')
478  set ignorecase
479  assert_equal(false, 'abc' == 'ABC')
480  assert_equal(false, 'abc' ==# 'ABC')
481  set noignorecase
482
483  CheckDefFailure(["let x = 'a' == xxx"], 'E1001:', 1)
484  CheckDefExecFailure(['let items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 4)
485
486  let bb = 0z3f
487  assert_equal(true, 0z3f == bb)
488  assert_equal(false, bb == 0z4f)
489  assert_equal(true, g:ablob == 0z01ab)
490  assert_equal(false, 0z3f == g:ablob)
491
492  assert_equal(true, [1, 2, 3] == [1, 2, 3])
493  assert_equal(false, [1, 2, 3] == [2, 3, 1])
494  assert_equal(true, [2, 3, 4] == g:alist)
495  assert_equal(false, g:alist == [2, 3, 1])
496  assert_equal(false, [1, 2, 3] == [])
497  assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
498
499  assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
500  assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
501  assert_equal(false, #{one: 1, two: 2} == #{two: 2})
502  assert_equal(false, #{one: 1, two: 2} == #{})
503  assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
504  assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
505
506  assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal'))
507  assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is'))
508
509  assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123]))
510  assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123]))
511  assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999]))
512
513  let OneFunc: func
514  let TwoFunc: func
515  OneFunc = function('len')
516  TwoFunc = function('len')
517  assert_equal(true, OneFunc('abc') == TwoFunc('123'))
518enddef
519
520" test != comperator
521def Test_expr4_notequal()
522  let trueVar = true
523  let falseVar = false
524  assert_equal(false, true != true)
525  assert_equal(true, true !=
526			false)
527  assert_equal(false, true
528  			!= trueVar)
529  assert_equal(true, true != falseVar)
530  assert_equal(false, true != g:atrue)
531  assert_equal(true, g:atrue != false)
532
533  assert_equal(false, v:none != v:none)
534  assert_equal(true, v:none != v:null)
535  assert_equal(false, g:anone != v:none)
536  assert_equal(true, v:none != g:anull)
537
538  let nr55 = 55
539  let nr0 = 55
540  assert_equal(true, 2 != 0)
541  assert_equal(true, 2 != nr0)
542  assert_equal(false, 55 != 55)
543  assert_equal(false, 55 != nr55)
544  assert_equal(false, g:anint != 10)
545  assert_equal(true, 61 != g:anint)
546
547  if has('float')
548    let ff = 0.3
549    assert_equal(false, 0.3 != ff)
550    assert_equal(true, 0.4 != ff)
551    assert_equal(false, 0.1 != g:afloat)
552    assert_equal(true, g:afloat != 0.3)
553
554    ff = 3.0
555    assert_equal(false, ff != 3)
556    assert_equal(false, 3 != ff)
557    ff = 3.1
558    assert_equal(true, ff != 3)
559    assert_equal(true, 3 != ff)
560  endif
561
562  assert_equal(false, 'abc' != 'abc')
563  assert_equal(true, 'xyz' != 'abc')
564  assert_equal(false, g:astring != 'asdf')
565  assert_equal(true, 'xyz' != g:astring)
566
567  assert_equal(true, 'abc' != 'ABC')
568  set ignorecase
569  assert_equal(true, 'abc' != 'ABC')
570  set noignorecase
571
572  let bb = 0z3f
573  assert_equal(false, 0z3f != bb)
574  assert_equal(true, bb != 0z4f)
575  assert_equal(false, g:ablob != 0z01ab)
576  assert_equal(true, 0z3f != g:ablob)
577
578  assert_equal(false, [1, 2, 3] != [1, 2, 3])
579  assert_equal(true, [1, 2, 3] != [2, 3, 1])
580  assert_equal(false, [2, 3, 4] != g:alist)
581  assert_equal(true, g:alist != [2, 3, 1])
582  assert_equal(true, [1, 2, 3] != [])
583  assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
584
585  assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
586  assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
587  assert_equal(true, #{one: 1, two: 2} != #{two: 2})
588  assert_equal(true, #{one: 1, two: 2} != #{})
589  assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
590  assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
591
592  assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal'))
593  assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is'))
594
595  assert_equal(false, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [123]))
596  assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_is', [123]))
597  assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [999]))
598enddef
599
600" test > comperator
601def Test_expr4_greater()
602  assert_true(2 > 0)
603  assert_true(2 >
604		1)
605  assert_false(2 > 2)
606  assert_false(2 > 3)
607  let nr2 = 2
608  assert_true(nr2 > 0)
609  assert_true(nr2 >
610		1)
611  assert_false(nr2 > 2)
612  assert_false(nr2
613  		    > 3)
614  if has('float')
615    let ff = 2.0
616    assert_true(ff > 0.0)
617    assert_true(ff > 1.0)
618    assert_false(ff > 2.0)
619    assert_false(ff > 3.0)
620  endif
621enddef
622
623" test >= comperator
624def Test_expr4_greaterequal()
625  assert_true(2 >= 0)
626  assert_true(2 >=
627			2)
628  assert_false(2 >= 3)
629  let nr2 = 2
630  assert_true(nr2 >= 0)
631  assert_true(nr2 >= 2)
632  assert_false(nr2 >= 3)
633  if has('float')
634    let ff = 2.0
635    assert_true(ff >= 0.0)
636    assert_true(ff >= 2.0)
637    assert_false(ff >= 3.0)
638  endif
639enddef
640
641" test < comperator
642def Test_expr4_smaller()
643  assert_false(2 < 0)
644  assert_false(2 <
645			2)
646  assert_true(2
647  		< 3)
648  let nr2 = 2
649  assert_false(nr2 < 0)
650  assert_false(nr2 < 2)
651  assert_true(nr2 < 3)
652  if has('float')
653    let ff = 2.0
654    assert_false(ff < 0.0)
655    assert_false(ff < 2.0)
656    assert_true(ff < 3.0)
657  endif
658enddef
659
660" test <= comperator
661def Test_expr4_smallerequal()
662  assert_false(2 <= 0)
663  assert_false(2 <=
664			1)
665  assert_true(2
666  		<= 2)
667  assert_true(2 <= 3)
668  let nr2 = 2
669  assert_false(nr2 <= 0)
670  assert_false(nr2 <= 1)
671  assert_true(nr2 <= 2)
672  assert_true(nr2 <= 3)
673  if has('float')
674    let ff = 2.0
675    assert_false(ff <= 0.0)
676    assert_false(ff <= 1.0)
677    assert_true(ff <= 2.0)
678    assert_true(ff <= 3.0)
679  endif
680enddef
681
682" test =~ comperator
683def Test_expr4_match()
684  assert_equal(false, '2' =~ '0')
685  assert_equal(false, ''
686  			 =~ '0')
687  assert_equal(true, '2' =~
688			'[0-9]')
689enddef
690
691" test !~ comperator
692def Test_expr4_nomatch()
693  assert_equal(true, '2' !~ '0')
694  assert_equal(true, ''
695  			!~ '0')
696  assert_equal(false, '2' !~
697			'[0-9]')
698enddef
699
700" test is comperator
701def Test_expr4_is()
702  let mylist = [2]
703  assert_false(mylist is [2])
704  let other = mylist
705  assert_true(mylist is
706		other)
707
708  let myblob = 0z1234
709  assert_false(myblob
710  			is 0z1234)
711  let otherblob = myblob
712  assert_true(myblob is otherblob)
713enddef
714
715" test isnot comperator
716def Test_expr4_isnot()
717  let mylist = [2]
718  assert_true('2' isnot '0')
719  assert_true(mylist isnot [2])
720  let other = mylist
721  assert_false(mylist isnot
722			other)
723
724  let myblob = 0z1234
725  assert_true(myblob
726  		isnot 0z1234)
727  let otherblob = myblob
728  assert_false(myblob isnot otherblob)
729enddef
730
731def RetVoid()
732  let x = 1
733enddef
734
735def Test_expr4_vim9script()
736  # check line continuation
737  let lines =<< trim END
738      vim9script
739      let var = 0
740      		< 1
741      assert_equal(true, var)
742  END
743  CheckScriptSuccess(lines)
744
745  lines =<< trim END
746      vim9script
747      let var = 123
748      		!= 123
749      assert_equal(false, var)
750  END
751  CheckScriptSuccess(lines)
752
753  lines =<< trim END
754      vim9script
755      let var = 123 ==
756      			123
757      assert_equal(true, var)
758  END
759  CheckScriptSuccess(lines)
760
761  lines =<< trim END
762      vim9script
763      let list = [1, 2, 3]
764      let var = list
765      		is list
766      assert_equal(true, var)
767  END
768  CheckScriptSuccess(lines)
769
770  lines =<< trim END
771      vim9script
772      let myblob = 0z1234
773      let var = myblob
774      		isnot 0z11
775      assert_equal(true, var)
776  END
777  CheckScriptSuccess(lines)
778
779  # spot check mismatching types
780  lines =<< trim END
781      vim9script
782      echo '' == 0
783  END
784  CheckScriptFailure(lines, 'E1072:', 2)
785
786  lines =<< trim END
787      vim9script
788      echo v:true > v:false
789  END
790  CheckScriptFailure(lines, 'Cannot compare bool with bool', 2)
791
792  lines =<< trim END
793      vim9script
794      echo 123 is 123
795  END
796  CheckScriptFailure(lines, 'Cannot use "is" with number', 2)
797
798  # check 'ignorecase' not being used
799  lines =<< trim END
800    vim9script
801    set ignorecase
802    assert_equal(false, 'abc' == 'ABC')
803    assert_equal(false, 'abc' ==# 'ABC')
804    assert_equal(true, 'abc' ==? 'ABC')
805
806    assert_equal(true, 'abc' != 'ABC')
807    assert_equal(true, 'abc' !=# 'ABC')
808    assert_equal(false, 'abc' !=? 'ABC')
809
810    assert_equal(false, 'abc' =~ 'ABC')
811    assert_equal(false, 'abc' =~# 'ABC')
812    assert_equal(true, 'abc' =~? 'ABC')
813    set noignorecase
814  END
815  CheckScriptSuccess(lines)
816
817  # check missing white space
818  lines =<< trim END
819    vim9script
820    echo 2>3
821  END
822  CheckScriptFailure(lines, 'E1004:', 2)
823  lines =<< trim END
824    vim9script
825    echo 2 >3
826  END
827  CheckScriptFailure(lines, 'E1004:', 2)
828  lines =<< trim END
829    vim9script
830    echo 2> 3
831  END
832  CheckScriptFailure(lines, 'E1004:', 2)
833  lines =<< trim END
834    vim9script
835    echo 2!=3
836  END
837  CheckScriptFailure(lines, 'E1004:', 2)
838  lines =<< trim END
839    vim9script
840    echo 2 !=3
841  END
842  CheckScriptFailure(lines, 'E1004:', 2)
843  lines =<< trim END
844    vim9script
845    echo 2!= 3
846  END
847  CheckScriptFailure(lines, 'E1004:', 2)
848
849  lines =<< trim END
850    vim9script
851    echo len('xxx') == 3
852  END
853  CheckScriptSuccess(lines)
854
855  lines =<< trim END
856    vim9script
857    let line = 'abc'
858    echo line[1] =~ '\w'
859  END
860  CheckScriptSuccess(lines)
861enddef
862
863func Test_expr4_fails()
864  let msg = "White space required before and after '>'"
865  call CheckDefFailure(["let x = 1>2"], msg, 1)
866  call CheckDefFailure(["let x = 1 >2"], msg, 1)
867  call CheckDefFailure(["let x = 1> 2"], msg, 1)
868
869  let msg = "White space required before and after '=='"
870  call CheckDefFailure(["let x = 1==2"], msg, 1)
871  call CheckDefFailure(["let x = 1 ==2"], msg, 1)
872  call CheckDefFailure(["let x = 1== 2"], msg, 1)
873
874  let msg = "White space required before and after 'is'"
875  call CheckDefFailure(["let x = '1'is'2'"], msg, 1)
876  call CheckDefFailure(["let x = '1' is'2'"], msg, 1)
877  call CheckDefFailure(["let x = '1'is '2'"], msg, 1)
878
879  let msg = "White space required before and after 'isnot'"
880  call CheckDefFailure(["let x = '1'isnot'2'"], msg, 1)
881  call CheckDefFailure(["let x = '1' isnot'2'"], msg, 1)
882  call CheckDefFailure(["let x = '1'isnot '2'"], msg, 1)
883
884  call CheckDefFailure(["let x = 1 is# 2"], 'E15:', 1)
885  call CheckDefFailure(["let x = 1 is? 2"], 'E15:', 1)
886  call CheckDefFailure(["let x = 1 isnot# 2"], 'E15:', 1)
887  call CheckDefFailure(["let x = 1 isnot? 2"], 'E15:', 1)
888
889  call CheckDefFailure(["let x = 1 == '2'"], 'Cannot compare number with string', 1)
890  call CheckDefFailure(["let x = '1' == 2"], 'Cannot compare string with number', 1)
891  call CheckDefFailure(["let x = 1 == RetVoid()"], 'Cannot compare number with void', 1)
892  call CheckDefFailure(["let x = RetVoid() == 1"], 'Cannot compare void with number', 1)
893
894  call CheckDefFailure(["let x = true > false"], 'Cannot compare bool with bool', 1)
895  call CheckDefFailure(["let x = true >= false"], 'Cannot compare bool with bool', 1)
896  call CheckDefFailure(["let x = true < false"], 'Cannot compare bool with bool', 1)
897  call CheckDefFailure(["let x = true <= false"], 'Cannot compare bool with bool', 1)
898  call CheckDefFailure(["let x = true =~ false"], 'Cannot compare bool with bool', 1)
899  call CheckDefFailure(["let x = true !~ false"], 'Cannot compare bool with bool', 1)
900  call CheckDefFailure(["let x = true is false"], 'Cannot use "is" with bool', 1)
901  call CheckDefFailure(["let x = true isnot false"], 'Cannot use "isnot" with bool', 1)
902
903  call CheckDefFailure(["let x = v:none is v:null"], 'Cannot use "is" with special', 1)
904  call CheckDefFailure(["let x = v:none isnot v:null"], 'Cannot use "isnot" with special', 1)
905  call CheckDefFailure(["let x = 123 is 123"], 'Cannot use "is" with number', 1)
906  call CheckDefFailure(["let x = 123 isnot 123"], 'Cannot use "isnot" with number', 1)
907  if has('float')
908    call CheckDefFailure(["let x = 1.3 is 1.3"], 'Cannot use "is" with float', 1)
909    call CheckDefFailure(["let x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float', 1)
910  endif
911
912  call CheckDefFailure(["let x = 0za1 > 0z34"], 'Cannot compare blob with blob', 1)
913  call CheckDefFailure(["let x = 0za1 >= 0z34"], 'Cannot compare blob with blob', 1)
914  call CheckDefFailure(["let x = 0za1 < 0z34"], 'Cannot compare blob with blob', 1)
915  call CheckDefFailure(["let x = 0za1 <= 0z34"], 'Cannot compare blob with blob', 1)
916  call CheckDefFailure(["let x = 0za1 =~ 0z34"], 'Cannot compare blob with blob', 1)
917  call CheckDefFailure(["let x = 0za1 !~ 0z34"], 'Cannot compare blob with blob', 1)
918
919  call CheckDefFailure(["let x = [13] > [88]"], 'Cannot compare list with list', 1)
920  call CheckDefFailure(["let x = [13] >= [88]"], 'Cannot compare list with list', 1)
921  call CheckDefFailure(["let x = [13] < [88]"], 'Cannot compare list with list', 1)
922  call CheckDefFailure(["let x = [13] <= [88]"], 'Cannot compare list with list', 1)
923  call CheckDefFailure(["let x = [13] =~ [88]"], 'Cannot compare list with list', 1)
924  call CheckDefFailure(["let x = [13] !~ [88]"], 'Cannot compare list with list', 1)
925
926  call CheckDefFailure(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel', 3)
927  call CheckDefFailure(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list', 3)
928  call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func', 3)
929  call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func', 3)
930endfunc
931
932" test addition, subtraction, concatenation
933def Test_expr5()
934  assert_equal(66, 60 + 6)
935  assert_equal(70, 60 +
936			g:anint)
937  assert_equal(9, g:thefour
938  			+ 5)
939  assert_equal(14, g:thefour + g:anint)
940  assert_equal([1, 2, 3, 4], [1] + g:alist)
941
942  assert_equal(54, 60 - 6)
943  assert_equal(50, 60 -
944		    g:anint)
945  assert_equal(-1, g:thefour
946  			- 5)
947  assert_equal(-6, g:thefour - g:anint)
948
949  assert_equal('hello', 'hel' .. 'lo')
950  assert_equal('hello 123', 'hello ' ..
951					123)
952  assert_equal('hello 123', 'hello '
953  				..  123)
954  assert_equal('123 hello', 123 .. ' hello')
955  assert_equal('123456', 123 .. 456)
956
957  assert_equal('av:true', 'a' .. true)
958  assert_equal('av:false', 'a' .. false)
959  assert_equal('av:null', 'a' .. v:null)
960  assert_equal('av:none', 'a' .. v:none)
961  if has('float')
962    assert_equal('a0.123', 'a' .. 0.123)
963  endif
964
965  assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
966  assert_equal(0z11223344, 0z1122 + 0z3344)
967  assert_equal(0z112201ab, 0z1122
968  				+ g:ablob)
969  assert_equal(0z01ab3344, g:ablob + 0z3344)
970  assert_equal(0z01ab01ab, g:ablob + g:ablob)
971
972  # concatenate non-constant to constant
973  let save_path = &path
974  &path = 'b'
975  assert_equal('ab', 'a' .. &path)
976  &path = save_path
977
978  @b = 'b'
979  assert_equal('ab', 'a' .. @b)
980
981  $ENVVAR = 'env'
982  assert_equal('aenv', 'a' .. $ENVVAR)
983enddef
984
985def Test_expr5_vim9script()
986  # check line continuation
987  let lines =<< trim END
988      vim9script
989      let var = 11
990      		+ 77
991		- 22
992      assert_equal(66, var)
993  END
994  CheckScriptSuccess(lines)
995
996  lines =<< trim END
997      vim9script
998      let var = 11 +
999		  77 -
1000		  22
1001      assert_equal(66, var)
1002  END
1003  CheckScriptSuccess(lines)
1004
1005  lines =<< trim END
1006      vim9script
1007      let var = 'one'
1008      		.. 'two'
1009      assert_equal('onetwo', var)
1010  END
1011  CheckScriptSuccess(lines)
1012
1013  lines =<< trim END
1014      vim9script
1015      echo 'abc' is# 'abc'
1016  END
1017  CheckScriptFailure(lines, 'E15:', 2)
1018
1019  lines =<< trim END
1020      vim9script
1021      echo 'abc' is? 'abc'
1022  END
1023  CheckScriptFailure(lines, 'E15:', 2)
1024
1025  lines =<< trim END
1026      vim9script
1027      echo 'abc' isnot# 'abc'
1028  END
1029  CheckScriptFailure(lines, 'E15:', 2)
1030
1031  lines =<< trim END
1032      vim9script
1033      echo 'abc' isnot? 'abc'
1034  END
1035  CheckScriptFailure(lines, 'E15:', 2)
1036
1037  # check white space
1038  lines =<< trim END
1039      vim9script
1040      echo 5+6
1041  END
1042  CheckScriptFailure(lines, 'E1004:', 2)
1043  lines =<< trim END
1044      vim9script
1045      echo 5 +6
1046  END
1047  CheckScriptFailure(lines, 'E1004:', 2)
1048  lines =<< trim END
1049      vim9script
1050      echo 5+ 6
1051  END
1052  CheckScriptFailure(lines, 'E1004:', 2)
1053
1054  lines =<< trim END
1055      vim9script
1056      echo 'a'..'b'
1057  END
1058  CheckScriptFailure(lines, 'E1004:', 2)
1059  lines =<< trim END
1060      vim9script
1061      echo 'a' ..'b'
1062  END
1063  CheckScriptFailure(lines, 'E1004:', 2)
1064  lines =<< trim END
1065      vim9script
1066      echo 'a'.. 'b'
1067  END
1068  CheckScriptFailure(lines, 'E1004:', 2)
1069
1070  # check valid string concatenation
1071  lines =<< trim END
1072      vim9script
1073      assert_equal('one123', 'one' .. 123)
1074      assert_equal('onev:true', 'one' .. true)
1075      assert_equal('onev:null', 'one' .. v:null)
1076      assert_equal('onev:none', 'one' .. v:none)
1077      if has('float')
1078        assert_equal('a0.123', 'a' .. 0.123)
1079      endif
1080  END
1081  CheckScriptSuccess(lines)
1082
1083  # check invalid string concatenation
1084  lines =<< trim END
1085      vim9script
1086      echo 'a' .. [1]
1087  END
1088  CheckScriptFailure(lines, 'E730:', 2)
1089  lines =<< trim END
1090      vim9script
1091      echo 'a' .. #{a: 1}
1092  END
1093  CheckScriptFailure(lines, 'E731:', 2)
1094  lines =<< trim END
1095      vim9script
1096      echo 'a' .. test_void()
1097  END
1098  CheckScriptFailure(lines, 'E908:', 2)
1099  lines =<< trim END
1100      vim9script
1101      echo 'a' .. 0z33
1102  END
1103  CheckScriptFailure(lines, 'E976:', 2)
1104  lines =<< trim END
1105      vim9script
1106      echo 'a' .. function('len')
1107  END
1108  CheckScriptFailure(lines, 'E729:', 2)
1109enddef
1110
1111def Test_expr5_vim9script_channel()
1112  if !has('channel')
1113    MissingFeature 'float'
1114  else
1115    let lines =<< trim END
1116        vim9script
1117        echo 'a' .. test_null_job()
1118    END
1119    CheckScriptFailure(lines, 'E908:', 2)
1120    lines =<< trim END
1121        vim9script
1122        echo 'a' .. test_null_channel()
1123    END
1124    CheckScriptFailure(lines, 'E908:', 2)
1125  endif
1126enddef
1127
1128def Test_expr5_float()
1129  if !has('float')
1130    MissingFeature 'float'
1131  else
1132    assert_equal(66.0, 60.0 + 6.0)
1133    assert_equal(66.0, 60.0 + 6)
1134    assert_equal(66.0, 60 +
1135			 6.0)
1136    assert_equal(5.1, g:afloat
1137    			+ 5)
1138    assert_equal(8.1, 8 + g:afloat)
1139    assert_equal(10.1, g:anint + g:afloat)
1140    assert_equal(10.1, g:afloat + g:anint)
1141
1142    assert_equal(54.0, 60.0 - 6.0)
1143    assert_equal(54.0, 60.0
1144    			    - 6)
1145    assert_equal(54.0, 60 - 6.0)
1146    assert_equal(-4.9, g:afloat - 5)
1147    assert_equal(7.9, 8 - g:afloat)
1148    assert_equal(9.9, g:anint - g:afloat)
1149    assert_equal(-9.9, g:afloat - g:anint)
1150  endif
1151enddef
1152
1153func Test_expr5_fails()
1154  let msg = "White space required before and after '+'"
1155  call CheckDefFailure(["let x = 1+2"], msg, 1)
1156  call CheckDefFailure(["let x = 1 +2"], msg, 1)
1157  call CheckDefFailure(["let x = 1+ 2"], msg, 1)
1158
1159  let msg = "White space required before and after '-'"
1160  call CheckDefFailure(["let x = 1-2"], msg, 1)
1161  call CheckDefFailure(["let x = 1 -2"], msg, 1)
1162  call CheckDefFailure(["let x = 1- 2"], msg, 1)
1163
1164  let msg = "White space required before and after '..'"
1165  call CheckDefFailure(["let x = '1'..'2'"], msg, 1)
1166  call CheckDefFailure(["let x = '1' ..'2'"], msg, 1)
1167  call CheckDefFailure(["let x = '1'.. '2'"], msg, 1)
1168
1169  call CheckDefFailure(["let x = 0z1122 + 33"], 'E1051', 1)
1170  call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1051', 1)
1171  call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1051', 1)
1172  call CheckDefFailure(["let x = 33 + 0z1122"], 'E1051', 1)
1173  call CheckDefFailure(["let x = [3] + 0z1122"], 'E1051', 1)
1174  call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1051', 1)
1175  call CheckDefFailure(["let x = 6 + xxx"], 'E1001', 1)
1176
1177  call CheckDefFailure(["let x = 'a' .. [1]"], 'E1105', 1)
1178  call CheckDefFailure(["let x = 'a' .. #{a: 1}"], 'E1105', 1)
1179  call CheckDefFailure(["let x = 'a' .. test_void()"], 'E1105', 1)
1180  call CheckDefFailure(["let x = 'a' .. 0z32"], 'E1105', 1)
1181  call CheckDefFailure(["let x = 'a' .. function('len')"], 'E1105', 1)
1182  call CheckDefFailure(["let x = 'a' .. function('len', ['a'])"], 'E1105', 1)
1183endfunc
1184
1185func Test_expr5_fails_channel()
1186  CheckFeature channel
1187  call CheckDefFailure(["let x = 'a' .. test_null_job()"], 'E1105', 1)
1188  call CheckDefFailure(["let x = 'a' .. test_null_channel()"], 'E1105', 1)
1189endfunc
1190
1191" test multiply, divide, modulo
1192def Test_expr6()
1193  assert_equal(36, 6 * 6)
1194  assert_equal(24, 6 *
1195			g:thefour)
1196  assert_equal(24, g:thefour
1197  			* 6)
1198  assert_equal(40, g:anint * g:thefour)
1199
1200  assert_equal(10, 60 / 6)
1201  assert_equal(6, 60 /
1202			g:anint)
1203  assert_equal(1, g:anint / 6)
1204  assert_equal(2, g:anint
1205  			/ g:thefour)
1206
1207  assert_equal(5, 11 % 6)
1208  assert_equal(4, g:anint % 6)
1209  assert_equal(3, 13 %
1210			g:anint)
1211  assert_equal(2, g:anint
1212  			% g:thefour)
1213
1214  assert_equal(4, 6 * 4 / 6)
1215
1216  let x = [2]
1217  let y = [3]
1218  assert_equal(5, x[0] + y[0])
1219  assert_equal(6, x[0] * y[0])
1220  if has('float')
1221    let xf = [2.0]
1222    let yf = [3.0]
1223    assert_equal(5.0, xf[0]
1224    			+ yf[0])
1225    assert_equal(6.0, xf[0]
1226    			* yf[0])
1227  endif
1228
1229  CheckDefFailure(["let x = 6 * xxx"], 'E1001', 1)
1230enddef
1231
1232def Test_expr6_vim9script()
1233  # check line continuation
1234  let lines =<< trim END
1235      vim9script
1236      let var = 11
1237      		* 22
1238		/ 3
1239      assert_equal(80, var)
1240  END
1241  CheckScriptSuccess(lines)
1242
1243  lines =<< trim END
1244      vim9script
1245      let var = 25
1246      		% 10
1247      assert_equal(5, var)
1248  END
1249  CheckScriptSuccess(lines)
1250
1251  lines =<< trim END
1252      vim9script
1253      let var = 11 *
1254      		22 /
1255		3
1256      assert_equal(80, var)
1257  END
1258  CheckScriptSuccess(lines)
1259
1260  # check white space
1261  lines =<< trim END
1262      vim9script
1263      echo 5*6
1264  END
1265  CheckScriptFailure(lines, 'E1004:', 2)
1266  lines =<< trim END
1267      vim9script
1268      echo 5 *6
1269  END
1270  CheckScriptFailure(lines, 'E1004:', 2)
1271  lines =<< trim END
1272      vim9script
1273      echo 5* 6
1274  END
1275  CheckScriptFailure(lines, 'E1004:', 2)
1276enddef
1277
1278def Test_expr6_float()
1279  if !has('float')
1280    MissingFeature 'float'
1281  else
1282    assert_equal(36.0, 6.0 * 6)
1283    assert_equal(36.0, 6 *
1284			   6.0)
1285    assert_equal(36.0, 6.0 * 6.0)
1286    assert_equal(1.0, g:afloat * g:anint)
1287
1288    assert_equal(10.0, 60 / 6.0)
1289    assert_equal(10.0, 60.0 /
1290			6)
1291    assert_equal(10.0, 60.0 / 6.0)
1292    assert_equal(0.01, g:afloat / g:anint)
1293
1294    assert_equal(4.0, 6.0 * 4 / 6)
1295    assert_equal(4.0, 6 *
1296			4.0 /
1297			6)
1298    assert_equal(4.0, 6 * 4 / 6.0)
1299    assert_equal(4.0, 6.0 * 4.0 / 6)
1300    assert_equal(4.0, 6 * 4.0 / 6.0)
1301    assert_equal(4.0, 6.0 * 4 / 6.0)
1302    assert_equal(4.0, 6.0 * 4.0 / 6.0)
1303
1304    assert_equal(4.0, 6.0 * 4.0 / 6.0)
1305  endif
1306enddef
1307
1308func Test_expr6_fails()
1309  let msg = "White space required before and after '*'"
1310  call CheckDefFailure(["let x = 1*2"], msg, 1)
1311  call CheckDefFailure(["let x = 1 *2"], msg, 1)
1312  call CheckDefFailure(["let x = 1* 2"], msg, 1)
1313
1314  let msg = "White space required before and after '/'"
1315  call CheckDefFailure(["let x = 1/2"], msg, 1)
1316  call CheckDefFailure(["let x = 1 /2"], msg, 1)
1317  call CheckDefFailure(["let x = 1/ 2"], msg, 1)
1318
1319  let msg = "White space required before and after '%'"
1320  call CheckDefFailure(["let x = 1%2"], msg, 1)
1321  call CheckDefFailure(["let x = 1 %2"], msg, 1)
1322  call CheckDefFailure(["let x = 1% 2"], msg, 1)
1323
1324  call CheckDefFailure(["let x = '1' * '2'"], 'E1036:', 1)
1325  call CheckDefFailure(["let x = '1' / '2'"], 'E1036:', 1)
1326  call CheckDefFailure(["let x = '1' % '2'"], 'E1035:', 1)
1327
1328  call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:', 1)
1329  call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:', 1)
1330  call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:', 1)
1331
1332  call CheckDefFailure(["let x = [1] * [2]"], 'E1036:', 1)
1333  call CheckDefFailure(["let x = [1] / [2]"], 'E1036:', 1)
1334  call CheckDefFailure(["let x = [1] % [2]"], 'E1035:', 1)
1335
1336  call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:', 1)
1337  call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:', 1)
1338  call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:', 1)
1339
1340  call CheckDefFailure(["let x = 0xff[1]"], 'E1107:', 1)
1341  if has('float')
1342    call CheckDefFailure(["let x = 0.7[1]"], 'E1107:', 1)
1343  endif
1344endfunc
1345
1346func Test_expr6_float_fails()
1347  CheckFeature float
1348  call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:', 1)
1349endfunc
1350
1351" define here to use old style parsing
1352if has('float')
1353  let g:float_zero = 0.0
1354  let g:float_neg = -9.8
1355  let g:float_big = 9.9e99
1356endif
1357let g:blob_empty = 0z
1358let g:blob_one = 0z01
1359let g:blob_long = 0z0102.0304
1360
1361let g:string_empty = ''
1362let g:string_short = 'x'
1363let g:string_long = 'abcdefghijklm'
1364let g:string_special = "ab\ncd\ref\ekk"
1365
1366let g:special_true = v:true
1367let g:special_false = v:false
1368let g:special_null = v:null
1369let g:special_none = v:none
1370
1371let g:list_empty = []
1372let g:list_mixed = [1, 'b', v:false]
1373
1374let g:dict_empty = {}
1375let g:dict_one = #{one: 1}
1376
1377let $TESTVAR = 'testvar'
1378
1379" type casts
1380def Test_expr7t()
1381  let ls: list<string> = ['a', <string>g:string_empty]
1382  let ln: list<number> = [<number>g:anint, <number>g:thefour]
1383  let nr = <number>234
1384  assert_equal(234, nr)
1385
1386  CheckDefFailure(["let x = <nr>123"], 'E1010:', 1)
1387  CheckDefFailure(["let x = <number >123"], 'E1068:', 1)
1388  CheckDefFailure(["let x = <number 123"], 'E1104:', 1)
1389enddef
1390
1391" test low level expression
1392def Test_expr7_number()
1393  # number constant
1394  assert_equal(0, 0)
1395  assert_equal(654, 0654)
1396
1397  assert_equal(6, 0x6)
1398  assert_equal(15, 0xf)
1399  assert_equal(255, 0xff)
1400enddef
1401
1402def Test_expr7_float()
1403  # float constant
1404  if !has('float')
1405    MissingFeature 'float'
1406  else
1407    assert_equal(g:float_zero, .0)
1408    assert_equal(g:float_zero, 0.0)
1409    assert_equal(g:float_neg, -9.8)
1410    assert_equal(g:float_big, 9.9e99)
1411  endif
1412enddef
1413
1414def Test_expr7_blob()
1415  # blob constant
1416  assert_equal(g:blob_empty, 0z)
1417  assert_equal(g:blob_one, 0z01)
1418  assert_equal(g:blob_long, 0z0102.0304)
1419
1420  CheckDefFailure(["let x = 0z123"], 'E973:', 1)
1421enddef
1422
1423def Test_expr7_string()
1424  # string constant
1425  assert_equal(g:string_empty, '')
1426  assert_equal(g:string_empty, "")
1427  assert_equal(g:string_short, 'x')
1428  assert_equal(g:string_short, "x")
1429  assert_equal(g:string_long, 'abcdefghijklm')
1430  assert_equal(g:string_long, "abcdefghijklm")
1431  assert_equal(g:string_special, "ab\ncd\ref\ekk")
1432
1433  CheckDefFailure(['let x = "abc'], 'E114:', 1)
1434  CheckDefFailure(["let x = 'abc"], 'E115:', 1)
1435enddef
1436
1437def Test_expr7_vimvar()
1438  let old: list<string> = v:oldfiles
1439  let compl: dict<any> = v:completed_item
1440
1441  CheckDefFailure(["let old: list<number> = v:oldfiles"], 'E1012: Type mismatch; expected list<number> but got list<string>', 1)
1442  new
1443  exec "normal! afoo fo\<C-N>\<Esc>"
1444  CheckDefExecFailure(["let old: dict<number> = v:completed_item"], 'E1012: Type mismatch; expected dict<number> but got dict<string>', 1)
1445  bwipe!
1446enddef
1447
1448def Test_expr7_special()
1449  # special constant
1450  assert_equal(g:special_true, true)
1451  assert_equal(g:special_false, false)
1452  assert_equal(g:special_true, v:true)
1453  assert_equal(g:special_false, v:false)
1454
1455  assert_equal(true, !false)
1456  assert_equal(false, !true)
1457  assert_equal(true, !0)
1458  assert_equal(false, !1)
1459  assert_equal(false, !!false)
1460  assert_equal(true, !!true)
1461  assert_equal(false, !!0)
1462  assert_equal(true, !!1)
1463
1464  assert_equal(g:special_null, v:null)
1465  assert_equal(g:special_none, v:none)
1466
1467  CheckDefFailure(['v:true = true'], 'E46:', 1)
1468  CheckDefFailure(['v:true = false'], 'E46:', 1)
1469  CheckDefFailure(['v:false = true'], 'E46:', 1)
1470  CheckDefFailure(['v:null = 11'], 'E46:', 1)
1471  CheckDefFailure(['v:none = 22'], 'E46:', 1)
1472enddef
1473
1474def Test_expr7_special_vim9script()
1475  let lines =<< trim END
1476      vim9script
1477      let t = true
1478      let f = false
1479      assert_equal(v:true, true)
1480      assert_equal(true, t)
1481      assert_equal(v:false, false)
1482      assert_equal(false, f)
1483      assert_equal(true, !false)
1484      assert_equal(false, !true)
1485      assert_equal(true, !0)
1486      assert_equal(false, !1)
1487      assert_equal(false, !!false)
1488      assert_equal(true, !!true)
1489      assert_equal(false, !!0)
1490      assert_equal(true, !!1)
1491  END
1492  CheckScriptSuccess(lines)
1493enddef
1494
1495def Test_expr7_list()
1496  # list
1497  assert_equal(g:list_empty, [])
1498  assert_equal(g:list_empty, [  ])
1499
1500  let numbers: list<number> = [1, 2, 3]
1501  numbers = [1]
1502  numbers = []
1503
1504  let strings: list<string> = ['a', 'b', 'c']
1505  strings = ['x']
1506  strings = []
1507
1508  let mixed: list<any> = [1, 'b', false,]
1509  assert_equal(g:list_mixed, mixed)
1510  assert_equal('b', mixed[1])
1511
1512  echo [1,
1513  	2] [3,
1514		4]
1515
1516  let llstring: list<list<string>> = [['text'], []]
1517  llstring = [[], ['text']]
1518  llstring = [[], []]
1519
1520  CheckDefFailure(["let x = 1234[3]"], 'E1107:', 1)
1521  CheckDefExecFailure(["let x = g:anint[3]"], 'E1062:', 1)
1522
1523  CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:', 1)
1524
1525  CheckDefFailure(["let x = [1,2,3]"], 'E1069:', 1)
1526  CheckDefFailure(["let x = [1 ,2, 3]"], 'E1068:', 1)
1527
1528  CheckDefExecFailure(["echo 1", "let x = [][0]", "echo 3"], 'E684:', 2)
1529
1530  CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E1012:', 1)
1531  CheckDefFailure(["let x = g:list_mixed["], 'E1097:', 2)
1532  CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:', 2)
1533  CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:', 1)
1534  CheckDefExecFailure(["let l: list<number> = [234, 'x']"], 'E1012:', 1)
1535  CheckDefExecFailure(["let l: list<number> = ['x', 234]"], 'E1012:', 1)
1536  CheckDefExecFailure(["let l: list<string> = [234, 'x']"], 'E1012:', 1)
1537  CheckDefExecFailure(["let l: list<string> = ['x', 123]"], 'E1012:', 1)
1538enddef
1539
1540def Test_expr7_list_vim9script()
1541  let lines =<< trim END
1542      vim9script
1543      let l = [
1544		11,
1545		22,
1546		]
1547      assert_equal([11, 22], l)
1548
1549      echo [1,
1550	    2] [3,
1551		    4]
1552  END
1553  CheckScriptSuccess(lines)
1554
1555  lines =<< trim END
1556      vim9script
1557      let l = [11,
1558		22]
1559      assert_equal([11, 22], l)
1560  END
1561  CheckScriptSuccess(lines)
1562
1563  lines =<< trim END
1564      vim9script
1565      let l = [11,22]
1566  END
1567  CheckScriptFailure(lines, 'E1069:', 2)
1568
1569  lines =<< trim END
1570      vim9script
1571      let l = [11 , 22]
1572  END
1573  CheckScriptFailure(lines, 'E1068:', 2)
1574
1575  lines =<< trim END
1576    vim9script
1577    let l: list<number> = [234, 'x']
1578  END
1579  CheckScriptFailure(lines, 'E1012:', 2)
1580  lines =<< trim END
1581    vim9script
1582    let l: list<number> = ['x', 234]
1583  END
1584  CheckScriptFailure(lines, 'E1012:', 2)
1585  lines =<< trim END
1586    vim9script
1587    let l: list<string> = ['x', 234]
1588  END
1589  CheckScriptFailure(lines, 'E1012:', 2)
1590  lines =<< trim END
1591    vim9script
1592    let l: list<string> = [234, 'x']
1593  END
1594  CheckScriptFailure(lines, 'E1012:', 2)
1595enddef
1596
1597def LambdaWithComments(): func
1598  return {x ->
1599            # some comment
1600            x == 1
1601            # some comment
1602            ||
1603            x == 2
1604        }
1605enddef
1606
1607def LambdaUsingArg(x: number): func
1608  return {->
1609            # some comment
1610            x == 1
1611            # some comment
1612            ||
1613            x == 2
1614        }
1615enddef
1616
1617def Test_expr7_lambda()
1618  let La = { -> 'result'}
1619  assert_equal('result', La())
1620  assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
1621
1622  # line continuation inside lambda with "cond ? expr : expr" works
1623  let ll = range(3)
1624  map(ll, {k, v -> v % 2 ? {
1625	    '111': 111 } : {}
1626	})
1627  assert_equal([{}, {'111': 111}, {}], ll)
1628
1629  ll = range(3)
1630  map(ll, {k, v -> v == 8 || v
1631		== 9
1632		|| v % 2 ? 111 : 222
1633	})
1634  assert_equal([222, 111, 222], ll)
1635
1636  ll = range(3)
1637  map(ll, {k, v -> v != 8 && v
1638		!= 9
1639		&& v % 2 == 0 ? 111 : 222
1640	})
1641  assert_equal([111, 222, 111], ll)
1642
1643  let dl = [{'key': 0}, {'key': 22}]->filter({ _, v -> v['key'] })
1644  assert_equal([{'key': 22}], dl)
1645
1646  dl = [{'key': 12}, {'foo': 34}]
1647  assert_equal([{'key': 12}], filter(dl,
1648	{_, v -> has_key(v, 'key') ? v['key'] == 12 : 0}))
1649
1650  assert_equal(false, LambdaWithComments()(0))
1651  assert_equal(true, LambdaWithComments()(1))
1652  assert_equal(true, LambdaWithComments()(2))
1653  assert_equal(false, LambdaWithComments()(3))
1654
1655  assert_equal(false, LambdaUsingArg(0)())
1656  assert_equal(true, LambdaUsingArg(1)())
1657
1658  CheckDefFailure(["filter([1, 2], {k,v -> 1})"], 'E1069:', 1)
1659  # error is in first line of the lambda
1660  CheckDefFailure(["let L = {a -> a + b}"], 'E1001:', 0)
1661
1662  assert_equal('xxxyyy', 'xxx'->{a, b -> a .. b}('yyy'))
1663
1664  CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x')"],
1665        'E1106: One argument too many')
1666  CheckDefExecFailure(["let s = 'asdf'->{a -> a}('x', 'y')"],
1667        'E1106: 2 arguments too many')
1668  CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1)
1669enddef
1670
1671def Test_expr7_lambda_vim9script()
1672  let lines =<< trim END
1673      vim9script
1674      let v = 10->{a ->
1675	    a
1676	      + 2
1677	  }()
1678      assert_equal(12, v)
1679  END
1680  CheckScriptSuccess(lines)
1681
1682  # nested lambda with line breaks
1683  lines =<< trim END
1684      vim9script
1685      search('"', 'cW', 0, 0, {->
1686	synstack('.', col('.'))
1687	->map({_, v -> synIDattr(v, 'name')})->len()})
1688  END
1689  CheckScriptSuccess(lines)
1690enddef
1691
1692def Test_epxr7_funcref()
1693  let lines =<< trim END
1694    def RetNumber(): number
1695      return 123
1696    enddef
1697    let FuncRef = RetNumber
1698    assert_equal(123, FuncRef())
1699  END
1700  CheckDefAndScriptSuccess(lines)
1701enddef
1702
1703def Test_expr7_dict()
1704  # dictionary
1705  assert_equal(g:dict_empty, {})
1706  assert_equal(g:dict_empty, {  })
1707  assert_equal(g:dict_one, {'one': 1})
1708  let key = 'one'
1709  let val = 1
1710  assert_equal(g:dict_one, {key: val})
1711
1712  let numbers: dict<number> = #{a: 1, b: 2, c: 3}
1713  numbers = #{a: 1}
1714  numbers = #{}
1715
1716  let strings: dict<string> = #{a: 'a', b: 'b', c: 'c'}
1717  strings = #{a: 'x'}
1718  strings = #{}
1719
1720  let mixed: dict<any> = #{a: 'a', b: 42}
1721  mixed = #{a: 'x'}
1722  mixed = #{a: 234}
1723  mixed = #{}
1724
1725  let dictlist: dict<list<string>> = #{absent: [], present: ['hi']}
1726  dictlist = #{absent: ['hi'], present: []}
1727  dictlist = #{absent: [], present: []}
1728
1729  let dictdict: dict<dict<string>> = #{one: #{a: 'text'}, two: #{}}
1730  dictdict = #{one: #{}, two: #{a: 'text'}}
1731  dictdict = #{one: #{}, two: #{}}
1732
1733  CheckDefFailure(["let x = #{a:8}"], 'E1069:', 1)
1734  CheckDefFailure(["let x = #{a : 8}"], 'E1068:', 1)
1735  CheckDefFailure(["let x = #{a :8}"], 'E1068:', 1)
1736  CheckDefFailure(["let x = #{a: 8 , b: 9}"], 'E1068:', 1)
1737
1738  CheckDefFailure(["let x = #{8: 8}"], 'E1014:', 1)
1739  CheckDefFailure(["let x = #{xxx}"], 'E720:', 1)
1740  CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:', 2)
1741  CheckDefFailure(["let x = #{xxx: 1,"], 'E723:', 2)
1742  CheckDefFailure(["let x = {'a': xxx}"], 'E1001:', 1)
1743  CheckDefFailure(["let x = {xxx: 8}"], 'E1001:', 1)
1744  CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:', 1)
1745  CheckDefFailure(["let x = #"], 'E1015:', 1)
1746  CheckDefExecFailure(["let x = g:anint.member"], 'E715:', 1)
1747  CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:', 1)
1748
1749  CheckDefExecFailure(['let x: dict<number> = #{a: 234, b: "1"}'], 'E1012:', 1)
1750  CheckDefExecFailure(['let x: dict<number> = #{a: "x", b: 134}'], 'E1012:', 1)
1751  CheckDefExecFailure(['let x: dict<string> = #{a: 234, b: "1"}'], 'E1012:', 1)
1752  CheckDefExecFailure(['let x: dict<string> = #{a: "x", b: 134}'], 'E1012:', 1)
1753enddef
1754
1755def Test_expr7_dict_vim9script()
1756  let lines =<< trim END
1757      vim9script
1758      let d = {
1759		'one':
1760		   1,
1761		'two': 2,
1762		   }
1763      assert_equal({'one': 1, 'two': 2}, d)
1764  END
1765  CheckScriptSuccess(lines)
1766
1767  lines =<< trim END
1768      vim9script
1769      let d = { "one": "one", "two": "two", }
1770      assert_equal({'one': 'one', 'two': 'two'}, d)
1771  END
1772  CheckScriptSuccess(lines)
1773
1774  lines =<< trim END
1775      vim9script
1776      let d = #{one: 1,
1777		two: 2,
1778	       }
1779      assert_equal({'one': 1, 'two': 2}, d)
1780  END
1781  CheckScriptSuccess(lines)
1782
1783  lines =<< trim END
1784      vim9script
1785      let d = #{one:1, two: 2}
1786  END
1787  CheckScriptFailure(lines, 'E1069:', 2)
1788
1789  lines =<< trim END
1790      vim9script
1791      let d = #{one: 1,two: 2}
1792  END
1793  CheckScriptFailure(lines, 'E1069:', 2)
1794
1795  lines =<< trim END
1796      vim9script
1797      let d = #{one : 1}
1798  END
1799  CheckScriptFailure(lines, 'E1068:', 2)
1800
1801  lines =<< trim END
1802      vim9script
1803      let d = #{one:1}
1804  END
1805  CheckScriptFailure(lines, 'E1069:', 2)
1806
1807  lines =<< trim END
1808      vim9script
1809      let d = #{one: 1 , two: 2}
1810  END
1811  CheckScriptFailure(lines, 'E1068:', 2)
1812
1813  lines =<< trim END
1814    vim9script
1815    let l: dict<number> = #{a: 234, b: 'x'}
1816  END
1817  CheckScriptFailure(lines, 'E1012:', 2)
1818  lines =<< trim END
1819    vim9script
1820    let l: dict<number> = #{a: 'x', b: 234}
1821  END
1822  CheckScriptFailure(lines, 'E1012:', 2)
1823  lines =<< trim END
1824    vim9script
1825    let l: dict<string> = #{a: 'x', b: 234}
1826  END
1827  CheckScriptFailure(lines, 'E1012:', 2)
1828  lines =<< trim END
1829    vim9script
1830    let l: dict<string> = #{a: 234, b: 'x'}
1831  END
1832  CheckScriptFailure(lines, 'E1012:', 2)
1833enddef
1834
1835let g:oneString = 'one'
1836
1837def Test_expr_member()
1838  assert_equal(1, g:dict_one.one)
1839  let d: dict<number> = g:dict_one
1840  assert_equal(1, d['one'])
1841  assert_equal(1, d[
1842		  'one'
1843		  ])
1844  assert_equal(1, d
1845  	.one)
1846  d = {'1': 1, '_': 2}
1847  assert_equal(1, d
1848  	.1)
1849  assert_equal(2, d
1850  	._)
1851
1852  # getting the one member should clear the dict after getting the item
1853  assert_equal('one', #{one: 'one'}.one)
1854  assert_equal('one', #{one: 'one'}[g:oneString])
1855
1856  CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:', 1)
1857  CheckDefExecFailure(["let d: dict<any>", "echo d['a']"], 'E716:', 2)
1858  CheckDefExecFailure(["let d: dict<number>", "d = g:list_empty"], 'E1012: Type mismatch; expected dict<number> but got list<unknown>', 2)
1859enddef
1860
1861def Test_expr7_any_index_slice()
1862  let lines =<< trim END
1863    # getting the one member should clear the list only after getting the item
1864    assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1])
1865
1866    # string is permissive, index out of range accepted
1867    g:teststring = 'abcdef'
1868    assert_equal('b', g:teststring[1])
1869    assert_equal('', g:teststring[-1])
1870    assert_equal('', g:teststring[99])
1871
1872    assert_equal('b', g:teststring[1:1])
1873    assert_equal('bcdef', g:teststring[1:])
1874    assert_equal('abcd', g:teststring[:3])
1875    assert_equal('cdef', g:teststring[-4:])
1876    assert_equal('abcdef', g:teststring[-9:])
1877    assert_equal('abcd', g:teststring[:-3])
1878    assert_equal('', g:teststring[:-9])
1879
1880    # blob index cannot be out of range
1881    g:testblob = 0z01ab
1882    assert_equal(0x01, g:testblob[0])
1883    assert_equal(0xab, g:testblob[1])
1884    assert_equal(0xab, g:testblob[-1])
1885    assert_equal(0x01, g:testblob[-2])
1886
1887    # blob slice accepts out of range
1888    assert_equal(0z01ab, g:testblob[0:1])
1889    assert_equal(0z01, g:testblob[0:0])
1890    assert_equal(0z01, g:testblob[-2:-2])
1891    assert_equal(0zab, g:testblob[1:1])
1892    assert_equal(0zab, g:testblob[-1:-1])
1893    assert_equal(0z, g:testblob[2:2])
1894    assert_equal(0z, g:testblob[0:-3])
1895
1896    # list index cannot be out of range
1897    g:testlist = [0, 1, 2, 3]
1898    assert_equal(0, g:testlist[0])
1899    assert_equal(1, g:testlist[1])
1900    assert_equal(3, g:testlist[3])
1901    assert_equal(3, g:testlist[-1])
1902    assert_equal(0, g:testlist[-4])
1903    assert_equal(1, g:testlist[g:theone])
1904
1905    # list slice accepts out of range
1906    assert_equal([0], g:testlist[0:0])
1907    assert_equal([3], g:testlist[3:3])
1908    assert_equal([0, 1], g:testlist[0:1])
1909    assert_equal([0, 1, 2, 3], g:testlist[0:3])
1910    assert_equal([0, 1, 2, 3], g:testlist[0:9])
1911    assert_equal([], g:testlist[-1:1])
1912    assert_equal([1], g:testlist[-3:1])
1913    assert_equal([0, 1], g:testlist[-4:1])
1914    assert_equal([0, 1], g:testlist[-9:1])
1915    assert_equal([1, 2, 3], g:testlist[1:-1])
1916    assert_equal([1], g:testlist[1:-3])
1917    assert_equal([], g:testlist[1:-4])
1918    assert_equal([], g:testlist[1:-9])
1919
1920    g:testdict = #{a: 1, b: 2}
1921    assert_equal(1, g:testdict['a'])
1922    assert_equal(2, g:testdict['b'])
1923  END
1924
1925  CheckDefSuccess(lines)
1926  CheckScriptSuccess(['vim9script'] + lines)
1927
1928  CheckDefExecFailure(['echo g:testblob[2]'], 'E979:', 1)
1929  CheckScriptFailure(['vim9script', 'echo g:testblob[2]'], 'E979:', 2)
1930  CheckDefExecFailure(['echo g:testblob[-3]'], 'E979:', 1)
1931  CheckScriptFailure(['vim9script', 'echo g:testblob[-3]'], 'E979:', 2)
1932
1933  CheckDefExecFailure(['echo g:testlist[4]'], 'E684:', 1)
1934  CheckScriptFailure(['vim9script', 'echo g:testlist[4]'], 'E684:', 2)
1935  CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1)
1936  CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684:', 2)
1937
1938  CheckDefExecFailure(['echo g:testdict["a":"b"]'], 'E719:', 1)
1939  CheckScriptFailure(['vim9script', 'echo g:testdict["a":"b"]'], 'E719:', 2)
1940  CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1)
1941  CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2)
1942
1943  unlet g:teststring
1944  unlet g:testblob
1945  unlet g:testlist
1946enddef
1947
1948def Test_expr_member_vim9script()
1949  let lines =<< trim END
1950      vim9script
1951      let d = #{one:
1952      		'one',
1953		two: 'two',
1954		1: 1,
1955		_: 2}
1956      assert_equal('one', d.one)
1957      assert_equal('one', d
1958                            .one)
1959      assert_equal(1, d
1960                            .1)
1961      assert_equal(2, d
1962                            ._)
1963      assert_equal('one', d[
1964			    'one'
1965			    ])
1966  END
1967  CheckScriptSuccess(lines)
1968
1969  lines =<< trim END
1970      vim9script
1971      let l = [1,
1972		  2,
1973		  3, 4
1974		  ]
1975      assert_equal(2, l[
1976			    1
1977			    ])
1978      assert_equal([2, 3], l[1 : 2])
1979      assert_equal([1, 2, 3], l[
1980				:
1981				2
1982				])
1983      assert_equal([3, 4], l[
1984				2
1985				:
1986				])
1987  END
1988  CheckScriptSuccess(lines)
1989enddef
1990
1991def Test_expr7_option()
1992  # option
1993  set ts=11
1994  assert_equal(11, &ts)
1995  &ts = 9
1996  assert_equal(9, &ts)
1997  set ts=8
1998  set grepprg=some\ text
1999  assert_equal('some text', &grepprg)
2000  &grepprg = test_null_string()
2001  assert_equal('', &grepprg)
2002  set grepprg&
2003enddef
2004
2005def Test_expr7_environment()
2006  # environment variable
2007  assert_equal('testvar', $TESTVAR)
2008  assert_equal('', $ASDF_ASD_XXX)
2009
2010  CheckDefFailure(["let x = $$$"], 'E1002:', 1)
2011enddef
2012
2013def Test_expr7_register()
2014  @a = 'register a'
2015  assert_equal('register a', @a)
2016
2017  let fname = expand('%')
2018  assert_equal(fname, @%)
2019
2020  feedkeys(":echo 'some'\<CR>", "xt")
2021  assert_equal("echo 'some'", @:)
2022
2023  normal axyz
2024  assert_equal("xyz", @.)
2025  CheckDefFailure(["@. = 'yes'"], 'E354:', 1)
2026
2027  @/ = 'slash'
2028  assert_equal('slash', @/)
2029
2030  @= = 'equal'
2031  assert_equal('equal', @=)
2032enddef
2033
2034def Test_expr7_namespace()
2035  g:some_var = 'some'
2036  assert_equal('some', get(g:, 'some_var'))
2037  assert_equal('some', get(g:, 'some_var', 'xxx'))
2038  assert_equal('xxx', get(g:, 'no_var', 'xxx'))
2039  unlet g:some_var
2040
2041  b:some_var = 'some'
2042  assert_equal('some', get(b:, 'some_var'))
2043  assert_equal('some', get(b:, 'some_var', 'xxx'))
2044  assert_equal('xxx', get(b:, 'no_var', 'xxx'))
2045  unlet b:some_var
2046
2047  w:some_var = 'some'
2048  assert_equal('some', get(w:, 'some_var'))
2049  assert_equal('some', get(w:, 'some_var', 'xxx'))
2050  assert_equal('xxx', get(w:, 'no_var', 'xxx'))
2051  unlet w:some_var
2052
2053  t:some_var = 'some'
2054  assert_equal('some', get(t:, 'some_var'))
2055  assert_equal('some', get(t:, 'some_var', 'xxx'))
2056  assert_equal('xxx', get(t:, 'no_var', 'xxx'))
2057  unlet t:some_var
2058enddef
2059
2060def Test_expr7_parens()
2061  # (expr)
2062  assert_equal(4, (6 * 4) / 6)
2063  assert_equal(0, 6 * ( 4 / 6 ))
2064
2065  assert_equal(6, +6)
2066  assert_equal(-6, -6)
2067  assert_equal(6, --6)
2068  assert_equal(6, -+-6)
2069  assert_equal(-6, ---6)
2070  assert_equal(false, !-3)
2071  assert_equal(true, !+-+0)
2072enddef
2073
2074def Test_expr7_parens_vim9script()
2075  let lines =<< trim END
2076      vim9script
2077      let s = (
2078		'one'
2079		..
2080		'two'
2081		)
2082      assert_equal('onetwo', s)
2083  END
2084  CheckScriptSuccess(lines)
2085enddef
2086
2087def Test_expr7_negate()
2088  assert_equal(-99, -99)
2089  assert_equal(99, --99)
2090  let nr = 88
2091  assert_equal(-88, -nr)
2092  assert_equal(88, --nr)
2093enddef
2094
2095def Echo(arg: any): string
2096  return arg
2097enddef
2098
2099def s:Echo4Arg(arg: any): string
2100  return arg
2101enddef
2102
2103def Test_expr7_call()
2104  assert_equal('yes', 'yes'->Echo())
2105  assert_equal('yes', 'yes'
2106  			->s:Echo4Arg())
2107  assert_equal(true, !range(5)->empty())
2108  assert_equal([0, 1, 2], --3->range())
2109
2110  CheckDefFailure(["let x = 'yes'->Echo"], 'E107:', 1)
2111  CheckScriptFailure([
2112   "vim9script",
2113   "let x = substitute ('x', 'x', 'x', 'x')"
2114   ], 'E121:', 2)
2115
2116  let auto_lines =<< trim END
2117      def g:some#func(): string
2118	return 'found'
2119      enddef
2120  END
2121  mkdir('Xruntime/autoload', 'p')
2122  writefile(auto_lines, 'Xruntime/autoload/some.vim')
2123  let save_rtp = &rtp
2124  &rtp = getcwd() .. '/Xruntime,' .. &rtp
2125  assert_equal('found', g:some#func())
2126  assert_equal('found', some#func())
2127
2128  &rtp = save_rtp
2129  delete('Xruntime', 'rf')
2130enddef
2131
2132
2133def Test_expr7_not()
2134  let lines =<< trim END
2135      assert_equal(true, !'')
2136      assert_equal(true, ![])
2137      assert_equal(false, !'asdf')
2138      assert_equal(false, ![2])
2139      assert_equal(true, !!'asdf')
2140      assert_equal(true, !![2])
2141
2142      assert_equal(true, !test_null_partial())
2143      assert_equal(false, !{-> 'yes'})
2144
2145      assert_equal(true, !test_null_dict())
2146      assert_equal(true, !{})
2147      assert_equal(false, !{'yes': 'no'})
2148
2149      if has('channel')
2150	assert_equal(true, !test_null_job())
2151	assert_equal(true, !test_null_channel())
2152      endif
2153
2154      assert_equal(true, !test_null_blob())
2155      assert_equal(true, !0z)
2156      assert_equal(false, !0z01)
2157
2158      assert_equal(true, !test_void())
2159      assert_equal(true, !test_unknown())
2160
2161      assert_equal(false, ![1, 2, 3]->reverse())
2162      assert_equal(true, ![]->reverse())
2163  END
2164  CheckDefSuccess(lines)
2165  CheckScriptSuccess(['vim9script'] + lines)
2166enddef
2167
2168func Test_expr7_fails()
2169  call CheckDefFailure(["let x = (12"], "E110:", 1)
2170
2171  call CheckDefFailure(["let x = -'xx'"], "E1030:", 1)
2172  call CheckDefFailure(["let x = +'xx'"], "E1030:", 1)
2173  call CheckDefFailure(["let x = -0z12"], "E974:", 1)
2174  call CheckDefExecFailure(["let x = -[8]"], "E39:", 1)
2175  call CheckDefExecFailure(["let x = -{'a': 1}"], "E39:", 1)
2176
2177  call CheckDefFailure(["let x = @"], "E1002:", 1)
2178  call CheckDefFailure(["let x = @<"], "E354:", 1)
2179
2180  call CheckDefFailure(["let x = [1, 2"], "E697:", 2)
2181  call CheckDefFailure(["let x = [notfound]"], "E1001:", 1)
2182
2183  call CheckDefFailure(["let x = { -> 123) }"], "E451:", 1)
2184  call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:", 1)
2185
2186  call CheckDefFailure(["let x = &notexist"], 'E113:', 1)
2187  call CheckDefFailure(["&grepprg = [343]"], 'E1012:', 1)
2188
2189  call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:', 1)
2190  call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:', 1)
2191
2192  call CheckDefFailure(["echo a:somevar"], 'E1075:', 1)
2193  call CheckDefFailure(["echo l:somevar"], 'E1075:', 1)
2194  call CheckDefFailure(["echo x:somevar"], 'E1075:', 1)
2195
2196  call CheckDefExecFailure(["let x = +g:astring"], 'E1030:', 1)
2197  call CheckDefExecFailure(["let x = +g:ablob"], 'E974:', 1)
2198  call CheckDefExecFailure(["let x = +g:alist"], 'E745:', 1)
2199  call CheckDefExecFailure(["let x = +g:adict"], 'E728:', 1)
2200
2201  call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:', 2)
2202
2203  call CheckDefFailure(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 1)
2204
2205  call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2)
2206  call CheckDefExecFailure(["#{a: 1->len()"], 'E488:', 1)
2207  call CheckDefExecFailure(["{'a': 1->len()"], 'E723:', 2)
2208endfunc
2209
2210let g:Funcrefs = [function('add')]
2211
2212func CallMe(arg)
2213  return a:arg
2214endfunc
2215
2216func CallMe2(one, two)
2217  return a:one .. a:two
2218endfunc
2219
2220def Test_expr7_trailing()
2221  # user function call
2222  assert_equal(123, g:CallMe(123))
2223  assert_equal(123, g:CallMe(  123))
2224  assert_equal(123, g:CallMe(123  ))
2225  assert_equal('yesno', g:CallMe2('yes', 'no'))
2226  assert_equal('yesno', g:CallMe2( 'yes', 'no' ))
2227  assert_equal('nothing', g:CallMe('nothing'))
2228
2229  # partial call
2230  let Part = function('g:CallMe')
2231  assert_equal('yes', Part('yes'))
2232
2233  # funcref call, using list index
2234  let l = []
2235  g:Funcrefs[0](l, 2)
2236  assert_equal([2], l)
2237
2238  # method call
2239  l = [2, 5, 6]
2240  l->map({k, v -> k + v})
2241  assert_equal([2, 6, 8], l)
2242
2243  # lambda method call
2244  l = [2, 5]
2245  l->{l -> add(l, 8)}()
2246  assert_equal([2, 5, 8], l)
2247
2248  # dict member
2249  let d = #{key: 123}
2250  assert_equal(123, d.key)
2251enddef
2252
2253def Test_expr7_string_subscript()
2254  let lines =<< trim END
2255    let text = 'abcdef'
2256    assert_equal('', text[-1])
2257    assert_equal('a', text[0])
2258    assert_equal('e', text[4])
2259    assert_equal('f', text[5])
2260    assert_equal('', text[6])
2261
2262    text = 'ábçdëf'
2263    assert_equal('', text[-999])
2264    assert_equal('', text[-1])
2265    assert_equal('á', text[0])
2266    assert_equal('b', text[1])
2267    assert_equal('ç', text[2])
2268    assert_equal('d', text[3])
2269    assert_equal('ë', text[4])
2270    assert_equal('f', text[5])
2271    assert_equal('', text[6])
2272    assert_equal('', text[999])
2273
2274    assert_equal('ábçdëf', text[0:-1])
2275    assert_equal('ábçdëf', text[0 :-1])
2276    assert_equal('ábçdëf', text[0: -1])
2277    assert_equal('ábçdëf', text[0 : -1])
2278    assert_equal('ábçdëf', text[0
2279                  :-1])
2280    assert_equal('ábçdëf', text[0:
2281                  -1])
2282    assert_equal('ábçdëf', text[0 : -1
2283                  ])
2284    assert_equal('bçdëf', text[1:-1])
2285    assert_equal('çdëf', text[2:-1])
2286    assert_equal('dëf', text[3:-1])
2287    assert_equal('ëf', text[4:-1])
2288    assert_equal('f', text[5:-1])
2289    assert_equal('', text[6:-1])
2290    assert_equal('', text[999:-1])
2291
2292    assert_equal('ábçd', text[:3])
2293    assert_equal('bçdëf', text[1:])
2294    assert_equal('ábçdëf', text[:])
2295  END
2296  CheckDefSuccess(lines)
2297  CheckScriptSuccess(['vim9script'] + lines)
2298enddef
2299
2300def Test_expr7_list_subscript()
2301  let lines =<< trim END
2302    let list = [0, 1, 2, 3, 4]
2303    assert_equal(0, list[0])
2304    assert_equal(4, list[4])
2305    assert_equal(4, list[-1])
2306    assert_equal(0, list[-5])
2307
2308    assert_equal([0, 1, 2, 3, 4], list[0:4])
2309    assert_equal([0, 1, 2, 3, 4], list[:])
2310    assert_equal([1, 2, 3, 4], list[1:])
2311    assert_equal([2, 3, 4], list[2:-1])
2312    assert_equal([4], list[4:-1])
2313    assert_equal([], list[5:-1])
2314    assert_equal([], list[999:-1])
2315    assert_equal([1, 2, 3, 4], list[g:theone:g:thefour])
2316
2317    assert_equal([0, 1, 2, 3], list[0:3])
2318    assert_equal([0], list[0:0])
2319    assert_equal([0, 1, 2, 3, 4], list[0:-1])
2320    assert_equal([0, 1, 2], list[0:-3])
2321    assert_equal([0], list[0:-5])
2322    assert_equal([], list[0:-6])
2323    assert_equal([], list[0:-99])
2324  END
2325  CheckDefSuccess(lines)
2326  CheckScriptSuccess(['vim9script'] + lines)
2327
2328  lines = ['let l = [0, 1, 2]', 'echo l[g:astring : g:theone]']
2329  CheckDefExecFailure(lines, 'E1012:')
2330  CheckScriptFailure(['vim9script'] + lines, 'E1030:', 3)
2331enddef
2332
2333def Test_expr7_dict_subscript()
2334  let lines =<< trim END
2335      vim9script
2336      let l = [#{lnum: 2}, #{lnum: 1}]
2337      let res = l[0].lnum > l[1].lnum
2338      assert_true(res)
2339  END
2340  CheckScriptSuccess(lines)
2341enddef
2342
2343def Test_expr7_subscript_linebreak()
2344  let range = range(
2345  		3)
2346  let l = range
2347	->map('string(v:key)')
2348  assert_equal(['0', '1', '2'], l)
2349
2350  l = range
2351  	->map('string(v:key)')
2352  assert_equal(['0', '1', '2'], l)
2353
2354  l = range # comment
2355  	->map('string(v:key)')
2356  assert_equal(['0', '1', '2'], l)
2357
2358  l = range
2359
2360  	->map('string(v:key)')
2361  assert_equal(['0', '1', '2'], l)
2362
2363  l = range
2364	# comment
2365  	->map('string(v:key)')
2366  assert_equal(['0', '1', '2'], l)
2367
2368  assert_equal('1', l[
2369	1])
2370
2371  let d = #{one: 33}
2372  assert_equal(33, d.
2373	one)
2374enddef
2375
2376def Test_expr7_method_call()
2377  new
2378  setline(1, ['first', 'last'])
2379  'second'->append(1)
2380  "third"->append(2)
2381  assert_equal(['first', 'second', 'third', 'last'], getline(1, '$'))
2382  bwipe!
2383
2384  let bufnr = bufnr()
2385  let loclist = [#{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}]
2386  loclist->setloclist(0)
2387  assert_equal([#{bufnr: bufnr,
2388  		lnum: 42,
2389		col: 17,
2390		text: 'wrong',
2391		pattern: '',
2392		valid: 1,
2393		vcol: 0,
2394		nr: 0,
2395		type: '',
2396		module: ''}
2397		], getloclist(0))
2398
2399  let result: bool = get(#{n: 0}, 'n', 0)
2400  assert_equal(false, result)
2401enddef
2402
2403func Test_expr7_trailing_fails()
2404  call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107:', 2)
2405  call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274:', 2)
2406endfunc
2407
2408func Test_expr_fails()
2409  call CheckDefFailure(["let x = '1'is2"], 'E488:', 1)
2410  call CheckDefFailure(["let x = '1'isnot2"], 'E488:', 1)
2411
2412  call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1)
2413  call CheckScriptFailure(["CallMe ('yes')"], 'E492:', 1)
2414  call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1)
2415  call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1)
2416
2417  call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1)
2418  call CheckDefFailure(["let v:statusmsg = ''"], 'E1016: Cannot declare a v: variable:', 1)
2419  call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:', 1)
2420
2421  call CheckDefFailure(["echo len('asdf'"], 'E110:', 2)
2422  call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:', 1)
2423  call CheckDefFailure(["echo doesnotexist()"], 'E117:', 1)
2424endfunc
2425
2426" vim: shiftwidth=2 sts=2 expandtab
2427