blob: 2c871d33e5f505ae7a9e42057f8cf08d6eb41df4 [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 Moolenaardd297bc2021-12-10 10:37:38 +00001227def Test_lambda_type_allocated()
1228 # Check that unreferencing a partial using a lambda can use the variable type
1229 # after the lambda has been freed and does not leak memory.
1230 var lines =<< trim END
1231 vim9script
1232
1233 func MyomniFunc1(val, findstart, base)
1234 return a:findstart ? 0 : []
1235 endfunc
1236
1237 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1238 &omnifunc = Lambda
1239 Lambda = (a, b) => MyomniFunc1(20, a, b)
1240 &omnifunc = string(Lambda)
1241 Lambda = (a, b) => strlen(a)
1242 END
1243 CheckScriptSuccess(lines)
1244enddef
1245
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001246" Default arg and varargs
1247def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001248 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001249 for s in rest
1250 res ..= ',' .. s
1251 endfor
1252 return res
1253enddef
1254
1255def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001256 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001257 MyDefVarargs('one')->assert_equal('one,foo')
1258 MyDefVarargs('one', 'two')->assert_equal('one,two')
1259 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001260 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001261 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001262 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001263 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001264
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001265 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001266 vim9script
1267 def Func(...l: list<string>)
1268 echo l
1269 enddef
1270 Func('a', 'b', 'c')
1271 END
1272 CheckScriptSuccess(lines)
1273
1274 lines =<< trim END
1275 vim9script
1276 def Func(...l: list<string>)
1277 echo l
1278 enddef
1279 Func()
1280 END
1281 CheckScriptSuccess(lines)
1282
1283 lines =<< trim END
1284 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001285 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001286 echo l
1287 enddef
1288 Func(0)
1289 END
1290 CheckScriptSuccess(lines)
1291
1292 lines =<< trim END
1293 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001294 def Func(...l: any)
1295 echo l
1296 enddef
1297 Func(0)
1298 END
1299 CheckScriptFailure(lines, 'E1180:', 2)
1300
1301 lines =<< trim END
1302 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001303 def Func(..._l: list<string>)
1304 echo _l
1305 enddef
1306 Func('a', 'b', 'c')
1307 END
1308 CheckScriptSuccess(lines)
1309
1310 lines =<< trim END
1311 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001312 def Func(...l: list<string>)
1313 echo l
1314 enddef
1315 Func(1, 2, 3)
1316 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001317 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001318
1319 lines =<< trim END
1320 vim9script
1321 def Func(...l: list<string>)
1322 echo l
1323 enddef
1324 Func('a', 9)
1325 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001326 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001327
1328 lines =<< trim END
1329 vim9script
1330 def Func(...l: list<string>)
1331 echo l
1332 enddef
1333 Func(1, 'a')
1334 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001335 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001336
1337 lines =<< trim END
1338 vim9script
1339 def Func( # some comment
1340 ...l = []
1341 )
1342 echo l
1343 enddef
1344 END
1345 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001346
1347 lines =<< trim END
1348 vim9script
1349 def DoIt()
1350 g:Later('')
1351 enddef
1352 defcompile
1353 def g:Later(...l: list<number>)
1354 enddef
1355 DoIt()
1356 END
1357 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001358enddef
1359
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001360let s:value = ''
1361
1362def FuncOneDefArg(opt = 'text')
1363 s:value = opt
1364enddef
1365
1366def FuncTwoDefArg(nr = 123, opt = 'text'): string
1367 return nr .. opt
1368enddef
1369
1370def FuncVarargs(...arg: list<string>): string
1371 return join(arg, ',')
1372enddef
1373
1374def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001375 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001376 RefDefArg = FuncOneDefArg
1377 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001378 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001379 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001380 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001381
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001382 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001383 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001384 RefDef2Arg()->assert_equal('123text')
1385 RefDef2Arg(99)->assert_equal('99text')
1386 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001387
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001388 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1389 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001390
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001391 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001392 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001393 RefVarargs()->assert_equal('')
1394 RefVarargs('one')->assert_equal('one')
1395 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001396
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001397 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1398 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001399enddef
1400
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001401" Only varargs
1402def MyVarargsOnly(...args: list<string>): string
1403 return join(args, ',')
1404enddef
1405
1406def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001407 MyVarargsOnly()->assert_equal('')
1408 MyVarargsOnly('one')->assert_equal('one')
1409 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001410 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1411 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001412enddef
1413
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001414def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001415 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001416 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001417 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001418enddef
1419
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001420def DictArg(arg: dict<string>)
1421 arg['key'] = 'value'
1422enddef
1423
1424def ListArg(arg: list<string>)
1425 arg[0] = 'value'
1426enddef
1427
1428def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001429 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001430 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001431 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001432 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001433 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001434 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001435 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001436
Bram Moolenaard2c61702020-09-06 15:58:36 +02001437 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001438 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001439enddef
1440
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001441" These argument names are reserved in legacy functions.
1442def WithReservedNames(firstline: string, lastline: string): string
1443 return firstline .. lastline
1444enddef
1445
1446def Test_argument_names()
1447 assert_equal('OK', WithReservedNames('O', 'K'))
1448enddef
1449
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001450def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001451 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001452 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001453enddef
1454
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001455func DefinedLater(arg)
1456 return a:arg
1457endfunc
1458
1459def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001460 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001461 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001462 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001463
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001464 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001465 vim9script
1466 def RetNumber(): number
1467 return 123
1468 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001469 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001470 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001471 END
1472 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001473
1474 lines =<< trim END
1475 vim9script
1476 def RetNumber(): number
1477 return 123
1478 enddef
1479 def Bar(F: func: number): number
1480 return F()
1481 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001482 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001483 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001484 END
1485 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001486
1487 lines =<< trim END
1488 vim9script
1489 def UseNumber(nr: number)
1490 echo nr
1491 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001492 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001493 Funcref(123)
1494 END
1495 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001496
1497 lines =<< trim END
1498 vim9script
1499 def UseNumber(nr: number)
1500 echo nr
1501 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001502 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001503 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001504 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001505
1506 lines =<< trim END
1507 vim9script
1508 def EchoNr(nr = 34)
1509 g:echo = nr
1510 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001511 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001512 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001513 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001514 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001515 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001516 END
1517 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001518
1519 lines =<< trim END
1520 vim9script
1521 def EchoList(...l: list<number>)
1522 g:echo = l
1523 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001524 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001525 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001526 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001527 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001528 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001529 END
1530 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001531
1532 lines =<< trim END
1533 vim9script
1534 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1535 g:optarg = opt
1536 g:listarg = l
1537 return nr
1538 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001539 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001540 Funcref(10)->assert_equal(10)
1541 g:optarg->assert_equal(12)
1542 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001543
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001544 Funcref(11, 22)->assert_equal(11)
1545 g:optarg->assert_equal(22)
1546 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001547
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001548 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1549 g:optarg->assert_equal(18)
1550 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001551 END
1552 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001553enddef
1554
1555let SomeFunc = function('len')
1556let NotAFunc = 'text'
1557
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001558def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001559 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001560 var Ref1: func(bool): string
1561 var Ref2: func(bool): number
1562 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001563 Ref3 = g:cond ? Ref1 : Ref2
1564
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001565 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001566 var Refa1: func(bool): number
1567 var Refa2: func(bool, number): number
1568 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001569 Refa3 = g:cond ? Refa1 : Refa2
1570
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001571 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001572 var Refb1: func(bool, string): number
1573 var Refb2: func(string, number): number
1574 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001575 Refb3 = g:cond ? Refb1 : Refb2
1576enddef
1577
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001578def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001579 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001580enddef
1581
1582def DefinedEvenLater(arg: string): string
1583 return arg
1584enddef
1585
1586def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001587 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001588 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001589enddef
1590
1591def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001592 CheckScriptFailure([
1593 'def Func(): number',
1594 'return "a"',
1595 'enddef',
1596 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001597 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001598 CheckScriptFailure([
1599 'def Func(): string',
1600 'return 1',
1601 'enddef',
1602 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001603 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001604 CheckScriptFailure([
1605 'def Func(): void',
1606 'return "a"',
1607 'enddef',
1608 'defcompile'],
1609 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001610 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001611 CheckScriptFailure([
1612 'def Func()',
1613 'return "a"',
1614 'enddef',
1615 'defcompile'],
1616 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001617 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001618
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001619 CheckScriptFailure([
1620 'def Func(): number',
1621 'return',
1622 'enddef',
1623 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001624 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001625
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001626 CheckScriptFailure([
1627 'def Func():number',
1628 'return 123',
1629 'enddef',
1630 'defcompile'], 'E1069:')
1631 delfunc! g:Func
1632
1633 CheckScriptFailure([
1634 'def Func() :number',
1635 'return 123',
1636 'enddef',
1637 'defcompile'], 'E1059:')
1638 delfunc! g:Func
1639
1640 CheckScriptFailure([
1641 'def Func() : number',
1642 'return 123',
1643 'enddef',
1644 'defcompile'], 'E1059:')
1645 delfunc! g:Func
1646
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001647 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001648 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001649 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001650 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001651 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001652 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001653
1654 CheckScriptFailure([
1655 'vim9script',
1656 'def FuncB()',
1657 ' return 123',
1658 'enddef',
1659 'def FuncA()',
1660 ' FuncB()',
1661 'enddef',
1662 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001663enddef
1664
1665def Test_arg_type_wrong()
1666 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001667 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001668 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001669 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001670 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1671 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001672enddef
1673
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001674def Test_white_space_before_comma()
1675 var lines =<< trim END
1676 vim9script
1677 def Func(a: number , b: number)
1678 enddef
1679 END
1680 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001681 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001682enddef
1683
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001684def Test_white_space_after_comma()
1685 var lines =<< trim END
1686 vim9script
1687 def Func(a: number,b: number)
1688 enddef
1689 END
1690 CheckScriptFailure(lines, 'E1069:')
1691
1692 # OK in legacy function
1693 lines =<< trim END
1694 vim9script
1695 func Func(a,b)
1696 endfunc
1697 END
1698 CheckScriptSuccess(lines)
1699enddef
1700
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001701def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001702 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001703 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001704 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001705 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001706 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001707 enddef
1708 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001709 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001710
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001711 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001712 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001713 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001714
Bram Moolenaar67979662020-06-20 22:50:47 +02001715 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001716 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001717 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001718
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001719 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001720 def ListFunc(arg: list<number>)
1721 listvar = arg
1722 enddef
1723 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001724 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001725
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001726 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001727 def DictFunc(arg: dict<number>)
1728 dictvar = arg
1729 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001730 {a: 1, b: 2}->DictFunc()
1731 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001732 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001733 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001734 enddef
1735 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001736 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001737
Bram Moolenaare0de1712020-12-02 17:36:54 +01001738 {a: 3, b: 4}->DictFunc()
1739 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001740
1741 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001742 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001743 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001744 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001745
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001746 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001747 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001748 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001749 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001750 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001751 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001752
1753 def UseString()
1754 'xyork'->MyFunc()
1755 enddef
1756 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001757 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001758
Bram Moolenaar10409562020-07-29 20:00:38 +02001759 def UseString2()
1760 "knife"->MyFunc()
1761 enddef
1762 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001763 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001764
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001765 # prepending a colon makes it a mark
1766 new
1767 setline(1, ['aaa', 'bbb', 'ccc'])
1768 normal! 3Gmt1G
1769 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001770 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001771 bwipe!
1772
Bram Moolenaare6b53242020-07-01 17:28:33 +02001773 MyFunc(
1774 'continued'
1775 )
1776 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001777 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001778 )
1779
1780 call MyFunc(
1781 'more'
1782 ..
1783 'lines'
1784 )
1785 assert_equal(
1786 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001787 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001788 END
1789 writefile(lines, 'Xcall.vim')
1790 source Xcall.vim
1791 delete('Xcall.vim')
1792enddef
1793
1794def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001795 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001796 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001797 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001798 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001799 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001800 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001801 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001802 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001803 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001804enddef
1805
Bram Moolenaar65b95452020-07-19 14:03:09 +02001806def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001807 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001808 vim9script
1809 def MyFunc(arg: string)
1810 echo arg
1811 enddef
1812 MyFunc(1234)
1813 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001814 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001815enddef
1816
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001817def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001818 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001819 vim9script
1820 const var = ''
1821 def MyFunc(arg: string)
1822 var = 'asdf'
1823 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001824 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001825 END
1826 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001827 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001828 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001829
1830 lines =<< trim END
1831 const g:Aconst = 77
1832 def Change()
1833 # comment
1834 g:Aconst = 99
1835 enddef
1836 call Change()
1837 unlet g:Aconst
1838 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001839 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001840enddef
1841
1842" Test that inside :function a Python function can be defined, :def is not
1843" recognized.
1844func Test_function_python()
1845 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001846 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001847 execute py "<< EOF"
1848def do_something():
1849 return 1
1850EOF
1851endfunc
1852
1853def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001854 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001855 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001856 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001857 echo 'hello'
1858 enddef
1859
1860 def CallGoneSoon()
1861 GoneSoon()
1862 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001863 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001864
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001865 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866 CallGoneSoon()
1867 END
1868 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001869 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1870 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001871
1872 delete('XToDelFunc')
1873enddef
1874
1875def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001876 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001877 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001878 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001879 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001880 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001881 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001882 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001883 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001884 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001885
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001886 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001887 g:Func1()->assert_equal('Func1')
1888 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001889
1890 delfunc! Func0
1891 delfunc! Func1
1892 delfunc! Func2
1893enddef
1894
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001895def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001896 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001897 vim9script
1898 func Func(arg)
1899 echo a:arg
1900 endfunc
1901 Func('text')
1902 END
1903 writefile(lines, 'XVim9Func')
1904 so XVim9Func
1905
1906 delete('XVim9Func')
1907enddef
1908
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001909let s:funcResult = 0
1910
1911def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001912 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001913enddef
1914
1915def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001916 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001917 return 1234
1918enddef
1919
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001920def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001921 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001922 return 'text'
1923enddef
1924
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001925def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001926 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001927enddef
1928
1929def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001930 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001931 return arg
1932enddef
1933
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001934def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001935 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001936enddef
1937
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001938def FuncOneArgRetString(arg: string): string
1939 return arg
1940enddef
1941
Bram Moolenaar89228602020-04-05 22:14:54 +02001942def FuncOneArgRetAny(arg: any): any
1943 return arg
1944enddef
1945
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001946def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001947 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001948 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001949 Ref1 = FuncNoArgNoRet
1950 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001951 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001952
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001953 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001954 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001955 Ref2 = FuncNoArgNoRet
1956 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001957 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001958
Bram Moolenaar53900992020-08-22 19:02:02 +02001959 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001960 Ref2 = FuncOneArgNoRet
1961 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001962 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001963
Bram Moolenaar53900992020-08-22 19:02:02 +02001964 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001965 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001966 Ref2()->assert_equal(1234)
1967 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001968
Bram Moolenaar53900992020-08-22 19:02:02 +02001969 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001970 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001971 Ref2(13)->assert_equal(13)
1972 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001973enddef
1974
Bram Moolenaar9978d472020-07-05 16:01:56 +02001975def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001976 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02001977 for n in repeat([1], 3)
1978 res += n
1979 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001980 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02001981
1982 res = 0
1983 for n in add([1, 2], 3)
1984 res += n
1985 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001986 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02001987enddef
1988
Bram Moolenaar846178a2020-07-05 17:04:13 +02001989def Test_argv_return_type()
1990 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001991 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02001992 for name in argv()
1993 res ..= name
1994 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001995 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02001996enddef
1997
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001998def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001999 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002000 RefVoid = FuncNoArgNoRet
2001 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002002 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2003 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002004
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002005 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002006 RefAny = FuncNoArgRetNumber
2007 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002008 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2009 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002010
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002011 var RefAnyNoArgs: func: any = RefAny
2012
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002013 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002014 RefNr = FuncNoArgRetNumber
2015 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002016 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2017 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002018
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002019 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002020 RefStr = FuncNoArgRetString
2021 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002022 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2023 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002024enddef
2025
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002026def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002027 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002028
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002029 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2030 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2031 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2032 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2033 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2034 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 +02002035
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002036 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2037 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2038 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:')
2039 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002040enddef
2041
Bram Moolenaar89228602020-04-05 22:14:54 +02002042def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002043 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002044 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002045 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002046
2047 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002048 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002049
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002050 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002051 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002052 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002053
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002054 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002055enddef
2056
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002057def Test_func_common_type()
2058 def FuncOne(n: number): number
2059 return n
2060 enddef
2061 def FuncTwo(s: string): number
2062 return len(s)
2063 enddef
2064 def FuncThree(n: number, s: string): number
2065 return n + len(s)
2066 enddef
2067 var list = [FuncOne, FuncTwo, FuncThree]
2068 assert_equal(8, list[0](8))
2069 assert_equal(4, list[1]('word'))
2070 assert_equal(7, list[2](3, 'word'))
2071enddef
2072
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002073def MultiLine(
2074 arg1: string,
2075 arg2 = 1234,
2076 ...rest: list<string>
2077 ): string
2078 return arg1 .. arg2 .. join(rest, '-')
2079enddef
2080
Bram Moolenaar2c330432020-04-13 14:41:35 +02002081def MultiLineComment(
2082 arg1: string, # comment
2083 arg2 = 1234, # comment
2084 ...rest: list<string> # comment
2085 ): string # comment
2086 return arg1 .. arg2 .. join(rest, '-')
2087enddef
2088
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002089def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002090 MultiLine('text')->assert_equal('text1234')
2091 MultiLine('text', 777)->assert_equal('text777')
2092 MultiLine('text', 777, 'one')->assert_equal('text777one')
2093 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002094enddef
2095
Bram Moolenaar23e03252020-04-12 22:22:31 +02002096func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002097 call MultiLine('text')->assert_equal('text1234')
2098 call MultiLine('text', 777)->assert_equal('text777')
2099 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2100 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002101endfunc
2102
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002103
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002104" When using CheckScriptFailure() for the below test, E1010 is generated instead
2105" of E1056.
2106func Test_E1056_1059()
2107 let caught_1056 = 0
2108 try
2109 def F():
2110 return 1
2111 enddef
2112 catch /E1056:/
2113 let caught_1056 = 1
2114 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002115 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002116
2117 let caught_1059 = 0
2118 try
2119 def F5(items : list)
2120 echo 'a'
2121 enddef
2122 catch /E1059:/
2123 let caught_1059 = 1
2124 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002125 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002126endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002127
Bram Moolenaar015f4262020-05-05 21:25:22 +02002128func DelMe()
2129 echo 'DelMe'
2130endfunc
2131
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002132def Test_error_reporting()
2133 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002134 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002135 " comment
2136 def Func()
2137 # comment
2138 # comment
2139 invalid
2140 enddef
2141 defcompile
2142 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002143 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002144 try
2145 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002146 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002147 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002148 v:exception->assert_match('Invalid command: invalid')
2149 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002150 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002151 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002152
2153 # comment lines after the start of the function
2154 lines =<< trim END
2155 " comment
2156 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002157 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002158 # comment
2159 # comment
2160 invalid
2161 enddef
2162 defcompile
2163 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002164 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002165 try
2166 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002167 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002168 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002169 v:exception->assert_match('Invalid command: invalid')
2170 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002171 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002172 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002173
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002174 lines =<< trim END
2175 vim9script
2176 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002177 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002178 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002179 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002180 enddef
2181 defcompile
2182 Func()
2183 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002184 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002185 try
2186 source Xdef
2187 assert_report('should have failed')
2188 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002189 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002190 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002191 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002192
Bram Moolenaar08052222020-09-14 17:04:31 +02002193 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002194enddef
2195
Bram Moolenaar015f4262020-05-05 21:25:22 +02002196def Test_deleted_function()
2197 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002198 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002199 'delfunc g:DelMe',
2200 'echo RefMe()'], 'E117:')
2201enddef
2202
2203def Test_unknown_function()
2204 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002205 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002206 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002207enddef
2208
Bram Moolenaar328eac22021-01-07 19:23:08 +01002209def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002210 return Ref('more')
2211enddef
2212
2213def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002214 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002215 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002216enddef
2217
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002218def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002219 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002220 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002221enddef
2222
2223def Test_closure_ref_after_return()
2224 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002225 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002226 unlet g:Ref
2227enddef
2228
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002229def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002230 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002231 g:Extend = (s) => local->add(s)
2232 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002233enddef
2234
2235def Test_closure_two_refs()
2236 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002237 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002238 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002239 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002240 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002241 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002242
2243 unlet g:Extend
2244 unlet g:Read
2245enddef
2246
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002247def ReadRef(Ref: func(): list<string>): string
2248 return join(Ref(), ' ')
2249enddef
2250
Bram Moolenaar5e654232020-09-16 15:22:00 +02002251def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002252 Ref(add)
2253enddef
2254
2255def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002256 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002257 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002258 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002259 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002260 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002261 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002262
2263 unlet g:Extend
2264 unlet g:Read
2265enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002266
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002267def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002268 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002269 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002270enddef
2271
2272def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002273 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002274 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002275enddef
2276
2277def Test_closure_using_argument()
2278 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002279 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002280
2281 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002282 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002283
2284 unlet g:UseArg
2285 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002286
2287 var lines =<< trim END
2288 vim9script
2289 def Test(Fun: func(number): number): list<number>
2290 return map([1, 2, 3], (_, i) => Fun(i))
2291 enddef
2292 def Inc(nr: number): number
2293 return nr + 2
2294 enddef
2295 assert_equal([3, 4, 5], Test(Inc))
2296 END
2297 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002298enddef
2299
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002300def MakeGetAndAppendRefs()
2301 var local = 'a'
2302
2303 def Append(arg: string)
2304 local ..= arg
2305 enddef
2306 g:Append = Append
2307
2308 def Get(): string
2309 return local
2310 enddef
2311 g:Get = Get
2312enddef
2313
2314def Test_closure_append_get()
2315 MakeGetAndAppendRefs()
2316 g:Get()->assert_equal('a')
2317 g:Append('-b')
2318 g:Get()->assert_equal('a-b')
2319 g:Append('-c')
2320 g:Get()->assert_equal('a-b-c')
2321
2322 unlet g:Append
2323 unlet g:Get
2324enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002325
Bram Moolenaar04b12692020-05-04 23:24:44 +02002326def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002327 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002328 def Closure(arg: string): string
2329 return local .. arg
2330 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002331 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002332enddef
2333
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002334func GetResult(Ref)
2335 return a:Ref('some')
2336endfunc
2337
2338def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002339 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002340 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002341 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002342enddef
2343
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002344def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002345 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002346 vim9script
2347 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002348 var name = 0
2349 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002350 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002351 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002352 enddef
2353 Func()
2354 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002355 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002356enddef
2357
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002358def Test_nested_closure_used()
2359 var lines =<< trim END
2360 vim9script
2361 def Func()
2362 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002363 var Closure = () => x
2364 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002365 enddef
2366 Func()
2367 assert_equal('hello', g:Myclosure())
2368 END
2369 CheckScriptSuccess(lines)
2370enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002371
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002372def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002373 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002374 vim9script
2375 def FuncA()
2376 FuncB(0)
2377 enddef
2378 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002379 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002380 enddef
2381 FuncA()
2382 END
2383 CheckScriptFailure(lines, 'E1012:')
2384enddef
2385
Bram Moolenaarf112f302020-12-20 17:47:52 +01002386def Test_global_closure()
2387 var lines =<< trim END
2388 vim9script
2389 def ReverseEveryNLines(n: number, line1: number, line2: number)
2390 var mods = 'sil keepj keepp lockm '
2391 var range = ':' .. line1 .. ',' .. line2
2392 def g:Offset(): number
2393 var offset = (line('.') - line1 + 1) % n
2394 return offset != 0 ? offset : n
2395 enddef
2396 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2397 enddef
2398
2399 new
2400 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2401 ReverseEveryNLines(3, 1, 9)
2402 END
2403 CheckScriptSuccess(lines)
2404 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2405 assert_equal(expected, getline(1, 9))
2406 bwipe!
2407enddef
2408
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002409def Test_global_closure_called_directly()
2410 var lines =<< trim END
2411 vim9script
2412 def Outer()
2413 var x = 1
2414 def g:Inner()
2415 var y = x
2416 x += 1
2417 assert_equal(1, y)
2418 enddef
2419 g:Inner()
2420 assert_equal(2, x)
2421 enddef
2422 Outer()
2423 END
2424 CheckScriptSuccess(lines)
2425 delfunc g:Inner
2426enddef
2427
Bram Moolenaar69c76172021-12-02 16:38:52 +00002428def Test_closure_called_from_legacy()
2429 var lines =<< trim END
2430 vim9script
2431 def Func()
2432 var outer = 'foo'
2433 var F = () => {
2434 outer = 'bar'
2435 }
2436 execute printf('call %s()', string(F))
2437 enddef
2438 Func()
2439 END
2440 CheckScriptFailure(lines, 'E1248')
2441enddef
2442
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002443def Test_failure_in_called_function()
2444 # this was using the frame index as the return value
2445 var lines =<< trim END
2446 vim9script
2447 au TerminalWinOpen * eval [][0]
2448 def PopupTerm(a: any)
2449 # make sure typvals on stack are string
2450 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2451 FireEvent()
2452 enddef
2453 def FireEvent()
2454 do TerminalWinOpen
2455 enddef
2456 # use try/catch to make eval fail
2457 try
2458 call PopupTerm(0)
2459 catch
2460 endtry
2461 au! TerminalWinOpen
2462 END
2463 CheckScriptSuccess(lines)
2464enddef
2465
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002466def Test_nested_lambda()
2467 var lines =<< trim END
2468 vim9script
2469 def Func()
2470 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002471 var Lambda1 = () => 7
2472 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002473 var res = Lambda2()
2474 assert_equal([7, 4], res)
2475 enddef
2476 Func()
2477 END
2478 CheckScriptSuccess(lines)
2479enddef
2480
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002481def Test_double_nested_lambda()
2482 var lines =<< trim END
2483 vim9script
2484 def F(head: string): func(string): func(string): string
2485 return (sep: string): func(string): string => ((tail: string): string => {
2486 return head .. sep .. tail
2487 })
2488 enddef
2489 assert_equal('hello-there', F('hello')('-')('there'))
2490 END
2491 CheckScriptSuccess(lines)
2492enddef
2493
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002494def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002495 var lines =<< trim END
2496 vim9script
2497 def F(text: string): func(string): func(string): string
2498 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002499 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002500 })
2501 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002502 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002503 END
2504 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002505
2506 lines =<< trim END
2507 vim9script
2508 echo range(4)->mapnew((_, v) => {
2509 return range(v) ->mapnew((_, s) => {
2510 return string(s)
2511 })
2512 })
2513 END
2514 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002515
2516 lines =<< trim END
2517 vim9script
2518
2519 def s:func()
2520 range(10)
2521 ->mapnew((_, _) => ({
2522 key: range(10)->mapnew((_, _) => {
2523 return ' '
2524 }),
2525 }))
2526 enddef
2527
2528 defcomp
2529 END
2530 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002531enddef
2532
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002533def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002534 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002535 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002536enddef
2537
2538def Test_lambda_arg_shadows_func()
2539 assert_equal([42], Shadowed())
2540enddef
2541
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002542def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002543 var path: string = empty(dir)
2544 \ ? 'empty'
2545 \ : 'full'
2546 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002547enddef
2548
2549def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002550 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002551enddef
2552
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002553def Test_script_var_in_lambda()
2554 var lines =<< trim END
2555 vim9script
2556 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002557 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002558 END
2559 CheckScriptSuccess(lines)
2560enddef
2561
Bram Moolenaar5e654232020-09-16 15:22:00 +02002562def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002563 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002564 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002565 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002566 ->reverse()
2567 return x
2568enddef
2569
2570def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002571 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002572
2573 var lines =<< trim END
2574 vim9script
2575 var res = [{n: 1, m: 2, s: 'xxx'}]
2576 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2577 v.n,
2578 v.m,
2579 substitute(v.s, '.*', 'yyy', '')
2580 ))
2581 assert_equal(['1:2:yyy'], res)
2582 END
2583 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002584enddef
2585
Bram Moolenaarb6571982021-01-08 22:24:19 +01002586def Test_list_lambda()
2587 timer_start(1000, (_) => 0)
2588 var body = execute(timer_info()[0].callback
2589 ->string()
2590 ->substitute("('", ' ', '')
2591 ->substitute("')", '', '')
2592 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002593 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002594enddef
2595
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002596def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002597 var lines =<< trim END
2598 vim9script
2599 var flist: list<func>
2600 for i in range(10)
2601 var inloop = i
2602 flist[i] = () => inloop
2603 endfor
2604 END
2605 CheckScriptSuccess(lines)
2606
2607 lines =<< trim END
2608 vim9script
2609 if true
2610 var outloop = 5
2611 var flist: list<func>
2612 for i in range(10)
2613 flist[i] = () => outloop
2614 endfor
2615 endif
2616 END
2617 CheckScriptSuccess(lines)
2618
2619 lines =<< trim END
2620 vim9script
2621 if true
2622 var outloop = 5
2623 endif
2624 var flist: list<func>
2625 for i in range(10)
2626 flist[i] = () => outloop
2627 endfor
2628 END
2629 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002630
2631 lines =<< trim END
2632 vim9script
2633 for i in range(10)
2634 var Ref = () => 0
2635 endfor
2636 assert_equal(0, ((i) => 0)(0))
2637 END
2638 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002639enddef
2640
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002641def Test_legacy_lambda()
2642 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002643
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002644 var lines =<< trim END
2645 echo {x -> 'hello ' .. x}('foo')
2646 END
2647 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002648
2649 lines =<< trim END
2650 vim9script
2651 def Func()
2652 echo (() => 'no error')()
2653 enddef
2654 legacy call s:Func()
2655 END
2656 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002657enddef
2658
Bram Moolenaarce024c32021-06-26 13:00:49 +02002659def Test_legacy()
2660 var lines =<< trim END
2661 vim9script
2662 func g:LegacyFunction()
2663 let g:legacyvar = 1
2664 endfunc
2665 def Testit()
2666 legacy call g:LegacyFunction()
2667 enddef
2668 Testit()
2669 assert_equal(1, g:legacyvar)
2670 unlet g:legacyvar
2671 delfunc g:LegacyFunction
2672 END
2673 CheckScriptSuccess(lines)
2674enddef
2675
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002676def Test_legacy_errors()
2677 for cmd in ['if', 'elseif', 'else', 'endif',
2678 'for', 'endfor', 'continue', 'break',
2679 'while', 'endwhile',
2680 'try', 'catch', 'finally', 'endtry']
2681 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2682 endfor
2683enddef
2684
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002685def Test_call_legacy_with_dict()
2686 var lines =<< trim END
2687 vim9script
2688 func Legacy() dict
2689 let g:result = self.value
2690 endfunc
2691 def TestDirect()
2692 var d = {value: 'yes', func: Legacy}
2693 d.func()
2694 enddef
2695 TestDirect()
2696 assert_equal('yes', g:result)
2697 unlet g:result
2698
2699 def TestIndirect()
2700 var d = {value: 'foo', func: Legacy}
2701 var Fi = d.func
2702 Fi()
2703 enddef
2704 TestIndirect()
2705 assert_equal('foo', g:result)
2706 unlet g:result
2707
2708 var d = {value: 'bar', func: Legacy}
2709 d.func()
2710 assert_equal('bar', g:result)
2711 unlet g:result
2712 END
2713 CheckScriptSuccess(lines)
2714enddef
2715
Bram Moolenaarab360522021-01-10 14:02:28 +01002716def DoFilterThis(a: string): list<string>
2717 # closure nested inside another closure using argument
2718 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2719 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2720enddef
2721
2722def Test_nested_closure_using_argument()
2723 assert_equal(['x', 'x2'], DoFilterThis('x'))
2724enddef
2725
Bram Moolenaar0186e582021-01-10 18:33:11 +01002726def Test_triple_nested_closure()
2727 var what = 'x'
2728 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2729 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2730 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2731enddef
2732
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002733func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002734 CheckScreendump
2735
2736 let lines =<< trim END
2737 vim9script
2738 def EchoNothing()
2739 silent echo ''
2740 enddef
2741 defcompile
2742 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002743 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002744
2745 " Check that the balloon shows up after a mouse move
2746 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002747 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002748 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2749
2750 " clean up
2751 call StopVimInTerminal(buf)
2752 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002753endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002754
Bram Moolenaar171fb922020-10-28 16:54:47 +01002755def SilentlyError()
2756 execute('silent! invalid')
2757 g:did_it = 'yes'
2758enddef
2759
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002760func UserError()
2761 silent! invalid
2762endfunc
2763
2764def SilentlyUserError()
2765 UserError()
2766 g:did_it = 'yes'
2767enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002768
2769" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002770func Test_ignore_silent_error()
2771 let g:did_it = 'no'
2772 call SilentlyError()
2773 call assert_equal('yes', g:did_it)
2774
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002775 let g:did_it = 'no'
2776 call SilentlyUserError()
2777 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002778
2779 unlet g:did_it
2780endfunc
2781
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002782def Test_ignore_silent_error_in_filter()
2783 var lines =<< trim END
2784 vim9script
2785 def Filter(winid: number, key: string): bool
2786 if key == 'o'
2787 silent! eval [][0]
2788 return true
2789 endif
2790 return popup_filter_menu(winid, key)
2791 enddef
2792
Bram Moolenaare0de1712020-12-02 17:36:54 +01002793 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002794 feedkeys("o\r", 'xnt')
2795 END
2796 CheckScriptSuccess(lines)
2797enddef
2798
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002799def Fibonacci(n: number): number
2800 if n < 2
2801 return n
2802 else
2803 return Fibonacci(n - 1) + Fibonacci(n - 2)
2804 endif
2805enddef
2806
Bram Moolenaar985116a2020-07-12 17:31:09 +02002807def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002808 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002809enddef
2810
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002811def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002812 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002813 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002814 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002815 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002816 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002817enddef
2818
2819def Test_closure_in_map()
2820 mkdir('XclosureDir/tdir', 'p')
2821 writefile(['111'], 'XclosureDir/file1')
2822 writefile(['222'], 'XclosureDir/file2')
2823 writefile(['333'], 'XclosureDir/tdir/file3')
2824
Bram Moolenaare0de1712020-12-02 17:36:54 +01002825 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002826
2827 delete('XclosureDir', 'rf')
2828enddef
2829
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002830def Test_invalid_function_name()
2831 var lines =<< trim END
2832 vim9script
2833 def s: list<string>
2834 END
2835 CheckScriptFailure(lines, 'E129:')
2836
2837 lines =<< trim END
2838 vim9script
2839 def g: list<string>
2840 END
2841 CheckScriptFailure(lines, 'E129:')
2842
2843 lines =<< trim END
2844 vim9script
2845 def <SID>: list<string>
2846 END
2847 CheckScriptFailure(lines, 'E884:')
2848
2849 lines =<< trim END
2850 vim9script
2851 def F list<string>
2852 END
2853 CheckScriptFailure(lines, 'E488:')
2854enddef
2855
Bram Moolenaara90afb92020-07-15 22:38:56 +02002856def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002857 var lines =<< trim END
2858 var Xsetlist: func
2859 Xsetlist = function('setloclist', [0])
2860 Xsetlist([], ' ', {title: 'test'})
2861 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002862
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002863 Xsetlist = function('setloclist', [0, [], ' '])
2864 Xsetlist({title: 'test'})
2865 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002866
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002867 Xsetlist = function('setqflist')
2868 Xsetlist([], ' ', {title: 'test'})
2869 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002870
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002871 Xsetlist = function('setqflist', [[], ' '])
2872 Xsetlist({title: 'test'})
2873 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002874
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002875 var Len: func: number = function('len', ['word'])
2876 assert_equal(4, Len())
2877
2878 var RepeatFunc = function('repeat', ['o'])
2879 assert_equal('ooooo', RepeatFunc(5))
2880 END
2881 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002882
2883 lines =<< trim END
2884 vim9script
2885 def Foo(Parser: any)
2886 enddef
2887 var Expr: func(dict<any>): dict<any>
2888 const Call = Foo(Expr)
2889 END
2890 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002891enddef
2892
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002893def Test_cmd_modifier()
2894 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002895 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002896enddef
2897
2898def Test_restore_modifiers()
2899 # check that when compiling a :def function command modifiers are not messed
2900 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002901 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002902 vim9script
2903 set eventignore=
2904 autocmd QuickFixCmdPost * copen
2905 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002906 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002907 enddef
2908 func Func()
2909 noautocmd call s:AutocmdsDisabled()
2910 let g:ei_after = &eventignore
2911 endfunc
2912 Func()
2913 END
2914 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002915 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002916enddef
2917
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002918def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002919 eval 1 + 2
2920 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002921 # call not on fourth line
2922 StackBot()
2923enddef
2924
2925def StackBot()
2926 # throw an error
2927 eval [][0]
2928enddef
2929
2930def Test_callstack_def()
2931 try
2932 StackTop()
2933 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002934 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002935 endtry
2936enddef
2937
Bram Moolenaare8211a32020-10-09 22:04:29 +02002938" Re-using spot for variable used in block
2939def Test_block_scoped_var()
2940 var lines =<< trim END
2941 vim9script
2942 def Func()
2943 var x = ['a', 'b', 'c']
2944 if 1
2945 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002946 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002947 endif
2948 var z = x
2949 assert_equal(['x', 'x', 'x'], z)
2950 enddef
2951 Func()
2952 END
2953 CheckScriptSuccess(lines)
2954enddef
2955
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002956def Test_reset_did_emsg()
2957 var lines =<< trim END
2958 @s = 'blah'
2959 au BufWinLeave * #
2960 def Func()
2961 var winid = popup_create('popup', {})
2962 exe '*s'
2963 popup_close(winid)
2964 enddef
2965 Func()
2966 END
2967 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002968 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002969enddef
2970
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002971def Test_did_emsg_reset()
2972 # executing an autocommand resets did_emsg, this should not result in a
2973 # builtin function considered failing
2974 var lines =<< trim END
2975 vim9script
2976 au BufWinLeave * #
2977 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02002978 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002979 eval [][0]
2980 enddef
2981 nno <F3> <cmd>call <sid>Func()<cr>
2982 feedkeys("\<F3>\e", 'xt')
2983 END
2984 writefile(lines, 'XemsgReset')
2985 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
2986 delete('XemsgReset')
2987 nunmap <F3>
2988 au! BufWinLeave
2989enddef
2990
Bram Moolenaar56602ba2020-12-05 21:22:08 +01002991def Test_abort_with_silent_call()
2992 var lines =<< trim END
2993 vim9script
2994 g:result = 'none'
2995 def Func()
2996 g:result += 3
2997 g:result = 'yes'
2998 enddef
2999 # error is silenced, but function aborts on error
3000 silent! Func()
3001 assert_equal('none', g:result)
3002 unlet g:result
3003 END
3004 CheckScriptSuccess(lines)
3005enddef
3006
Bram Moolenaarf665e972020-12-05 19:17:16 +01003007def Test_continues_with_silent_error()
3008 var lines =<< trim END
3009 vim9script
3010 g:result = 'none'
3011 def Func()
3012 silent! g:result += 3
3013 g:result = 'yes'
3014 enddef
3015 # error is silenced, function does not abort
3016 Func()
3017 assert_equal('yes', g:result)
3018 unlet g:result
3019 END
3020 CheckScriptSuccess(lines)
3021enddef
3022
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003023def Test_abort_even_with_silent()
3024 var lines =<< trim END
3025 vim9script
3026 g:result = 'none'
3027 def Func()
3028 eval {-> ''}() .. '' .. {}['X']
3029 g:result = 'yes'
3030 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003031 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003032 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003033 unlet g:result
3034 END
3035 CheckScriptSuccess(lines)
3036enddef
3037
Bram Moolenaarf665e972020-12-05 19:17:16 +01003038def Test_cmdmod_silent_restored()
3039 var lines =<< trim END
3040 vim9script
3041 def Func()
3042 g:result = 'none'
3043 silent! g:result += 3
3044 g:result = 'none'
3045 g:result += 3
3046 enddef
3047 Func()
3048 END
3049 # can't use CheckScriptFailure, it ignores the :silent!
3050 var fname = 'Xdefsilent'
3051 writefile(lines, fname)
3052 var caught = 'no'
3053 try
3054 exe 'source ' .. fname
3055 catch /E1030:/
3056 caught = 'yes'
3057 assert_match('Func, line 4', v:throwpoint)
3058 endtry
3059 assert_equal('yes', caught)
3060 delete(fname)
3061enddef
3062
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003063def Test_cmdmod_silent_nested()
3064 var lines =<< trim END
3065 vim9script
3066 var result = ''
3067
3068 def Error()
3069 result ..= 'Eb'
3070 eval [][0]
3071 result ..= 'Ea'
3072 enddef
3073
3074 def Crash()
3075 result ..= 'Cb'
3076 sil! Error()
3077 result ..= 'Ca'
3078 enddef
3079
3080 Crash()
3081 assert_equal('CbEbEaCa', result)
3082 END
3083 CheckScriptSuccess(lines)
3084enddef
3085
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003086def Test_dict_member_with_silent()
3087 var lines =<< trim END
3088 vim9script
3089 g:result = 'none'
3090 var d: dict<any>
3091 def Func()
3092 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003093 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003094 catch
3095 endtry
3096 enddef
3097 silent! Func()
3098 assert_equal('0', g:result)
3099 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003100 END
3101 CheckScriptSuccess(lines)
3102enddef
3103
Bram Moolenaarf9041332021-01-21 19:41:16 +01003104def Test_skip_cmds_with_silent()
3105 var lines =<< trim END
3106 vim9script
3107
3108 def Func(b: bool)
3109 Crash()
3110 enddef
3111
3112 def Crash()
3113 sil! :/not found/d _
3114 sil! :/not found/put _
3115 enddef
3116
3117 Func(true)
3118 END
3119 CheckScriptSuccess(lines)
3120enddef
3121
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003122def Test_opfunc()
3123 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3124 def g:Opfunc(_: any): string
3125 setline(1, 'ASDF')
3126 return ''
3127 enddef
3128 new
3129 setline(1, 'asdf')
3130 feedkeys("\<F3>$", 'x')
3131 assert_equal('ASDF', getline(1))
3132
3133 bwipe!
3134 nunmap <F3>
3135enddef
3136
Bram Moolenaar077a4232020-12-22 18:33:27 +01003137" this was crashing on exit
3138def Test_nested_lambda_in_closure()
3139 var lines =<< trim END
3140 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003141 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003142 def Outer()
3143 def g:Inner()
3144 echo map([1, 2, 3], {_, v -> v + 1})
3145 enddef
3146 g:Inner()
3147 enddef
3148 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003149 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003150 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003151 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003152 return
3153 endif
3154 assert_equal(['Done'], readfile('XnestedDone'))
3155 delete('XnestedDone')
3156enddef
3157
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003158def Test_check_func_arg_types()
3159 var lines =<< trim END
3160 vim9script
3161 def F1(x: string): string
3162 return x
3163 enddef
3164
3165 def F2(x: number): number
3166 return x + 1
3167 enddef
3168
3169 def G(g: func): dict<func>
3170 return {f: g}
3171 enddef
3172
3173 def H(d: dict<func>): string
3174 return d.f('a')
3175 enddef
3176 END
3177
3178 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3179 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3180enddef
3181
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003182def Test_list_any_type_checked()
3183 var lines =<< trim END
3184 vim9script
3185 def Foo()
3186 --decl--
3187 Bar(l)
3188 enddef
3189 def Bar(ll: list<dict<any>>)
3190 enddef
3191 Foo()
3192 END
3193 lines[2] = 'var l: list<any>'
3194 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3195
3196 lines[2] = 'var l: list<any> = []'
3197 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3198
3199 lines[2] = 'var l: list<any> = [11]'
3200 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3201enddef
3202
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003203def Test_compile_error()
3204 var lines =<< trim END
3205 def g:Broken()
3206 echo 'a' + {}
3207 enddef
3208 call g:Broken()
3209 END
3210 # First call: compilation error
3211 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3212
3213 # Second call won't try compiling again
3214 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003215 delfunc g:Broken
3216
3217 # No error when compiling with :silent!
3218 lines =<< trim END
3219 def g:Broken()
3220 echo 'a' + []
3221 enddef
3222 silent! defcompile
3223 END
3224 CheckScriptSuccess(lines)
3225
3226 # Calling the function won't try compiling again
3227 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3228 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003229enddef
3230
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003231def Test_ignored_argument()
3232 var lines =<< trim END
3233 vim9script
3234 def Ignore(_, _): string
3235 return 'yes'
3236 enddef
3237 assert_equal('yes', Ignore(1, 2))
3238
3239 func Ok(_)
3240 return a:_
3241 endfunc
3242 assert_equal('ok', Ok('ok'))
3243
3244 func Oktoo()
3245 let _ = 'too'
3246 return _
3247 endfunc
3248 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003249
3250 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003251 END
3252 CheckScriptSuccess(lines)
3253
3254 lines =<< trim END
3255 def Ignore(_: string): string
3256 return _
3257 enddef
3258 defcompile
3259 END
3260 CheckScriptFailure(lines, 'E1181:', 1)
3261
3262 lines =<< trim END
3263 var _ = 1
3264 END
3265 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003266
3267 lines =<< trim END
3268 var x = _
3269 END
3270 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003271enddef
3272
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003273def Test_too_many_arguments()
3274 var lines =<< trim END
3275 echo [0, 1, 2]->map(() => 123)
3276 END
3277 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3278
3279 lines =<< trim END
3280 echo [0, 1, 2]->map((_) => 123)
3281 END
3282 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3283enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003284
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003285def Test_closing_brace_at_start_of_line()
3286 var lines =<< trim END
3287 def Func()
3288 enddef
3289 Func(
3290 )
3291 END
3292 call CheckDefAndScriptSuccess(lines)
3293enddef
3294
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003295func CreateMydict()
3296 let g:mydict = {}
3297 func g:mydict.afunc()
3298 let g:result = self.key
3299 endfunc
3300endfunc
3301
3302def Test_numbered_function_reference()
3303 CreateMydict()
3304 var output = execute('legacy func g:mydict.afunc')
3305 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3306 execute 'function(' .. funcName .. ', [], {key: 42})()'
3307 # check that the function still exists
3308 assert_equal(output, execute('legacy func g:mydict.afunc'))
3309 unlet g:mydict
3310enddef
3311
Bram Moolenaar20677332021-06-06 17:02:53 +02003312if has('python3')
3313 def Test_python3_heredoc()
3314 py3 << trim EOF
3315 import vim
3316 vim.vars['didit'] = 'yes'
3317 EOF
3318 assert_equal('yes', g:didit)
3319
3320 python3 << trim EOF
3321 import vim
3322 vim.vars['didit'] = 'again'
3323 EOF
3324 assert_equal('again', g:didit)
3325 enddef
3326endif
3327
3328" This messes up syntax highlight, keep near the end.
3329if has('lua')
3330 def Test_lua_heredoc()
3331 g:d = {}
3332 lua << trim EOF
3333 x = vim.eval('g:d')
3334 x['key'] = 'val'
3335 EOF
3336 assert_equal('val', g:d.key)
3337 enddef
3338endif
3339
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003340
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003341" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker