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