blob: 1a14c101679576203531c38ad4fa15f775a4e7a0 [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000494 CheckDefAndScriptFailure(["call Test ('text')"], ['E476:', 'E1068:'])
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000550
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000551 # using script variable requires matching type or type cast when executed
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000552 lines =<< trim END
553 vim9script
554 var a: any
555 def Func(arg: string = a)
556 echo arg
557 enddef
558 defcompile
559 END
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000560 CheckScriptSuccess(lines + ['a = "text"', 'Func()'])
561 CheckScriptFailure(lines + ['a = 123', 'Func()'], 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000562
563 # using global variable does not require type cast
564 lines =<< trim END
565 vim9script
566 def Func(arg: string = g:str)
567 echo arg
568 enddef
569 g:str = 'works'
570 Func()
571 END
572 CheckScriptSuccess(lines)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200573enddef
574
Bram Moolenaarcef12702021-01-04 14:09:43 +0100575def FuncWithComment( # comment
576 a: number, #comment
577 b: bool, # comment
578 c: string) #comment
579 assert_equal(4, a)
580 assert_equal(true, b)
581 assert_equal('yes', c)
582enddef
583
584def Test_func_with_comments()
585 FuncWithComment(4, true, 'yes')
586
587 var lines =<< trim END
588 def Func(# comment
589 arg: string)
590 enddef
591 END
592 CheckScriptFailure(lines, 'E125:', 1)
593
594 lines =<< trim END
595 def Func(
596 arg: string# comment
597 )
598 enddef
599 END
600 CheckScriptFailure(lines, 'E475:', 2)
601
602 lines =<< trim END
603 def Func(
604 arg: string
605 )# comment
606 enddef
607 END
608 CheckScriptFailure(lines, 'E488:', 3)
609enddef
610
Bram Moolenaar04b12692020-05-04 23:24:44 +0200611def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000612 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200613 return 'nested ' .. arg
614 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000615 NestedDef(':def')->assert_equal('nested :def')
616
617 func NestedFunc(arg)
618 return 'nested ' .. a:arg
619 endfunc
620 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200621
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200622 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
623 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
624
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200625 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
626 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200627
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 var lines =<< trim END
629 def Outer()
630 def Inner()
631 # comment
632 enddef
633 def Inner()
634 enddef
635 enddef
636 END
637 CheckDefFailure(lines, 'E1073:')
638
639 lines =<< trim END
640 def Outer()
641 def Inner()
642 # comment
643 enddef
644 def! Inner()
645 enddef
646 enddef
647 END
648 CheckDefFailure(lines, 'E1117:')
649
Bram Moolenaardb8e5c22021-12-25 19:58:22 +0000650 lines =<< trim END
651 vim9script
652 def Outer()
653 def Inner()
654 g:result = 'ok'
655 enddef
656 Inner()
657 enddef
658 Outer()
659 Inner()
660 END
661 CheckScriptFailure(lines, 'E117: Unknown function: Inner')
662 assert_equal('ok', g:result)
663 unlet g:result
664
Bram Moolenaar54021752020-12-06 18:50:36 +0100665 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100666 lines =<< trim END
667 vim9script
668 var thecount = 0
669 if true
670 def Test(): number
671 def TheFunc(): number
672 thecount += 1
673 return thecount
674 enddef
675 return TheFunc()
676 enddef
677 endif
678 defcompile
679 assert_equal(1, Test())
680 assert_equal(2, Test())
681 END
682 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100683
684 # also works when "thecount" is inside the "if" block
685 lines =<< trim END
686 vim9script
687 if true
688 var thecount = 0
689 def Test(): number
690 def TheFunc(): number
691 thecount += 1
692 return thecount
693 enddef
694 return TheFunc()
695 enddef
696 endif
697 defcompile
698 assert_equal(1, Test())
699 assert_equal(2, Test())
700 END
701 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200702
703 lines =<< trim END
704 vim9script
705 def Outer()
706 def Inner()
707 echo 'hello'
708 enddef burp
709 enddef
710 defcompile
711 END
712 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200713enddef
714
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100715def Test_not_nested_function()
716 echo printf('%d',
717 function('len')('xxx'))
718enddef
719
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200720func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200721 call MyDefaultArgs()->assert_equal('string')
722 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200723 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200724endfunc
725
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200726def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200727 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200728 vim9script
729 def Outer()
730 def g:Inner(): string
731 return 'inner'
732 enddef
733 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200734 defcompile
735 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200736 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200737 delfunc g:Inner
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
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200744 END
745 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200746
747 lines =<< trim END
748 vim9script
749 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000750 func g:Inner()
751 return 'inner'
752 endfunc
753 enddef
754 defcompile
755 Outer()
756 g:Inner()->assert_equal('inner')
757 delfunc g:Inner
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 END
765 CheckScriptSuccess(lines)
766
767 lines =<< trim END
768 vim9script
769 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200770 def g:Inner(): string
771 return 'inner'
772 enddef
773 enddef
774 defcompile
775 Outer()
776 Outer()
777 END
778 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100779 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200780
781 lines =<< trim END
782 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100783 def Outer()
784 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100785 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100786 enddef
787 g:Inner()
788 enddef
789 Outer()
790 END
791 CheckScriptSuccess(lines)
792 delfunc g:Inner
793
794 lines =<< trim END
795 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200796 def Func()
797 echo 'script'
798 enddef
799 def Outer()
800 def Func()
801 echo 'inner'
802 enddef
803 enddef
804 defcompile
805 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000806 CheckScriptFailure(lines, "E1073:", 1)
807
808 lines =<< trim END
809 vim9script
810 def Func()
811 echo 'script'
812 enddef
813 def Func()
814 echo 'script'
815 enddef
816 END
817 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200818enddef
819
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100820def DefListAll()
821 def
822enddef
823
824def DefListOne()
825 def DefListOne
826enddef
827
828def DefListMatches()
829 def /DefList
830enddef
831
832def Test_nested_def_list()
833 var funcs = split(execute('call DefListAll()'), "\n")
834 assert_true(len(funcs) > 10)
835 assert_true(funcs->index('def DefListAll()') >= 0)
836
837 funcs = split(execute('call DefListOne()'), "\n")
838 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
839
840 funcs = split(execute('call DefListMatches()'), "\n")
841 assert_true(len(funcs) >= 3)
842 assert_true(funcs->index('def DefListAll()') >= 0)
843 assert_true(funcs->index('def DefListOne()') >= 0)
844 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100845
846 var lines =<< trim END
847 vim9script
848 def Func()
849 def +Func+
850 enddef
851 defcompile
852 END
853 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100854enddef
855
Bram Moolenaar333894b2020-08-01 18:53:07 +0200856def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200857 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200858 vim9script
859 def g:Func(): string
860 return 'global'
861 enddef
862 def Func(): string
863 return 'local'
864 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200865 g:Func()->assert_equal('global')
866 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100867 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200868 END
869 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200870
871 lines =<< trim END
872 vim9script
873 def g:Funcy()
874 echo 'funcy'
875 enddef
876 s:Funcy()
877 END
878 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200879enddef
880
Bram Moolenaar0f769812020-09-12 18:32:34 +0200881def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200882 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200883 vim9script
884 def g:Gfunc(): string
885 return 'global'
886 enddef
887 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200888 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200889 return Gfunc('testing')
890 enddef
891 g:Gfunc()->assert_equal('global')
892 AnotherFunc()->assert_equal(7)
893 delfunc g:Gfunc
894 END
895 CheckScriptSuccess(lines)
896
897 lines =<< trim END
898 vim9script
899 def g:Func(): string
900 return 'global'
901 enddef
902 def AnotherFunc()
903 g:Func = function('len')
904 enddef
905 AnotherFunc()
906 END
907 CheckScriptFailure(lines, 'E705:')
908 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200909
910 # global function is found without g: prefix
911 lines =<< trim END
912 vim9script
913 def g:Func(): string
914 return 'global'
915 enddef
916 def AnotherFunc(): string
917 return Func()
918 enddef
919 assert_equal('global', AnotherFunc())
920 delfunc g:Func
921 END
922 CheckScriptSuccess(lines)
923
924 lines =<< trim END
925 vim9script
926 def g:Func(): string
927 return 'global'
928 enddef
929 assert_equal('global', Func())
930 delfunc g:Func
931 END
932 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200933enddef
934
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200935func TakesOneArg(arg)
936 echo a:arg
937endfunc
938
939def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200940 CheckDefFailure(['TakesOneArg()'], 'E119:')
941 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
942 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
943 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200944
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200945 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200946 vim9script
947 def Func(s: string)
948 echo s
949 enddef
950 Func([])
951 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200952 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200953
954 lines =<< trim END
955 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100956 var name = 'piet'
957 def FuncOne(name: string)
958 echo nr
959 enddef
960 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100961 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100962
963 lines =<< trim END
964 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200965 def FuncOne(nr: number)
966 echo nr
967 enddef
968 def FuncTwo()
969 FuncOne()
970 enddef
971 defcompile
972 END
973 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200974 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200975 try
976 source Xscript
977 catch
978 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
979 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
980 didCatch = true
981 endtry
982 assert_true(didCatch)
983
984 lines =<< trim END
985 vim9script
986 def FuncOne(nr: number)
987 echo nr
988 enddef
989 def FuncTwo()
990 FuncOne(1, 2)
991 enddef
992 defcompile
993 END
994 writefile(lines, 'Xscript')
995 didCatch = false
996 try
997 source Xscript
998 catch
999 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
1000 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
1001 didCatch = true
1002 endtry
1003 assert_true(didCatch)
1004
1005 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001006enddef
1007
Bram Moolenaar50824712020-12-20 21:10:17 +01001008def Test_call_funcref_wrong_args()
1009 var head =<< trim END
1010 vim9script
1011 def Func3(a1: string, a2: number, a3: list<number>)
1012 echo a1 .. a2 .. a3[0]
1013 enddef
1014 def Testme()
1015 var funcMap: dict<func> = {func: Func3}
1016 END
1017 var tail =<< trim END
1018 enddef
1019 Testme()
1020 END
1021 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
1022
1023 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
1024 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001025
1026 var lines =<< trim END
1027 vim9script
1028 var Ref: func(number): any
1029 Ref = (j) => !j
1030 echo Ref(false)
1031 END
1032 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
1033
1034 lines =<< trim END
1035 vim9script
1036 var Ref: func(number): any
1037 Ref = (j) => !j
1038 call Ref(false)
1039 END
1040 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001041enddef
1042
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001043def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001044 var lines =<< trim END
1045 var Callback = (..._) => 'anything'
1046 assert_equal('anything', Callback())
1047 assert_equal('anything', Callback(1))
1048 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001049
1050 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001051 END
1052 CheckDefAndScriptSuccess(lines)
1053
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001054 CheckDefFailure(['echo ((i) => 0)()'],
1055 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001056
Bram Moolenaar2a389082021-04-09 20:24:31 +02001057 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001058 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001059 echo Ref(1, 'x')
1060 END
1061 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001062
1063 lines =<< trim END
1064 var Ref: func(job, string, number)
1065 Ref = (x, y) => 0
1066 END
1067 CheckDefAndScriptFailure(lines, 'E1012:')
1068
1069 lines =<< trim END
1070 var Ref: func(job, string)
1071 Ref = (x, y, z) => 0
1072 END
1073 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001074
1075 lines =<< trim END
1076 var one = 1
1077 var l = [1, 2, 3]
1078 echo map(l, (one) => one)
1079 END
1080 CheckDefFailure(lines, 'E1167:')
1081 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1082
1083 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001084 var Ref: func(any, ?any): bool
1085 Ref = (_, y = 1) => false
1086 END
1087 CheckDefAndScriptFailure(lines, 'E1172:')
1088
1089 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001090 var a = 0
1091 var b = (a == 0 ? 1 : 2)
1092 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001093 var txt = 'a'
1094 b = (txt =~ 'x' ? 1 : 2)
1095 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001096 END
1097 CheckDefAndScriptSuccess(lines)
1098
1099 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001100 def ShadowLocal()
1101 var one = 1
1102 var l = [1, 2, 3]
1103 echo map(l, (one) => one)
1104 enddef
1105 END
1106 CheckDefFailure(lines, 'E1167:')
1107
1108 lines =<< trim END
1109 def Shadowarg(one: number)
1110 var l = [1, 2, 3]
1111 echo map(l, (one) => one)
1112 enddef
1113 END
1114 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001115
1116 lines =<< trim END
1117 echo ((a) => a)('aa', 'bb')
1118 END
1119 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001120
1121 lines =<< trim END
1122 echo 'aa'->((a) => a)('bb')
1123 END
1124 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1125 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001126enddef
1127
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001128def Test_lambda_line_nr()
1129 var lines =<< trim END
1130 vim9script
1131 # comment
1132 # comment
1133 var id = timer_start(1'000, (_) => 0)
1134 var out = execute('verbose ' .. timer_info(id)[0].callback
1135 ->string()
1136 ->substitute("('\\|')", ' ', 'g'))
1137 assert_match('Last set from .* line 4', out)
1138 END
1139 CheckScriptSuccess(lines)
1140enddef
1141
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001142def FilterWithCond(x: string, Cond: func(string): bool): bool
1143 return Cond(x)
1144enddef
1145
Bram Moolenaar0346b792021-01-31 22:18:29 +01001146def Test_lambda_return_type()
1147 var lines =<< trim END
1148 var Ref = (): => 123
1149 END
1150 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001151
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001152 # no space before the return type
1153 lines =<< trim END
1154 var Ref = (x):number => x + 1
1155 END
1156 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1157
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001158 # this works
1159 for x in ['foo', 'boo']
1160 echo FilterWithCond(x, (v) => v =~ '^b')
1161 endfor
1162
1163 # this fails
1164 lines =<< trim END
1165 echo FilterWithCond('foo', (v) => v .. '^b')
1166 END
1167 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001168
1169 lines =<< trim END
1170 var Lambda1 = (x) => {
1171 return x
1172 }
1173 assert_equal('asdf', Lambda1('asdf'))
1174 var Lambda2 = (x): string => {
1175 return x
1176 }
1177 assert_equal('foo', Lambda2('foo'))
1178 END
1179 CheckDefAndScriptSuccess(lines)
1180
1181 lines =<< trim END
1182 var Lambda = (x): string => {
1183 return x
1184 }
1185 echo Lambda(['foo'])
1186 END
1187 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001188enddef
1189
Bram Moolenaar709664c2020-12-12 14:33:41 +01001190def Test_lambda_uses_assigned_var()
1191 CheckDefSuccess([
1192 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001193 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001194enddef
1195
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001196def Test_pass_legacy_lambda_to_def_func()
1197 var lines =<< trim END
1198 vim9script
1199 func Foo()
1200 eval s:Bar({x -> 0})
1201 endfunc
1202 def Bar(y: any)
1203 enddef
1204 Foo()
1205 END
1206 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001207
1208 lines =<< trim END
1209 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001210 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001211 enddef
1212 legacy call g:TestFunc({-> 0})
1213 delfunc g:TestFunc
1214
1215 def g:TestFunc(f: func(number))
1216 enddef
1217 legacy call g:TestFunc({nr -> 0})
1218 delfunc g:TestFunc
1219 END
1220 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001221enddef
1222
Bram Moolenaar844fb642021-10-23 13:32:30 +01001223def Test_lambda_in_reduce_line_break()
1224 # this was using freed memory
1225 var lines =<< trim END
1226 vim9script
1227 const result: dict<number> =
1228 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1229 ->reduce((acc, val) => {
1230 if has_key(acc, val)
1231 acc[val] += 1
1232 return acc
1233 else
1234 acc[val] = 1
1235 return acc
1236 endif
1237 }, {})
1238 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1239 END
1240 CheckScriptSuccess(lines)
1241enddef
1242
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001243def Test_set_opfunc_to_lambda()
1244 var lines =<< trim END
1245 vim9script
1246 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1247 def CountSpaces(type = ''): string
1248 if type == ''
1249 &operatorfunc = (t) => CountSpaces(t)
1250 return 'g@'
1251 endif
1252 normal! '[V']y
1253 g:result = getreg('"')->count(' ')
1254 return ''
1255 enddef
1256 new
1257 'a b c d e'->setline(1)
1258 feedkeys("\<F4>", 'x')
1259 assert_equal(4, g:result)
1260 bwipe!
1261 END
1262 CheckScriptSuccess(lines)
1263enddef
1264
Bram Moolenaaref082e12021-12-12 21:02:03 +00001265def Test_set_opfunc_to_global_function()
1266 var lines =<< trim END
1267 vim9script
1268 def g:CountSpaces(type = ''): string
1269 normal! '[V']y
1270 g:result = getreg('"')->count(' ')
1271 return ''
1272 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001273 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001274 &operatorfunc = g:CountSpaces
1275 new
1276 'a b c d e'->setline(1)
1277 feedkeys("g@_", 'x')
1278 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001279
1280 &operatorfunc = ''
1281 g:result = 0
1282 # global function works in :def function
1283 def Func()
1284 &operatorfunc = g:CountSpaces
1285 enddef
1286 Func()
1287 feedkeys("g@_", 'x')
1288 assert_equal(4, g:result)
1289
Bram Moolenaaref082e12021-12-12 21:02:03 +00001290 bwipe!
1291 END
1292 CheckScriptSuccess(lines)
1293 &operatorfunc = ''
1294enddef
1295
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001296def Test_use_script_func_name_with_prefix()
1297 var lines =<< trim END
1298 vim9script
1299 func s:Getit()
1300 return 'it'
1301 endfunc
1302 var Fn = s:Getit
1303 assert_equal('it', Fn())
1304 END
1305 CheckScriptSuccess(lines)
1306enddef
1307
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001308def Test_lambda_type_allocated()
1309 # Check that unreferencing a partial using a lambda can use the variable type
1310 # after the lambda has been freed and does not leak memory.
1311 var lines =<< trim END
1312 vim9script
1313
1314 func MyomniFunc1(val, findstart, base)
1315 return a:findstart ? 0 : []
1316 endfunc
1317
1318 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1319 &omnifunc = Lambda
1320 Lambda = (a, b) => MyomniFunc1(20, a, b)
1321 &omnifunc = string(Lambda)
1322 Lambda = (a, b) => strlen(a)
1323 END
1324 CheckScriptSuccess(lines)
1325enddef
1326
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001327" Default arg and varargs
1328def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001329 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001330 for s in rest
1331 res ..= ',' .. s
1332 endfor
1333 return res
1334enddef
1335
1336def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001337 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001338 MyDefVarargs('one')->assert_equal('one,foo')
1339 MyDefVarargs('one', 'two')->assert_equal('one,two')
1340 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001341 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001342 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001343 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001344 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001345
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001346 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001347 vim9script
1348 def Func(...l: list<string>)
1349 echo l
1350 enddef
1351 Func('a', 'b', 'c')
1352 END
1353 CheckScriptSuccess(lines)
1354
1355 lines =<< trim END
1356 vim9script
1357 def Func(...l: list<string>)
1358 echo l
1359 enddef
1360 Func()
1361 END
1362 CheckScriptSuccess(lines)
1363
1364 lines =<< trim END
1365 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001366 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001367 echo l
1368 enddef
1369 Func(0)
1370 END
1371 CheckScriptSuccess(lines)
1372
1373 lines =<< trim END
1374 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001375 def Func(...l: any)
1376 echo l
1377 enddef
1378 Func(0)
1379 END
1380 CheckScriptFailure(lines, 'E1180:', 2)
1381
1382 lines =<< trim END
1383 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001384 def Func(..._l: list<string>)
1385 echo _l
1386 enddef
1387 Func('a', 'b', 'c')
1388 END
1389 CheckScriptSuccess(lines)
1390
1391 lines =<< trim END
1392 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001393 def Func(...l: list<string>)
1394 echo l
1395 enddef
1396 Func(1, 2, 3)
1397 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001398 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001399
1400 lines =<< trim END
1401 vim9script
1402 def Func(...l: list<string>)
1403 echo l
1404 enddef
1405 Func('a', 9)
1406 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001407 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001408
1409 lines =<< trim END
1410 vim9script
1411 def Func(...l: list<string>)
1412 echo l
1413 enddef
1414 Func(1, 'a')
1415 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001416 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001417
1418 lines =<< trim END
1419 vim9script
1420 def Func( # some comment
1421 ...l = []
1422 )
1423 echo l
1424 enddef
1425 END
1426 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001427
1428 lines =<< trim END
1429 vim9script
1430 def DoIt()
1431 g:Later('')
1432 enddef
1433 defcompile
1434 def g:Later(...l: list<number>)
1435 enddef
1436 DoIt()
1437 END
1438 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001439enddef
1440
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001441let s:value = ''
1442
1443def FuncOneDefArg(opt = 'text')
1444 s:value = opt
1445enddef
1446
1447def FuncTwoDefArg(nr = 123, opt = 'text'): string
1448 return nr .. opt
1449enddef
1450
1451def FuncVarargs(...arg: list<string>): string
1452 return join(arg, ',')
1453enddef
1454
1455def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001456 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001457 RefDefArg = FuncOneDefArg
1458 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001459 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001460 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001461 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001462
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001463 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001464 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001465 RefDef2Arg()->assert_equal('123text')
1466 RefDef2Arg(99)->assert_equal('99text')
1467 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001468
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001469 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1470 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001471
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001472 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001473 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001474 RefVarargs()->assert_equal('')
1475 RefVarargs('one')->assert_equal('one')
1476 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001477
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001478 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1479 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001480enddef
1481
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001482" Only varargs
1483def MyVarargsOnly(...args: list<string>): string
1484 return join(args, ',')
1485enddef
1486
1487def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001488 MyVarargsOnly()->assert_equal('')
1489 MyVarargsOnly('one')->assert_equal('one')
1490 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001491 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1492 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001493enddef
1494
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001495def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001496 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001497 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001498 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001499enddef
1500
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001501def DictArg(arg: dict<string>)
1502 arg['key'] = 'value'
1503enddef
1504
1505def ListArg(arg: list<string>)
1506 arg[0] = 'value'
1507enddef
1508
1509def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001510 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001511 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001512 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001513 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001514 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001515 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001516 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001517
Bram Moolenaard2c61702020-09-06 15:58:36 +02001518 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001519 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001520enddef
1521
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001522" These argument names are reserved in legacy functions.
1523def WithReservedNames(firstline: string, lastline: string): string
1524 return firstline .. lastline
1525enddef
1526
1527def Test_argument_names()
1528 assert_equal('OK', WithReservedNames('O', 'K'))
1529enddef
1530
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001531def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001532 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001533 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001534enddef
1535
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001536func DefinedLater(arg)
1537 return a:arg
1538endfunc
1539
1540def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001541 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001542 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001543 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001544
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001545 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001546 vim9script
1547 def RetNumber(): number
1548 return 123
1549 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001550 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001551 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001552 END
1553 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001554
1555 lines =<< trim END
1556 vim9script
1557 def RetNumber(): number
1558 return 123
1559 enddef
1560 def Bar(F: func: number): number
1561 return F()
1562 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001563 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001564 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001565 END
1566 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001567
1568 lines =<< trim END
1569 vim9script
1570 def UseNumber(nr: number)
1571 echo nr
1572 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001573 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001574 Funcref(123)
1575 END
1576 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001577
1578 lines =<< trim END
1579 vim9script
1580 def UseNumber(nr: number)
1581 echo nr
1582 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001583 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001584 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001585 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001586
1587 lines =<< trim END
1588 vim9script
1589 def EchoNr(nr = 34)
1590 g:echo = nr
1591 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001592 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001593 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001594 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001595 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001596 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001597 END
1598 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001599
1600 lines =<< trim END
1601 vim9script
1602 def EchoList(...l: list<number>)
1603 g:echo = l
1604 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001605 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001606 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001607 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001608 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001609 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001610 END
1611 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001612
1613 lines =<< trim END
1614 vim9script
1615 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1616 g:optarg = opt
1617 g:listarg = l
1618 return nr
1619 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001620 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001621 Funcref(10)->assert_equal(10)
1622 g:optarg->assert_equal(12)
1623 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001624
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001625 Funcref(11, 22)->assert_equal(11)
1626 g:optarg->assert_equal(22)
1627 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001628
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001629 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1630 g:optarg->assert_equal(18)
1631 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001632 END
1633 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001634enddef
1635
1636let SomeFunc = function('len')
1637let NotAFunc = 'text'
1638
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001639def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001640 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001641 var Ref1: func(bool): string
1642 var Ref2: func(bool): number
1643 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001644 Ref3 = g:cond ? Ref1 : Ref2
1645
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001646 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001647 var Refa1: func(bool): number
1648 var Refa2: func(bool, number): number
1649 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001650 Refa3 = g:cond ? Refa1 : Refa2
1651
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001652 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001653 var Refb1: func(bool, string): number
1654 var Refb2: func(string, number): number
1655 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001656 Refb3 = g:cond ? Refb1 : Refb2
1657enddef
1658
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001659def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001660 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001661enddef
1662
1663def DefinedEvenLater(arg: string): string
1664 return arg
1665enddef
1666
1667def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001668 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001669 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001670enddef
1671
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001672def Test_nested_functin_with_nextcmd()
1673 var lines =<< trim END
1674 vim9script
1675 # Define an outer function
1676 def FirstFunction()
1677 # Define an inner function
1678 def SecondFunction()
1679 # the function has a body, a double free is detected.
1680 AAAAA
1681
1682 # enddef followed by | or } followed by # one or more characters
1683 enddef|BBBB
1684 enddef
1685
1686 # Compile all functions
1687 defcompile
1688 END
1689 CheckScriptFailure(lines, 'E476: Invalid command: AAAAA')
1690enddef
1691
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001692def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001693 CheckScriptFailure([
1694 'def Func(): number',
1695 'return "a"',
1696 'enddef',
1697 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001698 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001699 CheckScriptFailure([
1700 'def Func(): string',
1701 'return 1',
1702 'enddef',
1703 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001704 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001705 CheckScriptFailure([
1706 'def Func(): void',
1707 'return "a"',
1708 'enddef',
1709 'defcompile'],
1710 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001711 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001712 CheckScriptFailure([
1713 'def Func()',
1714 'return "a"',
1715 'enddef',
1716 'defcompile'],
1717 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001718 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001719
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001720 CheckScriptFailure([
1721 'def Func(): number',
1722 'return',
1723 'enddef',
1724 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001725 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001726
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001727 CheckScriptFailure([
1728 'def Func():number',
1729 'return 123',
1730 'enddef',
1731 'defcompile'], 'E1069:')
1732 delfunc! g:Func
1733
1734 CheckScriptFailure([
1735 'def Func() :number',
1736 'return 123',
1737 'enddef',
1738 'defcompile'], 'E1059:')
1739 delfunc! g:Func
1740
1741 CheckScriptFailure([
1742 'def Func() : number',
1743 'return 123',
1744 'enddef',
1745 'defcompile'], 'E1059:')
1746 delfunc! g:Func
1747
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001748 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001749 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001750 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001751 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001752 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001753 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001754
1755 CheckScriptFailure([
1756 'vim9script',
1757 'def FuncB()',
1758 ' return 123',
1759 'enddef',
1760 'def FuncA()',
1761 ' FuncB()',
1762 'enddef',
1763 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001764enddef
1765
1766def Test_arg_type_wrong()
1767 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001768 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001769 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001770 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001771 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1772 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001773enddef
1774
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001775def Test_white_space_before_comma()
1776 var lines =<< trim END
1777 vim9script
1778 def Func(a: number , b: number)
1779 enddef
1780 END
1781 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001782 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001783enddef
1784
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001785def Test_white_space_after_comma()
1786 var lines =<< trim END
1787 vim9script
1788 def Func(a: number,b: number)
1789 enddef
1790 END
1791 CheckScriptFailure(lines, 'E1069:')
1792
1793 # OK in legacy function
1794 lines =<< trim END
1795 vim9script
1796 func Func(a,b)
1797 endfunc
1798 END
1799 CheckScriptSuccess(lines)
1800enddef
1801
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001802def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001803 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001804 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001805 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001806 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001807 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001808 enddef
1809 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001810 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001811
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001812 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001813 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001814 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001815
Bram Moolenaar67979662020-06-20 22:50:47 +02001816 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001817 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001818 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001819
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001820 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001821 def ListFunc(arg: list<number>)
1822 listvar = arg
1823 enddef
1824 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001825 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001826
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001827 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001828 def DictFunc(arg: dict<number>)
1829 dictvar = arg
1830 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001831 {a: 1, b: 2}->DictFunc()
1832 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001834 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001835 enddef
1836 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001837 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001838
Bram Moolenaare0de1712020-12-02 17:36:54 +01001839 {a: 3, b: 4}->DictFunc()
1840 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001841
1842 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001843 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001844 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001845 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001846
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001847 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001848 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001849 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001850 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001851 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001852 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001853
1854 def UseString()
1855 'xyork'->MyFunc()
1856 enddef
1857 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001858 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001859
Bram Moolenaar10409562020-07-29 20:00:38 +02001860 def UseString2()
1861 "knife"->MyFunc()
1862 enddef
1863 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001864 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001865
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001866 # prepending a colon makes it a mark
1867 new
1868 setline(1, ['aaa', 'bbb', 'ccc'])
1869 normal! 3Gmt1G
1870 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001871 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001872 bwipe!
1873
Bram Moolenaare6b53242020-07-01 17:28:33 +02001874 MyFunc(
1875 'continued'
1876 )
1877 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001878 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001879 )
1880
1881 call MyFunc(
1882 'more'
1883 ..
1884 'lines'
1885 )
1886 assert_equal(
1887 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001888 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001889 END
1890 writefile(lines, 'Xcall.vim')
1891 source Xcall.vim
1892 delete('Xcall.vim')
1893enddef
1894
1895def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001896 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001898 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001899 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001900 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001901 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001902 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001903 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001904 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001905enddef
1906
Bram Moolenaar65b95452020-07-19 14:03:09 +02001907def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001908 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001909 vim9script
1910 def MyFunc(arg: string)
1911 echo arg
1912 enddef
1913 MyFunc(1234)
1914 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001915 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001916enddef
1917
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001918def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001919 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001920 vim9script
1921 const var = ''
1922 def MyFunc(arg: string)
1923 var = 'asdf'
1924 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001925 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001926 END
1927 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001928 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001929 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001930
1931 lines =<< trim END
1932 const g:Aconst = 77
1933 def Change()
1934 # comment
1935 g:Aconst = 99
1936 enddef
1937 call Change()
1938 unlet g:Aconst
1939 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001940 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001941enddef
1942
1943" Test that inside :function a Python function can be defined, :def is not
1944" recognized.
1945func Test_function_python()
1946 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001947 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001948 execute py "<< EOF"
1949def do_something():
1950 return 1
1951EOF
1952endfunc
1953
1954def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001955 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001956 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001957 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958 echo 'hello'
1959 enddef
1960
1961 def CallGoneSoon()
1962 GoneSoon()
1963 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001964 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001965
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001966 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001967 CallGoneSoon()
1968 END
1969 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001970 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1971 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001972
1973 delete('XToDelFunc')
1974enddef
1975
Bram Moolenaar7509ad82021-12-14 18:14:37 +00001976func Test_free_dict_while_in_funcstack()
1977 " relies on the sleep command
1978 CheckUnix
1979 call Run_Test_free_dict_while_in_funcstack()
1980endfunc
1981
1982def Run_Test_free_dict_while_in_funcstack()
1983
1984 # this was freeing the TermRun() default argument dictionary while it was
1985 # still referenced in a funcstack_T
1986 var lines =<< trim END
1987 vim9script
1988
1989 &updatetime = 400
1990 def TermRun(_ = {})
1991 def Post()
1992 enddef
1993 def Exec()
1994 term_start('sleep 1', {
1995 term_finish: 'close',
1996 exit_cb: (_, _) => Post(),
1997 })
1998 enddef
1999 Exec()
2000 enddef
2001 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
2002 timer_start(100, (_) => feedkeys("\<F4>"))
2003 timer_start(1000, (_) => feedkeys("\<F4>"))
2004 sleep 1500m
2005 END
2006 CheckScriptSuccess(lines)
2007 nunmap <F4>
2008 set updatetime&
2009enddef
2010
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002011def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002012 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002013 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002014 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002015 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002016 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002017 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002018 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002019 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002020 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002021
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002022 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002023 g:Func1()->assert_equal('Func1')
2024 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002025
2026 delfunc! Func0
2027 delfunc! Func1
2028 delfunc! Func2
2029enddef
2030
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002031def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002032 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002033 vim9script
2034 func Func(arg)
2035 echo a:arg
2036 endfunc
2037 Func('text')
2038 END
2039 writefile(lines, 'XVim9Func')
2040 so XVim9Func
2041
2042 delete('XVim9Func')
2043enddef
2044
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002045let s:funcResult = 0
2046
2047def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002048 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002049enddef
2050
2051def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002052 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002053 return 1234
2054enddef
2055
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002056def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002057 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002058 return 'text'
2059enddef
2060
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002061def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002062 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002063enddef
2064
2065def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002066 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002067 return arg
2068enddef
2069
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002070def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002071 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002072enddef
2073
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002074def FuncOneArgRetString(arg: string): string
2075 return arg
2076enddef
2077
Bram Moolenaar89228602020-04-05 22:14:54 +02002078def FuncOneArgRetAny(arg: any): any
2079 return arg
2080enddef
2081
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002082def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002083 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002084 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002085 Ref1 = FuncNoArgNoRet
2086 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002087 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002088
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002089 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002090 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002091 Ref2 = FuncNoArgNoRet
2092 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002093 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002094
Bram Moolenaar53900992020-08-22 19:02:02 +02002095 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002096 Ref2 = FuncOneArgNoRet
2097 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002098 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002099
Bram Moolenaar53900992020-08-22 19:02:02 +02002100 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002101 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002102 Ref2()->assert_equal(1234)
2103 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002104
Bram Moolenaar53900992020-08-22 19:02:02 +02002105 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002106 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002107 Ref2(13)->assert_equal(13)
2108 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002109enddef
2110
Bram Moolenaar9978d472020-07-05 16:01:56 +02002111def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002112 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002113 for n in repeat([1], 3)
2114 res += n
2115 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002116 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002117
2118 res = 0
2119 for n in add([1, 2], 3)
2120 res += n
2121 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002122 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002123enddef
2124
Bram Moolenaar846178a2020-07-05 17:04:13 +02002125def Test_argv_return_type()
2126 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002127 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002128 for name in argv()
2129 res ..= name
2130 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002131 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002132enddef
2133
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002134def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002135 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002136 RefVoid = FuncNoArgNoRet
2137 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002138 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2139 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002140
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002141 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002142 RefAny = FuncNoArgRetNumber
2143 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002144 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2145 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002146
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002147 var RefAnyNoArgs: func: any = RefAny
2148
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002149 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002150 RefNr = FuncNoArgRetNumber
2151 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002152 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2153 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002154
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002155 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002156 RefStr = FuncNoArgRetString
2157 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002158 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2159 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002160enddef
2161
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002162def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002163 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002164
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002165 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2166 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2167 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2168 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2169 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2170 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 +02002171
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002172 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2173 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2174 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:')
2175 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002176enddef
2177
Bram Moolenaar89228602020-04-05 22:14:54 +02002178def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002179 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002180 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002181 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002182
2183 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002184 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002185
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002186 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002187 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002188 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002189
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002190 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002191enddef
2192
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002193def Test_func_common_type()
2194 def FuncOne(n: number): number
2195 return n
2196 enddef
2197 def FuncTwo(s: string): number
2198 return len(s)
2199 enddef
2200 def FuncThree(n: number, s: string): number
2201 return n + len(s)
2202 enddef
2203 var list = [FuncOne, FuncTwo, FuncThree]
2204 assert_equal(8, list[0](8))
2205 assert_equal(4, list[1]('word'))
2206 assert_equal(7, list[2](3, 'word'))
2207enddef
2208
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002209def MultiLine(
2210 arg1: string,
2211 arg2 = 1234,
2212 ...rest: list<string>
2213 ): string
2214 return arg1 .. arg2 .. join(rest, '-')
2215enddef
2216
Bram Moolenaar2c330432020-04-13 14:41:35 +02002217def MultiLineComment(
2218 arg1: string, # comment
2219 arg2 = 1234, # comment
2220 ...rest: list<string> # comment
2221 ): string # comment
2222 return arg1 .. arg2 .. join(rest, '-')
2223enddef
2224
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002225def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002226 MultiLine('text')->assert_equal('text1234')
2227 MultiLine('text', 777)->assert_equal('text777')
2228 MultiLine('text', 777, 'one')->assert_equal('text777one')
2229 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002230enddef
2231
Bram Moolenaar23e03252020-04-12 22:22:31 +02002232func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002233 call MultiLine('text')->assert_equal('text1234')
2234 call MultiLine('text', 777)->assert_equal('text777')
2235 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2236 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002237endfunc
2238
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002239
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002240" When using CheckScriptFailure() for the below test, E1010 is generated instead
2241" of E1056.
2242func Test_E1056_1059()
2243 let caught_1056 = 0
2244 try
2245 def F():
2246 return 1
2247 enddef
2248 catch /E1056:/
2249 let caught_1056 = 1
2250 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002251 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002252
2253 let caught_1059 = 0
2254 try
2255 def F5(items : list)
2256 echo 'a'
2257 enddef
2258 catch /E1059:/
2259 let caught_1059 = 1
2260 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002261 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002262endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002263
Bram Moolenaar015f4262020-05-05 21:25:22 +02002264func DelMe()
2265 echo 'DelMe'
2266endfunc
2267
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002268def Test_error_reporting()
2269 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002270 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002271 " comment
2272 def Func()
2273 # comment
2274 # comment
2275 invalid
2276 enddef
2277 defcompile
2278 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002279 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002280 try
2281 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002282 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002283 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002284 v:exception->assert_match('Invalid command: invalid')
2285 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002286 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002287 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002288
2289 # comment lines after the start of the function
2290 lines =<< trim END
2291 " comment
2292 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002293 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002294 # comment
2295 # comment
2296 invalid
2297 enddef
2298 defcompile
2299 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002300 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002301 try
2302 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002303 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002304 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002305 v:exception->assert_match('Invalid command: invalid')
2306 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002307 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002308 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002309
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002310 lines =<< trim END
2311 vim9script
2312 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002313 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002314 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002315 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002316 enddef
2317 defcompile
2318 Func()
2319 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002320 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002321 try
2322 source Xdef
2323 assert_report('should have failed')
2324 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002325 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002326 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002327 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002328
Bram Moolenaar08052222020-09-14 17:04:31 +02002329 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002330enddef
2331
Bram Moolenaar015f4262020-05-05 21:25:22 +02002332def Test_deleted_function()
2333 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002334 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002335 'delfunc g:DelMe',
2336 'echo RefMe()'], 'E117:')
2337enddef
2338
2339def Test_unknown_function()
2340 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002341 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002342 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002343enddef
2344
Bram Moolenaar328eac22021-01-07 19:23:08 +01002345def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002346 return Ref('more')
2347enddef
2348
2349def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002350 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002351 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002352enddef
2353
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002354def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002355 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002356 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002357enddef
2358
2359def Test_closure_ref_after_return()
2360 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002361 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002362 unlet g:Ref
2363enddef
2364
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002365def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002366 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002367 g:Extend = (s) => local->add(s)
2368 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002369enddef
2370
2371def Test_closure_two_refs()
2372 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002373 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002374 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002375 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002376 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002377 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002378
2379 unlet g:Extend
2380 unlet g:Read
2381enddef
2382
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002383def ReadRef(Ref: func(): list<string>): string
2384 return join(Ref(), ' ')
2385enddef
2386
Bram Moolenaar5e654232020-09-16 15:22:00 +02002387def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002388 Ref(add)
2389enddef
2390
2391def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002392 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002393 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002394 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002395 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002396 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002397 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002398
2399 unlet g:Extend
2400 unlet g:Read
2401enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002402
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002403def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002404 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002405 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002406enddef
2407
2408def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002409 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002410 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002411enddef
2412
2413def Test_closure_using_argument()
2414 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002415 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002416
2417 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002418 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002419
2420 unlet g:UseArg
2421 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002422
2423 var lines =<< trim END
2424 vim9script
2425 def Test(Fun: func(number): number): list<number>
2426 return map([1, 2, 3], (_, i) => Fun(i))
2427 enddef
2428 def Inc(nr: number): number
2429 return nr + 2
2430 enddef
2431 assert_equal([3, 4, 5], Test(Inc))
2432 END
2433 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002434enddef
2435
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002436def MakeGetAndAppendRefs()
2437 var local = 'a'
2438
2439 def Append(arg: string)
2440 local ..= arg
2441 enddef
2442 g:Append = Append
2443
2444 def Get(): string
2445 return local
2446 enddef
2447 g:Get = Get
2448enddef
2449
2450def Test_closure_append_get()
2451 MakeGetAndAppendRefs()
2452 g:Get()->assert_equal('a')
2453 g:Append('-b')
2454 g:Get()->assert_equal('a-b')
2455 g:Append('-c')
2456 g:Get()->assert_equal('a-b-c')
2457
2458 unlet g:Append
2459 unlet g:Get
2460enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002461
Bram Moolenaar04b12692020-05-04 23:24:44 +02002462def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002463 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002464 def Closure(arg: string): string
2465 return local .. arg
2466 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002467 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002468enddef
2469
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002470func GetResult(Ref)
2471 return a:Ref('some')
2472endfunc
2473
2474def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002475 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002476 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002477 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002478enddef
2479
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002480def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002481 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002482 vim9script
2483 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002484 var name = 0
2485 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002486 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002487 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002488 enddef
2489 Func()
2490 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002491 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002492enddef
2493
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002494def Test_nested_closure_used()
2495 var lines =<< trim END
2496 vim9script
2497 def Func()
2498 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002499 var Closure = () => x
2500 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002501 enddef
2502 Func()
2503 assert_equal('hello', g:Myclosure())
2504 END
2505 CheckScriptSuccess(lines)
2506enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002507
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002508def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002509 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002510 vim9script
2511 def FuncA()
2512 FuncB(0)
2513 enddef
2514 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002515 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002516 enddef
2517 FuncA()
2518 END
2519 CheckScriptFailure(lines, 'E1012:')
2520enddef
2521
Bram Moolenaarf112f302020-12-20 17:47:52 +01002522def Test_global_closure()
2523 var lines =<< trim END
2524 vim9script
2525 def ReverseEveryNLines(n: number, line1: number, line2: number)
2526 var mods = 'sil keepj keepp lockm '
2527 var range = ':' .. line1 .. ',' .. line2
2528 def g:Offset(): number
2529 var offset = (line('.') - line1 + 1) % n
2530 return offset != 0 ? offset : n
2531 enddef
2532 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2533 enddef
2534
2535 new
2536 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2537 ReverseEveryNLines(3, 1, 9)
2538 END
2539 CheckScriptSuccess(lines)
2540 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2541 assert_equal(expected, getline(1, 9))
2542 bwipe!
2543enddef
2544
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002545def Test_global_closure_called_directly()
2546 var lines =<< trim END
2547 vim9script
2548 def Outer()
2549 var x = 1
2550 def g:Inner()
2551 var y = x
2552 x += 1
2553 assert_equal(1, y)
2554 enddef
2555 g:Inner()
2556 assert_equal(2, x)
2557 enddef
2558 Outer()
2559 END
2560 CheckScriptSuccess(lines)
2561 delfunc g:Inner
2562enddef
2563
Bram Moolenaar69c76172021-12-02 16:38:52 +00002564def Test_closure_called_from_legacy()
2565 var lines =<< trim END
2566 vim9script
2567 def Func()
2568 var outer = 'foo'
2569 var F = () => {
2570 outer = 'bar'
2571 }
2572 execute printf('call %s()', string(F))
2573 enddef
2574 Func()
2575 END
2576 CheckScriptFailure(lines, 'E1248')
2577enddef
2578
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002579def Test_failure_in_called_function()
2580 # this was using the frame index as the return value
2581 var lines =<< trim END
2582 vim9script
2583 au TerminalWinOpen * eval [][0]
2584 def PopupTerm(a: any)
2585 # make sure typvals on stack are string
2586 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2587 FireEvent()
2588 enddef
2589 def FireEvent()
2590 do TerminalWinOpen
2591 enddef
2592 # use try/catch to make eval fail
2593 try
2594 call PopupTerm(0)
2595 catch
2596 endtry
2597 au! TerminalWinOpen
2598 END
2599 CheckScriptSuccess(lines)
2600enddef
2601
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002602def Test_nested_lambda()
2603 var lines =<< trim END
2604 vim9script
2605 def Func()
2606 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002607 var Lambda1 = () => 7
2608 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002609 var res = Lambda2()
2610 assert_equal([7, 4], res)
2611 enddef
2612 Func()
2613 END
2614 CheckScriptSuccess(lines)
2615enddef
2616
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002617def Test_double_nested_lambda()
2618 var lines =<< trim END
2619 vim9script
2620 def F(head: string): func(string): func(string): string
2621 return (sep: string): func(string): string => ((tail: string): string => {
2622 return head .. sep .. tail
2623 })
2624 enddef
2625 assert_equal('hello-there', F('hello')('-')('there'))
2626 END
2627 CheckScriptSuccess(lines)
2628enddef
2629
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002630def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002631 var lines =<< trim END
2632 vim9script
2633 def F(text: string): func(string): func(string): string
2634 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002635 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002636 })
2637 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002638 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002639 END
2640 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002641
2642 lines =<< trim END
2643 vim9script
2644 echo range(4)->mapnew((_, v) => {
2645 return range(v) ->mapnew((_, s) => {
2646 return string(s)
2647 })
2648 })
2649 END
2650 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002651
2652 lines =<< trim END
2653 vim9script
2654
2655 def s:func()
2656 range(10)
2657 ->mapnew((_, _) => ({
2658 key: range(10)->mapnew((_, _) => {
2659 return ' '
2660 }),
2661 }))
2662 enddef
2663
2664 defcomp
2665 END
2666 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002667enddef
2668
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002669def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002670 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002671 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002672enddef
2673
2674def Test_lambda_arg_shadows_func()
2675 assert_equal([42], Shadowed())
2676enddef
2677
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002678def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002679 var path: string = empty(dir)
2680 \ ? 'empty'
2681 \ : 'full'
2682 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002683enddef
2684
2685def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002686 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002687enddef
2688
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002689def Test_script_var_in_lambda()
2690 var lines =<< trim END
2691 vim9script
2692 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002693 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002694 END
2695 CheckScriptSuccess(lines)
2696enddef
2697
Bram Moolenaar5e654232020-09-16 15:22:00 +02002698def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002699 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002700 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002701 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002702 ->reverse()
2703 return x
2704enddef
2705
2706def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002707 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002708
2709 var lines =<< trim END
2710 vim9script
2711 var res = [{n: 1, m: 2, s: 'xxx'}]
2712 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2713 v.n,
2714 v.m,
2715 substitute(v.s, '.*', 'yyy', '')
2716 ))
2717 assert_equal(['1:2:yyy'], res)
2718 END
2719 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002720enddef
2721
Bram Moolenaarb6571982021-01-08 22:24:19 +01002722def Test_list_lambda()
2723 timer_start(1000, (_) => 0)
2724 var body = execute(timer_info()[0].callback
2725 ->string()
2726 ->substitute("('", ' ', '')
2727 ->substitute("')", '', '')
2728 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002729 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002730enddef
2731
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002732def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002733 var lines =<< trim END
2734 vim9script
2735 var flist: list<func>
2736 for i in range(10)
2737 var inloop = i
2738 flist[i] = () => inloop
2739 endfor
2740 END
2741 CheckScriptSuccess(lines)
2742
2743 lines =<< trim END
2744 vim9script
2745 if true
2746 var outloop = 5
2747 var flist: list<func>
2748 for i in range(10)
2749 flist[i] = () => outloop
2750 endfor
2751 endif
2752 END
2753 CheckScriptSuccess(lines)
2754
2755 lines =<< trim END
2756 vim9script
2757 if true
2758 var outloop = 5
2759 endif
2760 var flist: list<func>
2761 for i in range(10)
2762 flist[i] = () => outloop
2763 endfor
2764 END
2765 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002766
2767 lines =<< trim END
2768 vim9script
2769 for i in range(10)
2770 var Ref = () => 0
2771 endfor
2772 assert_equal(0, ((i) => 0)(0))
2773 END
2774 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002775enddef
2776
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002777def Test_legacy_lambda()
2778 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002779
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002780 var lines =<< trim END
2781 echo {x -> 'hello ' .. x}('foo')
2782 END
2783 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002784
2785 lines =<< trim END
2786 vim9script
2787 def Func()
2788 echo (() => 'no error')()
2789 enddef
2790 legacy call s:Func()
2791 END
2792 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002793enddef
2794
Bram Moolenaarce024c32021-06-26 13:00:49 +02002795def Test_legacy()
2796 var lines =<< trim END
2797 vim9script
2798 func g:LegacyFunction()
2799 let g:legacyvar = 1
2800 endfunc
2801 def Testit()
2802 legacy call g:LegacyFunction()
2803 enddef
2804 Testit()
2805 assert_equal(1, g:legacyvar)
2806 unlet g:legacyvar
2807 delfunc g:LegacyFunction
2808 END
2809 CheckScriptSuccess(lines)
2810enddef
2811
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002812def Test_legacy_errors()
2813 for cmd in ['if', 'elseif', 'else', 'endif',
2814 'for', 'endfor', 'continue', 'break',
2815 'while', 'endwhile',
2816 'try', 'catch', 'finally', 'endtry']
2817 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2818 endfor
2819enddef
2820
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002821def Test_call_legacy_with_dict()
2822 var lines =<< trim END
2823 vim9script
2824 func Legacy() dict
2825 let g:result = self.value
2826 endfunc
2827 def TestDirect()
2828 var d = {value: 'yes', func: Legacy}
2829 d.func()
2830 enddef
2831 TestDirect()
2832 assert_equal('yes', g:result)
2833 unlet g:result
2834
2835 def TestIndirect()
2836 var d = {value: 'foo', func: Legacy}
2837 var Fi = d.func
2838 Fi()
2839 enddef
2840 TestIndirect()
2841 assert_equal('foo', g:result)
2842 unlet g:result
2843
2844 var d = {value: 'bar', func: Legacy}
2845 d.func()
2846 assert_equal('bar', g:result)
2847 unlet g:result
2848 END
2849 CheckScriptSuccess(lines)
2850enddef
2851
Bram Moolenaarab360522021-01-10 14:02:28 +01002852def DoFilterThis(a: string): list<string>
2853 # closure nested inside another closure using argument
2854 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2855 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2856enddef
2857
2858def Test_nested_closure_using_argument()
2859 assert_equal(['x', 'x2'], DoFilterThis('x'))
2860enddef
2861
Bram Moolenaar0186e582021-01-10 18:33:11 +01002862def Test_triple_nested_closure()
2863 var what = 'x'
2864 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2865 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2866 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2867enddef
2868
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002869func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002870 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002871 call Run_Test_silent_echo()
2872endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002873
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002874def Run_Test_silent_echo()
2875 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002876 vim9script
2877 def EchoNothing()
2878 silent echo ''
2879 enddef
2880 defcompile
2881 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002882 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002883
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002884 # Check that the balloon shows up after a mouse move
2885 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2886 term_sendkeys(buf, ":abc")
2887 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002888
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002889 # clean up
2890 StopVimInTerminal(buf)
2891 delete('XTest_silent_echo')
2892enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002893
Bram Moolenaar171fb922020-10-28 16:54:47 +01002894def SilentlyError()
2895 execute('silent! invalid')
2896 g:did_it = 'yes'
2897enddef
2898
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002899func UserError()
2900 silent! invalid
2901endfunc
2902
2903def SilentlyUserError()
2904 UserError()
2905 g:did_it = 'yes'
2906enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002907
2908" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002909func Test_ignore_silent_error()
2910 let g:did_it = 'no'
2911 call SilentlyError()
2912 call assert_equal('yes', g:did_it)
2913
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002914 let g:did_it = 'no'
2915 call SilentlyUserError()
2916 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002917
2918 unlet g:did_it
2919endfunc
2920
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002921def Test_ignore_silent_error_in_filter()
2922 var lines =<< trim END
2923 vim9script
2924 def Filter(winid: number, key: string): bool
2925 if key == 'o'
2926 silent! eval [][0]
2927 return true
2928 endif
2929 return popup_filter_menu(winid, key)
2930 enddef
2931
Bram Moolenaare0de1712020-12-02 17:36:54 +01002932 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002933 feedkeys("o\r", 'xnt')
2934 END
2935 CheckScriptSuccess(lines)
2936enddef
2937
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002938def Fibonacci(n: number): number
2939 if n < 2
2940 return n
2941 else
2942 return Fibonacci(n - 1) + Fibonacci(n - 2)
2943 endif
2944enddef
2945
Bram Moolenaar985116a2020-07-12 17:31:09 +02002946def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002947 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002948enddef
2949
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002950def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002951 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002952 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002953 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002954 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002955 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002956enddef
2957
2958def Test_closure_in_map()
2959 mkdir('XclosureDir/tdir', 'p')
2960 writefile(['111'], 'XclosureDir/file1')
2961 writefile(['222'], 'XclosureDir/file2')
2962 writefile(['333'], 'XclosureDir/tdir/file3')
2963
Bram Moolenaare0de1712020-12-02 17:36:54 +01002964 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002965
2966 delete('XclosureDir', 'rf')
2967enddef
2968
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002969def Test_invalid_function_name()
2970 var lines =<< trim END
2971 vim9script
2972 def s: list<string>
2973 END
2974 CheckScriptFailure(lines, 'E129:')
2975
2976 lines =<< trim END
2977 vim9script
2978 def g: list<string>
2979 END
2980 CheckScriptFailure(lines, 'E129:')
2981
2982 lines =<< trim END
2983 vim9script
2984 def <SID>: list<string>
2985 END
2986 CheckScriptFailure(lines, 'E884:')
2987
2988 lines =<< trim END
2989 vim9script
2990 def F list<string>
2991 END
2992 CheckScriptFailure(lines, 'E488:')
2993enddef
2994
Bram Moolenaara90afb92020-07-15 22:38:56 +02002995def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002996 var lines =<< trim END
2997 var Xsetlist: func
2998 Xsetlist = function('setloclist', [0])
2999 Xsetlist([], ' ', {title: 'test'})
3000 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003001
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003002 Xsetlist = function('setloclist', [0, [], ' '])
3003 Xsetlist({title: 'test'})
3004 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003005
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003006 Xsetlist = function('setqflist')
3007 Xsetlist([], ' ', {title: 'test'})
3008 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003009
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003010 Xsetlist = function('setqflist', [[], ' '])
3011 Xsetlist({title: 'test'})
3012 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02003013
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003014 var Len: func: number = function('len', ['word'])
3015 assert_equal(4, Len())
3016
3017 var RepeatFunc = function('repeat', ['o'])
3018 assert_equal('ooooo', RepeatFunc(5))
3019 END
3020 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003021
3022 lines =<< trim END
3023 vim9script
3024 def Foo(Parser: any)
3025 enddef
3026 var Expr: func(dict<any>): dict<any>
3027 const Call = Foo(Expr)
3028 END
3029 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003030enddef
3031
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003032def Test_cmd_modifier()
3033 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003034 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003035enddef
3036
3037def Test_restore_modifiers()
3038 # check that when compiling a :def function command modifiers are not messed
3039 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003040 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003041 vim9script
3042 set eventignore=
3043 autocmd QuickFixCmdPost * copen
3044 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003045 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003046 enddef
3047 func Func()
3048 noautocmd call s:AutocmdsDisabled()
3049 let g:ei_after = &eventignore
3050 endfunc
3051 Func()
3052 END
3053 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003054 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003055enddef
3056
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003057def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003058 eval 1 + 2
3059 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003060 # call not on fourth line
3061 StackBot()
3062enddef
3063
3064def StackBot()
3065 # throw an error
3066 eval [][0]
3067enddef
3068
3069def Test_callstack_def()
3070 try
3071 StackTop()
3072 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003073 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003074 endtry
3075enddef
3076
Bram Moolenaare8211a32020-10-09 22:04:29 +02003077" Re-using spot for variable used in block
3078def Test_block_scoped_var()
3079 var lines =<< trim END
3080 vim9script
3081 def Func()
3082 var x = ['a', 'b', 'c']
3083 if 1
3084 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003085 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003086 endif
3087 var z = x
3088 assert_equal(['x', 'x', 'x'], z)
3089 enddef
3090 Func()
3091 END
3092 CheckScriptSuccess(lines)
3093enddef
3094
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003095def Test_reset_did_emsg()
3096 var lines =<< trim END
3097 @s = 'blah'
3098 au BufWinLeave * #
3099 def Func()
3100 var winid = popup_create('popup', {})
3101 exe '*s'
3102 popup_close(winid)
3103 enddef
3104 Func()
3105 END
3106 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003107 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003108enddef
3109
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003110def Test_did_emsg_reset()
3111 # executing an autocommand resets did_emsg, this should not result in a
3112 # builtin function considered failing
3113 var lines =<< trim END
3114 vim9script
3115 au BufWinLeave * #
3116 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003117 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003118 eval [][0]
3119 enddef
3120 nno <F3> <cmd>call <sid>Func()<cr>
3121 feedkeys("\<F3>\e", 'xt')
3122 END
3123 writefile(lines, 'XemsgReset')
3124 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3125 delete('XemsgReset')
3126 nunmap <F3>
3127 au! BufWinLeave
3128enddef
3129
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003130def Test_abort_with_silent_call()
3131 var lines =<< trim END
3132 vim9script
3133 g:result = 'none'
3134 def Func()
3135 g:result += 3
3136 g:result = 'yes'
3137 enddef
3138 # error is silenced, but function aborts on error
3139 silent! Func()
3140 assert_equal('none', g:result)
3141 unlet g:result
3142 END
3143 CheckScriptSuccess(lines)
3144enddef
3145
Bram Moolenaarf665e972020-12-05 19:17:16 +01003146def Test_continues_with_silent_error()
3147 var lines =<< trim END
3148 vim9script
3149 g:result = 'none'
3150 def Func()
3151 silent! g:result += 3
3152 g:result = 'yes'
3153 enddef
3154 # error is silenced, function does not abort
3155 Func()
3156 assert_equal('yes', g:result)
3157 unlet g:result
3158 END
3159 CheckScriptSuccess(lines)
3160enddef
3161
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003162def Test_abort_even_with_silent()
3163 var lines =<< trim END
3164 vim9script
3165 g:result = 'none'
3166 def Func()
3167 eval {-> ''}() .. '' .. {}['X']
3168 g:result = 'yes'
3169 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003170 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003171 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003172 unlet g:result
3173 END
3174 CheckScriptSuccess(lines)
3175enddef
3176
Bram Moolenaarf665e972020-12-05 19:17:16 +01003177def Test_cmdmod_silent_restored()
3178 var lines =<< trim END
3179 vim9script
3180 def Func()
3181 g:result = 'none'
3182 silent! g:result += 3
3183 g:result = 'none'
3184 g:result += 3
3185 enddef
3186 Func()
3187 END
3188 # can't use CheckScriptFailure, it ignores the :silent!
3189 var fname = 'Xdefsilent'
3190 writefile(lines, fname)
3191 var caught = 'no'
3192 try
3193 exe 'source ' .. fname
3194 catch /E1030:/
3195 caught = 'yes'
3196 assert_match('Func, line 4', v:throwpoint)
3197 endtry
3198 assert_equal('yes', caught)
3199 delete(fname)
3200enddef
3201
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003202def Test_cmdmod_silent_nested()
3203 var lines =<< trim END
3204 vim9script
3205 var result = ''
3206
3207 def Error()
3208 result ..= 'Eb'
3209 eval [][0]
3210 result ..= 'Ea'
3211 enddef
3212
3213 def Crash()
3214 result ..= 'Cb'
3215 sil! Error()
3216 result ..= 'Ca'
3217 enddef
3218
3219 Crash()
3220 assert_equal('CbEbEaCa', result)
3221 END
3222 CheckScriptSuccess(lines)
3223enddef
3224
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003225def Test_dict_member_with_silent()
3226 var lines =<< trim END
3227 vim9script
3228 g:result = 'none'
3229 var d: dict<any>
3230 def Func()
3231 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003232 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003233 catch
3234 endtry
3235 enddef
3236 silent! Func()
3237 assert_equal('0', g:result)
3238 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003239 END
3240 CheckScriptSuccess(lines)
3241enddef
3242
Bram Moolenaarf9041332021-01-21 19:41:16 +01003243def Test_skip_cmds_with_silent()
3244 var lines =<< trim END
3245 vim9script
3246
3247 def Func(b: bool)
3248 Crash()
3249 enddef
3250
3251 def Crash()
3252 sil! :/not found/d _
3253 sil! :/not found/put _
3254 enddef
3255
3256 Func(true)
3257 END
3258 CheckScriptSuccess(lines)
3259enddef
3260
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003261def Test_opfunc()
3262 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3263 def g:Opfunc(_: any): string
3264 setline(1, 'ASDF')
3265 return ''
3266 enddef
3267 new
3268 setline(1, 'asdf')
3269 feedkeys("\<F3>$", 'x')
3270 assert_equal('ASDF', getline(1))
3271
3272 bwipe!
3273 nunmap <F3>
3274enddef
3275
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003276func Test_opfunc_error()
3277 CheckScreendump
3278 call Run_Test_opfunc_error()
3279endfunc
3280
3281def Run_Test_opfunc_error()
3282 # test that the error from Opfunc() is displayed right away
3283 var lines =<< trim END
3284 vim9script
3285
3286 def Opfunc(type: string)
3287 try
3288 eval [][0]
3289 catch /nothing/ # error not caught
3290 endtry
3291 enddef
3292 &operatorfunc = Opfunc
3293 nnoremap <expr> l <SID>L()
3294 def L(): string
3295 return 'l'
3296 enddef
3297 'x'->repeat(10)->setline(1)
3298 feedkeys('g@l', 'n')
3299 feedkeys('llll')
3300 END
3301 call writefile(lines, 'XTest_opfunc_error')
3302
3303 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003304 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003305 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003306
3307 # clean up
3308 StopVimInTerminal(buf)
3309 delete('XTest_opfunc_error')
3310enddef
3311
Bram Moolenaar077a4232020-12-22 18:33:27 +01003312" this was crashing on exit
3313def Test_nested_lambda_in_closure()
3314 var lines =<< trim END
3315 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003316 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003317 def Outer()
3318 def g:Inner()
3319 echo map([1, 2, 3], {_, v -> v + 1})
3320 enddef
3321 g:Inner()
3322 enddef
3323 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003324 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003325 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003326 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003327 return
3328 endif
3329 assert_equal(['Done'], readfile('XnestedDone'))
3330 delete('XnestedDone')
3331enddef
3332
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003333def Test_check_func_arg_types()
3334 var lines =<< trim END
3335 vim9script
3336 def F1(x: string): string
3337 return x
3338 enddef
3339
3340 def F2(x: number): number
3341 return x + 1
3342 enddef
3343
3344 def G(g: func): dict<func>
3345 return {f: g}
3346 enddef
3347
3348 def H(d: dict<func>): string
3349 return d.f('a')
3350 enddef
3351 END
3352
3353 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3354 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3355enddef
3356
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003357def Test_list_any_type_checked()
3358 var lines =<< trim END
3359 vim9script
3360 def Foo()
3361 --decl--
3362 Bar(l)
3363 enddef
3364 def Bar(ll: list<dict<any>>)
3365 enddef
3366 Foo()
3367 END
3368 lines[2] = 'var l: list<any>'
3369 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3370
3371 lines[2] = 'var l: list<any> = []'
3372 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3373
3374 lines[2] = 'var l: list<any> = [11]'
3375 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3376enddef
3377
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003378def Test_compile_error()
3379 var lines =<< trim END
3380 def g:Broken()
3381 echo 'a' + {}
3382 enddef
3383 call g:Broken()
3384 END
3385 # First call: compilation error
3386 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3387
3388 # Second call won't try compiling again
3389 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003390 delfunc g:Broken
3391
3392 # No error when compiling with :silent!
3393 lines =<< trim END
3394 def g:Broken()
3395 echo 'a' + []
3396 enddef
3397 silent! defcompile
3398 END
3399 CheckScriptSuccess(lines)
3400
3401 # Calling the function won't try compiling again
3402 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3403 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003404enddef
3405
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003406def Test_ignored_argument()
3407 var lines =<< trim END
3408 vim9script
3409 def Ignore(_, _): string
3410 return 'yes'
3411 enddef
3412 assert_equal('yes', Ignore(1, 2))
3413
3414 func Ok(_)
3415 return a:_
3416 endfunc
3417 assert_equal('ok', Ok('ok'))
3418
3419 func Oktoo()
3420 let _ = 'too'
3421 return _
3422 endfunc
3423 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003424
3425 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003426 END
3427 CheckScriptSuccess(lines)
3428
3429 lines =<< trim END
3430 def Ignore(_: string): string
3431 return _
3432 enddef
3433 defcompile
3434 END
3435 CheckScriptFailure(lines, 'E1181:', 1)
3436
3437 lines =<< trim END
3438 var _ = 1
3439 END
3440 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003441
3442 lines =<< trim END
3443 var x = _
3444 END
3445 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003446enddef
3447
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003448def Test_too_many_arguments()
3449 var lines =<< trim END
3450 echo [0, 1, 2]->map(() => 123)
3451 END
3452 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3453
3454 lines =<< trim END
3455 echo [0, 1, 2]->map((_) => 123)
3456 END
3457 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3458enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003459
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003460def Test_closing_brace_at_start_of_line()
3461 var lines =<< trim END
3462 def Func()
3463 enddef
3464 Func(
3465 )
3466 END
3467 call CheckDefAndScriptSuccess(lines)
3468enddef
3469
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003470func CreateMydict()
3471 let g:mydict = {}
3472 func g:mydict.afunc()
3473 let g:result = self.key
3474 endfunc
3475endfunc
3476
3477def Test_numbered_function_reference()
3478 CreateMydict()
3479 var output = execute('legacy func g:mydict.afunc')
3480 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3481 execute 'function(' .. funcName .. ', [], {key: 42})()'
3482 # check that the function still exists
3483 assert_equal(output, execute('legacy func g:mydict.afunc'))
3484 unlet g:mydict
3485enddef
3486
Bram Moolenaar20677332021-06-06 17:02:53 +02003487if has('python3')
3488 def Test_python3_heredoc()
3489 py3 << trim EOF
3490 import vim
3491 vim.vars['didit'] = 'yes'
3492 EOF
3493 assert_equal('yes', g:didit)
3494
3495 python3 << trim EOF
3496 import vim
3497 vim.vars['didit'] = 'again'
3498 EOF
3499 assert_equal('again', g:didit)
3500 enddef
3501endif
3502
3503" This messes up syntax highlight, keep near the end.
3504if has('lua')
3505 def Test_lua_heredoc()
3506 g:d = {}
3507 lua << trim EOF
3508 x = vim.eval('g:d')
3509 x['key'] = 'val'
3510 EOF
3511 assert_equal('val', g:d.key)
3512 enddef
3513endif
3514
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003515
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003516" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker