blob: 12a4c5976da84fe444a5296466cf9243dc03f3db [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()
494 CheckDefAndScriptFailure2(["call Test ('text')"], 'E476:', 'E1068:')
495enddef
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 Moolenaar04b12692020-05-04 23:24:44 +0200550enddef
551
Bram Moolenaarcef12702021-01-04 14:09:43 +0100552def FuncWithComment( # comment
553 a: number, #comment
554 b: bool, # comment
555 c: string) #comment
556 assert_equal(4, a)
557 assert_equal(true, b)
558 assert_equal('yes', c)
559enddef
560
561def Test_func_with_comments()
562 FuncWithComment(4, true, 'yes')
563
564 var lines =<< trim END
565 def Func(# comment
566 arg: string)
567 enddef
568 END
569 CheckScriptFailure(lines, 'E125:', 1)
570
571 lines =<< trim END
572 def Func(
573 arg: string# comment
574 )
575 enddef
576 END
577 CheckScriptFailure(lines, 'E475:', 2)
578
579 lines =<< trim END
580 def Func(
581 arg: string
582 )# comment
583 enddef
584 END
585 CheckScriptFailure(lines, 'E488:', 3)
586enddef
587
Bram Moolenaar04b12692020-05-04 23:24:44 +0200588def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000589 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200590 return 'nested ' .. arg
591 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000592 NestedDef(':def')->assert_equal('nested :def')
593
594 func NestedFunc(arg)
595 return 'nested ' .. a:arg
596 endfunc
597 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200598
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200599 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
600 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
601
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200602 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
603 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200604
Bram Moolenaar54021752020-12-06 18:50:36 +0100605 var lines =<< trim END
606 def Outer()
607 def Inner()
608 # comment
609 enddef
610 def Inner()
611 enddef
612 enddef
613 END
614 CheckDefFailure(lines, 'E1073:')
615
616 lines =<< trim END
617 def Outer()
618 def Inner()
619 # comment
620 enddef
621 def! Inner()
622 enddef
623 enddef
624 END
625 CheckDefFailure(lines, 'E1117:')
626
627 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 lines =<< trim END
629 vim9script
630 var thecount = 0
631 if true
632 def Test(): number
633 def TheFunc(): number
634 thecount += 1
635 return thecount
636 enddef
637 return TheFunc()
638 enddef
639 endif
640 defcompile
641 assert_equal(1, Test())
642 assert_equal(2, Test())
643 END
644 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100645
646 # also works when "thecount" is inside the "if" block
647 lines =<< trim END
648 vim9script
649 if true
650 var thecount = 0
651 def Test(): number
652 def TheFunc(): number
653 thecount += 1
654 return thecount
655 enddef
656 return TheFunc()
657 enddef
658 endif
659 defcompile
660 assert_equal(1, Test())
661 assert_equal(2, Test())
662 END
663 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200664
665 lines =<< trim END
666 vim9script
667 def Outer()
668 def Inner()
669 echo 'hello'
670 enddef burp
671 enddef
672 defcompile
673 END
674 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200675enddef
676
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100677def Test_not_nested_function()
678 echo printf('%d',
679 function('len')('xxx'))
680enddef
681
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200682func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200683 call MyDefaultArgs()->assert_equal('string')
684 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200685 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200686endfunc
687
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200688def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200689 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200690 vim9script
691 def Outer()
692 def g:Inner(): string
693 return 'inner'
694 enddef
695 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200696 defcompile
697 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200698 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200699 delfunc g:Inner
700 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200701 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200702 delfunc g:Inner
703 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200704 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200705 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200706 END
707 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200708
709 lines =<< trim END
710 vim9script
711 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000712 func g:Inner()
713 return 'inner'
714 endfunc
715 enddef
716 defcompile
717 Outer()
718 g:Inner()->assert_equal('inner')
719 delfunc g:Inner
720 Outer()
721 g:Inner()->assert_equal('inner')
722 delfunc g:Inner
723 Outer()
724 g:Inner()->assert_equal('inner')
725 delfunc g:Inner
726 END
727 CheckScriptSuccess(lines)
728
729 lines =<< trim END
730 vim9script
731 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200732 def g:Inner(): string
733 return 'inner'
734 enddef
735 enddef
736 defcompile
737 Outer()
738 Outer()
739 END
740 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100741 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200742
743 lines =<< trim END
744 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100745 def Outer()
746 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100747 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100748 enddef
749 g:Inner()
750 enddef
751 Outer()
752 END
753 CheckScriptSuccess(lines)
754 delfunc g:Inner
755
756 lines =<< trim END
757 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200758 def Func()
759 echo 'script'
760 enddef
761 def Outer()
762 def Func()
763 echo 'inner'
764 enddef
765 enddef
766 defcompile
767 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000768 CheckScriptFailure(lines, "E1073:", 1)
769
770 lines =<< trim END
771 vim9script
772 def Func()
773 echo 'script'
774 enddef
775 def Func()
776 echo 'script'
777 enddef
778 END
779 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200780enddef
781
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100782def DefListAll()
783 def
784enddef
785
786def DefListOne()
787 def DefListOne
788enddef
789
790def DefListMatches()
791 def /DefList
792enddef
793
794def Test_nested_def_list()
795 var funcs = split(execute('call DefListAll()'), "\n")
796 assert_true(len(funcs) > 10)
797 assert_true(funcs->index('def DefListAll()') >= 0)
798
799 funcs = split(execute('call DefListOne()'), "\n")
800 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
801
802 funcs = split(execute('call DefListMatches()'), "\n")
803 assert_true(len(funcs) >= 3)
804 assert_true(funcs->index('def DefListAll()') >= 0)
805 assert_true(funcs->index('def DefListOne()') >= 0)
806 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100807
808 var lines =<< trim END
809 vim9script
810 def Func()
811 def +Func+
812 enddef
813 defcompile
814 END
815 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100816enddef
817
Bram Moolenaar333894b2020-08-01 18:53:07 +0200818def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200819 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200820 vim9script
821 def g:Func(): string
822 return 'global'
823 enddef
824 def Func(): string
825 return 'local'
826 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200827 g:Func()->assert_equal('global')
828 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100829 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200830 END
831 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200832
833 lines =<< trim END
834 vim9script
835 def g:Funcy()
836 echo 'funcy'
837 enddef
838 s:Funcy()
839 END
840 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200841enddef
842
Bram Moolenaar0f769812020-09-12 18:32:34 +0200843def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200844 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200845 vim9script
846 def g:Gfunc(): string
847 return 'global'
848 enddef
849 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200850 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200851 return Gfunc('testing')
852 enddef
853 g:Gfunc()->assert_equal('global')
854 AnotherFunc()->assert_equal(7)
855 delfunc g:Gfunc
856 END
857 CheckScriptSuccess(lines)
858
859 lines =<< trim END
860 vim9script
861 def g:Func(): string
862 return 'global'
863 enddef
864 def AnotherFunc()
865 g:Func = function('len')
866 enddef
867 AnotherFunc()
868 END
869 CheckScriptFailure(lines, 'E705:')
870 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200871
872 # global function is found without g: prefix
873 lines =<< trim END
874 vim9script
875 def g:Func(): string
876 return 'global'
877 enddef
878 def AnotherFunc(): string
879 return Func()
880 enddef
881 assert_equal('global', AnotherFunc())
882 delfunc g:Func
883 END
884 CheckScriptSuccess(lines)
885
886 lines =<< trim END
887 vim9script
888 def g:Func(): string
889 return 'global'
890 enddef
891 assert_equal('global', Func())
892 delfunc g:Func
893 END
894 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200895enddef
896
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200897func TakesOneArg(arg)
898 echo a:arg
899endfunc
900
901def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200902 CheckDefFailure(['TakesOneArg()'], 'E119:')
903 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
904 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
905 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200906
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200907 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200908 vim9script
909 def Func(s: string)
910 echo s
911 enddef
912 Func([])
913 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200914 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200915
916 lines =<< trim END
917 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100918 var name = 'piet'
919 def FuncOne(name: string)
920 echo nr
921 enddef
922 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100923 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100924
925 lines =<< trim END
926 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200927 def FuncOne(nr: number)
928 echo nr
929 enddef
930 def FuncTwo()
931 FuncOne()
932 enddef
933 defcompile
934 END
935 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200936 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200937 try
938 source Xscript
939 catch
940 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
941 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
942 didCatch = true
943 endtry
944 assert_true(didCatch)
945
946 lines =<< trim END
947 vim9script
948 def FuncOne(nr: number)
949 echo nr
950 enddef
951 def FuncTwo()
952 FuncOne(1, 2)
953 enddef
954 defcompile
955 END
956 writefile(lines, 'Xscript')
957 didCatch = false
958 try
959 source Xscript
960 catch
961 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
962 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
963 didCatch = true
964 endtry
965 assert_true(didCatch)
966
967 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200968enddef
969
Bram Moolenaar50824712020-12-20 21:10:17 +0100970def Test_call_funcref_wrong_args()
971 var head =<< trim END
972 vim9script
973 def Func3(a1: string, a2: number, a3: list<number>)
974 echo a1 .. a2 .. a3[0]
975 enddef
976 def Testme()
977 var funcMap: dict<func> = {func: Func3}
978 END
979 var tail =<< trim END
980 enddef
981 Testme()
982 END
983 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
984
985 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
986 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100987
988 var lines =<< trim END
989 vim9script
990 var Ref: func(number): any
991 Ref = (j) => !j
992 echo Ref(false)
993 END
994 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
995
996 lines =<< trim END
997 vim9script
998 var Ref: func(number): any
999 Ref = (j) => !j
1000 call Ref(false)
1001 END
1002 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001003enddef
1004
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001005def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001006 var lines =<< trim END
1007 var Callback = (..._) => 'anything'
1008 assert_equal('anything', Callback())
1009 assert_equal('anything', Callback(1))
1010 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001011
1012 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001013 END
1014 CheckDefAndScriptSuccess(lines)
1015
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001016 CheckDefFailure(['echo ((i) => 0)()'],
1017 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001018
Bram Moolenaar2a389082021-04-09 20:24:31 +02001019 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001020 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001021 echo Ref(1, 'x')
1022 END
1023 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001024
1025 lines =<< trim END
1026 var Ref: func(job, string, number)
1027 Ref = (x, y) => 0
1028 END
1029 CheckDefAndScriptFailure(lines, 'E1012:')
1030
1031 lines =<< trim END
1032 var Ref: func(job, string)
1033 Ref = (x, y, z) => 0
1034 END
1035 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001036
1037 lines =<< trim END
1038 var one = 1
1039 var l = [1, 2, 3]
1040 echo map(l, (one) => one)
1041 END
1042 CheckDefFailure(lines, 'E1167:')
1043 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1044
1045 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001046 var Ref: func(any, ?any): bool
1047 Ref = (_, y = 1) => false
1048 END
1049 CheckDefAndScriptFailure(lines, 'E1172:')
1050
1051 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001052 var a = 0
1053 var b = (a == 0 ? 1 : 2)
1054 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001055 var txt = 'a'
1056 b = (txt =~ 'x' ? 1 : 2)
1057 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001058 END
1059 CheckDefAndScriptSuccess(lines)
1060
1061 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001062 def ShadowLocal()
1063 var one = 1
1064 var l = [1, 2, 3]
1065 echo map(l, (one) => one)
1066 enddef
1067 END
1068 CheckDefFailure(lines, 'E1167:')
1069
1070 lines =<< trim END
1071 def Shadowarg(one: number)
1072 var l = [1, 2, 3]
1073 echo map(l, (one) => one)
1074 enddef
1075 END
1076 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001077
1078 lines =<< trim END
1079 echo ((a) => a)('aa', 'bb')
1080 END
1081 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001082
1083 lines =<< trim END
1084 echo 'aa'->((a) => a)('bb')
1085 END
1086 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1087 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001088enddef
1089
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001090def Test_lambda_line_nr()
1091 var lines =<< trim END
1092 vim9script
1093 # comment
1094 # comment
1095 var id = timer_start(1'000, (_) => 0)
1096 var out = execute('verbose ' .. timer_info(id)[0].callback
1097 ->string()
1098 ->substitute("('\\|')", ' ', 'g'))
1099 assert_match('Last set from .* line 4', out)
1100 END
1101 CheckScriptSuccess(lines)
1102enddef
1103
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001104def FilterWithCond(x: string, Cond: func(string): bool): bool
1105 return Cond(x)
1106enddef
1107
Bram Moolenaar0346b792021-01-31 22:18:29 +01001108def Test_lambda_return_type()
1109 var lines =<< trim END
1110 var Ref = (): => 123
1111 END
1112 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001113
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001114 # no space before the return type
1115 lines =<< trim END
1116 var Ref = (x):number => x + 1
1117 END
1118 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1119
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001120 # this works
1121 for x in ['foo', 'boo']
1122 echo FilterWithCond(x, (v) => v =~ '^b')
1123 endfor
1124
1125 # this fails
1126 lines =<< trim END
1127 echo FilterWithCond('foo', (v) => v .. '^b')
1128 END
1129 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001130
1131 lines =<< trim END
1132 var Lambda1 = (x) => {
1133 return x
1134 }
1135 assert_equal('asdf', Lambda1('asdf'))
1136 var Lambda2 = (x): string => {
1137 return x
1138 }
1139 assert_equal('foo', Lambda2('foo'))
1140 END
1141 CheckDefAndScriptSuccess(lines)
1142
1143 lines =<< trim END
1144 var Lambda = (x): string => {
1145 return x
1146 }
1147 echo Lambda(['foo'])
1148 END
1149 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001150enddef
1151
Bram Moolenaar709664c2020-12-12 14:33:41 +01001152def Test_lambda_uses_assigned_var()
1153 CheckDefSuccess([
1154 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001155 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001156enddef
1157
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001158def Test_pass_legacy_lambda_to_def_func()
1159 var lines =<< trim END
1160 vim9script
1161 func Foo()
1162 eval s:Bar({x -> 0})
1163 endfunc
1164 def Bar(y: any)
1165 enddef
1166 Foo()
1167 END
1168 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001169
1170 lines =<< trim END
1171 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001172 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001173 enddef
1174 legacy call g:TestFunc({-> 0})
1175 delfunc g:TestFunc
1176
1177 def g:TestFunc(f: func(number))
1178 enddef
1179 legacy call g:TestFunc({nr -> 0})
1180 delfunc g:TestFunc
1181 END
1182 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001183enddef
1184
Bram Moolenaar844fb642021-10-23 13:32:30 +01001185def Test_lambda_in_reduce_line_break()
1186 # this was using freed memory
1187 var lines =<< trim END
1188 vim9script
1189 const result: dict<number> =
1190 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1191 ->reduce((acc, val) => {
1192 if has_key(acc, val)
1193 acc[val] += 1
1194 return acc
1195 else
1196 acc[val] = 1
1197 return acc
1198 endif
1199 }, {})
1200 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1201 END
1202 CheckScriptSuccess(lines)
1203enddef
1204
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001205def Test_set_opfunc_to_lambda()
1206 var lines =<< trim END
1207 vim9script
1208 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1209 def CountSpaces(type = ''): string
1210 if type == ''
1211 &operatorfunc = (t) => CountSpaces(t)
1212 return 'g@'
1213 endif
1214 normal! '[V']y
1215 g:result = getreg('"')->count(' ')
1216 return ''
1217 enddef
1218 new
1219 'a b c d e'->setline(1)
1220 feedkeys("\<F4>", 'x')
1221 assert_equal(4, g:result)
1222 bwipe!
1223 END
1224 CheckScriptSuccess(lines)
1225enddef
1226
Bram Moolenaaref082e12021-12-12 21:02:03 +00001227def Test_set_opfunc_to_global_function()
1228 var lines =<< trim END
1229 vim9script
1230 def g:CountSpaces(type = ''): string
1231 normal! '[V']y
1232 g:result = getreg('"')->count(' ')
1233 return ''
1234 enddef
1235 &operatorfunc = g:CountSpaces
1236 new
1237 'a b c d e'->setline(1)
1238 feedkeys("g@_", 'x')
1239 assert_equal(4, g:result)
1240 bwipe!
1241 END
1242 CheckScriptSuccess(lines)
1243 &operatorfunc = ''
1244enddef
1245
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001246def Test_use_script_func_name_with_prefix()
1247 var lines =<< trim END
1248 vim9script
1249 func s:Getit()
1250 return 'it'
1251 endfunc
1252 var Fn = s:Getit
1253 assert_equal('it', Fn())
1254 END
1255 CheckScriptSuccess(lines)
1256enddef
1257
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001258def Test_lambda_type_allocated()
1259 # Check that unreferencing a partial using a lambda can use the variable type
1260 # after the lambda has been freed and does not leak memory.
1261 var lines =<< trim END
1262 vim9script
1263
1264 func MyomniFunc1(val, findstart, base)
1265 return a:findstart ? 0 : []
1266 endfunc
1267
1268 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1269 &omnifunc = Lambda
1270 Lambda = (a, b) => MyomniFunc1(20, a, b)
1271 &omnifunc = string(Lambda)
1272 Lambda = (a, b) => strlen(a)
1273 END
1274 CheckScriptSuccess(lines)
1275enddef
1276
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001277" Default arg and varargs
1278def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001279 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001280 for s in rest
1281 res ..= ',' .. s
1282 endfor
1283 return res
1284enddef
1285
1286def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001287 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001288 MyDefVarargs('one')->assert_equal('one,foo')
1289 MyDefVarargs('one', 'two')->assert_equal('one,two')
1290 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001291 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001292 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001293 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001294 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001295
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001296 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001297 vim9script
1298 def Func(...l: list<string>)
1299 echo l
1300 enddef
1301 Func('a', 'b', 'c')
1302 END
1303 CheckScriptSuccess(lines)
1304
1305 lines =<< trim END
1306 vim9script
1307 def Func(...l: list<string>)
1308 echo l
1309 enddef
1310 Func()
1311 END
1312 CheckScriptSuccess(lines)
1313
1314 lines =<< trim END
1315 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001316 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001317 echo l
1318 enddef
1319 Func(0)
1320 END
1321 CheckScriptSuccess(lines)
1322
1323 lines =<< trim END
1324 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001325 def Func(...l: any)
1326 echo l
1327 enddef
1328 Func(0)
1329 END
1330 CheckScriptFailure(lines, 'E1180:', 2)
1331
1332 lines =<< trim END
1333 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001334 def Func(..._l: list<string>)
1335 echo _l
1336 enddef
1337 Func('a', 'b', 'c')
1338 END
1339 CheckScriptSuccess(lines)
1340
1341 lines =<< trim END
1342 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001343 def Func(...l: list<string>)
1344 echo l
1345 enddef
1346 Func(1, 2, 3)
1347 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001348 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001349
1350 lines =<< trim END
1351 vim9script
1352 def Func(...l: list<string>)
1353 echo l
1354 enddef
1355 Func('a', 9)
1356 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001357 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001358
1359 lines =<< trim END
1360 vim9script
1361 def Func(...l: list<string>)
1362 echo l
1363 enddef
1364 Func(1, 'a')
1365 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001366 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001367
1368 lines =<< trim END
1369 vim9script
1370 def Func( # some comment
1371 ...l = []
1372 )
1373 echo l
1374 enddef
1375 END
1376 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001377
1378 lines =<< trim END
1379 vim9script
1380 def DoIt()
1381 g:Later('')
1382 enddef
1383 defcompile
1384 def g:Later(...l: list<number>)
1385 enddef
1386 DoIt()
1387 END
1388 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001389enddef
1390
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001391let s:value = ''
1392
1393def FuncOneDefArg(opt = 'text')
1394 s:value = opt
1395enddef
1396
1397def FuncTwoDefArg(nr = 123, opt = 'text'): string
1398 return nr .. opt
1399enddef
1400
1401def FuncVarargs(...arg: list<string>): string
1402 return join(arg, ',')
1403enddef
1404
1405def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001406 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001407 RefDefArg = FuncOneDefArg
1408 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001409 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001410 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001411 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001412
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001413 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001414 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001415 RefDef2Arg()->assert_equal('123text')
1416 RefDef2Arg(99)->assert_equal('99text')
1417 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001418
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001419 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1420 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001421
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001422 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001423 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001424 RefVarargs()->assert_equal('')
1425 RefVarargs('one')->assert_equal('one')
1426 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001427
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001428 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1429 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001430enddef
1431
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001432" Only varargs
1433def MyVarargsOnly(...args: list<string>): string
1434 return join(args, ',')
1435enddef
1436
1437def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001438 MyVarargsOnly()->assert_equal('')
1439 MyVarargsOnly('one')->assert_equal('one')
1440 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001441 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1442 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001443enddef
1444
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001445def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001446 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001447 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001448 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001449enddef
1450
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001451def DictArg(arg: dict<string>)
1452 arg['key'] = 'value'
1453enddef
1454
1455def ListArg(arg: list<string>)
1456 arg[0] = 'value'
1457enddef
1458
1459def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001460 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001461 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001462 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001463 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001464 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001465 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001466 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001467
Bram Moolenaard2c61702020-09-06 15:58:36 +02001468 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001469 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001470enddef
1471
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001472" These argument names are reserved in legacy functions.
1473def WithReservedNames(firstline: string, lastline: string): string
1474 return firstline .. lastline
1475enddef
1476
1477def Test_argument_names()
1478 assert_equal('OK', WithReservedNames('O', 'K'))
1479enddef
1480
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001481def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001482 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001483 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001484enddef
1485
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001486func DefinedLater(arg)
1487 return a:arg
1488endfunc
1489
1490def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001491 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001492 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001493 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001494
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001495 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001496 vim9script
1497 def RetNumber(): number
1498 return 123
1499 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001500 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001501 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001502 END
1503 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001504
1505 lines =<< trim END
1506 vim9script
1507 def RetNumber(): number
1508 return 123
1509 enddef
1510 def Bar(F: func: number): number
1511 return F()
1512 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001513 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001514 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001515 END
1516 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001517
1518 lines =<< trim END
1519 vim9script
1520 def UseNumber(nr: number)
1521 echo nr
1522 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001523 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001524 Funcref(123)
1525 END
1526 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001527
1528 lines =<< trim END
1529 vim9script
1530 def UseNumber(nr: number)
1531 echo nr
1532 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001533 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001534 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001535 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001536
1537 lines =<< trim END
1538 vim9script
1539 def EchoNr(nr = 34)
1540 g:echo = nr
1541 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001542 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001543 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001544 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001545 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001546 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001547 END
1548 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001549
1550 lines =<< trim END
1551 vim9script
1552 def EchoList(...l: list<number>)
1553 g:echo = l
1554 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001555 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001556 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001557 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001558 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001559 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001560 END
1561 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001562
1563 lines =<< trim END
1564 vim9script
1565 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1566 g:optarg = opt
1567 g:listarg = l
1568 return nr
1569 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001570 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001571 Funcref(10)->assert_equal(10)
1572 g:optarg->assert_equal(12)
1573 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001574
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001575 Funcref(11, 22)->assert_equal(11)
1576 g:optarg->assert_equal(22)
1577 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001578
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001579 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1580 g:optarg->assert_equal(18)
1581 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001582 END
1583 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001584enddef
1585
1586let SomeFunc = function('len')
1587let NotAFunc = 'text'
1588
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001589def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001590 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001591 var Ref1: func(bool): string
1592 var Ref2: func(bool): number
1593 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001594 Ref3 = g:cond ? Ref1 : Ref2
1595
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001596 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001597 var Refa1: func(bool): number
1598 var Refa2: func(bool, number): number
1599 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001600 Refa3 = g:cond ? Refa1 : Refa2
1601
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001602 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001603 var Refb1: func(bool, string): number
1604 var Refb2: func(string, number): number
1605 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001606 Refb3 = g:cond ? Refb1 : Refb2
1607enddef
1608
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001609def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001610 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001611enddef
1612
1613def DefinedEvenLater(arg: string): string
1614 return arg
1615enddef
1616
1617def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001618 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001619 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001620enddef
1621
1622def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001623 CheckScriptFailure([
1624 'def Func(): number',
1625 'return "a"',
1626 'enddef',
1627 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001628 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001629 CheckScriptFailure([
1630 'def Func(): string',
1631 'return 1',
1632 'enddef',
1633 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001634 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001635 CheckScriptFailure([
1636 'def Func(): void',
1637 'return "a"',
1638 'enddef',
1639 'defcompile'],
1640 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001641 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001642 CheckScriptFailure([
1643 'def Func()',
1644 'return "a"',
1645 'enddef',
1646 'defcompile'],
1647 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001648 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001649
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001650 CheckScriptFailure([
1651 'def Func(): number',
1652 'return',
1653 'enddef',
1654 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001655 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001656
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001657 CheckScriptFailure([
1658 'def Func():number',
1659 'return 123',
1660 'enddef',
1661 'defcompile'], 'E1069:')
1662 delfunc! g:Func
1663
1664 CheckScriptFailure([
1665 'def Func() :number',
1666 'return 123',
1667 'enddef',
1668 'defcompile'], 'E1059:')
1669 delfunc! g:Func
1670
1671 CheckScriptFailure([
1672 'def Func() : number',
1673 'return 123',
1674 'enddef',
1675 'defcompile'], 'E1059:')
1676 delfunc! g:Func
1677
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001678 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001679 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001680 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001681 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001682 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001683 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001684
1685 CheckScriptFailure([
1686 'vim9script',
1687 'def FuncB()',
1688 ' return 123',
1689 'enddef',
1690 'def FuncA()',
1691 ' FuncB()',
1692 'enddef',
1693 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001694enddef
1695
1696def Test_arg_type_wrong()
1697 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001698 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001699 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001700 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001701 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1702 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001703enddef
1704
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001705def Test_white_space_before_comma()
1706 var lines =<< trim END
1707 vim9script
1708 def Func(a: number , b: number)
1709 enddef
1710 END
1711 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001712 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001713enddef
1714
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001715def Test_white_space_after_comma()
1716 var lines =<< trim END
1717 vim9script
1718 def Func(a: number,b: number)
1719 enddef
1720 END
1721 CheckScriptFailure(lines, 'E1069:')
1722
1723 # OK in legacy function
1724 lines =<< trim END
1725 vim9script
1726 func Func(a,b)
1727 endfunc
1728 END
1729 CheckScriptSuccess(lines)
1730enddef
1731
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001732def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001733 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001734 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001735 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001736 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001737 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001738 enddef
1739 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001740 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001741
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001742 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001743 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001744 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001745
Bram Moolenaar67979662020-06-20 22:50:47 +02001746 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001747 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001748 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001749
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001750 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001751 def ListFunc(arg: list<number>)
1752 listvar = arg
1753 enddef
1754 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001755 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001756
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001757 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001758 def DictFunc(arg: dict<number>)
1759 dictvar = arg
1760 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001761 {a: 1, b: 2}->DictFunc()
1762 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001763 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001764 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001765 enddef
1766 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001767 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001768
Bram Moolenaare0de1712020-12-02 17:36:54 +01001769 {a: 3, b: 4}->DictFunc()
1770 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001771
1772 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001773 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001774 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001775 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001776
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001777 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001778 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001779 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001780 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001781 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001782 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001783
1784 def UseString()
1785 'xyork'->MyFunc()
1786 enddef
1787 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001788 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001789
Bram Moolenaar10409562020-07-29 20:00:38 +02001790 def UseString2()
1791 "knife"->MyFunc()
1792 enddef
1793 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001794 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001795
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001796 # prepending a colon makes it a mark
1797 new
1798 setline(1, ['aaa', 'bbb', 'ccc'])
1799 normal! 3Gmt1G
1800 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001801 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001802 bwipe!
1803
Bram Moolenaare6b53242020-07-01 17:28:33 +02001804 MyFunc(
1805 'continued'
1806 )
1807 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001808 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001809 )
1810
1811 call MyFunc(
1812 'more'
1813 ..
1814 'lines'
1815 )
1816 assert_equal(
1817 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001818 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001819 END
1820 writefile(lines, 'Xcall.vim')
1821 source Xcall.vim
1822 delete('Xcall.vim')
1823enddef
1824
1825def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001826 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001827 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001828 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001829 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001830 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001831 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001832 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001834 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001835enddef
1836
Bram Moolenaar65b95452020-07-19 14:03:09 +02001837def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001838 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001839 vim9script
1840 def MyFunc(arg: string)
1841 echo arg
1842 enddef
1843 MyFunc(1234)
1844 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001845 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001846enddef
1847
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001848def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001849 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001850 vim9script
1851 const var = ''
1852 def MyFunc(arg: string)
1853 var = 'asdf'
1854 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001855 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001856 END
1857 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001858 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001859 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001860
1861 lines =<< trim END
1862 const g:Aconst = 77
1863 def Change()
1864 # comment
1865 g:Aconst = 99
1866 enddef
1867 call Change()
1868 unlet g:Aconst
1869 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001870 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001871enddef
1872
1873" Test that inside :function a Python function can be defined, :def is not
1874" recognized.
1875func Test_function_python()
1876 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001877 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001878 execute py "<< EOF"
1879def do_something():
1880 return 1
1881EOF
1882endfunc
1883
1884def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001885 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001886 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001887 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001888 echo 'hello'
1889 enddef
1890
1891 def CallGoneSoon()
1892 GoneSoon()
1893 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001894 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001895
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001896 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897 CallGoneSoon()
1898 END
1899 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001900 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1901 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001902
1903 delete('XToDelFunc')
1904enddef
1905
1906def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001907 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001908 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001909 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001910 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001911 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001912 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001913 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001914 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001915 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001917 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001918 g:Func1()->assert_equal('Func1')
1919 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001920
1921 delfunc! Func0
1922 delfunc! Func1
1923 delfunc! Func2
1924enddef
1925
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001926def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001927 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001928 vim9script
1929 func Func(arg)
1930 echo a:arg
1931 endfunc
1932 Func('text')
1933 END
1934 writefile(lines, 'XVim9Func')
1935 so XVim9Func
1936
1937 delete('XVim9Func')
1938enddef
1939
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001940let s:funcResult = 0
1941
1942def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001943 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001944enddef
1945
1946def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001947 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001948 return 1234
1949enddef
1950
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001951def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001952 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001953 return 'text'
1954enddef
1955
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001956def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001957 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958enddef
1959
1960def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001961 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001962 return arg
1963enddef
1964
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001965def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001966 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001967enddef
1968
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001969def FuncOneArgRetString(arg: string): string
1970 return arg
1971enddef
1972
Bram Moolenaar89228602020-04-05 22:14:54 +02001973def FuncOneArgRetAny(arg: any): any
1974 return arg
1975enddef
1976
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001977def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001978 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001979 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001980 Ref1 = FuncNoArgNoRet
1981 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001982 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001983
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001984 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001985 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001986 Ref2 = FuncNoArgNoRet
1987 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001988 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001989
Bram Moolenaar53900992020-08-22 19:02:02 +02001990 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001991 Ref2 = FuncOneArgNoRet
1992 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001993 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001994
Bram Moolenaar53900992020-08-22 19:02:02 +02001995 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001996 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001997 Ref2()->assert_equal(1234)
1998 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001999
Bram Moolenaar53900992020-08-22 19:02:02 +02002000 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002001 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002002 Ref2(13)->assert_equal(13)
2003 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002004enddef
2005
Bram Moolenaar9978d472020-07-05 16:01:56 +02002006def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002007 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002008 for n in repeat([1], 3)
2009 res += n
2010 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002011 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002012
2013 res = 0
2014 for n in add([1, 2], 3)
2015 res += n
2016 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002017 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002018enddef
2019
Bram Moolenaar846178a2020-07-05 17:04:13 +02002020def Test_argv_return_type()
2021 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002022 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002023 for name in argv()
2024 res ..= name
2025 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002026 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002027enddef
2028
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002029def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002030 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002031 RefVoid = FuncNoArgNoRet
2032 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002033 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2034 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002035
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002036 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002037 RefAny = FuncNoArgRetNumber
2038 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002039 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2040 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002041
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002042 var RefAnyNoArgs: func: any = RefAny
2043
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002044 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002045 RefNr = FuncNoArgRetNumber
2046 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002047 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2048 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002049
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002050 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002051 RefStr = FuncNoArgRetString
2052 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002053 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2054 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002055enddef
2056
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002057def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002058 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002059
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002060 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2061 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2062 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2063 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2064 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2065 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 +02002066
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002067 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2068 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2069 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:')
2070 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002071enddef
2072
Bram Moolenaar89228602020-04-05 22:14:54 +02002073def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002074 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002075 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002076 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002077
2078 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002079 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002080
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002081 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002082 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002083 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002084
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002085 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002086enddef
2087
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002088def Test_func_common_type()
2089 def FuncOne(n: number): number
2090 return n
2091 enddef
2092 def FuncTwo(s: string): number
2093 return len(s)
2094 enddef
2095 def FuncThree(n: number, s: string): number
2096 return n + len(s)
2097 enddef
2098 var list = [FuncOne, FuncTwo, FuncThree]
2099 assert_equal(8, list[0](8))
2100 assert_equal(4, list[1]('word'))
2101 assert_equal(7, list[2](3, 'word'))
2102enddef
2103
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002104def MultiLine(
2105 arg1: string,
2106 arg2 = 1234,
2107 ...rest: list<string>
2108 ): string
2109 return arg1 .. arg2 .. join(rest, '-')
2110enddef
2111
Bram Moolenaar2c330432020-04-13 14:41:35 +02002112def MultiLineComment(
2113 arg1: string, # comment
2114 arg2 = 1234, # comment
2115 ...rest: list<string> # comment
2116 ): string # comment
2117 return arg1 .. arg2 .. join(rest, '-')
2118enddef
2119
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002120def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002121 MultiLine('text')->assert_equal('text1234')
2122 MultiLine('text', 777)->assert_equal('text777')
2123 MultiLine('text', 777, 'one')->assert_equal('text777one')
2124 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002125enddef
2126
Bram Moolenaar23e03252020-04-12 22:22:31 +02002127func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002128 call MultiLine('text')->assert_equal('text1234')
2129 call MultiLine('text', 777)->assert_equal('text777')
2130 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2131 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002132endfunc
2133
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002134
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002135" When using CheckScriptFailure() for the below test, E1010 is generated instead
2136" of E1056.
2137func Test_E1056_1059()
2138 let caught_1056 = 0
2139 try
2140 def F():
2141 return 1
2142 enddef
2143 catch /E1056:/
2144 let caught_1056 = 1
2145 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002146 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002147
2148 let caught_1059 = 0
2149 try
2150 def F5(items : list)
2151 echo 'a'
2152 enddef
2153 catch /E1059:/
2154 let caught_1059 = 1
2155 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002156 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002157endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002158
Bram Moolenaar015f4262020-05-05 21:25:22 +02002159func DelMe()
2160 echo 'DelMe'
2161endfunc
2162
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002163def Test_error_reporting()
2164 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002165 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002166 " comment
2167 def Func()
2168 # comment
2169 # comment
2170 invalid
2171 enddef
2172 defcompile
2173 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002174 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002175 try
2176 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002177 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002178 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002179 v:exception->assert_match('Invalid command: invalid')
2180 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002181 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002182 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002183
2184 # comment lines after the start of the function
2185 lines =<< trim END
2186 " comment
2187 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002188 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002189 # comment
2190 # comment
2191 invalid
2192 enddef
2193 defcompile
2194 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002195 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002196 try
2197 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002198 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002199 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002200 v:exception->assert_match('Invalid command: invalid')
2201 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002202 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002203 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002204
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002205 lines =<< trim END
2206 vim9script
2207 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002208 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002209 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002210 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002211 enddef
2212 defcompile
2213 Func()
2214 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002215 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002216 try
2217 source Xdef
2218 assert_report('should have failed')
2219 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002220 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002221 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002222 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002223
Bram Moolenaar08052222020-09-14 17:04:31 +02002224 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002225enddef
2226
Bram Moolenaar015f4262020-05-05 21:25:22 +02002227def Test_deleted_function()
2228 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002229 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002230 'delfunc g:DelMe',
2231 'echo RefMe()'], 'E117:')
2232enddef
2233
2234def Test_unknown_function()
2235 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002236 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002237 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002238enddef
2239
Bram Moolenaar328eac22021-01-07 19:23:08 +01002240def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002241 return Ref('more')
2242enddef
2243
2244def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002245 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002246 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002247enddef
2248
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002249def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002250 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002251 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002252enddef
2253
2254def Test_closure_ref_after_return()
2255 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002256 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002257 unlet g:Ref
2258enddef
2259
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002260def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002261 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002262 g:Extend = (s) => local->add(s)
2263 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002264enddef
2265
2266def Test_closure_two_refs()
2267 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002268 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002269 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002270 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002271 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002272 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002273
2274 unlet g:Extend
2275 unlet g:Read
2276enddef
2277
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002278def ReadRef(Ref: func(): list<string>): string
2279 return join(Ref(), ' ')
2280enddef
2281
Bram Moolenaar5e654232020-09-16 15:22:00 +02002282def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002283 Ref(add)
2284enddef
2285
2286def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002287 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002288 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002289 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002290 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002291 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002292 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002293
2294 unlet g:Extend
2295 unlet g:Read
2296enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002297
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002298def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002299 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002300 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002301enddef
2302
2303def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002304 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002305 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002306enddef
2307
2308def Test_closure_using_argument()
2309 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002310 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002311
2312 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002313 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002314
2315 unlet g:UseArg
2316 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002317
2318 var lines =<< trim END
2319 vim9script
2320 def Test(Fun: func(number): number): list<number>
2321 return map([1, 2, 3], (_, i) => Fun(i))
2322 enddef
2323 def Inc(nr: number): number
2324 return nr + 2
2325 enddef
2326 assert_equal([3, 4, 5], Test(Inc))
2327 END
2328 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002329enddef
2330
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002331def MakeGetAndAppendRefs()
2332 var local = 'a'
2333
2334 def Append(arg: string)
2335 local ..= arg
2336 enddef
2337 g:Append = Append
2338
2339 def Get(): string
2340 return local
2341 enddef
2342 g:Get = Get
2343enddef
2344
2345def Test_closure_append_get()
2346 MakeGetAndAppendRefs()
2347 g:Get()->assert_equal('a')
2348 g:Append('-b')
2349 g:Get()->assert_equal('a-b')
2350 g:Append('-c')
2351 g:Get()->assert_equal('a-b-c')
2352
2353 unlet g:Append
2354 unlet g:Get
2355enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002356
Bram Moolenaar04b12692020-05-04 23:24:44 +02002357def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002358 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002359 def Closure(arg: string): string
2360 return local .. arg
2361 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002362 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002363enddef
2364
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002365func GetResult(Ref)
2366 return a:Ref('some')
2367endfunc
2368
2369def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002370 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002371 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002372 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002373enddef
2374
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002375def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002376 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002377 vim9script
2378 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002379 var name = 0
2380 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002381 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002382 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002383 enddef
2384 Func()
2385 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002386 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002387enddef
2388
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002389def Test_nested_closure_used()
2390 var lines =<< trim END
2391 vim9script
2392 def Func()
2393 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002394 var Closure = () => x
2395 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002396 enddef
2397 Func()
2398 assert_equal('hello', g:Myclosure())
2399 END
2400 CheckScriptSuccess(lines)
2401enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002402
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002403def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002404 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002405 vim9script
2406 def FuncA()
2407 FuncB(0)
2408 enddef
2409 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002410 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002411 enddef
2412 FuncA()
2413 END
2414 CheckScriptFailure(lines, 'E1012:')
2415enddef
2416
Bram Moolenaarf112f302020-12-20 17:47:52 +01002417def Test_global_closure()
2418 var lines =<< trim END
2419 vim9script
2420 def ReverseEveryNLines(n: number, line1: number, line2: number)
2421 var mods = 'sil keepj keepp lockm '
2422 var range = ':' .. line1 .. ',' .. line2
2423 def g:Offset(): number
2424 var offset = (line('.') - line1 + 1) % n
2425 return offset != 0 ? offset : n
2426 enddef
2427 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2428 enddef
2429
2430 new
2431 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2432 ReverseEveryNLines(3, 1, 9)
2433 END
2434 CheckScriptSuccess(lines)
2435 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2436 assert_equal(expected, getline(1, 9))
2437 bwipe!
2438enddef
2439
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002440def Test_global_closure_called_directly()
2441 var lines =<< trim END
2442 vim9script
2443 def Outer()
2444 var x = 1
2445 def g:Inner()
2446 var y = x
2447 x += 1
2448 assert_equal(1, y)
2449 enddef
2450 g:Inner()
2451 assert_equal(2, x)
2452 enddef
2453 Outer()
2454 END
2455 CheckScriptSuccess(lines)
2456 delfunc g:Inner
2457enddef
2458
Bram Moolenaar69c76172021-12-02 16:38:52 +00002459def Test_closure_called_from_legacy()
2460 var lines =<< trim END
2461 vim9script
2462 def Func()
2463 var outer = 'foo'
2464 var F = () => {
2465 outer = 'bar'
2466 }
2467 execute printf('call %s()', string(F))
2468 enddef
2469 Func()
2470 END
2471 CheckScriptFailure(lines, 'E1248')
2472enddef
2473
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002474def Test_failure_in_called_function()
2475 # this was using the frame index as the return value
2476 var lines =<< trim END
2477 vim9script
2478 au TerminalWinOpen * eval [][0]
2479 def PopupTerm(a: any)
2480 # make sure typvals on stack are string
2481 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2482 FireEvent()
2483 enddef
2484 def FireEvent()
2485 do TerminalWinOpen
2486 enddef
2487 # use try/catch to make eval fail
2488 try
2489 call PopupTerm(0)
2490 catch
2491 endtry
2492 au! TerminalWinOpen
2493 END
2494 CheckScriptSuccess(lines)
2495enddef
2496
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002497def Test_nested_lambda()
2498 var lines =<< trim END
2499 vim9script
2500 def Func()
2501 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002502 var Lambda1 = () => 7
2503 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002504 var res = Lambda2()
2505 assert_equal([7, 4], res)
2506 enddef
2507 Func()
2508 END
2509 CheckScriptSuccess(lines)
2510enddef
2511
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002512def Test_double_nested_lambda()
2513 var lines =<< trim END
2514 vim9script
2515 def F(head: string): func(string): func(string): string
2516 return (sep: string): func(string): string => ((tail: string): string => {
2517 return head .. sep .. tail
2518 })
2519 enddef
2520 assert_equal('hello-there', F('hello')('-')('there'))
2521 END
2522 CheckScriptSuccess(lines)
2523enddef
2524
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002525def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002526 var lines =<< trim END
2527 vim9script
2528 def F(text: string): func(string): func(string): string
2529 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002530 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002531 })
2532 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002533 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002534 END
2535 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002536
2537 lines =<< trim END
2538 vim9script
2539 echo range(4)->mapnew((_, v) => {
2540 return range(v) ->mapnew((_, s) => {
2541 return string(s)
2542 })
2543 })
2544 END
2545 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002546
2547 lines =<< trim END
2548 vim9script
2549
2550 def s:func()
2551 range(10)
2552 ->mapnew((_, _) => ({
2553 key: range(10)->mapnew((_, _) => {
2554 return ' '
2555 }),
2556 }))
2557 enddef
2558
2559 defcomp
2560 END
2561 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002562enddef
2563
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002564def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002565 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002566 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002567enddef
2568
2569def Test_lambda_arg_shadows_func()
2570 assert_equal([42], Shadowed())
2571enddef
2572
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002573def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002574 var path: string = empty(dir)
2575 \ ? 'empty'
2576 \ : 'full'
2577 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002578enddef
2579
2580def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002581 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002582enddef
2583
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002584def Test_script_var_in_lambda()
2585 var lines =<< trim END
2586 vim9script
2587 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002588 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002589 END
2590 CheckScriptSuccess(lines)
2591enddef
2592
Bram Moolenaar5e654232020-09-16 15:22:00 +02002593def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002594 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002595 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002596 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002597 ->reverse()
2598 return x
2599enddef
2600
2601def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002602 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002603
2604 var lines =<< trim END
2605 vim9script
2606 var res = [{n: 1, m: 2, s: 'xxx'}]
2607 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2608 v.n,
2609 v.m,
2610 substitute(v.s, '.*', 'yyy', '')
2611 ))
2612 assert_equal(['1:2:yyy'], res)
2613 END
2614 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002615enddef
2616
Bram Moolenaarb6571982021-01-08 22:24:19 +01002617def Test_list_lambda()
2618 timer_start(1000, (_) => 0)
2619 var body = execute(timer_info()[0].callback
2620 ->string()
2621 ->substitute("('", ' ', '')
2622 ->substitute("')", '', '')
2623 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002624 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002625enddef
2626
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002627def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002628 var lines =<< trim END
2629 vim9script
2630 var flist: list<func>
2631 for i in range(10)
2632 var inloop = i
2633 flist[i] = () => inloop
2634 endfor
2635 END
2636 CheckScriptSuccess(lines)
2637
2638 lines =<< trim END
2639 vim9script
2640 if true
2641 var outloop = 5
2642 var flist: list<func>
2643 for i in range(10)
2644 flist[i] = () => outloop
2645 endfor
2646 endif
2647 END
2648 CheckScriptSuccess(lines)
2649
2650 lines =<< trim END
2651 vim9script
2652 if true
2653 var outloop = 5
2654 endif
2655 var flist: list<func>
2656 for i in range(10)
2657 flist[i] = () => outloop
2658 endfor
2659 END
2660 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002661
2662 lines =<< trim END
2663 vim9script
2664 for i in range(10)
2665 var Ref = () => 0
2666 endfor
2667 assert_equal(0, ((i) => 0)(0))
2668 END
2669 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002670enddef
2671
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002672def Test_legacy_lambda()
2673 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002674
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002675 var lines =<< trim END
2676 echo {x -> 'hello ' .. x}('foo')
2677 END
2678 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002679
2680 lines =<< trim END
2681 vim9script
2682 def Func()
2683 echo (() => 'no error')()
2684 enddef
2685 legacy call s:Func()
2686 END
2687 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002688enddef
2689
Bram Moolenaarce024c32021-06-26 13:00:49 +02002690def Test_legacy()
2691 var lines =<< trim END
2692 vim9script
2693 func g:LegacyFunction()
2694 let g:legacyvar = 1
2695 endfunc
2696 def Testit()
2697 legacy call g:LegacyFunction()
2698 enddef
2699 Testit()
2700 assert_equal(1, g:legacyvar)
2701 unlet g:legacyvar
2702 delfunc g:LegacyFunction
2703 END
2704 CheckScriptSuccess(lines)
2705enddef
2706
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002707def Test_legacy_errors()
2708 for cmd in ['if', 'elseif', 'else', 'endif',
2709 'for', 'endfor', 'continue', 'break',
2710 'while', 'endwhile',
2711 'try', 'catch', 'finally', 'endtry']
2712 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2713 endfor
2714enddef
2715
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002716def Test_call_legacy_with_dict()
2717 var lines =<< trim END
2718 vim9script
2719 func Legacy() dict
2720 let g:result = self.value
2721 endfunc
2722 def TestDirect()
2723 var d = {value: 'yes', func: Legacy}
2724 d.func()
2725 enddef
2726 TestDirect()
2727 assert_equal('yes', g:result)
2728 unlet g:result
2729
2730 def TestIndirect()
2731 var d = {value: 'foo', func: Legacy}
2732 var Fi = d.func
2733 Fi()
2734 enddef
2735 TestIndirect()
2736 assert_equal('foo', g:result)
2737 unlet g:result
2738
2739 var d = {value: 'bar', func: Legacy}
2740 d.func()
2741 assert_equal('bar', g:result)
2742 unlet g:result
2743 END
2744 CheckScriptSuccess(lines)
2745enddef
2746
Bram Moolenaarab360522021-01-10 14:02:28 +01002747def DoFilterThis(a: string): list<string>
2748 # closure nested inside another closure using argument
2749 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2750 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2751enddef
2752
2753def Test_nested_closure_using_argument()
2754 assert_equal(['x', 'x2'], DoFilterThis('x'))
2755enddef
2756
Bram Moolenaar0186e582021-01-10 18:33:11 +01002757def Test_triple_nested_closure()
2758 var what = 'x'
2759 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2760 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2761 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2762enddef
2763
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002764func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002765 CheckScreendump
2766
2767 let lines =<< trim END
2768 vim9script
2769 def EchoNothing()
2770 silent echo ''
2771 enddef
2772 defcompile
2773 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002774 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002775
2776 " Check that the balloon shows up after a mouse move
2777 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002778 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002779 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2780
2781 " clean up
2782 call StopVimInTerminal(buf)
2783 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002784endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002785
Bram Moolenaar171fb922020-10-28 16:54:47 +01002786def SilentlyError()
2787 execute('silent! invalid')
2788 g:did_it = 'yes'
2789enddef
2790
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002791func UserError()
2792 silent! invalid
2793endfunc
2794
2795def SilentlyUserError()
2796 UserError()
2797 g:did_it = 'yes'
2798enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002799
2800" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002801func Test_ignore_silent_error()
2802 let g:did_it = 'no'
2803 call SilentlyError()
2804 call assert_equal('yes', g:did_it)
2805
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002806 let g:did_it = 'no'
2807 call SilentlyUserError()
2808 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002809
2810 unlet g:did_it
2811endfunc
2812
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002813def Test_ignore_silent_error_in_filter()
2814 var lines =<< trim END
2815 vim9script
2816 def Filter(winid: number, key: string): bool
2817 if key == 'o'
2818 silent! eval [][0]
2819 return true
2820 endif
2821 return popup_filter_menu(winid, key)
2822 enddef
2823
Bram Moolenaare0de1712020-12-02 17:36:54 +01002824 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002825 feedkeys("o\r", 'xnt')
2826 END
2827 CheckScriptSuccess(lines)
2828enddef
2829
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002830def Fibonacci(n: number): number
2831 if n < 2
2832 return n
2833 else
2834 return Fibonacci(n - 1) + Fibonacci(n - 2)
2835 endif
2836enddef
2837
Bram Moolenaar985116a2020-07-12 17:31:09 +02002838def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002839 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002840enddef
2841
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002842def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002843 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002844 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002845 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002846 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002847 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002848enddef
2849
2850def Test_closure_in_map()
2851 mkdir('XclosureDir/tdir', 'p')
2852 writefile(['111'], 'XclosureDir/file1')
2853 writefile(['222'], 'XclosureDir/file2')
2854 writefile(['333'], 'XclosureDir/tdir/file3')
2855
Bram Moolenaare0de1712020-12-02 17:36:54 +01002856 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002857
2858 delete('XclosureDir', 'rf')
2859enddef
2860
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002861def Test_invalid_function_name()
2862 var lines =<< trim END
2863 vim9script
2864 def s: list<string>
2865 END
2866 CheckScriptFailure(lines, 'E129:')
2867
2868 lines =<< trim END
2869 vim9script
2870 def g: list<string>
2871 END
2872 CheckScriptFailure(lines, 'E129:')
2873
2874 lines =<< trim END
2875 vim9script
2876 def <SID>: list<string>
2877 END
2878 CheckScriptFailure(lines, 'E884:')
2879
2880 lines =<< trim END
2881 vim9script
2882 def F list<string>
2883 END
2884 CheckScriptFailure(lines, 'E488:')
2885enddef
2886
Bram Moolenaara90afb92020-07-15 22:38:56 +02002887def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002888 var lines =<< trim END
2889 var Xsetlist: func
2890 Xsetlist = function('setloclist', [0])
2891 Xsetlist([], ' ', {title: 'test'})
2892 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002893
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002894 Xsetlist = function('setloclist', [0, [], ' '])
2895 Xsetlist({title: 'test'})
2896 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002897
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002898 Xsetlist = function('setqflist')
2899 Xsetlist([], ' ', {title: 'test'})
2900 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002901
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002902 Xsetlist = function('setqflist', [[], ' '])
2903 Xsetlist({title: 'test'})
2904 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002905
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002906 var Len: func: number = function('len', ['word'])
2907 assert_equal(4, Len())
2908
2909 var RepeatFunc = function('repeat', ['o'])
2910 assert_equal('ooooo', RepeatFunc(5))
2911 END
2912 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002913
2914 lines =<< trim END
2915 vim9script
2916 def Foo(Parser: any)
2917 enddef
2918 var Expr: func(dict<any>): dict<any>
2919 const Call = Foo(Expr)
2920 END
2921 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002922enddef
2923
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002924def Test_cmd_modifier()
2925 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002926 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002927enddef
2928
2929def Test_restore_modifiers()
2930 # check that when compiling a :def function command modifiers are not messed
2931 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002932 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002933 vim9script
2934 set eventignore=
2935 autocmd QuickFixCmdPost * copen
2936 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002937 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002938 enddef
2939 func Func()
2940 noautocmd call s:AutocmdsDisabled()
2941 let g:ei_after = &eventignore
2942 endfunc
2943 Func()
2944 END
2945 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002946 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002947enddef
2948
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002949def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002950 eval 1 + 2
2951 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002952 # call not on fourth line
2953 StackBot()
2954enddef
2955
2956def StackBot()
2957 # throw an error
2958 eval [][0]
2959enddef
2960
2961def Test_callstack_def()
2962 try
2963 StackTop()
2964 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002965 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002966 endtry
2967enddef
2968
Bram Moolenaare8211a32020-10-09 22:04:29 +02002969" Re-using spot for variable used in block
2970def Test_block_scoped_var()
2971 var lines =<< trim END
2972 vim9script
2973 def Func()
2974 var x = ['a', 'b', 'c']
2975 if 1
2976 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002977 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002978 endif
2979 var z = x
2980 assert_equal(['x', 'x', 'x'], z)
2981 enddef
2982 Func()
2983 END
2984 CheckScriptSuccess(lines)
2985enddef
2986
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002987def Test_reset_did_emsg()
2988 var lines =<< trim END
2989 @s = 'blah'
2990 au BufWinLeave * #
2991 def Func()
2992 var winid = popup_create('popup', {})
2993 exe '*s'
2994 popup_close(winid)
2995 enddef
2996 Func()
2997 END
2998 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002999 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003000enddef
3001
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003002def Test_did_emsg_reset()
3003 # executing an autocommand resets did_emsg, this should not result in a
3004 # builtin function considered failing
3005 var lines =<< trim END
3006 vim9script
3007 au BufWinLeave * #
3008 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003009 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003010 eval [][0]
3011 enddef
3012 nno <F3> <cmd>call <sid>Func()<cr>
3013 feedkeys("\<F3>\e", 'xt')
3014 END
3015 writefile(lines, 'XemsgReset')
3016 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3017 delete('XemsgReset')
3018 nunmap <F3>
3019 au! BufWinLeave
3020enddef
3021
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003022def Test_abort_with_silent_call()
3023 var lines =<< trim END
3024 vim9script
3025 g:result = 'none'
3026 def Func()
3027 g:result += 3
3028 g:result = 'yes'
3029 enddef
3030 # error is silenced, but function aborts on error
3031 silent! Func()
3032 assert_equal('none', g:result)
3033 unlet g:result
3034 END
3035 CheckScriptSuccess(lines)
3036enddef
3037
Bram Moolenaarf665e972020-12-05 19:17:16 +01003038def Test_continues_with_silent_error()
3039 var lines =<< trim END
3040 vim9script
3041 g:result = 'none'
3042 def Func()
3043 silent! g:result += 3
3044 g:result = 'yes'
3045 enddef
3046 # error is silenced, function does not abort
3047 Func()
3048 assert_equal('yes', g:result)
3049 unlet g:result
3050 END
3051 CheckScriptSuccess(lines)
3052enddef
3053
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003054def Test_abort_even_with_silent()
3055 var lines =<< trim END
3056 vim9script
3057 g:result = 'none'
3058 def Func()
3059 eval {-> ''}() .. '' .. {}['X']
3060 g:result = 'yes'
3061 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003062 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003063 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003064 unlet g:result
3065 END
3066 CheckScriptSuccess(lines)
3067enddef
3068
Bram Moolenaarf665e972020-12-05 19:17:16 +01003069def Test_cmdmod_silent_restored()
3070 var lines =<< trim END
3071 vim9script
3072 def Func()
3073 g:result = 'none'
3074 silent! g:result += 3
3075 g:result = 'none'
3076 g:result += 3
3077 enddef
3078 Func()
3079 END
3080 # can't use CheckScriptFailure, it ignores the :silent!
3081 var fname = 'Xdefsilent'
3082 writefile(lines, fname)
3083 var caught = 'no'
3084 try
3085 exe 'source ' .. fname
3086 catch /E1030:/
3087 caught = 'yes'
3088 assert_match('Func, line 4', v:throwpoint)
3089 endtry
3090 assert_equal('yes', caught)
3091 delete(fname)
3092enddef
3093
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003094def Test_cmdmod_silent_nested()
3095 var lines =<< trim END
3096 vim9script
3097 var result = ''
3098
3099 def Error()
3100 result ..= 'Eb'
3101 eval [][0]
3102 result ..= 'Ea'
3103 enddef
3104
3105 def Crash()
3106 result ..= 'Cb'
3107 sil! Error()
3108 result ..= 'Ca'
3109 enddef
3110
3111 Crash()
3112 assert_equal('CbEbEaCa', result)
3113 END
3114 CheckScriptSuccess(lines)
3115enddef
3116
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003117def Test_dict_member_with_silent()
3118 var lines =<< trim END
3119 vim9script
3120 g:result = 'none'
3121 var d: dict<any>
3122 def Func()
3123 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003124 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003125 catch
3126 endtry
3127 enddef
3128 silent! Func()
3129 assert_equal('0', g:result)
3130 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003131 END
3132 CheckScriptSuccess(lines)
3133enddef
3134
Bram Moolenaarf9041332021-01-21 19:41:16 +01003135def Test_skip_cmds_with_silent()
3136 var lines =<< trim END
3137 vim9script
3138
3139 def Func(b: bool)
3140 Crash()
3141 enddef
3142
3143 def Crash()
3144 sil! :/not found/d _
3145 sil! :/not found/put _
3146 enddef
3147
3148 Func(true)
3149 END
3150 CheckScriptSuccess(lines)
3151enddef
3152
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003153def Test_opfunc()
3154 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3155 def g:Opfunc(_: any): string
3156 setline(1, 'ASDF')
3157 return ''
3158 enddef
3159 new
3160 setline(1, 'asdf')
3161 feedkeys("\<F3>$", 'x')
3162 assert_equal('ASDF', getline(1))
3163
3164 bwipe!
3165 nunmap <F3>
3166enddef
3167
Bram Moolenaar077a4232020-12-22 18:33:27 +01003168" this was crashing on exit
3169def Test_nested_lambda_in_closure()
3170 var lines =<< trim END
3171 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003172 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003173 def Outer()
3174 def g:Inner()
3175 echo map([1, 2, 3], {_, v -> v + 1})
3176 enddef
3177 g:Inner()
3178 enddef
3179 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003180 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003181 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003182 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003183 return
3184 endif
3185 assert_equal(['Done'], readfile('XnestedDone'))
3186 delete('XnestedDone')
3187enddef
3188
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003189def Test_check_func_arg_types()
3190 var lines =<< trim END
3191 vim9script
3192 def F1(x: string): string
3193 return x
3194 enddef
3195
3196 def F2(x: number): number
3197 return x + 1
3198 enddef
3199
3200 def G(g: func): dict<func>
3201 return {f: g}
3202 enddef
3203
3204 def H(d: dict<func>): string
3205 return d.f('a')
3206 enddef
3207 END
3208
3209 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3210 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3211enddef
3212
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003213def Test_list_any_type_checked()
3214 var lines =<< trim END
3215 vim9script
3216 def Foo()
3217 --decl--
3218 Bar(l)
3219 enddef
3220 def Bar(ll: list<dict<any>>)
3221 enddef
3222 Foo()
3223 END
3224 lines[2] = 'var l: list<any>'
3225 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3226
3227 lines[2] = 'var l: list<any> = []'
3228 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3229
3230 lines[2] = 'var l: list<any> = [11]'
3231 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3232enddef
3233
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003234def Test_compile_error()
3235 var lines =<< trim END
3236 def g:Broken()
3237 echo 'a' + {}
3238 enddef
3239 call g:Broken()
3240 END
3241 # First call: compilation error
3242 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3243
3244 # Second call won't try compiling again
3245 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003246 delfunc g:Broken
3247
3248 # No error when compiling with :silent!
3249 lines =<< trim END
3250 def g:Broken()
3251 echo 'a' + []
3252 enddef
3253 silent! defcompile
3254 END
3255 CheckScriptSuccess(lines)
3256
3257 # Calling the function won't try compiling again
3258 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3259 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003260enddef
3261
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003262def Test_ignored_argument()
3263 var lines =<< trim END
3264 vim9script
3265 def Ignore(_, _): string
3266 return 'yes'
3267 enddef
3268 assert_equal('yes', Ignore(1, 2))
3269
3270 func Ok(_)
3271 return a:_
3272 endfunc
3273 assert_equal('ok', Ok('ok'))
3274
3275 func Oktoo()
3276 let _ = 'too'
3277 return _
3278 endfunc
3279 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003280
3281 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003282 END
3283 CheckScriptSuccess(lines)
3284
3285 lines =<< trim END
3286 def Ignore(_: string): string
3287 return _
3288 enddef
3289 defcompile
3290 END
3291 CheckScriptFailure(lines, 'E1181:', 1)
3292
3293 lines =<< trim END
3294 var _ = 1
3295 END
3296 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003297
3298 lines =<< trim END
3299 var x = _
3300 END
3301 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003302enddef
3303
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003304def Test_too_many_arguments()
3305 var lines =<< trim END
3306 echo [0, 1, 2]->map(() => 123)
3307 END
3308 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3309
3310 lines =<< trim END
3311 echo [0, 1, 2]->map((_) => 123)
3312 END
3313 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3314enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003315
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003316def Test_closing_brace_at_start_of_line()
3317 var lines =<< trim END
3318 def Func()
3319 enddef
3320 Func(
3321 )
3322 END
3323 call CheckDefAndScriptSuccess(lines)
3324enddef
3325
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003326func CreateMydict()
3327 let g:mydict = {}
3328 func g:mydict.afunc()
3329 let g:result = self.key
3330 endfunc
3331endfunc
3332
3333def Test_numbered_function_reference()
3334 CreateMydict()
3335 var output = execute('legacy func g:mydict.afunc')
3336 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3337 execute 'function(' .. funcName .. ', [], {key: 42})()'
3338 # check that the function still exists
3339 assert_equal(output, execute('legacy func g:mydict.afunc'))
3340 unlet g:mydict
3341enddef
3342
Bram Moolenaar20677332021-06-06 17:02:53 +02003343if has('python3')
3344 def Test_python3_heredoc()
3345 py3 << trim EOF
3346 import vim
3347 vim.vars['didit'] = 'yes'
3348 EOF
3349 assert_equal('yes', g:didit)
3350
3351 python3 << trim EOF
3352 import vim
3353 vim.vars['didit'] = 'again'
3354 EOF
3355 assert_equal('again', g:didit)
3356 enddef
3357endif
3358
3359" This messes up syntax highlight, keep near the end.
3360if has('lua')
3361 def Test_lua_heredoc()
3362 g:d = {}
3363 lua << trim EOF
3364 x = vim.eval('g:d')
3365 x['key'] = 'val'
3366 EOF
3367 assert_equal('val', g:d.key)
3368 enddef
3369endif
3370
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003371
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003372" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker