1" Tests for Vim9 script expressions
2
3source check.vim
4
5" Check that "line" inside ":def" results in an "error" message.
6func CheckDefFailure(line, error)
7  call writefile(['def! Func()', a:line, 'enddef'], 'Xdef')
8  call assert_fails('so Xdef', a:error, a:line)
9  call delete('Xdef')
10endfunc
11
12func CheckDefFailureList(lines, error)
13  call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef')
14  call assert_fails('so Xdef', a:error, string(a:lines))
15  call delete('Xdef')
16endfunc
17
18" test cond ? expr : expr
19def Test_expr1()
20  assert_equal('one', true ? 'one' : 'two')
21  assert_equal('one', 1 ? 'one' : 'two')
22  if has('float')
23    assert_equal('one', 0.1 ? 'one' : 'two')
24  endif
25  assert_equal('one', 'x' ? 'one' : 'two')
26"  assert_equal('one', 0z1234 ? 'one' : 'two')
27  assert_equal('one', [0] ? 'one' : 'two')
28"  assert_equal('one', #{x: 0} ? 'one' : 'two')
29  let var = 1
30  assert_equal('one', var ? 'one' : 'two')
31
32  assert_equal('two', false ? 'one' : 'two')
33  assert_equal('two', 0 ? 'one' : 'two')
34  if has('float')
35    assert_equal('two', 0.0 ? 'one' : 'two')
36  endif
37  assert_equal('two', '' ? 'one' : 'two')
38"  assert_equal('one', 0z ? 'one' : 'two')
39  assert_equal('two', [] ? 'one' : 'two')
40"  assert_equal('two', {} ? 'one' : 'two')
41  var = 0
42  assert_equal('two', var ? 'one' : 'two')
43enddef
44
45func Test_expr1_fails()
46  call CheckDefFailure("let x = 1 ? 'one'", "Missing ':' after '?'")
47
48  let msg = "white space required before and after '?'"
49  call CheckDefFailure("let x = 1? 'one' : 'two'", msg)
50  call CheckDefFailure("let x = 1 ?'one' : 'two'", msg)
51  call CheckDefFailure("let x = 1?'one' : 'two'", msg)
52
53  let msg = "white space required before and after ':'"
54  call CheckDefFailure("let x = 1 ? 'one': 'two'", msg)
55  call CheckDefFailure("let x = 1 ? 'one' :'two'", msg)
56  call CheckDefFailure("let x = 1 ? 'one':'two'", msg)
57endfunc
58
59" TODO: define inside test function
60def Record(val: any): any
61  g:vals->add(val)
62  return val
63enddef
64
65" test ||
66def Test_expr2()
67  assert_equal(2, 2 || 0)
68  assert_equal(7, 0 || 0 || 7)
69  assert_equal(0, 0 || 0)
70  assert_equal('', 0 || '')
71
72  g:vals = []
73  assert_equal(3, Record(3) || Record(1))
74  assert_equal([3], g:vals)
75
76  g:vals = []
77  assert_equal(5, Record(0) || Record(5))
78  assert_equal([0, 5], g:vals)
79
80  g:vals = []
81  assert_equal(4, Record(0) || Record(4) || Record(0))
82  assert_equal([0, 4], g:vals)
83
84  g:vals = []
85  assert_equal(0, Record([]) || Record('') || Record(0))
86  assert_equal([[], '', 0], g:vals)
87enddef
88
89func Test_expr2_fails()
90  let msg = "white space required before and after '||'"
91  call CheckDefFailure("let x = 1||2", msg)
92  call CheckDefFailure("let x = 1 ||2", msg)
93  call CheckDefFailure("let x = 1|| 2", msg)
94endfunc
95
96" test &&
97def Test_expr3()
98  assert_equal(0, 2 && 0)
99  assert_equal(0, 0 && 0 && 7)
100  assert_equal(7, 2 && 3 && 7)
101  assert_equal(0, 0 && 0)
102  assert_equal(0, 0 && '')
103  assert_equal('', 8 && '')
104
105  g:vals = []
106  assert_equal(1, Record(3) && Record(1))
107  assert_equal([3, 1], g:vals)
108
109  g:vals = []
110  assert_equal(0, Record(0) && Record(5))
111  assert_equal([0], g:vals)
112
113  g:vals = []
114  assert_equal(0, Record(0) && Record(4) && Record(0))
115  assert_equal([0], g:vals)
116
117  g:vals = []
118  assert_equal(0, Record(8) && Record(4) && Record(0))
119  assert_equal([8, 4, 0], g:vals)
120
121  g:vals = []
122  assert_equal(0, Record([1]) && Record('z') && Record(0))
123  assert_equal([[1], 'z', 0], g:vals)
124enddef
125
126func Test_expr3_fails()
127  let msg = "white space required before and after '&&'"
128  call CheckDefFailure("let x = 1&&2", msg)
129  call CheckDefFailure("let x = 1 &&2", msg)
130  call CheckDefFailure("let x = 1&& 2", msg)
131endfunc
132
133let atrue = v:true
134let afalse = v:false
135let anone = v:none
136let anull = v:null
137let anint = 10
138let alsoint = 4
139if has('float')
140  let afloat = 0.1
141endif
142let astring = 'asdf'
143let ablob = 0z01ab
144let alist = [2, 3, 4]
145let adict = #{aaa: 2, bbb: 8}
146
147" test == comperator
148def Test_expr4_equal()
149  assert_equal(true, true == true)
150  assert_equal(false, true == false)
151  assert_equal(true, true == g:atrue)
152  assert_equal(false, g:atrue == false)
153
154  assert_equal(true, v:none == v:none)
155  assert_equal(false, v:none == v:null)
156  assert_equal(true, g:anone == v:none)
157  assert_equal(false, v:none == g:anull)
158
159  assert_equal(false, 2 == 0)
160  assert_equal(true, 61 == 61)
161  assert_equal(true, g:anint == 10)
162  assert_equal(false, 61 == g:anint)
163
164  if has('float')
165    assert_equal(true, 0.3 == 0.3)
166    assert_equal(false, 0.4 == 0.3)
167    assert_equal(true, 0.1 == g:afloat)
168    assert_equal(false, g:afloat == 0.3)
169
170    assert_equal(true, 3.0 == 3)
171    assert_equal(true, 3 == 3.0)
172    assert_equal(false, 3.1 == 3)
173    assert_equal(false, 3 == 3.1)
174  endif
175
176  assert_equal(true, 'abc' == 'abc')
177  assert_equal(false, 'xyz' == 'abc')
178  assert_equal(true, g:astring == 'asdf')
179  assert_equal(false, 'xyz' == g:astring)
180
181  assert_equal(false, 'abc' == 'ABC')
182  set ignorecase
183  assert_equal(false, 'abc' == 'ABC')
184  set noignorecase
185
186  assert_equal(true, 0z3f == 0z3f)
187  assert_equal(false, 0z3f == 0z4f)
188  assert_equal(true, g:ablob == 0z01ab)
189  assert_equal(false, 0z3f == g:ablob)
190
191  assert_equal(true, [1, 2, 3] == [1, 2, 3])
192  assert_equal(false, [1, 2, 3] == [2, 3, 1])
193  assert_equal(true, [2, 3, 4] == g:alist)
194  assert_equal(false, g:alist == [2, 3, 1])
195  assert_equal(false, [1, 2, 3] == [])
196  assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
197
198  assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
199  assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
200  assert_equal(false, #{one: 1, two: 2} == #{two: 2})
201  assert_equal(false, #{one: 1, two: 2} == #{})
202  assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
203  assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
204
205  assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal'))
206  assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is'))
207
208  assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123]))
209  assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123]))
210  assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999]))
211enddef
212
213" test != comperator
214def Test_expr4_notequal()
215  assert_equal(false, true != true)
216  assert_equal(true, true != false)
217  assert_equal(false, true != g:atrue)
218  assert_equal(true, g:atrue != false)
219
220  assert_equal(false, v:none != v:none)
221  assert_equal(true, v:none != v:null)
222  assert_equal(false, g:anone != v:none)
223  assert_equal(true, v:none != g:anull)
224
225  assert_equal(true, 2 != 0)
226  assert_equal(false, 55 != 55)
227  assert_equal(false, g:anint != 10)
228  assert_equal(true, 61 != g:anint)
229
230  if has('float')
231    assert_equal(false, 0.3 != 0.3)
232    assert_equal(true, 0.4 != 0.3)
233    assert_equal(false, 0.1 != g:afloat)
234    assert_equal(true, g:afloat != 0.3)
235
236    assert_equal(false, 3.0 != 3)
237    assert_equal(false, 3 != 3.0)
238    assert_equal(true, 3.1 != 3)
239    assert_equal(true, 3 != 3.1)
240  endif
241
242  assert_equal(false, 'abc' != 'abc')
243  assert_equal(true, 'xyz' != 'abc')
244  assert_equal(false, g:astring != 'asdf')
245  assert_equal(true, 'xyz' != g:astring)
246
247  assert_equal(true, 'abc' != 'ABC')
248  set ignorecase
249  assert_equal(true, 'abc' != 'ABC')
250  set noignorecase
251
252  assert_equal(false, 0z3f != 0z3f)
253  assert_equal(true, 0z3f != 0z4f)
254  assert_equal(false, g:ablob != 0z01ab)
255  assert_equal(true, 0z3f != g:ablob)
256
257  assert_equal(false, [1, 2, 3] != [1, 2, 3])
258  assert_equal(true, [1, 2, 3] != [2, 3, 1])
259  assert_equal(false, [2, 3, 4] != g:alist)
260  assert_equal(true, g:alist != [2, 3, 1])
261  assert_equal(true, [1, 2, 3] != [])
262  assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
263
264  assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
265  assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
266  assert_equal(true, #{one: 1, two: 2} != #{two: 2})
267  assert_equal(true, #{one: 1, two: 2} != #{})
268  assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
269  assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
270
271  assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal'))
272  assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is'))
273
274  assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123]))
275  assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123]))
276  assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999]))
277enddef
278
279" test > comperator
280def Test_expr4_greater()
281  assert_equal(true, 2 > 0)
282  assert_equal(true, 2 > 1)
283  assert_equal(false, 2 > 2)
284  assert_equal(false, 2 > 3)
285enddef
286
287" test >= comperator
288def Test_expr4_greaterequal()
289  assert_equal(true, 2 >= 0)
290  assert_equal(true, 2 >= 2)
291  assert_equal(false, 2 >= 3)
292enddef
293
294" test < comperator
295def Test_expr4_smaller()
296  assert_equal(false, 2 < 0)
297  assert_equal(false, 2 < 2)
298  assert_equal(true, 2 < 3)
299enddef
300
301" test <= comperator
302def Test_expr4_smallerequal()
303  assert_equal(false, 2 <= 0)
304  assert_equal(false, 2 <= 1)
305  assert_equal(true, 2 <= 2)
306  assert_equal(true, 2 <= 3)
307enddef
308
309" test =~ comperator
310def Test_expr4_match()
311  assert_equal(false, '2' =~ '0')
312  assert_equal(true, '2' =~ '[0-9]')
313enddef
314
315" test !~ comperator
316def Test_expr4_nomatch()
317  assert_equal(true, '2' !~ '0')
318  assert_equal(false, '2' !~ '[0-9]')
319enddef
320
321" test is comperator
322def Test_expr4_is()
323  let mylist = [2]
324  assert_equal(false, mylist is [2])
325  let other = mylist
326  assert_equal(true, mylist is other)
327enddef
328
329" test isnot comperator
330def Test_expr4_isnot()
331  let mylist = [2]
332  assert_equal(true, '2' isnot '0')
333  assert_equal(true, mylist isnot [2])
334  let other = mylist
335  assert_equal(false, mylist isnot other)
336enddef
337
338def RetVoid()
339  let x = 1
340enddef
341
342func Test_expr4_fails()
343  let msg = "white space required before and after '>'"
344  call CheckDefFailure("let x = 1>2", msg)
345  call CheckDefFailure("let x = 1 >2", msg)
346  call CheckDefFailure("let x = 1> 2", msg)
347
348  let msg = "white space required before and after '=='"
349  call CheckDefFailure("let x = 1==2", msg)
350  call CheckDefFailure("let x = 1 ==2", msg)
351  call CheckDefFailure("let x = 1== 2", msg)
352
353  let msg = "white space required before and after 'is'"
354  call CheckDefFailure("let x = '1'is'2'", msg)
355  call CheckDefFailure("let x = '1' is'2'", msg)
356  call CheckDefFailure("let x = '1'is '2'", msg)
357
358  let msg = "white space required before and after 'isnot'"
359  call CheckDefFailure("let x = '1'isnot'2'", msg)
360  call CheckDefFailure("let x = '1' isnot'2'", msg)
361  call CheckDefFailure("let x = '1'isnot '2'", msg)
362
363  call CheckDefFailure("let x = 1 is# 2", 'E15:')
364  call CheckDefFailure("let x = 1 is? 2", 'E15:')
365  call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
366  call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
367
368  call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
369  call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
370  call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
371  call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
372
373  call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool')
374  call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool')
375  call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool')
376  call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool')
377  call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool')
378  call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
379  call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
380  call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
381
382  call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
383  call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
384  call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
385  call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
386  if has('float')
387    call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
388    call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
389  endif
390
391  call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob')
392  call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob')
393  call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob')
394  call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob')
395  call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
396  call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
397
398  call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list')
399  call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list')
400  call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list')
401  call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list')
402  call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
403  call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
404endfunc
405
406" test addition, subtraction, concatenation
407def Test_expr5()
408  assert_equal(66, 60 + 6)
409  assert_equal(70, 60 + g:anint)
410  assert_equal(9, g:alsoint + 5)
411  assert_equal(14, g:alsoint + g:anint)
412
413  assert_equal(54, 60 - 6)
414  assert_equal(50, 60 - g:anint)
415  assert_equal(-1, g:alsoint - 5)
416  assert_equal(-6, g:alsoint - g:anint)
417
418  assert_equal('hello', 'hel' .. 'lo')
419  assert_equal('hello 123', 'hello ' .. 123)
420  assert_equal('123 hello', 123 .. ' hello')
421  assert_equal('123456', 123 .. 456)
422enddef
423
424def Test_expr5_float()
425  if !has('float')
426    MissingFeature 'float'
427  else
428    assert_equal(66.0, 60.0 + 6.0)
429    assert_equal(66.0, 60.0 + 6)
430    assert_equal(66.0, 60 + 6.0)
431    assert_equal(5.1, g:afloat + 5)
432    assert_equal(8.1, 8 + g:afloat)
433    assert_equal(10.1, g:anint + g:afloat)
434    assert_equal(10.1, g:afloat + g:anint)
435
436    assert_equal(54.0, 60.0 - 6.0)
437    assert_equal(54.0, 60.0 - 6)
438    assert_equal(54.0, 60 - 6.0)
439    assert_equal(-4.9, g:afloat - 5)
440    assert_equal(7.9, 8 - g:afloat)
441    assert_equal(9.9, g:anint - g:afloat)
442    assert_equal(-9.9, g:afloat - g:anint)
443  endif
444enddef
445
446func Test_expr5_fails()
447  let msg = "white space required before and after '+'"
448  call CheckDefFailure("let x = 1+2", msg)
449  call CheckDefFailure("let x = 1 +2", msg)
450  call CheckDefFailure("let x = 1+ 2", msg)
451
452  let msg = "white space required before and after '-'"
453  call CheckDefFailure("let x = 1-2", msg)
454  call CheckDefFailure("let x = 1 -2", msg)
455  call CheckDefFailure("let x = 1- 2", msg)
456
457  let msg = "white space required before and after '..'"
458  call CheckDefFailure("let x = '1'..'2'", msg)
459  call CheckDefFailure("let x = '1' ..'2'", msg)
460  call CheckDefFailure("let x = '1'.. '2'", msg)
461endfunc
462
463" test multiply, divide, modulo
464def Test_expr6()
465  assert_equal(36, 6 * 6)
466  assert_equal(24, 6 * g:alsoint)
467  assert_equal(24, g:alsoint * 6)
468  assert_equal(40, g:anint * g:alsoint)
469
470  assert_equal(10, 60 / 6)
471  assert_equal(6, 60 / g:anint)
472  assert_equal(1, g:anint / 6)
473  assert_equal(2, g:anint / g:alsoint)
474
475  assert_equal(5, 11 % 6)
476  assert_equal(4, g:anint % 6)
477  assert_equal(3, 13 % g:anint)
478  assert_equal(2, g:anint % g:alsoint)
479
480  assert_equal(4, 6 * 4 / 6)
481enddef
482
483def Test_expr6_float()
484  if !has('float')
485    MissingFeature 'float'
486  else
487    assert_equal(36.0, 6.0 * 6)
488    assert_equal(36.0, 6 * 6.0)
489    assert_equal(36.0, 6.0 * 6.0)
490    assert_equal(1.0, g:afloat * g:anint)
491
492    assert_equal(10.0, 60 / 6.0)
493    assert_equal(10.0, 60.0 / 6)
494    assert_equal(10.0, 60.0 / 6.0)
495    assert_equal(0.01, g:afloat / g:anint)
496
497    assert_equal(4.0, 6.0 * 4 / 6)
498    assert_equal(4.0, 6 * 4.0 / 6)
499    assert_equal(4.0, 6 * 4 / 6.0)
500    assert_equal(4.0, 6.0 * 4.0 / 6)
501    assert_equal(4.0, 6 * 4.0 / 6.0)
502    assert_equal(4.0, 6.0 * 4 / 6.0)
503    assert_equal(4.0, 6.0 * 4.0 / 6.0)
504
505    assert_equal(4.0, 6.0 * 4.0 / 6.0)
506  endif
507enddef
508
509func Test_expr6_fails()
510  let msg = "white space required before and after '*'"
511  call CheckDefFailure("let x = 1*2", msg)
512  call CheckDefFailure("let x = 1 *2", msg)
513  call CheckDefFailure("let x = 1* 2", msg)
514
515  let msg = "white space required before and after '/'"
516  call CheckDefFailure("let x = 1/2", msg)
517  call CheckDefFailure("let x = 1 /2", msg)
518  call CheckDefFailure("let x = 1/ 2", msg)
519
520  let msg = "white space required before and after '%'"
521  call CheckDefFailure("let x = 1%2", msg)
522  call CheckDefFailure("let x = 1 %2", msg)
523  call CheckDefFailure("let x = 1% 2", msg)
524
525  call CheckDefFailure("let x = '1' * '2'", 'E1036:')
526  call CheckDefFailure("let x = '1' / '2'", 'E1036:')
527  call CheckDefFailure("let x = '1' % '2'", 'E1035:')
528
529  call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
530  call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
531  call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
532
533  call CheckDefFailure("let x = [1] * [2]", 'E1036:')
534  call CheckDefFailure("let x = [1] / [2]", 'E1036:')
535  call CheckDefFailure("let x = [1] % [2]", 'E1035:')
536
537  call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
538  call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
539  call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
540
541endfunc
542
543func Test_expr6_float_fails()
544  CheckFeature float
545  call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
546endfunc
547
548" define here to use old style parsing
549if has('float')
550  let g:float_zero = 0.0
551  let g:float_neg = -9.8
552  let g:float_big = 9.9e99
553endif
554let g:blob_empty = 0z
555let g:blob_one = 0z01
556let g:blob_long = 0z0102.0304
557
558let g:string_empty = ''
559let g:string_short = 'x'
560let g:string_long = 'abcdefghijklm'
561let g:string_special = "ab\ncd\ref\ekk"
562
563let g:special_true = v:true
564let g:special_false = v:false
565let g:special_null = v:null
566let g:special_none = v:none
567
568let g:list_empty = []
569let g:list_mixed = [1, 'b', v:false]
570
571let g:dict_empty = {}
572let g:dict_one = #{one: 1}
573
574let $TESTVAR = 'testvar'
575
576let @a = 'register a'
577
578" test low level expression
579def Test_expr7_number()
580  " number constant
581  assert_equal(0, 0)
582  assert_equal(654, 0654)
583
584  assert_equal(6, 0x6)
585  assert_equal(15, 0xf)
586  assert_equal(255, 0xff)
587enddef
588
589def Test_expr7_float()
590  " float constant
591  if !has('float')
592    MissingFeature 'float'
593  else
594    assert_equal(g:float_zero, .0)
595    assert_equal(g:float_zero, 0.0)
596    assert_equal(g:float_neg, -9.8)
597    assert_equal(g:float_big, 9.9e99)
598  endif
599enddef
600
601def Test_expr7_blob()
602  " blob constant
603  assert_equal(g:blob_empty, 0z)
604  assert_equal(g:blob_one, 0z01)
605  assert_equal(g:blob_long, 0z0102.0304)
606enddef
607
608def Test_expr7_string()
609  " string constant
610  assert_equal(g:string_empty, '')
611  assert_equal(g:string_empty, "")
612  assert_equal(g:string_short, 'x')
613  assert_equal(g:string_short, "x")
614  assert_equal(g:string_long, 'abcdefghijklm')
615  assert_equal(g:string_long, "abcdefghijklm")
616  assert_equal(g:string_special, "ab\ncd\ref\ekk")
617enddef
618
619def Test_expr7_special()
620  " special constant
621  assert_equal(g:special_true, true)
622  assert_equal(g:special_false, false)
623  assert_equal(g:special_null, v:null)
624  assert_equal(g:special_none, v:none)
625enddef
626
627def Test_expr7_list()
628  " list
629  assert_equal(g:list_empty, [])
630  assert_equal(g:list_empty, [  ])
631  assert_equal(g:list_mixed, [1, 'b', false])
632enddef
633
634def Test_expr7_lambda()
635  " lambda
636  let La = { -> 'result'}
637  assert_equal('result', La())
638  assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
639enddef
640
641def Test_expr7_dict()
642  " dictionary
643  assert_equal(g:dict_empty, {})
644  assert_equal(g:dict_empty, {  })
645  assert_equal(g:dict_one, {'one': 1})
646  let key = 'one'
647  let val = 1
648  assert_equal(g:dict_one, {key: val})
649enddef
650
651def Test_expr7_option()
652  " option
653  set ts=11
654  assert_equal(11, &ts)
655  set ts=8
656  set grepprg=some\ text
657  assert_equal('some text', &grepprg)
658  &grepprg = test_null_string()
659  assert_equal('', &grepprg)
660  set grepprg&
661enddef
662
663def Test_expr7_environment()
664  " environment variable
665  assert_equal('testvar', $TESTVAR)
666  assert_equal('', $ASDF_ASD_XXX)
667enddef
668
669def Test_expr7_register()
670  " register
671  assert_equal('register a', @a)
672enddef
673
674def Test_expr7_parens()
675  " (expr)
676  assert_equal(4, (6 * 4) / 6)
677  assert_equal(0, 6 * ( 4 / 6 ))
678
679  assert_equal(6, +6)
680  assert_equal(-6, -6)
681  assert_equal(6, --6)
682  assert_equal(6, -+-6)
683  assert_equal(-6, ---6)
684enddef
685
686def Test_expr7_not()
687  assert_equal(true, !'')
688  assert_equal(true, ![])
689  assert_equal(false, !'asdf')
690  assert_equal(false, ![2])
691  assert_equal(true, !!'asdf')
692  assert_equal(true, !![2])
693enddef
694
695func Test_expr7_fails()
696  call CheckDefFailure("let x = (12", "E110:")
697
698  call CheckDefFailure("let x = -'xx'", "E1030:")
699  call CheckDefFailure("let x = +'xx'", "E1030:")
700
701  call CheckDefFailure("let x = @", "E1002:")
702  call CheckDefFailure("let x = @<", "E354:")
703
704  call CheckDefFailure("let x = &notexist", "E113:")
705endfunc
706
707let g:Funcrefs = [function('add')]
708
709func CallMe(arg)
710  return a:arg
711endfunc
712
713def Test_expr7_trailing()
714  " user function call
715  assert_equal(123, CallMe(123))
716  assert_equal('nothing', CallMe('nothing'))
717
718  " partial call
719  let Part = function('CallMe')
720  assert_equal('yes', Part('yes'))
721
722  " funcref call, using list index
723  let l = []
724  g:Funcrefs[0](l, 2)
725  assert_equal([2], l)
726
727  " method call
728  l = [2, 5, 6]
729  l->map({k, v -> k + v})
730  assert_equal([2, 6, 8], l)
731
732  " lambda method call
733  l = [2, 5]
734  l->{l -> add(l, 8)}()
735  assert_equal([2, 5, 8], l)
736
737  " dict member
738  let d = #{key: 123}
739  assert_equal(123, d.key)
740enddef
741
742func Test_expr7_trailing_fails()
743  call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
744endfunc
745
746func Test_expr_fails()
747  call CheckDefFailure("let x = '1'is2", 'E488:')
748  call CheckDefFailure("let x = '1'isnot2", 'E488:')
749endfunc
750