blob: f11fc83893502f36cd85964c81e8adc7eb47c95d [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)
Bram Moolenaara8c17702020-04-01 21:17:24 +0200109
110 call CheckDefFailure("let x = 1 || xxx", 'E1001:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111endfunc
112
113" test &&
114def Test_expr3()
115 assert_equal(0, 2 && 0)
116 assert_equal(0, 0 && 0 && 7)
117 assert_equal(7, 2 && 3 && 7)
118 assert_equal(0, 0 && 0)
119 assert_equal(0, 0 && '')
120 assert_equal('', 8 && '')
121
122 g:vals = []
123 assert_equal(1, Record(3) && Record(1))
124 assert_equal([3, 1], g:vals)
125
126 g:vals = []
127 assert_equal(0, Record(0) && Record(5))
128 assert_equal([0], g:vals)
129
130 g:vals = []
131 assert_equal(0, Record(0) && Record(4) && Record(0))
132 assert_equal([0], g:vals)
133
134 g:vals = []
135 assert_equal(0, Record(8) && Record(4) && Record(0))
136 assert_equal([8, 4, 0], g:vals)
137
138 g:vals = []
139 assert_equal(0, Record([1]) && Record('z') && Record(0))
140 assert_equal([[1], 'z', 0], g:vals)
141enddef
142
143func Test_expr3_fails()
144 let msg = "white space required before and after '&&'"
145 call CheckDefFailure("let x = 1&&2", msg)
146 call CheckDefFailure("let x = 1 &&2", msg)
147 call CheckDefFailure("let x = 1&& 2", msg)
148endfunc
149
150let atrue = v:true
151let afalse = v:false
152let anone = v:none
153let anull = v:null
154let anint = 10
155let alsoint = 4
156if has('float')
157 let afloat = 0.1
158endif
159let astring = 'asdf'
160let ablob = 0z01ab
161let alist = [2, 3, 4]
162let adict = #{aaa: 2, bbb: 8}
163
164" test == comperator
165def Test_expr4_equal()
166 assert_equal(true, true == true)
167 assert_equal(false, true == false)
168 assert_equal(true, true == g:atrue)
169 assert_equal(false, g:atrue == false)
170
171 assert_equal(true, v:none == v:none)
172 assert_equal(false, v:none == v:null)
173 assert_equal(true, g:anone == v:none)
174 assert_equal(false, v:none == g:anull)
175
176 assert_equal(false, 2 == 0)
177 assert_equal(true, 61 == 61)
178 assert_equal(true, g:anint == 10)
179 assert_equal(false, 61 == g:anint)
180
181 if has('float')
182 assert_equal(true, 0.3 == 0.3)
183 assert_equal(false, 0.4 == 0.3)
184 assert_equal(true, 0.1 == g:afloat)
185 assert_equal(false, g:afloat == 0.3)
186
187 assert_equal(true, 3.0 == 3)
188 assert_equal(true, 3 == 3.0)
189 assert_equal(false, 3.1 == 3)
190 assert_equal(false, 3 == 3.1)
191 endif
192
193 assert_equal(true, 'abc' == 'abc')
194 assert_equal(false, 'xyz' == 'abc')
195 assert_equal(true, g:astring == 'asdf')
196 assert_equal(false, 'xyz' == g:astring)
197
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200198 assert_equal(false, 'abc' == 'aBc')
199 assert_equal(false, 'abc' ==# 'aBc')
200 assert_equal(true, 'abc' ==? 'aBc')
201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 assert_equal(false, 'abc' == 'ABC')
203 set ignorecase
204 assert_equal(false, 'abc' == 'ABC')
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200205 assert_equal(false, 'abc' ==# 'ABC')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206 set noignorecase
207
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200208 call CheckDefFailure("let x = 'a' == xxx", 'E1001:')
209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210 assert_equal(true, 0z3f == 0z3f)
211 assert_equal(false, 0z3f == 0z4f)
212 assert_equal(true, g:ablob == 0z01ab)
213 assert_equal(false, 0z3f == g:ablob)
214
215 assert_equal(true, [1, 2, 3] == [1, 2, 3])
216 assert_equal(false, [1, 2, 3] == [2, 3, 1])
217 assert_equal(true, [2, 3, 4] == g:alist)
218 assert_equal(false, g:alist == [2, 3, 1])
219 assert_equal(false, [1, 2, 3] == [])
220 assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
221
222 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
223 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
224 assert_equal(false, #{one: 1, two: 2} == #{two: 2})
225 assert_equal(false, #{one: 1, two: 2} == #{})
226 assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
227 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
228
229 assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal'))
230 assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is'))
231
232 assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123]))
233 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123]))
234 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999]))
235enddef
236
237" test != comperator
238def Test_expr4_notequal()
239 assert_equal(false, true != true)
240 assert_equal(true, true != false)
241 assert_equal(false, true != g:atrue)
242 assert_equal(true, g:atrue != false)
243
244 assert_equal(false, v:none != v:none)
245 assert_equal(true, v:none != v:null)
246 assert_equal(false, g:anone != v:none)
247 assert_equal(true, v:none != g:anull)
248
249 assert_equal(true, 2 != 0)
250 assert_equal(false, 55 != 55)
251 assert_equal(false, g:anint != 10)
252 assert_equal(true, 61 != g:anint)
253
254 if has('float')
255 assert_equal(false, 0.3 != 0.3)
256 assert_equal(true, 0.4 != 0.3)
257 assert_equal(false, 0.1 != g:afloat)
258 assert_equal(true, g:afloat != 0.3)
259
260 assert_equal(false, 3.0 != 3)
261 assert_equal(false, 3 != 3.0)
262 assert_equal(true, 3.1 != 3)
263 assert_equal(true, 3 != 3.1)
264 endif
265
266 assert_equal(false, 'abc' != 'abc')
267 assert_equal(true, 'xyz' != 'abc')
268 assert_equal(false, g:astring != 'asdf')
269 assert_equal(true, 'xyz' != g:astring)
270
271 assert_equal(true, 'abc' != 'ABC')
272 set ignorecase
273 assert_equal(true, 'abc' != 'ABC')
274 set noignorecase
275
276 assert_equal(false, 0z3f != 0z3f)
277 assert_equal(true, 0z3f != 0z4f)
278 assert_equal(false, g:ablob != 0z01ab)
279 assert_equal(true, 0z3f != g:ablob)
280
281 assert_equal(false, [1, 2, 3] != [1, 2, 3])
282 assert_equal(true, [1, 2, 3] != [2, 3, 1])
283 assert_equal(false, [2, 3, 4] != g:alist)
284 assert_equal(true, g:alist != [2, 3, 1])
285 assert_equal(true, [1, 2, 3] != [])
286 assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
287
288 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
289 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
290 assert_equal(true, #{one: 1, two: 2} != #{two: 2})
291 assert_equal(true, #{one: 1, two: 2} != #{})
292 assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
293 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
294
295 assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal'))
296 assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is'))
297
298 assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123]))
299 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123]))
300 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999]))
301enddef
302
303" test > comperator
304def Test_expr4_greater()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100305 assert_true(2 > 0)
306 assert_true(2 > 1)
307 assert_false(2 > 2)
308 assert_false(2 > 3)
309 if has('float')
310 assert_true(2.0 > 0.0)
311 assert_true(2.0 > 1.0)
312 assert_false(2.0 > 2.0)
313 assert_false(2.0 > 3.0)
314 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100315enddef
316
317" test >= comperator
318def Test_expr4_greaterequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100319 assert_true(2 >= 0)
320 assert_true(2 >= 2)
321 assert_false(2 >= 3)
322 if has('float')
323 assert_true(2.0 >= 0.0)
324 assert_true(2.0 >= 2.0)
325 assert_false(2.0 >= 3.0)
326 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327enddef
328
329" test < comperator
330def Test_expr4_smaller()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100331 assert_false(2 < 0)
332 assert_false(2 < 2)
333 assert_true(2 < 3)
334 if has('float')
335 assert_false(2.0 < 0.0)
336 assert_false(2.0 < 2.0)
337 assert_true(2.0 < 3.0)
338 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339enddef
340
341" test <= comperator
342def Test_expr4_smallerequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100343 assert_false(2 <= 0)
344 assert_false(2 <= 1)
345 assert_true(2 <= 2)
346 assert_true(2 <= 3)
347 if has('float')
348 assert_false(2.0 <= 0.0)
349 assert_false(2.0 <= 1.0)
350 assert_true(2.0 <= 2.0)
351 assert_true(2.0 <= 3.0)
352 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353enddef
354
355" test =~ comperator
356def Test_expr4_match()
357 assert_equal(false, '2' =~ '0')
358 assert_equal(true, '2' =~ '[0-9]')
359enddef
360
361" test !~ comperator
362def Test_expr4_nomatch()
363 assert_equal(true, '2' !~ '0')
364 assert_equal(false, '2' !~ '[0-9]')
365enddef
366
367" test is comperator
368def Test_expr4_is()
369 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100370 assert_false(mylist is [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100372 assert_true(mylist is other)
373
374 let myblob = 0z1234
375 assert_false(myblob is 0z1234)
376 let otherblob = myblob
377 assert_true(myblob is otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378enddef
379
380" test isnot comperator
381def Test_expr4_isnot()
382 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100383 assert_true('2' isnot '0')
384 assert_true(mylist isnot [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 let other = mylist
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100386 assert_false(mylist isnot other)
387
388 let myblob = 0z1234
389 assert_true(myblob isnot 0z1234)
390 let otherblob = myblob
391 assert_false(myblob isnot otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392enddef
393
394def RetVoid()
395 let x = 1
396enddef
397
398func Test_expr4_fails()
399 let msg = "white space required before and after '>'"
400 call CheckDefFailure("let x = 1>2", msg)
401 call CheckDefFailure("let x = 1 >2", msg)
402 call CheckDefFailure("let x = 1> 2", msg)
403
404 let msg = "white space required before and after '=='"
405 call CheckDefFailure("let x = 1==2", msg)
406 call CheckDefFailure("let x = 1 ==2", msg)
407 call CheckDefFailure("let x = 1== 2", msg)
408
409 let msg = "white space required before and after 'is'"
410 call CheckDefFailure("let x = '1'is'2'", msg)
411 call CheckDefFailure("let x = '1' is'2'", msg)
412 call CheckDefFailure("let x = '1'is '2'", msg)
413
414 let msg = "white space required before and after 'isnot'"
415 call CheckDefFailure("let x = '1'isnot'2'", msg)
416 call CheckDefFailure("let x = '1' isnot'2'", msg)
417 call CheckDefFailure("let x = '1'isnot '2'", msg)
418
419 call CheckDefFailure("let x = 1 is# 2", 'E15:')
420 call CheckDefFailure("let x = 1 is? 2", 'E15:')
421 call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
422 call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
423
424 call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
425 call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
426 call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
427 call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
428
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 =~ false", 'Cannot compare bool with bool')
434 call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
435 call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
436 call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
437
438 call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
439 call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
440 call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
441 call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
442 if has('float')
443 call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
444 call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
445 endif
446
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 call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
452 call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
453
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')
458 call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
459 call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100460
461 call CheckDefFailureMult(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel')
462 call CheckDefFailureMult(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200463 call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func')
464 call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465endfunc
466
467" test addition, subtraction, concatenation
468def Test_expr5()
469 assert_equal(66, 60 + 6)
470 assert_equal(70, 60 + g:anint)
471 assert_equal(9, g:alsoint + 5)
472 assert_equal(14, g:alsoint + g:anint)
473
474 assert_equal(54, 60 - 6)
475 assert_equal(50, 60 - g:anint)
476 assert_equal(-1, g:alsoint - 5)
477 assert_equal(-6, g:alsoint - g:anint)
478
479 assert_equal('hello', 'hel' .. 'lo')
480 assert_equal('hello 123', 'hello ' .. 123)
481 assert_equal('123 hello', 123 .. ' hello')
482 assert_equal('123456', 123 .. 456)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100483
484 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
485 assert_equal(0z11223344, 0z1122 + 0z3344)
486 assert_equal(0z112201ab, 0z1122 + g:ablob)
487 assert_equal(0z01ab3344, g:ablob + 0z3344)
488 assert_equal(0z01ab01ab, g:ablob + g:ablob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489enddef
490
491def Test_expr5_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100492 if !has('float')
493 MissingFeature 'float'
494 else
495 assert_equal(66.0, 60.0 + 6.0)
496 assert_equal(66.0, 60.0 + 6)
497 assert_equal(66.0, 60 + 6.0)
498 assert_equal(5.1, g:afloat + 5)
499 assert_equal(8.1, 8 + g:afloat)
500 assert_equal(10.1, g:anint + g:afloat)
501 assert_equal(10.1, g:afloat + g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100503 assert_equal(54.0, 60.0 - 6.0)
504 assert_equal(54.0, 60.0 - 6)
505 assert_equal(54.0, 60 - 6.0)
506 assert_equal(-4.9, g:afloat - 5)
507 assert_equal(7.9, 8 - g:afloat)
508 assert_equal(9.9, g:anint - g:afloat)
509 assert_equal(-9.9, g:afloat - g:anint)
510 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511enddef
512
513func Test_expr5_fails()
514 let msg = "white space required before and after '+'"
515 call CheckDefFailure("let x = 1+2", msg)
516 call CheckDefFailure("let x = 1 +2", msg)
517 call CheckDefFailure("let x = 1+ 2", msg)
518
519 let msg = "white space required before and after '-'"
520 call CheckDefFailure("let x = 1-2", msg)
521 call CheckDefFailure("let x = 1 -2", msg)
522 call CheckDefFailure("let x = 1- 2", msg)
523
524 let msg = "white space required before and after '..'"
525 call CheckDefFailure("let x = '1'..'2'", msg)
526 call CheckDefFailure("let x = '1' ..'2'", msg)
527 call CheckDefFailure("let x = '1'.. '2'", msg)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100528
529 call CheckDefFailure("let x = 0z1122 + 33", 'E1035')
530 call CheckDefFailure("let x = 0z1122 + [3]", 'E1035')
531 call CheckDefFailure("let x = 0z1122 + 'asd'", 'E1035')
532 call CheckDefFailure("let x = 33 + 0z1122", 'E1035')
533 call CheckDefFailure("let x = [3] + 0z1122", 'E1035')
534 call CheckDefFailure("let x = 'asdf' + 0z1122", 'E1035')
Bram Moolenaar92dba362020-03-30 21:22:56 +0200535 call CheckDefFailure("let x = 6 + xxx", 'E1001')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536endfunc
537
538" test multiply, divide, modulo
539def Test_expr6()
540 assert_equal(36, 6 * 6)
541 assert_equal(24, 6 * g:alsoint)
542 assert_equal(24, g:alsoint * 6)
543 assert_equal(40, g:anint * g:alsoint)
544
545 assert_equal(10, 60 / 6)
546 assert_equal(6, 60 / g:anint)
547 assert_equal(1, g:anint / 6)
548 assert_equal(2, g:anint / g:alsoint)
549
550 assert_equal(5, 11 % 6)
551 assert_equal(4, g:anint % 6)
552 assert_equal(3, 13 % g:anint)
553 assert_equal(2, g:anint % g:alsoint)
554
555 assert_equal(4, 6 * 4 / 6)
Bram Moolenaarb13af502020-02-17 21:12:08 +0100556
557 let x = [2]
558 let y = [3]
559 assert_equal(5, x[0] + y[0])
560 assert_equal(6, x[0] * y[0])
561 if has('float')
562 let xf = [2.0]
563 let yf = [3.0]
564 assert_equal(5.0, xf[0] + yf[0])
565 assert_equal(6.0, xf[0] * yf[0])
566 endif
Bram Moolenaar92dba362020-03-30 21:22:56 +0200567
568 call CheckDefFailure("let x = 6 * xxx", 'E1001')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569enddef
570
571def Test_expr6_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100572 if !has('float')
573 MissingFeature 'float'
574 else
575 assert_equal(36.0, 6.0 * 6)
576 assert_equal(36.0, 6 * 6.0)
577 assert_equal(36.0, 6.0 * 6.0)
578 assert_equal(1.0, g:afloat * g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100580 assert_equal(10.0, 60 / 6.0)
581 assert_equal(10.0, 60.0 / 6)
582 assert_equal(10.0, 60.0 / 6.0)
583 assert_equal(0.01, g:afloat / g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100585 assert_equal(4.0, 6.0 * 4 / 6)
586 assert_equal(4.0, 6 * 4.0 / 6)
587 assert_equal(4.0, 6 * 4 / 6.0)
588 assert_equal(4.0, 6.0 * 4.0 / 6)
589 assert_equal(4.0, 6 * 4.0 / 6.0)
590 assert_equal(4.0, 6.0 * 4 / 6.0)
591 assert_equal(4.0, 6.0 * 4.0 / 6.0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100593 assert_equal(4.0, 6.0 * 4.0 / 6.0)
594 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595enddef
596
597func Test_expr6_fails()
598 let msg = "white space required before and after '*'"
599 call CheckDefFailure("let x = 1*2", msg)
600 call CheckDefFailure("let x = 1 *2", msg)
601 call CheckDefFailure("let x = 1* 2", msg)
602
603 let msg = "white space required before and after '/'"
604 call CheckDefFailure("let x = 1/2", msg)
605 call CheckDefFailure("let x = 1 /2", msg)
606 call CheckDefFailure("let x = 1/ 2", msg)
607
608 let msg = "white space required before and after '%'"
609 call CheckDefFailure("let x = 1%2", msg)
610 call CheckDefFailure("let x = 1 %2", msg)
611 call CheckDefFailure("let x = 1% 2", msg)
612
613 call CheckDefFailure("let x = '1' * '2'", 'E1036:')
614 call CheckDefFailure("let x = '1' / '2'", 'E1036:')
615 call CheckDefFailure("let x = '1' % '2'", 'E1035:')
616
617 call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
618 call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
619 call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
620
621 call CheckDefFailure("let x = [1] * [2]", 'E1036:')
622 call CheckDefFailure("let x = [1] / [2]", 'E1036:')
623 call CheckDefFailure("let x = [1] % [2]", 'E1035:')
624
625 call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
626 call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
627 call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
628
Bram Moolenaarb13af502020-02-17 21:12:08 +0100629 call CheckDefFailure("let x = 0xff[1]", 'E714:')
630 if has('float')
631 call CheckDefFailure("let x = 0.7[1]", 'E714:')
632 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633endfunc
634
635func Test_expr6_float_fails()
636 CheckFeature float
637 call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
638endfunc
639
640" define here to use old style parsing
641if has('float')
642 let g:float_zero = 0.0
643 let g:float_neg = -9.8
644 let g:float_big = 9.9e99
645endif
646let g:blob_empty = 0z
647let g:blob_one = 0z01
648let g:blob_long = 0z0102.0304
649
650let g:string_empty = ''
651let g:string_short = 'x'
652let g:string_long = 'abcdefghijklm'
653let g:string_special = "ab\ncd\ref\ekk"
654
655let g:special_true = v:true
656let g:special_false = v:false
657let g:special_null = v:null
658let g:special_none = v:none
659
660let g:list_empty = []
661let g:list_mixed = [1, 'b', v:false]
662
663let g:dict_empty = {}
664let g:dict_one = #{one: 1}
665
666let $TESTVAR = 'testvar'
667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668" test low level expression
669def Test_expr7_number()
670 " number constant
671 assert_equal(0, 0)
672 assert_equal(654, 0654)
673
674 assert_equal(6, 0x6)
675 assert_equal(15, 0xf)
676 assert_equal(255, 0xff)
677enddef
678
679def Test_expr7_float()
680 " float constant
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100681 if !has('float')
682 MissingFeature 'float'
683 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 assert_equal(g:float_zero, .0)
685 assert_equal(g:float_zero, 0.0)
686 assert_equal(g:float_neg, -9.8)
687 assert_equal(g:float_big, 9.9e99)
688 endif
689enddef
690
691def Test_expr7_blob()
692 " blob constant
693 assert_equal(g:blob_empty, 0z)
694 assert_equal(g:blob_one, 0z01)
695 assert_equal(g:blob_long, 0z0102.0304)
Bram Moolenaar92dba362020-03-30 21:22:56 +0200696
697 call CheckDefFailure("let x = 0z123", 'E973:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698enddef
699
700def Test_expr7_string()
701 " string constant
702 assert_equal(g:string_empty, '')
703 assert_equal(g:string_empty, "")
704 assert_equal(g:string_short, 'x')
705 assert_equal(g:string_short, "x")
706 assert_equal(g:string_long, 'abcdefghijklm')
707 assert_equal(g:string_long, "abcdefghijklm")
708 assert_equal(g:string_special, "ab\ncd\ref\ekk")
Bram Moolenaar92dba362020-03-30 21:22:56 +0200709
710 call CheckDefFailure('let x = "abc', 'E114:')
711 call CheckDefFailure("let x = 'abc", 'E115:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712enddef
713
714def Test_expr7_special()
715 " special constant
716 assert_equal(g:special_true, true)
717 assert_equal(g:special_false, false)
718 assert_equal(g:special_null, v:null)
719 assert_equal(g:special_none, v:none)
720enddef
721
722def Test_expr7_list()
723 " list
724 assert_equal(g:list_empty, [])
725 assert_equal(g:list_empty, [ ])
726 assert_equal(g:list_mixed, [1, 'b', false])
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200727 assert_equal('b', g:list_mixed[1])
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100728
729 call CheckDefExecFailure("let x = g:anint[3]", 'E714:')
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200730 call CheckDefFailure("let x = g:list_mixed[xxx]", 'E1001:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100731 call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:')
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200732 call CheckDefFailure("let x = g:list_mixed[0", 'E111:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100733 call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734enddef
735
736def Test_expr7_lambda()
737 " lambda
738 let La = { -> 'result'}
739 assert_equal('result', La())
740 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
741enddef
742
743def Test_expr7_dict()
744 " dictionary
745 assert_equal(g:dict_empty, {})
746 assert_equal(g:dict_empty, { })
747 assert_equal(g:dict_one, {'one': 1})
748 let key = 'one'
749 let val = 1
750 assert_equal(g:dict_one, {key: val})
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100751
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200752 call CheckDefFailure("let x = #{8: 8}", 'E1014:')
753 call CheckDefFailure("let x = #{xxx}", 'E720:')
754 call CheckDefFailure("let x = #{xxx: 1", 'E722:')
755 call CheckDefFailure("let x = #{xxx: 1,", 'E723:')
756 call CheckDefFailure("let x = {'a': xxx}", 'E1001:')
757 call CheckDefFailure("let x = {xxx: 8}", 'E1001:')
758 call CheckDefFailure("let x = #{a: 1, a: 2}", 'E721:')
Bram Moolenaar92dba362020-03-30 21:22:56 +0200759 call CheckDefFailure("let x = #", 'E1015:')
Bram Moolenaard25ec2c2020-03-30 21:05:45 +0200760 call CheckDefFailure("let x += 1", 'E1020:')
761 call CheckDefFailure("let x = x + 1", 'E1001:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100762 call CheckDefExecFailure("let x = g:anint.member", 'E715:')
763 call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764enddef
765
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200766def Test_expr_member()
767 assert_equal(1, g:dict_one.one)
768
769 call CheckDefFailure("let x = g:dict_one.#$!", 'E1002:')
770enddef
771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772def Test_expr7_option()
773 " option
774 set ts=11
775 assert_equal(11, &ts)
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100776 &ts = 9
777 assert_equal(9, &ts)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 set ts=8
779 set grepprg=some\ text
780 assert_equal('some text', &grepprg)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100781 &grepprg = test_null_string()
782 assert_equal('', &grepprg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 set grepprg&
784enddef
785
786def Test_expr7_environment()
787 " environment variable
788 assert_equal('testvar', $TESTVAR)
789 assert_equal('', $ASDF_ASD_XXX)
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200790
791 call CheckDefFailure("let x = $$$", 'E1002:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792enddef
793
794def Test_expr7_register()
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100795 @a = 'register a'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 assert_equal('register a', @a)
797enddef
798
799def Test_expr7_parens()
800 " (expr)
801 assert_equal(4, (6 * 4) / 6)
802 assert_equal(0, 6 * ( 4 / 6 ))
803
804 assert_equal(6, +6)
805 assert_equal(-6, -6)
806 assert_equal(6, --6)
807 assert_equal(6, -+-6)
808 assert_equal(-6, ---6)
809enddef
810
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200811def Test_expr7_negate()
812 assert_equal(-99, -99)
813 assert_equal(99, --99)
814 let nr = 88
815 assert_equal(-88, -nr)
816 assert_equal(88, --nr)
817enddef
818
819def Echo(arg): string
820 return arg
821enddef
822
823def s:EchoArg(arg): string
824 return arg
825enddef
826
827def Test_expr7_call()
828 assert_equal('yes', 'yes'->Echo())
829 assert_equal('yes', 'yes'->s:EchoArg())
830
831 call CheckDefFailure("let x = 'yes'->Echo", 'E107:')
832enddef
833
834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835def Test_expr7_not()
836 assert_equal(true, !'')
837 assert_equal(true, ![])
838 assert_equal(false, !'asdf')
839 assert_equal(false, ![2])
840 assert_equal(true, !!'asdf')
841 assert_equal(true, !![2])
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100842
843 assert_equal(true, !test_null_partial())
844 assert_equal(false, !{-> 'yes'})
845
846 assert_equal(true, !test_null_dict())
847 assert_equal(true, !{})
848 assert_equal(false, !{'yes': 'no'})
849
Bram Moolenaarb4d2cb12020-02-22 20:33:08 +0100850 if has('channel')
851 assert_equal(true, !test_null_job())
852 assert_equal(true, !test_null_channel())
853 endif
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100854
855 assert_equal(true, !test_null_blob())
856 assert_equal(true, !0z)
857 assert_equal(false, !0z01)
858
859 assert_equal(true, !test_void())
860 assert_equal(true, !test_unknown())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861enddef
862
863func Test_expr7_fails()
864 call CheckDefFailure("let x = (12", "E110:")
865
866 call CheckDefFailure("let x = -'xx'", "E1030:")
867 call CheckDefFailure("let x = +'xx'", "E1030:")
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200868 call CheckDefFailure("let x = -0z12", "E974:")
869 call CheckDefExecFailure("let x = -[8]", "E39:")
870 call CheckDefExecFailure("let x = -{'a': 1}", "E39:")
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871
872 call CheckDefFailure("let x = @", "E1002:")
873 call CheckDefFailure("let x = @<", "E354:")
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100874
Bram Moolenaaree619e52020-03-28 21:38:06 +0100875 call CheckDefFailure("let x = [1, 2", "E697:")
876 call CheckDefFailure("let x = [notfound]", "E1001:")
877
878 call CheckDefFailure("let x = { -> 123) }", "E451:")
879 call CheckDefFailure("let x = 123->{x -> x + 5) }", "E451:")
880
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100881 call CheckDefFailure("let x = &notexist", 'E113:')
Bram Moolenaara8c17702020-04-01 21:17:24 +0200882 call CheckDefFailure("&grepprg = [343]", 'E1013:')
Bram Moolenaarfd1823e2020-02-19 20:23:11 +0100883
884 call CheckDefExecFailure("echo s:doesnt_exist", 'E121:')
885 call CheckDefExecFailure("echo g:doesnt_exist", 'E121:')
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100886
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100887 call CheckDefFailure("echo a:somevar", 'E1075:')
888 call CheckDefFailure("echo l:somevar", 'E1075:')
889 call CheckDefFailure("echo x:somevar", 'E1075:')
890
891 " TODO
892 call CheckDefFailure("echo b:somevar", 'not supported yet')
893 call CheckDefFailure("echo w:somevar", 'not supported yet')
894 call CheckDefFailure("echo t:somevar", 'not supported yet')
895
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100896 call CheckDefExecFailure("let x = +g:astring", 'E1030:')
897 call CheckDefExecFailure("let x = +g:ablob", 'E974:')
898 call CheckDefExecFailure("let x = +g:alist", 'E745:')
899 call CheckDefExecFailure("let x = +g:adict", 'E728:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100900
901 call CheckDefFailureMult(["let x = ''", "let y = x.memb"], 'E715:')
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100902
903 call CheckDefExecFailure("[1, 2->len()", 'E492:')
904 call CheckDefExecFailure("#{a: 1->len()", 'E488:')
905 call CheckDefExecFailure("{'a': 1->len()", 'E492:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906endfunc
907
908let g:Funcrefs = [function('add')]
909
910func CallMe(arg)
911 return a:arg
912endfunc
913
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100914func CallMe2(one, two)
915 return a:one .. a:two
916endfunc
917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918def Test_expr7_trailing()
919 " user function call
920 assert_equal(123, CallMe(123))
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100921 assert_equal(123, CallMe( 123))
922 assert_equal(123, CallMe(123 ))
923 assert_equal('yesno', CallMe2('yes', 'no'))
924 assert_equal('yesno', CallMe2( 'yes', 'no' ))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 assert_equal('nothing', CallMe('nothing'))
926
927 " partial call
928 let Part = function('CallMe')
929 assert_equal('yes', Part('yes'))
930
931 " funcref call, using list index
932 let l = []
933 g:Funcrefs[0](l, 2)
934 assert_equal([2], l)
935
936 " method call
937 l = [2, 5, 6]
938 l->map({k, v -> k + v})
939 assert_equal([2, 6, 8], l)
940
941 " lambda method call
942 l = [2, 5]
943 l->{l -> add(l, 8)}()
944 assert_equal([2, 5, 8], l)
945
946 " dict member
947 let d = #{key: 123}
948 assert_equal(123, d.key)
949enddef
950
951func Test_expr7_trailing_fails()
952 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100953 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954endfunc
955
956func Test_expr_fails()
957 call CheckDefFailure("let x = '1'is2", 'E488:')
958 call CheckDefFailure("let x = '1'isnot2", 'E488:')
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100959
960 call CheckDefExecFailure("CallMe ('yes')", 'E492:')
961 call CheckDefFailure("CallMe2('yes','no')", 'E1069:')
962 call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:')
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100963
964 call CheckDefFailure("v:nosuch += 3", 'E1001:')
965 call CheckDefFailure("let v:version = 3", 'E1064:')
966 call CheckDefFailure("let asdf = v:nosuch", 'E1001:')
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100967
968 call CheckDefFailure("echo len('asdf'", 'E110:')
969 call CheckDefFailure("echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()", 'E1011:')
970 call CheckDefFailure("echo doesnotexist()", 'E117:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971endfunc