blob: 062502fe2bc7950c7b97923c3cb445dec8daceec [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')
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +020035 assert_equal('one', 1 ?
36 'one' :
37 'two')
Bram Moolenaar5feabe02020-01-30 18:24:53 +010038 if has('float')
39 assert_equal('one', 0.1 ? 'one' : 'two')
40 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010041 assert_equal('one', 'x' ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010042 assert_equal('one', 0z1234 ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010043 assert_equal('one', [0] ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010044 assert_equal('one', #{x: 0} ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045 let var = 1
46 assert_equal('one', var ? 'one' : 'two')
47
48 assert_equal('two', false ? 'one' : 'two')
49 assert_equal('two', 0 ? 'one' : 'two')
Bram Moolenaar7f829ca2020-01-31 22:12:41 +010050 if has('float')
51 assert_equal('two', 0.0 ? 'one' : 'two')
52 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010053 assert_equal('two', '' ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010054 assert_equal('two', 0z ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010055 assert_equal('two', [] ? 'one' : 'two')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +010056 assert_equal('two', {} ? 'one' : 'two')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010057 var = 0
58 assert_equal('two', var ? 'one' : 'two')
59enddef
60
61func Test_expr1_fails()
62 call CheckDefFailure("let x = 1 ? 'one'", "Missing ':' after '?'")
Bram Moolenaar9be61bb2020-03-30 22:51:24 +020063 call CheckDefFailure("let x = 1 ? 'one' : xxx", "E1001:")
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064
65 let msg = "white space required before and after '?'"
66 call CheckDefFailure("let x = 1? 'one' : 'two'", msg)
67 call CheckDefFailure("let x = 1 ?'one' : 'two'", msg)
68 call CheckDefFailure("let x = 1?'one' : 'two'", msg)
69
70 let msg = "white space required before and after ':'"
71 call CheckDefFailure("let x = 1 ? 'one': 'two'", msg)
72 call CheckDefFailure("let x = 1 ? 'one' :'two'", msg)
73 call CheckDefFailure("let x = 1 ? 'one':'two'", msg)
74endfunc
75
76" TODO: define inside test function
77def Record(val: any): any
78 g:vals->add(val)
79 return val
80enddef
81
82" test ||
83def Test_expr2()
84 assert_equal(2, 2 || 0)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +020085 assert_equal(7, 0 ||
86 0 ||
87 7)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 assert_equal(0, 0 || 0)
89 assert_equal('', 0 || '')
90
91 g:vals = []
92 assert_equal(3, Record(3) || Record(1))
93 assert_equal([3], g:vals)
94
95 g:vals = []
96 assert_equal(5, Record(0) || Record(5))
97 assert_equal([0, 5], g:vals)
98
99 g:vals = []
100 assert_equal(4, Record(0) || Record(4) || Record(0))
101 assert_equal([0, 4], g:vals)
102
103 g:vals = []
104 assert_equal(0, Record([]) || Record('') || Record(0))
105 assert_equal([[], '', 0], g:vals)
106enddef
107
108func Test_expr2_fails()
109 let msg = "white space required before and after '||'"
110 call CheckDefFailure("let x = 1||2", msg)
111 call CheckDefFailure("let x = 1 ||2", msg)
112 call CheckDefFailure("let x = 1|| 2", msg)
Bram Moolenaara8c17702020-04-01 21:17:24 +0200113
114 call CheckDefFailure("let x = 1 || xxx", 'E1001:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115endfunc
116
117" test &&
118def Test_expr3()
119 assert_equal(0, 2 && 0)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200120 assert_equal(0, 0 &&
121 0 &&
122 7)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100123 assert_equal(7, 2 && 3 && 7)
124 assert_equal(0, 0 && 0)
125 assert_equal(0, 0 && '')
126 assert_equal('', 8 && '')
127
128 g:vals = []
129 assert_equal(1, Record(3) && Record(1))
130 assert_equal([3, 1], g:vals)
131
132 g:vals = []
133 assert_equal(0, Record(0) && Record(5))
134 assert_equal([0], g:vals)
135
136 g:vals = []
137 assert_equal(0, Record(0) && Record(4) && Record(0))
138 assert_equal([0], g:vals)
139
140 g:vals = []
141 assert_equal(0, Record(8) && Record(4) && Record(0))
142 assert_equal([8, 4, 0], g:vals)
143
144 g:vals = []
145 assert_equal(0, Record([1]) && Record('z') && Record(0))
146 assert_equal([[1], 'z', 0], g:vals)
147enddef
148
149func Test_expr3_fails()
150 let msg = "white space required before and after '&&'"
151 call CheckDefFailure("let x = 1&&2", msg)
152 call CheckDefFailure("let x = 1 &&2", msg)
153 call CheckDefFailure("let x = 1&& 2", msg)
154endfunc
155
156let atrue = v:true
157let afalse = v:false
158let anone = v:none
159let anull = v:null
160let anint = 10
161let alsoint = 4
162if has('float')
163 let afloat = 0.1
164endif
165let astring = 'asdf'
166let ablob = 0z01ab
167let alist = [2, 3, 4]
168let adict = #{aaa: 2, bbb: 8}
169
170" test == comperator
171def Test_expr4_equal()
172 assert_equal(true, true == true)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200173 assert_equal(false, true ==
174 false)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 assert_equal(true, true == g:atrue)
176 assert_equal(false, g:atrue == false)
177
178 assert_equal(true, v:none == v:none)
179 assert_equal(false, v:none == v:null)
180 assert_equal(true, g:anone == v:none)
181 assert_equal(false, v:none == g:anull)
182
183 assert_equal(false, 2 == 0)
184 assert_equal(true, 61 == 61)
185 assert_equal(true, g:anint == 10)
186 assert_equal(false, 61 == g:anint)
187
188 if has('float')
189 assert_equal(true, 0.3 == 0.3)
190 assert_equal(false, 0.4 == 0.3)
191 assert_equal(true, 0.1 == g:afloat)
192 assert_equal(false, g:afloat == 0.3)
193
194 assert_equal(true, 3.0 == 3)
195 assert_equal(true, 3 == 3.0)
196 assert_equal(false, 3.1 == 3)
197 assert_equal(false, 3 == 3.1)
198 endif
199
200 assert_equal(true, 'abc' == 'abc')
201 assert_equal(false, 'xyz' == 'abc')
202 assert_equal(true, g:astring == 'asdf')
203 assert_equal(false, 'xyz' == g:astring)
204
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200205 assert_equal(false, 'abc' == 'aBc')
206 assert_equal(false, 'abc' ==# 'aBc')
207 assert_equal(true, 'abc' ==? 'aBc')
208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209 assert_equal(false, 'abc' == 'ABC')
210 set ignorecase
211 assert_equal(false, 'abc' == 'ABC')
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200212 assert_equal(false, 'abc' ==# 'ABC')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 set noignorecase
214
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200215 call CheckDefFailure("let x = 'a' == xxx", 'E1001:')
216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 assert_equal(true, 0z3f == 0z3f)
218 assert_equal(false, 0z3f == 0z4f)
219 assert_equal(true, g:ablob == 0z01ab)
220 assert_equal(false, 0z3f == g:ablob)
221
222 assert_equal(true, [1, 2, 3] == [1, 2, 3])
223 assert_equal(false, [1, 2, 3] == [2, 3, 1])
224 assert_equal(true, [2, 3, 4] == g:alist)
225 assert_equal(false, g:alist == [2, 3, 1])
226 assert_equal(false, [1, 2, 3] == [])
227 assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
228
229 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2})
230 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2})
231 assert_equal(false, #{one: 1, two: 2} == #{two: 2})
232 assert_equal(false, #{one: 1, two: 2} == #{})
233 assert_equal(true, g:adict == #{bbb: 8, aaa: 2})
234 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict)
235
236 assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal'))
237 assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is'))
238
239 assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123]))
240 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123]))
241 assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999]))
242enddef
243
244" test != comperator
245def Test_expr4_notequal()
246 assert_equal(false, true != true)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200247 assert_equal(true, true !=
248 false)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 assert_equal(false, true != g:atrue)
250 assert_equal(true, g:atrue != false)
251
252 assert_equal(false, v:none != v:none)
253 assert_equal(true, v:none != v:null)
254 assert_equal(false, g:anone != v:none)
255 assert_equal(true, v:none != g:anull)
256
257 assert_equal(true, 2 != 0)
258 assert_equal(false, 55 != 55)
259 assert_equal(false, g:anint != 10)
260 assert_equal(true, 61 != g:anint)
261
262 if has('float')
263 assert_equal(false, 0.3 != 0.3)
264 assert_equal(true, 0.4 != 0.3)
265 assert_equal(false, 0.1 != g:afloat)
266 assert_equal(true, g:afloat != 0.3)
267
268 assert_equal(false, 3.0 != 3)
269 assert_equal(false, 3 != 3.0)
270 assert_equal(true, 3.1 != 3)
271 assert_equal(true, 3 != 3.1)
272 endif
273
274 assert_equal(false, 'abc' != 'abc')
275 assert_equal(true, 'xyz' != 'abc')
276 assert_equal(false, g:astring != 'asdf')
277 assert_equal(true, 'xyz' != g:astring)
278
279 assert_equal(true, 'abc' != 'ABC')
280 set ignorecase
281 assert_equal(true, 'abc' != 'ABC')
282 set noignorecase
283
284 assert_equal(false, 0z3f != 0z3f)
285 assert_equal(true, 0z3f != 0z4f)
286 assert_equal(false, g:ablob != 0z01ab)
287 assert_equal(true, 0z3f != g:ablob)
288
289 assert_equal(false, [1, 2, 3] != [1, 2, 3])
290 assert_equal(true, [1, 2, 3] != [2, 3, 1])
291 assert_equal(false, [2, 3, 4] != g:alist)
292 assert_equal(true, g:alist != [2, 3, 1])
293 assert_equal(true, [1, 2, 3] != [])
294 assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
295
296 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2})
297 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2})
298 assert_equal(true, #{one: 1, two: 2} != #{two: 2})
299 assert_equal(true, #{one: 1, two: 2} != #{})
300 assert_equal(false, g:adict != #{bbb: 8, aaa: 2})
301 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict)
302
303 assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal'))
304 assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is'))
305
306 assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123]))
307 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123]))
308 assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999]))
309enddef
310
311" test > comperator
312def Test_expr4_greater()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100313 assert_true(2 > 0)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200314 assert_true(2 >
315 1)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100316 assert_false(2 > 2)
317 assert_false(2 > 3)
318 if has('float')
319 assert_true(2.0 > 0.0)
320 assert_true(2.0 > 1.0)
321 assert_false(2.0 > 2.0)
322 assert_false(2.0 > 3.0)
323 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324enddef
325
326" test >= comperator
327def Test_expr4_greaterequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100328 assert_true(2 >= 0)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200329 assert_true(2 >=
330 2)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100331 assert_false(2 >= 3)
332 if has('float')
333 assert_true(2.0 >= 0.0)
334 assert_true(2.0 >= 2.0)
335 assert_false(2.0 >= 3.0)
336 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337enddef
338
339" test < comperator
340def Test_expr4_smaller()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100341 assert_false(2 < 0)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200342 assert_false(2 <
343 2)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100344 assert_true(2 < 3)
345 if has('float')
346 assert_false(2.0 < 0.0)
347 assert_false(2.0 < 2.0)
348 assert_true(2.0 < 3.0)
349 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350enddef
351
352" test <= comperator
353def Test_expr4_smallerequal()
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100354 assert_false(2 <= 0)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200355 assert_false(2 <=
356 1)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100357 assert_true(2 <= 2)
358 assert_true(2 <= 3)
359 if has('float')
360 assert_false(2.0 <= 0.0)
361 assert_false(2.0 <= 1.0)
362 assert_true(2.0 <= 2.0)
363 assert_true(2.0 <= 3.0)
364 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365enddef
366
367" test =~ comperator
368def Test_expr4_match()
369 assert_equal(false, '2' =~ '0')
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200370 assert_equal(true, '2' =~
371 '[0-9]')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372enddef
373
374" test !~ comperator
375def Test_expr4_nomatch()
376 assert_equal(true, '2' !~ '0')
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200377 assert_equal(false, '2' !~
378 '[0-9]')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379enddef
380
381" test is comperator
382def Test_expr4_is()
383 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100384 assert_false(mylist is [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 let other = mylist
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200386 assert_true(mylist is
387 other)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100388
389 let myblob = 0z1234
390 assert_false(myblob is 0z1234)
391 let otherblob = myblob
392 assert_true(myblob is otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393enddef
394
395" test isnot comperator
396def Test_expr4_isnot()
397 let mylist = [2]
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100398 assert_true('2' isnot '0')
399 assert_true(mylist isnot [2])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 let other = mylist
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200401 assert_false(mylist isnot
402 other)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100403
404 let myblob = 0z1234
405 assert_true(myblob isnot 0z1234)
406 let otherblob = myblob
407 assert_false(myblob isnot otherblob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100408enddef
409
410def RetVoid()
411 let x = 1
412enddef
413
414func Test_expr4_fails()
415 let msg = "white space required before and after '>'"
416 call CheckDefFailure("let x = 1>2", msg)
417 call CheckDefFailure("let x = 1 >2", msg)
418 call CheckDefFailure("let x = 1> 2", msg)
419
420 let msg = "white space required before and after '=='"
421 call CheckDefFailure("let x = 1==2", msg)
422 call CheckDefFailure("let x = 1 ==2", msg)
423 call CheckDefFailure("let x = 1== 2", msg)
424
425 let msg = "white space required before and after 'is'"
426 call CheckDefFailure("let x = '1'is'2'", msg)
427 call CheckDefFailure("let x = '1' is'2'", msg)
428 call CheckDefFailure("let x = '1'is '2'", msg)
429
430 let msg = "white space required before and after 'isnot'"
431 call CheckDefFailure("let x = '1'isnot'2'", msg)
432 call CheckDefFailure("let x = '1' isnot'2'", msg)
433 call CheckDefFailure("let x = '1'isnot '2'", msg)
434
435 call CheckDefFailure("let x = 1 is# 2", 'E15:')
436 call CheckDefFailure("let x = 1 is? 2", 'E15:')
437 call CheckDefFailure("let x = 1 isnot# 2", 'E15:')
438 call CheckDefFailure("let x = 1 isnot? 2", 'E15:')
439
440 call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string')
441 call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number')
442 call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value')
443 call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number')
444
445 call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool')
446 call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool')
447 call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool')
448 call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool')
449 call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool')
450 call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool')
451 call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool')
452 call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool')
453
454 call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special')
455 call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special')
456 call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number')
457 call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number')
458 if has('float')
459 call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float')
460 call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float')
461 endif
462
463 call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob')
464 call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob')
465 call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob')
466 call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob')
467 call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob')
468 call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob')
469
470 call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list')
471 call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list')
472 call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list')
473 call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list')
474 call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list')
475 call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list')
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100476
477 call CheckDefFailureMult(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel')
478 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 +0200479 call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func')
480 call CheckDefFailureMult(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481endfunc
482
483" test addition, subtraction, concatenation
484def Test_expr5()
485 assert_equal(66, 60 + 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200486 assert_equal(70, 60 +
487 g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 assert_equal(9, g:alsoint + 5)
489 assert_equal(14, g:alsoint + g:anint)
490
491 assert_equal(54, 60 - 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200492 assert_equal(50, 60 -
493 g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 assert_equal(-1, g:alsoint - 5)
495 assert_equal(-6, g:alsoint - g:anint)
496
497 assert_equal('hello', 'hel' .. 'lo')
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200498 assert_equal('hello 123', 'hello ' ..
499 123)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 assert_equal('123 hello', 123 .. ' hello')
501 assert_equal('123456', 123 .. 456)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100502
503 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
504 assert_equal(0z11223344, 0z1122 + 0z3344)
505 assert_equal(0z112201ab, 0z1122 + g:ablob)
506 assert_equal(0z01ab3344, g:ablob + 0z3344)
507 assert_equal(0z01ab01ab, g:ablob + g:ablob)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508enddef
509
510def Test_expr5_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100511 if !has('float')
512 MissingFeature 'float'
513 else
514 assert_equal(66.0, 60.0 + 6.0)
515 assert_equal(66.0, 60.0 + 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200516 assert_equal(66.0, 60 +
517 6.0)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100518 assert_equal(5.1, g:afloat + 5)
519 assert_equal(8.1, 8 + g:afloat)
520 assert_equal(10.1, g:anint + g:afloat)
521 assert_equal(10.1, g:afloat + g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100523 assert_equal(54.0, 60.0 - 6.0)
524 assert_equal(54.0, 60.0 - 6)
525 assert_equal(54.0, 60 - 6.0)
526 assert_equal(-4.9, g:afloat - 5)
527 assert_equal(7.9, 8 - g:afloat)
528 assert_equal(9.9, g:anint - g:afloat)
529 assert_equal(-9.9, g:afloat - g:anint)
530 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531enddef
532
533func Test_expr5_fails()
534 let msg = "white space required before and after '+'"
535 call CheckDefFailure("let x = 1+2", msg)
536 call CheckDefFailure("let x = 1 +2", msg)
537 call CheckDefFailure("let x = 1+ 2", msg)
538
539 let msg = "white space required before and after '-'"
540 call CheckDefFailure("let x = 1-2", msg)
541 call CheckDefFailure("let x = 1 -2", msg)
542 call CheckDefFailure("let x = 1- 2", msg)
543
544 let msg = "white space required before and after '..'"
545 call CheckDefFailure("let x = '1'..'2'", msg)
546 call CheckDefFailure("let x = '1' ..'2'", msg)
547 call CheckDefFailure("let x = '1'.. '2'", msg)
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100548
549 call CheckDefFailure("let x = 0z1122 + 33", 'E1035')
550 call CheckDefFailure("let x = 0z1122 + [3]", 'E1035')
551 call CheckDefFailure("let x = 0z1122 + 'asd'", 'E1035')
552 call CheckDefFailure("let x = 33 + 0z1122", 'E1035')
553 call CheckDefFailure("let x = [3] + 0z1122", 'E1035')
554 call CheckDefFailure("let x = 'asdf' + 0z1122", 'E1035')
Bram Moolenaar92dba362020-03-30 21:22:56 +0200555 call CheckDefFailure("let x = 6 + xxx", 'E1001')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556endfunc
557
558" test multiply, divide, modulo
559def Test_expr6()
560 assert_equal(36, 6 * 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200561 assert_equal(24, 6 *
562 g:alsoint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 assert_equal(24, g:alsoint * 6)
564 assert_equal(40, g:anint * g:alsoint)
565
566 assert_equal(10, 60 / 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200567 assert_equal(6, 60 /
568 g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 assert_equal(1, g:anint / 6)
570 assert_equal(2, g:anint / g:alsoint)
571
572 assert_equal(5, 11 % 6)
573 assert_equal(4, g:anint % 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200574 assert_equal(3, 13 %
575 g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 assert_equal(2, g:anint % g:alsoint)
577
578 assert_equal(4, 6 * 4 / 6)
Bram Moolenaarb13af502020-02-17 21:12:08 +0100579
580 let x = [2]
581 let y = [3]
582 assert_equal(5, x[0] + y[0])
583 assert_equal(6, x[0] * y[0])
584 if has('float')
585 let xf = [2.0]
586 let yf = [3.0]
587 assert_equal(5.0, xf[0] + yf[0])
588 assert_equal(6.0, xf[0] * yf[0])
589 endif
Bram Moolenaar92dba362020-03-30 21:22:56 +0200590
591 call CheckDefFailure("let x = 6 * xxx", 'E1001')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592enddef
593
594def Test_expr6_float()
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100595 if !has('float')
596 MissingFeature 'float'
597 else
598 assert_equal(36.0, 6.0 * 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200599 assert_equal(36.0, 6 *
600 6.0)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100601 assert_equal(36.0, 6.0 * 6.0)
602 assert_equal(1.0, g:afloat * g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100604 assert_equal(10.0, 60 / 6.0)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200605 assert_equal(10.0, 60.0 /
606 6)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100607 assert_equal(10.0, 60.0 / 6.0)
608 assert_equal(0.01, g:afloat / g:anint)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100610 assert_equal(4.0, 6.0 * 4 / 6)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200611 assert_equal(4.0, 6 *
612 4.0 /
613 6)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100614 assert_equal(4.0, 6 * 4 / 6.0)
615 assert_equal(4.0, 6.0 * 4.0 / 6)
616 assert_equal(4.0, 6 * 4.0 / 6.0)
617 assert_equal(4.0, 6.0 * 4 / 6.0)
618 assert_equal(4.0, 6.0 * 4.0 / 6.0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100620 assert_equal(4.0, 6.0 * 4.0 / 6.0)
621 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622enddef
623
624func Test_expr6_fails()
625 let msg = "white space required before and after '*'"
626 call CheckDefFailure("let x = 1*2", msg)
627 call CheckDefFailure("let x = 1 *2", msg)
628 call CheckDefFailure("let x = 1* 2", msg)
629
630 let msg = "white space required before and after '/'"
631 call CheckDefFailure("let x = 1/2", msg)
632 call CheckDefFailure("let x = 1 /2", msg)
633 call CheckDefFailure("let x = 1/ 2", msg)
634
635 let msg = "white space required before and after '%'"
636 call CheckDefFailure("let x = 1%2", msg)
637 call CheckDefFailure("let x = 1 %2", msg)
638 call CheckDefFailure("let x = 1% 2", msg)
639
640 call CheckDefFailure("let x = '1' * '2'", 'E1036:')
641 call CheckDefFailure("let x = '1' / '2'", 'E1036:')
642 call CheckDefFailure("let x = '1' % '2'", 'E1035:')
643
644 call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:')
645 call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:')
646 call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:')
647
648 call CheckDefFailure("let x = [1] * [2]", 'E1036:')
649 call CheckDefFailure("let x = [1] / [2]", 'E1036:')
650 call CheckDefFailure("let x = [1] % [2]", 'E1035:')
651
652 call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:')
653 call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:')
654 call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:')
655
Bram Moolenaarb13af502020-02-17 21:12:08 +0100656 call CheckDefFailure("let x = 0xff[1]", 'E714:')
657 if has('float')
658 call CheckDefFailure("let x = 0.7[1]", 'E714:')
659 endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660endfunc
661
662func Test_expr6_float_fails()
663 CheckFeature float
664 call CheckDefFailure("let x = 1.0 % 2", 'E1035:')
665endfunc
666
667" define here to use old style parsing
668if has('float')
669 let g:float_zero = 0.0
670 let g:float_neg = -9.8
671 let g:float_big = 9.9e99
672endif
673let g:blob_empty = 0z
674let g:blob_one = 0z01
675let g:blob_long = 0z0102.0304
676
677let g:string_empty = ''
678let g:string_short = 'x'
679let g:string_long = 'abcdefghijklm'
680let g:string_special = "ab\ncd\ref\ekk"
681
682let g:special_true = v:true
683let g:special_false = v:false
684let g:special_null = v:null
685let g:special_none = v:none
686
687let g:list_empty = []
688let g:list_mixed = [1, 'b', v:false]
689
690let g:dict_empty = {}
691let g:dict_one = #{one: 1}
692
693let $TESTVAR = 'testvar'
694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695" test low level expression
696def Test_expr7_number()
697 " number constant
698 assert_equal(0, 0)
699 assert_equal(654, 0654)
700
701 assert_equal(6, 0x6)
702 assert_equal(15, 0xf)
703 assert_equal(255, 0xff)
704enddef
705
706def Test_expr7_float()
707 " float constant
Bram Moolenaar7f829ca2020-01-31 22:12:41 +0100708 if !has('float')
709 MissingFeature 'float'
710 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 assert_equal(g:float_zero, .0)
712 assert_equal(g:float_zero, 0.0)
713 assert_equal(g:float_neg, -9.8)
714 assert_equal(g:float_big, 9.9e99)
715 endif
716enddef
717
718def Test_expr7_blob()
719 " blob constant
720 assert_equal(g:blob_empty, 0z)
721 assert_equal(g:blob_one, 0z01)
722 assert_equal(g:blob_long, 0z0102.0304)
Bram Moolenaar92dba362020-03-30 21:22:56 +0200723
724 call CheckDefFailure("let x = 0z123", 'E973:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725enddef
726
727def Test_expr7_string()
728 " string constant
729 assert_equal(g:string_empty, '')
730 assert_equal(g:string_empty, "")
731 assert_equal(g:string_short, 'x')
732 assert_equal(g:string_short, "x")
733 assert_equal(g:string_long, 'abcdefghijklm')
734 assert_equal(g:string_long, "abcdefghijklm")
735 assert_equal(g:string_special, "ab\ncd\ref\ekk")
Bram Moolenaar92dba362020-03-30 21:22:56 +0200736
737 call CheckDefFailure('let x = "abc', 'E114:')
738 call CheckDefFailure("let x = 'abc", 'E115:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739enddef
740
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200741def Test_expr7_vimvar()
742 let old: list<string> = v:oldfiles
743 let compl: dict<any> = v:completed_item
744
745 call CheckDefFailure("let old: list<number> = v:oldfiles", 'E1013: type mismatch, expected list<number> but got list<string>')
746 call CheckDefFailure("let old: dict<number> = v:completed_item", 'E1013: type mismatch, expected dict<number> but got dict<any>')
747enddef
748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749def Test_expr7_special()
750 " special constant
751 assert_equal(g:special_true, true)
752 assert_equal(g:special_false, false)
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200753 assert_equal(g:special_true, v:true)
754 assert_equal(g:special_false, v:false)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 assert_equal(g:special_null, v:null)
756 assert_equal(g:special_none, v:none)
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200757
758 call CheckDefFailure('v:true = true', 'E46:')
759 call CheckDefFailure('v:true = false', 'E46:')
760 call CheckDefFailure('v:false = true', 'E46:')
761 call CheckDefFailure('v:null = 11', 'E46:')
762 call CheckDefFailure('v:none = 22', 'E46:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763enddef
764
765def Test_expr7_list()
766 " list
767 assert_equal(g:list_empty, [])
768 assert_equal(g:list_empty, [ ])
769 assert_equal(g:list_mixed, [1, 'b', false])
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200770 assert_equal('b', g:list_mixed[1])
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100771
772 call CheckDefExecFailure("let x = g:anint[3]", 'E714:')
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200773 call CheckDefFailure("let x = g:list_mixed[xxx]", 'E1001:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100774 call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:')
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200775 call CheckDefFailure("let x = g:list_mixed[0", 'E111:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100776 call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777enddef
778
779def Test_expr7_lambda()
780 " lambda
781 let La = { -> 'result'}
782 assert_equal('result', La())
783 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val}))
784enddef
785
786def Test_expr7_dict()
787 " dictionary
788 assert_equal(g:dict_empty, {})
789 assert_equal(g:dict_empty, { })
790 assert_equal(g:dict_one, {'one': 1})
791 let key = 'one'
792 let val = 1
793 assert_equal(g:dict_one, {key: val})
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100794
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200795 call CheckDefFailure("let x = #{8: 8}", 'E1014:')
796 call CheckDefFailure("let x = #{xxx}", 'E720:')
Bram Moolenaar82de4642020-04-12 18:02:06 +0200797 call CheckDefFailureMult(["let x = #{xxx: 1", "let y = 2"], 'E722:')
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200798 call CheckDefFailure("let x = #{xxx: 1,", 'E723:')
799 call CheckDefFailure("let x = {'a': xxx}", 'E1001:')
800 call CheckDefFailure("let x = {xxx: 8}", 'E1001:')
801 call CheckDefFailure("let x = #{a: 1, a: 2}", 'E721:')
Bram Moolenaar92dba362020-03-30 21:22:56 +0200802 call CheckDefFailure("let x = #", 'E1015:')
Bram Moolenaard25ec2c2020-03-30 21:05:45 +0200803 call CheckDefFailure("let x += 1", 'E1020:')
804 call CheckDefFailure("let x = x + 1", 'E1001:')
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100805 call CheckDefExecFailure("let x = g:anint.member", 'E715:')
806 call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807enddef
808
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200809def Test_expr_member()
810 assert_equal(1, g:dict_one.one)
811
812 call CheckDefFailure("let x = g:dict_one.#$!", 'E1002:')
813enddef
814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815def Test_expr7_option()
816 " option
817 set ts=11
818 assert_equal(11, &ts)
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100819 &ts = 9
820 assert_equal(9, &ts)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 set ts=8
822 set grepprg=some\ text
823 assert_equal('some text', &grepprg)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100824 &grepprg = test_null_string()
825 assert_equal('', &grepprg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 set grepprg&
827enddef
828
829def Test_expr7_environment()
830 " environment variable
831 assert_equal('testvar', $TESTVAR)
832 assert_equal('', $ASDF_ASD_XXX)
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200833
834 call CheckDefFailure("let x = $$$", 'E1002:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835enddef
836
837def Test_expr7_register()
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100838 @a = 'register a'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 assert_equal('register a', @a)
840enddef
841
842def Test_expr7_parens()
843 " (expr)
844 assert_equal(4, (6 * 4) / 6)
845 assert_equal(0, 6 * ( 4 / 6 ))
846
847 assert_equal(6, +6)
848 assert_equal(-6, -6)
849 assert_equal(6, --6)
850 assert_equal(6, -+-6)
851 assert_equal(-6, ---6)
852enddef
853
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +0200854def Test_expr7_negate()
855 assert_equal(-99, -99)
856 assert_equal(99, --99)
857 let nr = 88
858 assert_equal(-88, -nr)
859 assert_equal(88, --nr)
860enddef
861
862def Echo(arg): string
863 return arg
864enddef
865
866def s:EchoArg(arg): string
867 return arg
868enddef
869
870def Test_expr7_call()
871 assert_equal('yes', 'yes'->Echo())
872 assert_equal('yes', 'yes'->s:EchoArg())
873
874 call CheckDefFailure("let x = 'yes'->Echo", 'E107:')
875enddef
876
877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878def Test_expr7_not()
879 assert_equal(true, !'')
880 assert_equal(true, ![])
881 assert_equal(false, !'asdf')
882 assert_equal(false, ![2])
883 assert_equal(true, !!'asdf')
884 assert_equal(true, !![2])
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100885
886 assert_equal(true, !test_null_partial())
887 assert_equal(false, !{-> 'yes'})
888
889 assert_equal(true, !test_null_dict())
890 assert_equal(true, !{})
891 assert_equal(false, !{'yes': 'no'})
892
Bram Moolenaarb4d2cb12020-02-22 20:33:08 +0100893 if has('channel')
894 assert_equal(true, !test_null_job())
895 assert_equal(true, !test_null_channel())
896 endif
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100897
898 assert_equal(true, !test_null_blob())
899 assert_equal(true, !0z)
900 assert_equal(false, !0z01)
901
902 assert_equal(true, !test_void())
903 assert_equal(true, !test_unknown())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904enddef
905
906func Test_expr7_fails()
907 call CheckDefFailure("let x = (12", "E110:")
908
909 call CheckDefFailure("let x = -'xx'", "E1030:")
910 call CheckDefFailure("let x = +'xx'", "E1030:")
Bram Moolenaarc58164c2020-03-29 18:40:30 +0200911 call CheckDefFailure("let x = -0z12", "E974:")
912 call CheckDefExecFailure("let x = -[8]", "E39:")
913 call CheckDefExecFailure("let x = -{'a': 1}", "E39:")
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914
915 call CheckDefFailure("let x = @", "E1002:")
916 call CheckDefFailure("let x = @<", "E354:")
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100917
Bram Moolenaaree619e52020-03-28 21:38:06 +0100918 call CheckDefFailure("let x = [1, 2", "E697:")
919 call CheckDefFailure("let x = [notfound]", "E1001:")
920
921 call CheckDefFailure("let x = { -> 123) }", "E451:")
922 call CheckDefFailure("let x = 123->{x -> x + 5) }", "E451:")
923
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100924 call CheckDefFailure("let x = &notexist", 'E113:')
Bram Moolenaara8c17702020-04-01 21:17:24 +0200925 call CheckDefFailure("&grepprg = [343]", 'E1013:')
Bram Moolenaarfd1823e2020-02-19 20:23:11 +0100926
927 call CheckDefExecFailure("echo s:doesnt_exist", 'E121:')
928 call CheckDefExecFailure("echo g:doesnt_exist", 'E121:')
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100929
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100930 call CheckDefFailure("echo a:somevar", 'E1075:')
931 call CheckDefFailure("echo l:somevar", 'E1075:')
932 call CheckDefFailure("echo x:somevar", 'E1075:')
933
934 " TODO
935 call CheckDefFailure("echo b:somevar", 'not supported yet')
936 call CheckDefFailure("echo w:somevar", 'not supported yet')
937 call CheckDefFailure("echo t:somevar", 'not supported yet')
938
Bram Moolenaar09f28f42020-02-20 23:08:34 +0100939 call CheckDefExecFailure("let x = +g:astring", 'E1030:')
940 call CheckDefExecFailure("let x = +g:ablob", 'E974:')
941 call CheckDefExecFailure("let x = +g:alist", 'E745:')
942 call CheckDefExecFailure("let x = +g:adict", 'E728:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100943
944 call CheckDefFailureMult(["let x = ''", "let y = x.memb"], 'E715:')
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100945
946 call CheckDefExecFailure("[1, 2->len()", 'E492:')
947 call CheckDefExecFailure("#{a: 1->len()", 'E488:')
948 call CheckDefExecFailure("{'a': 1->len()", 'E492:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949endfunc
950
951let g:Funcrefs = [function('add')]
952
953func CallMe(arg)
954 return a:arg
955endfunc
956
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100957func CallMe2(one, two)
958 return a:one .. a:two
959endfunc
960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961def Test_expr7_trailing()
962 " user function call
963 assert_equal(123, CallMe(123))
Bram Moolenaar38a5f512020-02-19 12:40:39 +0100964 assert_equal(123, CallMe( 123))
965 assert_equal(123, CallMe(123 ))
966 assert_equal('yesno', CallMe2('yes', 'no'))
967 assert_equal('yesno', CallMe2( 'yes', 'no' ))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 assert_equal('nothing', CallMe('nothing'))
969
970 " partial call
971 let Part = function('CallMe')
972 assert_equal('yes', Part('yes'))
973
974 " funcref call, using list index
975 let l = []
976 g:Funcrefs[0](l, 2)
977 assert_equal([2], l)
978
979 " method call
980 l = [2, 5, 6]
981 l->map({k, v -> k + v})
982 assert_equal([2, 6, 8], l)
983
984 " lambda method call
985 l = [2, 5]
986 l->{l -> add(l, 8)}()
987 assert_equal([2, 5, 8], l)
988
989 " dict member
990 let d = #{key: 123}
991 assert_equal(123, d.key)
992enddef
993
994func Test_expr7_trailing_fails()
995 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100996 call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997endfunc
998
999func Test_expr_fails()
1000 call CheckDefFailure("let x = '1'is2", 'E488:')
1001 call CheckDefFailure("let x = '1'isnot2", 'E488:')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001002
1003 call CheckDefExecFailure("CallMe ('yes')", 'E492:')
1004 call CheckDefFailure("CallMe2('yes','no')", 'E1069:')
1005 call CheckDefFailure("CallMe2('yes' , 'no')", 'E1068:')
Bram Moolenaar0c2ca582020-02-25 22:58:29 +01001006
1007 call CheckDefFailure("v:nosuch += 3", 'E1001:')
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001008 call CheckDefFailure("let v:statusmsg = ''", 'E1064:')
Bram Moolenaar0c2ca582020-02-25 22:58:29 +01001009 call CheckDefFailure("let asdf = v:nosuch", 'E1001:')
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001010
1011 call CheckDefFailure("echo len('asdf'", 'E110:')
1012 call CheckDefFailure("echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()", 'E1011:')
1013 call CheckDefFailure("echo doesnotexist()", 'E117:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014endfunc