blob: c8b821e75102824d154f3c53bce94a4edc284ea8 [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
494 CheckDefAndScriptFailure2(["call Test ('text')"], 'E476:', 'E1068:')
495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200550enddef
551
Bram Moolenaarcef12702021-01-04 14:09:43 +0100552def FuncWithComment( # comment
553 a: number, #comment
554 b: bool, # comment
555 c: string) #comment
556 assert_equal(4, a)
557 assert_equal(true, b)
558 assert_equal('yes', c)
559enddef
560
561def Test_func_with_comments()
562 FuncWithComment(4, true, 'yes')
563
564 var lines =<< trim END
565 def Func(# comment
566 arg: string)
567 enddef
568 END
569 CheckScriptFailure(lines, 'E125:', 1)
570
571 lines =<< trim END
572 def Func(
573 arg: string# comment
574 )
575 enddef
576 END
577 CheckScriptFailure(lines, 'E475:', 2)
578
579 lines =<< trim END
580 def Func(
581 arg: string
582 )# comment
583 enddef
584 END
585 CheckScriptFailure(lines, 'E488:', 3)
586enddef
587
Bram Moolenaar04b12692020-05-04 23:24:44 +0200588def Test_nested_function()
589 def Nested(arg: string): string
590 return 'nested ' .. arg
591 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200592 Nested('function')->assert_equal('nested function')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200593
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200594 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
595 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
596
Bram Moolenaar04b12692020-05-04 23:24:44 +0200597 CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200598 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
599 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200600
Bram Moolenaar54021752020-12-06 18:50:36 +0100601 var lines =<< trim END
602 def Outer()
603 def Inner()
604 # comment
605 enddef
606 def Inner()
607 enddef
608 enddef
609 END
610 CheckDefFailure(lines, 'E1073:')
611
612 lines =<< trim END
613 def Outer()
614 def Inner()
615 # comment
616 enddef
617 def! Inner()
618 enddef
619 enddef
620 END
621 CheckDefFailure(lines, 'E1117:')
622
623 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100624 lines =<< trim END
625 vim9script
626 var thecount = 0
627 if true
628 def Test(): number
629 def TheFunc(): number
630 thecount += 1
631 return thecount
632 enddef
633 return TheFunc()
634 enddef
635 endif
636 defcompile
637 assert_equal(1, Test())
638 assert_equal(2, Test())
639 END
640 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100641
642 # also works when "thecount" is inside the "if" block
643 lines =<< trim END
644 vim9script
645 if true
646 var thecount = 0
647 def Test(): number
648 def TheFunc(): number
649 thecount += 1
650 return thecount
651 enddef
652 return TheFunc()
653 enddef
654 endif
655 defcompile
656 assert_equal(1, Test())
657 assert_equal(2, Test())
658 END
659 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200660
661 lines =<< trim END
662 vim9script
663 def Outer()
664 def Inner()
665 echo 'hello'
666 enddef burp
667 enddef
668 defcompile
669 END
670 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200671enddef
672
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100673def Test_not_nested_function()
674 echo printf('%d',
675 function('len')('xxx'))
676enddef
677
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200678func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200679 call MyDefaultArgs()->assert_equal('string')
680 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200681 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200682endfunc
683
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200684def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200685 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200686 vim9script
687 def Outer()
688 def g:Inner(): string
689 return 'inner'
690 enddef
691 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200692 defcompile
693 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200694 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200695 delfunc g:Inner
696 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200697 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200698 delfunc g:Inner
699 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200700 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200701 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200702 END
703 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200704
705 lines =<< trim END
706 vim9script
707 def Outer()
708 def g:Inner(): string
709 return 'inner'
710 enddef
711 enddef
712 defcompile
713 Outer()
714 Outer()
715 END
716 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100717 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200718
719 lines =<< trim END
720 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100721 def Outer()
722 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100723 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100724 enddef
725 g:Inner()
726 enddef
727 Outer()
728 END
729 CheckScriptSuccess(lines)
730 delfunc g:Inner
731
732 lines =<< trim END
733 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200734 def Func()
735 echo 'script'
736 enddef
737 def Outer()
738 def Func()
739 echo 'inner'
740 enddef
741 enddef
742 defcompile
743 END
744 CheckScriptFailure(lines, "E1073:")
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200745enddef
746
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100747def DefListAll()
748 def
749enddef
750
751def DefListOne()
752 def DefListOne
753enddef
754
755def DefListMatches()
756 def /DefList
757enddef
758
759def Test_nested_def_list()
760 var funcs = split(execute('call DefListAll()'), "\n")
761 assert_true(len(funcs) > 10)
762 assert_true(funcs->index('def DefListAll()') >= 0)
763
764 funcs = split(execute('call DefListOne()'), "\n")
765 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
766
767 funcs = split(execute('call DefListMatches()'), "\n")
768 assert_true(len(funcs) >= 3)
769 assert_true(funcs->index('def DefListAll()') >= 0)
770 assert_true(funcs->index('def DefListOne()') >= 0)
771 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100772
773 var lines =<< trim END
774 vim9script
775 def Func()
776 def +Func+
777 enddef
778 defcompile
779 END
780 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100781enddef
782
Bram Moolenaar333894b2020-08-01 18:53:07 +0200783def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200784 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200785 vim9script
786 def g:Func(): string
787 return 'global'
788 enddef
789 def Func(): string
790 return 'local'
791 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200792 g:Func()->assert_equal('global')
793 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100794 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200795 END
796 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200797
798 lines =<< trim END
799 vim9script
800 def g:Funcy()
801 echo 'funcy'
802 enddef
803 s:Funcy()
804 END
805 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200806enddef
807
Bram Moolenaar0f769812020-09-12 18:32:34 +0200808def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200809 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200810 vim9script
811 def g:Gfunc(): string
812 return 'global'
813 enddef
814 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200815 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200816 return Gfunc('testing')
817 enddef
818 g:Gfunc()->assert_equal('global')
819 AnotherFunc()->assert_equal(7)
820 delfunc g:Gfunc
821 END
822 CheckScriptSuccess(lines)
823
824 lines =<< trim END
825 vim9script
826 def g:Func(): string
827 return 'global'
828 enddef
829 def AnotherFunc()
830 g:Func = function('len')
831 enddef
832 AnotherFunc()
833 END
834 CheckScriptFailure(lines, 'E705:')
835 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200836
837 # global function is found without g: prefix
838 lines =<< trim END
839 vim9script
840 def g:Func(): string
841 return 'global'
842 enddef
843 def AnotherFunc(): string
844 return Func()
845 enddef
846 assert_equal('global', AnotherFunc())
847 delfunc g:Func
848 END
849 CheckScriptSuccess(lines)
850
851 lines =<< trim END
852 vim9script
853 def g:Func(): string
854 return 'global'
855 enddef
856 assert_equal('global', Func())
857 delfunc g:Func
858 END
859 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200860enddef
861
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200862func TakesOneArg(arg)
863 echo a:arg
864endfunc
865
866def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200867 CheckDefFailure(['TakesOneArg()'], 'E119:')
868 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
869 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
870 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200871
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200872 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200873 vim9script
874 def Func(s: string)
875 echo s
876 enddef
877 Func([])
878 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200879 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200880
881 lines =<< trim END
882 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100883 var name = 'piet'
884 def FuncOne(name: string)
885 echo nr
886 enddef
887 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100888 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100889
890 lines =<< trim END
891 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200892 def FuncOne(nr: number)
893 echo nr
894 enddef
895 def FuncTwo()
896 FuncOne()
897 enddef
898 defcompile
899 END
900 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200901 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200902 try
903 source Xscript
904 catch
905 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
906 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
907 didCatch = true
908 endtry
909 assert_true(didCatch)
910
911 lines =<< trim END
912 vim9script
913 def FuncOne(nr: number)
914 echo nr
915 enddef
916 def FuncTwo()
917 FuncOne(1, 2)
918 enddef
919 defcompile
920 END
921 writefile(lines, 'Xscript')
922 didCatch = false
923 try
924 source Xscript
925 catch
926 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
927 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
928 didCatch = true
929 endtry
930 assert_true(didCatch)
931
932 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200933enddef
934
Bram Moolenaar50824712020-12-20 21:10:17 +0100935def Test_call_funcref_wrong_args()
936 var head =<< trim END
937 vim9script
938 def Func3(a1: string, a2: number, a3: list<number>)
939 echo a1 .. a2 .. a3[0]
940 enddef
941 def Testme()
942 var funcMap: dict<func> = {func: Func3}
943 END
944 var tail =<< trim END
945 enddef
946 Testme()
947 END
948 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
949
950 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
951 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100952
953 var lines =<< trim END
954 vim9script
955 var Ref: func(number): any
956 Ref = (j) => !j
957 echo Ref(false)
958 END
959 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
960
961 lines =<< trim END
962 vim9script
963 var Ref: func(number): any
964 Ref = (j) => !j
965 call Ref(false)
966 END
967 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +0100968enddef
969
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100970def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +0200971 var lines =<< trim END
972 var Callback = (..._) => 'anything'
973 assert_equal('anything', Callback())
974 assert_equal('anything', Callback(1))
975 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +0200976
977 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +0200978 END
979 CheckDefAndScriptSuccess(lines)
980
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100981 CheckDefFailure(['echo ((i) => 0)()'],
982 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100983
Bram Moolenaar2a389082021-04-09 20:24:31 +0200984 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100985 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100986 echo Ref(1, 'x')
987 END
988 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +0100989
990 lines =<< trim END
991 var Ref: func(job, string, number)
992 Ref = (x, y) => 0
993 END
994 CheckDefAndScriptFailure(lines, 'E1012:')
995
996 lines =<< trim END
997 var Ref: func(job, string)
998 Ref = (x, y, z) => 0
999 END
1000 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001001
1002 lines =<< trim END
1003 var one = 1
1004 var l = [1, 2, 3]
1005 echo map(l, (one) => one)
1006 END
1007 CheckDefFailure(lines, 'E1167:')
1008 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1009
1010 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001011 var Ref: func(any, ?any): bool
1012 Ref = (_, y = 1) => false
1013 END
1014 CheckDefAndScriptFailure(lines, 'E1172:')
1015
1016 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001017 var a = 0
1018 var b = (a == 0 ? 1 : 2)
1019 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001020 var txt = 'a'
1021 b = (txt =~ 'x' ? 1 : 2)
1022 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001023 END
1024 CheckDefAndScriptSuccess(lines)
1025
1026 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001027 def ShadowLocal()
1028 var one = 1
1029 var l = [1, 2, 3]
1030 echo map(l, (one) => one)
1031 enddef
1032 END
1033 CheckDefFailure(lines, 'E1167:')
1034
1035 lines =<< trim END
1036 def Shadowarg(one: number)
1037 var l = [1, 2, 3]
1038 echo map(l, (one) => one)
1039 enddef
1040 END
1041 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001042
1043 lines =<< trim END
1044 echo ((a) => a)('aa', 'bb')
1045 END
1046 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001047
1048 lines =<< trim END
1049 echo 'aa'->((a) => a)('bb')
1050 END
1051 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1052 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001053enddef
1054
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001055def Test_lambda_line_nr()
1056 var lines =<< trim END
1057 vim9script
1058 # comment
1059 # comment
1060 var id = timer_start(1'000, (_) => 0)
1061 var out = execute('verbose ' .. timer_info(id)[0].callback
1062 ->string()
1063 ->substitute("('\\|')", ' ', 'g'))
1064 assert_match('Last set from .* line 4', out)
1065 END
1066 CheckScriptSuccess(lines)
1067enddef
1068
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001069def FilterWithCond(x: string, Cond: func(string): bool): bool
1070 return Cond(x)
1071enddef
1072
Bram Moolenaar0346b792021-01-31 22:18:29 +01001073def Test_lambda_return_type()
1074 var lines =<< trim END
1075 var Ref = (): => 123
1076 END
1077 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001078
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001079 # no space before the return type
1080 lines =<< trim END
1081 var Ref = (x):number => x + 1
1082 END
1083 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1084
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001085 # this works
1086 for x in ['foo', 'boo']
1087 echo FilterWithCond(x, (v) => v =~ '^b')
1088 endfor
1089
1090 # this fails
1091 lines =<< trim END
1092 echo FilterWithCond('foo', (v) => v .. '^b')
1093 END
1094 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001095
1096 lines =<< trim END
1097 var Lambda1 = (x) => {
1098 return x
1099 }
1100 assert_equal('asdf', Lambda1('asdf'))
1101 var Lambda2 = (x): string => {
1102 return x
1103 }
1104 assert_equal('foo', Lambda2('foo'))
1105 END
1106 CheckDefAndScriptSuccess(lines)
1107
1108 lines =<< trim END
1109 var Lambda = (x): string => {
1110 return x
1111 }
1112 echo Lambda(['foo'])
1113 END
1114 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001115enddef
1116
Bram Moolenaar709664c2020-12-12 14:33:41 +01001117def Test_lambda_uses_assigned_var()
1118 CheckDefSuccess([
1119 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001120 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001121enddef
1122
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001123def Test_pass_legacy_lambda_to_def_func()
1124 var lines =<< trim END
1125 vim9script
1126 func Foo()
1127 eval s:Bar({x -> 0})
1128 endfunc
1129 def Bar(y: any)
1130 enddef
1131 Foo()
1132 END
1133 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001134
1135 lines =<< trim END
1136 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001137 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001138 enddef
1139 legacy call g:TestFunc({-> 0})
1140 delfunc g:TestFunc
1141
1142 def g:TestFunc(f: func(number))
1143 enddef
1144 legacy call g:TestFunc({nr -> 0})
1145 delfunc g:TestFunc
1146 END
1147 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001148enddef
1149
Bram Moolenaar844fb642021-10-23 13:32:30 +01001150def Test_lambda_in_reduce_line_break()
1151 # this was using freed memory
1152 var lines =<< trim END
1153 vim9script
1154 const result: dict<number> =
1155 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1156 ->reduce((acc, val) => {
1157 if has_key(acc, val)
1158 acc[val] += 1
1159 return acc
1160 else
1161 acc[val] = 1
1162 return acc
1163 endif
1164 }, {})
1165 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1166 END
1167 CheckScriptSuccess(lines)
1168enddef
1169
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001170" Default arg and varargs
1171def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001172 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001173 for s in rest
1174 res ..= ',' .. s
1175 endfor
1176 return res
1177enddef
1178
1179def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001180 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001181 MyDefVarargs('one')->assert_equal('one,foo')
1182 MyDefVarargs('one', 'two')->assert_equal('one,two')
1183 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001184 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001185 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001186 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001187 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001188
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001189 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001190 vim9script
1191 def Func(...l: list<string>)
1192 echo l
1193 enddef
1194 Func('a', 'b', 'c')
1195 END
1196 CheckScriptSuccess(lines)
1197
1198 lines =<< trim END
1199 vim9script
1200 def Func(...l: list<string>)
1201 echo l
1202 enddef
1203 Func()
1204 END
1205 CheckScriptSuccess(lines)
1206
1207 lines =<< trim END
1208 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001209 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001210 echo l
1211 enddef
1212 Func(0)
1213 END
1214 CheckScriptSuccess(lines)
1215
1216 lines =<< trim END
1217 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001218 def Func(...l: any)
1219 echo l
1220 enddef
1221 Func(0)
1222 END
1223 CheckScriptFailure(lines, 'E1180:', 2)
1224
1225 lines =<< trim END
1226 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001227 def Func(..._l: list<string>)
1228 echo _l
1229 enddef
1230 Func('a', 'b', 'c')
1231 END
1232 CheckScriptSuccess(lines)
1233
1234 lines =<< trim END
1235 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001236 def Func(...l: list<string>)
1237 echo l
1238 enddef
1239 Func(1, 2, 3)
1240 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001241 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001242
1243 lines =<< trim END
1244 vim9script
1245 def Func(...l: list<string>)
1246 echo l
1247 enddef
1248 Func('a', 9)
1249 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001250 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001251
1252 lines =<< trim END
1253 vim9script
1254 def Func(...l: list<string>)
1255 echo l
1256 enddef
1257 Func(1, 'a')
1258 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001259 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001260
1261 lines =<< trim END
1262 vim9script
1263 def Func( # some comment
1264 ...l = []
1265 )
1266 echo l
1267 enddef
1268 END
1269 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001270
1271 lines =<< trim END
1272 vim9script
1273 def DoIt()
1274 g:Later('')
1275 enddef
1276 defcompile
1277 def g:Later(...l: list<number>)
1278 enddef
1279 DoIt()
1280 END
1281 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001282enddef
1283
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001284let s:value = ''
1285
1286def FuncOneDefArg(opt = 'text')
1287 s:value = opt
1288enddef
1289
1290def FuncTwoDefArg(nr = 123, opt = 'text'): string
1291 return nr .. opt
1292enddef
1293
1294def FuncVarargs(...arg: list<string>): string
1295 return join(arg, ',')
1296enddef
1297
1298def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001299 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001300 RefDefArg = FuncOneDefArg
1301 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001302 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001303 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001304 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001305
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001306 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001307 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001308 RefDef2Arg()->assert_equal('123text')
1309 RefDef2Arg(99)->assert_equal('99text')
1310 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001311
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001312 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1313 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001314
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001315 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001316 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001317 RefVarargs()->assert_equal('')
1318 RefVarargs('one')->assert_equal('one')
1319 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001320
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001321 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1322 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001323enddef
1324
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001325" Only varargs
1326def MyVarargsOnly(...args: list<string>): string
1327 return join(args, ',')
1328enddef
1329
1330def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001331 MyVarargsOnly()->assert_equal('')
1332 MyVarargsOnly('one')->assert_equal('one')
1333 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001334 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1335 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001336enddef
1337
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001338def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001339 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001340 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001341 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001342enddef
1343
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001344def DictArg(arg: dict<string>)
1345 arg['key'] = 'value'
1346enddef
1347
1348def ListArg(arg: list<string>)
1349 arg[0] = 'value'
1350enddef
1351
1352def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001353 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001354 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001355 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001356 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001357 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001358 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001359 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001360
Bram Moolenaard2c61702020-09-06 15:58:36 +02001361 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001362 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001363enddef
1364
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001365" These argument names are reserved in legacy functions.
1366def WithReservedNames(firstline: string, lastline: string): string
1367 return firstline .. lastline
1368enddef
1369
1370def Test_argument_names()
1371 assert_equal('OK', WithReservedNames('O', 'K'))
1372enddef
1373
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001374def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001375 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001376 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001377enddef
1378
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001379func DefinedLater(arg)
1380 return a:arg
1381endfunc
1382
1383def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001384 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001385 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
1386 assert_fails('g:NotAFunc()', 'E117:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001387
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001388 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001389 vim9script
1390 def RetNumber(): number
1391 return 123
1392 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001393 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001394 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001395 END
1396 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001397
1398 lines =<< trim END
1399 vim9script
1400 def RetNumber(): number
1401 return 123
1402 enddef
1403 def Bar(F: func: number): number
1404 return F()
1405 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001406 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001407 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001408 END
1409 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001410
1411 lines =<< trim END
1412 vim9script
1413 def UseNumber(nr: number)
1414 echo nr
1415 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001416 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001417 Funcref(123)
1418 END
1419 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001420
1421 lines =<< trim END
1422 vim9script
1423 def UseNumber(nr: number)
1424 echo nr
1425 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001426 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001427 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001428 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001429
1430 lines =<< trim END
1431 vim9script
1432 def EchoNr(nr = 34)
1433 g:echo = nr
1434 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001435 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001436 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001437 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001438 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001439 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001440 END
1441 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001442
1443 lines =<< trim END
1444 vim9script
1445 def EchoList(...l: list<number>)
1446 g:echo = l
1447 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001448 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001449 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001450 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001451 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001452 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001453 END
1454 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001455
1456 lines =<< trim END
1457 vim9script
1458 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1459 g:optarg = opt
1460 g:listarg = l
1461 return nr
1462 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001463 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001464 Funcref(10)->assert_equal(10)
1465 g:optarg->assert_equal(12)
1466 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001467
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001468 Funcref(11, 22)->assert_equal(11)
1469 g:optarg->assert_equal(22)
1470 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001471
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001472 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1473 g:optarg->assert_equal(18)
1474 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001475 END
1476 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001477enddef
1478
1479let SomeFunc = function('len')
1480let NotAFunc = 'text'
1481
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001482def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001483 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001484 var Ref1: func(bool): string
1485 var Ref2: func(bool): number
1486 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001487 Ref3 = g:cond ? Ref1 : Ref2
1488
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001489 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001490 var Refa1: func(bool): number
1491 var Refa2: func(bool, number): number
1492 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001493 Refa3 = g:cond ? Refa1 : Refa2
1494
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001495 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001496 var Refb1: func(bool, string): number
1497 var Refb2: func(string, number): number
1498 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001499 Refb3 = g:cond ? Refb1 : Refb2
1500enddef
1501
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001502def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001503 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001504enddef
1505
1506def DefinedEvenLater(arg: string): string
1507 return arg
1508enddef
1509
1510def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001511 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001512 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001513enddef
1514
1515def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001516 CheckScriptFailure([
1517 'def Func(): number',
1518 'return "a"',
1519 'enddef',
1520 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001521 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001522 CheckScriptFailure([
1523 'def Func(): string',
1524 'return 1',
1525 'enddef',
1526 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001527 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001528 CheckScriptFailure([
1529 'def Func(): void',
1530 'return "a"',
1531 'enddef',
1532 'defcompile'],
1533 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001534 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001535 CheckScriptFailure([
1536 'def Func()',
1537 'return "a"',
1538 'enddef',
1539 'defcompile'],
1540 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001541 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001542
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001543 CheckScriptFailure([
1544 'def Func(): number',
1545 'return',
1546 'enddef',
1547 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001548 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001549
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001550 CheckScriptFailure([
1551 'def Func():number',
1552 'return 123',
1553 'enddef',
1554 'defcompile'], 'E1069:')
1555 delfunc! g:Func
1556
1557 CheckScriptFailure([
1558 'def Func() :number',
1559 'return 123',
1560 'enddef',
1561 'defcompile'], 'E1059:')
1562 delfunc! g:Func
1563
1564 CheckScriptFailure([
1565 'def Func() : number',
1566 'return 123',
1567 'enddef',
1568 'defcompile'], 'E1059:')
1569 delfunc! g:Func
1570
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001571 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001572 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001573 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001574 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001575 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001576 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001577
1578 CheckScriptFailure([
1579 'vim9script',
1580 'def FuncB()',
1581 ' return 123',
1582 'enddef',
1583 'def FuncA()',
1584 ' FuncB()',
1585 'enddef',
1586 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001587enddef
1588
1589def Test_arg_type_wrong()
1590 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001591 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001592 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001593 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001594 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1595 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001596enddef
1597
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001598def Test_white_space_before_comma()
1599 var lines =<< trim END
1600 vim9script
1601 def Func(a: number , b: number)
1602 enddef
1603 END
1604 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001605 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001606enddef
1607
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001608def Test_white_space_after_comma()
1609 var lines =<< trim END
1610 vim9script
1611 def Func(a: number,b: number)
1612 enddef
1613 END
1614 CheckScriptFailure(lines, 'E1069:')
1615
1616 # OK in legacy function
1617 lines =<< trim END
1618 vim9script
1619 func Func(a,b)
1620 endfunc
1621 END
1622 CheckScriptSuccess(lines)
1623enddef
1624
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001625def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001626 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001627 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001628 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001629 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001630 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001631 enddef
1632 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001633 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001634
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001635 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001636 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001637 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001638
Bram Moolenaar67979662020-06-20 22:50:47 +02001639 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001640 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001641 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001642
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001643 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001644 def ListFunc(arg: list<number>)
1645 listvar = arg
1646 enddef
1647 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001648 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001649
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001650 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001651 def DictFunc(arg: dict<number>)
1652 dictvar = arg
1653 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001654 {a: 1, b: 2}->DictFunc()
1655 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001656 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001657 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001658 enddef
1659 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001660 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001661
Bram Moolenaare0de1712020-12-02 17:36:54 +01001662 {a: 3, b: 4}->DictFunc()
1663 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001664
1665 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001666 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001667 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001668 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001669
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001670 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001671 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001672 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001673 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001674 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001675 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001676
1677 def UseString()
1678 'xyork'->MyFunc()
1679 enddef
1680 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001681 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001682
Bram Moolenaar10409562020-07-29 20:00:38 +02001683 def UseString2()
1684 "knife"->MyFunc()
1685 enddef
1686 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001687 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001688
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001689 # prepending a colon makes it a mark
1690 new
1691 setline(1, ['aaa', 'bbb', 'ccc'])
1692 normal! 3Gmt1G
1693 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001694 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001695 bwipe!
1696
Bram Moolenaare6b53242020-07-01 17:28:33 +02001697 MyFunc(
1698 'continued'
1699 )
1700 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001701 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001702 )
1703
1704 call MyFunc(
1705 'more'
1706 ..
1707 'lines'
1708 )
1709 assert_equal(
1710 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001711 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001712 END
1713 writefile(lines, 'Xcall.vim')
1714 source Xcall.vim
1715 delete('Xcall.vim')
1716enddef
1717
1718def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001719 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001720 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001721 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001722 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001723 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001724 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001725 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001726 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001727 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001728enddef
1729
Bram Moolenaar65b95452020-07-19 14:03:09 +02001730def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001731 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001732 vim9script
1733 def MyFunc(arg: string)
1734 echo arg
1735 enddef
1736 MyFunc(1234)
1737 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001738 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001739enddef
1740
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001741def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001742 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001743 vim9script
1744 const var = ''
1745 def MyFunc(arg: string)
1746 var = 'asdf'
1747 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001748 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001749 END
1750 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001751 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001752 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001753
1754 lines =<< trim END
1755 const g:Aconst = 77
1756 def Change()
1757 # comment
1758 g:Aconst = 99
1759 enddef
1760 call Change()
1761 unlet g:Aconst
1762 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001763 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001764enddef
1765
1766" Test that inside :function a Python function can be defined, :def is not
1767" recognized.
1768func Test_function_python()
1769 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001770 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001771 execute py "<< EOF"
1772def do_something():
1773 return 1
1774EOF
1775endfunc
1776
1777def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001778 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001779 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001780 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001781 echo 'hello'
1782 enddef
1783
1784 def CallGoneSoon()
1785 GoneSoon()
1786 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001787 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001788
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001789 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001790 CallGoneSoon()
1791 END
1792 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001793 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1794 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001795
1796 delete('XToDelFunc')
1797enddef
1798
1799def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001800 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001801 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001802 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001803 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001804 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001805 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001806 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001807 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001808 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001809
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001810 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001811 g:Func1()->assert_equal('Func1')
1812 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001813
1814 delfunc! Func0
1815 delfunc! Func1
1816 delfunc! Func2
1817enddef
1818
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001819def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001820 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001821 vim9script
1822 func Func(arg)
1823 echo a:arg
1824 endfunc
1825 Func('text')
1826 END
1827 writefile(lines, 'XVim9Func')
1828 so XVim9Func
1829
1830 delete('XVim9Func')
1831enddef
1832
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833let s:funcResult = 0
1834
1835def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001836 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001837enddef
1838
1839def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001840 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001841 return 1234
1842enddef
1843
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001844def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001845 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001846 return 'text'
1847enddef
1848
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001849def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001850 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001851enddef
1852
1853def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001854 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001855 return arg
1856enddef
1857
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001858def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001859 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001860enddef
1861
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001862def FuncOneArgRetString(arg: string): string
1863 return arg
1864enddef
1865
Bram Moolenaar89228602020-04-05 22:14:54 +02001866def FuncOneArgRetAny(arg: any): any
1867 return arg
1868enddef
1869
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001871 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001872 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001873 Ref1 = FuncNoArgNoRet
1874 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001875 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001876
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001877 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001878 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001879 Ref2 = FuncNoArgNoRet
1880 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001881 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001882
Bram Moolenaar53900992020-08-22 19:02:02 +02001883 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001884 Ref2 = FuncOneArgNoRet
1885 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001886 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001887
Bram Moolenaar53900992020-08-22 19:02:02 +02001888 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001889 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001890 Ref2()->assert_equal(1234)
1891 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001892
Bram Moolenaar53900992020-08-22 19:02:02 +02001893 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001894 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001895 Ref2(13)->assert_equal(13)
1896 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897enddef
1898
Bram Moolenaar9978d472020-07-05 16:01:56 +02001899def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001900 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02001901 for n in repeat([1], 3)
1902 res += n
1903 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001904 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02001905
1906 res = 0
1907 for n in add([1, 2], 3)
1908 res += n
1909 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001910 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02001911enddef
1912
Bram Moolenaar846178a2020-07-05 17:04:13 +02001913def Test_argv_return_type()
1914 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001915 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02001916 for name in argv()
1917 res ..= name
1918 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001919 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02001920enddef
1921
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001922def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001923 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001924 RefVoid = FuncNoArgNoRet
1925 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001926 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
1927 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001928
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001929 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001930 RefAny = FuncNoArgRetNumber
1931 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001932 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
1933 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001934
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001935 var RefAnyNoArgs: func: any = RefAny
1936
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001937 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001938 RefNr = FuncNoArgRetNumber
1939 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001940 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
1941 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001942
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001943 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001944 RefStr = FuncNoArgRetString
1945 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001946 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
1947 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001948enddef
1949
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001950def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001951 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001952
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001953 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
1954 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
1955 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
1956 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
1957 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
1958 CheckDefFailure(['var Ref1: func(...bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(...bool) but got func(bool, number)')
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001959
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001960 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
1961 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
1962 CheckDefFailure(['var RefWrong: func(bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool)'], 'E1005:')
1963 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001964enddef
1965
Bram Moolenaar89228602020-04-05 22:14:54 +02001966def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001967 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02001968 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001969 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02001970
1971 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001972 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02001973
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001974 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02001975 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001976 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02001977
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001978 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02001979enddef
1980
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001981def Test_func_common_type()
1982 def FuncOne(n: number): number
1983 return n
1984 enddef
1985 def FuncTwo(s: string): number
1986 return len(s)
1987 enddef
1988 def FuncThree(n: number, s: string): number
1989 return n + len(s)
1990 enddef
1991 var list = [FuncOne, FuncTwo, FuncThree]
1992 assert_equal(8, list[0](8))
1993 assert_equal(4, list[1]('word'))
1994 assert_equal(7, list[2](3, 'word'))
1995enddef
1996
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001997def MultiLine(
1998 arg1: string,
1999 arg2 = 1234,
2000 ...rest: list<string>
2001 ): string
2002 return arg1 .. arg2 .. join(rest, '-')
2003enddef
2004
Bram Moolenaar2c330432020-04-13 14:41:35 +02002005def MultiLineComment(
2006 arg1: string, # comment
2007 arg2 = 1234, # comment
2008 ...rest: list<string> # comment
2009 ): string # comment
2010 return arg1 .. arg2 .. join(rest, '-')
2011enddef
2012
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002013def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002014 MultiLine('text')->assert_equal('text1234')
2015 MultiLine('text', 777)->assert_equal('text777')
2016 MultiLine('text', 777, 'one')->assert_equal('text777one')
2017 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002018enddef
2019
Bram Moolenaar23e03252020-04-12 22:22:31 +02002020func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002021 call MultiLine('text')->assert_equal('text1234')
2022 call MultiLine('text', 777)->assert_equal('text777')
2023 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2024 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002025endfunc
2026
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002027
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002028" When using CheckScriptFailure() for the below test, E1010 is generated instead
2029" of E1056.
2030func Test_E1056_1059()
2031 let caught_1056 = 0
2032 try
2033 def F():
2034 return 1
2035 enddef
2036 catch /E1056:/
2037 let caught_1056 = 1
2038 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002039 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002040
2041 let caught_1059 = 0
2042 try
2043 def F5(items : list)
2044 echo 'a'
2045 enddef
2046 catch /E1059:/
2047 let caught_1059 = 1
2048 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002049 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002050endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002051
Bram Moolenaar015f4262020-05-05 21:25:22 +02002052func DelMe()
2053 echo 'DelMe'
2054endfunc
2055
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002056def Test_error_reporting()
2057 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002058 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002059 " comment
2060 def Func()
2061 # comment
2062 # comment
2063 invalid
2064 enddef
2065 defcompile
2066 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002067 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002068 try
2069 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002070 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002071 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002072 v:exception->assert_match('Invalid command: invalid')
2073 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002074 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002075 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002076
2077 # comment lines after the start of the function
2078 lines =<< trim END
2079 " comment
2080 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002081 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002082 # comment
2083 # comment
2084 invalid
2085 enddef
2086 defcompile
2087 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002088 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002089 try
2090 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002091 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002092 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002093 v:exception->assert_match('Invalid command: invalid')
2094 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002095 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002096 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002097
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002098 lines =<< trim END
2099 vim9script
2100 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002101 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002102 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002103 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002104 enddef
2105 defcompile
2106 Func()
2107 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002108 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002109 try
2110 source Xdef
2111 assert_report('should have failed')
2112 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002113 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002114 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002115 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002116
Bram Moolenaar08052222020-09-14 17:04:31 +02002117 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002118enddef
2119
Bram Moolenaar015f4262020-05-05 21:25:22 +02002120def Test_deleted_function()
2121 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002122 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002123 'delfunc g:DelMe',
2124 'echo RefMe()'], 'E117:')
2125enddef
2126
2127def Test_unknown_function()
2128 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002129 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002130 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002131enddef
2132
Bram Moolenaar328eac22021-01-07 19:23:08 +01002133def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002134 return Ref('more')
2135enddef
2136
2137def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002138 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002139 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002140enddef
2141
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002142def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002143 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002144 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002145enddef
2146
2147def Test_closure_ref_after_return()
2148 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002149 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002150 unlet g:Ref
2151enddef
2152
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002153def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002154 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002155 g:Extend = (s) => local->add(s)
2156 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002157enddef
2158
2159def Test_closure_two_refs()
2160 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002161 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002162 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002163 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002164 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002165 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002166
2167 unlet g:Extend
2168 unlet g:Read
2169enddef
2170
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002171def ReadRef(Ref: func(): list<string>): string
2172 return join(Ref(), ' ')
2173enddef
2174
Bram Moolenaar5e654232020-09-16 15:22:00 +02002175def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002176 Ref(add)
2177enddef
2178
2179def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002180 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002181 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002182 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002183 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002184 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002185 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002186
2187 unlet g:Extend
2188 unlet g:Read
2189enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002190
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002191def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002192 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002193 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002194enddef
2195
2196def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002197 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002198 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002199enddef
2200
2201def Test_closure_using_argument()
2202 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002203 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002204
2205 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002206 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002207
2208 unlet g:UseArg
2209 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002210
2211 var lines =<< trim END
2212 vim9script
2213 def Test(Fun: func(number): number): list<number>
2214 return map([1, 2, 3], (_, i) => Fun(i))
2215 enddef
2216 def Inc(nr: number): number
2217 return nr + 2
2218 enddef
2219 assert_equal([3, 4, 5], Test(Inc))
2220 END
2221 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002222enddef
2223
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002224def MakeGetAndAppendRefs()
2225 var local = 'a'
2226
2227 def Append(arg: string)
2228 local ..= arg
2229 enddef
2230 g:Append = Append
2231
2232 def Get(): string
2233 return local
2234 enddef
2235 g:Get = Get
2236enddef
2237
2238def Test_closure_append_get()
2239 MakeGetAndAppendRefs()
2240 g:Get()->assert_equal('a')
2241 g:Append('-b')
2242 g:Get()->assert_equal('a-b')
2243 g:Append('-c')
2244 g:Get()->assert_equal('a-b-c')
2245
2246 unlet g:Append
2247 unlet g:Get
2248enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002249
Bram Moolenaar04b12692020-05-04 23:24:44 +02002250def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002251 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002252 def Closure(arg: string): string
2253 return local .. arg
2254 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002255 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002256enddef
2257
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002258func GetResult(Ref)
2259 return a:Ref('some')
2260endfunc
2261
2262def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002263 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002264 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002265 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002266enddef
2267
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002268def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002269 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002270 vim9script
2271 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002272 var name = 0
2273 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002274 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002275 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002276 enddef
2277 Func()
2278 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002279 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002280enddef
2281
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002282def Test_nested_closure_used()
2283 var lines =<< trim END
2284 vim9script
2285 def Func()
2286 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002287 var Closure = () => x
2288 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002289 enddef
2290 Func()
2291 assert_equal('hello', g:Myclosure())
2292 END
2293 CheckScriptSuccess(lines)
2294enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002295
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002296def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002297 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002298 vim9script
2299 def FuncA()
2300 FuncB(0)
2301 enddef
2302 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002303 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002304 enddef
2305 FuncA()
2306 END
2307 CheckScriptFailure(lines, 'E1012:')
2308enddef
2309
Bram Moolenaarf112f302020-12-20 17:47:52 +01002310def Test_global_closure()
2311 var lines =<< trim END
2312 vim9script
2313 def ReverseEveryNLines(n: number, line1: number, line2: number)
2314 var mods = 'sil keepj keepp lockm '
2315 var range = ':' .. line1 .. ',' .. line2
2316 def g:Offset(): number
2317 var offset = (line('.') - line1 + 1) % n
2318 return offset != 0 ? offset : n
2319 enddef
2320 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2321 enddef
2322
2323 new
2324 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2325 ReverseEveryNLines(3, 1, 9)
2326 END
2327 CheckScriptSuccess(lines)
2328 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2329 assert_equal(expected, getline(1, 9))
2330 bwipe!
2331enddef
2332
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002333def Test_global_closure_called_directly()
2334 var lines =<< trim END
2335 vim9script
2336 def Outer()
2337 var x = 1
2338 def g:Inner()
2339 var y = x
2340 x += 1
2341 assert_equal(1, y)
2342 enddef
2343 g:Inner()
2344 assert_equal(2, x)
2345 enddef
2346 Outer()
2347 END
2348 CheckScriptSuccess(lines)
2349 delfunc g:Inner
2350enddef
2351
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002352def Test_failure_in_called_function()
2353 # this was using the frame index as the return value
2354 var lines =<< trim END
2355 vim9script
2356 au TerminalWinOpen * eval [][0]
2357 def PopupTerm(a: any)
2358 # make sure typvals on stack are string
2359 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2360 FireEvent()
2361 enddef
2362 def FireEvent()
2363 do TerminalWinOpen
2364 enddef
2365 # use try/catch to make eval fail
2366 try
2367 call PopupTerm(0)
2368 catch
2369 endtry
2370 au! TerminalWinOpen
2371 END
2372 CheckScriptSuccess(lines)
2373enddef
2374
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002375def Test_nested_lambda()
2376 var lines =<< trim END
2377 vim9script
2378 def Func()
2379 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002380 var Lambda1 = () => 7
2381 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002382 var res = Lambda2()
2383 assert_equal([7, 4], res)
2384 enddef
2385 Func()
2386 END
2387 CheckScriptSuccess(lines)
2388enddef
2389
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002390def Test_double_nested_lambda()
2391 var lines =<< trim END
2392 vim9script
2393 def F(head: string): func(string): func(string): string
2394 return (sep: string): func(string): string => ((tail: string): string => {
2395 return head .. sep .. tail
2396 })
2397 enddef
2398 assert_equal('hello-there', F('hello')('-')('there'))
2399 END
2400 CheckScriptSuccess(lines)
2401enddef
2402
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002403def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002404 var lines =<< trim END
2405 vim9script
2406 def F(text: string): func(string): func(string): string
2407 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002408 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002409 })
2410 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002411 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002412 END
2413 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002414
2415 lines =<< trim END
2416 vim9script
2417 echo range(4)->mapnew((_, v) => {
2418 return range(v) ->mapnew((_, s) => {
2419 return string(s)
2420 })
2421 })
2422 END
2423 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002424
2425 lines =<< trim END
2426 vim9script
2427
2428 def s:func()
2429 range(10)
2430 ->mapnew((_, _) => ({
2431 key: range(10)->mapnew((_, _) => {
2432 return ' '
2433 }),
2434 }))
2435 enddef
2436
2437 defcomp
2438 END
2439 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002440enddef
2441
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002442def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002443 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002444 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002445enddef
2446
2447def Test_lambda_arg_shadows_func()
2448 assert_equal([42], Shadowed())
2449enddef
2450
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002451def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002452 var path: string = empty(dir)
2453 \ ? 'empty'
2454 \ : 'full'
2455 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002456enddef
2457
2458def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002459 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002460enddef
2461
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002462def Test_script_var_in_lambda()
2463 var lines =<< trim END
2464 vim9script
2465 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002466 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002467 END
2468 CheckScriptSuccess(lines)
2469enddef
2470
Bram Moolenaar5e654232020-09-16 15:22:00 +02002471def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002472 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002473 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002474 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002475 ->reverse()
2476 return x
2477enddef
2478
2479def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002480 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002481
2482 var lines =<< trim END
2483 vim9script
2484 var res = [{n: 1, m: 2, s: 'xxx'}]
2485 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2486 v.n,
2487 v.m,
2488 substitute(v.s, '.*', 'yyy', '')
2489 ))
2490 assert_equal(['1:2:yyy'], res)
2491 END
2492 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002493enddef
2494
Bram Moolenaarb6571982021-01-08 22:24:19 +01002495def Test_list_lambda()
2496 timer_start(1000, (_) => 0)
2497 var body = execute(timer_info()[0].callback
2498 ->string()
2499 ->substitute("('", ' ', '')
2500 ->substitute("')", '', '')
2501 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002502 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002503enddef
2504
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002505def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002506 var lines =<< trim END
2507 vim9script
2508 var flist: list<func>
2509 for i in range(10)
2510 var inloop = i
2511 flist[i] = () => inloop
2512 endfor
2513 END
2514 CheckScriptSuccess(lines)
2515
2516 lines =<< trim END
2517 vim9script
2518 if true
2519 var outloop = 5
2520 var flist: list<func>
2521 for i in range(10)
2522 flist[i] = () => outloop
2523 endfor
2524 endif
2525 END
2526 CheckScriptSuccess(lines)
2527
2528 lines =<< trim END
2529 vim9script
2530 if true
2531 var outloop = 5
2532 endif
2533 var flist: list<func>
2534 for i in range(10)
2535 flist[i] = () => outloop
2536 endfor
2537 END
2538 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002539
2540 lines =<< trim END
2541 vim9script
2542 for i in range(10)
2543 var Ref = () => 0
2544 endfor
2545 assert_equal(0, ((i) => 0)(0))
2546 END
2547 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002548enddef
2549
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002550def Test_legacy_lambda()
2551 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002552
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002553 var lines =<< trim END
2554 echo {x -> 'hello ' .. x}('foo')
2555 END
2556 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002557
2558 lines =<< trim END
2559 vim9script
2560 def Func()
2561 echo (() => 'no error')()
2562 enddef
2563 legacy call s:Func()
2564 END
2565 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002566enddef
2567
Bram Moolenaarce024c32021-06-26 13:00:49 +02002568def Test_legacy()
2569 var lines =<< trim END
2570 vim9script
2571 func g:LegacyFunction()
2572 let g:legacyvar = 1
2573 endfunc
2574 def Testit()
2575 legacy call g:LegacyFunction()
2576 enddef
2577 Testit()
2578 assert_equal(1, g:legacyvar)
2579 unlet g:legacyvar
2580 delfunc g:LegacyFunction
2581 END
2582 CheckScriptSuccess(lines)
2583enddef
2584
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002585def Test_legacy_errors()
2586 for cmd in ['if', 'elseif', 'else', 'endif',
2587 'for', 'endfor', 'continue', 'break',
2588 'while', 'endwhile',
2589 'try', 'catch', 'finally', 'endtry']
2590 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2591 endfor
2592enddef
2593
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002594def Test_call_legacy_with_dict()
2595 var lines =<< trim END
2596 vim9script
2597 func Legacy() dict
2598 let g:result = self.value
2599 endfunc
2600 def TestDirect()
2601 var d = {value: 'yes', func: Legacy}
2602 d.func()
2603 enddef
2604 TestDirect()
2605 assert_equal('yes', g:result)
2606 unlet g:result
2607
2608 def TestIndirect()
2609 var d = {value: 'foo', func: Legacy}
2610 var Fi = d.func
2611 Fi()
2612 enddef
2613 TestIndirect()
2614 assert_equal('foo', g:result)
2615 unlet g:result
2616
2617 var d = {value: 'bar', func: Legacy}
2618 d.func()
2619 assert_equal('bar', g:result)
2620 unlet g:result
2621 END
2622 CheckScriptSuccess(lines)
2623enddef
2624
Bram Moolenaarab360522021-01-10 14:02:28 +01002625def DoFilterThis(a: string): list<string>
2626 # closure nested inside another closure using argument
2627 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2628 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2629enddef
2630
2631def Test_nested_closure_using_argument()
2632 assert_equal(['x', 'x2'], DoFilterThis('x'))
2633enddef
2634
Bram Moolenaar0186e582021-01-10 18:33:11 +01002635def Test_triple_nested_closure()
2636 var what = 'x'
2637 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2638 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2639 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2640enddef
2641
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002642func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002643 CheckScreendump
2644
2645 let lines =<< trim END
2646 vim9script
2647 def EchoNothing()
2648 silent echo ''
2649 enddef
2650 defcompile
2651 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002652 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002653
2654 " Check that the balloon shows up after a mouse move
2655 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002656 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002657 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2658
2659 " clean up
2660 call StopVimInTerminal(buf)
2661 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002662endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002663
Bram Moolenaar171fb922020-10-28 16:54:47 +01002664def SilentlyError()
2665 execute('silent! invalid')
2666 g:did_it = 'yes'
2667enddef
2668
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002669func UserError()
2670 silent! invalid
2671endfunc
2672
2673def SilentlyUserError()
2674 UserError()
2675 g:did_it = 'yes'
2676enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002677
2678" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002679func Test_ignore_silent_error()
2680 let g:did_it = 'no'
2681 call SilentlyError()
2682 call assert_equal('yes', g:did_it)
2683
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002684 let g:did_it = 'no'
2685 call SilentlyUserError()
2686 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002687
2688 unlet g:did_it
2689endfunc
2690
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002691def Test_ignore_silent_error_in_filter()
2692 var lines =<< trim END
2693 vim9script
2694 def Filter(winid: number, key: string): bool
2695 if key == 'o'
2696 silent! eval [][0]
2697 return true
2698 endif
2699 return popup_filter_menu(winid, key)
2700 enddef
2701
Bram Moolenaare0de1712020-12-02 17:36:54 +01002702 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002703 feedkeys("o\r", 'xnt')
2704 END
2705 CheckScriptSuccess(lines)
2706enddef
2707
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002708def Fibonacci(n: number): number
2709 if n < 2
2710 return n
2711 else
2712 return Fibonacci(n - 1) + Fibonacci(n - 2)
2713 endif
2714enddef
2715
Bram Moolenaar985116a2020-07-12 17:31:09 +02002716def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002717 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002718enddef
2719
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002720def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002721 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002722 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002723 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002724 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002725 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002726enddef
2727
2728def Test_closure_in_map()
2729 mkdir('XclosureDir/tdir', 'p')
2730 writefile(['111'], 'XclosureDir/file1')
2731 writefile(['222'], 'XclosureDir/file2')
2732 writefile(['333'], 'XclosureDir/tdir/file3')
2733
Bram Moolenaare0de1712020-12-02 17:36:54 +01002734 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002735
2736 delete('XclosureDir', 'rf')
2737enddef
2738
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002739def Test_invalid_function_name()
2740 var lines =<< trim END
2741 vim9script
2742 def s: list<string>
2743 END
2744 CheckScriptFailure(lines, 'E129:')
2745
2746 lines =<< trim END
2747 vim9script
2748 def g: list<string>
2749 END
2750 CheckScriptFailure(lines, 'E129:')
2751
2752 lines =<< trim END
2753 vim9script
2754 def <SID>: list<string>
2755 END
2756 CheckScriptFailure(lines, 'E884:')
2757
2758 lines =<< trim END
2759 vim9script
2760 def F list<string>
2761 END
2762 CheckScriptFailure(lines, 'E488:')
2763enddef
2764
Bram Moolenaara90afb92020-07-15 22:38:56 +02002765def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002766 var lines =<< trim END
2767 var Xsetlist: func
2768 Xsetlist = function('setloclist', [0])
2769 Xsetlist([], ' ', {title: 'test'})
2770 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002771
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002772 Xsetlist = function('setloclist', [0, [], ' '])
2773 Xsetlist({title: 'test'})
2774 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002775
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002776 Xsetlist = function('setqflist')
2777 Xsetlist([], ' ', {title: 'test'})
2778 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002779
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002780 Xsetlist = function('setqflist', [[], ' '])
2781 Xsetlist({title: 'test'})
2782 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002783
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002784 var Len: func: number = function('len', ['word'])
2785 assert_equal(4, Len())
2786
2787 var RepeatFunc = function('repeat', ['o'])
2788 assert_equal('ooooo', RepeatFunc(5))
2789 END
2790 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002791
2792 lines =<< trim END
2793 vim9script
2794 def Foo(Parser: any)
2795 enddef
2796 var Expr: func(dict<any>): dict<any>
2797 const Call = Foo(Expr)
2798 END
2799 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002800enddef
2801
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002802def Test_cmd_modifier()
2803 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002804 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002805enddef
2806
2807def Test_restore_modifiers()
2808 # check that when compiling a :def function command modifiers are not messed
2809 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002810 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002811 vim9script
2812 set eventignore=
2813 autocmd QuickFixCmdPost * copen
2814 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002815 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002816 enddef
2817 func Func()
2818 noautocmd call s:AutocmdsDisabled()
2819 let g:ei_after = &eventignore
2820 endfunc
2821 Func()
2822 END
2823 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002824 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002825enddef
2826
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002827def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002828 eval 1 + 2
2829 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002830 # call not on fourth line
2831 StackBot()
2832enddef
2833
2834def StackBot()
2835 # throw an error
2836 eval [][0]
2837enddef
2838
2839def Test_callstack_def()
2840 try
2841 StackTop()
2842 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002843 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002844 endtry
2845enddef
2846
Bram Moolenaare8211a32020-10-09 22:04:29 +02002847" Re-using spot for variable used in block
2848def Test_block_scoped_var()
2849 var lines =<< trim END
2850 vim9script
2851 def Func()
2852 var x = ['a', 'b', 'c']
2853 if 1
2854 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002855 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002856 endif
2857 var z = x
2858 assert_equal(['x', 'x', 'x'], z)
2859 enddef
2860 Func()
2861 END
2862 CheckScriptSuccess(lines)
2863enddef
2864
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002865def Test_reset_did_emsg()
2866 var lines =<< trim END
2867 @s = 'blah'
2868 au BufWinLeave * #
2869 def Func()
2870 var winid = popup_create('popup', {})
2871 exe '*s'
2872 popup_close(winid)
2873 enddef
2874 Func()
2875 END
2876 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002877 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002878enddef
2879
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002880def Test_did_emsg_reset()
2881 # executing an autocommand resets did_emsg, this should not result in a
2882 # builtin function considered failing
2883 var lines =<< trim END
2884 vim9script
2885 au BufWinLeave * #
2886 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02002887 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002888 eval [][0]
2889 enddef
2890 nno <F3> <cmd>call <sid>Func()<cr>
2891 feedkeys("\<F3>\e", 'xt')
2892 END
2893 writefile(lines, 'XemsgReset')
2894 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
2895 delete('XemsgReset')
2896 nunmap <F3>
2897 au! BufWinLeave
2898enddef
2899
Bram Moolenaar56602ba2020-12-05 21:22:08 +01002900def Test_abort_with_silent_call()
2901 var lines =<< trim END
2902 vim9script
2903 g:result = 'none'
2904 def Func()
2905 g:result += 3
2906 g:result = 'yes'
2907 enddef
2908 # error is silenced, but function aborts on error
2909 silent! Func()
2910 assert_equal('none', g:result)
2911 unlet g:result
2912 END
2913 CheckScriptSuccess(lines)
2914enddef
2915
Bram Moolenaarf665e972020-12-05 19:17:16 +01002916def Test_continues_with_silent_error()
2917 var lines =<< trim END
2918 vim9script
2919 g:result = 'none'
2920 def Func()
2921 silent! g:result += 3
2922 g:result = 'yes'
2923 enddef
2924 # error is silenced, function does not abort
2925 Func()
2926 assert_equal('yes', g:result)
2927 unlet g:result
2928 END
2929 CheckScriptSuccess(lines)
2930enddef
2931
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002932def Test_abort_even_with_silent()
2933 var lines =<< trim END
2934 vim9script
2935 g:result = 'none'
2936 def Func()
2937 eval {-> ''}() .. '' .. {}['X']
2938 g:result = 'yes'
2939 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01002940 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002941 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002942 unlet g:result
2943 END
2944 CheckScriptSuccess(lines)
2945enddef
2946
Bram Moolenaarf665e972020-12-05 19:17:16 +01002947def Test_cmdmod_silent_restored()
2948 var lines =<< trim END
2949 vim9script
2950 def Func()
2951 g:result = 'none'
2952 silent! g:result += 3
2953 g:result = 'none'
2954 g:result += 3
2955 enddef
2956 Func()
2957 END
2958 # can't use CheckScriptFailure, it ignores the :silent!
2959 var fname = 'Xdefsilent'
2960 writefile(lines, fname)
2961 var caught = 'no'
2962 try
2963 exe 'source ' .. fname
2964 catch /E1030:/
2965 caught = 'yes'
2966 assert_match('Func, line 4', v:throwpoint)
2967 endtry
2968 assert_equal('yes', caught)
2969 delete(fname)
2970enddef
2971
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002972def Test_cmdmod_silent_nested()
2973 var lines =<< trim END
2974 vim9script
2975 var result = ''
2976
2977 def Error()
2978 result ..= 'Eb'
2979 eval [][0]
2980 result ..= 'Ea'
2981 enddef
2982
2983 def Crash()
2984 result ..= 'Cb'
2985 sil! Error()
2986 result ..= 'Ca'
2987 enddef
2988
2989 Crash()
2990 assert_equal('CbEbEaCa', result)
2991 END
2992 CheckScriptSuccess(lines)
2993enddef
2994
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002995def Test_dict_member_with_silent()
2996 var lines =<< trim END
2997 vim9script
2998 g:result = 'none'
2999 var d: dict<any>
3000 def Func()
3001 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003002 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003003 catch
3004 endtry
3005 enddef
3006 silent! Func()
3007 assert_equal('0', g:result)
3008 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003009 END
3010 CheckScriptSuccess(lines)
3011enddef
3012
Bram Moolenaarf9041332021-01-21 19:41:16 +01003013def Test_skip_cmds_with_silent()
3014 var lines =<< trim END
3015 vim9script
3016
3017 def Func(b: bool)
3018 Crash()
3019 enddef
3020
3021 def Crash()
3022 sil! :/not found/d _
3023 sil! :/not found/put _
3024 enddef
3025
3026 Func(true)
3027 END
3028 CheckScriptSuccess(lines)
3029enddef
3030
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003031def Test_opfunc()
3032 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3033 def g:Opfunc(_: any): string
3034 setline(1, 'ASDF')
3035 return ''
3036 enddef
3037 new
3038 setline(1, 'asdf')
3039 feedkeys("\<F3>$", 'x')
3040 assert_equal('ASDF', getline(1))
3041
3042 bwipe!
3043 nunmap <F3>
3044enddef
3045
Bram Moolenaar077a4232020-12-22 18:33:27 +01003046" this was crashing on exit
3047def Test_nested_lambda_in_closure()
3048 var lines =<< trim END
3049 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003050 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003051 def Outer()
3052 def g:Inner()
3053 echo map([1, 2, 3], {_, v -> v + 1})
3054 enddef
3055 g:Inner()
3056 enddef
3057 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003058 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003059 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003060 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003061 return
3062 endif
3063 assert_equal(['Done'], readfile('XnestedDone'))
3064 delete('XnestedDone')
3065enddef
3066
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003067def Test_check_func_arg_types()
3068 var lines =<< trim END
3069 vim9script
3070 def F1(x: string): string
3071 return x
3072 enddef
3073
3074 def F2(x: number): number
3075 return x + 1
3076 enddef
3077
3078 def G(g: func): dict<func>
3079 return {f: g}
3080 enddef
3081
3082 def H(d: dict<func>): string
3083 return d.f('a')
3084 enddef
3085 END
3086
3087 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3088 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3089enddef
3090
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003091def Test_list_any_type_checked()
3092 var lines =<< trim END
3093 vim9script
3094 def Foo()
3095 --decl--
3096 Bar(l)
3097 enddef
3098 def Bar(ll: list<dict<any>>)
3099 enddef
3100 Foo()
3101 END
3102 lines[2] = 'var l: list<any>'
3103 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3104
3105 lines[2] = 'var l: list<any> = []'
3106 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3107
3108 lines[2] = 'var l: list<any> = [11]'
3109 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3110enddef
3111
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003112def Test_compile_error()
3113 var lines =<< trim END
3114 def g:Broken()
3115 echo 'a' + {}
3116 enddef
3117 call g:Broken()
3118 END
3119 # First call: compilation error
3120 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3121
3122 # Second call won't try compiling again
3123 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003124 delfunc g:Broken
3125
3126 # No error when compiling with :silent!
3127 lines =<< trim END
3128 def g:Broken()
3129 echo 'a' + []
3130 enddef
3131 silent! defcompile
3132 END
3133 CheckScriptSuccess(lines)
3134
3135 # Calling the function won't try compiling again
3136 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3137 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003138enddef
3139
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003140def Test_ignored_argument()
3141 var lines =<< trim END
3142 vim9script
3143 def Ignore(_, _): string
3144 return 'yes'
3145 enddef
3146 assert_equal('yes', Ignore(1, 2))
3147
3148 func Ok(_)
3149 return a:_
3150 endfunc
3151 assert_equal('ok', Ok('ok'))
3152
3153 func Oktoo()
3154 let _ = 'too'
3155 return _
3156 endfunc
3157 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003158
3159 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003160 END
3161 CheckScriptSuccess(lines)
3162
3163 lines =<< trim END
3164 def Ignore(_: string): string
3165 return _
3166 enddef
3167 defcompile
3168 END
3169 CheckScriptFailure(lines, 'E1181:', 1)
3170
3171 lines =<< trim END
3172 var _ = 1
3173 END
3174 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003175
3176 lines =<< trim END
3177 var x = _
3178 END
3179 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003180enddef
3181
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003182def Test_too_many_arguments()
3183 var lines =<< trim END
3184 echo [0, 1, 2]->map(() => 123)
3185 END
3186 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3187
3188 lines =<< trim END
3189 echo [0, 1, 2]->map((_) => 123)
3190 END
3191 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3192enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003193
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003194def Test_closing_brace_at_start_of_line()
3195 var lines =<< trim END
3196 def Func()
3197 enddef
3198 Func(
3199 )
3200 END
3201 call CheckDefAndScriptSuccess(lines)
3202enddef
3203
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003204func CreateMydict()
3205 let g:mydict = {}
3206 func g:mydict.afunc()
3207 let g:result = self.key
3208 endfunc
3209endfunc
3210
3211def Test_numbered_function_reference()
3212 CreateMydict()
3213 var output = execute('legacy func g:mydict.afunc')
3214 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3215 execute 'function(' .. funcName .. ', [], {key: 42})()'
3216 # check that the function still exists
3217 assert_equal(output, execute('legacy func g:mydict.afunc'))
3218 unlet g:mydict
3219enddef
3220
Bram Moolenaar20677332021-06-06 17:02:53 +02003221if has('python3')
3222 def Test_python3_heredoc()
3223 py3 << trim EOF
3224 import vim
3225 vim.vars['didit'] = 'yes'
3226 EOF
3227 assert_equal('yes', g:didit)
3228
3229 python3 << trim EOF
3230 import vim
3231 vim.vars['didit'] = 'again'
3232 EOF
3233 assert_equal('again', g:didit)
3234 enddef
3235endif
3236
3237" This messes up syntax highlight, keep near the end.
3238if has('lua')
3239 def Test_lua_heredoc()
3240 g:d = {}
3241 lua << trim EOF
3242 x = vim.eval('g:d')
3243 x['key'] = 'val'
3244 EOF
3245 assert_equal('val', g:d.key)
3246 enddef
3247endif
3248
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003249
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003250" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker