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 CheckDefFailureMult(lines, error)
13  call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef')
14  call assert_fails('so Xdef', a:error, join(a:lines, ' | '))
15  call delete('Xdef')
16endfunc
17
18" Check that "line" inside ":def" results in an "error" message when executed.
19func CheckDefExecFailure(line, error)
20  call writefile(['def! Func()', a:line, 'enddef'], 'Xdef')
21  so Xdef
22  call assert_fails('call Func()', a:error, a:line)
23  call delete('Xdef')
24endfunc
25
26func CheckDefFailureList(lines, error)
27  call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef')
28  call assert_fails('so Xdef', a:error, string(a:lines))
29  call delete('Xdef')
30endfunc
31
32" test cond ? expr : expr
33def Test_expr1()
34  assert_equal('one', true ? 'one' : 'two')
35  assert_equal('one', 1 ? 'one' : 'two')
36  if has('float')
37    assert_equal('one', 0.1 ? 'one' : 'two')
38  endif
39  assert_equal('one', 'x' ? 'one' : 'two')
40  assert_equal('one', 0z1234 ? 'one' : 'two')
41  assert_equal('one', [0] ? 'one' : 'two')
42  assert_equal('one', #{x: 0} ? 'one' : 'two')
43  let var = 1
44  assert_equal('one', var ? 'one' : 'two')
45
46  assert_equal('two', false ? 'one' : 'two')
47  assert_equal('two', 0 ? 'one' : 'two')
48  if has('float')
49    assert_equal('two', 0.0 ? 'one' : 'two')
50  endif
51  assert_equal('two', '' ? 'one' : 'two')
52  assert_equal('two', 0z ? 'one' : 'two')
53  assert_equal('two', [] ? 'one' : 'two')
54  assert_equal('two', {} ? 'one' : 'two')
55  var = 0
56  assert_equal('two', var ? 'one' : 'two')
57enddef
58
59func Test_expr1_fails()
60  call CheckDefFailure("let x = 1 ? 'one'", "Missing ':' after '?'")
61  call CheckDefFailure("let x = 1 ? 'one' : xxx", "E1001:")
62
63  let msg = "white space required before and after '?'"
64  call CheckDefFailure("let x = 1? 'one' : 'two'", msg)
65  call CheckDefFailure("let x = 1 ?'one' : 'two'", msg)
66  call CheckDefFailure("let x = 1?'one' : 'two'", msg)
67
68  let msg = "white space required before and after ':'"
69  call CheckDefFailure("let x = 1 ? 'one': 'two'", msg)
70  call CheckDefFailure("let x = 1 ? 'one' :'two'", msg)
71  call CheckDefFailure("let x = 1 ? 'one':'two'", msg)
72endfunc
73
74" TODO: define inside test function
75def Record(val: any): any
76  g:vals->add(val)
77  return val
78enddef
79
80" test ||
81def Test_expr2()
82  assert_equal(2, 2 || 0)
83  assert_equal(7, 0 || 0 || 7)
84  assert_equal(0, 0 || 0)
85  assert_equal('', 0 || '')
86
87  g:vals = []
88  assert_equal(3, Record(3) || Record(1))
89  assert_equal([3], g:vals)
90
91  g:vals = []
92  assert_equal(5, Record(0) || Record(5))
93  assert_equal([0, 5], g:vals)
94
95  g:vals = []
96  assert_equal(4, Record(0) || Record(4) || Record(0))
97  assert_equal([0, 4], g:vals)
98
99  g:vals = []
100  assert_equal(0, Record([]) || Record('') || Record(0))
101  assert_equal([[], '', 0], g:vals)
102enddef
103
104func Test_expr2_fails()
105  let msg = "white space required before and after '||'"
106  call CheckDefFailure("let x = 1||2", msg)
107  call CheckDefFailure("let x = 1 ||2", msg)
108  call CheckDefFailure("let x = 1|| 2", msg)
109
110  call CheckDefFailure("let x = 1 || xxx", 'E1001:')
111endfunc
112
113" test &&
114def Test_expr3()
115  assert_equal(0, 2 && 0)
116  assert_equal(0, 0 && 0 && 7)
117  assert_equal(7, 2 && 3 && 7)
118  assert_equal(0, 0 && 0)
119  assert_equal(0, 0 && '')
120  assert_equal('', 8 && '')
121
122  g:vals = []
123  assert_equal(1, Record(3) && Record(1))
124  assert_equal([3, 1], g:vals)
125
126  g:vals = []
127  assert_equal(0, Record(0) && Record(5))
128  assert_equal([0], g:vals)
129
130  g:vals = []
131  assert_equal(0, Record(0) && Record(4) && Record(0))
132  assert_equal([0], g:vals)
133
134  g:vals = []
135  assert_equal(0, Record(8) && Record(4) && Record(0))
136  assert_equal([8, 4, 0], g:vals)
137
138  g:vals = []
139  assert_equal(0, Record([1]) && Record('z') && Record(0))
140  assert_equal([[1], 'z', 0], g:vals)
141enddef
142
143func Test_expr3_fails()
144  let msg = "white space required before and after '&&'"
145  call CheckDefFailure("let x = 1&&2", msg)
146  call CheckDefFailure("let x = 1 &&2", msg)
147  call CheckDefFailure("let x = 1&& 2", msg)
148endfunc
149
150let atrue = v:true
151let afalse = v:false
152let anone = v:none
153let anull = v:null
154let anint = 10
155let alsoint = 4
156if has('float')
157  let afloat = 0.1
158endif
159let astring = 'asdf'
160let ablob = 0z01ab
161let alist = [2, 3, 4]
162let adict = #{aaa: 2, bbb: 8}
163
164" test == comperator
165def Test_expr4_equal()
166  assert_equal(true, true == true)
167  assert_equal(false, true == false)
168  assert_equal(true, true == g:atrue)
169  assert_equal(false, g:atrue == false)
170
171  assert_equal(true, v:none == v:none)
172  assert_equal(false, v:none == v:null)
173  assert_equal(true, g:anone == v:none)
174  assert_equal(false, v:none == g:anull)
175
176  assert_equal(false, 2 == 0)
177  assert_equal(true, 61 == 61)
178  assert_equal(true, g:anint == 10)
179  assert_equal(false, 61 == g:anint)
180
181  if has('float')
182    assert_equal(true, 0.3 == 0.3)
183    assert_equal(false, 0.4 == 0.3)
184    assert_equal(true, 0.1 == g:afloat)
185    assert_equal(false, g:afloat == 0.3)
186
187    assert_equal(true, 3.0 == 3)
188    assert_equal(true, 3 == 3.0)
189    assert_equal(false, 3.1 == 3)
190    assert_equal(false, 3 == 3.1)
191  endif
192
193  assert_equal(true, 'abc' == 'abc')
194  assert_equal(false, 'xyz' == 'abc')
195  assert_equal(true, g:astring == 'asdf')
196  assert_equal(false, 'xyz' == g:astring)
197
198  assert_equal(false, 'abc' == 'aBc')
199  assert_equal(false, 'abc' ==# 'aBc')
200  assert_equal(true, 'abc' ==? 'aBc')
201
202  assert_equal(false, 'abc' == 'ABC')
203  set ignorecase
204  assert_equal(false, 'abc' == 'ABC')
205  assert_equal(false, 'abc' ==# 'ABC')
206  set noignorecase
207
208  call CheckDefFailure("let x = 'a' == xxx", 'E1001:')
209
210  assert_equal(true, 0z3f == 0z3f)
211  assert_equal(false, 0z3f == 0z4f)
212  assert_equal(true, g:ablob == 0z01ab)
213  assert_equal(false, 0z3f == g:ablob)
214
215  assert_equal(true, [1, 2, 3] == [1, 2, 3])
216  assert_equal(false, [1, 2, 3] == [2, 3, 1])
217  assert_equal(true, [2, 3, 4] == g:alist)
218  assert_equal(false, g:alist == [2, 3, 1])
219  assert_equal(false, [1, 2, 3] == [])
220  assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
221
222  assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
223  assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
224  assert_equal(false, #{one: 1, two: 2} == #{two: 2})
225  assert_equal(false, #{one: 1, two: 2} == #{})
226  assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
227  assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
228
229  assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal'))
230  assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is'))
231
232  assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123]))
233  assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123]))
234  assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999]))
235enddef
236
237" test != comperator
238def Test_expr4_notequal()
239  assert_equal(false, true != true)
240  assert_equal(true, true != false)
241  assert_equal(false, true != g:atrue)
242  assert_equal(true, g:atrue != false)
243
244  assert_equal(false, v:none != v:none)
245  assert_equal(true, v:none != v:null)
246  assert_equal(false, g:anone != v:none)
247  assert_equal(true, v:none != g:anull)
248
249  assert_equal(true, 2 != 0)
250  assert_equal(false, 55 != 55)
251  assert_equal(false, g:anint != 10)
252  assert_equal(true, 61 != g:anint)
253
254  if has('float')
255    assert_equal(false, 0.3 != 0.3)
256    assert_equal(true, 0.4 != 0.3)
257    assert_equal(false, 0.1 != g:afloat)
258    assert_equal(true, g:afloat != 0.3)
259
260    assert_equal(false, 3.0 != 3)
261    assert_equal(false, 3 != 3.0)
262    assert_equal(true, 3.1 != 3)
263    assert_equal(true, 3 != 3.1)
264  endif
265
266  assert_equal(false, 'abc' != 'abc')
267  assert_equal(true, 'xyz' != 'abc')
268  assert_equal(false, g:astring != 'asdf')
269  assert_equal(true, 'xyz' != g:astring)
270
271  assert_equal(true, 'abc' != 'ABC')
272  set ignorecase
273  assert_equal(true, 'abc' != 'ABC')
274  set noignorecase
275
276  assert_equal(false, 0z3f != 0z3f)
277  assert_equal(true, 0z3f != 0z4f)
278  assert_equal(false, g:ablob != 0z01ab)
279  assert_equal(true, 0z3f != g:ablob)
280
281  assert_equal(false, [1, 2, 3] != [1, 2, 3])
282  assert_equal(true, [1, 2, 3] != [2, 3, 1])
283  assert_equal(false, [2, 3, 4] != g:alist)
284  assert_equal(true, g:alist != [2, 3, 1])
285  assert_equal(true, [1, 2, 3] != [])
286  assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
287
288  assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
289  assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
290  assert_equal(true, #{one: 1, two: 2} != #{two: 2})
291  assert_equal(true, #{one: 1, two: 2} != #{})
292  assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
293  assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
294
295  assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal'))
296  assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is'))
297
298  assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123]))
299  assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123]))
300  assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999]))
301enddef
302
303" test > comperator
304def Test_expr4_greater()
305  assert_true(2 > 0)
306  assert_true(2 > 1)
307  assert_false(2 > 2)
308  assert_false(2 > 3)
309  if has('float')
310    assert_true(2.0 > 0.0)
311    assert_true(2.0 > 1.0)
312    assert_false(2.0 > 2.0)
313    assert_false(2.0 > 3.0)
314  endif
315enddef
316
317" test >= comperator
318def Test_expr4_greaterequal()
319  assert_true(2 >= 0)
320  assert_true(2 >= 2)
321  assert_false(2 >= 3)
322  if has('float')
323    assert_true(2.0 >= 0.0)
324    assert_true(2.0 >= 2.0)
325    assert_false(2.0 >= 3.0)
326  endif
327enddef
328
329" test < comperator
330def Test_expr4_smaller()
331  assert_false(2 < 0)
332  assert_false(2 < 2)
333  assert_true(2 < 3)
334  if has('float')
335    assert_false(2.0 < 0.0)
336    assert_false(2.0 < 2.0)
337    assert_true(2.0 < 3.0)
338  endif
339enddef
340
341" test <= comperator
342def Test_expr4_smallerequal()
343  assert_false(2 <= 0)
344  assert_false(2 <= 1)
345  assert_true(2 <= 2)
346  assert_true(2 <= 3)
347  if has('float')
348    assert_false(2.0 <= 0.0)
349    assert_false(2.0 <= 1.0)
350    assert_true(2.0 <= 2.0)
351    assert_true(2.0 <= 3.0)
352  endif
353enddef
354
355" test =~ comperator
356def Test_expr4_match()
357  assert_equal(false, '2' =~ '0')
358  assert_equal(true, '2' =~ '[0-9]')
359enddef
360
361" test !~ comperator
362def Test_expr4_nomatch()
363  assert_equal(true, '2' !~ '0')
364  assert_equal(false, '2' !~ '[0-9]')
365enddef
366
367" test is comperator
368def Test_expr4_is()
369  let mylist = [2]
370  assert_false(mylist is [2])
371  let other = mylist
372  assert_true(mylist is other)
373
374  let myblob = 0z1234
375  assert_false(myblob is 0z1234)
376  let otherblob = myblob
377  assert_true(myblob is otherblob)
378enddef
379
380" test isnot comperator
381def Test_expr4_isnot()
382  let mylist = [2]
383  assert_true('2' isnot '0')
384  assert_true(mylist isnot [2])
385  let other = mylist
386  assert_false(mylist isnot other)
387
388  let myblob = 0z1234
389  assert_true(myblob isnot 0z1234)
390  let otherblob = myblob
391  assert_false(myblob isnot otherblob)
392enddef
393
394def RetVoid()
395  let x = 1
396enddef
397
398func Test_expr4_fails()
399  let msg = "white space required before and after '>'"
400  call CheckDefFailure("let x = 1>2", msg)
401  call CheckDefFailure("let x = 1 >2", msg)
402  call CheckDefFailure("let x = 1> 2", msg)
403
404  let msg = "white space required before and after '=='"
405  call CheckDefFailure("let x = 1==2", msg)
406  call CheckDefFailure("let x = 1 ==2", msg)
407  call CheckDefFailure("let x = 1== 2", msg)
408
409  let msg = "white space required before and after 'is'"
410  call CheckDefFailure("let x = '1'is'2'", msg)
411  call CheckDefFailure("let x = '1' is'2'", msg)
412  call CheckDefFailure("let x = '1'is '2'", msg)
413
414  let msg = "white space required before and after 'isnot'"
415  call CheckDefFailure("let x = '1'isnot'2'", msg)
416  call CheckDefFailure("let x = '1' isnot'2'", msg)
417  call CheckDefFailure("let x = '1'isnot '2'", msg)
418
419  call CheckDefFailure("let x = 1 is# 2", 'E15:')
420  call CheckDefFailure("let x = 1 is? 2", 'E15:')
421  call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
422  call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
423
424  call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
425  call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
426  call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
427  call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
428
429  call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool')
430  call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool')
431  call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool')
432  call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool')
433  call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool')
434  call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
435  call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
436  call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
437
438  call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
439  call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
440  call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
441  call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
442  if has('float')
443    call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
444    call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
445  endif
446
447  call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob')
448  call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob')
449  call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob')
450  call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob')
451  call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
452  call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
453
454  call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list')
455  call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list')
456  call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list')
457  call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list')
458  call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
459  call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
460
461  call CheckDefFailureMult(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel')
462  call CheckDefFailureMult(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list')
463  call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func')
464  call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func')
465endfunc
466
467" test addition, subtraction, concatenation
468def Test_expr5()
469  assert_equal(66, 60 + 6)
470  assert_equal(70, 60 + g:anint)
471  assert_equal(9, g:alsoint + 5)
472  assert_equal(14, g:alsoint + g:anint)
473
474  assert_equal(54, 60 - 6)
475  assert_equal(50, 60 - g:anint)
476  assert_equal(-1, g:alsoint - 5)
477  assert_equal(-6, g:alsoint - g:anint)
478
479  assert_equal('hello', 'hel' .. 'lo')
480  assert_equal('hello 123', 'hello ' .. 123)
481  assert_equal('123 hello', 123 .. ' hello')
482  assert_equal('123456', 123 .. 456)
483
484  assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
485  assert_equal(0z11223344, 0z1122 + 0z3344)
486  assert_equal(0z112201ab, 0z1122 + g:ablob)
487  assert_equal(0z01ab3344, g:ablob + 0z3344)
488  assert_equal(0z01ab01ab, g:ablob + g:ablob)
489enddef
490
491def Test_expr5_float()
492  if !has('float')
493    MissingFeature 'float'
494  else
495    assert_equal(66.0, 60.0 + 6.0)
496    assert_equal(66.0, 60.0 + 6)
497    assert_equal(66.0, 60 + 6.0)
498    assert_equal(5.1, g:afloat + 5)
499    assert_equal(8.1, 8 + g:afloat)
500    assert_equal(10.1, g:anint + g:afloat)
501    assert_equal(10.1, g:afloat + g:anint)
502
503    assert_equal(54.0, 60.0 - 6.0)
504    assert_equal(54.0, 60.0 - 6)
505    assert_equal(54.0, 60 - 6.0)
506    assert_equal(-4.9, g:afloat - 5)
507    assert_equal(7.9, 8 - g:afloat)
508    assert_equal(9.9, g:anint - g:afloat)
509    assert_equal(-9.9, g:afloat - g:anint)
510  endif
511enddef
512
513func Test_expr5_fails()
514  let msg = "white space required before and after '+'"
515  call CheckDefFailure("let x = 1+2", msg)
516  call CheckDefFailure("let x = 1 +2", msg)
517  call CheckDefFailure("let x = 1+ 2", msg)
518
519  let msg = "white space required before and after '-'"
520  call CheckDefFailure("let x = 1-2", msg)
521  call CheckDefFailure("let x = 1 -2", msg)
522  call CheckDefFailure("let x = 1- 2", msg)
523
524  let msg = "white space required before and after '..'"
525  call CheckDefFailure("let x = '1'..'2'", msg)
526  call CheckDefFailure("let x = '1' ..'2'", msg)
527  call CheckDefFailure("let x = '1'.. '2'", msg)
528
529  call CheckDefFailure("let x = 0z1122 + 33", 'E1035')
530  call CheckDefFailure("let x = 0z1122 + [3]", 'E1035')
531  call CheckDefFailure("let x = 0z1122 + 'asd'", 'E1035')
532  call CheckDefFailure("let x = 33 + 0z1122", 'E1035')
533  call CheckDefFailure("let x = [3] + 0z1122", 'E1035')
534  call CheckDefFailure("let x = 'asdf' + 0z1122", 'E1035')
535  call CheckDefFailure("let x = 6 + xxx", 'E1001')
536endfunc
537
538" test multiply, divide, modulo
539def Test_expr6()
540  assert_equal(36, 6 * 6)
541  assert_equal(24, 6 * g:alsoint)
542  assert_equal(24, g:alsoint * 6)
543  assert_equal(40, g:anint * g:alsoint)
544
545  assert_equal(10, 60 / 6)
546  assert_equal(6, 60 / g:anint)
547  assert_equal(1, g:anint / 6)
548  assert_equal(2, g:anint / g:alsoint)
549
550  assert_equal(5, 11 % 6)
551  assert_equal(4, g:anint % 6)
552  assert_equal(3, 13 % g:anint)
553  assert_equal(2, g:anint % g:alsoint)
554
555  assert_equal(4, 6 * 4 / 6)
556
557  let x = [2]
558  let y = [3]
559  assert_equal(5, x[0] + y[0])
560  assert_equal(6, x[0] * y[0])
561  if has('float')
562    let xf = [2.0]
563    let yf = [3.0]
564    assert_equal(5.0, xf[0] + yf[0])
565    assert_equal(6.0, xf[0] * yf[0])
566  endif
567
568  call CheckDefFailure("let x = 6 * xxx", 'E1001')
569enddef
570
571def Test_expr6_float()
572  if !has('float')
573    MissingFeature 'float'
574  else
575    assert_equal(36.0, 6.0 * 6)
576    assert_equal(36.0, 6 * 6.0)
577    assert_equal(36.0, 6.0 * 6.0)
578    assert_equal(1.0, g:afloat * g:anint)
579
580    assert_equal(10.0, 60 / 6.0)
581    assert_equal(10.0, 60.0 / 6)
582    assert_equal(10.0, 60.0 / 6.0)
583    assert_equal(0.01, g:afloat / g:anint)
584
585    assert_equal(4.0, 6.0 * 4 / 6)
586    assert_equal(4.0, 6 * 4.0 / 6)
587    assert_equal(4.0, 6 * 4 / 6.0)
588    assert_equal(4.0, 6.0 * 4.0 / 6)
589    assert_equal(4.0, 6 * 4.0 / 6.0)
590    assert_equal(4.0, 6.0 * 4 / 6.0)
591    assert_equal(4.0, 6.0 * 4.0 / 6.0)
592
593    assert_equal(4.0, 6.0 * 4.0 / 6.0)
594  endif
595enddef
596
597func Test_expr6_fails()
598  let msg = "white space required before and after '*'"
599  call CheckDefFailure("let x = 1*2", msg)
600  call CheckDefFailure("let x = 1 *2", msg)
601  call CheckDefFailure("let x = 1* 2", msg)
602
603  let msg = "white space required before and after '/'"
604  call CheckDefFailure("let x = 1/2", msg)
605  call CheckDefFailure("let x = 1 /2", msg)
606  call CheckDefFailure("let x = 1/ 2", msg)
607
608  let msg = "white space required before and after '%'"
609  call CheckDefFailure("let x = 1%2", msg)
610  call CheckDefFailure("let x = 1 %2", msg)
611  call CheckDefFailure("let x = 1% 2", msg)
612
613  call CheckDefFailure("let x = '1' * '2'", 'E1036:')
614  call CheckDefFailure("let x = '1' / '2'", 'E1036:')
615  call CheckDefFailure("let x = '1' % '2'", 'E1035:')
616
617  call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
618  call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
619  call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
620
621  call CheckDefFailure("let x = [1] * [2]", 'E1036:')
622  call CheckDefFailure("let x = [1] / [2]", 'E1036:')
623  call CheckDefFailure("let x = [1] % [2]", 'E1035:')
624
625  call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
626  call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
627  call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
628
629  call CheckDefFailure("let x = 0xff[1]", 'E714:')
630  if has('float')
631    call CheckDefFailure("let x = 0.7[1]", 'E714:')
632  endif
633endfunc
634
635func Test_expr6_float_fails()
636  CheckFeature float
637  call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
638endfunc
639
640" define here to use old style parsing
641if has('float')
642  let g:float_zero = 0.0
643  let g:float_neg = -9.8
644  let g:float_big = 9.9e99
645endif
646let g:blob_empty = 0z
647let g:blob_one = 0z01
648let g:blob_long = 0z0102.0304
649
650let g:string_empty = ''
651let g:string_short = 'x'
652let g:string_long = 'abcdefghijklm'
653let g:string_special = "ab\ncd\ref\ekk"
654
655let g:special_true = v:true
656let g:special_false = v:false
657let g:special_null = v:null
658let g:special_none = v:none
659
660let g:list_empty = []
661let g:list_mixed = [1, 'b', v:false]
662
663let g:dict_empty = {}
664let g:dict_one = #{one: 1}
665
666let $TESTVAR = 'testvar'
667
668" test low level expression
669def Test_expr7_number()
670  " number constant
671  assert_equal(0, 0)
672  assert_equal(654, 0654)
673
674  assert_equal(6, 0x6)
675  assert_equal(15, 0xf)
676  assert_equal(255, 0xff)
677enddef
678
679def Test_expr7_float()
680  " float constant
681  if !has('float')
682    MissingFeature 'float'
683  else
684    assert_equal(g:float_zero, .0)
685    assert_equal(g:float_zero, 0.0)
686    assert_equal(g:float_neg, -9.8)
687    assert_equal(g:float_big, 9.9e99)
688  endif
689enddef
690
691def Test_expr7_blob()
692  " blob constant
693  assert_equal(g:blob_empty, 0z)
694  assert_equal(g:blob_one, 0z01)
695  assert_equal(g:blob_long, 0z0102.0304)
696
697  call CheckDefFailure("let x = 0z123", 'E973:')
698enddef
699
700def Test_expr7_string()
701  " string constant
702  assert_equal(g:string_empty, '')
703  assert_equal(g:string_empty, "")
704  assert_equal(g:string_short, 'x')
705  assert_equal(g:string_short, "x")
706  assert_equal(g:string_long, 'abcdefghijklm')
707  assert_equal(g:string_long, "abcdefghijklm")
708  assert_equal(g:string_special, "ab\ncd\ref\ekk")
709
710  call CheckDefFailure('let x = "abc', 'E114:')
711  call CheckDefFailure("let x = 'abc", 'E115:')
712enddef
713
714def Test_expr7_vimvar()
715  let old: list<string> = v:oldfiles
716  let compl: dict<any> = v:completed_item
717
718  call CheckDefFailure("let old: list<number> = v:oldfiles", 'E1013: type mismatch, expected list<number> but got list<string>')
719  call CheckDefFailure("let old: dict<number> = v:completed_item", 'E1013: type mismatch, expected dict<number> but got dict<any>')
720enddef
721
722def Test_expr7_special()
723  " special constant
724  assert_equal(g:special_true, true)
725  assert_equal(g:special_false, false)
726  assert_equal(g:special_true, v:true)
727  assert_equal(g:special_false, v:false)
728  assert_equal(g:special_null, v:null)
729  assert_equal(g:special_none, v:none)
730
731  call CheckDefFailure('v:true = true', 'E46:')
732  call CheckDefFailure('v:true = false', 'E46:')
733  call CheckDefFailure('v:false = true', 'E46:')
734  call CheckDefFailure('v:null = 11', 'E46:')
735  call CheckDefFailure('v:none = 22', 'E46:')
736enddef
737
738def Test_expr7_list()
739  " list
740  assert_equal(g:list_empty, [])
741  assert_equal(g:list_empty, [  ])
742  assert_equal(g:list_mixed, [1, 'b', false])
743  assert_equal('b', g:list_mixed[1])
744
745  call CheckDefExecFailure("let x = g:anint[3]", 'E714:')
746  call CheckDefFailure("let x = g:list_mixed[xxx]", 'E1001:')
747  call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:')
748  call CheckDefFailure("let x = g:list_mixed[0", 'E111:')
749  call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:')
750enddef
751
752def Test_expr7_lambda()
753  " lambda
754  let La = { -> 'result'}
755  assert_equal('result', La())
756  assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
757enddef
758
759def Test_expr7_dict()
760  " dictionary
761  assert_equal(g:dict_empty, {})
762  assert_equal(g:dict_empty, {  })
763  assert_equal(g:dict_one, {'one': 1})
764  let key = 'one'
765  let val = 1
766  assert_equal(g:dict_one, {key: val})
767
768  call CheckDefFailure("let x = #{8: 8}", 'E1014:')
769  call CheckDefFailure("let x = #{xxx}", 'E720:')
770  call CheckDefFailure("let x = #{xxx: 1", 'E722:')
771  call CheckDefFailure("let x = #{xxx: 1,", 'E723:')
772  call CheckDefFailure("let x = {'a': xxx}", 'E1001:')
773  call CheckDefFailure("let x = {xxx: 8}", 'E1001:')
774  call CheckDefFailure("let x = #{a: 1, a: 2}", 'E721:')
775  call CheckDefFailure("let x = #", 'E1015:')
776  call CheckDefFailure("let x += 1", 'E1020:')
777  call CheckDefFailure("let x = x + 1", 'E1001:')
778  call CheckDefExecFailure("let x = g:anint.member", 'E715:')
779  call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:')
780enddef
781
782def Test_expr_member()
783  assert_equal(1, g:dict_one.one)
784
785  call CheckDefFailure("let x = g:dict_one.#$!", 'E1002:')
786enddef
787
788def Test_expr7_option()
789  " option
790  set ts=11
791  assert_equal(11, &ts)
792  &ts = 9
793  assert_equal(9, &ts)
794  set ts=8
795  set grepprg=some\ text
796  assert_equal('some text', &grepprg)
797  &grepprg = test_null_string()
798  assert_equal('', &grepprg)
799  set grepprg&
800enddef
801
802def Test_expr7_environment()
803  " environment variable
804  assert_equal('testvar', $TESTVAR)
805  assert_equal('', $ASDF_ASD_XXX)
806
807  call CheckDefFailure("let x = $$$", 'E1002:')
808enddef
809
810def Test_expr7_register()
811  @a = 'register a'
812  assert_equal('register a', @a)
813enddef
814
815def Test_expr7_parens()
816  " (expr)
817  assert_equal(4, (6 * 4) / 6)
818  assert_equal(0, 6 * ( 4 / 6 ))
819
820  assert_equal(6, +6)
821  assert_equal(-6, -6)
822  assert_equal(6, --6)
823  assert_equal(6, -+-6)
824  assert_equal(-6, ---6)
825enddef
826
827def Test_expr7_negate()
828  assert_equal(-99, -99)
829  assert_equal(99, --99)
830  let nr = 88
831  assert_equal(-88, -nr)
832  assert_equal(88, --nr)
833enddef
834
835def Echo(arg): string
836  return arg
837enddef
838
839def s:EchoArg(arg): string
840  return arg
841enddef
842
843def Test_expr7_call()
844  assert_equal('yes', 'yes'->Echo())
845  assert_equal('yes', 'yes'->s:EchoArg())
846
847  call CheckDefFailure("let x = 'yes'->Echo", 'E107:')
848enddef
849
850
851def Test_expr7_not()
852  assert_equal(true, !'')
853  assert_equal(true, ![])
854  assert_equal(false, !'asdf')
855  assert_equal(false, ![2])
856  assert_equal(true, !!'asdf')
857  assert_equal(true, !![2])
858
859  assert_equal(true, !test_null_partial())
860  assert_equal(false, !{-> 'yes'})
861
862  assert_equal(true, !test_null_dict())
863  assert_equal(true, !{})
864  assert_equal(false, !{'yes': 'no'})
865
866  if has('channel')
867    assert_equal(true, !test_null_job())
868    assert_equal(true, !test_null_channel())
869  endif
870
871  assert_equal(true, !test_null_blob())
872  assert_equal(true, !0z)
873  assert_equal(false, !0z01)
874
875  assert_equal(true, !test_void())
876  assert_equal(true, !test_unknown())
877enddef
878
879func Test_expr7_fails()
880  call CheckDefFailure("let x = (12", "E110:")
881
882  call CheckDefFailure("let x = -'xx'", "E1030:")
883  call CheckDefFailure("let x = +'xx'", "E1030:")
884  call CheckDefFailure("let x = -0z12", "E974:")
885  call CheckDefExecFailure("let x = -[8]", "E39:")
886  call CheckDefExecFailure("let x = -{'a': 1}", "E39:")
887
888  call CheckDefFailure("let x = @", "E1002:")
889  call CheckDefFailure("let x = @<", "E354:")
890
891  call CheckDefFailure("let x = [1, 2", "E697:")
892  call CheckDefFailure("let x = [notfound]", "E1001:")
893
894  call CheckDefFailure("let x = { -> 123) }", "E451:")
895  call CheckDefFailure("let x = 123->{x -> x + 5) }", "E451:")
896
897  call CheckDefFailure("let x = &notexist", 'E113:')
898  call CheckDefFailure("&grepprg = [343]", 'E1013:')
899
900  call CheckDefExecFailure("echo s:doesnt_exist", 'E121:')
901  call CheckDefExecFailure("echo g:doesnt_exist", 'E121:')
902
903  call CheckDefFailure("echo a:somevar", 'E1075:')
904  call CheckDefFailure("echo l:somevar", 'E1075:')
905  call CheckDefFailure("echo x:somevar", 'E1075:')
906
907  " TODO
908  call CheckDefFailure("echo b:somevar", 'not supported yet')
909  call CheckDefFailure("echo w:somevar", 'not supported yet')
910  call CheckDefFailure("echo t:somevar", 'not supported yet')
911
912  call CheckDefExecFailure("let x = +g:astring", 'E1030:')
913  call CheckDefExecFailure("let x = +g:ablob", 'E974:')
914  call CheckDefExecFailure("let x = +g:alist", 'E745:')
915  call CheckDefExecFailure("let x = +g:adict", 'E728:')
916
917  call CheckDefFailureMult(["let x = ''", "let y = x.memb"], 'E715:')
918
919  call CheckDefExecFailure("[1, 2->len()", 'E492:')
920  call CheckDefExecFailure("#{a: 1->len()", 'E488:')
921  call CheckDefExecFailure("{'a': 1->len()", 'E492:')
922endfunc
923
924let g:Funcrefs = [function('add')]
925
926func CallMe(arg)
927  return a:arg
928endfunc
929
930func CallMe2(one, two)
931  return a:one .. a:two
932endfunc
933
934def Test_expr7_trailing()
935  " user function call
936  assert_equal(123, CallMe(123))
937  assert_equal(123, CallMe(  123))
938  assert_equal(123, CallMe(123  ))
939  assert_equal('yesno', CallMe2('yes', 'no'))
940  assert_equal('yesno', CallMe2( 'yes', 'no' ))
941  assert_equal('nothing', CallMe('nothing'))
942
943  " partial call
944  let Part = function('CallMe')
945  assert_equal('yes', Part('yes'))
946
947  " funcref call, using list index
948  let l = []
949  g:Funcrefs[0](l, 2)
950  assert_equal([2], l)
951
952  " method call
953  l = [2, 5, 6]
954  l->map({k, v -> k + v})
955  assert_equal([2, 6, 8], l)
956
957  " lambda method call
958  l = [2, 5]
959  l->{l -> add(l, 8)}()
960  assert_equal([2, 5, 8], l)
961
962  " dict member
963  let d = #{key: 123}
964  assert_equal(123, d.key)
965enddef
966
967func Test_expr7_trailing_fails()
968  call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
969  call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274')
970endfunc
971
972func Test_expr_fails()
973  call CheckDefFailure("let x = '1'is2", 'E488:')
974  call CheckDefFailure("let x = '1'isnot2", 'E488:')
975
976  call CheckDefExecFailure("CallMe ('yes')", 'E492:')
977  call CheckDefFailure("CallMe2('yes','no')", 'E1069:')
978  call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:')
979
980  call CheckDefFailure("v:nosuch += 3", 'E1001:')
981  call CheckDefFailure("let v:statusmsg = ''", 'E1064:')
982  call CheckDefFailure("let asdf = v:nosuch", 'E1001:')
983
984  call CheckDefFailure("echo len('asdf'", 'E110:')
985  call CheckDefFailure("echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()", 'E1011:')
986  call CheckDefFailure("echo doesnotexist()", 'E117:')
987endfunc
988