blob: 06c200dbf8f0583211868020f9c10671d7eee3a9 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" Tests for Vim9 script expressions
2
3source check.vim
4
5" Check that "line" inside ":def" results in an "error" message.
6func CheckDefFailure(line, error)
7 call writefile(['def! Func()', a:line, 'enddef'], 'Xdef')
8 call assert_fails('so Xdef', a:error, a:line)
9 call delete('Xdef')
10endfunc
11
Bram Moolenaar42a480b2020-02-29 23:23:47 +010012func CheckDefFailureMult(lines, error)
13 call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef')
14 call assert_fails('so Xdef', a:error, join(a:lines, ' | '))
15 call delete('Xdef')
16endfunc
17
Bram Moolenaar38a5f512020-02-19 12:40:39 +010018" Check that "line" inside ":def" results in an "error" message when executed.
19func CheckDefExecFailure(line, error)
20 call writefile(['def! Func()', a:line, 'enddef'], 'Xdef')
21 so Xdef
22 call assert_fails('call Func()', a:error, a:line)
23 call delete('Xdef')
24endfunc
25
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010026func CheckDefFailureList(lines, error)
27 call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef')
28 call assert_fails('so Xdef', a:error, string(a:lines))
29 call delete('Xdef')
30endfunc
31
32" test cond ? expr : expr
33def Test_expr1()
34 assert_equal('one', true ? 'one' : 'two')
35 assert_equal('one', 1 ? 'one' : 'two')
Bram Moolenaar5feabe02020-01-30 18:24:53 +010036 if has('float')
37 assert_equal('one', 0.1 ? 'one' : 'two')
38 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039 assert_equal('one', 'x' ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010040 assert_equal('one', 0z1234 ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010041 assert_equal('one', [0] ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010042 assert_equal('one', #{x: 0} ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010043 let var = 1
44 assert_equal('one', var ? 'one' : 'two')
45
46 assert_equal('two', false ? 'one' : 'two')
47 assert_equal('two', 0 ? 'one' : 'two')
Bram Moolenaar7f829ca2020-01-31 22:12:41 +010048 if has('float')
49 assert_equal('two', 0.0 ? 'one' : 'two')
50 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010051 assert_equal('two', '' ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010052 assert_equal('two', 0z ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010053 assert_equal('two', [] ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010054 assert_equal('two', {} ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010055 var = 0
56 assert_equal('two', var ? 'one' : 'two')
57enddef
58
59func Test_expr1_fails()
60 call CheckDefFailure("let x = 1 ? 'one'", "Missing ':' after '?'")
Bram Moolenaar9be61bb2020-03-30 22:51:24 +020061 call CheckDefFailure("let x = 1 ? 'one' : xxx", "E1001:")
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062
63 let msg = "white space required before and after '?'"
64 call CheckDefFailure("let x = 1? 'one' : 'two'", msg)
65 call CheckDefFailure("let x = 1 ?'one' : 'two'", msg)
66 call CheckDefFailure("let x = 1?'one' : 'two'", msg)
67
68 let msg = "white space required before and after ':'"
69 call CheckDefFailure("let x = 1 ? 'one': 'two'", msg)
70 call CheckDefFailure("let x = 1 ? 'one' :'two'", msg)
71 call CheckDefFailure("let x = 1 ? 'one':'two'", msg)
72endfunc
73
74" TODO: define inside test function
75def Record(val: any): any
76 g:vals->add(val)
77 return val
78enddef
79
80" test ||
81def Test_expr2()
82 assert_equal(2, 2 || 0)
83 assert_equal(7, 0 || 0 || 7)
84 assert_equal(0, 0 || 0)
85 assert_equal('', 0 || '')
86
87 g:vals = []
88 assert_equal(3, Record(3) || Record(1))
89 assert_equal([3], g:vals)
90
91 g:vals = []
92 assert_equal(5, Record(0) || Record(5))
93 assert_equal([0, 5], g:vals)
94
95 g:vals = []
96 assert_equal(4, Record(0) || Record(4) || Record(0))
97 assert_equal([0, 4], g:vals)
98
99 g:vals = []
100 assert_equal(0, Record([]) || Record('') || Record(0))
101 assert_equal([[], '', 0], g:vals)
102enddef
103
104func Test_expr2_fails()
105 let msg = "white space required before and after '||'"
106 call CheckDefFailure("let x = 1||2", msg)
107 call CheckDefFailure("let x = 1 ||2", msg)
108 call CheckDefFailure("let x = 1|| 2", msg)
109endfunc
110
111" test &&
112def Test_expr3()
113 assert_equal(0, 2 && 0)
114 assert_equal(0, 0 && 0 && 7)
115 assert_equal(7, 2 && 3 && 7)
116 assert_equal(0, 0 && 0)
117 assert_equal(0, 0 && '')
118 assert_equal('', 8 && '')
119
120 g:vals = []
121 assert_equal(1, Record(3) && Record(1))
122 assert_equal([3, 1], g:vals)
123
124 g:vals = []
125 assert_equal(0, Record(0) && Record(5))
126 assert_equal([0], g:vals)
127
128 g:vals = []
129 assert_equal(0, Record(0) && Record(4) && Record(0))
130 assert_equal([0], g:vals)
131
132 g:vals = []
133 assert_equal(0, Record(8) && Record(4) && Record(0))
134 assert_equal([8, 4, 0], g:vals)
135
136 g:vals = []
137 assert_equal(0, Record([1]) && Record('z') && Record(0))
138 assert_equal([[1], 'z', 0], g:vals)
139enddef
140
141func Test_expr3_fails()
142 let msg = "white space required before and after '&&'"
143 call CheckDefFailure("let x = 1&&2", msg)
144 call CheckDefFailure("let x = 1 &&2", msg)
145 call CheckDefFailure("let x = 1&& 2", msg)
146endfunc
147
148let atrue = v:true
149let afalse = v:false
150let anone = v:none
151let anull = v:null
152let anint = 10
153let alsoint = 4
154if has('float')
155 let afloat = 0.1
156endif
157let astring = 'asdf'
158let ablob = 0z01ab
159let alist = [2, 3, 4]
160let adict = #{aaa: 2, bbb: 8}
161
162" test == comperator
163def Test_expr4_equal()
164 assert_equal(true, true == true)
165 assert_equal(false, true == false)
166 assert_equal(true, true == g:atrue)
167 assert_equal(false, g:atrue == false)
168
169 assert_equal(true, v:none == v:none)
170 assert_equal(false, v:none == v:null)
171 assert_equal(true, g:anone == v:none)
172 assert_equal(false, v:none == g:anull)
173
174 assert_equal(false, 2 == 0)
175 assert_equal(true, 61 == 61)
176 assert_equal(true, g:anint == 10)
177 assert_equal(false, 61 == g:anint)
178
179 if has('float')
180 assert_equal(true, 0.3 == 0.3)
181 assert_equal(false, 0.4 == 0.3)
182 assert_equal(true, 0.1 == g:afloat)
183 assert_equal(false, g:afloat == 0.3)
184
185 assert_equal(true, 3.0 == 3)
186 assert_equal(true, 3 == 3.0)
187 assert_equal(false, 3.1 == 3)
188 assert_equal(false, 3 == 3.1)
189 endif
190
191 assert_equal(true, 'abc' == 'abc')
192 assert_equal(false, 'xyz' == 'abc')
193 assert_equal(true, g:astring == 'asdf')
194 assert_equal(false, 'xyz' == g:astring)
195
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200196 assert_equal(false, 'abc' == 'aBc')
197 assert_equal(false, 'abc' ==# 'aBc')
198 assert_equal(true, 'abc' ==? 'aBc')
199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100200 assert_equal(false, 'abc' == 'ABC')
201 set ignorecase
202 assert_equal(false, 'abc' == 'ABC')
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200203 assert_equal(false, 'abc' ==# 'ABC')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 set noignorecase
205
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200206 call CheckDefFailure("let x = 'a' == xxx", 'E1001:')
207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 assert_equal(true, 0z3f == 0z3f)
209 assert_equal(false, 0z3f == 0z4f)
210 assert_equal(true, g:ablob == 0z01ab)
211 assert_equal(false, 0z3f == g:ablob)
212
213 assert_equal(true, [1, 2, 3] == [1, 2, 3])
214 assert_equal(false, [1, 2, 3] == [2, 3, 1])
215 assert_equal(true, [2, 3, 4] == g:alist)
216 assert_equal(false, g:alist == [2, 3, 1])
217 assert_equal(false, [1, 2, 3] == [])
218 assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
219
220 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
221 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
222 assert_equal(false, #{one: 1, two: 2} == #{two: 2})
223 assert_equal(false, #{one: 1, two: 2} == #{})
224 assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
225 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
226
227 assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal'))
228 assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is'))
229
230 assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123]))
231 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123]))
232 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999]))
233enddef
234
235" test != comperator
236def Test_expr4_notequal()
237 assert_equal(false, true != true)
238 assert_equal(true, true != false)
239 assert_equal(false, true != g:atrue)
240 assert_equal(true, g:atrue != false)
241
242 assert_equal(false, v:none != v:none)
243 assert_equal(true, v:none != v:null)
244 assert_equal(false, g:anone != v:none)
245 assert_equal(true, v:none != g:anull)
246
247 assert_equal(true, 2 != 0)
248 assert_equal(false, 55 != 55)
249 assert_equal(false, g:anint != 10)
250 assert_equal(true, 61 != g:anint)
251
252 if has('float')
253 assert_equal(false, 0.3 != 0.3)
254 assert_equal(true, 0.4 != 0.3)
255 assert_equal(false, 0.1 != g:afloat)
256 assert_equal(true, g:afloat != 0.3)
257
258 assert_equal(false, 3.0 != 3)
259 assert_equal(false, 3 != 3.0)
260 assert_equal(true, 3.1 != 3)
261 assert_equal(true, 3 != 3.1)
262 endif
263
264 assert_equal(false, 'abc' != 'abc')
265 assert_equal(true, 'xyz' != 'abc')
266 assert_equal(false, g:astring != 'asdf')
267 assert_equal(true, 'xyz' != g:astring)
268
269 assert_equal(true, 'abc' != 'ABC')
270 set ignorecase
271 assert_equal(true, 'abc' != 'ABC')
272 set noignorecase
273
274 assert_equal(false, 0z3f != 0z3f)
275 assert_equal(true, 0z3f != 0z4f)
276 assert_equal(false, g:ablob != 0z01ab)
277 assert_equal(true, 0z3f != g:ablob)
278
279 assert_equal(false, [1, 2, 3] != [1, 2, 3])
280 assert_equal(true, [1, 2, 3] != [2, 3, 1])
281 assert_equal(false, [2, 3, 4] != g:alist)
282 assert_equal(true, g:alist != [2, 3, 1])
283 assert_equal(true, [1, 2, 3] != [])
284 assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
285
286 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
287 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
288 assert_equal(true, #{one: 1, two: 2} != #{two: 2})
289 assert_equal(true, #{one: 1, two: 2} != #{})
290 assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
291 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
292
293 assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal'))
294 assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is'))
295
296 assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123]))
297 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123]))
298 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999]))
299enddef
300
301" test > comperator
302def Test_expr4_greater()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100303 assert_true(2 > 0)
304 assert_true(2 > 1)
305 assert_false(2 > 2)
306 assert_false(2 > 3)
307 if has('float')
308 assert_true(2.0 > 0.0)
309 assert_true(2.0 > 1.0)
310 assert_false(2.0 > 2.0)
311 assert_false(2.0 > 3.0)
312 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313enddef
314
315" test >= comperator
316def Test_expr4_greaterequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100317 assert_true(2 >= 0)
318 assert_true(2 >= 2)
319 assert_false(2 >= 3)
320 if has('float')
321 assert_true(2.0 >= 0.0)
322 assert_true(2.0 >= 2.0)
323 assert_false(2.0 >= 3.0)
324 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325enddef
326
327" test < comperator
328def Test_expr4_smaller()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100329 assert_false(2 < 0)
330 assert_false(2 < 2)
331 assert_true(2 < 3)
332 if has('float')
333 assert_false(2.0 < 0.0)
334 assert_false(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_smallerequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100341 assert_false(2 <= 0)
342 assert_false(2 <= 1)
343 assert_true(2 <= 2)
344 assert_true(2 <= 3)
345 if has('float')
346 assert_false(2.0 <= 0.0)
347 assert_false(2.0 <= 1.0)
348 assert_true(2.0 <= 2.0)
349 assert_true(2.0 <= 3.0)
350 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351enddef
352
353" test =~ comperator
354def Test_expr4_match()
355 assert_equal(false, '2' =~ '0')
356 assert_equal(true, '2' =~ '[0-9]')
357enddef
358
359" test !~ comperator
360def Test_expr4_nomatch()
361 assert_equal(true, '2' !~ '0')
362 assert_equal(false, '2' !~ '[0-9]')
363enddef
364
365" test is comperator
366def Test_expr4_is()
367 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100368 assert_false(mylist is [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100370 assert_true(mylist is other)
371
372 let myblob = 0z1234
373 assert_false(myblob is 0z1234)
374 let otherblob = myblob
375 assert_true(myblob is otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376enddef
377
378" test isnot comperator
379def Test_expr4_isnot()
380 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100381 assert_true('2' isnot '0')
382 assert_true(mylist isnot [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100384 assert_false(mylist isnot other)
385
386 let myblob = 0z1234
387 assert_true(myblob isnot 0z1234)
388 let otherblob = myblob
389 assert_false(myblob isnot otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390enddef
391
392def RetVoid()
393 let x = 1
394enddef
395
396func Test_expr4_fails()
397 let msg = "white space required before and after '>'"
398 call CheckDefFailure("let x = 1>2", msg)
399 call CheckDefFailure("let x = 1 >2", msg)
400 call CheckDefFailure("let x = 1> 2", msg)
401
402 let msg = "white space required before and after '=='"
403 call CheckDefFailure("let x = 1==2", msg)
404 call CheckDefFailure("let x = 1 ==2", msg)
405 call CheckDefFailure("let x = 1== 2", msg)
406
407 let msg = "white space required before and after 'is'"
408 call CheckDefFailure("let x = '1'is'2'", msg)
409 call CheckDefFailure("let x = '1' is'2'", msg)
410 call CheckDefFailure("let x = '1'is '2'", msg)
411
412 let msg = "white space required before and after 'isnot'"
413 call CheckDefFailure("let x = '1'isnot'2'", msg)
414 call CheckDefFailure("let x = '1' isnot'2'", msg)
415 call CheckDefFailure("let x = '1'isnot '2'", msg)
416
417 call CheckDefFailure("let x = 1 is# 2", 'E15:')
418 call CheckDefFailure("let x = 1 is? 2", 'E15:')
419 call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
420 call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
421
422 call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
423 call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
424 call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
425 call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
426
427 call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool')
428 call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool')
429 call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool')
430 call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool')
431 call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool')
432 call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
433 call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
434 call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
435
436 call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
437 call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
438 call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
439 call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
440 if has('float')
441 call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
442 call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
443 endif
444
445 call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob')
446 call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob')
447 call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob')
448 call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob')
449 call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
450 call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
451
452 call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list')
453 call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list')
454 call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list')
455 call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list')
456 call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
457 call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100458
459 call CheckDefFailureMult(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel')
460 call CheckDefFailureMult(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list')
461 call CheckDefFailureMult(['let j: job', 'let x: func', 'let r = j == x'], 'Cannot compare job with func')
462 call CheckDefFailureMult(['let j: job', 'let x: partial', 'let r = j == x'], 'Cannot compare job with partial')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463endfunc
464
465" test addition, subtraction, concatenation
466def Test_expr5()
467 assert_equal(66, 60 + 6)
468 assert_equal(70, 60 + g:anint)
469 assert_equal(9, g:alsoint + 5)
470 assert_equal(14, g:alsoint + g:anint)
471
472 assert_equal(54, 60 - 6)
473 assert_equal(50, 60 - g:anint)
474 assert_equal(-1, g:alsoint - 5)
475 assert_equal(-6, g:alsoint - g:anint)
476
477 assert_equal('hello', 'hel' .. 'lo')
478 assert_equal('hello 123', 'hello ' .. 123)
479 assert_equal('123 hello', 123 .. ' hello')
480 assert_equal('123456', 123 .. 456)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100481
482 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
483 assert_equal(0z11223344, 0z1122 + 0z3344)
484 assert_equal(0z112201ab, 0z1122 + g:ablob)
485 assert_equal(0z01ab3344, g:ablob + 0z3344)
486 assert_equal(0z01ab01ab, g:ablob + g:ablob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487enddef
488
489def Test_expr5_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100490 if !has('float')
491 MissingFeature 'float'
492 else
493 assert_equal(66.0, 60.0 + 6.0)
494 assert_equal(66.0, 60.0 + 6)
495 assert_equal(66.0, 60 + 6.0)
496 assert_equal(5.1, g:afloat + 5)
497 assert_equal(8.1, 8 + g:afloat)
498 assert_equal(10.1, g:anint + g:afloat)
499 assert_equal(10.1, g:afloat + g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100501 assert_equal(54.0, 60.0 - 6.0)
502 assert_equal(54.0, 60.0 - 6)
503 assert_equal(54.0, 60 - 6.0)
504 assert_equal(-4.9, g:afloat - 5)
505 assert_equal(7.9, 8 - g:afloat)
506 assert_equal(9.9, g:anint - g:afloat)
507 assert_equal(-9.9, g:afloat - g:anint)
508 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509enddef
510
511func Test_expr5_fails()
512 let msg = "white space required before and after '+'"
513 call CheckDefFailure("let x = 1+2", msg)
514 call CheckDefFailure("let x = 1 +2", msg)
515 call CheckDefFailure("let x = 1+ 2", msg)
516
517 let msg = "white space required before and after '-'"
518 call CheckDefFailure("let x = 1-2", msg)
519 call CheckDefFailure("let x = 1 -2", msg)
520 call CheckDefFailure("let x = 1- 2", msg)
521
522 let msg = "white space required before and after '..'"
523 call CheckDefFailure("let x = '1'..'2'", msg)
524 call CheckDefFailure("let x = '1' ..'2'", msg)
525 call CheckDefFailure("let x = '1'.. '2'", msg)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100526
527 call CheckDefFailure("let x = 0z1122 + 33", 'E1035')
528 call CheckDefFailure("let x = 0z1122 + [3]", 'E1035')
529 call CheckDefFailure("let x = 0z1122 + 'asd'", 'E1035')
530 call CheckDefFailure("let x = 33 + 0z1122", 'E1035')
531 call CheckDefFailure("let x = [3] + 0z1122", 'E1035')
532 call CheckDefFailure("let x = 'asdf' + 0z1122", 'E1035')
Bram Moolenaar92dba362020-03-30 21:22:56 +0200533 call CheckDefFailure("let x = 6 + xxx", 'E1001')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534endfunc
535
536" test multiply, divide, modulo
537def Test_expr6()
538 assert_equal(36, 6 * 6)
539 assert_equal(24, 6 * g:alsoint)
540 assert_equal(24, g:alsoint * 6)
541 assert_equal(40, g:anint * g:alsoint)
542
543 assert_equal(10, 60 / 6)
544 assert_equal(6, 60 / g:anint)
545 assert_equal(1, g:anint / 6)
546 assert_equal(2, g:anint / g:alsoint)
547
548 assert_equal(5, 11 % 6)
549 assert_equal(4, g:anint % 6)
550 assert_equal(3, 13 % g:anint)
551 assert_equal(2, g:anint % g:alsoint)
552
553 assert_equal(4, 6 * 4 / 6)
Bram Moolenaarb13af502020-02-17 21:12:08 +0100554
555 let x = [2]
556 let y = [3]
557 assert_equal(5, x[0] + y[0])
558 assert_equal(6, x[0] * y[0])
559 if has('float')
560 let xf = [2.0]
561 let yf = [3.0]
562 assert_equal(5.0, xf[0] + yf[0])
563 assert_equal(6.0, xf[0] * yf[0])
564 endif
Bram Moolenaar92dba362020-03-30 21:22:56 +0200565
566 call CheckDefFailure("let x = 6 * xxx", 'E1001')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567enddef
568
569def Test_expr6_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100570 if !has('float')
571 MissingFeature 'float'
572 else
573 assert_equal(36.0, 6.0 * 6)
574 assert_equal(36.0, 6 * 6.0)
575 assert_equal(36.0, 6.0 * 6.0)
576 assert_equal(1.0, g:afloat * g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100578 assert_equal(10.0, 60 / 6.0)
579 assert_equal(10.0, 60.0 / 6)
580 assert_equal(10.0, 60.0 / 6.0)
581 assert_equal(0.01, g:afloat / g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100583 assert_equal(4.0, 6.0 * 4 / 6)
584 assert_equal(4.0, 6 * 4.0 / 6)
585 assert_equal(4.0, 6 * 4 / 6.0)
586 assert_equal(4.0, 6.0 * 4.0 / 6)
587 assert_equal(4.0, 6 * 4.0 / 6.0)
588 assert_equal(4.0, 6.0 * 4 / 6.0)
589 assert_equal(4.0, 6.0 * 4.0 / 6.0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100591 assert_equal(4.0, 6.0 * 4.0 / 6.0)
592 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593enddef
594
595func Test_expr6_fails()
596 let msg = "white space required before and after '*'"
597 call CheckDefFailure("let x = 1*2", msg)
598 call CheckDefFailure("let x = 1 *2", msg)
599 call CheckDefFailure("let x = 1* 2", msg)
600
601 let msg = "white space required before and after '/'"
602 call CheckDefFailure("let x = 1/2", msg)
603 call CheckDefFailure("let x = 1 /2", msg)
604 call CheckDefFailure("let x = 1/ 2", msg)
605
606 let msg = "white space required before and after '%'"
607 call CheckDefFailure("let x = 1%2", msg)
608 call CheckDefFailure("let x = 1 %2", msg)
609 call CheckDefFailure("let x = 1% 2", msg)
610
611 call CheckDefFailure("let x = '1' * '2'", 'E1036:')
612 call CheckDefFailure("let x = '1' / '2'", 'E1036:')
613 call CheckDefFailure("let x = '1' % '2'", 'E1035:')
614
615 call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
616 call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
617 call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
618
619 call CheckDefFailure("let x = [1] * [2]", 'E1036:')
620 call CheckDefFailure("let x = [1] / [2]", 'E1036:')
621 call CheckDefFailure("let x = [1] % [2]", 'E1035:')
622
623 call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
624 call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
625 call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
626
Bram Moolenaarb13af502020-02-17 21:12:08 +0100627 call CheckDefFailure("let x = 0xff[1]", 'E714:')
628 if has('float')
629 call CheckDefFailure("let x = 0.7[1]", 'E714:')
630 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631endfunc
632
633func Test_expr6_float_fails()
634 CheckFeature float
635 call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
636endfunc
637
638" define here to use old style parsing
639if has('float')
640 let g:float_zero = 0.0
641 let g:float_neg = -9.8
642 let g:float_big = 9.9e99
643endif
644let g:blob_empty = 0z
645let g:blob_one = 0z01
646let g:blob_long = 0z0102.0304
647
648let g:string_empty = ''
649let g:string_short = 'x'
650let g:string_long = 'abcdefghijklm'
651let g:string_special = "ab\ncd\ref\ekk"
652
653let g:special_true = v:true
654let g:special_false = v:false
655let g:special_null = v:null
656let g:special_none = v:none
657
658let g:list_empty = []
659let g:list_mixed = [1, 'b', v:false]
660
661let g:dict_empty = {}
662let g:dict_one = #{one: 1}
663
664let $TESTVAR = 'testvar'
665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666" test low level expression
667def Test_expr7_number()
668 " number constant
669 assert_equal(0, 0)
670 assert_equal(654, 0654)
671
672 assert_equal(6, 0x6)
673 assert_equal(15, 0xf)
674 assert_equal(255, 0xff)
675enddef
676
677def Test_expr7_float()
678 " float constant
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100679 if !has('float')
680 MissingFeature 'float'
681 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 assert_equal(g:float_zero, .0)
683 assert_equal(g:float_zero, 0.0)
684 assert_equal(g:float_neg, -9.8)
685 assert_equal(g:float_big, 9.9e99)
686 endif
687enddef
688
689def Test_expr7_blob()
690 " blob constant
691 assert_equal(g:blob_empty, 0z)
692 assert_equal(g:blob_one, 0z01)
693 assert_equal(g:blob_long, 0z0102.0304)
Bram Moolenaar92dba362020-03-30 21:22:56 +0200694
695 call CheckDefFailure("let x = 0z123", 'E973:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696enddef
697
698def Test_expr7_string()
699 " string constant
700 assert_equal(g:string_empty, '')
701 assert_equal(g:string_empty, "")
702 assert_equal(g:string_short, 'x')
703 assert_equal(g:string_short, "x")
704 assert_equal(g:string_long, 'abcdefghijklm')
705 assert_equal(g:string_long, "abcdefghijklm")
706 assert_equal(g:string_special, "ab\ncd\ref\ekk")
Bram Moolenaar92dba362020-03-30 21:22:56 +0200707
708 call CheckDefFailure('let x = "abc', 'E114:')
709 call CheckDefFailure("let x = 'abc", 'E115:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710enddef
711
712def Test_expr7_special()
713 " special constant
714 assert_equal(g:special_true, true)
715 assert_equal(g:special_false, false)
716 assert_equal(g:special_null, v:null)
717 assert_equal(g:special_none, v:none)
718enddef
719
720def Test_expr7_list()
721 " list
722 assert_equal(g:list_empty, [])
723 assert_equal(g:list_empty, [ ])
724 assert_equal(g:list_mixed, [1, 'b', false])
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200725 assert_equal('b', g:list_mixed[1])
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100726
727 call CheckDefExecFailure("let x = g:anint[3]", 'E714:')
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200728 call CheckDefFailure("let x = g:list_mixed[xxx]", 'E1001:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100729 call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:')
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200730 call CheckDefFailure("let x = g:list_mixed[0", 'E111:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100731 call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732enddef
733
734def Test_expr7_lambda()
735 " lambda
736 let La = { -> 'result'}
737 assert_equal('result', La())
738 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
739enddef
740
741def Test_expr7_dict()
742 " dictionary
743 assert_equal(g:dict_empty, {})
744 assert_equal(g:dict_empty, { })
745 assert_equal(g:dict_one, {'one': 1})
746 let key = 'one'
747 let val = 1
748 assert_equal(g:dict_one, {key: val})
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100749
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200750 call CheckDefFailure("let x = #{8: 8}", 'E1014:')
751 call CheckDefFailure("let x = #{xxx}", 'E720:')
752 call CheckDefFailure("let x = #{xxx: 1", 'E722:')
753 call CheckDefFailure("let x = #{xxx: 1,", 'E723:')
754 call CheckDefFailure("let x = {'a': xxx}", 'E1001:')
755 call CheckDefFailure("let x = {xxx: 8}", 'E1001:')
756 call CheckDefFailure("let x = #{a: 1, a: 2}", 'E721:')
Bram Moolenaar92dba362020-03-30 21:22:56 +0200757 call CheckDefFailure("let x = #", 'E1015:')
Bram Moolenaard25ec2c2020-03-30 21:05:45 +0200758 call CheckDefFailure("let x += 1", 'E1020:')
759 call CheckDefFailure("let x = x + 1", 'E1001:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100760 call CheckDefExecFailure("let x = g:anint.member", 'E715:')
761 call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762enddef
763
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200764def Test_expr_member()
765 assert_equal(1, g:dict_one.one)
766
767 call CheckDefFailure("let x = g:dict_one.#$!", 'E1002:')
768enddef
769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770def Test_expr7_option()
771 " option
772 set ts=11
773 assert_equal(11, &ts)
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100774 &ts = 9
775 assert_equal(9, &ts)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 set ts=8
777 set grepprg=some\ text
778 assert_equal('some text', &grepprg)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100779 &grepprg = test_null_string()
780 assert_equal('', &grepprg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 set grepprg&
782enddef
783
784def Test_expr7_environment()
785 " environment variable
786 assert_equal('testvar', $TESTVAR)
787 assert_equal('', $ASDF_ASD_XXX)
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200788
789 call CheckDefFailure("let x = $$$", 'E1002:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790enddef
791
792def Test_expr7_register()
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100793 @a = 'register a'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 assert_equal('register a', @a)
795enddef
796
797def Test_expr7_parens()
798 " (expr)
799 assert_equal(4, (6 * 4) / 6)
800 assert_equal(0, 6 * ( 4 / 6 ))
801
802 assert_equal(6, +6)
803 assert_equal(-6, -6)
804 assert_equal(6, --6)
805 assert_equal(6, -+-6)
806 assert_equal(-6, ---6)
807enddef
808
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200809def Test_expr7_negate()
810 assert_equal(-99, -99)
811 assert_equal(99, --99)
812 let nr = 88
813 assert_equal(-88, -nr)
814 assert_equal(88, --nr)
815enddef
816
817def Echo(arg): string
818 return arg
819enddef
820
821def s:EchoArg(arg): string
822 return arg
823enddef
824
825def Test_expr7_call()
826 assert_equal('yes', 'yes'->Echo())
827 assert_equal('yes', 'yes'->s:EchoArg())
828
829 call CheckDefFailure("let x = 'yes'->Echo", 'E107:')
830enddef
831
832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833def Test_expr7_not()
834 assert_equal(true, !'')
835 assert_equal(true, ![])
836 assert_equal(false, !'asdf')
837 assert_equal(false, ![2])
838 assert_equal(true, !!'asdf')
839 assert_equal(true, !![2])
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100840
841 assert_equal(true, !test_null_partial())
842 assert_equal(false, !{-> 'yes'})
843
844 assert_equal(true, !test_null_dict())
845 assert_equal(true, !{})
846 assert_equal(false, !{'yes': 'no'})
847
Bram Moolenaarb4d2cb12020-02-22 20:33:08 +0100848 if has('channel')
849 assert_equal(true, !test_null_job())
850 assert_equal(true, !test_null_channel())
851 endif
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100852
853 assert_equal(true, !test_null_blob())
854 assert_equal(true, !0z)
855 assert_equal(false, !0z01)
856
857 assert_equal(true, !test_void())
858 assert_equal(true, !test_unknown())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859enddef
860
861func Test_expr7_fails()
862 call CheckDefFailure("let x = (12", "E110:")
863
864 call CheckDefFailure("let x = -'xx'", "E1030:")
865 call CheckDefFailure("let x = +'xx'", "E1030:")
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200866 call CheckDefFailure("let x = -0z12", "E974:")
867 call CheckDefExecFailure("let x = -[8]", "E39:")
868 call CheckDefExecFailure("let x = -{'a': 1}", "E39:")
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
870 call CheckDefFailure("let x = @", "E1002:")
871 call CheckDefFailure("let x = @<", "E354:")
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100872
Bram Moolenaaree619e52020-03-28 21:38:06 +0100873 call CheckDefFailure("let x = [1, 2", "E697:")
874 call CheckDefFailure("let x = [notfound]", "E1001:")
875
876 call CheckDefFailure("let x = { -> 123) }", "E451:")
877 call CheckDefFailure("let x = 123->{x -> x + 5) }", "E451:")
878
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100879 call CheckDefFailure("let x = &notexist", 'E113:')
880 call CheckDefExecFailure("&grepprg = [343]", 'E1051:')
Bram Moolenaarfd1823e2020-02-19 20:23:11 +0100881
882 call CheckDefExecFailure("echo s:doesnt_exist", 'E121:')
883 call CheckDefExecFailure("echo g:doesnt_exist", 'E121:')
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100884
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100885 call CheckDefFailure("echo a:somevar", 'E1075:')
886 call CheckDefFailure("echo l:somevar", 'E1075:')
887 call CheckDefFailure("echo x:somevar", 'E1075:')
888
889 " TODO
890 call CheckDefFailure("echo b:somevar", 'not supported yet')
891 call CheckDefFailure("echo w:somevar", 'not supported yet')
892 call CheckDefFailure("echo t:somevar", 'not supported yet')
893
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100894 call CheckDefExecFailure("let x = +g:astring", 'E1030:')
895 call CheckDefExecFailure("let x = +g:ablob", 'E974:')
896 call CheckDefExecFailure("let x = +g:alist", 'E745:')
897 call CheckDefExecFailure("let x = +g:adict", 'E728:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100898
899 call CheckDefFailureMult(["let x = ''", "let y = x.memb"], 'E715:')
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100900
901 call CheckDefExecFailure("[1, 2->len()", 'E492:')
902 call CheckDefExecFailure("#{a: 1->len()", 'E488:')
903 call CheckDefExecFailure("{'a': 1->len()", 'E492:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904endfunc
905
906let g:Funcrefs = [function('add')]
907
908func CallMe(arg)
909 return a:arg
910endfunc
911
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100912func CallMe2(one, two)
913 return a:one .. a:two
914endfunc
915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916def Test_expr7_trailing()
917 " user function call
918 assert_equal(123, CallMe(123))
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100919 assert_equal(123, CallMe( 123))
920 assert_equal(123, CallMe(123 ))
921 assert_equal('yesno', CallMe2('yes', 'no'))
922 assert_equal('yesno', CallMe2( 'yes', 'no' ))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 assert_equal('nothing', CallMe('nothing'))
924
925 " partial call
926 let Part = function('CallMe')
927 assert_equal('yes', Part('yes'))
928
929 " funcref call, using list index
930 let l = []
931 g:Funcrefs[0](l, 2)
932 assert_equal([2], l)
933
934 " method call
935 l = [2, 5, 6]
936 l->map({k, v -> k + v})
937 assert_equal([2, 6, 8], l)
938
939 " lambda method call
940 l = [2, 5]
941 l->{l -> add(l, 8)}()
942 assert_equal([2, 5, 8], l)
943
944 " dict member
945 let d = #{key: 123}
946 assert_equal(123, d.key)
947enddef
948
949func Test_expr7_trailing_fails()
950 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100951 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952endfunc
953
954func Test_expr_fails()
955 call CheckDefFailure("let x = '1'is2", 'E488:')
956 call CheckDefFailure("let x = '1'isnot2", 'E488:')
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100957
958 call CheckDefExecFailure("CallMe ('yes')", 'E492:')
959 call CheckDefFailure("CallMe2('yes','no')", 'E1069:')
960 call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:')
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100961
962 call CheckDefFailure("v:nosuch += 3", 'E1001:')
963 call CheckDefFailure("let v:version = 3", 'E1064:')
964 call CheckDefFailure("let asdf = v:nosuch", 'E1001:')
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100965
966 call CheckDefFailure("echo len('asdf'", 'E110:')
967 call CheckDefFailure("echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()", 'E1011:')
968 call CheckDefFailure("echo doesnotexist()", 'E117:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969endfunc