blob: fb7d39f41e8c68be6ec28469983fb8f0f8fcaf04 [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 Moolenaar38a5f512020-02-19 12:40:39 +010012" Check that "line" inside ":def" results in an "error" message when executed.
13func CheckDefExecFailure(line, error)
14 call writefile(['def! Func()', a:line, 'enddef'], 'Xdef')
15 so Xdef
16 call assert_fails('call Func()', a:error, a:line)
17 call delete('Xdef')
18endfunc
19
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010020func CheckDefFailureList(lines, error)
21 call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef')
22 call assert_fails('so Xdef', a:error, string(a:lines))
23 call delete('Xdef')
24endfunc
25
26" test cond ? expr : expr
27def Test_expr1()
28 assert_equal('one', true ? 'one' : 'two')
29 assert_equal('one', 1 ? 'one' : 'two')
Bram Moolenaar5feabe02020-01-30 18:24:53 +010030 if has('float')
31 assert_equal('one', 0.1 ? 'one' : 'two')
32 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033 assert_equal('one', 'x' ? 'one' : 'two')
34" assert_equal('one', 0z1234 ? 'one' : 'two')
35 assert_equal('one', [0] ? 'one' : 'two')
36" assert_equal('one', #{x: 0} ? 'one' : 'two')
37 let var = 1
38 assert_equal('one', var ? 'one' : 'two')
39
40 assert_equal('two', false ? 'one' : 'two')
41 assert_equal('two', 0 ? 'one' : 'two')
Bram Moolenaar7f829ca2020-01-31 22:12:41 +010042 if has('float')
43 assert_equal('two', 0.0 ? 'one' : 'two')
44 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045 assert_equal('two', '' ? 'one' : 'two')
46" assert_equal('one', 0z ? 'one' : 'two')
47 assert_equal('two', [] ? 'one' : 'two')
48" assert_equal('two', {} ? 'one' : 'two')
49 var = 0
50 assert_equal('two', var ? 'one' : 'two')
51enddef
52
53func Test_expr1_fails()
54 call CheckDefFailure("let x = 1 ? 'one'", "Missing ':' after '?'")
55
56 let msg = "white space required before and after '?'"
57 call CheckDefFailure("let x = 1? 'one' : 'two'", msg)
58 call CheckDefFailure("let x = 1 ?'one' : 'two'", msg)
59 call CheckDefFailure("let x = 1?'one' : 'two'", msg)
60
61 let msg = "white space required before and after ':'"
62 call CheckDefFailure("let x = 1 ? 'one': 'two'", msg)
63 call CheckDefFailure("let x = 1 ? 'one' :'two'", msg)
64 call CheckDefFailure("let x = 1 ? 'one':'two'", msg)
65endfunc
66
67" TODO: define inside test function
68def Record(val: any): any
69 g:vals->add(val)
70 return val
71enddef
72
73" test ||
74def Test_expr2()
75 assert_equal(2, 2 || 0)
76 assert_equal(7, 0 || 0 || 7)
77 assert_equal(0, 0 || 0)
78 assert_equal('', 0 || '')
79
80 g:vals = []
81 assert_equal(3, Record(3) || Record(1))
82 assert_equal([3], g:vals)
83
84 g:vals = []
85 assert_equal(5, Record(0) || Record(5))
86 assert_equal([0, 5], g:vals)
87
88 g:vals = []
89 assert_equal(4, Record(0) || Record(4) || Record(0))
90 assert_equal([0, 4], g:vals)
91
92 g:vals = []
93 assert_equal(0, Record([]) || Record('') || Record(0))
94 assert_equal([[], '', 0], g:vals)
95enddef
96
97func Test_expr2_fails()
98 let msg = "white space required before and after '||'"
99 call CheckDefFailure("let x = 1||2", msg)
100 call CheckDefFailure("let x = 1 ||2", msg)
101 call CheckDefFailure("let x = 1|| 2", msg)
102endfunc
103
104" test &&
105def Test_expr3()
106 assert_equal(0, 2 && 0)
107 assert_equal(0, 0 && 0 && 7)
108 assert_equal(7, 2 && 3 && 7)
109 assert_equal(0, 0 && 0)
110 assert_equal(0, 0 && '')
111 assert_equal('', 8 && '')
112
113 g:vals = []
114 assert_equal(1, Record(3) && Record(1))
115 assert_equal([3, 1], g:vals)
116
117 g:vals = []
118 assert_equal(0, Record(0) && Record(5))
119 assert_equal([0], g:vals)
120
121 g:vals = []
122 assert_equal(0, Record(0) && Record(4) && Record(0))
123 assert_equal([0], g:vals)
124
125 g:vals = []
126 assert_equal(0, Record(8) && Record(4) && Record(0))
127 assert_equal([8, 4, 0], g:vals)
128
129 g:vals = []
130 assert_equal(0, Record([1]) && Record('z') && Record(0))
131 assert_equal([[1], 'z', 0], g:vals)
132enddef
133
134func Test_expr3_fails()
135 let msg = "white space required before and after '&&'"
136 call CheckDefFailure("let x = 1&&2", msg)
137 call CheckDefFailure("let x = 1 &&2", msg)
138 call CheckDefFailure("let x = 1&& 2", msg)
139endfunc
140
141let atrue = v:true
142let afalse = v:false
143let anone = v:none
144let anull = v:null
145let anint = 10
146let alsoint = 4
147if has('float')
148 let afloat = 0.1
149endif
150let astring = 'asdf'
151let ablob = 0z01ab
152let alist = [2, 3, 4]
153let adict = #{aaa: 2, bbb: 8}
154
155" test == comperator
156def Test_expr4_equal()
157 assert_equal(true, true == true)
158 assert_equal(false, true == false)
159 assert_equal(true, true == g:atrue)
160 assert_equal(false, g:atrue == false)
161
162 assert_equal(true, v:none == v:none)
163 assert_equal(false, v:none == v:null)
164 assert_equal(true, g:anone == v:none)
165 assert_equal(false, v:none == g:anull)
166
167 assert_equal(false, 2 == 0)
168 assert_equal(true, 61 == 61)
169 assert_equal(true, g:anint == 10)
170 assert_equal(false, 61 == g:anint)
171
172 if has('float')
173 assert_equal(true, 0.3 == 0.3)
174 assert_equal(false, 0.4 == 0.3)
175 assert_equal(true, 0.1 == g:afloat)
176 assert_equal(false, g:afloat == 0.3)
177
178 assert_equal(true, 3.0 == 3)
179 assert_equal(true, 3 == 3.0)
180 assert_equal(false, 3.1 == 3)
181 assert_equal(false, 3 == 3.1)
182 endif
183
184 assert_equal(true, 'abc' == 'abc')
185 assert_equal(false, 'xyz' == 'abc')
186 assert_equal(true, g:astring == 'asdf')
187 assert_equal(false, 'xyz' == g:astring)
188
189 assert_equal(false, 'abc' == 'ABC')
190 set ignorecase
191 assert_equal(false, 'abc' == 'ABC')
192 set noignorecase
193
194 assert_equal(true, 0z3f == 0z3f)
195 assert_equal(false, 0z3f == 0z4f)
196 assert_equal(true, g:ablob == 0z01ab)
197 assert_equal(false, 0z3f == g:ablob)
198
199 assert_equal(true, [1, 2, 3] == [1, 2, 3])
200 assert_equal(false, [1, 2, 3] == [2, 3, 1])
201 assert_equal(true, [2, 3, 4] == g:alist)
202 assert_equal(false, g:alist == [2, 3, 1])
203 assert_equal(false, [1, 2, 3] == [])
204 assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
205
206 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
207 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
208 assert_equal(false, #{one: 1, two: 2} == #{two: 2})
209 assert_equal(false, #{one: 1, two: 2} == #{})
210 assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
211 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
212
213 assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal'))
214 assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is'))
215
216 assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123]))
217 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123]))
218 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999]))
219enddef
220
221" test != comperator
222def Test_expr4_notequal()
223 assert_equal(false, true != true)
224 assert_equal(true, true != false)
225 assert_equal(false, true != g:atrue)
226 assert_equal(true, g:atrue != false)
227
228 assert_equal(false, v:none != v:none)
229 assert_equal(true, v:none != v:null)
230 assert_equal(false, g:anone != v:none)
231 assert_equal(true, v:none != g:anull)
232
233 assert_equal(true, 2 != 0)
234 assert_equal(false, 55 != 55)
235 assert_equal(false, g:anint != 10)
236 assert_equal(true, 61 != g:anint)
237
238 if has('float')
239 assert_equal(false, 0.3 != 0.3)
240 assert_equal(true, 0.4 != 0.3)
241 assert_equal(false, 0.1 != g:afloat)
242 assert_equal(true, g:afloat != 0.3)
243
244 assert_equal(false, 3.0 != 3)
245 assert_equal(false, 3 != 3.0)
246 assert_equal(true, 3.1 != 3)
247 assert_equal(true, 3 != 3.1)
248 endif
249
250 assert_equal(false, 'abc' != 'abc')
251 assert_equal(true, 'xyz' != 'abc')
252 assert_equal(false, g:astring != 'asdf')
253 assert_equal(true, 'xyz' != g:astring)
254
255 assert_equal(true, 'abc' != 'ABC')
256 set ignorecase
257 assert_equal(true, 'abc' != 'ABC')
258 set noignorecase
259
260 assert_equal(false, 0z3f != 0z3f)
261 assert_equal(true, 0z3f != 0z4f)
262 assert_equal(false, g:ablob != 0z01ab)
263 assert_equal(true, 0z3f != g:ablob)
264
265 assert_equal(false, [1, 2, 3] != [1, 2, 3])
266 assert_equal(true, [1, 2, 3] != [2, 3, 1])
267 assert_equal(false, [2, 3, 4] != g:alist)
268 assert_equal(true, g:alist != [2, 3, 1])
269 assert_equal(true, [1, 2, 3] != [])
270 assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
271
272 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
273 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
274 assert_equal(true, #{one: 1, two: 2} != #{two: 2})
275 assert_equal(true, #{one: 1, two: 2} != #{})
276 assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
277 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
278
279 assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal'))
280 assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is'))
281
282 assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123]))
283 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123]))
284 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999]))
285enddef
286
287" test > comperator
288def Test_expr4_greater()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100289 assert_true(2 > 0)
290 assert_true(2 > 1)
291 assert_false(2 > 2)
292 assert_false(2 > 3)
293 if has('float')
294 assert_true(2.0 > 0.0)
295 assert_true(2.0 > 1.0)
296 assert_false(2.0 > 2.0)
297 assert_false(2.0 > 3.0)
298 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299enddef
300
301" test >= comperator
302def Test_expr4_greaterequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100303 assert_true(2 >= 0)
304 assert_true(2 >= 2)
305 assert_false(2 >= 3)
306 if has('float')
307 assert_true(2.0 >= 0.0)
308 assert_true(2.0 >= 2.0)
309 assert_false(2.0 >= 3.0)
310 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311enddef
312
313" test < comperator
314def Test_expr4_smaller()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100315 assert_false(2 < 0)
316 assert_false(2 < 2)
317 assert_true(2 < 3)
318 if has('float')
319 assert_false(2.0 < 0.0)
320 assert_false(2.0 < 2.0)
321 assert_true(2.0 < 3.0)
322 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323enddef
324
325" test <= comperator
326def Test_expr4_smallerequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100327 assert_false(2 <= 0)
328 assert_false(2 <= 1)
329 assert_true(2 <= 2)
330 assert_true(2 <= 3)
331 if has('float')
332 assert_false(2.0 <= 0.0)
333 assert_false(2.0 <= 1.0)
334 assert_true(2.0 <= 2.0)
335 assert_true(2.0 <= 3.0)
336 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337enddef
338
339" test =~ comperator
340def Test_expr4_match()
341 assert_equal(false, '2' =~ '0')
342 assert_equal(true, '2' =~ '[0-9]')
343enddef
344
345" test !~ comperator
346def Test_expr4_nomatch()
347 assert_equal(true, '2' !~ '0')
348 assert_equal(false, '2' !~ '[0-9]')
349enddef
350
351" test is comperator
352def Test_expr4_is()
353 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100354 assert_false(mylist is [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100356 assert_true(mylist is other)
357
358 let myblob = 0z1234
359 assert_false(myblob is 0z1234)
360 let otherblob = myblob
361 assert_true(myblob is otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362enddef
363
364" test isnot comperator
365def Test_expr4_isnot()
366 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100367 assert_true('2' isnot '0')
368 assert_true(mylist isnot [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100370 assert_false(mylist isnot other)
371
372 let myblob = 0z1234
373 assert_true(myblob isnot 0z1234)
374 let otherblob = myblob
375 assert_false(myblob isnot otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376enddef
377
378def RetVoid()
379 let x = 1
380enddef
381
382func Test_expr4_fails()
383 let msg = "white space required before and after '>'"
384 call CheckDefFailure("let x = 1>2", msg)
385 call CheckDefFailure("let x = 1 >2", msg)
386 call CheckDefFailure("let x = 1> 2", msg)
387
388 let msg = "white space required before and after '=='"
389 call CheckDefFailure("let x = 1==2", msg)
390 call CheckDefFailure("let x = 1 ==2", msg)
391 call CheckDefFailure("let x = 1== 2", msg)
392
393 let msg = "white space required before and after 'is'"
394 call CheckDefFailure("let x = '1'is'2'", msg)
395 call CheckDefFailure("let x = '1' is'2'", msg)
396 call CheckDefFailure("let x = '1'is '2'", msg)
397
398 let msg = "white space required before and after 'isnot'"
399 call CheckDefFailure("let x = '1'isnot'2'", msg)
400 call CheckDefFailure("let x = '1' isnot'2'", msg)
401 call CheckDefFailure("let x = '1'isnot '2'", msg)
402
403 call CheckDefFailure("let x = 1 is# 2", 'E15:')
404 call CheckDefFailure("let x = 1 is? 2", 'E15:')
405 call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
406 call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
407
408 call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
409 call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
410 call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
411 call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
412
413 call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool')
414 call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool')
415 call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool')
416 call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool')
417 call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool')
418 call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
419 call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
420 call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
421
422 call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
423 call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
424 call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
425 call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
426 if has('float')
427 call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
428 call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
429 endif
430
431 call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob')
432 call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob')
433 call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob')
434 call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob')
435 call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
436 call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
437
438 call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list')
439 call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list')
440 call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list')
441 call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list')
442 call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
443 call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
444endfunc
445
446" test addition, subtraction, concatenation
447def Test_expr5()
448 assert_equal(66, 60 + 6)
449 assert_equal(70, 60 + g:anint)
450 assert_equal(9, g:alsoint + 5)
451 assert_equal(14, g:alsoint + g:anint)
452
453 assert_equal(54, 60 - 6)
454 assert_equal(50, 60 - g:anint)
455 assert_equal(-1, g:alsoint - 5)
456 assert_equal(-6, g:alsoint - g:anint)
457
458 assert_equal('hello', 'hel' .. 'lo')
459 assert_equal('hello 123', 'hello ' .. 123)
460 assert_equal('123 hello', 123 .. ' hello')
461 assert_equal('123456', 123 .. 456)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100462
463 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
464 assert_equal(0z11223344, 0z1122 + 0z3344)
465 assert_equal(0z112201ab, 0z1122 + g:ablob)
466 assert_equal(0z01ab3344, g:ablob + 0z3344)
467 assert_equal(0z01ab01ab, g:ablob + g:ablob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468enddef
469
470def Test_expr5_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100471 if !has('float')
472 MissingFeature 'float'
473 else
474 assert_equal(66.0, 60.0 + 6.0)
475 assert_equal(66.0, 60.0 + 6)
476 assert_equal(66.0, 60 + 6.0)
477 assert_equal(5.1, g:afloat + 5)
478 assert_equal(8.1, 8 + g:afloat)
479 assert_equal(10.1, g:anint + g:afloat)
480 assert_equal(10.1, g:afloat + g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100482 assert_equal(54.0, 60.0 - 6.0)
483 assert_equal(54.0, 60.0 - 6)
484 assert_equal(54.0, 60 - 6.0)
485 assert_equal(-4.9, g:afloat - 5)
486 assert_equal(7.9, 8 - g:afloat)
487 assert_equal(9.9, g:anint - g:afloat)
488 assert_equal(-9.9, g:afloat - g:anint)
489 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100490enddef
491
492func Test_expr5_fails()
493 let msg = "white space required before and after '+'"
494 call CheckDefFailure("let x = 1+2", msg)
495 call CheckDefFailure("let x = 1 +2", msg)
496 call CheckDefFailure("let x = 1+ 2", msg)
497
498 let msg = "white space required before and after '-'"
499 call CheckDefFailure("let x = 1-2", msg)
500 call CheckDefFailure("let x = 1 -2", msg)
501 call CheckDefFailure("let x = 1- 2", msg)
502
503 let msg = "white space required before and after '..'"
504 call CheckDefFailure("let x = '1'..'2'", msg)
505 call CheckDefFailure("let x = '1' ..'2'", msg)
506 call CheckDefFailure("let x = '1'.. '2'", msg)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100507
508 call CheckDefFailure("let x = 0z1122 + 33", 'E1035')
509 call CheckDefFailure("let x = 0z1122 + [3]", 'E1035')
510 call CheckDefFailure("let x = 0z1122 + 'asd'", 'E1035')
511 call CheckDefFailure("let x = 33 + 0z1122", 'E1035')
512 call CheckDefFailure("let x = [3] + 0z1122", 'E1035')
513 call CheckDefFailure("let x = 'asdf' + 0z1122", 'E1035')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514endfunc
515
516" test multiply, divide, modulo
517def Test_expr6()
518 assert_equal(36, 6 * 6)
519 assert_equal(24, 6 * g:alsoint)
520 assert_equal(24, g:alsoint * 6)
521 assert_equal(40, g:anint * g:alsoint)
522
523 assert_equal(10, 60 / 6)
524 assert_equal(6, 60 / g:anint)
525 assert_equal(1, g:anint / 6)
526 assert_equal(2, g:anint / g:alsoint)
527
528 assert_equal(5, 11 % 6)
529 assert_equal(4, g:anint % 6)
530 assert_equal(3, 13 % g:anint)
531 assert_equal(2, g:anint % g:alsoint)
532
533 assert_equal(4, 6 * 4 / 6)
Bram Moolenaarb13af502020-02-17 21:12:08 +0100534
535 let x = [2]
536 let y = [3]
537 assert_equal(5, x[0] + y[0])
538 assert_equal(6, x[0] * y[0])
539 if has('float')
540 let xf = [2.0]
541 let yf = [3.0]
542 assert_equal(5.0, xf[0] + yf[0])
543 assert_equal(6.0, xf[0] * yf[0])
544 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545enddef
546
547def Test_expr6_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100548 if !has('float')
549 MissingFeature 'float'
550 else
551 assert_equal(36.0, 6.0 * 6)
552 assert_equal(36.0, 6 * 6.0)
553 assert_equal(36.0, 6.0 * 6.0)
554 assert_equal(1.0, g:afloat * g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100556 assert_equal(10.0, 60 / 6.0)
557 assert_equal(10.0, 60.0 / 6)
558 assert_equal(10.0, 60.0 / 6.0)
559 assert_equal(0.01, g:afloat / g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100561 assert_equal(4.0, 6.0 * 4 / 6)
562 assert_equal(4.0, 6 * 4.0 / 6)
563 assert_equal(4.0, 6 * 4 / 6.0)
564 assert_equal(4.0, 6.0 * 4.0 / 6)
565 assert_equal(4.0, 6 * 4.0 / 6.0)
566 assert_equal(4.0, 6.0 * 4 / 6.0)
567 assert_equal(4.0, 6.0 * 4.0 / 6.0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100569 assert_equal(4.0, 6.0 * 4.0 / 6.0)
570 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571enddef
572
573func Test_expr6_fails()
574 let msg = "white space required before and after '*'"
575 call CheckDefFailure("let x = 1*2", msg)
576 call CheckDefFailure("let x = 1 *2", msg)
577 call CheckDefFailure("let x = 1* 2", msg)
578
579 let msg = "white space required before and after '/'"
580 call CheckDefFailure("let x = 1/2", msg)
581 call CheckDefFailure("let x = 1 /2", msg)
582 call CheckDefFailure("let x = 1/ 2", msg)
583
584 let msg = "white space required before and after '%'"
585 call CheckDefFailure("let x = 1%2", msg)
586 call CheckDefFailure("let x = 1 %2", msg)
587 call CheckDefFailure("let x = 1% 2", msg)
588
589 call CheckDefFailure("let x = '1' * '2'", 'E1036:')
590 call CheckDefFailure("let x = '1' / '2'", 'E1036:')
591 call CheckDefFailure("let x = '1' % '2'", 'E1035:')
592
593 call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
594 call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
595 call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
596
597 call CheckDefFailure("let x = [1] * [2]", 'E1036:')
598 call CheckDefFailure("let x = [1] / [2]", 'E1036:')
599 call CheckDefFailure("let x = [1] % [2]", 'E1035:')
600
601 call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
602 call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
603 call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
604
Bram Moolenaarb13af502020-02-17 21:12:08 +0100605 call CheckDefFailure("let x = 0xff[1]", 'E714:')
606 if has('float')
607 call CheckDefFailure("let x = 0.7[1]", 'E714:')
608 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609endfunc
610
611func Test_expr6_float_fails()
612 CheckFeature float
613 call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
614endfunc
615
616" define here to use old style parsing
617if has('float')
618 let g:float_zero = 0.0
619 let g:float_neg = -9.8
620 let g:float_big = 9.9e99
621endif
622let g:blob_empty = 0z
623let g:blob_one = 0z01
624let g:blob_long = 0z0102.0304
625
626let g:string_empty = ''
627let g:string_short = 'x'
628let g:string_long = 'abcdefghijklm'
629let g:string_special = "ab\ncd\ref\ekk"
630
631let g:special_true = v:true
632let g:special_false = v:false
633let g:special_null = v:null
634let g:special_none = v:none
635
636let g:list_empty = []
637let g:list_mixed = [1, 'b', v:false]
638
639let g:dict_empty = {}
640let g:dict_one = #{one: 1}
641
642let $TESTVAR = 'testvar'
643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644" test low level expression
645def Test_expr7_number()
646 " number constant
647 assert_equal(0, 0)
648 assert_equal(654, 0654)
649
650 assert_equal(6, 0x6)
651 assert_equal(15, 0xf)
652 assert_equal(255, 0xff)
653enddef
654
655def Test_expr7_float()
656 " float constant
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100657 if !has('float')
658 MissingFeature 'float'
659 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660 assert_equal(g:float_zero, .0)
661 assert_equal(g:float_zero, 0.0)
662 assert_equal(g:float_neg, -9.8)
663 assert_equal(g:float_big, 9.9e99)
664 endif
665enddef
666
667def Test_expr7_blob()
668 " blob constant
669 assert_equal(g:blob_empty, 0z)
670 assert_equal(g:blob_one, 0z01)
671 assert_equal(g:blob_long, 0z0102.0304)
672enddef
673
674def Test_expr7_string()
675 " string constant
676 assert_equal(g:string_empty, '')
677 assert_equal(g:string_empty, "")
678 assert_equal(g:string_short, 'x')
679 assert_equal(g:string_short, "x")
680 assert_equal(g:string_long, 'abcdefghijklm')
681 assert_equal(g:string_long, "abcdefghijklm")
682 assert_equal(g:string_special, "ab\ncd\ref\ekk")
683enddef
684
685def Test_expr7_special()
686 " special constant
687 assert_equal(g:special_true, true)
688 assert_equal(g:special_false, false)
689 assert_equal(g:special_null, v:null)
690 assert_equal(g:special_none, v:none)
691enddef
692
693def Test_expr7_list()
694 " list
695 assert_equal(g:list_empty, [])
696 assert_equal(g:list_empty, [ ])
697 assert_equal(g:list_mixed, [1, 'b', false])
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100698
699 call CheckDefExecFailure("let x = g:anint[3]", 'E714:')
700 call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:')
701 call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702enddef
703
704def Test_expr7_lambda()
705 " lambda
706 let La = { -> 'result'}
707 assert_equal('result', La())
708 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
709enddef
710
711def Test_expr7_dict()
712 " dictionary
713 assert_equal(g:dict_empty, {})
714 assert_equal(g:dict_empty, { })
715 assert_equal(g:dict_one, {'one': 1})
716 let key = 'one'
717 let val = 1
718 assert_equal(g:dict_one, {key: val})
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100719
720 call CheckDefExecFailure("let x = g:anint.member", 'E715:')
721 call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722enddef
723
724def Test_expr7_option()
725 " option
726 set ts=11
727 assert_equal(11, &ts)
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100728 &ts = 9
729 assert_equal(9, &ts)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 set ts=8
731 set grepprg=some\ text
732 assert_equal('some text', &grepprg)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100733 &grepprg = test_null_string()
734 assert_equal('', &grepprg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 set grepprg&
736enddef
737
738def Test_expr7_environment()
739 " environment variable
740 assert_equal('testvar', $TESTVAR)
741 assert_equal('', $ASDF_ASD_XXX)
742enddef
743
744def Test_expr7_register()
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100745 @a = 'register a'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 assert_equal('register a', @a)
747enddef
748
749def Test_expr7_parens()
750 " (expr)
751 assert_equal(4, (6 * 4) / 6)
752 assert_equal(0, 6 * ( 4 / 6 ))
753
754 assert_equal(6, +6)
755 assert_equal(-6, -6)
756 assert_equal(6, --6)
757 assert_equal(6, -+-6)
758 assert_equal(-6, ---6)
759enddef
760
761def Test_expr7_not()
762 assert_equal(true, !'')
763 assert_equal(true, ![])
764 assert_equal(false, !'asdf')
765 assert_equal(false, ![2])
766 assert_equal(true, !!'asdf')
767 assert_equal(true, !![2])
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100768
769 assert_equal(true, !test_null_partial())
770 assert_equal(false, !{-> 'yes'})
771
772 assert_equal(true, !test_null_dict())
773 assert_equal(true, !{})
774 assert_equal(false, !{'yes': 'no'})
775
Bram Moolenaarb4d2cb12020-02-22 20:33:08 +0100776 if has('channel')
777 assert_equal(true, !test_null_job())
778 assert_equal(true, !test_null_channel())
779 endif
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100780
781 assert_equal(true, !test_null_blob())
782 assert_equal(true, !0z)
783 assert_equal(false, !0z01)
784
785 assert_equal(true, !test_void())
786 assert_equal(true, !test_unknown())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787enddef
788
789func Test_expr7_fails()
790 call CheckDefFailure("let x = (12", "E110:")
791
792 call CheckDefFailure("let x = -'xx'", "E1030:")
793 call CheckDefFailure("let x = +'xx'", "E1030:")
794
795 call CheckDefFailure("let x = @", "E1002:")
796 call CheckDefFailure("let x = @<", "E354:")
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100797
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100798 call CheckDefFailure("let x = &notexist", 'E113:')
799 call CheckDefExecFailure("&grepprg = [343]", 'E1051:')
Bram Moolenaarfd1823e2020-02-19 20:23:11 +0100800
801 call CheckDefExecFailure("echo s:doesnt_exist", 'E121:')
802 call CheckDefExecFailure("echo g:doesnt_exist", 'E121:')
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100803
804 call CheckDefExecFailure("let x = +g:astring", 'E1030:')
805 call CheckDefExecFailure("let x = +g:ablob", 'E974:')
806 call CheckDefExecFailure("let x = +g:alist", 'E745:')
807 call CheckDefExecFailure("let x = +g:adict", 'E728:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808endfunc
809
810let g:Funcrefs = [function('add')]
811
812func CallMe(arg)
813 return a:arg
814endfunc
815
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100816func CallMe2(one, two)
817 return a:one .. a:two
818endfunc
819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820def Test_expr7_trailing()
821 " user function call
822 assert_equal(123, CallMe(123))
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100823 assert_equal(123, CallMe( 123))
824 assert_equal(123, CallMe(123 ))
825 assert_equal('yesno', CallMe2('yes', 'no'))
826 assert_equal('yesno', CallMe2( 'yes', 'no' ))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 assert_equal('nothing', CallMe('nothing'))
828
829 " partial call
830 let Part = function('CallMe')
831 assert_equal('yes', Part('yes'))
832
833 " funcref call, using list index
834 let l = []
835 g:Funcrefs[0](l, 2)
836 assert_equal([2], l)
837
838 " method call
839 l = [2, 5, 6]
840 l->map({k, v -> k + v})
841 assert_equal([2, 6, 8], l)
842
843 " lambda method call
844 l = [2, 5]
845 l->{l -> add(l, 8)}()
846 assert_equal([2, 5, 8], l)
847
848 " dict member
849 let d = #{key: 123}
850 assert_equal(123, d.key)
851enddef
852
853func Test_expr7_trailing_fails()
854 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
855endfunc
856
857func Test_expr_fails()
858 call CheckDefFailure("let x = '1'is2", 'E488:')
859 call CheckDefFailure("let x = '1'isnot2", 'E488:')
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100860
861 call CheckDefExecFailure("CallMe ('yes')", 'E492:')
862 call CheckDefFailure("CallMe2('yes','no')", 'E1069:')
863 call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864endfunc