blob: 81fa7e49c7b94916c71e1deb8c2bfdb8e7002772 [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
551 # using script variable requires matching type or type cast
552 lines =<< trim END
553 vim9script
554 var a: any
555 def Func(arg: string = a)
556 echo arg
557 enddef
558 defcompile
559 END
560 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got any')
561
562 lines =<< trim END
563 vim9script
564 var a: any
565 def Func(arg: string = <string>a)
566 echo arg
567 enddef
568 a = 'works'
569 Func()
570 END
571 CheckScriptSuccess(lines)
572
573 # using global variable does not require type cast
574 lines =<< trim END
575 vim9script
576 def Func(arg: string = g:str)
577 echo arg
578 enddef
579 g:str = 'works'
580 Func()
581 END
582 CheckScriptSuccess(lines)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200583enddef
584
Bram Moolenaarcef12702021-01-04 14:09:43 +0100585def FuncWithComment( # comment
586 a: number, #comment
587 b: bool, # comment
588 c: string) #comment
589 assert_equal(4, a)
590 assert_equal(true, b)
591 assert_equal('yes', c)
592enddef
593
594def Test_func_with_comments()
595 FuncWithComment(4, true, 'yes')
596
597 var lines =<< trim END
598 def Func(# comment
599 arg: string)
600 enddef
601 END
602 CheckScriptFailure(lines, 'E125:', 1)
603
604 lines =<< trim END
605 def Func(
606 arg: string# comment
607 )
608 enddef
609 END
610 CheckScriptFailure(lines, 'E475:', 2)
611
612 lines =<< trim END
613 def Func(
614 arg: string
615 )# comment
616 enddef
617 END
618 CheckScriptFailure(lines, 'E488:', 3)
619enddef
620
Bram Moolenaar04b12692020-05-04 23:24:44 +0200621def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000622 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200623 return 'nested ' .. arg
624 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000625 NestedDef(':def')->assert_equal('nested :def')
626
627 func NestedFunc(arg)
628 return 'nested ' .. a:arg
629 endfunc
630 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200631
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200632 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
633 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
634
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200635 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
636 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200637
Bram Moolenaar54021752020-12-06 18:50:36 +0100638 var lines =<< trim END
639 def Outer()
640 def Inner()
641 # comment
642 enddef
643 def Inner()
644 enddef
645 enddef
646 END
647 CheckDefFailure(lines, 'E1073:')
648
649 lines =<< trim END
650 def Outer()
651 def Inner()
652 # comment
653 enddef
654 def! Inner()
655 enddef
656 enddef
657 END
658 CheckDefFailure(lines, 'E1117:')
659
660 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100661 lines =<< trim END
662 vim9script
663 var thecount = 0
664 if true
665 def Test(): number
666 def TheFunc(): number
667 thecount += 1
668 return thecount
669 enddef
670 return TheFunc()
671 enddef
672 endif
673 defcompile
674 assert_equal(1, Test())
675 assert_equal(2, Test())
676 END
677 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100678
679 # also works when "thecount" is inside the "if" block
680 lines =<< trim END
681 vim9script
682 if true
683 var thecount = 0
684 def Test(): number
685 def TheFunc(): number
686 thecount += 1
687 return thecount
688 enddef
689 return TheFunc()
690 enddef
691 endif
692 defcompile
693 assert_equal(1, Test())
694 assert_equal(2, Test())
695 END
696 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200697
698 lines =<< trim END
699 vim9script
700 def Outer()
701 def Inner()
702 echo 'hello'
703 enddef burp
704 enddef
705 defcompile
706 END
707 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200708enddef
709
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100710def Test_not_nested_function()
711 echo printf('%d',
712 function('len')('xxx'))
713enddef
714
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200715func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200716 call MyDefaultArgs()->assert_equal('string')
717 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200718 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200719endfunc
720
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200721def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200722 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200723 vim9script
724 def Outer()
725 def g:Inner(): string
726 return 'inner'
727 enddef
728 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200729 defcompile
730 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200731 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200732 delfunc g:Inner
733 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200734 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200735 delfunc g:Inner
736 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200737 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200738 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200739 END
740 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200741
742 lines =<< trim END
743 vim9script
744 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000745 func g:Inner()
746 return 'inner'
747 endfunc
748 enddef
749 defcompile
750 Outer()
751 g:Inner()->assert_equal('inner')
752 delfunc g:Inner
753 Outer()
754 g:Inner()->assert_equal('inner')
755 delfunc g:Inner
756 Outer()
757 g:Inner()->assert_equal('inner')
758 delfunc g:Inner
759 END
760 CheckScriptSuccess(lines)
761
762 lines =<< trim END
763 vim9script
764 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200765 def g:Inner(): string
766 return 'inner'
767 enddef
768 enddef
769 defcompile
770 Outer()
771 Outer()
772 END
773 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100774 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200775
776 lines =<< trim END
777 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100778 def Outer()
779 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100780 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100781 enddef
782 g:Inner()
783 enddef
784 Outer()
785 END
786 CheckScriptSuccess(lines)
787 delfunc g:Inner
788
789 lines =<< trim END
790 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200791 def Func()
792 echo 'script'
793 enddef
794 def Outer()
795 def Func()
796 echo 'inner'
797 enddef
798 enddef
799 defcompile
800 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000801 CheckScriptFailure(lines, "E1073:", 1)
802
803 lines =<< trim END
804 vim9script
805 def Func()
806 echo 'script'
807 enddef
808 def Func()
809 echo 'script'
810 enddef
811 END
812 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200813enddef
814
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100815def DefListAll()
816 def
817enddef
818
819def DefListOne()
820 def DefListOne
821enddef
822
823def DefListMatches()
824 def /DefList
825enddef
826
827def Test_nested_def_list()
828 var funcs = split(execute('call DefListAll()'), "\n")
829 assert_true(len(funcs) > 10)
830 assert_true(funcs->index('def DefListAll()') >= 0)
831
832 funcs = split(execute('call DefListOne()'), "\n")
833 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
834
835 funcs = split(execute('call DefListMatches()'), "\n")
836 assert_true(len(funcs) >= 3)
837 assert_true(funcs->index('def DefListAll()') >= 0)
838 assert_true(funcs->index('def DefListOne()') >= 0)
839 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100840
841 var lines =<< trim END
842 vim9script
843 def Func()
844 def +Func+
845 enddef
846 defcompile
847 END
848 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100849enddef
850
Bram Moolenaar333894b2020-08-01 18:53:07 +0200851def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200852 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200853 vim9script
854 def g:Func(): string
855 return 'global'
856 enddef
857 def Func(): string
858 return 'local'
859 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200860 g:Func()->assert_equal('global')
861 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100862 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200863 END
864 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200865
866 lines =<< trim END
867 vim9script
868 def g:Funcy()
869 echo 'funcy'
870 enddef
871 s:Funcy()
872 END
873 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200874enddef
875
Bram Moolenaar0f769812020-09-12 18:32:34 +0200876def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200877 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200878 vim9script
879 def g:Gfunc(): string
880 return 'global'
881 enddef
882 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200883 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200884 return Gfunc('testing')
885 enddef
886 g:Gfunc()->assert_equal('global')
887 AnotherFunc()->assert_equal(7)
888 delfunc g:Gfunc
889 END
890 CheckScriptSuccess(lines)
891
892 lines =<< trim END
893 vim9script
894 def g:Func(): string
895 return 'global'
896 enddef
897 def AnotherFunc()
898 g:Func = function('len')
899 enddef
900 AnotherFunc()
901 END
902 CheckScriptFailure(lines, 'E705:')
903 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200904
905 # global function is found without g: prefix
906 lines =<< trim END
907 vim9script
908 def g:Func(): string
909 return 'global'
910 enddef
911 def AnotherFunc(): string
912 return Func()
913 enddef
914 assert_equal('global', AnotherFunc())
915 delfunc g:Func
916 END
917 CheckScriptSuccess(lines)
918
919 lines =<< trim END
920 vim9script
921 def g:Func(): string
922 return 'global'
923 enddef
924 assert_equal('global', Func())
925 delfunc g:Func
926 END
927 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200928enddef
929
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200930func TakesOneArg(arg)
931 echo a:arg
932endfunc
933
934def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200935 CheckDefFailure(['TakesOneArg()'], 'E119:')
936 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
937 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
938 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200939
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200940 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200941 vim9script
942 def Func(s: string)
943 echo s
944 enddef
945 Func([])
946 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200947 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200948
949 lines =<< trim END
950 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100951 var name = 'piet'
952 def FuncOne(name: string)
953 echo nr
954 enddef
955 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100956 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100957
958 lines =<< trim END
959 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200960 def FuncOne(nr: number)
961 echo nr
962 enddef
963 def FuncTwo()
964 FuncOne()
965 enddef
966 defcompile
967 END
968 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200969 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200970 try
971 source Xscript
972 catch
973 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
974 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
975 didCatch = true
976 endtry
977 assert_true(didCatch)
978
979 lines =<< trim END
980 vim9script
981 def FuncOne(nr: number)
982 echo nr
983 enddef
984 def FuncTwo()
985 FuncOne(1, 2)
986 enddef
987 defcompile
988 END
989 writefile(lines, 'Xscript')
990 didCatch = false
991 try
992 source Xscript
993 catch
994 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
995 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
996 didCatch = true
997 endtry
998 assert_true(didCatch)
999
1000 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001001enddef
1002
Bram Moolenaar50824712020-12-20 21:10:17 +01001003def Test_call_funcref_wrong_args()
1004 var head =<< trim END
1005 vim9script
1006 def Func3(a1: string, a2: number, a3: list<number>)
1007 echo a1 .. a2 .. a3[0]
1008 enddef
1009 def Testme()
1010 var funcMap: dict<func> = {func: Func3}
1011 END
1012 var tail =<< trim END
1013 enddef
1014 Testme()
1015 END
1016 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
1017
1018 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
1019 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001020
1021 var lines =<< trim END
1022 vim9script
1023 var Ref: func(number): any
1024 Ref = (j) => !j
1025 echo Ref(false)
1026 END
1027 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
1028
1029 lines =<< trim END
1030 vim9script
1031 var Ref: func(number): any
1032 Ref = (j) => !j
1033 call Ref(false)
1034 END
1035 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001036enddef
1037
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001038def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001039 var lines =<< trim END
1040 var Callback = (..._) => 'anything'
1041 assert_equal('anything', Callback())
1042 assert_equal('anything', Callback(1))
1043 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001044
1045 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001046 END
1047 CheckDefAndScriptSuccess(lines)
1048
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001049 CheckDefFailure(['echo ((i) => 0)()'],
1050 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001051
Bram Moolenaar2a389082021-04-09 20:24:31 +02001052 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001053 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001054 echo Ref(1, 'x')
1055 END
1056 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001057
1058 lines =<< trim END
1059 var Ref: func(job, string, number)
1060 Ref = (x, y) => 0
1061 END
1062 CheckDefAndScriptFailure(lines, 'E1012:')
1063
1064 lines =<< trim END
1065 var Ref: func(job, string)
1066 Ref = (x, y, z) => 0
1067 END
1068 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001069
1070 lines =<< trim END
1071 var one = 1
1072 var l = [1, 2, 3]
1073 echo map(l, (one) => one)
1074 END
1075 CheckDefFailure(lines, 'E1167:')
1076 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1077
1078 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001079 var Ref: func(any, ?any): bool
1080 Ref = (_, y = 1) => false
1081 END
1082 CheckDefAndScriptFailure(lines, 'E1172:')
1083
1084 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001085 var a = 0
1086 var b = (a == 0 ? 1 : 2)
1087 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001088 var txt = 'a'
1089 b = (txt =~ 'x' ? 1 : 2)
1090 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001091 END
1092 CheckDefAndScriptSuccess(lines)
1093
1094 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001095 def ShadowLocal()
1096 var one = 1
1097 var l = [1, 2, 3]
1098 echo map(l, (one) => one)
1099 enddef
1100 END
1101 CheckDefFailure(lines, 'E1167:')
1102
1103 lines =<< trim END
1104 def Shadowarg(one: number)
1105 var l = [1, 2, 3]
1106 echo map(l, (one) => one)
1107 enddef
1108 END
1109 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001110
1111 lines =<< trim END
1112 echo ((a) => a)('aa', 'bb')
1113 END
1114 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001115
1116 lines =<< trim END
1117 echo 'aa'->((a) => a)('bb')
1118 END
1119 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1120 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001121enddef
1122
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001123def Test_lambda_line_nr()
1124 var lines =<< trim END
1125 vim9script
1126 # comment
1127 # comment
1128 var id = timer_start(1'000, (_) => 0)
1129 var out = execute('verbose ' .. timer_info(id)[0].callback
1130 ->string()
1131 ->substitute("('\\|')", ' ', 'g'))
1132 assert_match('Last set from .* line 4', out)
1133 END
1134 CheckScriptSuccess(lines)
1135enddef
1136
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001137def FilterWithCond(x: string, Cond: func(string): bool): bool
1138 return Cond(x)
1139enddef
1140
Bram Moolenaar0346b792021-01-31 22:18:29 +01001141def Test_lambda_return_type()
1142 var lines =<< trim END
1143 var Ref = (): => 123
1144 END
1145 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001146
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001147 # no space before the return type
1148 lines =<< trim END
1149 var Ref = (x):number => x + 1
1150 END
1151 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1152
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001153 # this works
1154 for x in ['foo', 'boo']
1155 echo FilterWithCond(x, (v) => v =~ '^b')
1156 endfor
1157
1158 # this fails
1159 lines =<< trim END
1160 echo FilterWithCond('foo', (v) => v .. '^b')
1161 END
1162 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001163
1164 lines =<< trim END
1165 var Lambda1 = (x) => {
1166 return x
1167 }
1168 assert_equal('asdf', Lambda1('asdf'))
1169 var Lambda2 = (x): string => {
1170 return x
1171 }
1172 assert_equal('foo', Lambda2('foo'))
1173 END
1174 CheckDefAndScriptSuccess(lines)
1175
1176 lines =<< trim END
1177 var Lambda = (x): string => {
1178 return x
1179 }
1180 echo Lambda(['foo'])
1181 END
1182 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001183enddef
1184
Bram Moolenaar709664c2020-12-12 14:33:41 +01001185def Test_lambda_uses_assigned_var()
1186 CheckDefSuccess([
1187 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001188 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001189enddef
1190
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001191def Test_pass_legacy_lambda_to_def_func()
1192 var lines =<< trim END
1193 vim9script
1194 func Foo()
1195 eval s:Bar({x -> 0})
1196 endfunc
1197 def Bar(y: any)
1198 enddef
1199 Foo()
1200 END
1201 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001202
1203 lines =<< trim END
1204 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001205 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001206 enddef
1207 legacy call g:TestFunc({-> 0})
1208 delfunc g:TestFunc
1209
1210 def g:TestFunc(f: func(number))
1211 enddef
1212 legacy call g:TestFunc({nr -> 0})
1213 delfunc g:TestFunc
1214 END
1215 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001216enddef
1217
Bram Moolenaar844fb642021-10-23 13:32:30 +01001218def Test_lambda_in_reduce_line_break()
1219 # this was using freed memory
1220 var lines =<< trim END
1221 vim9script
1222 const result: dict<number> =
1223 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1224 ->reduce((acc, val) => {
1225 if has_key(acc, val)
1226 acc[val] += 1
1227 return acc
1228 else
1229 acc[val] = 1
1230 return acc
1231 endif
1232 }, {})
1233 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1234 END
1235 CheckScriptSuccess(lines)
1236enddef
1237
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001238def Test_set_opfunc_to_lambda()
1239 var lines =<< trim END
1240 vim9script
1241 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1242 def CountSpaces(type = ''): string
1243 if type == ''
1244 &operatorfunc = (t) => CountSpaces(t)
1245 return 'g@'
1246 endif
1247 normal! '[V']y
1248 g:result = getreg('"')->count(' ')
1249 return ''
1250 enddef
1251 new
1252 'a b c d e'->setline(1)
1253 feedkeys("\<F4>", 'x')
1254 assert_equal(4, g:result)
1255 bwipe!
1256 END
1257 CheckScriptSuccess(lines)
1258enddef
1259
Bram Moolenaaref082e12021-12-12 21:02:03 +00001260def Test_set_opfunc_to_global_function()
1261 var lines =<< trim END
1262 vim9script
1263 def g:CountSpaces(type = ''): string
1264 normal! '[V']y
1265 g:result = getreg('"')->count(' ')
1266 return ''
1267 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001268 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001269 &operatorfunc = g:CountSpaces
1270 new
1271 'a b c d e'->setline(1)
1272 feedkeys("g@_", 'x')
1273 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001274
1275 &operatorfunc = ''
1276 g:result = 0
1277 # global function works in :def function
1278 def Func()
1279 &operatorfunc = g:CountSpaces
1280 enddef
1281 Func()
1282 feedkeys("g@_", 'x')
1283 assert_equal(4, g:result)
1284
Bram Moolenaaref082e12021-12-12 21:02:03 +00001285 bwipe!
1286 END
1287 CheckScriptSuccess(lines)
1288 &operatorfunc = ''
1289enddef
1290
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001291def Test_use_script_func_name_with_prefix()
1292 var lines =<< trim END
1293 vim9script
1294 func s:Getit()
1295 return 'it'
1296 endfunc
1297 var Fn = s:Getit
1298 assert_equal('it', Fn())
1299 END
1300 CheckScriptSuccess(lines)
1301enddef
1302
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001303def Test_lambda_type_allocated()
1304 # Check that unreferencing a partial using a lambda can use the variable type
1305 # after the lambda has been freed and does not leak memory.
1306 var lines =<< trim END
1307 vim9script
1308
1309 func MyomniFunc1(val, findstart, base)
1310 return a:findstart ? 0 : []
1311 endfunc
1312
1313 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1314 &omnifunc = Lambda
1315 Lambda = (a, b) => MyomniFunc1(20, a, b)
1316 &omnifunc = string(Lambda)
1317 Lambda = (a, b) => strlen(a)
1318 END
1319 CheckScriptSuccess(lines)
1320enddef
1321
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001322" Default arg and varargs
1323def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001324 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001325 for s in rest
1326 res ..= ',' .. s
1327 endfor
1328 return res
1329enddef
1330
1331def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001332 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001333 MyDefVarargs('one')->assert_equal('one,foo')
1334 MyDefVarargs('one', 'two')->assert_equal('one,two')
1335 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001336 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001337 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001338 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001339 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001340
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001341 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001342 vim9script
1343 def Func(...l: list<string>)
1344 echo l
1345 enddef
1346 Func('a', 'b', 'c')
1347 END
1348 CheckScriptSuccess(lines)
1349
1350 lines =<< trim END
1351 vim9script
1352 def Func(...l: list<string>)
1353 echo l
1354 enddef
1355 Func()
1356 END
1357 CheckScriptSuccess(lines)
1358
1359 lines =<< trim END
1360 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001361 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001362 echo l
1363 enddef
1364 Func(0)
1365 END
1366 CheckScriptSuccess(lines)
1367
1368 lines =<< trim END
1369 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001370 def Func(...l: any)
1371 echo l
1372 enddef
1373 Func(0)
1374 END
1375 CheckScriptFailure(lines, 'E1180:', 2)
1376
1377 lines =<< trim END
1378 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001379 def Func(..._l: list<string>)
1380 echo _l
1381 enddef
1382 Func('a', 'b', 'c')
1383 END
1384 CheckScriptSuccess(lines)
1385
1386 lines =<< trim END
1387 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001388 def Func(...l: list<string>)
1389 echo l
1390 enddef
1391 Func(1, 2, 3)
1392 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001393 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001394
1395 lines =<< trim END
1396 vim9script
1397 def Func(...l: list<string>)
1398 echo l
1399 enddef
1400 Func('a', 9)
1401 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001402 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001403
1404 lines =<< trim END
1405 vim9script
1406 def Func(...l: list<string>)
1407 echo l
1408 enddef
1409 Func(1, 'a')
1410 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001411 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001412
1413 lines =<< trim END
1414 vim9script
1415 def Func( # some comment
1416 ...l = []
1417 )
1418 echo l
1419 enddef
1420 END
1421 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001422
1423 lines =<< trim END
1424 vim9script
1425 def DoIt()
1426 g:Later('')
1427 enddef
1428 defcompile
1429 def g:Later(...l: list<number>)
1430 enddef
1431 DoIt()
1432 END
1433 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001434enddef
1435
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001436let s:value = ''
1437
1438def FuncOneDefArg(opt = 'text')
1439 s:value = opt
1440enddef
1441
1442def FuncTwoDefArg(nr = 123, opt = 'text'): string
1443 return nr .. opt
1444enddef
1445
1446def FuncVarargs(...arg: list<string>): string
1447 return join(arg, ',')
1448enddef
1449
1450def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001451 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001452 RefDefArg = FuncOneDefArg
1453 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001454 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001455 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001456 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001457
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001458 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001459 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001460 RefDef2Arg()->assert_equal('123text')
1461 RefDef2Arg(99)->assert_equal('99text')
1462 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001463
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001464 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1465 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001466
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001467 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001468 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001469 RefVarargs()->assert_equal('')
1470 RefVarargs('one')->assert_equal('one')
1471 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001472
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001473 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1474 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001475enddef
1476
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001477" Only varargs
1478def MyVarargsOnly(...args: list<string>): string
1479 return join(args, ',')
1480enddef
1481
1482def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001483 MyVarargsOnly()->assert_equal('')
1484 MyVarargsOnly('one')->assert_equal('one')
1485 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001486 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1487 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001488enddef
1489
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001490def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001491 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001492 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001493 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001494enddef
1495
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001496def DictArg(arg: dict<string>)
1497 arg['key'] = 'value'
1498enddef
1499
1500def ListArg(arg: list<string>)
1501 arg[0] = 'value'
1502enddef
1503
1504def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001505 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001506 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001507 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001508 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001509 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001510 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001511 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001512
Bram Moolenaard2c61702020-09-06 15:58:36 +02001513 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001514 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001515enddef
1516
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001517" These argument names are reserved in legacy functions.
1518def WithReservedNames(firstline: string, lastline: string): string
1519 return firstline .. lastline
1520enddef
1521
1522def Test_argument_names()
1523 assert_equal('OK', WithReservedNames('O', 'K'))
1524enddef
1525
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001526def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001527 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001528 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001529enddef
1530
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001531func DefinedLater(arg)
1532 return a:arg
1533endfunc
1534
1535def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001536 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001537 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001538 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001539
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001540 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001541 vim9script
1542 def RetNumber(): number
1543 return 123
1544 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001545 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001546 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001547 END
1548 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001549
1550 lines =<< trim END
1551 vim9script
1552 def RetNumber(): number
1553 return 123
1554 enddef
1555 def Bar(F: func: number): number
1556 return F()
1557 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001558 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001559 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001560 END
1561 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001562
1563 lines =<< trim END
1564 vim9script
1565 def UseNumber(nr: number)
1566 echo nr
1567 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001568 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001569 Funcref(123)
1570 END
1571 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001572
1573 lines =<< trim END
1574 vim9script
1575 def UseNumber(nr: number)
1576 echo nr
1577 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001578 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001579 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001580 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001581
1582 lines =<< trim END
1583 vim9script
1584 def EchoNr(nr = 34)
1585 g:echo = nr
1586 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001587 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001588 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001589 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001590 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001591 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001592 END
1593 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001594
1595 lines =<< trim END
1596 vim9script
1597 def EchoList(...l: list<number>)
1598 g:echo = l
1599 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001600 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001601 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001602 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001603 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001604 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001605 END
1606 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001607
1608 lines =<< trim END
1609 vim9script
1610 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1611 g:optarg = opt
1612 g:listarg = l
1613 return nr
1614 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001615 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001616 Funcref(10)->assert_equal(10)
1617 g:optarg->assert_equal(12)
1618 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001619
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001620 Funcref(11, 22)->assert_equal(11)
1621 g:optarg->assert_equal(22)
1622 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001623
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001624 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1625 g:optarg->assert_equal(18)
1626 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001627 END
1628 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001629enddef
1630
1631let SomeFunc = function('len')
1632let NotAFunc = 'text'
1633
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001634def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001635 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001636 var Ref1: func(bool): string
1637 var Ref2: func(bool): number
1638 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001639 Ref3 = g:cond ? Ref1 : Ref2
1640
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001641 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001642 var Refa1: func(bool): number
1643 var Refa2: func(bool, number): number
1644 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001645 Refa3 = g:cond ? Refa1 : Refa2
1646
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001647 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001648 var Refb1: func(bool, string): number
1649 var Refb2: func(string, number): number
1650 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001651 Refb3 = g:cond ? Refb1 : Refb2
1652enddef
1653
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001654def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001655 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001656enddef
1657
1658def DefinedEvenLater(arg: string): string
1659 return arg
1660enddef
1661
1662def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001663 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001664 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001665enddef
1666
1667def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001668 CheckScriptFailure([
1669 'def Func(): number',
1670 'return "a"',
1671 'enddef',
1672 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001673 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001674 CheckScriptFailure([
1675 'def Func(): string',
1676 'return 1',
1677 'enddef',
1678 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001679 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001680 CheckScriptFailure([
1681 'def Func(): void',
1682 'return "a"',
1683 'enddef',
1684 'defcompile'],
1685 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001686 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001687 CheckScriptFailure([
1688 'def Func()',
1689 'return "a"',
1690 'enddef',
1691 'defcompile'],
1692 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001693 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001694
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001695 CheckScriptFailure([
1696 'def Func(): number',
1697 'return',
1698 'enddef',
1699 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001700 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001701
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001702 CheckScriptFailure([
1703 'def Func():number',
1704 'return 123',
1705 'enddef',
1706 'defcompile'], 'E1069:')
1707 delfunc! g:Func
1708
1709 CheckScriptFailure([
1710 'def Func() :number',
1711 'return 123',
1712 'enddef',
1713 'defcompile'], 'E1059:')
1714 delfunc! g:Func
1715
1716 CheckScriptFailure([
1717 'def Func() : number',
1718 'return 123',
1719 'enddef',
1720 'defcompile'], 'E1059:')
1721 delfunc! g:Func
1722
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001723 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001724 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001725 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001726 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001727 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001728 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001729
1730 CheckScriptFailure([
1731 'vim9script',
1732 'def FuncB()',
1733 ' return 123',
1734 'enddef',
1735 'def FuncA()',
1736 ' FuncB()',
1737 'enddef',
1738 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001739enddef
1740
1741def Test_arg_type_wrong()
1742 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001743 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001744 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001745 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001746 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1747 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001748enddef
1749
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001750def Test_white_space_before_comma()
1751 var lines =<< trim END
1752 vim9script
1753 def Func(a: number , b: number)
1754 enddef
1755 END
1756 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001757 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001758enddef
1759
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001760def Test_white_space_after_comma()
1761 var lines =<< trim END
1762 vim9script
1763 def Func(a: number,b: number)
1764 enddef
1765 END
1766 CheckScriptFailure(lines, 'E1069:')
1767
1768 # OK in legacy function
1769 lines =<< trim END
1770 vim9script
1771 func Func(a,b)
1772 endfunc
1773 END
1774 CheckScriptSuccess(lines)
1775enddef
1776
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001777def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001778 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001779 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001780 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001781 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001782 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001783 enddef
1784 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001785 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001786
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001787 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001788 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001789 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001790
Bram Moolenaar67979662020-06-20 22:50:47 +02001791 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001792 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001793 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001794
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001795 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001796 def ListFunc(arg: list<number>)
1797 listvar = arg
1798 enddef
1799 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001800 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001801
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001802 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001803 def DictFunc(arg: dict<number>)
1804 dictvar = arg
1805 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001806 {a: 1, b: 2}->DictFunc()
1807 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001808 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001809 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001810 enddef
1811 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001812 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001813
Bram Moolenaare0de1712020-12-02 17:36:54 +01001814 {a: 3, b: 4}->DictFunc()
1815 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001816
1817 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001818 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001819 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001820 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001821
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001822 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001823 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001824 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001825 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001826 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001827 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001828
1829 def UseString()
1830 'xyork'->MyFunc()
1831 enddef
1832 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001833 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001834
Bram Moolenaar10409562020-07-29 20:00:38 +02001835 def UseString2()
1836 "knife"->MyFunc()
1837 enddef
1838 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001839 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001840
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001841 # prepending a colon makes it a mark
1842 new
1843 setline(1, ['aaa', 'bbb', 'ccc'])
1844 normal! 3Gmt1G
1845 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001846 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001847 bwipe!
1848
Bram Moolenaare6b53242020-07-01 17:28:33 +02001849 MyFunc(
1850 'continued'
1851 )
1852 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001853 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001854 )
1855
1856 call MyFunc(
1857 'more'
1858 ..
1859 'lines'
1860 )
1861 assert_equal(
1862 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001863 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001864 END
1865 writefile(lines, 'Xcall.vim')
1866 source Xcall.vim
1867 delete('Xcall.vim')
1868enddef
1869
1870def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001871 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001872 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001873 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001874 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001875 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001877 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001878 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001879 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001880enddef
1881
Bram Moolenaar65b95452020-07-19 14:03:09 +02001882def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001883 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001884 vim9script
1885 def MyFunc(arg: string)
1886 echo arg
1887 enddef
1888 MyFunc(1234)
1889 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001890 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001891enddef
1892
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001893def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001894 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001895 vim9script
1896 const var = ''
1897 def MyFunc(arg: string)
1898 var = 'asdf'
1899 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001900 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001901 END
1902 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001903 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001904 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001905
1906 lines =<< trim END
1907 const g:Aconst = 77
1908 def Change()
1909 # comment
1910 g:Aconst = 99
1911 enddef
1912 call Change()
1913 unlet g:Aconst
1914 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001915 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916enddef
1917
1918" Test that inside :function a Python function can be defined, :def is not
1919" recognized.
1920func Test_function_python()
1921 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001922 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001923 execute py "<< EOF"
1924def do_something():
1925 return 1
1926EOF
1927endfunc
1928
1929def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001930 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001931 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001932 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001933 echo 'hello'
1934 enddef
1935
1936 def CallGoneSoon()
1937 GoneSoon()
1938 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001939 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001940
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001941 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001942 CallGoneSoon()
1943 END
1944 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001945 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1946 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001947
1948 delete('XToDelFunc')
1949enddef
1950
Bram Moolenaar7509ad82021-12-14 18:14:37 +00001951func Test_free_dict_while_in_funcstack()
1952 " relies on the sleep command
1953 CheckUnix
1954 call Run_Test_free_dict_while_in_funcstack()
1955endfunc
1956
1957def Run_Test_free_dict_while_in_funcstack()
1958
1959 # this was freeing the TermRun() default argument dictionary while it was
1960 # still referenced in a funcstack_T
1961 var lines =<< trim END
1962 vim9script
1963
1964 &updatetime = 400
1965 def TermRun(_ = {})
1966 def Post()
1967 enddef
1968 def Exec()
1969 term_start('sleep 1', {
1970 term_finish: 'close',
1971 exit_cb: (_, _) => Post(),
1972 })
1973 enddef
1974 Exec()
1975 enddef
1976 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
1977 timer_start(100, (_) => feedkeys("\<F4>"))
1978 timer_start(1000, (_) => feedkeys("\<F4>"))
1979 sleep 1500m
1980 END
1981 CheckScriptSuccess(lines)
1982 nunmap <F4>
1983 set updatetime&
1984enddef
1985
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001986def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001987 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001988 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001989 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001990 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001991 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001992 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001993 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001994 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001995 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001996
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001997 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001998 g:Func1()->assert_equal('Func1')
1999 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002000
2001 delfunc! Func0
2002 delfunc! Func1
2003 delfunc! Func2
2004enddef
2005
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002006def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002007 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002008 vim9script
2009 func Func(arg)
2010 echo a:arg
2011 endfunc
2012 Func('text')
2013 END
2014 writefile(lines, 'XVim9Func')
2015 so XVim9Func
2016
2017 delete('XVim9Func')
2018enddef
2019
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020let s:funcResult = 0
2021
2022def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002023 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002024enddef
2025
2026def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002027 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002028 return 1234
2029enddef
2030
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002031def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002032 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002033 return 'text'
2034enddef
2035
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002036def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002037 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002038enddef
2039
2040def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002041 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002042 return arg
2043enddef
2044
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002045def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002046 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002047enddef
2048
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002049def FuncOneArgRetString(arg: string): string
2050 return arg
2051enddef
2052
Bram Moolenaar89228602020-04-05 22:14:54 +02002053def FuncOneArgRetAny(arg: any): any
2054 return arg
2055enddef
2056
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002057def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002058 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002059 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002060 Ref1 = FuncNoArgNoRet
2061 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002062 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002063
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002064 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002065 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002066 Ref2 = FuncNoArgNoRet
2067 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002068 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002069
Bram Moolenaar53900992020-08-22 19:02:02 +02002070 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002071 Ref2 = FuncOneArgNoRet
2072 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002073 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002074
Bram Moolenaar53900992020-08-22 19:02:02 +02002075 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002076 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002077 Ref2()->assert_equal(1234)
2078 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002079
Bram Moolenaar53900992020-08-22 19:02:02 +02002080 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002081 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002082 Ref2(13)->assert_equal(13)
2083 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002084enddef
2085
Bram Moolenaar9978d472020-07-05 16:01:56 +02002086def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002087 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002088 for n in repeat([1], 3)
2089 res += n
2090 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002091 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002092
2093 res = 0
2094 for n in add([1, 2], 3)
2095 res += n
2096 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002097 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002098enddef
2099
Bram Moolenaar846178a2020-07-05 17:04:13 +02002100def Test_argv_return_type()
2101 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002102 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002103 for name in argv()
2104 res ..= name
2105 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002106 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002107enddef
2108
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002109def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002110 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002111 RefVoid = FuncNoArgNoRet
2112 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002113 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2114 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002115
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002116 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002117 RefAny = FuncNoArgRetNumber
2118 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002119 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2120 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002121
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002122 var RefAnyNoArgs: func: any = RefAny
2123
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002124 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002125 RefNr = FuncNoArgRetNumber
2126 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002127 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2128 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002129
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002130 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002131 RefStr = FuncNoArgRetString
2132 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002133 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2134 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002135enddef
2136
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002137def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002138 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002139
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002140 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2141 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2142 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2143 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2144 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2145 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 +02002146
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002147 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2148 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2149 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:')
2150 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002151enddef
2152
Bram Moolenaar89228602020-04-05 22:14:54 +02002153def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002154 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002155 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002156 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002157
2158 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002159 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002160
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002161 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002162 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002163 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002164
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002165 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002166enddef
2167
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002168def Test_func_common_type()
2169 def FuncOne(n: number): number
2170 return n
2171 enddef
2172 def FuncTwo(s: string): number
2173 return len(s)
2174 enddef
2175 def FuncThree(n: number, s: string): number
2176 return n + len(s)
2177 enddef
2178 var list = [FuncOne, FuncTwo, FuncThree]
2179 assert_equal(8, list[0](8))
2180 assert_equal(4, list[1]('word'))
2181 assert_equal(7, list[2](3, 'word'))
2182enddef
2183
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002184def MultiLine(
2185 arg1: string,
2186 arg2 = 1234,
2187 ...rest: list<string>
2188 ): string
2189 return arg1 .. arg2 .. join(rest, '-')
2190enddef
2191
Bram Moolenaar2c330432020-04-13 14:41:35 +02002192def MultiLineComment(
2193 arg1: string, # comment
2194 arg2 = 1234, # comment
2195 ...rest: list<string> # comment
2196 ): string # comment
2197 return arg1 .. arg2 .. join(rest, '-')
2198enddef
2199
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002200def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002201 MultiLine('text')->assert_equal('text1234')
2202 MultiLine('text', 777)->assert_equal('text777')
2203 MultiLine('text', 777, 'one')->assert_equal('text777one')
2204 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002205enddef
2206
Bram Moolenaar23e03252020-04-12 22:22:31 +02002207func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002208 call MultiLine('text')->assert_equal('text1234')
2209 call MultiLine('text', 777)->assert_equal('text777')
2210 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2211 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002212endfunc
2213
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002214
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002215" When using CheckScriptFailure() for the below test, E1010 is generated instead
2216" of E1056.
2217func Test_E1056_1059()
2218 let caught_1056 = 0
2219 try
2220 def F():
2221 return 1
2222 enddef
2223 catch /E1056:/
2224 let caught_1056 = 1
2225 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002226 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002227
2228 let caught_1059 = 0
2229 try
2230 def F5(items : list)
2231 echo 'a'
2232 enddef
2233 catch /E1059:/
2234 let caught_1059 = 1
2235 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002236 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002237endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002238
Bram Moolenaar015f4262020-05-05 21:25:22 +02002239func DelMe()
2240 echo 'DelMe'
2241endfunc
2242
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002243def Test_error_reporting()
2244 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002245 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002246 " comment
2247 def Func()
2248 # comment
2249 # comment
2250 invalid
2251 enddef
2252 defcompile
2253 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002254 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002255 try
2256 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002257 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002258 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002259 v:exception->assert_match('Invalid command: invalid')
2260 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002261 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002262 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002263
2264 # comment lines after the start of the function
2265 lines =<< trim END
2266 " comment
2267 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002268 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002269 # comment
2270 # comment
2271 invalid
2272 enddef
2273 defcompile
2274 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002275 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002276 try
2277 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002278 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002279 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002280 v:exception->assert_match('Invalid command: invalid')
2281 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002282 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002283 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002284
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002285 lines =<< trim END
2286 vim9script
2287 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002288 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002289 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002290 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002291 enddef
2292 defcompile
2293 Func()
2294 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002295 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002296 try
2297 source Xdef
2298 assert_report('should have failed')
2299 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002300 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002301 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002302 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002303
Bram Moolenaar08052222020-09-14 17:04:31 +02002304 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002305enddef
2306
Bram Moolenaar015f4262020-05-05 21:25:22 +02002307def Test_deleted_function()
2308 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002309 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002310 'delfunc g:DelMe',
2311 'echo RefMe()'], 'E117:')
2312enddef
2313
2314def Test_unknown_function()
2315 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002316 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002317 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002318enddef
2319
Bram Moolenaar328eac22021-01-07 19:23:08 +01002320def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002321 return Ref('more')
2322enddef
2323
2324def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002325 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002326 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002327enddef
2328
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002329def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002330 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002331 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002332enddef
2333
2334def Test_closure_ref_after_return()
2335 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002336 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002337 unlet g:Ref
2338enddef
2339
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002340def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002341 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002342 g:Extend = (s) => local->add(s)
2343 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002344enddef
2345
2346def Test_closure_two_refs()
2347 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002348 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002349 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002350 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002351 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002352 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002353
2354 unlet g:Extend
2355 unlet g:Read
2356enddef
2357
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002358def ReadRef(Ref: func(): list<string>): string
2359 return join(Ref(), ' ')
2360enddef
2361
Bram Moolenaar5e654232020-09-16 15:22:00 +02002362def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002363 Ref(add)
2364enddef
2365
2366def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002367 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002368 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002369 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002370 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002371 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002372 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002373
2374 unlet g:Extend
2375 unlet g:Read
2376enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002377
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002378def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002379 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002380 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002381enddef
2382
2383def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002384 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002385 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002386enddef
2387
2388def Test_closure_using_argument()
2389 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002390 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002391
2392 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002393 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002394
2395 unlet g:UseArg
2396 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002397
2398 var lines =<< trim END
2399 vim9script
2400 def Test(Fun: func(number): number): list<number>
2401 return map([1, 2, 3], (_, i) => Fun(i))
2402 enddef
2403 def Inc(nr: number): number
2404 return nr + 2
2405 enddef
2406 assert_equal([3, 4, 5], Test(Inc))
2407 END
2408 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002409enddef
2410
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002411def MakeGetAndAppendRefs()
2412 var local = 'a'
2413
2414 def Append(arg: string)
2415 local ..= arg
2416 enddef
2417 g:Append = Append
2418
2419 def Get(): string
2420 return local
2421 enddef
2422 g:Get = Get
2423enddef
2424
2425def Test_closure_append_get()
2426 MakeGetAndAppendRefs()
2427 g:Get()->assert_equal('a')
2428 g:Append('-b')
2429 g:Get()->assert_equal('a-b')
2430 g:Append('-c')
2431 g:Get()->assert_equal('a-b-c')
2432
2433 unlet g:Append
2434 unlet g:Get
2435enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002436
Bram Moolenaar04b12692020-05-04 23:24:44 +02002437def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002438 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002439 def Closure(arg: string): string
2440 return local .. arg
2441 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002442 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002443enddef
2444
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002445func GetResult(Ref)
2446 return a:Ref('some')
2447endfunc
2448
2449def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002450 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002451 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002452 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002453enddef
2454
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002455def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002456 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002457 vim9script
2458 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002459 var name = 0
2460 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002461 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002462 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002463 enddef
2464 Func()
2465 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002466 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002467enddef
2468
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002469def Test_nested_closure_used()
2470 var lines =<< trim END
2471 vim9script
2472 def Func()
2473 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002474 var Closure = () => x
2475 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002476 enddef
2477 Func()
2478 assert_equal('hello', g:Myclosure())
2479 END
2480 CheckScriptSuccess(lines)
2481enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002482
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002483def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002484 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002485 vim9script
2486 def FuncA()
2487 FuncB(0)
2488 enddef
2489 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002490 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002491 enddef
2492 FuncA()
2493 END
2494 CheckScriptFailure(lines, 'E1012:')
2495enddef
2496
Bram Moolenaarf112f302020-12-20 17:47:52 +01002497def Test_global_closure()
2498 var lines =<< trim END
2499 vim9script
2500 def ReverseEveryNLines(n: number, line1: number, line2: number)
2501 var mods = 'sil keepj keepp lockm '
2502 var range = ':' .. line1 .. ',' .. line2
2503 def g:Offset(): number
2504 var offset = (line('.') - line1 + 1) % n
2505 return offset != 0 ? offset : n
2506 enddef
2507 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2508 enddef
2509
2510 new
2511 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2512 ReverseEveryNLines(3, 1, 9)
2513 END
2514 CheckScriptSuccess(lines)
2515 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2516 assert_equal(expected, getline(1, 9))
2517 bwipe!
2518enddef
2519
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002520def Test_global_closure_called_directly()
2521 var lines =<< trim END
2522 vim9script
2523 def Outer()
2524 var x = 1
2525 def g:Inner()
2526 var y = x
2527 x += 1
2528 assert_equal(1, y)
2529 enddef
2530 g:Inner()
2531 assert_equal(2, x)
2532 enddef
2533 Outer()
2534 END
2535 CheckScriptSuccess(lines)
2536 delfunc g:Inner
2537enddef
2538
Bram Moolenaar69c76172021-12-02 16:38:52 +00002539def Test_closure_called_from_legacy()
2540 var lines =<< trim END
2541 vim9script
2542 def Func()
2543 var outer = 'foo'
2544 var F = () => {
2545 outer = 'bar'
2546 }
2547 execute printf('call %s()', string(F))
2548 enddef
2549 Func()
2550 END
2551 CheckScriptFailure(lines, 'E1248')
2552enddef
2553
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002554def Test_failure_in_called_function()
2555 # this was using the frame index as the return value
2556 var lines =<< trim END
2557 vim9script
2558 au TerminalWinOpen * eval [][0]
2559 def PopupTerm(a: any)
2560 # make sure typvals on stack are string
2561 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2562 FireEvent()
2563 enddef
2564 def FireEvent()
2565 do TerminalWinOpen
2566 enddef
2567 # use try/catch to make eval fail
2568 try
2569 call PopupTerm(0)
2570 catch
2571 endtry
2572 au! TerminalWinOpen
2573 END
2574 CheckScriptSuccess(lines)
2575enddef
2576
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002577def Test_nested_lambda()
2578 var lines =<< trim END
2579 vim9script
2580 def Func()
2581 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002582 var Lambda1 = () => 7
2583 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002584 var res = Lambda2()
2585 assert_equal([7, 4], res)
2586 enddef
2587 Func()
2588 END
2589 CheckScriptSuccess(lines)
2590enddef
2591
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002592def Test_double_nested_lambda()
2593 var lines =<< trim END
2594 vim9script
2595 def F(head: string): func(string): func(string): string
2596 return (sep: string): func(string): string => ((tail: string): string => {
2597 return head .. sep .. tail
2598 })
2599 enddef
2600 assert_equal('hello-there', F('hello')('-')('there'))
2601 END
2602 CheckScriptSuccess(lines)
2603enddef
2604
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002605def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002606 var lines =<< trim END
2607 vim9script
2608 def F(text: string): func(string): func(string): string
2609 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002610 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002611 })
2612 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002613 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002614 END
2615 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002616
2617 lines =<< trim END
2618 vim9script
2619 echo range(4)->mapnew((_, v) => {
2620 return range(v) ->mapnew((_, s) => {
2621 return string(s)
2622 })
2623 })
2624 END
2625 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002626
2627 lines =<< trim END
2628 vim9script
2629
2630 def s:func()
2631 range(10)
2632 ->mapnew((_, _) => ({
2633 key: range(10)->mapnew((_, _) => {
2634 return ' '
2635 }),
2636 }))
2637 enddef
2638
2639 defcomp
2640 END
2641 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002642enddef
2643
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002644def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002645 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002646 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002647enddef
2648
2649def Test_lambda_arg_shadows_func()
2650 assert_equal([42], Shadowed())
2651enddef
2652
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002653def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002654 var path: string = empty(dir)
2655 \ ? 'empty'
2656 \ : 'full'
2657 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002658enddef
2659
2660def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002661 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002662enddef
2663
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002664def Test_script_var_in_lambda()
2665 var lines =<< trim END
2666 vim9script
2667 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002668 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002669 END
2670 CheckScriptSuccess(lines)
2671enddef
2672
Bram Moolenaar5e654232020-09-16 15:22:00 +02002673def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002674 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002675 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002676 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002677 ->reverse()
2678 return x
2679enddef
2680
2681def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002682 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002683
2684 var lines =<< trim END
2685 vim9script
2686 var res = [{n: 1, m: 2, s: 'xxx'}]
2687 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2688 v.n,
2689 v.m,
2690 substitute(v.s, '.*', 'yyy', '')
2691 ))
2692 assert_equal(['1:2:yyy'], res)
2693 END
2694 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002695enddef
2696
Bram Moolenaarb6571982021-01-08 22:24:19 +01002697def Test_list_lambda()
2698 timer_start(1000, (_) => 0)
2699 var body = execute(timer_info()[0].callback
2700 ->string()
2701 ->substitute("('", ' ', '')
2702 ->substitute("')", '', '')
2703 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002704 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002705enddef
2706
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002707def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002708 var lines =<< trim END
2709 vim9script
2710 var flist: list<func>
2711 for i in range(10)
2712 var inloop = i
2713 flist[i] = () => inloop
2714 endfor
2715 END
2716 CheckScriptSuccess(lines)
2717
2718 lines =<< trim END
2719 vim9script
2720 if true
2721 var outloop = 5
2722 var flist: list<func>
2723 for i in range(10)
2724 flist[i] = () => outloop
2725 endfor
2726 endif
2727 END
2728 CheckScriptSuccess(lines)
2729
2730 lines =<< trim END
2731 vim9script
2732 if true
2733 var outloop = 5
2734 endif
2735 var flist: list<func>
2736 for i in range(10)
2737 flist[i] = () => outloop
2738 endfor
2739 END
2740 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002741
2742 lines =<< trim END
2743 vim9script
2744 for i in range(10)
2745 var Ref = () => 0
2746 endfor
2747 assert_equal(0, ((i) => 0)(0))
2748 END
2749 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002750enddef
2751
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002752def Test_legacy_lambda()
2753 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002754
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002755 var lines =<< trim END
2756 echo {x -> 'hello ' .. x}('foo')
2757 END
2758 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002759
2760 lines =<< trim END
2761 vim9script
2762 def Func()
2763 echo (() => 'no error')()
2764 enddef
2765 legacy call s:Func()
2766 END
2767 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002768enddef
2769
Bram Moolenaarce024c32021-06-26 13:00:49 +02002770def Test_legacy()
2771 var lines =<< trim END
2772 vim9script
2773 func g:LegacyFunction()
2774 let g:legacyvar = 1
2775 endfunc
2776 def Testit()
2777 legacy call g:LegacyFunction()
2778 enddef
2779 Testit()
2780 assert_equal(1, g:legacyvar)
2781 unlet g:legacyvar
2782 delfunc g:LegacyFunction
2783 END
2784 CheckScriptSuccess(lines)
2785enddef
2786
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002787def Test_legacy_errors()
2788 for cmd in ['if', 'elseif', 'else', 'endif',
2789 'for', 'endfor', 'continue', 'break',
2790 'while', 'endwhile',
2791 'try', 'catch', 'finally', 'endtry']
2792 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2793 endfor
2794enddef
2795
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002796def Test_call_legacy_with_dict()
2797 var lines =<< trim END
2798 vim9script
2799 func Legacy() dict
2800 let g:result = self.value
2801 endfunc
2802 def TestDirect()
2803 var d = {value: 'yes', func: Legacy}
2804 d.func()
2805 enddef
2806 TestDirect()
2807 assert_equal('yes', g:result)
2808 unlet g:result
2809
2810 def TestIndirect()
2811 var d = {value: 'foo', func: Legacy}
2812 var Fi = d.func
2813 Fi()
2814 enddef
2815 TestIndirect()
2816 assert_equal('foo', g:result)
2817 unlet g:result
2818
2819 var d = {value: 'bar', func: Legacy}
2820 d.func()
2821 assert_equal('bar', g:result)
2822 unlet g:result
2823 END
2824 CheckScriptSuccess(lines)
2825enddef
2826
Bram Moolenaarab360522021-01-10 14:02:28 +01002827def DoFilterThis(a: string): list<string>
2828 # closure nested inside another closure using argument
2829 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2830 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2831enddef
2832
2833def Test_nested_closure_using_argument()
2834 assert_equal(['x', 'x2'], DoFilterThis('x'))
2835enddef
2836
Bram Moolenaar0186e582021-01-10 18:33:11 +01002837def Test_triple_nested_closure()
2838 var what = 'x'
2839 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2840 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2841 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2842enddef
2843
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002844func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002845 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002846 call Run_Test_silent_echo()
2847endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002848
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002849def Run_Test_silent_echo()
2850 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002851 vim9script
2852 def EchoNothing()
2853 silent echo ''
2854 enddef
2855 defcompile
2856 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002857 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002858
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002859 # Check that the balloon shows up after a mouse move
2860 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2861 term_sendkeys(buf, ":abc")
2862 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002863
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002864 # clean up
2865 StopVimInTerminal(buf)
2866 delete('XTest_silent_echo')
2867enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002868
Bram Moolenaar171fb922020-10-28 16:54:47 +01002869def SilentlyError()
2870 execute('silent! invalid')
2871 g:did_it = 'yes'
2872enddef
2873
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002874func UserError()
2875 silent! invalid
2876endfunc
2877
2878def SilentlyUserError()
2879 UserError()
2880 g:did_it = 'yes'
2881enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002882
2883" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002884func Test_ignore_silent_error()
2885 let g:did_it = 'no'
2886 call SilentlyError()
2887 call assert_equal('yes', g:did_it)
2888
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002889 let g:did_it = 'no'
2890 call SilentlyUserError()
2891 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002892
2893 unlet g:did_it
2894endfunc
2895
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002896def Test_ignore_silent_error_in_filter()
2897 var lines =<< trim END
2898 vim9script
2899 def Filter(winid: number, key: string): bool
2900 if key == 'o'
2901 silent! eval [][0]
2902 return true
2903 endif
2904 return popup_filter_menu(winid, key)
2905 enddef
2906
Bram Moolenaare0de1712020-12-02 17:36:54 +01002907 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002908 feedkeys("o\r", 'xnt')
2909 END
2910 CheckScriptSuccess(lines)
2911enddef
2912
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002913def Fibonacci(n: number): number
2914 if n < 2
2915 return n
2916 else
2917 return Fibonacci(n - 1) + Fibonacci(n - 2)
2918 endif
2919enddef
2920
Bram Moolenaar985116a2020-07-12 17:31:09 +02002921def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002922 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002923enddef
2924
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002925def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002926 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002927 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002928 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002929 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002930 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002931enddef
2932
2933def Test_closure_in_map()
2934 mkdir('XclosureDir/tdir', 'p')
2935 writefile(['111'], 'XclosureDir/file1')
2936 writefile(['222'], 'XclosureDir/file2')
2937 writefile(['333'], 'XclosureDir/tdir/file3')
2938
Bram Moolenaare0de1712020-12-02 17:36:54 +01002939 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002940
2941 delete('XclosureDir', 'rf')
2942enddef
2943
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002944def Test_invalid_function_name()
2945 var lines =<< trim END
2946 vim9script
2947 def s: list<string>
2948 END
2949 CheckScriptFailure(lines, 'E129:')
2950
2951 lines =<< trim END
2952 vim9script
2953 def g: list<string>
2954 END
2955 CheckScriptFailure(lines, 'E129:')
2956
2957 lines =<< trim END
2958 vim9script
2959 def <SID>: list<string>
2960 END
2961 CheckScriptFailure(lines, 'E884:')
2962
2963 lines =<< trim END
2964 vim9script
2965 def F list<string>
2966 END
2967 CheckScriptFailure(lines, 'E488:')
2968enddef
2969
Bram Moolenaara90afb92020-07-15 22:38:56 +02002970def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002971 var lines =<< trim END
2972 var Xsetlist: func
2973 Xsetlist = function('setloclist', [0])
2974 Xsetlist([], ' ', {title: 'test'})
2975 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002976
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002977 Xsetlist = function('setloclist', [0, [], ' '])
2978 Xsetlist({title: 'test'})
2979 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002980
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002981 Xsetlist = function('setqflist')
2982 Xsetlist([], ' ', {title: 'test'})
2983 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002984
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002985 Xsetlist = function('setqflist', [[], ' '])
2986 Xsetlist({title: 'test'})
2987 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002988
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002989 var Len: func: number = function('len', ['word'])
2990 assert_equal(4, Len())
2991
2992 var RepeatFunc = function('repeat', ['o'])
2993 assert_equal('ooooo', RepeatFunc(5))
2994 END
2995 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002996
2997 lines =<< trim END
2998 vim9script
2999 def Foo(Parser: any)
3000 enddef
3001 var Expr: func(dict<any>): dict<any>
3002 const Call = Foo(Expr)
3003 END
3004 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003005enddef
3006
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003007def Test_cmd_modifier()
3008 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003009 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003010enddef
3011
3012def Test_restore_modifiers()
3013 # check that when compiling a :def function command modifiers are not messed
3014 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003015 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003016 vim9script
3017 set eventignore=
3018 autocmd QuickFixCmdPost * copen
3019 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003020 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003021 enddef
3022 func Func()
3023 noautocmd call s:AutocmdsDisabled()
3024 let g:ei_after = &eventignore
3025 endfunc
3026 Func()
3027 END
3028 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003029 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003030enddef
3031
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003032def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003033 eval 1 + 2
3034 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003035 # call not on fourth line
3036 StackBot()
3037enddef
3038
3039def StackBot()
3040 # throw an error
3041 eval [][0]
3042enddef
3043
3044def Test_callstack_def()
3045 try
3046 StackTop()
3047 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003048 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003049 endtry
3050enddef
3051
Bram Moolenaare8211a32020-10-09 22:04:29 +02003052" Re-using spot for variable used in block
3053def Test_block_scoped_var()
3054 var lines =<< trim END
3055 vim9script
3056 def Func()
3057 var x = ['a', 'b', 'c']
3058 if 1
3059 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003060 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003061 endif
3062 var z = x
3063 assert_equal(['x', 'x', 'x'], z)
3064 enddef
3065 Func()
3066 END
3067 CheckScriptSuccess(lines)
3068enddef
3069
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003070def Test_reset_did_emsg()
3071 var lines =<< trim END
3072 @s = 'blah'
3073 au BufWinLeave * #
3074 def Func()
3075 var winid = popup_create('popup', {})
3076 exe '*s'
3077 popup_close(winid)
3078 enddef
3079 Func()
3080 END
3081 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003082 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003083enddef
3084
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003085def Test_did_emsg_reset()
3086 # executing an autocommand resets did_emsg, this should not result in a
3087 # builtin function considered failing
3088 var lines =<< trim END
3089 vim9script
3090 au BufWinLeave * #
3091 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003092 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003093 eval [][0]
3094 enddef
3095 nno <F3> <cmd>call <sid>Func()<cr>
3096 feedkeys("\<F3>\e", 'xt')
3097 END
3098 writefile(lines, 'XemsgReset')
3099 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3100 delete('XemsgReset')
3101 nunmap <F3>
3102 au! BufWinLeave
3103enddef
3104
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003105def Test_abort_with_silent_call()
3106 var lines =<< trim END
3107 vim9script
3108 g:result = 'none'
3109 def Func()
3110 g:result += 3
3111 g:result = 'yes'
3112 enddef
3113 # error is silenced, but function aborts on error
3114 silent! Func()
3115 assert_equal('none', g:result)
3116 unlet g:result
3117 END
3118 CheckScriptSuccess(lines)
3119enddef
3120
Bram Moolenaarf665e972020-12-05 19:17:16 +01003121def Test_continues_with_silent_error()
3122 var lines =<< trim END
3123 vim9script
3124 g:result = 'none'
3125 def Func()
3126 silent! g:result += 3
3127 g:result = 'yes'
3128 enddef
3129 # error is silenced, function does not abort
3130 Func()
3131 assert_equal('yes', g:result)
3132 unlet g:result
3133 END
3134 CheckScriptSuccess(lines)
3135enddef
3136
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003137def Test_abort_even_with_silent()
3138 var lines =<< trim END
3139 vim9script
3140 g:result = 'none'
3141 def Func()
3142 eval {-> ''}() .. '' .. {}['X']
3143 g:result = 'yes'
3144 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003145 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003146 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003147 unlet g:result
3148 END
3149 CheckScriptSuccess(lines)
3150enddef
3151
Bram Moolenaarf665e972020-12-05 19:17:16 +01003152def Test_cmdmod_silent_restored()
3153 var lines =<< trim END
3154 vim9script
3155 def Func()
3156 g:result = 'none'
3157 silent! g:result += 3
3158 g:result = 'none'
3159 g:result += 3
3160 enddef
3161 Func()
3162 END
3163 # can't use CheckScriptFailure, it ignores the :silent!
3164 var fname = 'Xdefsilent'
3165 writefile(lines, fname)
3166 var caught = 'no'
3167 try
3168 exe 'source ' .. fname
3169 catch /E1030:/
3170 caught = 'yes'
3171 assert_match('Func, line 4', v:throwpoint)
3172 endtry
3173 assert_equal('yes', caught)
3174 delete(fname)
3175enddef
3176
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003177def Test_cmdmod_silent_nested()
3178 var lines =<< trim END
3179 vim9script
3180 var result = ''
3181
3182 def Error()
3183 result ..= 'Eb'
3184 eval [][0]
3185 result ..= 'Ea'
3186 enddef
3187
3188 def Crash()
3189 result ..= 'Cb'
3190 sil! Error()
3191 result ..= 'Ca'
3192 enddef
3193
3194 Crash()
3195 assert_equal('CbEbEaCa', result)
3196 END
3197 CheckScriptSuccess(lines)
3198enddef
3199
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003200def Test_dict_member_with_silent()
3201 var lines =<< trim END
3202 vim9script
3203 g:result = 'none'
3204 var d: dict<any>
3205 def Func()
3206 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003207 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003208 catch
3209 endtry
3210 enddef
3211 silent! Func()
3212 assert_equal('0', g:result)
3213 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003214 END
3215 CheckScriptSuccess(lines)
3216enddef
3217
Bram Moolenaarf9041332021-01-21 19:41:16 +01003218def Test_skip_cmds_with_silent()
3219 var lines =<< trim END
3220 vim9script
3221
3222 def Func(b: bool)
3223 Crash()
3224 enddef
3225
3226 def Crash()
3227 sil! :/not found/d _
3228 sil! :/not found/put _
3229 enddef
3230
3231 Func(true)
3232 END
3233 CheckScriptSuccess(lines)
3234enddef
3235
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003236def Test_opfunc()
3237 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3238 def g:Opfunc(_: any): string
3239 setline(1, 'ASDF')
3240 return ''
3241 enddef
3242 new
3243 setline(1, 'asdf')
3244 feedkeys("\<F3>$", 'x')
3245 assert_equal('ASDF', getline(1))
3246
3247 bwipe!
3248 nunmap <F3>
3249enddef
3250
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003251func Test_opfunc_error()
3252 CheckScreendump
3253 call Run_Test_opfunc_error()
3254endfunc
3255
3256def Run_Test_opfunc_error()
3257 # test that the error from Opfunc() is displayed right away
3258 var lines =<< trim END
3259 vim9script
3260
3261 def Opfunc(type: string)
3262 try
3263 eval [][0]
3264 catch /nothing/ # error not caught
3265 endtry
3266 enddef
3267 &operatorfunc = Opfunc
3268 nnoremap <expr> l <SID>L()
3269 def L(): string
3270 return 'l'
3271 enddef
3272 'x'->repeat(10)->setline(1)
3273 feedkeys('g@l', 'n')
3274 feedkeys('llll')
3275 END
3276 call writefile(lines, 'XTest_opfunc_error')
3277
3278 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003279 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003280 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003281
3282 # clean up
3283 StopVimInTerminal(buf)
3284 delete('XTest_opfunc_error')
3285enddef
3286
Bram Moolenaar077a4232020-12-22 18:33:27 +01003287" this was crashing on exit
3288def Test_nested_lambda_in_closure()
3289 var lines =<< trim END
3290 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003291 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003292 def Outer()
3293 def g:Inner()
3294 echo map([1, 2, 3], {_, v -> v + 1})
3295 enddef
3296 g:Inner()
3297 enddef
3298 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003299 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003300 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003301 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003302 return
3303 endif
3304 assert_equal(['Done'], readfile('XnestedDone'))
3305 delete('XnestedDone')
3306enddef
3307
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003308def Test_check_func_arg_types()
3309 var lines =<< trim END
3310 vim9script
3311 def F1(x: string): string
3312 return x
3313 enddef
3314
3315 def F2(x: number): number
3316 return x + 1
3317 enddef
3318
3319 def G(g: func): dict<func>
3320 return {f: g}
3321 enddef
3322
3323 def H(d: dict<func>): string
3324 return d.f('a')
3325 enddef
3326 END
3327
3328 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3329 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3330enddef
3331
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003332def Test_list_any_type_checked()
3333 var lines =<< trim END
3334 vim9script
3335 def Foo()
3336 --decl--
3337 Bar(l)
3338 enddef
3339 def Bar(ll: list<dict<any>>)
3340 enddef
3341 Foo()
3342 END
3343 lines[2] = 'var l: list<any>'
3344 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3345
3346 lines[2] = 'var l: list<any> = []'
3347 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3348
3349 lines[2] = 'var l: list<any> = [11]'
3350 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3351enddef
3352
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003353def Test_compile_error()
3354 var lines =<< trim END
3355 def g:Broken()
3356 echo 'a' + {}
3357 enddef
3358 call g:Broken()
3359 END
3360 # First call: compilation error
3361 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3362
3363 # Second call won't try compiling again
3364 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003365 delfunc g:Broken
3366
3367 # No error when compiling with :silent!
3368 lines =<< trim END
3369 def g:Broken()
3370 echo 'a' + []
3371 enddef
3372 silent! defcompile
3373 END
3374 CheckScriptSuccess(lines)
3375
3376 # Calling the function won't try compiling again
3377 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3378 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003379enddef
3380
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003381def Test_ignored_argument()
3382 var lines =<< trim END
3383 vim9script
3384 def Ignore(_, _): string
3385 return 'yes'
3386 enddef
3387 assert_equal('yes', Ignore(1, 2))
3388
3389 func Ok(_)
3390 return a:_
3391 endfunc
3392 assert_equal('ok', Ok('ok'))
3393
3394 func Oktoo()
3395 let _ = 'too'
3396 return _
3397 endfunc
3398 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003399
3400 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003401 END
3402 CheckScriptSuccess(lines)
3403
3404 lines =<< trim END
3405 def Ignore(_: string): string
3406 return _
3407 enddef
3408 defcompile
3409 END
3410 CheckScriptFailure(lines, 'E1181:', 1)
3411
3412 lines =<< trim END
3413 var _ = 1
3414 END
3415 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003416
3417 lines =<< trim END
3418 var x = _
3419 END
3420 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003421enddef
3422
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003423def Test_too_many_arguments()
3424 var lines =<< trim END
3425 echo [0, 1, 2]->map(() => 123)
3426 END
3427 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3428
3429 lines =<< trim END
3430 echo [0, 1, 2]->map((_) => 123)
3431 END
3432 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3433enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003434
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003435def Test_closing_brace_at_start_of_line()
3436 var lines =<< trim END
3437 def Func()
3438 enddef
3439 Func(
3440 )
3441 END
3442 call CheckDefAndScriptSuccess(lines)
3443enddef
3444
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003445func CreateMydict()
3446 let g:mydict = {}
3447 func g:mydict.afunc()
3448 let g:result = self.key
3449 endfunc
3450endfunc
3451
3452def Test_numbered_function_reference()
3453 CreateMydict()
3454 var output = execute('legacy func g:mydict.afunc')
3455 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3456 execute 'function(' .. funcName .. ', [], {key: 42})()'
3457 # check that the function still exists
3458 assert_equal(output, execute('legacy func g:mydict.afunc'))
3459 unlet g:mydict
3460enddef
3461
Bram Moolenaar20677332021-06-06 17:02:53 +02003462if has('python3')
3463 def Test_python3_heredoc()
3464 py3 << trim EOF
3465 import vim
3466 vim.vars['didit'] = 'yes'
3467 EOF
3468 assert_equal('yes', g:didit)
3469
3470 python3 << trim EOF
3471 import vim
3472 vim.vars['didit'] = 'again'
3473 EOF
3474 assert_equal('again', g:didit)
3475 enddef
3476endif
3477
3478" This messes up syntax highlight, keep near the end.
3479if has('lua')
3480 def Test_lua_heredoc()
3481 g:d = {}
3482 lua << trim EOF
3483 x = vim.eval('g:d')
3484 x['key'] = 'val'
3485 EOF
3486 assert_equal('val', g:d.key)
3487 enddef
3488endif
3489
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003490
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003491" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker