blob: 1f3e4176105d68f5bb202c8ae057e22b8189187d [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()
289 assert_equal(true, 2 > 0)
290 assert_equal(true, 2 > 1)
291 assert_equal(false, 2 > 2)
292 assert_equal(false, 2 > 3)
293enddef
294
295" test >= comperator
296def Test_expr4_greaterequal()
297 assert_equal(true, 2 >= 0)
298 assert_equal(true, 2 >= 2)
299 assert_equal(false, 2 >= 3)
300enddef
301
302" test < comperator
303def Test_expr4_smaller()
304 assert_equal(false, 2 < 0)
305 assert_equal(false, 2 < 2)
306 assert_equal(true, 2 < 3)
307enddef
308
309" test <= comperator
310def Test_expr4_smallerequal()
311 assert_equal(false, 2 <= 0)
312 assert_equal(false, 2 <= 1)
313 assert_equal(true, 2 <= 2)
314 assert_equal(true, 2 <= 3)
315enddef
316
317" test =~ comperator
318def Test_expr4_match()
319 assert_equal(false, '2' =~ '0')
320 assert_equal(true, '2' =~ '[0-9]')
321enddef
322
323" test !~ comperator
324def Test_expr4_nomatch()
325 assert_equal(true, '2' !~ '0')
326 assert_equal(false, '2' !~ '[0-9]')
327enddef
328
329" test is comperator
330def Test_expr4_is()
331 let mylist = [2]
332 assert_equal(false, mylist is [2])
333 let other = mylist
334 assert_equal(true, mylist is other)
335enddef
336
337" test isnot comperator
338def Test_expr4_isnot()
339 let mylist = [2]
340 assert_equal(true, '2' isnot '0')
341 assert_equal(true, mylist isnot [2])
342 let other = mylist
343 assert_equal(false, mylist isnot other)
344enddef
345
346def RetVoid()
347 let x = 1
348enddef
349
350func Test_expr4_fails()
351 let msg = "white space required before and after '>'"
352 call CheckDefFailure("let x = 1>2", msg)
353 call CheckDefFailure("let x = 1 >2", msg)
354 call CheckDefFailure("let x = 1> 2", msg)
355
356 let msg = "white space required before and after '=='"
357 call CheckDefFailure("let x = 1==2", msg)
358 call CheckDefFailure("let x = 1 ==2", msg)
359 call CheckDefFailure("let x = 1== 2", msg)
360
361 let msg = "white space required before and after 'is'"
362 call CheckDefFailure("let x = '1'is'2'", msg)
363 call CheckDefFailure("let x = '1' is'2'", msg)
364 call CheckDefFailure("let x = '1'is '2'", msg)
365
366 let msg = "white space required before and after 'isnot'"
367 call CheckDefFailure("let x = '1'isnot'2'", msg)
368 call CheckDefFailure("let x = '1' isnot'2'", msg)
369 call CheckDefFailure("let x = '1'isnot '2'", msg)
370
371 call CheckDefFailure("let x = 1 is# 2", 'E15:')
372 call CheckDefFailure("let x = 1 is? 2", 'E15:')
373 call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
374 call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
375
376 call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
377 call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
378 call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
379 call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
380
381 call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool')
382 call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool')
383 call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool')
384 call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool')
385 call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool')
386 call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
387 call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
388 call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
389
390 call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
391 call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
392 call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
393 call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
394 if has('float')
395 call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
396 call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
397 endif
398
399 call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob')
400 call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob')
401 call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob')
402 call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob')
403 call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
404 call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
405
406 call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list')
407 call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list')
408 call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list')
409 call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list')
410 call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
411 call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
412endfunc
413
414" test addition, subtraction, concatenation
415def Test_expr5()
416 assert_equal(66, 60 + 6)
417 assert_equal(70, 60 + g:anint)
418 assert_equal(9, g:alsoint + 5)
419 assert_equal(14, g:alsoint + g:anint)
420
421 assert_equal(54, 60 - 6)
422 assert_equal(50, 60 - g:anint)
423 assert_equal(-1, g:alsoint - 5)
424 assert_equal(-6, g:alsoint - g:anint)
425
426 assert_equal('hello', 'hel' .. 'lo')
427 assert_equal('hello 123', 'hello ' .. 123)
428 assert_equal('123 hello', 123 .. ' hello')
429 assert_equal('123456', 123 .. 456)
430enddef
431
432def Test_expr5_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100433 if !has('float')
434 MissingFeature 'float'
435 else
436 assert_equal(66.0, 60.0 + 6.0)
437 assert_equal(66.0, 60.0 + 6)
438 assert_equal(66.0, 60 + 6.0)
439 assert_equal(5.1, g:afloat + 5)
440 assert_equal(8.1, 8 + g:afloat)
441 assert_equal(10.1, g:anint + g:afloat)
442 assert_equal(10.1, g:afloat + g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100444 assert_equal(54.0, 60.0 - 6.0)
445 assert_equal(54.0, 60.0 - 6)
446 assert_equal(54.0, 60 - 6.0)
447 assert_equal(-4.9, g:afloat - 5)
448 assert_equal(7.9, 8 - g:afloat)
449 assert_equal(9.9, g:anint - g:afloat)
450 assert_equal(-9.9, g:afloat - g:anint)
451 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452enddef
453
454func Test_expr5_fails()
455 let msg = "white space required before and after '+'"
456 call CheckDefFailure("let x = 1+2", msg)
457 call CheckDefFailure("let x = 1 +2", msg)
458 call CheckDefFailure("let x = 1+ 2", msg)
459
460 let msg = "white space required before and after '-'"
461 call CheckDefFailure("let x = 1-2", msg)
462 call CheckDefFailure("let x = 1 -2", msg)
463 call CheckDefFailure("let x = 1- 2", msg)
464
465 let msg = "white space required before and after '..'"
466 call CheckDefFailure("let x = '1'..'2'", msg)
467 call CheckDefFailure("let x = '1' ..'2'", msg)
468 call CheckDefFailure("let x = '1'.. '2'", msg)
469endfunc
470
471" test multiply, divide, modulo
472def Test_expr6()
473 assert_equal(36, 6 * 6)
474 assert_equal(24, 6 * g:alsoint)
475 assert_equal(24, g:alsoint * 6)
476 assert_equal(40, g:anint * g:alsoint)
477
478 assert_equal(10, 60 / 6)
479 assert_equal(6, 60 / g:anint)
480 assert_equal(1, g:anint / 6)
481 assert_equal(2, g:anint / g:alsoint)
482
483 assert_equal(5, 11 % 6)
484 assert_equal(4, g:anint % 6)
485 assert_equal(3, 13 % g:anint)
486 assert_equal(2, g:anint % g:alsoint)
487
488 assert_equal(4, 6 * 4 / 6)
Bram Moolenaarb13af502020-02-17 21:12:08 +0100489
490 let x = [2]
491 let y = [3]
492 assert_equal(5, x[0] + y[0])
493 assert_equal(6, x[0] * y[0])
494 if has('float')
495 let xf = [2.0]
496 let yf = [3.0]
497 assert_equal(5.0, xf[0] + yf[0])
498 assert_equal(6.0, xf[0] * yf[0])
499 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500enddef
501
502def Test_expr6_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100503 if !has('float')
504 MissingFeature 'float'
505 else
506 assert_equal(36.0, 6.0 * 6)
507 assert_equal(36.0, 6 * 6.0)
508 assert_equal(36.0, 6.0 * 6.0)
509 assert_equal(1.0, g:afloat * g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100511 assert_equal(10.0, 60 / 6.0)
512 assert_equal(10.0, 60.0 / 6)
513 assert_equal(10.0, 60.0 / 6.0)
514 assert_equal(0.01, g:afloat / g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100516 assert_equal(4.0, 6.0 * 4 / 6)
517 assert_equal(4.0, 6 * 4.0 / 6)
518 assert_equal(4.0, 6 * 4 / 6.0)
519 assert_equal(4.0, 6.0 * 4.0 / 6)
520 assert_equal(4.0, 6 * 4.0 / 6.0)
521 assert_equal(4.0, 6.0 * 4 / 6.0)
522 assert_equal(4.0, 6.0 * 4.0 / 6.0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100524 assert_equal(4.0, 6.0 * 4.0 / 6.0)
525 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526enddef
527
528func Test_expr6_fails()
529 let msg = "white space required before and after '*'"
530 call CheckDefFailure("let x = 1*2", msg)
531 call CheckDefFailure("let x = 1 *2", msg)
532 call CheckDefFailure("let x = 1* 2", msg)
533
534 let msg = "white space required before and after '/'"
535 call CheckDefFailure("let x = 1/2", msg)
536 call CheckDefFailure("let x = 1 /2", msg)
537 call CheckDefFailure("let x = 1/ 2", msg)
538
539 let msg = "white space required before and after '%'"
540 call CheckDefFailure("let x = 1%2", msg)
541 call CheckDefFailure("let x = 1 %2", msg)
542 call CheckDefFailure("let x = 1% 2", msg)
543
544 call CheckDefFailure("let x = '1' * '2'", 'E1036:')
545 call CheckDefFailure("let x = '1' / '2'", 'E1036:')
546 call CheckDefFailure("let x = '1' % '2'", 'E1035:')
547
548 call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
549 call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
550 call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
551
552 call CheckDefFailure("let x = [1] * [2]", 'E1036:')
553 call CheckDefFailure("let x = [1] / [2]", 'E1036:')
554 call CheckDefFailure("let x = [1] % [2]", 'E1035:')
555
556 call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
557 call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
558 call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
559
Bram Moolenaarb13af502020-02-17 21:12:08 +0100560 call CheckDefFailure("let x = 0xff[1]", 'E714:')
561 if has('float')
562 call CheckDefFailure("let x = 0.7[1]", 'E714:')
563 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564endfunc
565
566func Test_expr6_float_fails()
567 CheckFeature float
568 call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
569endfunc
570
571" define here to use old style parsing
572if has('float')
573 let g:float_zero = 0.0
574 let g:float_neg = -9.8
575 let g:float_big = 9.9e99
576endif
577let g:blob_empty = 0z
578let g:blob_one = 0z01
579let g:blob_long = 0z0102.0304
580
581let g:string_empty = ''
582let g:string_short = 'x'
583let g:string_long = 'abcdefghijklm'
584let g:string_special = "ab\ncd\ref\ekk"
585
586let g:special_true = v:true
587let g:special_false = v:false
588let g:special_null = v:null
589let g:special_none = v:none
590
591let g:list_empty = []
592let g:list_mixed = [1, 'b', v:false]
593
594let g:dict_empty = {}
595let g:dict_one = #{one: 1}
596
597let $TESTVAR = 'testvar'
598
599let @a = 'register a'
600
601" test low level expression
602def Test_expr7_number()
603 " number constant
604 assert_equal(0, 0)
605 assert_equal(654, 0654)
606
607 assert_equal(6, 0x6)
608 assert_equal(15, 0xf)
609 assert_equal(255, 0xff)
610enddef
611
612def Test_expr7_float()
613 " float constant
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100614 if !has('float')
615 MissingFeature 'float'
616 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 assert_equal(g:float_zero, .0)
618 assert_equal(g:float_zero, 0.0)
619 assert_equal(g:float_neg, -9.8)
620 assert_equal(g:float_big, 9.9e99)
621 endif
622enddef
623
624def Test_expr7_blob()
625 " blob constant
626 assert_equal(g:blob_empty, 0z)
627 assert_equal(g:blob_one, 0z01)
628 assert_equal(g:blob_long, 0z0102.0304)
629enddef
630
631def Test_expr7_string()
632 " string constant
633 assert_equal(g:string_empty, '')
634 assert_equal(g:string_empty, "")
635 assert_equal(g:string_short, 'x')
636 assert_equal(g:string_short, "x")
637 assert_equal(g:string_long, 'abcdefghijklm')
638 assert_equal(g:string_long, "abcdefghijklm")
639 assert_equal(g:string_special, "ab\ncd\ref\ekk")
640enddef
641
642def Test_expr7_special()
643 " special constant
644 assert_equal(g:special_true, true)
645 assert_equal(g:special_false, false)
646 assert_equal(g:special_null, v:null)
647 assert_equal(g:special_none, v:none)
648enddef
649
650def Test_expr7_list()
651 " list
652 assert_equal(g:list_empty, [])
653 assert_equal(g:list_empty, [ ])
654 assert_equal(g:list_mixed, [1, 'b', false])
655enddef
656
657def Test_expr7_lambda()
658 " lambda
659 let La = { -> 'result'}
660 assert_equal('result', La())
661 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
662enddef
663
664def Test_expr7_dict()
665 " dictionary
666 assert_equal(g:dict_empty, {})
667 assert_equal(g:dict_empty, { })
668 assert_equal(g:dict_one, {'one': 1})
669 let key = 'one'
670 let val = 1
671 assert_equal(g:dict_one, {key: val})
672enddef
673
674def Test_expr7_option()
675 " option
676 set ts=11
677 assert_equal(11, &ts)
678 set ts=8
679 set grepprg=some\ text
680 assert_equal('some text', &grepprg)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100681 &grepprg = test_null_string()
682 assert_equal('', &grepprg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 set grepprg&
684enddef
685
686def Test_expr7_environment()
687 " environment variable
688 assert_equal('testvar', $TESTVAR)
689 assert_equal('', $ASDF_ASD_XXX)
690enddef
691
692def Test_expr7_register()
693 " register
694 assert_equal('register a', @a)
695enddef
696
697def Test_expr7_parens()
698 " (expr)
699 assert_equal(4, (6 * 4) / 6)
700 assert_equal(0, 6 * ( 4 / 6 ))
701
702 assert_equal(6, +6)
703 assert_equal(-6, -6)
704 assert_equal(6, --6)
705 assert_equal(6, -+-6)
706 assert_equal(-6, ---6)
707enddef
708
709def Test_expr7_not()
710 assert_equal(true, !'')
711 assert_equal(true, ![])
712 assert_equal(false, !'asdf')
713 assert_equal(false, ![2])
714 assert_equal(true, !!'asdf')
715 assert_equal(true, !![2])
716enddef
717
718func Test_expr7_fails()
719 call CheckDefFailure("let x = (12", "E110:")
720
721 call CheckDefFailure("let x = -'xx'", "E1030:")
722 call CheckDefFailure("let x = +'xx'", "E1030:")
723
724 call CheckDefFailure("let x = @", "E1002:")
725 call CheckDefFailure("let x = @<", "E354:")
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100726
727 call CheckDefFailure("let x = &notexist", "E113:")
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728endfunc
729
730let g:Funcrefs = [function('add')]
731
732func CallMe(arg)
733 return a:arg
734endfunc
735
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100736func CallMe2(one, two)
737 return a:one .. a:two
738endfunc
739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740def Test_expr7_trailing()
741 " user function call
742 assert_equal(123, CallMe(123))
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100743 assert_equal(123, CallMe( 123))
744 assert_equal(123, CallMe(123 ))
745 assert_equal('yesno', CallMe2('yes', 'no'))
746 assert_equal('yesno', CallMe2( 'yes', 'no' ))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 assert_equal('nothing', CallMe('nothing'))
748
749 " partial call
750 let Part = function('CallMe')
751 assert_equal('yes', Part('yes'))
752
753 " funcref call, using list index
754 let l = []
755 g:Funcrefs[0](l, 2)
756 assert_equal([2], l)
757
758 " method call
759 l = [2, 5, 6]
760 l->map({k, v -> k + v})
761 assert_equal([2, 6, 8], l)
762
763 " lambda method call
764 l = [2, 5]
765 l->{l -> add(l, 8)}()
766 assert_equal([2, 5, 8], l)
767
768 " dict member
769 let d = #{key: 123}
770 assert_equal(123, d.key)
771enddef
772
773func Test_expr7_trailing_fails()
774 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
775endfunc
776
777func Test_expr_fails()
778 call CheckDefFailure("let x = '1'is2", 'E488:')
779 call CheckDefFailure("let x = '1'isnot2", 'E488:')
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100780
781 call CheckDefExecFailure("CallMe ('yes')", 'E492:')
782 call CheckDefFailure("CallMe2('yes','no')", 'E1069:')
783 call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784endfunc