blob: 1ec3c6f1b028482a6fbbcc2f7403fe1d0d125974 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" 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
Bram Moolenaar42a480b2020-02-29 23:23:47 +010012func 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
Bram Moolenaar38a5f512020-02-19 12:40:39 +010018" 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
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010026func 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')
Bram Moolenaar5feabe02020-01-30 18:24:53 +010036 if has('float')
37 assert_equal('one', 0.1 ? 'one' : 'two')
38 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039 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')
Bram Moolenaar7f829ca2020-01-31 22:12:41 +010048 if has('float')
49 assert_equal('two', 0.0 ? 'one' : 'two')
50 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010051 assert_equal('two', '' ? 'one' : 'two')
52" assert_equal('one', 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
62 let msg = "white space required before and after '?'"
63 call CheckDefFailure("let x = 1? 'one' : 'two'", msg)
64 call CheckDefFailure("let x = 1 ?'one' : 'two'", msg)
65 call CheckDefFailure("let x = 1?'one' : 'two'", msg)
66
67 let msg = "white space required before and after ':'"
68 call CheckDefFailure("let x = 1 ? 'one': 'two'", msg)
69 call CheckDefFailure("let x = 1 ? 'one' :'two'", msg)
70 call CheckDefFailure("let x = 1 ? 'one':'two'", msg)
71endfunc
72
73" TODO: define inside test function
74def Record(val: any): any
75 g:vals->add(val)
76 return val
77enddef
78
79" test ||
80def Test_expr2()
81 assert_equal(2, 2 || 0)
82 assert_equal(7, 0 || 0 || 7)
83 assert_equal(0, 0 || 0)
84 assert_equal('', 0 || '')
85
86 g:vals = []
87 assert_equal(3, Record(3) || Record(1))
88 assert_equal([3], g:vals)
89
90 g:vals = []
91 assert_equal(5, Record(0) || Record(5))
92 assert_equal([0, 5], g:vals)
93
94 g:vals = []
95 assert_equal(4, Record(0) || Record(4) || Record(0))
96 assert_equal([0, 4], g:vals)
97
98 g:vals = []
99 assert_equal(0, Record([]) || Record('') || Record(0))
100 assert_equal([[], '', 0], g:vals)
101enddef
102
103func Test_expr2_fails()
104 let msg = "white space required before and after '||'"
105 call CheckDefFailure("let x = 1||2", msg)
106 call CheckDefFailure("let x = 1 ||2", msg)
107 call CheckDefFailure("let x = 1|| 2", msg)
108endfunc
109
110" test &&
111def Test_expr3()
112 assert_equal(0, 2 && 0)
113 assert_equal(0, 0 && 0 && 7)
114 assert_equal(7, 2 && 3 && 7)
115 assert_equal(0, 0 && 0)
116 assert_equal(0, 0 && '')
117 assert_equal('', 8 && '')
118
119 g:vals = []
120 assert_equal(1, Record(3) && Record(1))
121 assert_equal([3, 1], g:vals)
122
123 g:vals = []
124 assert_equal(0, Record(0) && Record(5))
125 assert_equal([0], g:vals)
126
127 g:vals = []
128 assert_equal(0, Record(0) && Record(4) && Record(0))
129 assert_equal([0], g:vals)
130
131 g:vals = []
132 assert_equal(0, Record(8) && Record(4) && Record(0))
133 assert_equal([8, 4, 0], g:vals)
134
135 g:vals = []
136 assert_equal(0, Record([1]) && Record('z') && Record(0))
137 assert_equal([[1], 'z', 0], g:vals)
138enddef
139
140func Test_expr3_fails()
141 let msg = "white space required before and after '&&'"
142 call CheckDefFailure("let x = 1&&2", msg)
143 call CheckDefFailure("let x = 1 &&2", msg)
144 call CheckDefFailure("let x = 1&& 2", msg)
145endfunc
146
147let atrue = v:true
148let afalse = v:false
149let anone = v:none
150let anull = v:null
151let anint = 10
152let alsoint = 4
153if has('float')
154 let afloat = 0.1
155endif
156let astring = 'asdf'
157let ablob = 0z01ab
158let alist = [2, 3, 4]
159let adict = #{aaa: 2, bbb: 8}
160
161" test == comperator
162def Test_expr4_equal()
163 assert_equal(true, true == true)
164 assert_equal(false, true == false)
165 assert_equal(true, true == g:atrue)
166 assert_equal(false, g:atrue == false)
167
168 assert_equal(true, v:none == v:none)
169 assert_equal(false, v:none == v:null)
170 assert_equal(true, g:anone == v:none)
171 assert_equal(false, v:none == g:anull)
172
173 assert_equal(false, 2 == 0)
174 assert_equal(true, 61 == 61)
175 assert_equal(true, g:anint == 10)
176 assert_equal(false, 61 == g:anint)
177
178 if has('float')
179 assert_equal(true, 0.3 == 0.3)
180 assert_equal(false, 0.4 == 0.3)
181 assert_equal(true, 0.1 == g:afloat)
182 assert_equal(false, g:afloat == 0.3)
183
184 assert_equal(true, 3.0 == 3)
185 assert_equal(true, 3 == 3.0)
186 assert_equal(false, 3.1 == 3)
187 assert_equal(false, 3 == 3.1)
188 endif
189
190 assert_equal(true, 'abc' == 'abc')
191 assert_equal(false, 'xyz' == 'abc')
192 assert_equal(true, g:astring == 'asdf')
193 assert_equal(false, 'xyz' == g:astring)
194
195 assert_equal(false, 'abc' == 'ABC')
196 set ignorecase
197 assert_equal(false, 'abc' == 'ABC')
198 set noignorecase
199
200 assert_equal(true, 0z3f == 0z3f)
201 assert_equal(false, 0z3f == 0z4f)
202 assert_equal(true, g:ablob == 0z01ab)
203 assert_equal(false, 0z3f == g:ablob)
204
205 assert_equal(true, [1, 2, 3] == [1, 2, 3])
206 assert_equal(false, [1, 2, 3] == [2, 3, 1])
207 assert_equal(true, [2, 3, 4] == g:alist)
208 assert_equal(false, g:alist == [2, 3, 1])
209 assert_equal(false, [1, 2, 3] == [])
210 assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
211
212 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
213 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
214 assert_equal(false, #{one: 1, two: 2} == #{two: 2})
215 assert_equal(false, #{one: 1, two: 2} == #{})
216 assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
217 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
218
219 assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal'))
220 assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is'))
221
222 assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123]))
223 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123]))
224 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999]))
225enddef
226
227" test != comperator
228def Test_expr4_notequal()
229 assert_equal(false, true != true)
230 assert_equal(true, true != false)
231 assert_equal(false, true != g:atrue)
232 assert_equal(true, g:atrue != false)
233
234 assert_equal(false, v:none != v:none)
235 assert_equal(true, v:none != v:null)
236 assert_equal(false, g:anone != v:none)
237 assert_equal(true, v:none != g:anull)
238
239 assert_equal(true, 2 != 0)
240 assert_equal(false, 55 != 55)
241 assert_equal(false, g:anint != 10)
242 assert_equal(true, 61 != g:anint)
243
244 if has('float')
245 assert_equal(false, 0.3 != 0.3)
246 assert_equal(true, 0.4 != 0.3)
247 assert_equal(false, 0.1 != g:afloat)
248 assert_equal(true, g:afloat != 0.3)
249
250 assert_equal(false, 3.0 != 3)
251 assert_equal(false, 3 != 3.0)
252 assert_equal(true, 3.1 != 3)
253 assert_equal(true, 3 != 3.1)
254 endif
255
256 assert_equal(false, 'abc' != 'abc')
257 assert_equal(true, 'xyz' != 'abc')
258 assert_equal(false, g:astring != 'asdf')
259 assert_equal(true, 'xyz' != g:astring)
260
261 assert_equal(true, 'abc' != 'ABC')
262 set ignorecase
263 assert_equal(true, 'abc' != 'ABC')
264 set noignorecase
265
266 assert_equal(false, 0z3f != 0z3f)
267 assert_equal(true, 0z3f != 0z4f)
268 assert_equal(false, g:ablob != 0z01ab)
269 assert_equal(true, 0z3f != g:ablob)
270
271 assert_equal(false, [1, 2, 3] != [1, 2, 3])
272 assert_equal(true, [1, 2, 3] != [2, 3, 1])
273 assert_equal(false, [2, 3, 4] != g:alist)
274 assert_equal(true, g:alist != [2, 3, 1])
275 assert_equal(true, [1, 2, 3] != [])
276 assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
277
278 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
279 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
280 assert_equal(true, #{one: 1, two: 2} != #{two: 2})
281 assert_equal(true, #{one: 1, two: 2} != #{})
282 assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
283 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
284
285 assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal'))
286 assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is'))
287
288 assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123]))
289 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123]))
290 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999]))
291enddef
292
293" test > comperator
294def Test_expr4_greater()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100295 assert_true(2 > 0)
296 assert_true(2 > 1)
297 assert_false(2 > 2)
298 assert_false(2 > 3)
299 if has('float')
300 assert_true(2.0 > 0.0)
301 assert_true(2.0 > 1.0)
302 assert_false(2.0 > 2.0)
303 assert_false(2.0 > 3.0)
304 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305enddef
306
307" test >= comperator
308def Test_expr4_greaterequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100309 assert_true(2 >= 0)
310 assert_true(2 >= 2)
311 assert_false(2 >= 3)
312 if has('float')
313 assert_true(2.0 >= 0.0)
314 assert_true(2.0 >= 2.0)
315 assert_false(2.0 >= 3.0)
316 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317enddef
318
319" test < comperator
320def Test_expr4_smaller()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100321 assert_false(2 < 0)
322 assert_false(2 < 2)
323 assert_true(2 < 3)
324 if has('float')
325 assert_false(2.0 < 0.0)
326 assert_false(2.0 < 2.0)
327 assert_true(2.0 < 3.0)
328 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329enddef
330
331" test <= comperator
332def Test_expr4_smallerequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100333 assert_false(2 <= 0)
334 assert_false(2 <= 1)
335 assert_true(2 <= 2)
336 assert_true(2 <= 3)
337 if has('float')
338 assert_false(2.0 <= 0.0)
339 assert_false(2.0 <= 1.0)
340 assert_true(2.0 <= 2.0)
341 assert_true(2.0 <= 3.0)
342 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343enddef
344
345" test =~ comperator
346def Test_expr4_match()
347 assert_equal(false, '2' =~ '0')
348 assert_equal(true, '2' =~ '[0-9]')
349enddef
350
351" test !~ comperator
352def Test_expr4_nomatch()
353 assert_equal(true, '2' !~ '0')
354 assert_equal(false, '2' !~ '[0-9]')
355enddef
356
357" test is comperator
358def Test_expr4_is()
359 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100360 assert_false(mylist is [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100362 assert_true(mylist is other)
363
364 let myblob = 0z1234
365 assert_false(myblob is 0z1234)
366 let otherblob = myblob
367 assert_true(myblob is otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368enddef
369
370" test isnot comperator
371def Test_expr4_isnot()
372 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100373 assert_true('2' isnot '0')
374 assert_true(mylist isnot [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100376 assert_false(mylist isnot other)
377
378 let myblob = 0z1234
379 assert_true(myblob isnot 0z1234)
380 let otherblob = myblob
381 assert_false(myblob isnot otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382enddef
383
384def RetVoid()
385 let x = 1
386enddef
387
388func Test_expr4_fails()
389 let msg = "white space required before and after '>'"
390 call CheckDefFailure("let x = 1>2", msg)
391 call CheckDefFailure("let x = 1 >2", msg)
392 call CheckDefFailure("let x = 1> 2", msg)
393
394 let msg = "white space required before and after '=='"
395 call CheckDefFailure("let x = 1==2", msg)
396 call CheckDefFailure("let x = 1 ==2", msg)
397 call CheckDefFailure("let x = 1== 2", msg)
398
399 let msg = "white space required before and after 'is'"
400 call CheckDefFailure("let x = '1'is'2'", msg)
401 call CheckDefFailure("let x = '1' is'2'", msg)
402 call CheckDefFailure("let x = '1'is '2'", msg)
403
404 let msg = "white space required before and after 'isnot'"
405 call CheckDefFailure("let x = '1'isnot'2'", msg)
406 call CheckDefFailure("let x = '1' isnot'2'", msg)
407 call CheckDefFailure("let x = '1'isnot '2'", msg)
408
409 call CheckDefFailure("let x = 1 is# 2", 'E15:')
410 call CheckDefFailure("let x = 1 is? 2", 'E15:')
411 call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
412 call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
413
414 call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
415 call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
416 call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
417 call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
418
419 call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool')
420 call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool')
421 call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool')
422 call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool')
423 call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool')
424 call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
425 call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
426 call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
427
428 call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
429 call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
430 call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
431 call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
432 if has('float')
433 call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
434 call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
435 endif
436
437 call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob')
438 call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob')
439 call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob')
440 call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob')
441 call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
442 call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
443
444 call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list')
445 call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list')
446 call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list')
447 call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list')
448 call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
449 call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
450endfunc
451
452" test addition, subtraction, concatenation
453def Test_expr5()
454 assert_equal(66, 60 + 6)
455 assert_equal(70, 60 + g:anint)
456 assert_equal(9, g:alsoint + 5)
457 assert_equal(14, g:alsoint + g:anint)
458
459 assert_equal(54, 60 - 6)
460 assert_equal(50, 60 - g:anint)
461 assert_equal(-1, g:alsoint - 5)
462 assert_equal(-6, g:alsoint - g:anint)
463
464 assert_equal('hello', 'hel' .. 'lo')
465 assert_equal('hello 123', 'hello ' .. 123)
466 assert_equal('123 hello', 123 .. ' hello')
467 assert_equal('123456', 123 .. 456)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100468
469 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
470 assert_equal(0z11223344, 0z1122 + 0z3344)
471 assert_equal(0z112201ab, 0z1122 + g:ablob)
472 assert_equal(0z01ab3344, g:ablob + 0z3344)
473 assert_equal(0z01ab01ab, g:ablob + g:ablob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474enddef
475
476def Test_expr5_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100477 if !has('float')
478 MissingFeature 'float'
479 else
480 assert_equal(66.0, 60.0 + 6.0)
481 assert_equal(66.0, 60.0 + 6)
482 assert_equal(66.0, 60 + 6.0)
483 assert_equal(5.1, g:afloat + 5)
484 assert_equal(8.1, 8 + g:afloat)
485 assert_equal(10.1, g:anint + g:afloat)
486 assert_equal(10.1, g:afloat + g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100488 assert_equal(54.0, 60.0 - 6.0)
489 assert_equal(54.0, 60.0 - 6)
490 assert_equal(54.0, 60 - 6.0)
491 assert_equal(-4.9, g:afloat - 5)
492 assert_equal(7.9, 8 - g:afloat)
493 assert_equal(9.9, g:anint - g:afloat)
494 assert_equal(-9.9, g:afloat - g:anint)
495 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496enddef
497
498func Test_expr5_fails()
499 let msg = "white space required before and after '+'"
500 call CheckDefFailure("let x = 1+2", msg)
501 call CheckDefFailure("let x = 1 +2", msg)
502 call CheckDefFailure("let x = 1+ 2", msg)
503
504 let msg = "white space required before and after '-'"
505 call CheckDefFailure("let x = 1-2", msg)
506 call CheckDefFailure("let x = 1 -2", msg)
507 call CheckDefFailure("let x = 1- 2", msg)
508
509 let msg = "white space required before and after '..'"
510 call CheckDefFailure("let x = '1'..'2'", msg)
511 call CheckDefFailure("let x = '1' ..'2'", msg)
512 call CheckDefFailure("let x = '1'.. '2'", msg)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100513
514 call CheckDefFailure("let x = 0z1122 + 33", 'E1035')
515 call CheckDefFailure("let x = 0z1122 + [3]", 'E1035')
516 call CheckDefFailure("let x = 0z1122 + 'asd'", 'E1035')
517 call CheckDefFailure("let x = 33 + 0z1122", 'E1035')
518 call CheckDefFailure("let x = [3] + 0z1122", 'E1035')
519 call CheckDefFailure("let x = 'asdf' + 0z1122", 'E1035')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520endfunc
521
522" test multiply, divide, modulo
523def Test_expr6()
524 assert_equal(36, 6 * 6)
525 assert_equal(24, 6 * g:alsoint)
526 assert_equal(24, g:alsoint * 6)
527 assert_equal(40, g:anint * g:alsoint)
528
529 assert_equal(10, 60 / 6)
530 assert_equal(6, 60 / g:anint)
531 assert_equal(1, g:anint / 6)
532 assert_equal(2, g:anint / g:alsoint)
533
534 assert_equal(5, 11 % 6)
535 assert_equal(4, g:anint % 6)
536 assert_equal(3, 13 % g:anint)
537 assert_equal(2, g:anint % g:alsoint)
538
539 assert_equal(4, 6 * 4 / 6)
Bram Moolenaarb13af502020-02-17 21:12:08 +0100540
541 let x = [2]
542 let y = [3]
543 assert_equal(5, x[0] + y[0])
544 assert_equal(6, x[0] * y[0])
545 if has('float')
546 let xf = [2.0]
547 let yf = [3.0]
548 assert_equal(5.0, xf[0] + yf[0])
549 assert_equal(6.0, xf[0] * yf[0])
550 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551enddef
552
553def Test_expr6_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100554 if !has('float')
555 MissingFeature 'float'
556 else
557 assert_equal(36.0, 6.0 * 6)
558 assert_equal(36.0, 6 * 6.0)
559 assert_equal(36.0, 6.0 * 6.0)
560 assert_equal(1.0, g:afloat * g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100562 assert_equal(10.0, 60 / 6.0)
563 assert_equal(10.0, 60.0 / 6)
564 assert_equal(10.0, 60.0 / 6.0)
565 assert_equal(0.01, g:afloat / g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100567 assert_equal(4.0, 6.0 * 4 / 6)
568 assert_equal(4.0, 6 * 4.0 / 6)
569 assert_equal(4.0, 6 * 4 / 6.0)
570 assert_equal(4.0, 6.0 * 4.0 / 6)
571 assert_equal(4.0, 6 * 4.0 / 6.0)
572 assert_equal(4.0, 6.0 * 4 / 6.0)
573 assert_equal(4.0, 6.0 * 4.0 / 6.0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100575 assert_equal(4.0, 6.0 * 4.0 / 6.0)
576 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577enddef
578
579func Test_expr6_fails()
580 let msg = "white space required before and after '*'"
581 call CheckDefFailure("let x = 1*2", msg)
582 call CheckDefFailure("let x = 1 *2", msg)
583 call CheckDefFailure("let x = 1* 2", msg)
584
585 let msg = "white space required before and after '/'"
586 call CheckDefFailure("let x = 1/2", msg)
587 call CheckDefFailure("let x = 1 /2", msg)
588 call CheckDefFailure("let x = 1/ 2", msg)
589
590 let msg = "white space required before and after '%'"
591 call CheckDefFailure("let x = 1%2", msg)
592 call CheckDefFailure("let x = 1 %2", msg)
593 call CheckDefFailure("let x = 1% 2", msg)
594
595 call CheckDefFailure("let x = '1' * '2'", 'E1036:')
596 call CheckDefFailure("let x = '1' / '2'", 'E1036:')
597 call CheckDefFailure("let x = '1' % '2'", 'E1035:')
598
599 call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
600 call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
601 call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
602
603 call CheckDefFailure("let x = [1] * [2]", 'E1036:')
604 call CheckDefFailure("let x = [1] / [2]", 'E1036:')
605 call CheckDefFailure("let x = [1] % [2]", 'E1035:')
606
607 call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
608 call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
609 call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
610
Bram Moolenaarb13af502020-02-17 21:12:08 +0100611 call CheckDefFailure("let x = 0xff[1]", 'E714:')
612 if has('float')
613 call CheckDefFailure("let x = 0.7[1]", 'E714:')
614 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615endfunc
616
617func Test_expr6_float_fails()
618 CheckFeature float
619 call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
620endfunc
621
622" define here to use old style parsing
623if has('float')
624 let g:float_zero = 0.0
625 let g:float_neg = -9.8
626 let g:float_big = 9.9e99
627endif
628let g:blob_empty = 0z
629let g:blob_one = 0z01
630let g:blob_long = 0z0102.0304
631
632let g:string_empty = ''
633let g:string_short = 'x'
634let g:string_long = 'abcdefghijklm'
635let g:string_special = "ab\ncd\ref\ekk"
636
637let g:special_true = v:true
638let g:special_false = v:false
639let g:special_null = v:null
640let g:special_none = v:none
641
642let g:list_empty = []
643let g:list_mixed = [1, 'b', v:false]
644
645let g:dict_empty = {}
646let g:dict_one = #{one: 1}
647
648let $TESTVAR = 'testvar'
649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650" test low level expression
651def Test_expr7_number()
652 " number constant
653 assert_equal(0, 0)
654 assert_equal(654, 0654)
655
656 assert_equal(6, 0x6)
657 assert_equal(15, 0xf)
658 assert_equal(255, 0xff)
659enddef
660
661def Test_expr7_float()
662 " float constant
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100663 if !has('float')
664 MissingFeature 'float'
665 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 assert_equal(g:float_zero, .0)
667 assert_equal(g:float_zero, 0.0)
668 assert_equal(g:float_neg, -9.8)
669 assert_equal(g:float_big, 9.9e99)
670 endif
671enddef
672
673def Test_expr7_blob()
674 " blob constant
675 assert_equal(g:blob_empty, 0z)
676 assert_equal(g:blob_one, 0z01)
677 assert_equal(g:blob_long, 0z0102.0304)
678enddef
679
680def Test_expr7_string()
681 " string constant
682 assert_equal(g:string_empty, '')
683 assert_equal(g:string_empty, "")
684 assert_equal(g:string_short, 'x')
685 assert_equal(g:string_short, "x")
686 assert_equal(g:string_long, 'abcdefghijklm')
687 assert_equal(g:string_long, "abcdefghijklm")
688 assert_equal(g:string_special, "ab\ncd\ref\ekk")
689enddef
690
691def Test_expr7_special()
692 " special constant
693 assert_equal(g:special_true, true)
694 assert_equal(g:special_false, false)
695 assert_equal(g:special_null, v:null)
696 assert_equal(g:special_none, v:none)
697enddef
698
699def Test_expr7_list()
700 " list
701 assert_equal(g:list_empty, [])
702 assert_equal(g:list_empty, [ ])
703 assert_equal(g:list_mixed, [1, 'b', false])
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100704
705 call CheckDefExecFailure("let x = g:anint[3]", 'E714:')
706 call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:')
707 call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708enddef
709
710def Test_expr7_lambda()
711 " lambda
712 let La = { -> 'result'}
713 assert_equal('result', La())
714 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
715enddef
716
717def Test_expr7_dict()
718 " dictionary
719 assert_equal(g:dict_empty, {})
720 assert_equal(g:dict_empty, { })
721 assert_equal(g:dict_one, {'one': 1})
722 let key = 'one'
723 let val = 1
724 assert_equal(g:dict_one, {key: val})
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100725
726 call CheckDefExecFailure("let x = g:anint.member", 'E715:')
727 call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728enddef
729
730def Test_expr7_option()
731 " option
732 set ts=11
733 assert_equal(11, &ts)
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100734 &ts = 9
735 assert_equal(9, &ts)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 set ts=8
737 set grepprg=some\ text
738 assert_equal('some text', &grepprg)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100739 &grepprg = test_null_string()
740 assert_equal('', &grepprg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 set grepprg&
742enddef
743
744def Test_expr7_environment()
745 " environment variable
746 assert_equal('testvar', $TESTVAR)
747 assert_equal('', $ASDF_ASD_XXX)
748enddef
749
750def Test_expr7_register()
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100751 @a = 'register a'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 assert_equal('register a', @a)
753enddef
754
755def Test_expr7_parens()
756 " (expr)
757 assert_equal(4, (6 * 4) / 6)
758 assert_equal(0, 6 * ( 4 / 6 ))
759
760 assert_equal(6, +6)
761 assert_equal(-6, -6)
762 assert_equal(6, --6)
763 assert_equal(6, -+-6)
764 assert_equal(-6, ---6)
765enddef
766
767def Test_expr7_not()
768 assert_equal(true, !'')
769 assert_equal(true, ![])
770 assert_equal(false, !'asdf')
771 assert_equal(false, ![2])
772 assert_equal(true, !!'asdf')
773 assert_equal(true, !![2])
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100774
775 assert_equal(true, !test_null_partial())
776 assert_equal(false, !{-> 'yes'})
777
778 assert_equal(true, !test_null_dict())
779 assert_equal(true, !{})
780 assert_equal(false, !{'yes': 'no'})
781
Bram Moolenaarb4d2cb12020-02-22 20:33:08 +0100782 if has('channel')
783 assert_equal(true, !test_null_job())
784 assert_equal(true, !test_null_channel())
785 endif
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100786
787 assert_equal(true, !test_null_blob())
788 assert_equal(true, !0z)
789 assert_equal(false, !0z01)
790
791 assert_equal(true, !test_void())
792 assert_equal(true, !test_unknown())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793enddef
794
795func Test_expr7_fails()
796 call CheckDefFailure("let x = (12", "E110:")
797
798 call CheckDefFailure("let x = -'xx'", "E1030:")
799 call CheckDefFailure("let x = +'xx'", "E1030:")
800
801 call CheckDefFailure("let x = @", "E1002:")
802 call CheckDefFailure("let x = @<", "E354:")
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100803
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100804 call CheckDefFailure("let x = &notexist", 'E113:')
805 call CheckDefExecFailure("&grepprg = [343]", 'E1051:')
Bram Moolenaarfd1823e2020-02-19 20:23:11 +0100806
807 call CheckDefExecFailure("echo s:doesnt_exist", 'E121:')
808 call CheckDefExecFailure("echo g:doesnt_exist", 'E121:')
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100809
810 call CheckDefExecFailure("let x = +g:astring", 'E1030:')
811 call CheckDefExecFailure("let x = +g:ablob", 'E974:')
812 call CheckDefExecFailure("let x = +g:alist", 'E745:')
813 call CheckDefExecFailure("let x = +g:adict", 'E728:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100814
815 call CheckDefFailureMult(["let x = ''", "let y = x.memb"], 'E715:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816endfunc
817
818let g:Funcrefs = [function('add')]
819
820func CallMe(arg)
821 return a:arg
822endfunc
823
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100824func CallMe2(one, two)
825 return a:one .. a:two
826endfunc
827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828def Test_expr7_trailing()
829 " user function call
830 assert_equal(123, CallMe(123))
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100831 assert_equal(123, CallMe( 123))
832 assert_equal(123, CallMe(123 ))
833 assert_equal('yesno', CallMe2('yes', 'no'))
834 assert_equal('yesno', CallMe2( 'yes', 'no' ))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 assert_equal('nothing', CallMe('nothing'))
836
837 " partial call
838 let Part = function('CallMe')
839 assert_equal('yes', Part('yes'))
840
841 " funcref call, using list index
842 let l = []
843 g:Funcrefs[0](l, 2)
844 assert_equal([2], l)
845
846 " method call
847 l = [2, 5, 6]
848 l->map({k, v -> k + v})
849 assert_equal([2, 6, 8], l)
850
851 " lambda method call
852 l = [2, 5]
853 l->{l -> add(l, 8)}()
854 assert_equal([2, 5, 8], l)
855
856 " dict member
857 let d = #{key: 123}
858 assert_equal(123, d.key)
859enddef
860
861func Test_expr7_trailing_fails()
862 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
863endfunc
864
865func Test_expr_fails()
866 call CheckDefFailure("let x = '1'is2", 'E488:')
867 call CheckDefFailure("let x = '1'isnot2", 'E488:')
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100868
869 call CheckDefExecFailure("CallMe ('yes')", 'E492:')
870 call CheckDefFailure("CallMe2('yes','no')", 'E1069:')
871 call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:')
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100872
873 call CheckDefFailure("v:nosuch += 3", 'E1001:')
874 call CheckDefFailure("let v:version = 3", 'E1064:')
875 call CheckDefFailure("let asdf = v:nosuch", 'E1001:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876endfunc