blob: 75d0dd0b65e81dfe03d37f933aad897d90726103 [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()
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000442 # This used to fail but now the actual list type is checked, and since it has
443 # an item of type string it can be used as list<string>.
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200444 var lines =<< trim END
445 vim9script
446 def Func(): list<string>
447 var l: list<any>
448 l->add('string')
449 return l
450 enddef
451 echo Func()
452 END
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000453 CheckScriptSuccess(lines)
454
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200455 lines =<< trim END
456 vim9script
457 def Func(): list<string>
458 var l: list<any>
459 l += ['string']
460 return l
461 enddef
462 echo Func()
463 END
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000464 CheckScriptSuccess(lines)
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200465enddef
466
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200467func Increment()
468 let g:counter += 1
469endfunc
470
471def Test_call_ufunc_count()
472 g:counter = 1
473 Increment()
474 Increment()
475 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200476 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200477 g:counter->assert_equal(4)
478 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200479 unlet g:counter
480enddef
481
482def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200483 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200484 for s in rest
485 res ..= ',' .. s
486 endfor
487 return res
488enddef
489
490def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200491 MyVarargs('one')->assert_equal('one')
492 MyVarargs('one', 'two')->assert_equal('one,two')
493 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200494enddef
495
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200496def Test_call_white_space()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000497 CheckDefAndScriptFailure(["call Test ('text')"], ['E476:', 'E1068:'])
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200498enddef
499
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200500def MyDefaultArgs(name = 'string'): string
501 return name
502enddef
503
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200504def MyDefaultSecond(name: string, second: bool = true): string
505 return second ? name : 'none'
506enddef
507
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200509def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200510 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200511 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200513 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200514
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200515 MyDefaultSecond('test')->assert_equal('test')
516 MyDefaultSecond('test', true)->assert_equal('test')
517 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200518
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200519 var lines =<< trim END
520 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
521 return name .. aa .. bb
522 enddef
523
524 MyDefaultThird('->')->assert_equal('->aabb')
525 MyDefaultThird('->', v:none)->assert_equal('->aabb')
526 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
527 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
528 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
529 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
530 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200531
532 def DefArg(mandatory: any, optional = mandatory): string
533 return mandatory .. optional
534 enddef
535 DefArg(1234)->assert_equal('12341234')
536 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200537 END
538 CheckDefAndScriptSuccess(lines)
539
Bram Moolenaar822ba242020-05-24 23:00:18 +0200540 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100541 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200542 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 +0100543 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200544 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100545
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200546 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100547 vim9script
548 def Func(a = b == 0 ? 1 : 2, b = 0)
549 enddef
550 defcompile
551 END
552 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000553
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000554 # using script variable requires matching type or type cast when executed
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000555 lines =<< trim END
556 vim9script
557 var a: any
558 def Func(arg: string = a)
559 echo arg
560 enddef
561 defcompile
562 END
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000563 CheckScriptSuccess(lines + ['a = "text"', 'Func()'])
564 CheckScriptFailure(lines + ['a = 123', 'Func()'], 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000565
566 # using global variable does not require type cast
567 lines =<< trim END
568 vim9script
569 def Func(arg: string = g:str)
570 echo arg
571 enddef
572 g:str = 'works'
573 Func()
574 END
575 CheckScriptSuccess(lines)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200576enddef
577
Bram Moolenaarcef12702021-01-04 14:09:43 +0100578def FuncWithComment( # comment
579 a: number, #comment
580 b: bool, # comment
581 c: string) #comment
582 assert_equal(4, a)
583 assert_equal(true, b)
584 assert_equal('yes', c)
585enddef
586
587def Test_func_with_comments()
588 FuncWithComment(4, true, 'yes')
589
590 var lines =<< trim END
591 def Func(# comment
592 arg: string)
593 enddef
594 END
595 CheckScriptFailure(lines, 'E125:', 1)
596
597 lines =<< trim END
598 def Func(
599 arg: string# comment
600 )
601 enddef
602 END
603 CheckScriptFailure(lines, 'E475:', 2)
604
605 lines =<< trim END
606 def Func(
607 arg: string
608 )# comment
609 enddef
610 END
611 CheckScriptFailure(lines, 'E488:', 3)
612enddef
613
Bram Moolenaar04b12692020-05-04 23:24:44 +0200614def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000615 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200616 return 'nested ' .. arg
617 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000618 NestedDef(':def')->assert_equal('nested :def')
619
620 func NestedFunc(arg)
621 return 'nested ' .. a:arg
622 endfunc
623 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200624
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200625 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
626 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
627
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200628 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
629 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200630
Bram Moolenaar54021752020-12-06 18:50:36 +0100631 var lines =<< trim END
632 def Outer()
633 def Inner()
634 # comment
635 enddef
636 def Inner()
637 enddef
638 enddef
639 END
640 CheckDefFailure(lines, 'E1073:')
641
642 lines =<< trim END
643 def Outer()
644 def Inner()
645 # comment
646 enddef
647 def! Inner()
648 enddef
649 enddef
650 END
651 CheckDefFailure(lines, 'E1117:')
652
Bram Moolenaardb8e5c22021-12-25 19:58:22 +0000653 lines =<< trim END
654 vim9script
655 def Outer()
656 def Inner()
657 g:result = 'ok'
658 enddef
659 Inner()
660 enddef
661 Outer()
662 Inner()
663 END
664 CheckScriptFailure(lines, 'E117: Unknown function: Inner')
665 assert_equal('ok', g:result)
666 unlet g:result
667
Bram Moolenaar54021752020-12-06 18:50:36 +0100668 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100669 lines =<< trim END
670 vim9script
671 var thecount = 0
672 if true
673 def Test(): number
674 def TheFunc(): number
675 thecount += 1
676 return thecount
677 enddef
678 return TheFunc()
679 enddef
680 endif
681 defcompile
682 assert_equal(1, Test())
683 assert_equal(2, Test())
684 END
685 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100686
687 # also works when "thecount" is inside the "if" block
688 lines =<< trim END
689 vim9script
690 if true
691 var thecount = 0
692 def Test(): number
693 def TheFunc(): number
694 thecount += 1
695 return thecount
696 enddef
697 return TheFunc()
698 enddef
699 endif
700 defcompile
701 assert_equal(1, Test())
702 assert_equal(2, Test())
703 END
704 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200705
706 lines =<< trim END
707 vim9script
708 def Outer()
709 def Inner()
710 echo 'hello'
711 enddef burp
712 enddef
713 defcompile
714 END
715 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200716enddef
717
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100718def Test_not_nested_function()
719 echo printf('%d',
720 function('len')('xxx'))
721enddef
722
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200723func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200724 call MyDefaultArgs()->assert_equal('string')
725 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200726 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200727endfunc
728
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200729def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200730 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200731 vim9script
732 def Outer()
733 def g:Inner(): string
734 return 'inner'
735 enddef
736 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200737 defcompile
738 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200739 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200740 delfunc g:Inner
741 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200742 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200743 delfunc g:Inner
744 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200745 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200746 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200747 END
748 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200749
750 lines =<< trim END
751 vim9script
752 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000753 func g:Inner()
754 return 'inner'
755 endfunc
756 enddef
757 defcompile
758 Outer()
759 g:Inner()->assert_equal('inner')
760 delfunc g:Inner
761 Outer()
762 g:Inner()->assert_equal('inner')
763 delfunc g:Inner
764 Outer()
765 g:Inner()->assert_equal('inner')
766 delfunc g:Inner
767 END
768 CheckScriptSuccess(lines)
769
770 lines =<< trim END
771 vim9script
772 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200773 def g:Inner(): string
774 return 'inner'
775 enddef
776 enddef
777 defcompile
778 Outer()
779 Outer()
780 END
781 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100782 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200783
784 lines =<< trim END
785 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100786 def Outer()
787 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100788 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100789 enddef
790 g:Inner()
791 enddef
792 Outer()
793 END
794 CheckScriptSuccess(lines)
795 delfunc g:Inner
796
797 lines =<< trim END
798 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200799 def Func()
800 echo 'script'
801 enddef
802 def Outer()
803 def Func()
804 echo 'inner'
805 enddef
806 enddef
807 defcompile
808 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000809 CheckScriptFailure(lines, "E1073:", 1)
810
811 lines =<< trim END
812 vim9script
813 def Func()
814 echo 'script'
815 enddef
816 def Func()
817 echo 'script'
818 enddef
819 END
820 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200821enddef
822
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100823def DefListAll()
824 def
825enddef
826
827def DefListOne()
828 def DefListOne
829enddef
830
831def DefListMatches()
832 def /DefList
833enddef
834
835def Test_nested_def_list()
836 var funcs = split(execute('call DefListAll()'), "\n")
837 assert_true(len(funcs) > 10)
838 assert_true(funcs->index('def DefListAll()') >= 0)
839
840 funcs = split(execute('call DefListOne()'), "\n")
841 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
842
843 funcs = split(execute('call DefListMatches()'), "\n")
844 assert_true(len(funcs) >= 3)
845 assert_true(funcs->index('def DefListAll()') >= 0)
846 assert_true(funcs->index('def DefListOne()') >= 0)
847 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100848
849 var lines =<< trim END
850 vim9script
851 def Func()
852 def +Func+
853 enddef
854 defcompile
855 END
856 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100857enddef
858
Bram Moolenaar333894b2020-08-01 18:53:07 +0200859def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200860 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200861 vim9script
862 def g:Func(): string
863 return 'global'
864 enddef
865 def Func(): string
866 return 'local'
867 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200868 g:Func()->assert_equal('global')
869 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100870 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200871 END
872 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200873
874 lines =<< trim END
875 vim9script
876 def g:Funcy()
877 echo 'funcy'
878 enddef
879 s:Funcy()
880 END
881 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200882enddef
883
Bram Moolenaar0f769812020-09-12 18:32:34 +0200884def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200885 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200886 vim9script
887 def g:Gfunc(): string
888 return 'global'
889 enddef
890 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200891 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200892 return Gfunc('testing')
893 enddef
894 g:Gfunc()->assert_equal('global')
895 AnotherFunc()->assert_equal(7)
896 delfunc g:Gfunc
897 END
898 CheckScriptSuccess(lines)
899
900 lines =<< trim END
901 vim9script
902 def g:Func(): string
903 return 'global'
904 enddef
905 def AnotherFunc()
906 g:Func = function('len')
907 enddef
908 AnotherFunc()
909 END
910 CheckScriptFailure(lines, 'E705:')
911 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200912
913 # global function is found without g: prefix
914 lines =<< trim END
915 vim9script
916 def g:Func(): string
917 return 'global'
918 enddef
919 def AnotherFunc(): string
920 return Func()
921 enddef
922 assert_equal('global', AnotherFunc())
923 delfunc g:Func
924 END
925 CheckScriptSuccess(lines)
926
927 lines =<< trim END
928 vim9script
929 def g:Func(): string
930 return 'global'
931 enddef
932 assert_equal('global', Func())
933 delfunc g:Func
934 END
935 CheckScriptSuccess(lines)
Bram Moolenaar58493cf2022-01-06 12:23:30 +0000936
937 # This does not shadow "i" which is visible only inside the for loop
938 lines =<< trim END
939 vim9script
940
941 def Foo(i: number)
942 echo i
943 enddef
944
945 for i in range(3)
946 # Foo() is compiled here
947 Foo(i)
948 endfor
949 END
950 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200951enddef
952
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200953func TakesOneArg(arg)
954 echo a:arg
955endfunc
956
957def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200958 CheckDefFailure(['TakesOneArg()'], 'E119:')
959 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
960 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
961 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200962
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200963 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200964 vim9script
965 def Func(s: string)
966 echo s
967 enddef
968 Func([])
969 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200970 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200971
Bram Moolenaar9a015112021-12-31 14:06:45 +0000972 # argument name declared earlier is found when declaring a function
Bram Moolenaarb185a402020-09-18 22:42:00 +0200973 lines =<< trim END
974 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100975 var name = 'piet'
976 def FuncOne(name: string)
977 echo nr
978 enddef
979 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100980 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100981
Bram Moolenaar9a015112021-12-31 14:06:45 +0000982 # argument name declared later is only found when compiling
983 lines =<< trim END
984 vim9script
985 def FuncOne(name: string)
986 echo nr
987 enddef
988 var name = 'piet'
989 END
990 CheckScriptSuccess(lines)
991 CheckScriptFailure(lines + ['defcompile'], 'E1168:')
992
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100993 lines =<< trim END
994 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200995 def FuncOne(nr: number)
996 echo nr
997 enddef
998 def FuncTwo()
999 FuncOne()
1000 enddef
1001 defcompile
1002 END
1003 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001004 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +02001005 try
1006 source Xscript
1007 catch
1008 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
1009 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
1010 didCatch = true
1011 endtry
1012 assert_true(didCatch)
1013
1014 lines =<< trim END
1015 vim9script
1016 def FuncOne(nr: number)
1017 echo nr
1018 enddef
1019 def FuncTwo()
1020 FuncOne(1, 2)
1021 enddef
1022 defcompile
1023 END
1024 writefile(lines, 'Xscript')
1025 didCatch = false
1026 try
1027 source Xscript
1028 catch
1029 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
1030 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
1031 didCatch = true
1032 endtry
1033 assert_true(didCatch)
1034
1035 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001036enddef
1037
Bram Moolenaar50824712020-12-20 21:10:17 +01001038def Test_call_funcref_wrong_args()
1039 var head =<< trim END
1040 vim9script
1041 def Func3(a1: string, a2: number, a3: list<number>)
1042 echo a1 .. a2 .. a3[0]
1043 enddef
1044 def Testme()
1045 var funcMap: dict<func> = {func: Func3}
1046 END
1047 var tail =<< trim END
1048 enddef
1049 Testme()
1050 END
1051 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
1052
1053 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
1054 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001055
1056 var lines =<< trim END
1057 vim9script
1058 var Ref: func(number): any
1059 Ref = (j) => !j
1060 echo Ref(false)
1061 END
1062 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
1063
1064 lines =<< trim END
1065 vim9script
1066 var Ref: func(number): any
1067 Ref = (j) => !j
1068 call Ref(false)
1069 END
1070 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001071enddef
1072
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001073def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001074 var lines =<< trim END
1075 var Callback = (..._) => 'anything'
1076 assert_equal('anything', Callback())
1077 assert_equal('anything', Callback(1))
1078 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001079
1080 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001081 END
1082 CheckDefAndScriptSuccess(lines)
1083
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001084 CheckDefFailure(['echo ((i) => 0)()'],
1085 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001086
Bram Moolenaar2a389082021-04-09 20:24:31 +02001087 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001088 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001089 echo Ref(1, 'x')
1090 END
1091 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001092
1093 lines =<< trim END
1094 var Ref: func(job, string, number)
1095 Ref = (x, y) => 0
1096 END
1097 CheckDefAndScriptFailure(lines, 'E1012:')
1098
1099 lines =<< trim END
1100 var Ref: func(job, string)
1101 Ref = (x, y, z) => 0
1102 END
1103 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001104
1105 lines =<< trim END
1106 var one = 1
1107 var l = [1, 2, 3]
1108 echo map(l, (one) => one)
1109 END
1110 CheckDefFailure(lines, 'E1167:')
1111 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1112
1113 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001114 var Ref: func(any, ?any): bool
1115 Ref = (_, y = 1) => false
1116 END
1117 CheckDefAndScriptFailure(lines, 'E1172:')
1118
1119 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001120 var a = 0
1121 var b = (a == 0 ? 1 : 2)
1122 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001123 var txt = 'a'
1124 b = (txt =~ 'x' ? 1 : 2)
1125 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001126 END
1127 CheckDefAndScriptSuccess(lines)
1128
1129 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001130 def ShadowLocal()
1131 var one = 1
1132 var l = [1, 2, 3]
1133 echo map(l, (one) => one)
1134 enddef
1135 END
1136 CheckDefFailure(lines, 'E1167:')
1137
1138 lines =<< trim END
1139 def Shadowarg(one: number)
1140 var l = [1, 2, 3]
1141 echo map(l, (one) => one)
1142 enddef
1143 END
1144 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001145
1146 lines =<< trim END
1147 echo ((a) => a)('aa', 'bb')
1148 END
1149 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001150
1151 lines =<< trim END
1152 echo 'aa'->((a) => a)('bb')
1153 END
1154 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1155 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001156enddef
1157
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001158def Test_lambda_line_nr()
1159 var lines =<< trim END
1160 vim9script
1161 # comment
1162 # comment
1163 var id = timer_start(1'000, (_) => 0)
1164 var out = execute('verbose ' .. timer_info(id)[0].callback
1165 ->string()
1166 ->substitute("('\\|')", ' ', 'g'))
1167 assert_match('Last set from .* line 4', out)
1168 END
1169 CheckScriptSuccess(lines)
1170enddef
1171
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001172def FilterWithCond(x: string, Cond: func(string): bool): bool
1173 return Cond(x)
1174enddef
1175
Bram Moolenaar0346b792021-01-31 22:18:29 +01001176def Test_lambda_return_type()
1177 var lines =<< trim END
1178 var Ref = (): => 123
1179 END
1180 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001181
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001182 # no space before the return type
1183 lines =<< trim END
1184 var Ref = (x):number => x + 1
1185 END
1186 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1187
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001188 # this works
1189 for x in ['foo', 'boo']
1190 echo FilterWithCond(x, (v) => v =~ '^b')
1191 endfor
1192
1193 # this fails
1194 lines =<< trim END
1195 echo FilterWithCond('foo', (v) => v .. '^b')
1196 END
1197 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001198
1199 lines =<< trim END
1200 var Lambda1 = (x) => {
1201 return x
1202 }
1203 assert_equal('asdf', Lambda1('asdf'))
1204 var Lambda2 = (x): string => {
1205 return x
1206 }
1207 assert_equal('foo', Lambda2('foo'))
1208 END
1209 CheckDefAndScriptSuccess(lines)
1210
1211 lines =<< trim END
1212 var Lambda = (x): string => {
1213 return x
1214 }
1215 echo Lambda(['foo'])
1216 END
1217 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001218enddef
1219
Bram Moolenaar709664c2020-12-12 14:33:41 +01001220def Test_lambda_uses_assigned_var()
1221 CheckDefSuccess([
1222 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001223 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001224enddef
1225
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001226def Test_pass_legacy_lambda_to_def_func()
1227 var lines =<< trim END
1228 vim9script
1229 func Foo()
1230 eval s:Bar({x -> 0})
1231 endfunc
1232 def Bar(y: any)
1233 enddef
1234 Foo()
1235 END
1236 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001237
1238 lines =<< trim END
1239 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001240 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001241 enddef
1242 legacy call g:TestFunc({-> 0})
1243 delfunc g:TestFunc
1244
1245 def g:TestFunc(f: func(number))
1246 enddef
1247 legacy call g:TestFunc({nr -> 0})
1248 delfunc g:TestFunc
1249 END
1250 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001251enddef
1252
Bram Moolenaar844fb642021-10-23 13:32:30 +01001253def Test_lambda_in_reduce_line_break()
1254 # this was using freed memory
1255 var lines =<< trim END
1256 vim9script
1257 const result: dict<number> =
1258 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1259 ->reduce((acc, val) => {
1260 if has_key(acc, val)
1261 acc[val] += 1
1262 return acc
1263 else
1264 acc[val] = 1
1265 return acc
1266 endif
1267 }, {})
1268 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1269 END
1270 CheckScriptSuccess(lines)
1271enddef
1272
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001273def Test_set_opfunc_to_lambda()
1274 var lines =<< trim END
1275 vim9script
1276 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1277 def CountSpaces(type = ''): string
1278 if type == ''
1279 &operatorfunc = (t) => CountSpaces(t)
1280 return 'g@'
1281 endif
1282 normal! '[V']y
1283 g:result = getreg('"')->count(' ')
1284 return ''
1285 enddef
1286 new
1287 'a b c d e'->setline(1)
1288 feedkeys("\<F4>", 'x')
1289 assert_equal(4, g:result)
1290 bwipe!
1291 END
1292 CheckScriptSuccess(lines)
1293enddef
1294
Bram Moolenaaref082e12021-12-12 21:02:03 +00001295def Test_set_opfunc_to_global_function()
1296 var lines =<< trim END
1297 vim9script
1298 def g:CountSpaces(type = ''): string
1299 normal! '[V']y
1300 g:result = getreg('"')->count(' ')
1301 return ''
1302 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001303 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001304 &operatorfunc = g:CountSpaces
1305 new
1306 'a b c d e'->setline(1)
1307 feedkeys("g@_", 'x')
1308 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001309
1310 &operatorfunc = ''
1311 g:result = 0
1312 # global function works in :def function
1313 def Func()
1314 &operatorfunc = g:CountSpaces
1315 enddef
1316 Func()
1317 feedkeys("g@_", 'x')
1318 assert_equal(4, g:result)
1319
Bram Moolenaaref082e12021-12-12 21:02:03 +00001320 bwipe!
1321 END
1322 CheckScriptSuccess(lines)
1323 &operatorfunc = ''
1324enddef
1325
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001326def Test_use_script_func_name_with_prefix()
1327 var lines =<< trim END
1328 vim9script
1329 func s:Getit()
1330 return 'it'
1331 endfunc
1332 var Fn = s:Getit
1333 assert_equal('it', Fn())
1334 END
1335 CheckScriptSuccess(lines)
1336enddef
1337
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001338def Test_lambda_type_allocated()
1339 # Check that unreferencing a partial using a lambda can use the variable type
1340 # after the lambda has been freed and does not leak memory.
1341 var lines =<< trim END
1342 vim9script
1343
1344 func MyomniFunc1(val, findstart, base)
1345 return a:findstart ? 0 : []
1346 endfunc
1347
1348 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1349 &omnifunc = Lambda
1350 Lambda = (a, b) => MyomniFunc1(20, a, b)
1351 &omnifunc = string(Lambda)
1352 Lambda = (a, b) => strlen(a)
1353 END
1354 CheckScriptSuccess(lines)
1355enddef
1356
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001357" Default arg and varargs
1358def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001359 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001360 for s in rest
1361 res ..= ',' .. s
1362 endfor
1363 return res
1364enddef
1365
1366def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001367 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001368 MyDefVarargs('one')->assert_equal('one,foo')
1369 MyDefVarargs('one', 'two')->assert_equal('one,two')
1370 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001371 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001372 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001373 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001374 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001375
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001376 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001377 vim9script
1378 def Func(...l: list<string>)
1379 echo l
1380 enddef
1381 Func('a', 'b', 'c')
1382 END
1383 CheckScriptSuccess(lines)
1384
1385 lines =<< trim END
1386 vim9script
1387 def Func(...l: list<string>)
1388 echo l
1389 enddef
1390 Func()
1391 END
1392 CheckScriptSuccess(lines)
1393
1394 lines =<< trim END
1395 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001396 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001397 echo l
1398 enddef
1399 Func(0)
1400 END
1401 CheckScriptSuccess(lines)
1402
1403 lines =<< trim END
1404 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001405 def Func(...l: any)
1406 echo l
1407 enddef
1408 Func(0)
1409 END
1410 CheckScriptFailure(lines, 'E1180:', 2)
1411
1412 lines =<< trim END
1413 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001414 def Func(..._l: list<string>)
1415 echo _l
1416 enddef
1417 Func('a', 'b', 'c')
1418 END
1419 CheckScriptSuccess(lines)
1420
1421 lines =<< trim END
1422 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001423 def Func(...l: list<string>)
1424 echo l
1425 enddef
1426 Func(1, 2, 3)
1427 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001428 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001429
1430 lines =<< trim END
1431 vim9script
1432 def Func(...l: list<string>)
1433 echo l
1434 enddef
1435 Func('a', 9)
1436 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001437 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001438
1439 lines =<< trim END
1440 vim9script
1441 def Func(...l: list<string>)
1442 echo l
1443 enddef
1444 Func(1, 'a')
1445 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001446 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001447
1448 lines =<< trim END
1449 vim9script
1450 def Func( # some comment
1451 ...l = []
1452 )
1453 echo l
1454 enddef
1455 END
1456 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001457
1458 lines =<< trim END
1459 vim9script
1460 def DoIt()
1461 g:Later('')
1462 enddef
1463 defcompile
1464 def g:Later(...l: list<number>)
1465 enddef
1466 DoIt()
1467 END
1468 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001469enddef
1470
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001471let s:value = ''
1472
1473def FuncOneDefArg(opt = 'text')
1474 s:value = opt
1475enddef
1476
1477def FuncTwoDefArg(nr = 123, opt = 'text'): string
1478 return nr .. opt
1479enddef
1480
1481def FuncVarargs(...arg: list<string>): string
1482 return join(arg, ',')
1483enddef
1484
1485def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001486 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001487 RefDefArg = FuncOneDefArg
1488 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001489 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001490 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001491 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001492
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001493 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001494 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001495 RefDef2Arg()->assert_equal('123text')
1496 RefDef2Arg(99)->assert_equal('99text')
1497 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001498
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001499 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1500 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001501
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001502 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001503 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001504 RefVarargs()->assert_equal('')
1505 RefVarargs('one')->assert_equal('one')
1506 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001507
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001508 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1509 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001510enddef
1511
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001512" Only varargs
1513def MyVarargsOnly(...args: list<string>): string
1514 return join(args, ',')
1515enddef
1516
1517def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001518 MyVarargsOnly()->assert_equal('')
1519 MyVarargsOnly('one')->assert_equal('one')
1520 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001521 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1522 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001523enddef
1524
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001525def Test_using_var_as_arg()
Bram Moolenaard2939812021-12-30 17:09:05 +00001526 var lines =<< trim END
1527 def Func(x: number)
1528 var x = 234
1529 enddef
1530 END
1531 CheckDefFailure(lines, 'E1006:')
1532
1533 lines =<< trim END
1534 def Func(Ref: number)
1535 def Ref()
1536 enddef
1537 enddef
1538 END
1539 CheckDefFailure(lines, 'E1073:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001540enddef
1541
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001542def DictArg(arg: dict<string>)
1543 arg['key'] = 'value'
1544enddef
1545
1546def ListArg(arg: list<string>)
1547 arg[0] = 'value'
1548enddef
1549
1550def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001551 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001552 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001553 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001554 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001555 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001556 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001557 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001558
Bram Moolenaard2c61702020-09-06 15:58:36 +02001559 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001560 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001561enddef
1562
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001563" These argument names are reserved in legacy functions.
1564def WithReservedNames(firstline: string, lastline: string): string
1565 return firstline .. lastline
1566enddef
1567
1568def Test_argument_names()
1569 assert_equal('OK', WithReservedNames('O', 'K'))
1570enddef
1571
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001572def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001573 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001574 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001575enddef
1576
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001577func DefinedLater(arg)
1578 return a:arg
1579endfunc
1580
1581def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001582 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001583 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001584 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001585
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001586 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001587 vim9script
1588 def RetNumber(): number
1589 return 123
1590 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001591 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001592 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001593 END
1594 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001595
1596 lines =<< trim END
1597 vim9script
1598 def RetNumber(): number
1599 return 123
1600 enddef
1601 def Bar(F: func: number): number
1602 return F()
1603 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001604 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001605 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001606 END
1607 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001608
1609 lines =<< trim END
1610 vim9script
1611 def UseNumber(nr: number)
1612 echo nr
1613 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001614 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001615 Funcref(123)
1616 END
1617 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001618
1619 lines =<< trim END
1620 vim9script
1621 def UseNumber(nr: number)
1622 echo nr
1623 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001624 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001625 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001626 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001627
1628 lines =<< trim END
1629 vim9script
1630 def EchoNr(nr = 34)
1631 g:echo = nr
1632 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001633 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001634 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001635 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001636 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001637 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001638 END
1639 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001640
1641 lines =<< trim END
1642 vim9script
1643 def EchoList(...l: list<number>)
1644 g:echo = l
1645 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001646 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001647 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001648 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001649 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001650 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001651 END
1652 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001653
1654 lines =<< trim END
1655 vim9script
1656 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1657 g:optarg = opt
1658 g:listarg = l
1659 return nr
1660 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001661 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001662 Funcref(10)->assert_equal(10)
1663 g:optarg->assert_equal(12)
1664 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001665
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001666 Funcref(11, 22)->assert_equal(11)
1667 g:optarg->assert_equal(22)
1668 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001669
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001670 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1671 g:optarg->assert_equal(18)
1672 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001673 END
1674 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001675enddef
1676
1677let SomeFunc = function('len')
1678let NotAFunc = 'text'
1679
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001680def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001681 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001682 var Ref1: func(bool): string
1683 var Ref2: func(bool): number
1684 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001685 Ref3 = g:cond ? Ref1 : Ref2
1686
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001688 var Refa1: func(bool): number
1689 var Refa2: func(bool, number): number
1690 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001691 Refa3 = g:cond ? Refa1 : Refa2
1692
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001693 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001694 var Refb1: func(bool, string): number
1695 var Refb2: func(string, number): number
1696 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001697 Refb3 = g:cond ? Refb1 : Refb2
1698enddef
1699
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001700def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001701 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001702enddef
1703
1704def DefinedEvenLater(arg: string): string
1705 return arg
1706enddef
1707
1708def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001709 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001710 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001711enddef
1712
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001713def Test_nested_function_with_nextcmd()
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001714 var lines =<< trim END
1715 vim9script
1716 # Define an outer function
1717 def FirstFunction()
1718 # Define an inner function
1719 def SecondFunction()
1720 # the function has a body, a double free is detected.
1721 AAAAA
1722
1723 # enddef followed by | or } followed by # one or more characters
1724 enddef|BBBB
1725 enddef
1726
1727 # Compile all functions
1728 defcompile
1729 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001730 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001731enddef
1732
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001733def Test_nested_function_with_args_split()
1734 var lines =<< trim END
1735 vim9script
1736 def FirstFunction()
1737 def SecondFunction(
1738 )
1739 # had a double free if the right parenthesis of the nested function is
1740 # on the next line
1741
1742 enddef|BBBB
1743 enddef
1744 # Compile all functions
1745 defcompile
1746 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001747 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
1748
1749 lines =<< trim END
1750 vim9script
1751 def FirstFunction()
1752 func SecondFunction()
1753 endfunc|BBBB
1754 enddef
1755 defcompile
1756 END
1757 CheckScriptFailure(lines, 'E1173: Text found after endfunction: BBBB')
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001758enddef
1759
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001760def Test_error_in_function_args()
1761 var lines =<< trim END
1762 def FirstFunction()
1763 def SecondFunction(J =
1764 # Nois
1765 # one
1766
1767 enddef|BBBB
1768 enddef
1769 # Compile all functions
1770 defcompile
1771 END
1772 CheckScriptFailure(lines, 'E488:')
1773enddef
1774
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001775def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001776 CheckScriptFailure([
1777 'def Func(): number',
1778 'return "a"',
1779 'enddef',
1780 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001781 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001782 CheckScriptFailure([
1783 'def Func(): string',
1784 'return 1',
1785 'enddef',
1786 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001787 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001788 CheckScriptFailure([
1789 'def Func(): void',
1790 'return "a"',
1791 'enddef',
1792 'defcompile'],
1793 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001794 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001795 CheckScriptFailure([
1796 'def Func()',
1797 'return "a"',
1798 'enddef',
1799 'defcompile'],
1800 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001801 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001802
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001803 CheckScriptFailure([
1804 'def Func(): number',
1805 'return',
1806 'enddef',
1807 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001808 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001809
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001810 CheckScriptFailure([
1811 'def Func():number',
1812 'return 123',
1813 'enddef',
1814 'defcompile'], 'E1069:')
1815 delfunc! g:Func
1816
1817 CheckScriptFailure([
1818 'def Func() :number',
1819 'return 123',
1820 'enddef',
1821 'defcompile'], 'E1059:')
1822 delfunc! g:Func
1823
1824 CheckScriptFailure([
1825 'def Func() : number',
1826 'return 123',
1827 'enddef',
1828 'defcompile'], 'E1059:')
1829 delfunc! g:Func
1830
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001831 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001832 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001834 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001835 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001836 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001837
1838 CheckScriptFailure([
1839 'vim9script',
1840 'def FuncB()',
1841 ' return 123',
1842 'enddef',
1843 'def FuncA()',
1844 ' FuncB()',
1845 'enddef',
1846 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001847enddef
1848
1849def Test_arg_type_wrong()
1850 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001851 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001852 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001853 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001854 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1855 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001856enddef
1857
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001858def Test_white_space_before_comma()
1859 var lines =<< trim END
1860 vim9script
1861 def Func(a: number , b: number)
1862 enddef
1863 END
1864 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001865 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001866enddef
1867
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001868def Test_white_space_after_comma()
1869 var lines =<< trim END
1870 vim9script
1871 def Func(a: number,b: number)
1872 enddef
1873 END
1874 CheckScriptFailure(lines, 'E1069:')
1875
1876 # OK in legacy function
1877 lines =<< trim END
1878 vim9script
1879 func Func(a,b)
1880 endfunc
1881 END
1882 CheckScriptSuccess(lines)
1883enddef
1884
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001885def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001886 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001887 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001888 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001889 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001890 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001891 enddef
1892 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001893 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001894
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001895 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001896 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001897 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001898
Bram Moolenaar67979662020-06-20 22:50:47 +02001899 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001900 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001901 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001902
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001903 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001904 def ListFunc(arg: list<number>)
1905 listvar = arg
1906 enddef
1907 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001908 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001909
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001910 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001911 def DictFunc(arg: dict<number>)
1912 dictvar = arg
1913 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001914 {a: 1, b: 2}->DictFunc()
1915 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001917 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001918 enddef
1919 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001920 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001921
Bram Moolenaare0de1712020-12-02 17:36:54 +01001922 {a: 3, b: 4}->DictFunc()
1923 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001924
1925 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001926 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001927 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001928 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001929
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001930 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001931 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001932 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001933 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001934 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001935 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001936
1937 def UseString()
1938 'xyork'->MyFunc()
1939 enddef
1940 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001941 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001942
Bram Moolenaar10409562020-07-29 20:00:38 +02001943 def UseString2()
1944 "knife"->MyFunc()
1945 enddef
1946 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001947 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001948
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001949 # prepending a colon makes it a mark
1950 new
1951 setline(1, ['aaa', 'bbb', 'ccc'])
1952 normal! 3Gmt1G
1953 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001954 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001955 bwipe!
1956
Bram Moolenaare6b53242020-07-01 17:28:33 +02001957 MyFunc(
1958 'continued'
1959 )
1960 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001961 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001962 )
1963
1964 call MyFunc(
1965 'more'
1966 ..
1967 'lines'
1968 )
1969 assert_equal(
1970 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001971 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001972 END
1973 writefile(lines, 'Xcall.vim')
1974 source Xcall.vim
1975 delete('Xcall.vim')
1976enddef
1977
1978def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001979 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001980 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001981 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001982 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001983 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001984 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001985 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001986 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001987 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001988enddef
1989
Bram Moolenaar65b95452020-07-19 14:03:09 +02001990def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001991 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001992 vim9script
1993 def MyFunc(arg: string)
1994 echo arg
1995 enddef
1996 MyFunc(1234)
1997 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001998 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001999enddef
2000
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002001def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002002 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002003 vim9script
2004 const var = ''
2005 def MyFunc(arg: string)
2006 var = 'asdf'
2007 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02002008 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002009 END
2010 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002011 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002012 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002013
2014 lines =<< trim END
2015 const g:Aconst = 77
2016 def Change()
2017 # comment
2018 g:Aconst = 99
2019 enddef
2020 call Change()
2021 unlet g:Aconst
2022 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01002023 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002024enddef
2025
2026" Test that inside :function a Python function can be defined, :def is not
2027" recognized.
2028func Test_function_python()
2029 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02002030 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002031 execute py "<< EOF"
2032def do_something():
2033 return 1
2034EOF
2035endfunc
2036
2037def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002038 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002040 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002041 echo 'hello'
2042 enddef
2043
2044 def CallGoneSoon()
2045 GoneSoon()
2046 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02002047 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002049 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002050 CallGoneSoon()
2051 END
2052 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002053 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
2054 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002055
2056 delete('XToDelFunc')
2057enddef
2058
Bram Moolenaar7509ad82021-12-14 18:14:37 +00002059func Test_free_dict_while_in_funcstack()
2060 " relies on the sleep command
2061 CheckUnix
2062 call Run_Test_free_dict_while_in_funcstack()
2063endfunc
2064
2065def Run_Test_free_dict_while_in_funcstack()
Bram Moolenaar7509ad82021-12-14 18:14:37 +00002066 # this was freeing the TermRun() default argument dictionary while it was
2067 # still referenced in a funcstack_T
2068 var lines =<< trim END
2069 vim9script
2070
2071 &updatetime = 400
2072 def TermRun(_ = {})
2073 def Post()
2074 enddef
2075 def Exec()
2076 term_start('sleep 1', {
2077 term_finish: 'close',
2078 exit_cb: (_, _) => Post(),
2079 })
2080 enddef
2081 Exec()
2082 enddef
2083 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
2084 timer_start(100, (_) => feedkeys("\<F4>"))
2085 timer_start(1000, (_) => feedkeys("\<F4>"))
2086 sleep 1500m
2087 END
2088 CheckScriptSuccess(lines)
2089 nunmap <F4>
2090 set updatetime&
2091enddef
2092
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002093def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002094 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002095 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002096 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002097 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002098 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002099 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002100 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002101 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002102 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002103
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002104 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002105 g:Func1()->assert_equal('Func1')
2106 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002107
2108 delfunc! Func0
2109 delfunc! Func1
2110 delfunc! Func2
2111enddef
2112
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002113def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002114 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002115 vim9script
2116 func Func(arg)
2117 echo a:arg
2118 endfunc
2119 Func('text')
2120 END
2121 writefile(lines, 'XVim9Func')
2122 so XVim9Func
2123
2124 delete('XVim9Func')
2125enddef
2126
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002127let s:funcResult = 0
2128
2129def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002130 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002131enddef
2132
2133def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002134 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002135 return 1234
2136enddef
2137
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002138def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002139 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002140 return 'text'
2141enddef
2142
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002143def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002144 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002145enddef
2146
2147def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002148 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002149 return arg
2150enddef
2151
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002152def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002153 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002154enddef
2155
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002156def FuncOneArgRetString(arg: string): string
2157 return arg
2158enddef
2159
Bram Moolenaar89228602020-04-05 22:14:54 +02002160def FuncOneArgRetAny(arg: any): any
2161 return arg
2162enddef
2163
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002164def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002165 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002166 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002167 Ref1 = FuncNoArgNoRet
2168 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002169 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002170
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002171 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002172 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002173 Ref2 = FuncNoArgNoRet
2174 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002175 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002176
Bram Moolenaar53900992020-08-22 19:02:02 +02002177 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002178 Ref2 = FuncOneArgNoRet
2179 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002180 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002181
Bram Moolenaar53900992020-08-22 19:02:02 +02002182 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002183 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002184 Ref2()->assert_equal(1234)
2185 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002186
Bram Moolenaar53900992020-08-22 19:02:02 +02002187 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002188 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002189 Ref2(13)->assert_equal(13)
2190 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002191enddef
2192
Bram Moolenaar9978d472020-07-05 16:01:56 +02002193def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002194 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002195 for n in repeat([1], 3)
2196 res += n
2197 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002198 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002199
2200 res = 0
2201 for n in add([1, 2], 3)
2202 res += n
2203 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002204 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002205enddef
2206
Bram Moolenaar846178a2020-07-05 17:04:13 +02002207def Test_argv_return_type()
2208 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002209 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002210 for name in argv()
2211 res ..= name
2212 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002213 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002214enddef
2215
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002216def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002217 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002218 RefVoid = FuncNoArgNoRet
2219 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002220 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2221 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002222
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002223 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002224 RefAny = FuncNoArgRetNumber
2225 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002226 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2227 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002228
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002229 var RefAnyNoArgs: func: any = RefAny
2230
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002231 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002232 RefNr = FuncNoArgRetNumber
2233 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002234 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2235 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002236
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002237 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002238 RefStr = FuncNoArgRetString
2239 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002240 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2241 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002242enddef
2243
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002244def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002245 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002246
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002247 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2248 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2249 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2250 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2251 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2252 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 +02002253
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002254 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2255 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2256 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:')
2257 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002258enddef
2259
Bram Moolenaar89228602020-04-05 22:14:54 +02002260def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002261 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002262 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002263 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002264
2265 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002266 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002267
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002268 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002269 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002270 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002271
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002272 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002273enddef
2274
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002275def Test_func_common_type()
2276 def FuncOne(n: number): number
2277 return n
2278 enddef
2279 def FuncTwo(s: string): number
2280 return len(s)
2281 enddef
2282 def FuncThree(n: number, s: string): number
2283 return n + len(s)
2284 enddef
2285 var list = [FuncOne, FuncTwo, FuncThree]
2286 assert_equal(8, list[0](8))
2287 assert_equal(4, list[1]('word'))
2288 assert_equal(7, list[2](3, 'word'))
2289enddef
2290
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002291def MultiLine(
2292 arg1: string,
2293 arg2 = 1234,
2294 ...rest: list<string>
2295 ): string
2296 return arg1 .. arg2 .. join(rest, '-')
2297enddef
2298
Bram Moolenaar2c330432020-04-13 14:41:35 +02002299def MultiLineComment(
2300 arg1: string, # comment
2301 arg2 = 1234, # comment
2302 ...rest: list<string> # comment
2303 ): string # comment
2304 return arg1 .. arg2 .. join(rest, '-')
2305enddef
2306
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002307def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002308 MultiLine('text')->assert_equal('text1234')
2309 MultiLine('text', 777)->assert_equal('text777')
2310 MultiLine('text', 777, 'one')->assert_equal('text777one')
2311 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002312enddef
2313
Bram Moolenaar23e03252020-04-12 22:22:31 +02002314func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002315 call MultiLine('text')->assert_equal('text1234')
2316 call MultiLine('text', 777)->assert_equal('text777')
2317 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2318 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002319endfunc
2320
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002321
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002322" When using CheckScriptFailure() for the below test, E1010 is generated instead
2323" of E1056.
2324func Test_E1056_1059()
2325 let caught_1056 = 0
2326 try
2327 def F():
2328 return 1
2329 enddef
2330 catch /E1056:/
2331 let caught_1056 = 1
2332 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002333 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002334
2335 let caught_1059 = 0
2336 try
2337 def F5(items : list)
2338 echo 'a'
2339 enddef
2340 catch /E1059:/
2341 let caught_1059 = 1
2342 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002343 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002344endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002345
Bram Moolenaar015f4262020-05-05 21:25:22 +02002346func DelMe()
2347 echo 'DelMe'
2348endfunc
2349
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002350def Test_error_reporting()
2351 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002352 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002353 " comment
2354 def Func()
2355 # comment
2356 # comment
2357 invalid
2358 enddef
2359 defcompile
2360 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002361 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002362 try
2363 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002364 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002365 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002366 v:exception->assert_match('Invalid command: invalid')
2367 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002368 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002369 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002370
2371 # comment lines after the start of the function
2372 lines =<< trim END
2373 " comment
2374 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002375 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002376 # comment
2377 # comment
2378 invalid
2379 enddef
2380 defcompile
2381 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002382 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002383 try
2384 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002385 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002386 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002387 v:exception->assert_match('Invalid command: invalid')
2388 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002389 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002390 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002391
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002392 lines =<< trim END
2393 vim9script
2394 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002395 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002396 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002397 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002398 enddef
2399 defcompile
2400 Func()
2401 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002402 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002403 try
2404 source Xdef
2405 assert_report('should have failed')
2406 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002407 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002408 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002409 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002410
Bram Moolenaar08052222020-09-14 17:04:31 +02002411 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002412enddef
2413
Bram Moolenaar015f4262020-05-05 21:25:22 +02002414def Test_deleted_function()
2415 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002416 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002417 'delfunc g:DelMe',
2418 'echo RefMe()'], 'E117:')
2419enddef
2420
2421def Test_unknown_function()
2422 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002423 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002424 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002425enddef
2426
Bram Moolenaar328eac22021-01-07 19:23:08 +01002427def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002428 return Ref('more')
2429enddef
2430
2431def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002432 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002433 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002434enddef
2435
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002436def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002437 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002438 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002439enddef
2440
2441def Test_closure_ref_after_return()
2442 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002443 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002444 unlet g:Ref
2445enddef
2446
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002447def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002448 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002449 g:Extend = (s) => local->add(s)
2450 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002451enddef
2452
2453def Test_closure_two_refs()
2454 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002455 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002456 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002457 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002458 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002459 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002460
2461 unlet g:Extend
2462 unlet g:Read
2463enddef
2464
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002465def ReadRef(Ref: func(): list<string>): string
2466 return join(Ref(), ' ')
2467enddef
2468
Bram Moolenaar5e654232020-09-16 15:22:00 +02002469def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002470 Ref(add)
2471enddef
2472
2473def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002474 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002475 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002476 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002477 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002478 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002479 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002480
2481 unlet g:Extend
2482 unlet g:Read
2483enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002484
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002485def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002486 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002487 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002488enddef
2489
2490def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002491 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002492 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002493enddef
2494
2495def Test_closure_using_argument()
2496 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002497 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002498
2499 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002500 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002501
2502 unlet g:UseArg
2503 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002504
2505 var lines =<< trim END
2506 vim9script
2507 def Test(Fun: func(number): number): list<number>
2508 return map([1, 2, 3], (_, i) => Fun(i))
2509 enddef
2510 def Inc(nr: number): number
2511 return nr + 2
2512 enddef
2513 assert_equal([3, 4, 5], Test(Inc))
2514 END
2515 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002516enddef
2517
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002518def MakeGetAndAppendRefs()
2519 var local = 'a'
2520
2521 def Append(arg: string)
2522 local ..= arg
2523 enddef
2524 g:Append = Append
2525
2526 def Get(): string
2527 return local
2528 enddef
2529 g:Get = Get
2530enddef
2531
2532def Test_closure_append_get()
2533 MakeGetAndAppendRefs()
2534 g:Get()->assert_equal('a')
2535 g:Append('-b')
2536 g:Get()->assert_equal('a-b')
2537 g:Append('-c')
2538 g:Get()->assert_equal('a-b-c')
2539
2540 unlet g:Append
2541 unlet g:Get
2542enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002543
Bram Moolenaar04b12692020-05-04 23:24:44 +02002544def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002545 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002546 def Closure(arg: string): string
2547 return local .. arg
2548 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002549 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002550enddef
2551
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002552func GetResult(Ref)
2553 return a:Ref('some')
2554endfunc
2555
2556def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002557 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002558 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002559 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002560enddef
2561
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002562def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002563 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002564 vim9script
2565 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002566 var name = 0
2567 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002568 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002569 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002570 enddef
2571 Func()
2572 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002573 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002574enddef
2575
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002576def Test_nested_closure_used()
2577 var lines =<< trim END
2578 vim9script
2579 def Func()
2580 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002581 var Closure = () => x
2582 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002583 enddef
2584 Func()
2585 assert_equal('hello', g:Myclosure())
2586 END
2587 CheckScriptSuccess(lines)
2588enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002589
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002590def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002591 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002592 vim9script
2593 def FuncA()
2594 FuncB(0)
2595 enddef
2596 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002597 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002598 enddef
2599 FuncA()
2600 END
2601 CheckScriptFailure(lines, 'E1012:')
2602enddef
2603
Bram Moolenaarf112f302020-12-20 17:47:52 +01002604def Test_global_closure()
2605 var lines =<< trim END
2606 vim9script
2607 def ReverseEveryNLines(n: number, line1: number, line2: number)
2608 var mods = 'sil keepj keepp lockm '
2609 var range = ':' .. line1 .. ',' .. line2
2610 def g:Offset(): number
2611 var offset = (line('.') - line1 + 1) % n
2612 return offset != 0 ? offset : n
2613 enddef
2614 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2615 enddef
2616
2617 new
2618 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2619 ReverseEveryNLines(3, 1, 9)
2620 END
2621 CheckScriptSuccess(lines)
2622 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2623 assert_equal(expected, getline(1, 9))
2624 bwipe!
2625enddef
2626
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002627def Test_global_closure_called_directly()
2628 var lines =<< trim END
2629 vim9script
2630 def Outer()
2631 var x = 1
2632 def g:Inner()
2633 var y = x
2634 x += 1
2635 assert_equal(1, y)
2636 enddef
2637 g:Inner()
2638 assert_equal(2, x)
2639 enddef
2640 Outer()
2641 END
2642 CheckScriptSuccess(lines)
2643 delfunc g:Inner
2644enddef
2645
Bram Moolenaar69c76172021-12-02 16:38:52 +00002646def Test_closure_called_from_legacy()
2647 var lines =<< trim END
2648 vim9script
2649 def Func()
2650 var outer = 'foo'
2651 var F = () => {
2652 outer = 'bar'
2653 }
2654 execute printf('call %s()', string(F))
2655 enddef
2656 Func()
2657 END
2658 CheckScriptFailure(lines, 'E1248')
2659enddef
2660
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002661def Test_failure_in_called_function()
2662 # this was using the frame index as the return value
2663 var lines =<< trim END
2664 vim9script
2665 au TerminalWinOpen * eval [][0]
2666 def PopupTerm(a: any)
2667 # make sure typvals on stack are string
2668 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2669 FireEvent()
2670 enddef
2671 def FireEvent()
2672 do TerminalWinOpen
2673 enddef
2674 # use try/catch to make eval fail
2675 try
2676 call PopupTerm(0)
2677 catch
2678 endtry
2679 au! TerminalWinOpen
2680 END
2681 CheckScriptSuccess(lines)
2682enddef
2683
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002684def Test_nested_lambda()
2685 var lines =<< trim END
2686 vim9script
2687 def Func()
2688 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002689 var Lambda1 = () => 7
2690 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002691 var res = Lambda2()
2692 assert_equal([7, 4], res)
2693 enddef
2694 Func()
2695 END
2696 CheckScriptSuccess(lines)
2697enddef
2698
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002699def Test_double_nested_lambda()
2700 var lines =<< trim END
2701 vim9script
2702 def F(head: string): func(string): func(string): string
2703 return (sep: string): func(string): string => ((tail: string): string => {
2704 return head .. sep .. tail
2705 })
2706 enddef
2707 assert_equal('hello-there', F('hello')('-')('there'))
2708 END
2709 CheckScriptSuccess(lines)
2710enddef
2711
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002712def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002713 var lines =<< trim END
2714 vim9script
2715 def F(text: string): func(string): func(string): string
2716 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002717 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002718 })
2719 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002720 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002721 END
2722 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002723
2724 lines =<< trim END
2725 vim9script
2726 echo range(4)->mapnew((_, v) => {
2727 return range(v) ->mapnew((_, s) => {
2728 return string(s)
2729 })
2730 })
2731 END
2732 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002733
2734 lines =<< trim END
2735 vim9script
2736
2737 def s:func()
2738 range(10)
2739 ->mapnew((_, _) => ({
2740 key: range(10)->mapnew((_, _) => {
2741 return ' '
2742 }),
2743 }))
2744 enddef
2745
2746 defcomp
2747 END
2748 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002749enddef
2750
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002751def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002752 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002753 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002754enddef
2755
2756def Test_lambda_arg_shadows_func()
2757 assert_equal([42], Shadowed())
2758enddef
2759
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002760def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002761 var path: string = empty(dir)
2762 \ ? 'empty'
2763 \ : 'full'
2764 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002765enddef
2766
2767def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002768 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002769enddef
2770
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002771def Test_script_var_in_lambda()
2772 var lines =<< trim END
2773 vim9script
2774 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002775 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002776 END
2777 CheckScriptSuccess(lines)
2778enddef
2779
Bram Moolenaar5e654232020-09-16 15:22:00 +02002780def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002781 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002782 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002783 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002784 ->reverse()
2785 return x
2786enddef
2787
2788def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002789 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002790
2791 var lines =<< trim END
2792 vim9script
2793 var res = [{n: 1, m: 2, s: 'xxx'}]
2794 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2795 v.n,
2796 v.m,
2797 substitute(v.s, '.*', 'yyy', '')
2798 ))
2799 assert_equal(['1:2:yyy'], res)
2800 END
2801 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002802enddef
2803
Bram Moolenaarb6571982021-01-08 22:24:19 +01002804def Test_list_lambda()
2805 timer_start(1000, (_) => 0)
2806 var body = execute(timer_info()[0].callback
2807 ->string()
2808 ->substitute("('", ' ', '')
2809 ->substitute("')", '', '')
2810 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002811 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002812enddef
2813
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002814def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002815 var lines =<< trim END
2816 vim9script
2817 var flist: list<func>
2818 for i in range(10)
2819 var inloop = i
2820 flist[i] = () => inloop
2821 endfor
2822 END
2823 CheckScriptSuccess(lines)
2824
2825 lines =<< trim END
2826 vim9script
2827 if true
2828 var outloop = 5
2829 var flist: list<func>
2830 for i in range(10)
2831 flist[i] = () => outloop
2832 endfor
2833 endif
2834 END
2835 CheckScriptSuccess(lines)
2836
2837 lines =<< trim END
2838 vim9script
2839 if true
2840 var outloop = 5
2841 endif
2842 var flist: list<func>
2843 for i in range(10)
2844 flist[i] = () => outloop
2845 endfor
2846 END
2847 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002848
2849 lines =<< trim END
2850 vim9script
2851 for i in range(10)
2852 var Ref = () => 0
2853 endfor
2854 assert_equal(0, ((i) => 0)(0))
2855 END
2856 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002857enddef
2858
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002859def Test_legacy_lambda()
2860 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002861
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002862 var lines =<< trim END
2863 echo {x -> 'hello ' .. x}('foo')
2864 END
2865 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002866
2867 lines =<< trim END
2868 vim9script
2869 def Func()
2870 echo (() => 'no error')()
2871 enddef
2872 legacy call s:Func()
2873 END
2874 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002875enddef
2876
Bram Moolenaarce024c32021-06-26 13:00:49 +02002877def Test_legacy()
2878 var lines =<< trim END
2879 vim9script
2880 func g:LegacyFunction()
2881 let g:legacyvar = 1
2882 endfunc
2883 def Testit()
2884 legacy call g:LegacyFunction()
2885 enddef
2886 Testit()
2887 assert_equal(1, g:legacyvar)
2888 unlet g:legacyvar
2889 delfunc g:LegacyFunction
2890 END
2891 CheckScriptSuccess(lines)
2892enddef
2893
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002894def Test_legacy_errors()
2895 for cmd in ['if', 'elseif', 'else', 'endif',
2896 'for', 'endfor', 'continue', 'break',
2897 'while', 'endwhile',
2898 'try', 'catch', 'finally', 'endtry']
2899 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2900 endfor
2901enddef
2902
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002903def Test_call_legacy_with_dict()
2904 var lines =<< trim END
2905 vim9script
2906 func Legacy() dict
2907 let g:result = self.value
2908 endfunc
2909 def TestDirect()
2910 var d = {value: 'yes', func: Legacy}
2911 d.func()
2912 enddef
2913 TestDirect()
2914 assert_equal('yes', g:result)
2915 unlet g:result
2916
2917 def TestIndirect()
2918 var d = {value: 'foo', func: Legacy}
2919 var Fi = d.func
2920 Fi()
2921 enddef
2922 TestIndirect()
2923 assert_equal('foo', g:result)
2924 unlet g:result
2925
2926 var d = {value: 'bar', func: Legacy}
2927 d.func()
2928 assert_equal('bar', g:result)
2929 unlet g:result
2930 END
2931 CheckScriptSuccess(lines)
2932enddef
2933
Bram Moolenaarab360522021-01-10 14:02:28 +01002934def DoFilterThis(a: string): list<string>
2935 # closure nested inside another closure using argument
2936 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2937 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2938enddef
2939
2940def Test_nested_closure_using_argument()
2941 assert_equal(['x', 'x2'], DoFilterThis('x'))
2942enddef
2943
Bram Moolenaar0186e582021-01-10 18:33:11 +01002944def Test_triple_nested_closure()
2945 var what = 'x'
2946 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2947 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2948 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2949enddef
2950
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002951func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002952 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002953 call Run_Test_silent_echo()
2954endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002955
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002956def Run_Test_silent_echo()
2957 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002958 vim9script
2959 def EchoNothing()
2960 silent echo ''
2961 enddef
2962 defcompile
2963 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002964 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002965
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002966 # Check that the balloon shows up after a mouse move
2967 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2968 term_sendkeys(buf, ":abc")
2969 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002970
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002971 # clean up
2972 StopVimInTerminal(buf)
2973 delete('XTest_silent_echo')
2974enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002975
Bram Moolenaar171fb922020-10-28 16:54:47 +01002976def SilentlyError()
2977 execute('silent! invalid')
2978 g:did_it = 'yes'
2979enddef
2980
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002981func UserError()
2982 silent! invalid
2983endfunc
2984
2985def SilentlyUserError()
2986 UserError()
2987 g:did_it = 'yes'
2988enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002989
2990" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002991func Test_ignore_silent_error()
2992 let g:did_it = 'no'
2993 call SilentlyError()
2994 call assert_equal('yes', g:did_it)
2995
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002996 let g:did_it = 'no'
2997 call SilentlyUserError()
2998 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002999
3000 unlet g:did_it
3001endfunc
3002
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003003def Test_ignore_silent_error_in_filter()
3004 var lines =<< trim END
3005 vim9script
3006 def Filter(winid: number, key: string): bool
3007 if key == 'o'
3008 silent! eval [][0]
3009 return true
3010 endif
3011 return popup_filter_menu(winid, key)
3012 enddef
3013
Bram Moolenaare0de1712020-12-02 17:36:54 +01003014 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003015 feedkeys("o\r", 'xnt')
3016 END
3017 CheckScriptSuccess(lines)
3018enddef
3019
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02003020def Fibonacci(n: number): number
3021 if n < 2
3022 return n
3023 else
3024 return Fibonacci(n - 1) + Fibonacci(n - 2)
3025 endif
3026enddef
3027
Bram Moolenaar985116a2020-07-12 17:31:09 +02003028def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003029 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02003030enddef
3031
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003032def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01003033 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003034 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01003035 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003036 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003037 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003038enddef
3039
3040def Test_closure_in_map()
3041 mkdir('XclosureDir/tdir', 'p')
3042 writefile(['111'], 'XclosureDir/file1')
3043 writefile(['222'], 'XclosureDir/file2')
3044 writefile(['333'], 'XclosureDir/tdir/file3')
3045
Bram Moolenaare0de1712020-12-02 17:36:54 +01003046 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003047
3048 delete('XclosureDir', 'rf')
3049enddef
3050
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003051def Test_invalid_function_name()
3052 var lines =<< trim END
3053 vim9script
3054 def s: list<string>
3055 END
3056 CheckScriptFailure(lines, 'E129:')
3057
3058 lines =<< trim END
3059 vim9script
3060 def g: list<string>
3061 END
3062 CheckScriptFailure(lines, 'E129:')
3063
3064 lines =<< trim END
3065 vim9script
3066 def <SID>: list<string>
3067 END
3068 CheckScriptFailure(lines, 'E884:')
3069
3070 lines =<< trim END
3071 vim9script
3072 def F list<string>
3073 END
3074 CheckScriptFailure(lines, 'E488:')
3075enddef
3076
Bram Moolenaara90afb92020-07-15 22:38:56 +02003077def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003078 var lines =<< trim END
3079 var Xsetlist: func
3080 Xsetlist = function('setloclist', [0])
3081 Xsetlist([], ' ', {title: 'test'})
3082 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003083
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003084 Xsetlist = function('setloclist', [0, [], ' '])
3085 Xsetlist({title: 'test'})
3086 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003087
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003088 Xsetlist = function('setqflist')
3089 Xsetlist([], ' ', {title: 'test'})
3090 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003091
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003092 Xsetlist = function('setqflist', [[], ' '])
3093 Xsetlist({title: 'test'})
3094 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02003095
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003096 var Len: func: number = function('len', ['word'])
3097 assert_equal(4, Len())
3098
3099 var RepeatFunc = function('repeat', ['o'])
3100 assert_equal('ooooo', RepeatFunc(5))
3101 END
3102 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003103
3104 lines =<< trim END
3105 vim9script
3106 def Foo(Parser: any)
3107 enddef
3108 var Expr: func(dict<any>): dict<any>
3109 const Call = Foo(Expr)
3110 END
3111 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003112enddef
3113
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003114def Test_cmd_modifier()
3115 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003116 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003117enddef
3118
3119def Test_restore_modifiers()
3120 # check that when compiling a :def function command modifiers are not messed
3121 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003122 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003123 vim9script
3124 set eventignore=
3125 autocmd QuickFixCmdPost * copen
3126 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003127 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003128 enddef
3129 func Func()
3130 noautocmd call s:AutocmdsDisabled()
3131 let g:ei_after = &eventignore
3132 endfunc
3133 Func()
3134 END
3135 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003136 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003137enddef
3138
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003139def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003140 eval 1 + 2
3141 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003142 # call not on fourth line
3143 StackBot()
3144enddef
3145
3146def StackBot()
3147 # throw an error
3148 eval [][0]
3149enddef
3150
3151def Test_callstack_def()
3152 try
3153 StackTop()
3154 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003155 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003156 endtry
3157enddef
3158
Bram Moolenaare8211a32020-10-09 22:04:29 +02003159" Re-using spot for variable used in block
3160def Test_block_scoped_var()
3161 var lines =<< trim END
3162 vim9script
3163 def Func()
3164 var x = ['a', 'b', 'c']
3165 if 1
3166 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003167 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003168 endif
3169 var z = x
3170 assert_equal(['x', 'x', 'x'], z)
3171 enddef
3172 Func()
3173 END
3174 CheckScriptSuccess(lines)
3175enddef
3176
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003177def Test_reset_did_emsg()
3178 var lines =<< trim END
3179 @s = 'blah'
3180 au BufWinLeave * #
3181 def Func()
3182 var winid = popup_create('popup', {})
3183 exe '*s'
3184 popup_close(winid)
3185 enddef
3186 Func()
3187 END
3188 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003189 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003190enddef
3191
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003192def Test_did_emsg_reset()
3193 # executing an autocommand resets did_emsg, this should not result in a
3194 # builtin function considered failing
3195 var lines =<< trim END
3196 vim9script
3197 au BufWinLeave * #
3198 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003199 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003200 eval [][0]
3201 enddef
3202 nno <F3> <cmd>call <sid>Func()<cr>
3203 feedkeys("\<F3>\e", 'xt')
3204 END
3205 writefile(lines, 'XemsgReset')
3206 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3207 delete('XemsgReset')
3208 nunmap <F3>
3209 au! BufWinLeave
3210enddef
3211
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003212def Test_abort_with_silent_call()
3213 var lines =<< trim END
3214 vim9script
3215 g:result = 'none'
3216 def Func()
3217 g:result += 3
3218 g:result = 'yes'
3219 enddef
3220 # error is silenced, but function aborts on error
3221 silent! Func()
3222 assert_equal('none', g:result)
3223 unlet g:result
3224 END
3225 CheckScriptSuccess(lines)
3226enddef
3227
Bram Moolenaarf665e972020-12-05 19:17:16 +01003228def Test_continues_with_silent_error()
3229 var lines =<< trim END
3230 vim9script
3231 g:result = 'none'
3232 def Func()
3233 silent! g:result += 3
3234 g:result = 'yes'
3235 enddef
3236 # error is silenced, function does not abort
3237 Func()
3238 assert_equal('yes', g:result)
3239 unlet g:result
3240 END
3241 CheckScriptSuccess(lines)
3242enddef
3243
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003244def Test_abort_even_with_silent()
3245 var lines =<< trim END
3246 vim9script
3247 g:result = 'none'
3248 def Func()
3249 eval {-> ''}() .. '' .. {}['X']
3250 g:result = 'yes'
3251 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003252 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003253 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003254 unlet g:result
3255 END
3256 CheckScriptSuccess(lines)
3257enddef
3258
Bram Moolenaarf665e972020-12-05 19:17:16 +01003259def Test_cmdmod_silent_restored()
3260 var lines =<< trim END
3261 vim9script
3262 def Func()
3263 g:result = 'none'
3264 silent! g:result += 3
3265 g:result = 'none'
3266 g:result += 3
3267 enddef
3268 Func()
3269 END
3270 # can't use CheckScriptFailure, it ignores the :silent!
3271 var fname = 'Xdefsilent'
3272 writefile(lines, fname)
3273 var caught = 'no'
3274 try
3275 exe 'source ' .. fname
3276 catch /E1030:/
3277 caught = 'yes'
3278 assert_match('Func, line 4', v:throwpoint)
3279 endtry
3280 assert_equal('yes', caught)
3281 delete(fname)
3282enddef
3283
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003284def Test_cmdmod_silent_nested()
3285 var lines =<< trim END
3286 vim9script
3287 var result = ''
3288
3289 def Error()
3290 result ..= 'Eb'
3291 eval [][0]
3292 result ..= 'Ea'
3293 enddef
3294
3295 def Crash()
3296 result ..= 'Cb'
3297 sil! Error()
3298 result ..= 'Ca'
3299 enddef
3300
3301 Crash()
3302 assert_equal('CbEbEaCa', result)
3303 END
3304 CheckScriptSuccess(lines)
3305enddef
3306
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003307def Test_dict_member_with_silent()
3308 var lines =<< trim END
3309 vim9script
3310 g:result = 'none'
3311 var d: dict<any>
3312 def Func()
3313 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003314 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003315 catch
3316 endtry
3317 enddef
3318 silent! Func()
3319 assert_equal('0', g:result)
3320 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003321 END
3322 CheckScriptSuccess(lines)
3323enddef
3324
Bram Moolenaarf9041332021-01-21 19:41:16 +01003325def Test_skip_cmds_with_silent()
3326 var lines =<< trim END
3327 vim9script
3328
3329 def Func(b: bool)
3330 Crash()
3331 enddef
3332
3333 def Crash()
3334 sil! :/not found/d _
3335 sil! :/not found/put _
3336 enddef
3337
3338 Func(true)
3339 END
3340 CheckScriptSuccess(lines)
3341enddef
3342
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003343def Test_opfunc()
3344 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3345 def g:Opfunc(_: any): string
3346 setline(1, 'ASDF')
3347 return ''
3348 enddef
3349 new
3350 setline(1, 'asdf')
3351 feedkeys("\<F3>$", 'x')
3352 assert_equal('ASDF', getline(1))
3353
3354 bwipe!
3355 nunmap <F3>
3356enddef
3357
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003358func Test_opfunc_error()
3359 CheckScreendump
3360 call Run_Test_opfunc_error()
3361endfunc
3362
3363def Run_Test_opfunc_error()
3364 # test that the error from Opfunc() is displayed right away
3365 var lines =<< trim END
3366 vim9script
3367
3368 def Opfunc(type: string)
3369 try
3370 eval [][0]
3371 catch /nothing/ # error not caught
3372 endtry
3373 enddef
3374 &operatorfunc = Opfunc
3375 nnoremap <expr> l <SID>L()
3376 def L(): string
3377 return 'l'
3378 enddef
3379 'x'->repeat(10)->setline(1)
3380 feedkeys('g@l', 'n')
3381 feedkeys('llll')
3382 END
3383 call writefile(lines, 'XTest_opfunc_error')
3384
3385 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003386 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003387 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003388
3389 # clean up
3390 StopVimInTerminal(buf)
3391 delete('XTest_opfunc_error')
3392enddef
3393
Bram Moolenaar077a4232020-12-22 18:33:27 +01003394" this was crashing on exit
3395def Test_nested_lambda_in_closure()
3396 var lines =<< trim END
3397 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003398 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003399 def Outer()
3400 def g:Inner()
3401 echo map([1, 2, 3], {_, v -> v + 1})
3402 enddef
3403 g:Inner()
3404 enddef
3405 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003406 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003407 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003408 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003409 return
3410 endif
3411 assert_equal(['Done'], readfile('XnestedDone'))
3412 delete('XnestedDone')
3413enddef
3414
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003415def Test_check_func_arg_types()
3416 var lines =<< trim END
3417 vim9script
3418 def F1(x: string): string
3419 return x
3420 enddef
3421
3422 def F2(x: number): number
3423 return x + 1
3424 enddef
3425
3426 def G(g: func): dict<func>
3427 return {f: g}
3428 enddef
3429
3430 def H(d: dict<func>): string
3431 return d.f('a')
3432 enddef
3433 END
3434
3435 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3436 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3437enddef
3438
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003439def Test_list_any_type_checked()
3440 var lines =<< trim END
3441 vim9script
3442 def Foo()
3443 --decl--
3444 Bar(l)
3445 enddef
3446 def Bar(ll: list<dict<any>>)
3447 enddef
3448 Foo()
3449 END
3450 lines[2] = 'var l: list<any>'
3451 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3452
3453 lines[2] = 'var l: list<any> = []'
3454 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3455
3456 lines[2] = 'var l: list<any> = [11]'
3457 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3458enddef
3459
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003460def Test_compile_error()
3461 var lines =<< trim END
3462 def g:Broken()
3463 echo 'a' + {}
3464 enddef
3465 call g:Broken()
3466 END
3467 # First call: compilation error
3468 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3469
3470 # Second call won't try compiling again
3471 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003472 delfunc g:Broken
3473
3474 # No error when compiling with :silent!
3475 lines =<< trim END
3476 def g:Broken()
3477 echo 'a' + []
3478 enddef
3479 silent! defcompile
3480 END
3481 CheckScriptSuccess(lines)
3482
3483 # Calling the function won't try compiling again
3484 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3485 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003486enddef
3487
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003488def Test_ignored_argument()
3489 var lines =<< trim END
3490 vim9script
3491 def Ignore(_, _): string
3492 return 'yes'
3493 enddef
3494 assert_equal('yes', Ignore(1, 2))
3495
3496 func Ok(_)
3497 return a:_
3498 endfunc
3499 assert_equal('ok', Ok('ok'))
3500
3501 func Oktoo()
3502 let _ = 'too'
3503 return _
3504 endfunc
3505 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003506
3507 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003508 END
3509 CheckScriptSuccess(lines)
3510
3511 lines =<< trim END
3512 def Ignore(_: string): string
3513 return _
3514 enddef
3515 defcompile
3516 END
3517 CheckScriptFailure(lines, 'E1181:', 1)
3518
3519 lines =<< trim END
3520 var _ = 1
3521 END
3522 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003523
3524 lines =<< trim END
3525 var x = _
3526 END
3527 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003528enddef
3529
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003530def Test_too_many_arguments()
3531 var lines =<< trim END
3532 echo [0, 1, 2]->map(() => 123)
3533 END
3534 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3535
3536 lines =<< trim END
3537 echo [0, 1, 2]->map((_) => 123)
3538 END
3539 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3540enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003541
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003542def Test_closing_brace_at_start_of_line()
3543 var lines =<< trim END
3544 def Func()
3545 enddef
3546 Func(
3547 )
3548 END
3549 call CheckDefAndScriptSuccess(lines)
3550enddef
3551
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003552func CreateMydict()
3553 let g:mydict = {}
3554 func g:mydict.afunc()
3555 let g:result = self.key
3556 endfunc
3557endfunc
3558
3559def Test_numbered_function_reference()
3560 CreateMydict()
3561 var output = execute('legacy func g:mydict.afunc')
3562 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3563 execute 'function(' .. funcName .. ', [], {key: 42})()'
3564 # check that the function still exists
3565 assert_equal(output, execute('legacy func g:mydict.afunc'))
3566 unlet g:mydict
3567enddef
3568
Bram Moolenaard3a11782022-01-05 16:50:40 +00003569def Test_go_beyond_end_of_cmd()
3570 # this was reading the byte after the end of the line
3571 var lines =<< trim END
3572 def F()
3573 cal
3574 enddef
3575 defcompile
3576 END
3577 CheckScriptFailure(lines, 'E476:')
3578enddef
3579
Bram Moolenaar20677332021-06-06 17:02:53 +02003580if has('python3')
3581 def Test_python3_heredoc()
3582 py3 << trim EOF
3583 import vim
3584 vim.vars['didit'] = 'yes'
3585 EOF
3586 assert_equal('yes', g:didit)
3587
3588 python3 << trim EOF
3589 import vim
3590 vim.vars['didit'] = 'again'
3591 EOF
3592 assert_equal('again', g:didit)
3593 enddef
3594endif
3595
3596" This messes up syntax highlight, keep near the end.
3597if has('lua')
3598 def Test_lua_heredoc()
3599 g:d = {}
3600 lua << trim EOF
3601 x = vim.eval('g:d')
3602 x['key'] = 'val'
3603 EOF
3604 assert_equal('val', g:d.key)
3605 enddef
3606endif
3607
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003608
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003609" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker