blob: 9e420ade748b9794fcdeb08fc305743ce67b53ef [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 Moolenaar5deeb3f2020-04-05 17:08:17 +02001760def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001761 CheckScriptFailure([
1762 'def Func(): number',
1763 'return "a"',
1764 'enddef',
1765 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001766 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001767 CheckScriptFailure([
1768 'def Func(): string',
1769 'return 1',
1770 'enddef',
1771 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001772 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001773 CheckScriptFailure([
1774 'def Func(): void',
1775 'return "a"',
1776 'enddef',
1777 'defcompile'],
1778 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001779 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001780 CheckScriptFailure([
1781 'def Func()',
1782 'return "a"',
1783 'enddef',
1784 'defcompile'],
1785 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001786 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001787
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001788 CheckScriptFailure([
1789 'def Func(): number',
1790 'return',
1791 'enddef',
1792 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001793 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001794
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001795 CheckScriptFailure([
1796 'def Func():number',
1797 'return 123',
1798 'enddef',
1799 'defcompile'], 'E1069:')
1800 delfunc! g:Func
1801
1802 CheckScriptFailure([
1803 'def Func() :number',
1804 'return 123',
1805 'enddef',
1806 'defcompile'], 'E1059:')
1807 delfunc! g:Func
1808
1809 CheckScriptFailure([
1810 'def Func() : number',
1811 'return 123',
1812 'enddef',
1813 'defcompile'], 'E1059:')
1814 delfunc! g:Func
1815
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001816 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001817 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001818 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001819 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001820 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001821 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001822
1823 CheckScriptFailure([
1824 'vim9script',
1825 'def FuncB()',
1826 ' return 123',
1827 'enddef',
1828 'def FuncA()',
1829 ' FuncB()',
1830 'enddef',
1831 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001832enddef
1833
1834def Test_arg_type_wrong()
1835 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001836 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001837 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001838 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001839 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1840 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001841enddef
1842
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001843def Test_white_space_before_comma()
1844 var lines =<< trim END
1845 vim9script
1846 def Func(a: number , b: number)
1847 enddef
1848 END
1849 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001850 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001851enddef
1852
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001853def Test_white_space_after_comma()
1854 var lines =<< trim END
1855 vim9script
1856 def Func(a: number,b: number)
1857 enddef
1858 END
1859 CheckScriptFailure(lines, 'E1069:')
1860
1861 # OK in legacy function
1862 lines =<< trim END
1863 vim9script
1864 func Func(a,b)
1865 endfunc
1866 END
1867 CheckScriptSuccess(lines)
1868enddef
1869
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001871 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001872 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001873 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001874 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001875 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876 enddef
1877 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001878 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001879
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001880 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001881 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001882 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001883
Bram Moolenaar67979662020-06-20 22:50:47 +02001884 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001885 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001886 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001887
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001888 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001889 def ListFunc(arg: list<number>)
1890 listvar = arg
1891 enddef
1892 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001893 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001894
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001895 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001896 def DictFunc(arg: dict<number>)
1897 dictvar = arg
1898 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001899 {a: 1, b: 2}->DictFunc()
1900 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001901 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001902 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001903 enddef
1904 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001905 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001906
Bram Moolenaare0de1712020-12-02 17:36:54 +01001907 {a: 3, b: 4}->DictFunc()
1908 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001909
1910 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001911 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001912 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001913 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001914
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001915 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001916 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001917 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001918 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001919 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001920 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001921
1922 def UseString()
1923 'xyork'->MyFunc()
1924 enddef
1925 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001926 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001927
Bram Moolenaar10409562020-07-29 20:00:38 +02001928 def UseString2()
1929 "knife"->MyFunc()
1930 enddef
1931 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001932 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001933
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001934 # prepending a colon makes it a mark
1935 new
1936 setline(1, ['aaa', 'bbb', 'ccc'])
1937 normal! 3Gmt1G
1938 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001939 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001940 bwipe!
1941
Bram Moolenaare6b53242020-07-01 17:28:33 +02001942 MyFunc(
1943 'continued'
1944 )
1945 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001946 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001947 )
1948
1949 call MyFunc(
1950 'more'
1951 ..
1952 'lines'
1953 )
1954 assert_equal(
1955 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001956 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001957 END
1958 writefile(lines, 'Xcall.vim')
1959 source Xcall.vim
1960 delete('Xcall.vim')
1961enddef
1962
1963def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001964 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001965 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001966 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001967 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001968 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001969 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001970 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001971 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001972 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001973enddef
1974
Bram Moolenaar65b95452020-07-19 14:03:09 +02001975def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001976 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001977 vim9script
1978 def MyFunc(arg: string)
1979 echo arg
1980 enddef
1981 MyFunc(1234)
1982 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001983 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001984enddef
1985
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001986def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001987 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001988 vim9script
1989 const var = ''
1990 def MyFunc(arg: string)
1991 var = 'asdf'
1992 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001993 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001994 END
1995 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001996 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001997 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001998
1999 lines =<< trim END
2000 const g:Aconst = 77
2001 def Change()
2002 # comment
2003 g:Aconst = 99
2004 enddef
2005 call Change()
2006 unlet g:Aconst
2007 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01002008 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002009enddef
2010
2011" Test that inside :function a Python function can be defined, :def is not
2012" recognized.
2013func Test_function_python()
2014 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02002015 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002016 execute py "<< EOF"
2017def do_something():
2018 return 1
2019EOF
2020endfunc
2021
2022def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002023 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002024 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002025 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002026 echo 'hello'
2027 enddef
2028
2029 def CallGoneSoon()
2030 GoneSoon()
2031 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02002032 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002033
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002034 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002035 CallGoneSoon()
2036 END
2037 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002038 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
2039 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002040
2041 delete('XToDelFunc')
2042enddef
2043
Bram Moolenaar7509ad82021-12-14 18:14:37 +00002044func Test_free_dict_while_in_funcstack()
2045 " relies on the sleep command
2046 CheckUnix
2047 call Run_Test_free_dict_while_in_funcstack()
2048endfunc
2049
2050def Run_Test_free_dict_while_in_funcstack()
2051
2052 # this was freeing the TermRun() default argument dictionary while it was
2053 # still referenced in a funcstack_T
2054 var lines =<< trim END
2055 vim9script
2056
2057 &updatetime = 400
2058 def TermRun(_ = {})
2059 def Post()
2060 enddef
2061 def Exec()
2062 term_start('sleep 1', {
2063 term_finish: 'close',
2064 exit_cb: (_, _) => Post(),
2065 })
2066 enddef
2067 Exec()
2068 enddef
2069 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
2070 timer_start(100, (_) => feedkeys("\<F4>"))
2071 timer_start(1000, (_) => feedkeys("\<F4>"))
2072 sleep 1500m
2073 END
2074 CheckScriptSuccess(lines)
2075 nunmap <F4>
2076 set updatetime&
2077enddef
2078
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002079def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002080 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002081 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002082 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002083 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002084 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002085 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002086 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002087 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002088 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002089
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002090 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002091 g:Func1()->assert_equal('Func1')
2092 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002093
2094 delfunc! Func0
2095 delfunc! Func1
2096 delfunc! Func2
2097enddef
2098
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002099def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002100 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002101 vim9script
2102 func Func(arg)
2103 echo a:arg
2104 endfunc
2105 Func('text')
2106 END
2107 writefile(lines, 'XVim9Func')
2108 so XVim9Func
2109
2110 delete('XVim9Func')
2111enddef
2112
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002113let s:funcResult = 0
2114
2115def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002116 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002117enddef
2118
2119def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002120 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002121 return 1234
2122enddef
2123
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002124def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002125 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002126 return 'text'
2127enddef
2128
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002129def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002130 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002131enddef
2132
2133def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002134 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002135 return arg
2136enddef
2137
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002138def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002139 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002140enddef
2141
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002142def FuncOneArgRetString(arg: string): string
2143 return arg
2144enddef
2145
Bram Moolenaar89228602020-04-05 22:14:54 +02002146def FuncOneArgRetAny(arg: any): any
2147 return arg
2148enddef
2149
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002150def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002151 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002152 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002153 Ref1 = FuncNoArgNoRet
2154 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002155 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002156
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002157 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002158 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002159 Ref2 = FuncNoArgNoRet
2160 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002161 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002162
Bram Moolenaar53900992020-08-22 19:02:02 +02002163 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002164 Ref2 = FuncOneArgNoRet
2165 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002166 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002167
Bram Moolenaar53900992020-08-22 19:02:02 +02002168 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002169 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002170 Ref2()->assert_equal(1234)
2171 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002172
Bram Moolenaar53900992020-08-22 19:02:02 +02002173 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002174 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002175 Ref2(13)->assert_equal(13)
2176 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002177enddef
2178
Bram Moolenaar9978d472020-07-05 16:01:56 +02002179def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002180 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002181 for n in repeat([1], 3)
2182 res += n
2183 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002184 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002185
2186 res = 0
2187 for n in add([1, 2], 3)
2188 res += n
2189 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002190 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002191enddef
2192
Bram Moolenaar846178a2020-07-05 17:04:13 +02002193def Test_argv_return_type()
2194 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002195 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002196 for name in argv()
2197 res ..= name
2198 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002199 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002200enddef
2201
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002202def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002203 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002204 RefVoid = FuncNoArgNoRet
2205 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002206 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2207 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002208
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002209 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002210 RefAny = FuncNoArgRetNumber
2211 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002212 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2213 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002214
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002215 var RefAnyNoArgs: func: any = RefAny
2216
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002217 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002218 RefNr = FuncNoArgRetNumber
2219 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002220 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2221 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002222
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002223 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002224 RefStr = FuncNoArgRetString
2225 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002226 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2227 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002228enddef
2229
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002230def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002231 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002232
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002233 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2234 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2235 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2236 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2237 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2238 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 +02002239
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002240 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2241 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2242 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:')
2243 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002244enddef
2245
Bram Moolenaar89228602020-04-05 22:14:54 +02002246def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002247 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002248 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002249 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002250
2251 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002252 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002253
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002254 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002255 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002256 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002257
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002258 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002259enddef
2260
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002261def Test_func_common_type()
2262 def FuncOne(n: number): number
2263 return n
2264 enddef
2265 def FuncTwo(s: string): number
2266 return len(s)
2267 enddef
2268 def FuncThree(n: number, s: string): number
2269 return n + len(s)
2270 enddef
2271 var list = [FuncOne, FuncTwo, FuncThree]
2272 assert_equal(8, list[0](8))
2273 assert_equal(4, list[1]('word'))
2274 assert_equal(7, list[2](3, 'word'))
2275enddef
2276
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002277def MultiLine(
2278 arg1: string,
2279 arg2 = 1234,
2280 ...rest: list<string>
2281 ): string
2282 return arg1 .. arg2 .. join(rest, '-')
2283enddef
2284
Bram Moolenaar2c330432020-04-13 14:41:35 +02002285def MultiLineComment(
2286 arg1: string, # comment
2287 arg2 = 1234, # comment
2288 ...rest: list<string> # comment
2289 ): string # comment
2290 return arg1 .. arg2 .. join(rest, '-')
2291enddef
2292
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002293def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002294 MultiLine('text')->assert_equal('text1234')
2295 MultiLine('text', 777)->assert_equal('text777')
2296 MultiLine('text', 777, 'one')->assert_equal('text777one')
2297 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002298enddef
2299
Bram Moolenaar23e03252020-04-12 22:22:31 +02002300func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002301 call MultiLine('text')->assert_equal('text1234')
2302 call MultiLine('text', 777)->assert_equal('text777')
2303 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2304 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002305endfunc
2306
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002307
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002308" When using CheckScriptFailure() for the below test, E1010 is generated instead
2309" of E1056.
2310func Test_E1056_1059()
2311 let caught_1056 = 0
2312 try
2313 def F():
2314 return 1
2315 enddef
2316 catch /E1056:/
2317 let caught_1056 = 1
2318 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002319 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002320
2321 let caught_1059 = 0
2322 try
2323 def F5(items : list)
2324 echo 'a'
2325 enddef
2326 catch /E1059:/
2327 let caught_1059 = 1
2328 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002329 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002330endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002331
Bram Moolenaar015f4262020-05-05 21:25:22 +02002332func DelMe()
2333 echo 'DelMe'
2334endfunc
2335
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002336def Test_error_reporting()
2337 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002338 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002339 " comment
2340 def Func()
2341 # comment
2342 # comment
2343 invalid
2344 enddef
2345 defcompile
2346 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002347 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002348 try
2349 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002350 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002351 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002352 v:exception->assert_match('Invalid command: invalid')
2353 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002354 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002355 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002356
2357 # comment lines after the start of the function
2358 lines =<< trim END
2359 " comment
2360 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002361 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002362 # comment
2363 # comment
2364 invalid
2365 enddef
2366 defcompile
2367 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002368 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002369 try
2370 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002371 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002372 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002373 v:exception->assert_match('Invalid command: invalid')
2374 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002375 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002376 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002377
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002378 lines =<< trim END
2379 vim9script
2380 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002381 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002382 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002383 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002384 enddef
2385 defcompile
2386 Func()
2387 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002388 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002389 try
2390 source Xdef
2391 assert_report('should have failed')
2392 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002393 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002394 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002395 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002396
Bram Moolenaar08052222020-09-14 17:04:31 +02002397 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002398enddef
2399
Bram Moolenaar015f4262020-05-05 21:25:22 +02002400def Test_deleted_function()
2401 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002402 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002403 'delfunc g:DelMe',
2404 'echo RefMe()'], 'E117:')
2405enddef
2406
2407def Test_unknown_function()
2408 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002409 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002410 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002411enddef
2412
Bram Moolenaar328eac22021-01-07 19:23:08 +01002413def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002414 return Ref('more')
2415enddef
2416
2417def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002418 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002419 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002420enddef
2421
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002422def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002423 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002424 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002425enddef
2426
2427def Test_closure_ref_after_return()
2428 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002429 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002430 unlet g:Ref
2431enddef
2432
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002433def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002434 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002435 g:Extend = (s) => local->add(s)
2436 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002437enddef
2438
2439def Test_closure_two_refs()
2440 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002441 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002442 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002443 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002444 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002445 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002446
2447 unlet g:Extend
2448 unlet g:Read
2449enddef
2450
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002451def ReadRef(Ref: func(): list<string>): string
2452 return join(Ref(), ' ')
2453enddef
2454
Bram Moolenaar5e654232020-09-16 15:22:00 +02002455def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002456 Ref(add)
2457enddef
2458
2459def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002460 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002461 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002462 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002463 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002464 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002465 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002466
2467 unlet g:Extend
2468 unlet g:Read
2469enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002470
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002471def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002472 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002473 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002474enddef
2475
2476def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002477 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002478 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002479enddef
2480
2481def Test_closure_using_argument()
2482 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002483 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002484
2485 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002486 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002487
2488 unlet g:UseArg
2489 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002490
2491 var lines =<< trim END
2492 vim9script
2493 def Test(Fun: func(number): number): list<number>
2494 return map([1, 2, 3], (_, i) => Fun(i))
2495 enddef
2496 def Inc(nr: number): number
2497 return nr + 2
2498 enddef
2499 assert_equal([3, 4, 5], Test(Inc))
2500 END
2501 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002502enddef
2503
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002504def MakeGetAndAppendRefs()
2505 var local = 'a'
2506
2507 def Append(arg: string)
2508 local ..= arg
2509 enddef
2510 g:Append = Append
2511
2512 def Get(): string
2513 return local
2514 enddef
2515 g:Get = Get
2516enddef
2517
2518def Test_closure_append_get()
2519 MakeGetAndAppendRefs()
2520 g:Get()->assert_equal('a')
2521 g:Append('-b')
2522 g:Get()->assert_equal('a-b')
2523 g:Append('-c')
2524 g:Get()->assert_equal('a-b-c')
2525
2526 unlet g:Append
2527 unlet g:Get
2528enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002529
Bram Moolenaar04b12692020-05-04 23:24:44 +02002530def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002531 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002532 def Closure(arg: string): string
2533 return local .. arg
2534 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002535 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002536enddef
2537
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002538func GetResult(Ref)
2539 return a:Ref('some')
2540endfunc
2541
2542def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002543 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002544 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002545 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002546enddef
2547
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002548def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002549 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002550 vim9script
2551 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002552 var name = 0
2553 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002554 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002555 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002556 enddef
2557 Func()
2558 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002559 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002560enddef
2561
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002562def Test_nested_closure_used()
2563 var lines =<< trim END
2564 vim9script
2565 def Func()
2566 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002567 var Closure = () => x
2568 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002569 enddef
2570 Func()
2571 assert_equal('hello', g:Myclosure())
2572 END
2573 CheckScriptSuccess(lines)
2574enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002575
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002576def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002577 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002578 vim9script
2579 def FuncA()
2580 FuncB(0)
2581 enddef
2582 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002583 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002584 enddef
2585 FuncA()
2586 END
2587 CheckScriptFailure(lines, 'E1012:')
2588enddef
2589
Bram Moolenaarf112f302020-12-20 17:47:52 +01002590def Test_global_closure()
2591 var lines =<< trim END
2592 vim9script
2593 def ReverseEveryNLines(n: number, line1: number, line2: number)
2594 var mods = 'sil keepj keepp lockm '
2595 var range = ':' .. line1 .. ',' .. line2
2596 def g:Offset(): number
2597 var offset = (line('.') - line1 + 1) % n
2598 return offset != 0 ? offset : n
2599 enddef
2600 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2601 enddef
2602
2603 new
2604 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2605 ReverseEveryNLines(3, 1, 9)
2606 END
2607 CheckScriptSuccess(lines)
2608 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2609 assert_equal(expected, getline(1, 9))
2610 bwipe!
2611enddef
2612
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002613def Test_global_closure_called_directly()
2614 var lines =<< trim END
2615 vim9script
2616 def Outer()
2617 var x = 1
2618 def g:Inner()
2619 var y = x
2620 x += 1
2621 assert_equal(1, y)
2622 enddef
2623 g:Inner()
2624 assert_equal(2, x)
2625 enddef
2626 Outer()
2627 END
2628 CheckScriptSuccess(lines)
2629 delfunc g:Inner
2630enddef
2631
Bram Moolenaar69c76172021-12-02 16:38:52 +00002632def Test_closure_called_from_legacy()
2633 var lines =<< trim END
2634 vim9script
2635 def Func()
2636 var outer = 'foo'
2637 var F = () => {
2638 outer = 'bar'
2639 }
2640 execute printf('call %s()', string(F))
2641 enddef
2642 Func()
2643 END
2644 CheckScriptFailure(lines, 'E1248')
2645enddef
2646
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002647def Test_failure_in_called_function()
2648 # this was using the frame index as the return value
2649 var lines =<< trim END
2650 vim9script
2651 au TerminalWinOpen * eval [][0]
2652 def PopupTerm(a: any)
2653 # make sure typvals on stack are string
2654 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2655 FireEvent()
2656 enddef
2657 def FireEvent()
2658 do TerminalWinOpen
2659 enddef
2660 # use try/catch to make eval fail
2661 try
2662 call PopupTerm(0)
2663 catch
2664 endtry
2665 au! TerminalWinOpen
2666 END
2667 CheckScriptSuccess(lines)
2668enddef
2669
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002670def Test_nested_lambda()
2671 var lines =<< trim END
2672 vim9script
2673 def Func()
2674 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002675 var Lambda1 = () => 7
2676 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002677 var res = Lambda2()
2678 assert_equal([7, 4], res)
2679 enddef
2680 Func()
2681 END
2682 CheckScriptSuccess(lines)
2683enddef
2684
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002685def Test_double_nested_lambda()
2686 var lines =<< trim END
2687 vim9script
2688 def F(head: string): func(string): func(string): string
2689 return (sep: string): func(string): string => ((tail: string): string => {
2690 return head .. sep .. tail
2691 })
2692 enddef
2693 assert_equal('hello-there', F('hello')('-')('there'))
2694 END
2695 CheckScriptSuccess(lines)
2696enddef
2697
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002698def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002699 var lines =<< trim END
2700 vim9script
2701 def F(text: string): func(string): func(string): string
2702 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002703 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002704 })
2705 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002706 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002707 END
2708 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002709
2710 lines =<< trim END
2711 vim9script
2712 echo range(4)->mapnew((_, v) => {
2713 return range(v) ->mapnew((_, s) => {
2714 return string(s)
2715 })
2716 })
2717 END
2718 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002719
2720 lines =<< trim END
2721 vim9script
2722
2723 def s:func()
2724 range(10)
2725 ->mapnew((_, _) => ({
2726 key: range(10)->mapnew((_, _) => {
2727 return ' '
2728 }),
2729 }))
2730 enddef
2731
2732 defcomp
2733 END
2734 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002735enddef
2736
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002737def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002738 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002739 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002740enddef
2741
2742def Test_lambda_arg_shadows_func()
2743 assert_equal([42], Shadowed())
2744enddef
2745
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002746def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002747 var path: string = empty(dir)
2748 \ ? 'empty'
2749 \ : 'full'
2750 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002751enddef
2752
2753def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002754 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002755enddef
2756
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002757def Test_script_var_in_lambda()
2758 var lines =<< trim END
2759 vim9script
2760 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002761 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002762 END
2763 CheckScriptSuccess(lines)
2764enddef
2765
Bram Moolenaar5e654232020-09-16 15:22:00 +02002766def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002767 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002768 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002769 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002770 ->reverse()
2771 return x
2772enddef
2773
2774def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002775 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002776
2777 var lines =<< trim END
2778 vim9script
2779 var res = [{n: 1, m: 2, s: 'xxx'}]
2780 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2781 v.n,
2782 v.m,
2783 substitute(v.s, '.*', 'yyy', '')
2784 ))
2785 assert_equal(['1:2:yyy'], res)
2786 END
2787 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002788enddef
2789
Bram Moolenaarb6571982021-01-08 22:24:19 +01002790def Test_list_lambda()
2791 timer_start(1000, (_) => 0)
2792 var body = execute(timer_info()[0].callback
2793 ->string()
2794 ->substitute("('", ' ', '')
2795 ->substitute("')", '', '')
2796 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002797 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002798enddef
2799
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002800def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002801 var lines =<< trim END
2802 vim9script
2803 var flist: list<func>
2804 for i in range(10)
2805 var inloop = i
2806 flist[i] = () => inloop
2807 endfor
2808 END
2809 CheckScriptSuccess(lines)
2810
2811 lines =<< trim END
2812 vim9script
2813 if true
2814 var outloop = 5
2815 var flist: list<func>
2816 for i in range(10)
2817 flist[i] = () => outloop
2818 endfor
2819 endif
2820 END
2821 CheckScriptSuccess(lines)
2822
2823 lines =<< trim END
2824 vim9script
2825 if true
2826 var outloop = 5
2827 endif
2828 var flist: list<func>
2829 for i in range(10)
2830 flist[i] = () => outloop
2831 endfor
2832 END
2833 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002834
2835 lines =<< trim END
2836 vim9script
2837 for i in range(10)
2838 var Ref = () => 0
2839 endfor
2840 assert_equal(0, ((i) => 0)(0))
2841 END
2842 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002843enddef
2844
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002845def Test_legacy_lambda()
2846 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002847
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002848 var lines =<< trim END
2849 echo {x -> 'hello ' .. x}('foo')
2850 END
2851 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002852
2853 lines =<< trim END
2854 vim9script
2855 def Func()
2856 echo (() => 'no error')()
2857 enddef
2858 legacy call s:Func()
2859 END
2860 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002861enddef
2862
Bram Moolenaarce024c32021-06-26 13:00:49 +02002863def Test_legacy()
2864 var lines =<< trim END
2865 vim9script
2866 func g:LegacyFunction()
2867 let g:legacyvar = 1
2868 endfunc
2869 def Testit()
2870 legacy call g:LegacyFunction()
2871 enddef
2872 Testit()
2873 assert_equal(1, g:legacyvar)
2874 unlet g:legacyvar
2875 delfunc g:LegacyFunction
2876 END
2877 CheckScriptSuccess(lines)
2878enddef
2879
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002880def Test_legacy_errors()
2881 for cmd in ['if', 'elseif', 'else', 'endif',
2882 'for', 'endfor', 'continue', 'break',
2883 'while', 'endwhile',
2884 'try', 'catch', 'finally', 'endtry']
2885 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2886 endfor
2887enddef
2888
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002889def Test_call_legacy_with_dict()
2890 var lines =<< trim END
2891 vim9script
2892 func Legacy() dict
2893 let g:result = self.value
2894 endfunc
2895 def TestDirect()
2896 var d = {value: 'yes', func: Legacy}
2897 d.func()
2898 enddef
2899 TestDirect()
2900 assert_equal('yes', g:result)
2901 unlet g:result
2902
2903 def TestIndirect()
2904 var d = {value: 'foo', func: Legacy}
2905 var Fi = d.func
2906 Fi()
2907 enddef
2908 TestIndirect()
2909 assert_equal('foo', g:result)
2910 unlet g:result
2911
2912 var d = {value: 'bar', func: Legacy}
2913 d.func()
2914 assert_equal('bar', g:result)
2915 unlet g:result
2916 END
2917 CheckScriptSuccess(lines)
2918enddef
2919
Bram Moolenaarab360522021-01-10 14:02:28 +01002920def DoFilterThis(a: string): list<string>
2921 # closure nested inside another closure using argument
2922 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2923 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2924enddef
2925
2926def Test_nested_closure_using_argument()
2927 assert_equal(['x', 'x2'], DoFilterThis('x'))
2928enddef
2929
Bram Moolenaar0186e582021-01-10 18:33:11 +01002930def Test_triple_nested_closure()
2931 var what = 'x'
2932 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2933 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2934 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2935enddef
2936
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002937func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002938 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002939 call Run_Test_silent_echo()
2940endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002941
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002942def Run_Test_silent_echo()
2943 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002944 vim9script
2945 def EchoNothing()
2946 silent echo ''
2947 enddef
2948 defcompile
2949 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002950 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002951
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002952 # Check that the balloon shows up after a mouse move
2953 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2954 term_sendkeys(buf, ":abc")
2955 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002956
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002957 # clean up
2958 StopVimInTerminal(buf)
2959 delete('XTest_silent_echo')
2960enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002961
Bram Moolenaar171fb922020-10-28 16:54:47 +01002962def SilentlyError()
2963 execute('silent! invalid')
2964 g:did_it = 'yes'
2965enddef
2966
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002967func UserError()
2968 silent! invalid
2969endfunc
2970
2971def SilentlyUserError()
2972 UserError()
2973 g:did_it = 'yes'
2974enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002975
2976" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002977func Test_ignore_silent_error()
2978 let g:did_it = 'no'
2979 call SilentlyError()
2980 call assert_equal('yes', g:did_it)
2981
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002982 let g:did_it = 'no'
2983 call SilentlyUserError()
2984 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002985
2986 unlet g:did_it
2987endfunc
2988
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002989def Test_ignore_silent_error_in_filter()
2990 var lines =<< trim END
2991 vim9script
2992 def Filter(winid: number, key: string): bool
2993 if key == 'o'
2994 silent! eval [][0]
2995 return true
2996 endif
2997 return popup_filter_menu(winid, key)
2998 enddef
2999
Bram Moolenaare0de1712020-12-02 17:36:54 +01003000 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003001 feedkeys("o\r", 'xnt')
3002 END
3003 CheckScriptSuccess(lines)
3004enddef
3005
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02003006def Fibonacci(n: number): number
3007 if n < 2
3008 return n
3009 else
3010 return Fibonacci(n - 1) + Fibonacci(n - 2)
3011 endif
3012enddef
3013
Bram Moolenaar985116a2020-07-12 17:31:09 +02003014def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003015 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02003016enddef
3017
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003018def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01003019 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003020 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01003021 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003022 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003023 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003024enddef
3025
3026def Test_closure_in_map()
3027 mkdir('XclosureDir/tdir', 'p')
3028 writefile(['111'], 'XclosureDir/file1')
3029 writefile(['222'], 'XclosureDir/file2')
3030 writefile(['333'], 'XclosureDir/tdir/file3')
3031
Bram Moolenaare0de1712020-12-02 17:36:54 +01003032 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003033
3034 delete('XclosureDir', 'rf')
3035enddef
3036
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003037def Test_invalid_function_name()
3038 var lines =<< trim END
3039 vim9script
3040 def s: list<string>
3041 END
3042 CheckScriptFailure(lines, 'E129:')
3043
3044 lines =<< trim END
3045 vim9script
3046 def g: list<string>
3047 END
3048 CheckScriptFailure(lines, 'E129:')
3049
3050 lines =<< trim END
3051 vim9script
3052 def <SID>: list<string>
3053 END
3054 CheckScriptFailure(lines, 'E884:')
3055
3056 lines =<< trim END
3057 vim9script
3058 def F list<string>
3059 END
3060 CheckScriptFailure(lines, 'E488:')
3061enddef
3062
Bram Moolenaara90afb92020-07-15 22:38:56 +02003063def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003064 var lines =<< trim END
3065 var Xsetlist: func
3066 Xsetlist = function('setloclist', [0])
3067 Xsetlist([], ' ', {title: 'test'})
3068 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003069
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003070 Xsetlist = function('setloclist', [0, [], ' '])
3071 Xsetlist({title: 'test'})
3072 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003073
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003074 Xsetlist = function('setqflist')
3075 Xsetlist([], ' ', {title: 'test'})
3076 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003077
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003078 Xsetlist = function('setqflist', [[], ' '])
3079 Xsetlist({title: 'test'})
3080 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02003081
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003082 var Len: func: number = function('len', ['word'])
3083 assert_equal(4, Len())
3084
3085 var RepeatFunc = function('repeat', ['o'])
3086 assert_equal('ooooo', RepeatFunc(5))
3087 END
3088 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003089
3090 lines =<< trim END
3091 vim9script
3092 def Foo(Parser: any)
3093 enddef
3094 var Expr: func(dict<any>): dict<any>
3095 const Call = Foo(Expr)
3096 END
3097 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003098enddef
3099
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003100def Test_cmd_modifier()
3101 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003102 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003103enddef
3104
3105def Test_restore_modifiers()
3106 # check that when compiling a :def function command modifiers are not messed
3107 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003108 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003109 vim9script
3110 set eventignore=
3111 autocmd QuickFixCmdPost * copen
3112 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003113 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003114 enddef
3115 func Func()
3116 noautocmd call s:AutocmdsDisabled()
3117 let g:ei_after = &eventignore
3118 endfunc
3119 Func()
3120 END
3121 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003122 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003123enddef
3124
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003125def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003126 eval 1 + 2
3127 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003128 # call not on fourth line
3129 StackBot()
3130enddef
3131
3132def StackBot()
3133 # throw an error
3134 eval [][0]
3135enddef
3136
3137def Test_callstack_def()
3138 try
3139 StackTop()
3140 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003141 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003142 endtry
3143enddef
3144
Bram Moolenaare8211a32020-10-09 22:04:29 +02003145" Re-using spot for variable used in block
3146def Test_block_scoped_var()
3147 var lines =<< trim END
3148 vim9script
3149 def Func()
3150 var x = ['a', 'b', 'c']
3151 if 1
3152 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003153 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003154 endif
3155 var z = x
3156 assert_equal(['x', 'x', 'x'], z)
3157 enddef
3158 Func()
3159 END
3160 CheckScriptSuccess(lines)
3161enddef
3162
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003163def Test_reset_did_emsg()
3164 var lines =<< trim END
3165 @s = 'blah'
3166 au BufWinLeave * #
3167 def Func()
3168 var winid = popup_create('popup', {})
3169 exe '*s'
3170 popup_close(winid)
3171 enddef
3172 Func()
3173 END
3174 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003175 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003176enddef
3177
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003178def Test_did_emsg_reset()
3179 # executing an autocommand resets did_emsg, this should not result in a
3180 # builtin function considered failing
3181 var lines =<< trim END
3182 vim9script
3183 au BufWinLeave * #
3184 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003185 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003186 eval [][0]
3187 enddef
3188 nno <F3> <cmd>call <sid>Func()<cr>
3189 feedkeys("\<F3>\e", 'xt')
3190 END
3191 writefile(lines, 'XemsgReset')
3192 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3193 delete('XemsgReset')
3194 nunmap <F3>
3195 au! BufWinLeave
3196enddef
3197
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003198def Test_abort_with_silent_call()
3199 var lines =<< trim END
3200 vim9script
3201 g:result = 'none'
3202 def Func()
3203 g:result += 3
3204 g:result = 'yes'
3205 enddef
3206 # error is silenced, but function aborts on error
3207 silent! Func()
3208 assert_equal('none', g:result)
3209 unlet g:result
3210 END
3211 CheckScriptSuccess(lines)
3212enddef
3213
Bram Moolenaarf665e972020-12-05 19:17:16 +01003214def Test_continues_with_silent_error()
3215 var lines =<< trim END
3216 vim9script
3217 g:result = 'none'
3218 def Func()
3219 silent! g:result += 3
3220 g:result = 'yes'
3221 enddef
3222 # error is silenced, function does not abort
3223 Func()
3224 assert_equal('yes', g:result)
3225 unlet g:result
3226 END
3227 CheckScriptSuccess(lines)
3228enddef
3229
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003230def Test_abort_even_with_silent()
3231 var lines =<< trim END
3232 vim9script
3233 g:result = 'none'
3234 def Func()
3235 eval {-> ''}() .. '' .. {}['X']
3236 g:result = 'yes'
3237 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003238 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003239 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003240 unlet g:result
3241 END
3242 CheckScriptSuccess(lines)
3243enddef
3244
Bram Moolenaarf665e972020-12-05 19:17:16 +01003245def Test_cmdmod_silent_restored()
3246 var lines =<< trim END
3247 vim9script
3248 def Func()
3249 g:result = 'none'
3250 silent! g:result += 3
3251 g:result = 'none'
3252 g:result += 3
3253 enddef
3254 Func()
3255 END
3256 # can't use CheckScriptFailure, it ignores the :silent!
3257 var fname = 'Xdefsilent'
3258 writefile(lines, fname)
3259 var caught = 'no'
3260 try
3261 exe 'source ' .. fname
3262 catch /E1030:/
3263 caught = 'yes'
3264 assert_match('Func, line 4', v:throwpoint)
3265 endtry
3266 assert_equal('yes', caught)
3267 delete(fname)
3268enddef
3269
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003270def Test_cmdmod_silent_nested()
3271 var lines =<< trim END
3272 vim9script
3273 var result = ''
3274
3275 def Error()
3276 result ..= 'Eb'
3277 eval [][0]
3278 result ..= 'Ea'
3279 enddef
3280
3281 def Crash()
3282 result ..= 'Cb'
3283 sil! Error()
3284 result ..= 'Ca'
3285 enddef
3286
3287 Crash()
3288 assert_equal('CbEbEaCa', result)
3289 END
3290 CheckScriptSuccess(lines)
3291enddef
3292
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003293def Test_dict_member_with_silent()
3294 var lines =<< trim END
3295 vim9script
3296 g:result = 'none'
3297 var d: dict<any>
3298 def Func()
3299 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003300 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003301 catch
3302 endtry
3303 enddef
3304 silent! Func()
3305 assert_equal('0', g:result)
3306 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003307 END
3308 CheckScriptSuccess(lines)
3309enddef
3310
Bram Moolenaarf9041332021-01-21 19:41:16 +01003311def Test_skip_cmds_with_silent()
3312 var lines =<< trim END
3313 vim9script
3314
3315 def Func(b: bool)
3316 Crash()
3317 enddef
3318
3319 def Crash()
3320 sil! :/not found/d _
3321 sil! :/not found/put _
3322 enddef
3323
3324 Func(true)
3325 END
3326 CheckScriptSuccess(lines)
3327enddef
3328
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003329def Test_opfunc()
3330 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3331 def g:Opfunc(_: any): string
3332 setline(1, 'ASDF')
3333 return ''
3334 enddef
3335 new
3336 setline(1, 'asdf')
3337 feedkeys("\<F3>$", 'x')
3338 assert_equal('ASDF', getline(1))
3339
3340 bwipe!
3341 nunmap <F3>
3342enddef
3343
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003344func Test_opfunc_error()
3345 CheckScreendump
3346 call Run_Test_opfunc_error()
3347endfunc
3348
3349def Run_Test_opfunc_error()
3350 # test that the error from Opfunc() is displayed right away
3351 var lines =<< trim END
3352 vim9script
3353
3354 def Opfunc(type: string)
3355 try
3356 eval [][0]
3357 catch /nothing/ # error not caught
3358 endtry
3359 enddef
3360 &operatorfunc = Opfunc
3361 nnoremap <expr> l <SID>L()
3362 def L(): string
3363 return 'l'
3364 enddef
3365 'x'->repeat(10)->setline(1)
3366 feedkeys('g@l', 'n')
3367 feedkeys('llll')
3368 END
3369 call writefile(lines, 'XTest_opfunc_error')
3370
3371 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003372 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003373 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003374
3375 # clean up
3376 StopVimInTerminal(buf)
3377 delete('XTest_opfunc_error')
3378enddef
3379
Bram Moolenaar077a4232020-12-22 18:33:27 +01003380" this was crashing on exit
3381def Test_nested_lambda_in_closure()
3382 var lines =<< trim END
3383 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003384 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003385 def Outer()
3386 def g:Inner()
3387 echo map([1, 2, 3], {_, v -> v + 1})
3388 enddef
3389 g:Inner()
3390 enddef
3391 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003392 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003393 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003394 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003395 return
3396 endif
3397 assert_equal(['Done'], readfile('XnestedDone'))
3398 delete('XnestedDone')
3399enddef
3400
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003401def Test_check_func_arg_types()
3402 var lines =<< trim END
3403 vim9script
3404 def F1(x: string): string
3405 return x
3406 enddef
3407
3408 def F2(x: number): number
3409 return x + 1
3410 enddef
3411
3412 def G(g: func): dict<func>
3413 return {f: g}
3414 enddef
3415
3416 def H(d: dict<func>): string
3417 return d.f('a')
3418 enddef
3419 END
3420
3421 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3422 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3423enddef
3424
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003425def Test_list_any_type_checked()
3426 var lines =<< trim END
3427 vim9script
3428 def Foo()
3429 --decl--
3430 Bar(l)
3431 enddef
3432 def Bar(ll: list<dict<any>>)
3433 enddef
3434 Foo()
3435 END
3436 lines[2] = 'var l: list<any>'
3437 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3438
3439 lines[2] = 'var l: list<any> = []'
3440 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3441
3442 lines[2] = 'var l: list<any> = [11]'
3443 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3444enddef
3445
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003446def Test_compile_error()
3447 var lines =<< trim END
3448 def g:Broken()
3449 echo 'a' + {}
3450 enddef
3451 call g:Broken()
3452 END
3453 # First call: compilation error
3454 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3455
3456 # Second call won't try compiling again
3457 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003458 delfunc g:Broken
3459
3460 # No error when compiling with :silent!
3461 lines =<< trim END
3462 def g:Broken()
3463 echo 'a' + []
3464 enddef
3465 silent! defcompile
3466 END
3467 CheckScriptSuccess(lines)
3468
3469 # Calling the function won't try compiling again
3470 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3471 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003472enddef
3473
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003474def Test_ignored_argument()
3475 var lines =<< trim END
3476 vim9script
3477 def Ignore(_, _): string
3478 return 'yes'
3479 enddef
3480 assert_equal('yes', Ignore(1, 2))
3481
3482 func Ok(_)
3483 return a:_
3484 endfunc
3485 assert_equal('ok', Ok('ok'))
3486
3487 func Oktoo()
3488 let _ = 'too'
3489 return _
3490 endfunc
3491 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003492
3493 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003494 END
3495 CheckScriptSuccess(lines)
3496
3497 lines =<< trim END
3498 def Ignore(_: string): string
3499 return _
3500 enddef
3501 defcompile
3502 END
3503 CheckScriptFailure(lines, 'E1181:', 1)
3504
3505 lines =<< trim END
3506 var _ = 1
3507 END
3508 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003509
3510 lines =<< trim END
3511 var x = _
3512 END
3513 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003514enddef
3515
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003516def Test_too_many_arguments()
3517 var lines =<< trim END
3518 echo [0, 1, 2]->map(() => 123)
3519 END
3520 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3521
3522 lines =<< trim END
3523 echo [0, 1, 2]->map((_) => 123)
3524 END
3525 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3526enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003527
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003528def Test_closing_brace_at_start_of_line()
3529 var lines =<< trim END
3530 def Func()
3531 enddef
3532 Func(
3533 )
3534 END
3535 call CheckDefAndScriptSuccess(lines)
3536enddef
3537
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003538func CreateMydict()
3539 let g:mydict = {}
3540 func g:mydict.afunc()
3541 let g:result = self.key
3542 endfunc
3543endfunc
3544
3545def Test_numbered_function_reference()
3546 CreateMydict()
3547 var output = execute('legacy func g:mydict.afunc')
3548 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3549 execute 'function(' .. funcName .. ', [], {key: 42})()'
3550 # check that the function still exists
3551 assert_equal(output, execute('legacy func g:mydict.afunc'))
3552 unlet g:mydict
3553enddef
3554
Bram Moolenaard3a11782022-01-05 16:50:40 +00003555def Test_go_beyond_end_of_cmd()
3556 # this was reading the byte after the end of the line
3557 var lines =<< trim END
3558 def F()
3559 cal
3560 enddef
3561 defcompile
3562 END
3563 CheckScriptFailure(lines, 'E476:')
3564enddef
3565
Bram Moolenaar20677332021-06-06 17:02:53 +02003566if has('python3')
3567 def Test_python3_heredoc()
3568 py3 << trim EOF
3569 import vim
3570 vim.vars['didit'] = 'yes'
3571 EOF
3572 assert_equal('yes', g:didit)
3573
3574 python3 << trim EOF
3575 import vim
3576 vim.vars['didit'] = 'again'
3577 EOF
3578 assert_equal('again', g:didit)
3579 enddef
3580endif
3581
3582" This messes up syntax highlight, keep near the end.
3583if has('lua')
3584 def Test_lua_heredoc()
3585 g:d = {}
3586 lua << trim EOF
3587 x = vim.eval('g:d')
3588 x['key'] = 'val'
3589 EOF
3590 assert_equal('val', g:d.key)
3591 enddef
3592endif
3593
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003594
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003595" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker