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