blob: 7b2f5016c3fac979eae3b9e81c1312c9fa515155 [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 Moolenaard2939812021-12-30 17:09:05 +00001496 var lines =<< trim END
1497 def Func(x: number)
1498 var x = 234
1499 enddef
1500 END
1501 CheckDefFailure(lines, 'E1006:')
1502
1503 lines =<< trim END
1504 def Func(Ref: number)
1505 def Ref()
1506 enddef
1507 enddef
1508 END
1509 CheckDefFailure(lines, 'E1073:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001510enddef
1511
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001512def DictArg(arg: dict<string>)
1513 arg['key'] = 'value'
1514enddef
1515
1516def ListArg(arg: list<string>)
1517 arg[0] = 'value'
1518enddef
1519
1520def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001521 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001522 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001523 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001524 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001525 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001526 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001527 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001528
Bram Moolenaard2c61702020-09-06 15:58:36 +02001529 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001530 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001531enddef
1532
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001533" These argument names are reserved in legacy functions.
1534def WithReservedNames(firstline: string, lastline: string): string
1535 return firstline .. lastline
1536enddef
1537
1538def Test_argument_names()
1539 assert_equal('OK', WithReservedNames('O', 'K'))
1540enddef
1541
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001542def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001543 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001544 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001545enddef
1546
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001547func DefinedLater(arg)
1548 return a:arg
1549endfunc
1550
1551def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001552 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001553 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001554 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001555
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001556 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001557 vim9script
1558 def RetNumber(): number
1559 return 123
1560 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001561 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001562 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001563 END
1564 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001565
1566 lines =<< trim END
1567 vim9script
1568 def RetNumber(): number
1569 return 123
1570 enddef
1571 def Bar(F: func: number): number
1572 return F()
1573 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001574 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001575 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001576 END
1577 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001578
1579 lines =<< trim END
1580 vim9script
1581 def UseNumber(nr: number)
1582 echo nr
1583 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001584 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001585 Funcref(123)
1586 END
1587 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001588
1589 lines =<< trim END
1590 vim9script
1591 def UseNumber(nr: number)
1592 echo nr
1593 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001594 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001595 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001596 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001597
1598 lines =<< trim END
1599 vim9script
1600 def EchoNr(nr = 34)
1601 g:echo = nr
1602 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001603 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001604 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001605 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001606 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001607 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001608 END
1609 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001610
1611 lines =<< trim END
1612 vim9script
1613 def EchoList(...l: list<number>)
1614 g:echo = l
1615 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001616 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001617 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001618 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001619 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001620 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001621 END
1622 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001623
1624 lines =<< trim END
1625 vim9script
1626 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1627 g:optarg = opt
1628 g:listarg = l
1629 return nr
1630 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001631 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001632 Funcref(10)->assert_equal(10)
1633 g:optarg->assert_equal(12)
1634 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001635
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001636 Funcref(11, 22)->assert_equal(11)
1637 g:optarg->assert_equal(22)
1638 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001639
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001640 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1641 g:optarg->assert_equal(18)
1642 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001643 END
1644 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001645enddef
1646
1647let SomeFunc = function('len')
1648let NotAFunc = 'text'
1649
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001650def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001651 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001652 var Ref1: func(bool): string
1653 var Ref2: func(bool): number
1654 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001655 Ref3 = g:cond ? Ref1 : Ref2
1656
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001657 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001658 var Refa1: func(bool): number
1659 var Refa2: func(bool, number): number
1660 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001661 Refa3 = g:cond ? Refa1 : Refa2
1662
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001663 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001664 var Refb1: func(bool, string): number
1665 var Refb2: func(string, number): number
1666 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001667 Refb3 = g:cond ? Refb1 : Refb2
1668enddef
1669
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001670def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001671 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001672enddef
1673
1674def DefinedEvenLater(arg: string): string
1675 return arg
1676enddef
1677
1678def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001679 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001680 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001681enddef
1682
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001683def Test_nested_function_with_nextcmd()
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001684 var lines =<< trim END
1685 vim9script
1686 # Define an outer function
1687 def FirstFunction()
1688 # Define an inner function
1689 def SecondFunction()
1690 # the function has a body, a double free is detected.
1691 AAAAA
1692
1693 # enddef followed by | or } followed by # one or more characters
1694 enddef|BBBB
1695 enddef
1696
1697 # Compile all functions
1698 defcompile
1699 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001700 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001701enddef
1702
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001703def Test_nested_function_with_args_split()
1704 var lines =<< trim END
1705 vim9script
1706 def FirstFunction()
1707 def SecondFunction(
1708 )
1709 # had a double free if the right parenthesis of the nested function is
1710 # on the next line
1711
1712 enddef|BBBB
1713 enddef
1714 # Compile all functions
1715 defcompile
1716 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001717 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
1718
1719 lines =<< trim END
1720 vim9script
1721 def FirstFunction()
1722 func SecondFunction()
1723 endfunc|BBBB
1724 enddef
1725 defcompile
1726 END
1727 CheckScriptFailure(lines, 'E1173: Text found after endfunction: BBBB')
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001728enddef
1729
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001730def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001731 CheckScriptFailure([
1732 'def Func(): number',
1733 'return "a"',
1734 'enddef',
1735 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001736 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001737 CheckScriptFailure([
1738 'def Func(): string',
1739 'return 1',
1740 'enddef',
1741 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001742 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001743 CheckScriptFailure([
1744 'def Func(): void',
1745 'return "a"',
1746 'enddef',
1747 'defcompile'],
1748 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001749 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001750 CheckScriptFailure([
1751 'def Func()',
1752 'return "a"',
1753 'enddef',
1754 'defcompile'],
1755 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001756 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001757
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001758 CheckScriptFailure([
1759 'def Func(): number',
1760 'return',
1761 'enddef',
1762 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001763 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001764
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001765 CheckScriptFailure([
1766 'def Func():number',
1767 'return 123',
1768 'enddef',
1769 'defcompile'], 'E1069:')
1770 delfunc! g:Func
1771
1772 CheckScriptFailure([
1773 'def Func() :number',
1774 'return 123',
1775 'enddef',
1776 'defcompile'], 'E1059:')
1777 delfunc! g:Func
1778
1779 CheckScriptFailure([
1780 'def Func() : number',
1781 'return 123',
1782 'enddef',
1783 'defcompile'], 'E1059:')
1784 delfunc! g:Func
1785
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001786 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001787 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001788 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001789 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001790 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001791 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001792
1793 CheckScriptFailure([
1794 'vim9script',
1795 'def FuncB()',
1796 ' return 123',
1797 'enddef',
1798 'def FuncA()',
1799 ' FuncB()',
1800 'enddef',
1801 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001802enddef
1803
1804def Test_arg_type_wrong()
1805 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001806 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001807 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001808 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001809 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1810 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001811enddef
1812
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001813def Test_white_space_before_comma()
1814 var lines =<< trim END
1815 vim9script
1816 def Func(a: number , b: number)
1817 enddef
1818 END
1819 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001820 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001821enddef
1822
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001823def Test_white_space_after_comma()
1824 var lines =<< trim END
1825 vim9script
1826 def Func(a: number,b: number)
1827 enddef
1828 END
1829 CheckScriptFailure(lines, 'E1069:')
1830
1831 # OK in legacy function
1832 lines =<< trim END
1833 vim9script
1834 func Func(a,b)
1835 endfunc
1836 END
1837 CheckScriptSuccess(lines)
1838enddef
1839
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001840def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001841 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001842 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001843 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001844 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001845 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001846 enddef
1847 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001848 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001849
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001850 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001851 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001852 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001853
Bram Moolenaar67979662020-06-20 22:50:47 +02001854 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001855 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001856 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001857
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001858 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001859 def ListFunc(arg: list<number>)
1860 listvar = arg
1861 enddef
1862 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001863 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001864
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001865 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866 def DictFunc(arg: dict<number>)
1867 dictvar = arg
1868 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001869 {a: 1, b: 2}->DictFunc()
1870 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001871 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001872 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001873 enddef
1874 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001875 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876
Bram Moolenaare0de1712020-12-02 17:36:54 +01001877 {a: 3, b: 4}->DictFunc()
1878 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001879
1880 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001881 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001882 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001883 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001884
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001885 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001886 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001887 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001888 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001889 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001890 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001891
1892 def UseString()
1893 'xyork'->MyFunc()
1894 enddef
1895 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001896 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001897
Bram Moolenaar10409562020-07-29 20:00:38 +02001898 def UseString2()
1899 "knife"->MyFunc()
1900 enddef
1901 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001902 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001903
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001904 # prepending a colon makes it a mark
1905 new
1906 setline(1, ['aaa', 'bbb', 'ccc'])
1907 normal! 3Gmt1G
1908 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001909 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001910 bwipe!
1911
Bram Moolenaare6b53242020-07-01 17:28:33 +02001912 MyFunc(
1913 'continued'
1914 )
1915 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001916 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001917 )
1918
1919 call MyFunc(
1920 'more'
1921 ..
1922 'lines'
1923 )
1924 assert_equal(
1925 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001926 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001927 END
1928 writefile(lines, 'Xcall.vim')
1929 source Xcall.vim
1930 delete('Xcall.vim')
1931enddef
1932
1933def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001934 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001935 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001936 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001937 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001938 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001939 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001940 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001941 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001942 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001943enddef
1944
Bram Moolenaar65b95452020-07-19 14:03:09 +02001945def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001946 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001947 vim9script
1948 def MyFunc(arg: string)
1949 echo arg
1950 enddef
1951 MyFunc(1234)
1952 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001953 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001954enddef
1955
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001956def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001957 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958 vim9script
1959 const var = ''
1960 def MyFunc(arg: string)
1961 var = 'asdf'
1962 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001963 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001964 END
1965 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001966 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001967 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001968
1969 lines =<< trim END
1970 const g:Aconst = 77
1971 def Change()
1972 # comment
1973 g:Aconst = 99
1974 enddef
1975 call Change()
1976 unlet g:Aconst
1977 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001978 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001979enddef
1980
1981" Test that inside :function a Python function can be defined, :def is not
1982" recognized.
1983func Test_function_python()
1984 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001985 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001986 execute py "<< EOF"
1987def do_something():
1988 return 1
1989EOF
1990endfunc
1991
1992def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001993 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001994 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001995 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001996 echo 'hello'
1997 enddef
1998
1999 def CallGoneSoon()
2000 GoneSoon()
2001 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02002002 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002003
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002004 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002005 CallGoneSoon()
2006 END
2007 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002008 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
2009 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002010
2011 delete('XToDelFunc')
2012enddef
2013
Bram Moolenaar7509ad82021-12-14 18:14:37 +00002014func Test_free_dict_while_in_funcstack()
2015 " relies on the sleep command
2016 CheckUnix
2017 call Run_Test_free_dict_while_in_funcstack()
2018endfunc
2019
2020def Run_Test_free_dict_while_in_funcstack()
2021
2022 # this was freeing the TermRun() default argument dictionary while it was
2023 # still referenced in a funcstack_T
2024 var lines =<< trim END
2025 vim9script
2026
2027 &updatetime = 400
2028 def TermRun(_ = {})
2029 def Post()
2030 enddef
2031 def Exec()
2032 term_start('sleep 1', {
2033 term_finish: 'close',
2034 exit_cb: (_, _) => Post(),
2035 })
2036 enddef
2037 Exec()
2038 enddef
2039 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
2040 timer_start(100, (_) => feedkeys("\<F4>"))
2041 timer_start(1000, (_) => feedkeys("\<F4>"))
2042 sleep 1500m
2043 END
2044 CheckScriptSuccess(lines)
2045 nunmap <F4>
2046 set updatetime&
2047enddef
2048
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002049def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002050 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002051 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002052 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002053 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002054 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002055 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002056 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002057 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002058 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002059
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002060 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002061 g:Func1()->assert_equal('Func1')
2062 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002063
2064 delfunc! Func0
2065 delfunc! Func1
2066 delfunc! Func2
2067enddef
2068
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002069def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002070 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002071 vim9script
2072 func Func(arg)
2073 echo a:arg
2074 endfunc
2075 Func('text')
2076 END
2077 writefile(lines, 'XVim9Func')
2078 so XVim9Func
2079
2080 delete('XVim9Func')
2081enddef
2082
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002083let s:funcResult = 0
2084
2085def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002086 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002087enddef
2088
2089def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002090 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002091 return 1234
2092enddef
2093
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002094def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002095 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002096 return 'text'
2097enddef
2098
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002099def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002100 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002101enddef
2102
2103def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002104 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002105 return arg
2106enddef
2107
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002108def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002109 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002110enddef
2111
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002112def FuncOneArgRetString(arg: string): string
2113 return arg
2114enddef
2115
Bram Moolenaar89228602020-04-05 22:14:54 +02002116def FuncOneArgRetAny(arg: any): any
2117 return arg
2118enddef
2119
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002120def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002121 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002122 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002123 Ref1 = FuncNoArgNoRet
2124 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002125 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002126
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002127 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002128 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002129 Ref2 = FuncNoArgNoRet
2130 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002131 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002132
Bram Moolenaar53900992020-08-22 19:02:02 +02002133 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002134 Ref2 = FuncOneArgNoRet
2135 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002136 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002137
Bram Moolenaar53900992020-08-22 19:02:02 +02002138 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002139 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002140 Ref2()->assert_equal(1234)
2141 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002142
Bram Moolenaar53900992020-08-22 19:02:02 +02002143 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002144 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002145 Ref2(13)->assert_equal(13)
2146 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002147enddef
2148
Bram Moolenaar9978d472020-07-05 16:01:56 +02002149def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002150 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002151 for n in repeat([1], 3)
2152 res += n
2153 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002154 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002155
2156 res = 0
2157 for n in add([1, 2], 3)
2158 res += n
2159 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002160 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002161enddef
2162
Bram Moolenaar846178a2020-07-05 17:04:13 +02002163def Test_argv_return_type()
2164 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002165 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002166 for name in argv()
2167 res ..= name
2168 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002169 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002170enddef
2171
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002172def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002173 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002174 RefVoid = FuncNoArgNoRet
2175 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002176 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2177 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002178
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002179 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002180 RefAny = FuncNoArgRetNumber
2181 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002182 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2183 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002184
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002185 var RefAnyNoArgs: func: any = RefAny
2186
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002187 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002188 RefNr = FuncNoArgRetNumber
2189 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002190 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2191 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002192
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002193 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002194 RefStr = FuncNoArgRetString
2195 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002196 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2197 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002198enddef
2199
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002200def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002201 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002202
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002203 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2204 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2205 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2206 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2207 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2208 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 +02002209
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002210 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2211 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2212 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:')
2213 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002214enddef
2215
Bram Moolenaar89228602020-04-05 22:14:54 +02002216def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002217 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002218 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002219 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002220
2221 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002222 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002223
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002224 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002225 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002226 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002227
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002228 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002229enddef
2230
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002231def Test_func_common_type()
2232 def FuncOne(n: number): number
2233 return n
2234 enddef
2235 def FuncTwo(s: string): number
2236 return len(s)
2237 enddef
2238 def FuncThree(n: number, s: string): number
2239 return n + len(s)
2240 enddef
2241 var list = [FuncOne, FuncTwo, FuncThree]
2242 assert_equal(8, list[0](8))
2243 assert_equal(4, list[1]('word'))
2244 assert_equal(7, list[2](3, 'word'))
2245enddef
2246
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002247def MultiLine(
2248 arg1: string,
2249 arg2 = 1234,
2250 ...rest: list<string>
2251 ): string
2252 return arg1 .. arg2 .. join(rest, '-')
2253enddef
2254
Bram Moolenaar2c330432020-04-13 14:41:35 +02002255def MultiLineComment(
2256 arg1: string, # comment
2257 arg2 = 1234, # comment
2258 ...rest: list<string> # comment
2259 ): string # comment
2260 return arg1 .. arg2 .. join(rest, '-')
2261enddef
2262
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002263def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002264 MultiLine('text')->assert_equal('text1234')
2265 MultiLine('text', 777)->assert_equal('text777')
2266 MultiLine('text', 777, 'one')->assert_equal('text777one')
2267 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002268enddef
2269
Bram Moolenaar23e03252020-04-12 22:22:31 +02002270func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002271 call MultiLine('text')->assert_equal('text1234')
2272 call MultiLine('text', 777)->assert_equal('text777')
2273 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2274 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002275endfunc
2276
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002277
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002278" When using CheckScriptFailure() for the below test, E1010 is generated instead
2279" of E1056.
2280func Test_E1056_1059()
2281 let caught_1056 = 0
2282 try
2283 def F():
2284 return 1
2285 enddef
2286 catch /E1056:/
2287 let caught_1056 = 1
2288 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002289 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002290
2291 let caught_1059 = 0
2292 try
2293 def F5(items : list)
2294 echo 'a'
2295 enddef
2296 catch /E1059:/
2297 let caught_1059 = 1
2298 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002299 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002300endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002301
Bram Moolenaar015f4262020-05-05 21:25:22 +02002302func DelMe()
2303 echo 'DelMe'
2304endfunc
2305
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002306def Test_error_reporting()
2307 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002308 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002309 " comment
2310 def Func()
2311 # comment
2312 # comment
2313 invalid
2314 enddef
2315 defcompile
2316 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002317 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002318 try
2319 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002320 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002321 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002322 v:exception->assert_match('Invalid command: invalid')
2323 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002324 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002325 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002326
2327 # comment lines after the start of the function
2328 lines =<< trim END
2329 " comment
2330 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002331 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002332 # comment
2333 # comment
2334 invalid
2335 enddef
2336 defcompile
2337 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002338 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002339 try
2340 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002341 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002342 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002343 v:exception->assert_match('Invalid command: invalid')
2344 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002345 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002346 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002347
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002348 lines =<< trim END
2349 vim9script
2350 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002351 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002352 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002353 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002354 enddef
2355 defcompile
2356 Func()
2357 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002358 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002359 try
2360 source Xdef
2361 assert_report('should have failed')
2362 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002363 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002364 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002365 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002366
Bram Moolenaar08052222020-09-14 17:04:31 +02002367 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002368enddef
2369
Bram Moolenaar015f4262020-05-05 21:25:22 +02002370def Test_deleted_function()
2371 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002372 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002373 'delfunc g:DelMe',
2374 'echo RefMe()'], 'E117:')
2375enddef
2376
2377def Test_unknown_function()
2378 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002379 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002380 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002381enddef
2382
Bram Moolenaar328eac22021-01-07 19:23:08 +01002383def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002384 return Ref('more')
2385enddef
2386
2387def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002388 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002389 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002390enddef
2391
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002392def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002393 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002394 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002395enddef
2396
2397def Test_closure_ref_after_return()
2398 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002399 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002400 unlet g:Ref
2401enddef
2402
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002403def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002404 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002405 g:Extend = (s) => local->add(s)
2406 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002407enddef
2408
2409def Test_closure_two_refs()
2410 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002411 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002412 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002413 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002414 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002415 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002416
2417 unlet g:Extend
2418 unlet g:Read
2419enddef
2420
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002421def ReadRef(Ref: func(): list<string>): string
2422 return join(Ref(), ' ')
2423enddef
2424
Bram Moolenaar5e654232020-09-16 15:22:00 +02002425def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002426 Ref(add)
2427enddef
2428
2429def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002430 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002431 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002432 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002433 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002434 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002435 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002436
2437 unlet g:Extend
2438 unlet g:Read
2439enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002440
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002441def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002442 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002443 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002444enddef
2445
2446def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002447 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002448 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002449enddef
2450
2451def Test_closure_using_argument()
2452 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002453 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002454
2455 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002456 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002457
2458 unlet g:UseArg
2459 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002460
2461 var lines =<< trim END
2462 vim9script
2463 def Test(Fun: func(number): number): list<number>
2464 return map([1, 2, 3], (_, i) => Fun(i))
2465 enddef
2466 def Inc(nr: number): number
2467 return nr + 2
2468 enddef
2469 assert_equal([3, 4, 5], Test(Inc))
2470 END
2471 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002472enddef
2473
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002474def MakeGetAndAppendRefs()
2475 var local = 'a'
2476
2477 def Append(arg: string)
2478 local ..= arg
2479 enddef
2480 g:Append = Append
2481
2482 def Get(): string
2483 return local
2484 enddef
2485 g:Get = Get
2486enddef
2487
2488def Test_closure_append_get()
2489 MakeGetAndAppendRefs()
2490 g:Get()->assert_equal('a')
2491 g:Append('-b')
2492 g:Get()->assert_equal('a-b')
2493 g:Append('-c')
2494 g:Get()->assert_equal('a-b-c')
2495
2496 unlet g:Append
2497 unlet g:Get
2498enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002499
Bram Moolenaar04b12692020-05-04 23:24:44 +02002500def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002501 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002502 def Closure(arg: string): string
2503 return local .. arg
2504 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002505 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002506enddef
2507
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002508func GetResult(Ref)
2509 return a:Ref('some')
2510endfunc
2511
2512def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002513 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002514 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002515 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002516enddef
2517
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002518def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002519 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002520 vim9script
2521 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002522 var name = 0
2523 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002524 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002525 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002526 enddef
2527 Func()
2528 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002529 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002530enddef
2531
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002532def Test_nested_closure_used()
2533 var lines =<< trim END
2534 vim9script
2535 def Func()
2536 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002537 var Closure = () => x
2538 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002539 enddef
2540 Func()
2541 assert_equal('hello', g:Myclosure())
2542 END
2543 CheckScriptSuccess(lines)
2544enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002545
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002546def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002547 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002548 vim9script
2549 def FuncA()
2550 FuncB(0)
2551 enddef
2552 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002553 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002554 enddef
2555 FuncA()
2556 END
2557 CheckScriptFailure(lines, 'E1012:')
2558enddef
2559
Bram Moolenaarf112f302020-12-20 17:47:52 +01002560def Test_global_closure()
2561 var lines =<< trim END
2562 vim9script
2563 def ReverseEveryNLines(n: number, line1: number, line2: number)
2564 var mods = 'sil keepj keepp lockm '
2565 var range = ':' .. line1 .. ',' .. line2
2566 def g:Offset(): number
2567 var offset = (line('.') - line1 + 1) % n
2568 return offset != 0 ? offset : n
2569 enddef
2570 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2571 enddef
2572
2573 new
2574 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2575 ReverseEveryNLines(3, 1, 9)
2576 END
2577 CheckScriptSuccess(lines)
2578 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2579 assert_equal(expected, getline(1, 9))
2580 bwipe!
2581enddef
2582
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002583def Test_global_closure_called_directly()
2584 var lines =<< trim END
2585 vim9script
2586 def Outer()
2587 var x = 1
2588 def g:Inner()
2589 var y = x
2590 x += 1
2591 assert_equal(1, y)
2592 enddef
2593 g:Inner()
2594 assert_equal(2, x)
2595 enddef
2596 Outer()
2597 END
2598 CheckScriptSuccess(lines)
2599 delfunc g:Inner
2600enddef
2601
Bram Moolenaar69c76172021-12-02 16:38:52 +00002602def Test_closure_called_from_legacy()
2603 var lines =<< trim END
2604 vim9script
2605 def Func()
2606 var outer = 'foo'
2607 var F = () => {
2608 outer = 'bar'
2609 }
2610 execute printf('call %s()', string(F))
2611 enddef
2612 Func()
2613 END
2614 CheckScriptFailure(lines, 'E1248')
2615enddef
2616
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002617def Test_failure_in_called_function()
2618 # this was using the frame index as the return value
2619 var lines =<< trim END
2620 vim9script
2621 au TerminalWinOpen * eval [][0]
2622 def PopupTerm(a: any)
2623 # make sure typvals on stack are string
2624 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2625 FireEvent()
2626 enddef
2627 def FireEvent()
2628 do TerminalWinOpen
2629 enddef
2630 # use try/catch to make eval fail
2631 try
2632 call PopupTerm(0)
2633 catch
2634 endtry
2635 au! TerminalWinOpen
2636 END
2637 CheckScriptSuccess(lines)
2638enddef
2639
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002640def Test_nested_lambda()
2641 var lines =<< trim END
2642 vim9script
2643 def Func()
2644 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002645 var Lambda1 = () => 7
2646 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002647 var res = Lambda2()
2648 assert_equal([7, 4], res)
2649 enddef
2650 Func()
2651 END
2652 CheckScriptSuccess(lines)
2653enddef
2654
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002655def Test_double_nested_lambda()
2656 var lines =<< trim END
2657 vim9script
2658 def F(head: string): func(string): func(string): string
2659 return (sep: string): func(string): string => ((tail: string): string => {
2660 return head .. sep .. tail
2661 })
2662 enddef
2663 assert_equal('hello-there', F('hello')('-')('there'))
2664 END
2665 CheckScriptSuccess(lines)
2666enddef
2667
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002668def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002669 var lines =<< trim END
2670 vim9script
2671 def F(text: string): func(string): func(string): string
2672 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002673 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002674 })
2675 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002676 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002677 END
2678 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002679
2680 lines =<< trim END
2681 vim9script
2682 echo range(4)->mapnew((_, v) => {
2683 return range(v) ->mapnew((_, s) => {
2684 return string(s)
2685 })
2686 })
2687 END
2688 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002689
2690 lines =<< trim END
2691 vim9script
2692
2693 def s:func()
2694 range(10)
2695 ->mapnew((_, _) => ({
2696 key: range(10)->mapnew((_, _) => {
2697 return ' '
2698 }),
2699 }))
2700 enddef
2701
2702 defcomp
2703 END
2704 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002705enddef
2706
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002707def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002708 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002709 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002710enddef
2711
2712def Test_lambda_arg_shadows_func()
2713 assert_equal([42], Shadowed())
2714enddef
2715
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002716def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002717 var path: string = empty(dir)
2718 \ ? 'empty'
2719 \ : 'full'
2720 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002721enddef
2722
2723def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002724 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002725enddef
2726
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002727def Test_script_var_in_lambda()
2728 var lines =<< trim END
2729 vim9script
2730 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002731 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002732 END
2733 CheckScriptSuccess(lines)
2734enddef
2735
Bram Moolenaar5e654232020-09-16 15:22:00 +02002736def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002737 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002738 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002739 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002740 ->reverse()
2741 return x
2742enddef
2743
2744def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002745 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002746
2747 var lines =<< trim END
2748 vim9script
2749 var res = [{n: 1, m: 2, s: 'xxx'}]
2750 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2751 v.n,
2752 v.m,
2753 substitute(v.s, '.*', 'yyy', '')
2754 ))
2755 assert_equal(['1:2:yyy'], res)
2756 END
2757 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002758enddef
2759
Bram Moolenaarb6571982021-01-08 22:24:19 +01002760def Test_list_lambda()
2761 timer_start(1000, (_) => 0)
2762 var body = execute(timer_info()[0].callback
2763 ->string()
2764 ->substitute("('", ' ', '')
2765 ->substitute("')", '', '')
2766 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002767 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002768enddef
2769
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002770def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002771 var lines =<< trim END
2772 vim9script
2773 var flist: list<func>
2774 for i in range(10)
2775 var inloop = i
2776 flist[i] = () => inloop
2777 endfor
2778 END
2779 CheckScriptSuccess(lines)
2780
2781 lines =<< trim END
2782 vim9script
2783 if true
2784 var outloop = 5
2785 var flist: list<func>
2786 for i in range(10)
2787 flist[i] = () => outloop
2788 endfor
2789 endif
2790 END
2791 CheckScriptSuccess(lines)
2792
2793 lines =<< trim END
2794 vim9script
2795 if true
2796 var outloop = 5
2797 endif
2798 var flist: list<func>
2799 for i in range(10)
2800 flist[i] = () => outloop
2801 endfor
2802 END
2803 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002804
2805 lines =<< trim END
2806 vim9script
2807 for i in range(10)
2808 var Ref = () => 0
2809 endfor
2810 assert_equal(0, ((i) => 0)(0))
2811 END
2812 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002813enddef
2814
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002815def Test_legacy_lambda()
2816 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002817
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002818 var lines =<< trim END
2819 echo {x -> 'hello ' .. x}('foo')
2820 END
2821 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002822
2823 lines =<< trim END
2824 vim9script
2825 def Func()
2826 echo (() => 'no error')()
2827 enddef
2828 legacy call s:Func()
2829 END
2830 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002831enddef
2832
Bram Moolenaarce024c32021-06-26 13:00:49 +02002833def Test_legacy()
2834 var lines =<< trim END
2835 vim9script
2836 func g:LegacyFunction()
2837 let g:legacyvar = 1
2838 endfunc
2839 def Testit()
2840 legacy call g:LegacyFunction()
2841 enddef
2842 Testit()
2843 assert_equal(1, g:legacyvar)
2844 unlet g:legacyvar
2845 delfunc g:LegacyFunction
2846 END
2847 CheckScriptSuccess(lines)
2848enddef
2849
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002850def Test_legacy_errors()
2851 for cmd in ['if', 'elseif', 'else', 'endif',
2852 'for', 'endfor', 'continue', 'break',
2853 'while', 'endwhile',
2854 'try', 'catch', 'finally', 'endtry']
2855 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2856 endfor
2857enddef
2858
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002859def Test_call_legacy_with_dict()
2860 var lines =<< trim END
2861 vim9script
2862 func Legacy() dict
2863 let g:result = self.value
2864 endfunc
2865 def TestDirect()
2866 var d = {value: 'yes', func: Legacy}
2867 d.func()
2868 enddef
2869 TestDirect()
2870 assert_equal('yes', g:result)
2871 unlet g:result
2872
2873 def TestIndirect()
2874 var d = {value: 'foo', func: Legacy}
2875 var Fi = d.func
2876 Fi()
2877 enddef
2878 TestIndirect()
2879 assert_equal('foo', g:result)
2880 unlet g:result
2881
2882 var d = {value: 'bar', func: Legacy}
2883 d.func()
2884 assert_equal('bar', g:result)
2885 unlet g:result
2886 END
2887 CheckScriptSuccess(lines)
2888enddef
2889
Bram Moolenaarab360522021-01-10 14:02:28 +01002890def DoFilterThis(a: string): list<string>
2891 # closure nested inside another closure using argument
2892 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2893 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2894enddef
2895
2896def Test_nested_closure_using_argument()
2897 assert_equal(['x', 'x2'], DoFilterThis('x'))
2898enddef
2899
Bram Moolenaar0186e582021-01-10 18:33:11 +01002900def Test_triple_nested_closure()
2901 var what = 'x'
2902 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2903 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2904 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2905enddef
2906
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002907func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002908 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002909 call Run_Test_silent_echo()
2910endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002911
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002912def Run_Test_silent_echo()
2913 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002914 vim9script
2915 def EchoNothing()
2916 silent echo ''
2917 enddef
2918 defcompile
2919 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002920 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002921
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002922 # Check that the balloon shows up after a mouse move
2923 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2924 term_sendkeys(buf, ":abc")
2925 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002926
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002927 # clean up
2928 StopVimInTerminal(buf)
2929 delete('XTest_silent_echo')
2930enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002931
Bram Moolenaar171fb922020-10-28 16:54:47 +01002932def SilentlyError()
2933 execute('silent! invalid')
2934 g:did_it = 'yes'
2935enddef
2936
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002937func UserError()
2938 silent! invalid
2939endfunc
2940
2941def SilentlyUserError()
2942 UserError()
2943 g:did_it = 'yes'
2944enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002945
2946" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002947func Test_ignore_silent_error()
2948 let g:did_it = 'no'
2949 call SilentlyError()
2950 call assert_equal('yes', g:did_it)
2951
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002952 let g:did_it = 'no'
2953 call SilentlyUserError()
2954 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002955
2956 unlet g:did_it
2957endfunc
2958
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002959def Test_ignore_silent_error_in_filter()
2960 var lines =<< trim END
2961 vim9script
2962 def Filter(winid: number, key: string): bool
2963 if key == 'o'
2964 silent! eval [][0]
2965 return true
2966 endif
2967 return popup_filter_menu(winid, key)
2968 enddef
2969
Bram Moolenaare0de1712020-12-02 17:36:54 +01002970 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002971 feedkeys("o\r", 'xnt')
2972 END
2973 CheckScriptSuccess(lines)
2974enddef
2975
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002976def Fibonacci(n: number): number
2977 if n < 2
2978 return n
2979 else
2980 return Fibonacci(n - 1) + Fibonacci(n - 2)
2981 endif
2982enddef
2983
Bram Moolenaar985116a2020-07-12 17:31:09 +02002984def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002985 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002986enddef
2987
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002988def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002989 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002990 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002991 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002992 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002993 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002994enddef
2995
2996def Test_closure_in_map()
2997 mkdir('XclosureDir/tdir', 'p')
2998 writefile(['111'], 'XclosureDir/file1')
2999 writefile(['222'], 'XclosureDir/file2')
3000 writefile(['333'], 'XclosureDir/tdir/file3')
3001
Bram Moolenaare0de1712020-12-02 17:36:54 +01003002 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003003
3004 delete('XclosureDir', 'rf')
3005enddef
3006
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003007def Test_invalid_function_name()
3008 var lines =<< trim END
3009 vim9script
3010 def s: list<string>
3011 END
3012 CheckScriptFailure(lines, 'E129:')
3013
3014 lines =<< trim END
3015 vim9script
3016 def g: list<string>
3017 END
3018 CheckScriptFailure(lines, 'E129:')
3019
3020 lines =<< trim END
3021 vim9script
3022 def <SID>: list<string>
3023 END
3024 CheckScriptFailure(lines, 'E884:')
3025
3026 lines =<< trim END
3027 vim9script
3028 def F list<string>
3029 END
3030 CheckScriptFailure(lines, 'E488:')
3031enddef
3032
Bram Moolenaara90afb92020-07-15 22:38:56 +02003033def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003034 var lines =<< trim END
3035 var Xsetlist: func
3036 Xsetlist = function('setloclist', [0])
3037 Xsetlist([], ' ', {title: 'test'})
3038 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003039
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003040 Xsetlist = function('setloclist', [0, [], ' '])
3041 Xsetlist({title: 'test'})
3042 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003043
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003044 Xsetlist = function('setqflist')
3045 Xsetlist([], ' ', {title: 'test'})
3046 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003047
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003048 Xsetlist = function('setqflist', [[], ' '])
3049 Xsetlist({title: 'test'})
3050 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02003051
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003052 var Len: func: number = function('len', ['word'])
3053 assert_equal(4, Len())
3054
3055 var RepeatFunc = function('repeat', ['o'])
3056 assert_equal('ooooo', RepeatFunc(5))
3057 END
3058 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003059
3060 lines =<< trim END
3061 vim9script
3062 def Foo(Parser: any)
3063 enddef
3064 var Expr: func(dict<any>): dict<any>
3065 const Call = Foo(Expr)
3066 END
3067 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003068enddef
3069
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003070def Test_cmd_modifier()
3071 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003072 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003073enddef
3074
3075def Test_restore_modifiers()
3076 # check that when compiling a :def function command modifiers are not messed
3077 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003078 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003079 vim9script
3080 set eventignore=
3081 autocmd QuickFixCmdPost * copen
3082 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003083 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003084 enddef
3085 func Func()
3086 noautocmd call s:AutocmdsDisabled()
3087 let g:ei_after = &eventignore
3088 endfunc
3089 Func()
3090 END
3091 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003092 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003093enddef
3094
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003095def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003096 eval 1 + 2
3097 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003098 # call not on fourth line
3099 StackBot()
3100enddef
3101
3102def StackBot()
3103 # throw an error
3104 eval [][0]
3105enddef
3106
3107def Test_callstack_def()
3108 try
3109 StackTop()
3110 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003111 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003112 endtry
3113enddef
3114
Bram Moolenaare8211a32020-10-09 22:04:29 +02003115" Re-using spot for variable used in block
3116def Test_block_scoped_var()
3117 var lines =<< trim END
3118 vim9script
3119 def Func()
3120 var x = ['a', 'b', 'c']
3121 if 1
3122 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003123 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003124 endif
3125 var z = x
3126 assert_equal(['x', 'x', 'x'], z)
3127 enddef
3128 Func()
3129 END
3130 CheckScriptSuccess(lines)
3131enddef
3132
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003133def Test_reset_did_emsg()
3134 var lines =<< trim END
3135 @s = 'blah'
3136 au BufWinLeave * #
3137 def Func()
3138 var winid = popup_create('popup', {})
3139 exe '*s'
3140 popup_close(winid)
3141 enddef
3142 Func()
3143 END
3144 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003145 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003146enddef
3147
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003148def Test_did_emsg_reset()
3149 # executing an autocommand resets did_emsg, this should not result in a
3150 # builtin function considered failing
3151 var lines =<< trim END
3152 vim9script
3153 au BufWinLeave * #
3154 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003155 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003156 eval [][0]
3157 enddef
3158 nno <F3> <cmd>call <sid>Func()<cr>
3159 feedkeys("\<F3>\e", 'xt')
3160 END
3161 writefile(lines, 'XemsgReset')
3162 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3163 delete('XemsgReset')
3164 nunmap <F3>
3165 au! BufWinLeave
3166enddef
3167
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003168def Test_abort_with_silent_call()
3169 var lines =<< trim END
3170 vim9script
3171 g:result = 'none'
3172 def Func()
3173 g:result += 3
3174 g:result = 'yes'
3175 enddef
3176 # error is silenced, but function aborts on error
3177 silent! Func()
3178 assert_equal('none', g:result)
3179 unlet g:result
3180 END
3181 CheckScriptSuccess(lines)
3182enddef
3183
Bram Moolenaarf665e972020-12-05 19:17:16 +01003184def Test_continues_with_silent_error()
3185 var lines =<< trim END
3186 vim9script
3187 g:result = 'none'
3188 def Func()
3189 silent! g:result += 3
3190 g:result = 'yes'
3191 enddef
3192 # error is silenced, function does not abort
3193 Func()
3194 assert_equal('yes', g:result)
3195 unlet g:result
3196 END
3197 CheckScriptSuccess(lines)
3198enddef
3199
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003200def Test_abort_even_with_silent()
3201 var lines =<< trim END
3202 vim9script
3203 g:result = 'none'
3204 def Func()
3205 eval {-> ''}() .. '' .. {}['X']
3206 g:result = 'yes'
3207 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003208 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003209 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003210 unlet g:result
3211 END
3212 CheckScriptSuccess(lines)
3213enddef
3214
Bram Moolenaarf665e972020-12-05 19:17:16 +01003215def Test_cmdmod_silent_restored()
3216 var lines =<< trim END
3217 vim9script
3218 def Func()
3219 g:result = 'none'
3220 silent! g:result += 3
3221 g:result = 'none'
3222 g:result += 3
3223 enddef
3224 Func()
3225 END
3226 # can't use CheckScriptFailure, it ignores the :silent!
3227 var fname = 'Xdefsilent'
3228 writefile(lines, fname)
3229 var caught = 'no'
3230 try
3231 exe 'source ' .. fname
3232 catch /E1030:/
3233 caught = 'yes'
3234 assert_match('Func, line 4', v:throwpoint)
3235 endtry
3236 assert_equal('yes', caught)
3237 delete(fname)
3238enddef
3239
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003240def Test_cmdmod_silent_nested()
3241 var lines =<< trim END
3242 vim9script
3243 var result = ''
3244
3245 def Error()
3246 result ..= 'Eb'
3247 eval [][0]
3248 result ..= 'Ea'
3249 enddef
3250
3251 def Crash()
3252 result ..= 'Cb'
3253 sil! Error()
3254 result ..= 'Ca'
3255 enddef
3256
3257 Crash()
3258 assert_equal('CbEbEaCa', result)
3259 END
3260 CheckScriptSuccess(lines)
3261enddef
3262
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003263def Test_dict_member_with_silent()
3264 var lines =<< trim END
3265 vim9script
3266 g:result = 'none'
3267 var d: dict<any>
3268 def Func()
3269 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003270 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003271 catch
3272 endtry
3273 enddef
3274 silent! Func()
3275 assert_equal('0', g:result)
3276 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003277 END
3278 CheckScriptSuccess(lines)
3279enddef
3280
Bram Moolenaarf9041332021-01-21 19:41:16 +01003281def Test_skip_cmds_with_silent()
3282 var lines =<< trim END
3283 vim9script
3284
3285 def Func(b: bool)
3286 Crash()
3287 enddef
3288
3289 def Crash()
3290 sil! :/not found/d _
3291 sil! :/not found/put _
3292 enddef
3293
3294 Func(true)
3295 END
3296 CheckScriptSuccess(lines)
3297enddef
3298
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003299def Test_opfunc()
3300 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3301 def g:Opfunc(_: any): string
3302 setline(1, 'ASDF')
3303 return ''
3304 enddef
3305 new
3306 setline(1, 'asdf')
3307 feedkeys("\<F3>$", 'x')
3308 assert_equal('ASDF', getline(1))
3309
3310 bwipe!
3311 nunmap <F3>
3312enddef
3313
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003314func Test_opfunc_error()
3315 CheckScreendump
3316 call Run_Test_opfunc_error()
3317endfunc
3318
3319def Run_Test_opfunc_error()
3320 # test that the error from Opfunc() is displayed right away
3321 var lines =<< trim END
3322 vim9script
3323
3324 def Opfunc(type: string)
3325 try
3326 eval [][0]
3327 catch /nothing/ # error not caught
3328 endtry
3329 enddef
3330 &operatorfunc = Opfunc
3331 nnoremap <expr> l <SID>L()
3332 def L(): string
3333 return 'l'
3334 enddef
3335 'x'->repeat(10)->setline(1)
3336 feedkeys('g@l', 'n')
3337 feedkeys('llll')
3338 END
3339 call writefile(lines, 'XTest_opfunc_error')
3340
3341 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003342 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003343 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003344
3345 # clean up
3346 StopVimInTerminal(buf)
3347 delete('XTest_opfunc_error')
3348enddef
3349
Bram Moolenaar077a4232020-12-22 18:33:27 +01003350" this was crashing on exit
3351def Test_nested_lambda_in_closure()
3352 var lines =<< trim END
3353 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003354 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003355 def Outer()
3356 def g:Inner()
3357 echo map([1, 2, 3], {_, v -> v + 1})
3358 enddef
3359 g:Inner()
3360 enddef
3361 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003362 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003363 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003364 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003365 return
3366 endif
3367 assert_equal(['Done'], readfile('XnestedDone'))
3368 delete('XnestedDone')
3369enddef
3370
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003371def Test_check_func_arg_types()
3372 var lines =<< trim END
3373 vim9script
3374 def F1(x: string): string
3375 return x
3376 enddef
3377
3378 def F2(x: number): number
3379 return x + 1
3380 enddef
3381
3382 def G(g: func): dict<func>
3383 return {f: g}
3384 enddef
3385
3386 def H(d: dict<func>): string
3387 return d.f('a')
3388 enddef
3389 END
3390
3391 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3392 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3393enddef
3394
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003395def Test_list_any_type_checked()
3396 var lines =<< trim END
3397 vim9script
3398 def Foo()
3399 --decl--
3400 Bar(l)
3401 enddef
3402 def Bar(ll: list<dict<any>>)
3403 enddef
3404 Foo()
3405 END
3406 lines[2] = 'var l: list<any>'
3407 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3408
3409 lines[2] = 'var l: list<any> = []'
3410 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3411
3412 lines[2] = 'var l: list<any> = [11]'
3413 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3414enddef
3415
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003416def Test_compile_error()
3417 var lines =<< trim END
3418 def g:Broken()
3419 echo 'a' + {}
3420 enddef
3421 call g:Broken()
3422 END
3423 # First call: compilation error
3424 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3425
3426 # Second call won't try compiling again
3427 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003428 delfunc g:Broken
3429
3430 # No error when compiling with :silent!
3431 lines =<< trim END
3432 def g:Broken()
3433 echo 'a' + []
3434 enddef
3435 silent! defcompile
3436 END
3437 CheckScriptSuccess(lines)
3438
3439 # Calling the function won't try compiling again
3440 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3441 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003442enddef
3443
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003444def Test_ignored_argument()
3445 var lines =<< trim END
3446 vim9script
3447 def Ignore(_, _): string
3448 return 'yes'
3449 enddef
3450 assert_equal('yes', Ignore(1, 2))
3451
3452 func Ok(_)
3453 return a:_
3454 endfunc
3455 assert_equal('ok', Ok('ok'))
3456
3457 func Oktoo()
3458 let _ = 'too'
3459 return _
3460 endfunc
3461 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003462
3463 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003464 END
3465 CheckScriptSuccess(lines)
3466
3467 lines =<< trim END
3468 def Ignore(_: string): string
3469 return _
3470 enddef
3471 defcompile
3472 END
3473 CheckScriptFailure(lines, 'E1181:', 1)
3474
3475 lines =<< trim END
3476 var _ = 1
3477 END
3478 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003479
3480 lines =<< trim END
3481 var x = _
3482 END
3483 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003484enddef
3485
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003486def Test_too_many_arguments()
3487 var lines =<< trim END
3488 echo [0, 1, 2]->map(() => 123)
3489 END
3490 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3491
3492 lines =<< trim END
3493 echo [0, 1, 2]->map((_) => 123)
3494 END
3495 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3496enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003497
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003498def Test_closing_brace_at_start_of_line()
3499 var lines =<< trim END
3500 def Func()
3501 enddef
3502 Func(
3503 )
3504 END
3505 call CheckDefAndScriptSuccess(lines)
3506enddef
3507
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003508func CreateMydict()
3509 let g:mydict = {}
3510 func g:mydict.afunc()
3511 let g:result = self.key
3512 endfunc
3513endfunc
3514
3515def Test_numbered_function_reference()
3516 CreateMydict()
3517 var output = execute('legacy func g:mydict.afunc')
3518 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3519 execute 'function(' .. funcName .. ', [], {key: 42})()'
3520 # check that the function still exists
3521 assert_equal(output, execute('legacy func g:mydict.afunc'))
3522 unlet g:mydict
3523enddef
3524
Bram Moolenaar20677332021-06-06 17:02:53 +02003525if has('python3')
3526 def Test_python3_heredoc()
3527 py3 << trim EOF
3528 import vim
3529 vim.vars['didit'] = 'yes'
3530 EOF
3531 assert_equal('yes', g:didit)
3532
3533 python3 << trim EOF
3534 import vim
3535 vim.vars['didit'] = 'again'
3536 EOF
3537 assert_equal('again', g:didit)
3538 enddef
3539endif
3540
3541" This messes up syntax highlight, keep near the end.
3542if has('lua')
3543 def Test_lua_heredoc()
3544 g:d = {}
3545 lua << trim EOF
3546 x = vim.eval('g:d')
3547 x['key'] = 'val'
3548 EOF
3549 assert_equal('val', g:d.key)
3550 enddef
3551endif
3552
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003553
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003554" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker