blob: 40affb39cb30a83e6f729a32be9848e70fd4cdba [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 Moolenaar5deeb3f2020-04-05 17:08:17 +02001227" Default arg and varargs
1228def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001229 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001230 for s in rest
1231 res ..= ',' .. s
1232 endfor
1233 return res
1234enddef
1235
1236def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001237 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001238 MyDefVarargs('one')->assert_equal('one,foo')
1239 MyDefVarargs('one', 'two')->assert_equal('one,two')
1240 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001241 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001242 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001243 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001244 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001245
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001246 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001247 vim9script
1248 def Func(...l: list<string>)
1249 echo l
1250 enddef
1251 Func('a', 'b', 'c')
1252 END
1253 CheckScriptSuccess(lines)
1254
1255 lines =<< trim END
1256 vim9script
1257 def Func(...l: list<string>)
1258 echo l
1259 enddef
1260 Func()
1261 END
1262 CheckScriptSuccess(lines)
1263
1264 lines =<< trim END
1265 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001266 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001267 echo l
1268 enddef
1269 Func(0)
1270 END
1271 CheckScriptSuccess(lines)
1272
1273 lines =<< trim END
1274 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001275 def Func(...l: any)
1276 echo l
1277 enddef
1278 Func(0)
1279 END
1280 CheckScriptFailure(lines, 'E1180:', 2)
1281
1282 lines =<< trim END
1283 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001284 def Func(..._l: list<string>)
1285 echo _l
1286 enddef
1287 Func('a', 'b', 'c')
1288 END
1289 CheckScriptSuccess(lines)
1290
1291 lines =<< trim END
1292 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001293 def Func(...l: list<string>)
1294 echo l
1295 enddef
1296 Func(1, 2, 3)
1297 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001298 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001299
1300 lines =<< trim END
1301 vim9script
1302 def Func(...l: list<string>)
1303 echo l
1304 enddef
1305 Func('a', 9)
1306 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001307 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001308
1309 lines =<< trim END
1310 vim9script
1311 def Func(...l: list<string>)
1312 echo l
1313 enddef
1314 Func(1, 'a')
1315 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001316 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001317
1318 lines =<< trim END
1319 vim9script
1320 def Func( # some comment
1321 ...l = []
1322 )
1323 echo l
1324 enddef
1325 END
1326 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001327
1328 lines =<< trim END
1329 vim9script
1330 def DoIt()
1331 g:Later('')
1332 enddef
1333 defcompile
1334 def g:Later(...l: list<number>)
1335 enddef
1336 DoIt()
1337 END
1338 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001339enddef
1340
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001341let s:value = ''
1342
1343def FuncOneDefArg(opt = 'text')
1344 s:value = opt
1345enddef
1346
1347def FuncTwoDefArg(nr = 123, opt = 'text'): string
1348 return nr .. opt
1349enddef
1350
1351def FuncVarargs(...arg: list<string>): string
1352 return join(arg, ',')
1353enddef
1354
1355def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001356 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001357 RefDefArg = FuncOneDefArg
1358 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001359 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001360 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001361 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001362
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001363 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001364 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001365 RefDef2Arg()->assert_equal('123text')
1366 RefDef2Arg(99)->assert_equal('99text')
1367 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001368
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001369 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1370 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001371
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001372 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001373 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001374 RefVarargs()->assert_equal('')
1375 RefVarargs('one')->assert_equal('one')
1376 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001377
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001378 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1379 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001380enddef
1381
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001382" Only varargs
1383def MyVarargsOnly(...args: list<string>): string
1384 return join(args, ',')
1385enddef
1386
1387def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001388 MyVarargsOnly()->assert_equal('')
1389 MyVarargsOnly('one')->assert_equal('one')
1390 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001391 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1392 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001393enddef
1394
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001395def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001396 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001397 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001398 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001399enddef
1400
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001401def DictArg(arg: dict<string>)
1402 arg['key'] = 'value'
1403enddef
1404
1405def ListArg(arg: list<string>)
1406 arg[0] = 'value'
1407enddef
1408
1409def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001410 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001411 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001412 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001413 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001414 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001415 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001416 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001417
Bram Moolenaard2c61702020-09-06 15:58:36 +02001418 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001419 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001420enddef
1421
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001422" These argument names are reserved in legacy functions.
1423def WithReservedNames(firstline: string, lastline: string): string
1424 return firstline .. lastline
1425enddef
1426
1427def Test_argument_names()
1428 assert_equal('OK', WithReservedNames('O', 'K'))
1429enddef
1430
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001431def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001432 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001433 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001434enddef
1435
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001436func DefinedLater(arg)
1437 return a:arg
1438endfunc
1439
1440def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001441 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001442 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
1443 assert_fails('g:NotAFunc()', 'E117:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001444
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001445 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001446 vim9script
1447 def RetNumber(): number
1448 return 123
1449 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001450 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001451 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001452 END
1453 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001454
1455 lines =<< trim END
1456 vim9script
1457 def RetNumber(): number
1458 return 123
1459 enddef
1460 def Bar(F: func: number): number
1461 return F()
1462 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001463 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001464 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001465 END
1466 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001467
1468 lines =<< trim END
1469 vim9script
1470 def UseNumber(nr: number)
1471 echo nr
1472 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001473 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001474 Funcref(123)
1475 END
1476 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001477
1478 lines =<< trim END
1479 vim9script
1480 def UseNumber(nr: number)
1481 echo nr
1482 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001483 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001484 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001485 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001486
1487 lines =<< trim END
1488 vim9script
1489 def EchoNr(nr = 34)
1490 g:echo = nr
1491 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001492 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001493 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001494 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001495 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001496 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001497 END
1498 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001499
1500 lines =<< trim END
1501 vim9script
1502 def EchoList(...l: list<number>)
1503 g:echo = l
1504 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001505 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001506 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001507 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001508 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001509 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001510 END
1511 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001512
1513 lines =<< trim END
1514 vim9script
1515 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1516 g:optarg = opt
1517 g:listarg = l
1518 return nr
1519 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001520 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001521 Funcref(10)->assert_equal(10)
1522 g:optarg->assert_equal(12)
1523 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001524
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001525 Funcref(11, 22)->assert_equal(11)
1526 g:optarg->assert_equal(22)
1527 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001528
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001529 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1530 g:optarg->assert_equal(18)
1531 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001532 END
1533 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001534enddef
1535
1536let SomeFunc = function('len')
1537let NotAFunc = 'text'
1538
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001539def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001540 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001541 var Ref1: func(bool): string
1542 var Ref2: func(bool): number
1543 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001544 Ref3 = g:cond ? Ref1 : Ref2
1545
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001546 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001547 var Refa1: func(bool): number
1548 var Refa2: func(bool, number): number
1549 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001550 Refa3 = g:cond ? Refa1 : Refa2
1551
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001552 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001553 var Refb1: func(bool, string): number
1554 var Refb2: func(string, number): number
1555 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001556 Refb3 = g:cond ? Refb1 : Refb2
1557enddef
1558
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001559def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001560 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001561enddef
1562
1563def DefinedEvenLater(arg: string): string
1564 return arg
1565enddef
1566
1567def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001568 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001569 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001570enddef
1571
1572def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001573 CheckScriptFailure([
1574 'def Func(): number',
1575 'return "a"',
1576 'enddef',
1577 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001578 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001579 CheckScriptFailure([
1580 'def Func(): string',
1581 'return 1',
1582 'enddef',
1583 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001584 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001585 CheckScriptFailure([
1586 'def Func(): void',
1587 'return "a"',
1588 'enddef',
1589 'defcompile'],
1590 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001591 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001592 CheckScriptFailure([
1593 'def Func()',
1594 'return "a"',
1595 'enddef',
1596 'defcompile'],
1597 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001598 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001599
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001600 CheckScriptFailure([
1601 'def Func(): number',
1602 'return',
1603 'enddef',
1604 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001605 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001606
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001607 CheckScriptFailure([
1608 'def Func():number',
1609 'return 123',
1610 'enddef',
1611 'defcompile'], 'E1069:')
1612 delfunc! g:Func
1613
1614 CheckScriptFailure([
1615 'def Func() :number',
1616 'return 123',
1617 'enddef',
1618 'defcompile'], 'E1059:')
1619 delfunc! g:Func
1620
1621 CheckScriptFailure([
1622 'def Func() : number',
1623 'return 123',
1624 'enddef',
1625 'defcompile'], 'E1059:')
1626 delfunc! g:Func
1627
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001628 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001629 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001630 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001631 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001632 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001633 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001634
1635 CheckScriptFailure([
1636 'vim9script',
1637 'def FuncB()',
1638 ' return 123',
1639 'enddef',
1640 'def FuncA()',
1641 ' FuncB()',
1642 'enddef',
1643 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001644enddef
1645
1646def Test_arg_type_wrong()
1647 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001648 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001649 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001650 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001651 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1652 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001653enddef
1654
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001655def Test_white_space_before_comma()
1656 var lines =<< trim END
1657 vim9script
1658 def Func(a: number , b: number)
1659 enddef
1660 END
1661 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001662 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001663enddef
1664
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001665def Test_white_space_after_comma()
1666 var lines =<< trim END
1667 vim9script
1668 def Func(a: number,b: number)
1669 enddef
1670 END
1671 CheckScriptFailure(lines, 'E1069:')
1672
1673 # OK in legacy function
1674 lines =<< trim END
1675 vim9script
1676 func Func(a,b)
1677 endfunc
1678 END
1679 CheckScriptSuccess(lines)
1680enddef
1681
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001682def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001683 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001684 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001685 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001686 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001687 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001688 enddef
1689 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001690 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001691
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001692 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001693 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001694 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001695
Bram Moolenaar67979662020-06-20 22:50:47 +02001696 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001697 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001698 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001699
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001700 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001701 def ListFunc(arg: list<number>)
1702 listvar = arg
1703 enddef
1704 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001705 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001706
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001707 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001708 def DictFunc(arg: dict<number>)
1709 dictvar = arg
1710 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001711 {a: 1, b: 2}->DictFunc()
1712 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001713 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001714 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001715 enddef
1716 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001717 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001718
Bram Moolenaare0de1712020-12-02 17:36:54 +01001719 {a: 3, b: 4}->DictFunc()
1720 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001721
1722 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001723 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001724 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001725 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001726
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001727 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001728 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001729 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001730 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001731 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001732 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001733
1734 def UseString()
1735 'xyork'->MyFunc()
1736 enddef
1737 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001738 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001739
Bram Moolenaar10409562020-07-29 20:00:38 +02001740 def UseString2()
1741 "knife"->MyFunc()
1742 enddef
1743 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001744 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001745
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001746 # prepending a colon makes it a mark
1747 new
1748 setline(1, ['aaa', 'bbb', 'ccc'])
1749 normal! 3Gmt1G
1750 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001751 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001752 bwipe!
1753
Bram Moolenaare6b53242020-07-01 17:28:33 +02001754 MyFunc(
1755 'continued'
1756 )
1757 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001758 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001759 )
1760
1761 call MyFunc(
1762 'more'
1763 ..
1764 'lines'
1765 )
1766 assert_equal(
1767 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001768 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001769 END
1770 writefile(lines, 'Xcall.vim')
1771 source Xcall.vim
1772 delete('Xcall.vim')
1773enddef
1774
1775def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001776 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001777 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001778 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001779 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001780 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001781 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001782 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001783 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001784 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001785enddef
1786
Bram Moolenaar65b95452020-07-19 14:03:09 +02001787def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001788 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001789 vim9script
1790 def MyFunc(arg: string)
1791 echo arg
1792 enddef
1793 MyFunc(1234)
1794 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001795 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001796enddef
1797
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001798def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001799 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001800 vim9script
1801 const var = ''
1802 def MyFunc(arg: string)
1803 var = 'asdf'
1804 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001805 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001806 END
1807 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001808 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001809 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001810
1811 lines =<< trim END
1812 const g:Aconst = 77
1813 def Change()
1814 # comment
1815 g:Aconst = 99
1816 enddef
1817 call Change()
1818 unlet g:Aconst
1819 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001820 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001821enddef
1822
1823" Test that inside :function a Python function can be defined, :def is not
1824" recognized.
1825func Test_function_python()
1826 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001827 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001828 execute py "<< EOF"
1829def do_something():
1830 return 1
1831EOF
1832endfunc
1833
1834def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001835 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001836 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001837 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001838 echo 'hello'
1839 enddef
1840
1841 def CallGoneSoon()
1842 GoneSoon()
1843 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001844 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001845
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001846 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001847 CallGoneSoon()
1848 END
1849 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001850 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1851 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001852
1853 delete('XToDelFunc')
1854enddef
1855
1856def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001857 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001858 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001859 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001860 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001861 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001862 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001863 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001864 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001865 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001867 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001868 g:Func1()->assert_equal('Func1')
1869 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870
1871 delfunc! Func0
1872 delfunc! Func1
1873 delfunc! Func2
1874enddef
1875
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001876def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001877 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001878 vim9script
1879 func Func(arg)
1880 echo a:arg
1881 endfunc
1882 Func('text')
1883 END
1884 writefile(lines, 'XVim9Func')
1885 so XVim9Func
1886
1887 delete('XVim9Func')
1888enddef
1889
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001890let s:funcResult = 0
1891
1892def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001893 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001894enddef
1895
1896def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001897 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001898 return 1234
1899enddef
1900
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001901def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001902 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001903 return 'text'
1904enddef
1905
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001906def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001907 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001908enddef
1909
1910def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001911 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001912 return arg
1913enddef
1914
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001915def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001916 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001917enddef
1918
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001919def FuncOneArgRetString(arg: string): string
1920 return arg
1921enddef
1922
Bram Moolenaar89228602020-04-05 22:14:54 +02001923def FuncOneArgRetAny(arg: any): any
1924 return arg
1925enddef
1926
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001927def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001928 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001929 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001930 Ref1 = FuncNoArgNoRet
1931 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001932 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001933
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001934 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001935 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001936 Ref2 = FuncNoArgNoRet
1937 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001938 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001939
Bram Moolenaar53900992020-08-22 19:02:02 +02001940 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001941 Ref2 = FuncOneArgNoRet
1942 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001943 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001944
Bram Moolenaar53900992020-08-22 19:02:02 +02001945 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001946 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001947 Ref2()->assert_equal(1234)
1948 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001949
Bram Moolenaar53900992020-08-22 19:02:02 +02001950 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001951 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001952 Ref2(13)->assert_equal(13)
1953 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001954enddef
1955
Bram Moolenaar9978d472020-07-05 16:01:56 +02001956def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001957 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02001958 for n in repeat([1], 3)
1959 res += n
1960 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001961 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02001962
1963 res = 0
1964 for n in add([1, 2], 3)
1965 res += n
1966 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001967 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02001968enddef
1969
Bram Moolenaar846178a2020-07-05 17:04:13 +02001970def Test_argv_return_type()
1971 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001972 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02001973 for name in argv()
1974 res ..= name
1975 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001976 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02001977enddef
1978
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001979def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001980 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001981 RefVoid = FuncNoArgNoRet
1982 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001983 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
1984 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001985
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001986 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001987 RefAny = FuncNoArgRetNumber
1988 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001989 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
1990 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001991
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001992 var RefAnyNoArgs: func: any = RefAny
1993
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001994 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001995 RefNr = FuncNoArgRetNumber
1996 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001997 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
1998 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001999
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002000 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002001 RefStr = FuncNoArgRetString
2002 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002003 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2004 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002005enddef
2006
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002007def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002008 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002009
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002010 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2011 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2012 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2013 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2014 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2015 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 +02002016
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002017 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2018 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2019 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:')
2020 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002021enddef
2022
Bram Moolenaar89228602020-04-05 22:14:54 +02002023def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002024 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002025 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002026 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002027
2028 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002029 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002030
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002031 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002032 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002033 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002034
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002035 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002036enddef
2037
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002038def Test_func_common_type()
2039 def FuncOne(n: number): number
2040 return n
2041 enddef
2042 def FuncTwo(s: string): number
2043 return len(s)
2044 enddef
2045 def FuncThree(n: number, s: string): number
2046 return n + len(s)
2047 enddef
2048 var list = [FuncOne, FuncTwo, FuncThree]
2049 assert_equal(8, list[0](8))
2050 assert_equal(4, list[1]('word'))
2051 assert_equal(7, list[2](3, 'word'))
2052enddef
2053
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002054def MultiLine(
2055 arg1: string,
2056 arg2 = 1234,
2057 ...rest: list<string>
2058 ): string
2059 return arg1 .. arg2 .. join(rest, '-')
2060enddef
2061
Bram Moolenaar2c330432020-04-13 14:41:35 +02002062def MultiLineComment(
2063 arg1: string, # comment
2064 arg2 = 1234, # comment
2065 ...rest: list<string> # comment
2066 ): string # comment
2067 return arg1 .. arg2 .. join(rest, '-')
2068enddef
2069
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002070def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002071 MultiLine('text')->assert_equal('text1234')
2072 MultiLine('text', 777)->assert_equal('text777')
2073 MultiLine('text', 777, 'one')->assert_equal('text777one')
2074 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002075enddef
2076
Bram Moolenaar23e03252020-04-12 22:22:31 +02002077func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002078 call MultiLine('text')->assert_equal('text1234')
2079 call MultiLine('text', 777)->assert_equal('text777')
2080 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2081 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002082endfunc
2083
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002084
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002085" When using CheckScriptFailure() for the below test, E1010 is generated instead
2086" of E1056.
2087func Test_E1056_1059()
2088 let caught_1056 = 0
2089 try
2090 def F():
2091 return 1
2092 enddef
2093 catch /E1056:/
2094 let caught_1056 = 1
2095 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002096 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002097
2098 let caught_1059 = 0
2099 try
2100 def F5(items : list)
2101 echo 'a'
2102 enddef
2103 catch /E1059:/
2104 let caught_1059 = 1
2105 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002106 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002107endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002108
Bram Moolenaar015f4262020-05-05 21:25:22 +02002109func DelMe()
2110 echo 'DelMe'
2111endfunc
2112
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002113def Test_error_reporting()
2114 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002115 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002116 " comment
2117 def Func()
2118 # comment
2119 # comment
2120 invalid
2121 enddef
2122 defcompile
2123 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002124 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002125 try
2126 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002127 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002128 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002129 v:exception->assert_match('Invalid command: invalid')
2130 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002131 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002132 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002133
2134 # comment lines after the start of the function
2135 lines =<< trim END
2136 " comment
2137 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002138 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002139 # comment
2140 # comment
2141 invalid
2142 enddef
2143 defcompile
2144 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002145 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002146 try
2147 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002148 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002149 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002150 v:exception->assert_match('Invalid command: invalid')
2151 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002152 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002153 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002154
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002155 lines =<< trim END
2156 vim9script
2157 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002158 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002159 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002160 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002161 enddef
2162 defcompile
2163 Func()
2164 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002165 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002166 try
2167 source Xdef
2168 assert_report('should have failed')
2169 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002170 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002171 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002172 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002173
Bram Moolenaar08052222020-09-14 17:04:31 +02002174 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002175enddef
2176
Bram Moolenaar015f4262020-05-05 21:25:22 +02002177def Test_deleted_function()
2178 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002179 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002180 'delfunc g:DelMe',
2181 'echo RefMe()'], 'E117:')
2182enddef
2183
2184def Test_unknown_function()
2185 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002186 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002187 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002188enddef
2189
Bram Moolenaar328eac22021-01-07 19:23:08 +01002190def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002191 return Ref('more')
2192enddef
2193
2194def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002195 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002196 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002197enddef
2198
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002199def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002200 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002201 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002202enddef
2203
2204def Test_closure_ref_after_return()
2205 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002206 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002207 unlet g:Ref
2208enddef
2209
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002210def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002211 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002212 g:Extend = (s) => local->add(s)
2213 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002214enddef
2215
2216def Test_closure_two_refs()
2217 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002218 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002219 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002220 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002221 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002222 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002223
2224 unlet g:Extend
2225 unlet g:Read
2226enddef
2227
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002228def ReadRef(Ref: func(): list<string>): string
2229 return join(Ref(), ' ')
2230enddef
2231
Bram Moolenaar5e654232020-09-16 15:22:00 +02002232def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002233 Ref(add)
2234enddef
2235
2236def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002237 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002238 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002239 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002240 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002241 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002242 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002243
2244 unlet g:Extend
2245 unlet g:Read
2246enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002247
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002248def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002249 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002250 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002251enddef
2252
2253def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002254 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002255 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002256enddef
2257
2258def Test_closure_using_argument()
2259 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002260 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002261
2262 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002263 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002264
2265 unlet g:UseArg
2266 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002267
2268 var lines =<< trim END
2269 vim9script
2270 def Test(Fun: func(number): number): list<number>
2271 return map([1, 2, 3], (_, i) => Fun(i))
2272 enddef
2273 def Inc(nr: number): number
2274 return nr + 2
2275 enddef
2276 assert_equal([3, 4, 5], Test(Inc))
2277 END
2278 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002279enddef
2280
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002281def MakeGetAndAppendRefs()
2282 var local = 'a'
2283
2284 def Append(arg: string)
2285 local ..= arg
2286 enddef
2287 g:Append = Append
2288
2289 def Get(): string
2290 return local
2291 enddef
2292 g:Get = Get
2293enddef
2294
2295def Test_closure_append_get()
2296 MakeGetAndAppendRefs()
2297 g:Get()->assert_equal('a')
2298 g:Append('-b')
2299 g:Get()->assert_equal('a-b')
2300 g:Append('-c')
2301 g:Get()->assert_equal('a-b-c')
2302
2303 unlet g:Append
2304 unlet g:Get
2305enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002306
Bram Moolenaar04b12692020-05-04 23:24:44 +02002307def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002308 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002309 def Closure(arg: string): string
2310 return local .. arg
2311 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002312 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002313enddef
2314
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002315func GetResult(Ref)
2316 return a:Ref('some')
2317endfunc
2318
2319def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002320 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002321 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002322 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002323enddef
2324
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002325def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002326 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002327 vim9script
2328 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002329 var name = 0
2330 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002331 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002332 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002333 enddef
2334 Func()
2335 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002336 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002337enddef
2338
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002339def Test_nested_closure_used()
2340 var lines =<< trim END
2341 vim9script
2342 def Func()
2343 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002344 var Closure = () => x
2345 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002346 enddef
2347 Func()
2348 assert_equal('hello', g:Myclosure())
2349 END
2350 CheckScriptSuccess(lines)
2351enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002352
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002353def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002354 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002355 vim9script
2356 def FuncA()
2357 FuncB(0)
2358 enddef
2359 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002360 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002361 enddef
2362 FuncA()
2363 END
2364 CheckScriptFailure(lines, 'E1012:')
2365enddef
2366
Bram Moolenaarf112f302020-12-20 17:47:52 +01002367def Test_global_closure()
2368 var lines =<< trim END
2369 vim9script
2370 def ReverseEveryNLines(n: number, line1: number, line2: number)
2371 var mods = 'sil keepj keepp lockm '
2372 var range = ':' .. line1 .. ',' .. line2
2373 def g:Offset(): number
2374 var offset = (line('.') - line1 + 1) % n
2375 return offset != 0 ? offset : n
2376 enddef
2377 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2378 enddef
2379
2380 new
2381 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2382 ReverseEveryNLines(3, 1, 9)
2383 END
2384 CheckScriptSuccess(lines)
2385 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2386 assert_equal(expected, getline(1, 9))
2387 bwipe!
2388enddef
2389
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002390def Test_global_closure_called_directly()
2391 var lines =<< trim END
2392 vim9script
2393 def Outer()
2394 var x = 1
2395 def g:Inner()
2396 var y = x
2397 x += 1
2398 assert_equal(1, y)
2399 enddef
2400 g:Inner()
2401 assert_equal(2, x)
2402 enddef
2403 Outer()
2404 END
2405 CheckScriptSuccess(lines)
2406 delfunc g:Inner
2407enddef
2408
Bram Moolenaar69c76172021-12-02 16:38:52 +00002409def Test_closure_called_from_legacy()
2410 var lines =<< trim END
2411 vim9script
2412 def Func()
2413 var outer = 'foo'
2414 var F = () => {
2415 outer = 'bar'
2416 }
2417 execute printf('call %s()', string(F))
2418 enddef
2419 Func()
2420 END
2421 CheckScriptFailure(lines, 'E1248')
2422enddef
2423
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002424def Test_failure_in_called_function()
2425 # this was using the frame index as the return value
2426 var lines =<< trim END
2427 vim9script
2428 au TerminalWinOpen * eval [][0]
2429 def PopupTerm(a: any)
2430 # make sure typvals on stack are string
2431 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2432 FireEvent()
2433 enddef
2434 def FireEvent()
2435 do TerminalWinOpen
2436 enddef
2437 # use try/catch to make eval fail
2438 try
2439 call PopupTerm(0)
2440 catch
2441 endtry
2442 au! TerminalWinOpen
2443 END
2444 CheckScriptSuccess(lines)
2445enddef
2446
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002447def Test_nested_lambda()
2448 var lines =<< trim END
2449 vim9script
2450 def Func()
2451 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002452 var Lambda1 = () => 7
2453 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002454 var res = Lambda2()
2455 assert_equal([7, 4], res)
2456 enddef
2457 Func()
2458 END
2459 CheckScriptSuccess(lines)
2460enddef
2461
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002462def Test_double_nested_lambda()
2463 var lines =<< trim END
2464 vim9script
2465 def F(head: string): func(string): func(string): string
2466 return (sep: string): func(string): string => ((tail: string): string => {
2467 return head .. sep .. tail
2468 })
2469 enddef
2470 assert_equal('hello-there', F('hello')('-')('there'))
2471 END
2472 CheckScriptSuccess(lines)
2473enddef
2474
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002475def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002476 var lines =<< trim END
2477 vim9script
2478 def F(text: string): func(string): func(string): string
2479 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002480 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002481 })
2482 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002483 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002484 END
2485 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002486
2487 lines =<< trim END
2488 vim9script
2489 echo range(4)->mapnew((_, v) => {
2490 return range(v) ->mapnew((_, s) => {
2491 return string(s)
2492 })
2493 })
2494 END
2495 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002496
2497 lines =<< trim END
2498 vim9script
2499
2500 def s:func()
2501 range(10)
2502 ->mapnew((_, _) => ({
2503 key: range(10)->mapnew((_, _) => {
2504 return ' '
2505 }),
2506 }))
2507 enddef
2508
2509 defcomp
2510 END
2511 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002512enddef
2513
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002514def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002515 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002516 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002517enddef
2518
2519def Test_lambda_arg_shadows_func()
2520 assert_equal([42], Shadowed())
2521enddef
2522
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002523def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002524 var path: string = empty(dir)
2525 \ ? 'empty'
2526 \ : 'full'
2527 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002528enddef
2529
2530def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002531 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002532enddef
2533
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002534def Test_script_var_in_lambda()
2535 var lines =<< trim END
2536 vim9script
2537 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002538 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002539 END
2540 CheckScriptSuccess(lines)
2541enddef
2542
Bram Moolenaar5e654232020-09-16 15:22:00 +02002543def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002544 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002545 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002546 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002547 ->reverse()
2548 return x
2549enddef
2550
2551def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002552 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002553
2554 var lines =<< trim END
2555 vim9script
2556 var res = [{n: 1, m: 2, s: 'xxx'}]
2557 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2558 v.n,
2559 v.m,
2560 substitute(v.s, '.*', 'yyy', '')
2561 ))
2562 assert_equal(['1:2:yyy'], res)
2563 END
2564 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002565enddef
2566
Bram Moolenaarb6571982021-01-08 22:24:19 +01002567def Test_list_lambda()
2568 timer_start(1000, (_) => 0)
2569 var body = execute(timer_info()[0].callback
2570 ->string()
2571 ->substitute("('", ' ', '')
2572 ->substitute("')", '', '')
2573 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002574 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002575enddef
2576
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002577def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002578 var lines =<< trim END
2579 vim9script
2580 var flist: list<func>
2581 for i in range(10)
2582 var inloop = i
2583 flist[i] = () => inloop
2584 endfor
2585 END
2586 CheckScriptSuccess(lines)
2587
2588 lines =<< trim END
2589 vim9script
2590 if true
2591 var outloop = 5
2592 var flist: list<func>
2593 for i in range(10)
2594 flist[i] = () => outloop
2595 endfor
2596 endif
2597 END
2598 CheckScriptSuccess(lines)
2599
2600 lines =<< trim END
2601 vim9script
2602 if true
2603 var outloop = 5
2604 endif
2605 var flist: list<func>
2606 for i in range(10)
2607 flist[i] = () => outloop
2608 endfor
2609 END
2610 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002611
2612 lines =<< trim END
2613 vim9script
2614 for i in range(10)
2615 var Ref = () => 0
2616 endfor
2617 assert_equal(0, ((i) => 0)(0))
2618 END
2619 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002620enddef
2621
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002622def Test_legacy_lambda()
2623 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002624
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002625 var lines =<< trim END
2626 echo {x -> 'hello ' .. x}('foo')
2627 END
2628 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002629
2630 lines =<< trim END
2631 vim9script
2632 def Func()
2633 echo (() => 'no error')()
2634 enddef
2635 legacy call s:Func()
2636 END
2637 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002638enddef
2639
Bram Moolenaarce024c32021-06-26 13:00:49 +02002640def Test_legacy()
2641 var lines =<< trim END
2642 vim9script
2643 func g:LegacyFunction()
2644 let g:legacyvar = 1
2645 endfunc
2646 def Testit()
2647 legacy call g:LegacyFunction()
2648 enddef
2649 Testit()
2650 assert_equal(1, g:legacyvar)
2651 unlet g:legacyvar
2652 delfunc g:LegacyFunction
2653 END
2654 CheckScriptSuccess(lines)
2655enddef
2656
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002657def Test_legacy_errors()
2658 for cmd in ['if', 'elseif', 'else', 'endif',
2659 'for', 'endfor', 'continue', 'break',
2660 'while', 'endwhile',
2661 'try', 'catch', 'finally', 'endtry']
2662 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2663 endfor
2664enddef
2665
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002666def Test_call_legacy_with_dict()
2667 var lines =<< trim END
2668 vim9script
2669 func Legacy() dict
2670 let g:result = self.value
2671 endfunc
2672 def TestDirect()
2673 var d = {value: 'yes', func: Legacy}
2674 d.func()
2675 enddef
2676 TestDirect()
2677 assert_equal('yes', g:result)
2678 unlet g:result
2679
2680 def TestIndirect()
2681 var d = {value: 'foo', func: Legacy}
2682 var Fi = d.func
2683 Fi()
2684 enddef
2685 TestIndirect()
2686 assert_equal('foo', g:result)
2687 unlet g:result
2688
2689 var d = {value: 'bar', func: Legacy}
2690 d.func()
2691 assert_equal('bar', g:result)
2692 unlet g:result
2693 END
2694 CheckScriptSuccess(lines)
2695enddef
2696
Bram Moolenaarab360522021-01-10 14:02:28 +01002697def DoFilterThis(a: string): list<string>
2698 # closure nested inside another closure using argument
2699 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2700 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2701enddef
2702
2703def Test_nested_closure_using_argument()
2704 assert_equal(['x', 'x2'], DoFilterThis('x'))
2705enddef
2706
Bram Moolenaar0186e582021-01-10 18:33:11 +01002707def Test_triple_nested_closure()
2708 var what = 'x'
2709 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2710 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2711 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2712enddef
2713
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002714func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002715 CheckScreendump
2716
2717 let lines =<< trim END
2718 vim9script
2719 def EchoNothing()
2720 silent echo ''
2721 enddef
2722 defcompile
2723 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002724 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002725
2726 " Check that the balloon shows up after a mouse move
2727 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002728 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002729 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2730
2731 " clean up
2732 call StopVimInTerminal(buf)
2733 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002734endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002735
Bram Moolenaar171fb922020-10-28 16:54:47 +01002736def SilentlyError()
2737 execute('silent! invalid')
2738 g:did_it = 'yes'
2739enddef
2740
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002741func UserError()
2742 silent! invalid
2743endfunc
2744
2745def SilentlyUserError()
2746 UserError()
2747 g:did_it = 'yes'
2748enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002749
2750" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002751func Test_ignore_silent_error()
2752 let g:did_it = 'no'
2753 call SilentlyError()
2754 call assert_equal('yes', g:did_it)
2755
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002756 let g:did_it = 'no'
2757 call SilentlyUserError()
2758 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002759
2760 unlet g:did_it
2761endfunc
2762
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002763def Test_ignore_silent_error_in_filter()
2764 var lines =<< trim END
2765 vim9script
2766 def Filter(winid: number, key: string): bool
2767 if key == 'o'
2768 silent! eval [][0]
2769 return true
2770 endif
2771 return popup_filter_menu(winid, key)
2772 enddef
2773
Bram Moolenaare0de1712020-12-02 17:36:54 +01002774 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002775 feedkeys("o\r", 'xnt')
2776 END
2777 CheckScriptSuccess(lines)
2778enddef
2779
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002780def Fibonacci(n: number): number
2781 if n < 2
2782 return n
2783 else
2784 return Fibonacci(n - 1) + Fibonacci(n - 2)
2785 endif
2786enddef
2787
Bram Moolenaar985116a2020-07-12 17:31:09 +02002788def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002789 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002790enddef
2791
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002792def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002793 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002794 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002795 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002796 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002797 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002798enddef
2799
2800def Test_closure_in_map()
2801 mkdir('XclosureDir/tdir', 'p')
2802 writefile(['111'], 'XclosureDir/file1')
2803 writefile(['222'], 'XclosureDir/file2')
2804 writefile(['333'], 'XclosureDir/tdir/file3')
2805
Bram Moolenaare0de1712020-12-02 17:36:54 +01002806 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002807
2808 delete('XclosureDir', 'rf')
2809enddef
2810
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002811def Test_invalid_function_name()
2812 var lines =<< trim END
2813 vim9script
2814 def s: list<string>
2815 END
2816 CheckScriptFailure(lines, 'E129:')
2817
2818 lines =<< trim END
2819 vim9script
2820 def g: list<string>
2821 END
2822 CheckScriptFailure(lines, 'E129:')
2823
2824 lines =<< trim END
2825 vim9script
2826 def <SID>: list<string>
2827 END
2828 CheckScriptFailure(lines, 'E884:')
2829
2830 lines =<< trim END
2831 vim9script
2832 def F list<string>
2833 END
2834 CheckScriptFailure(lines, 'E488:')
2835enddef
2836
Bram Moolenaara90afb92020-07-15 22:38:56 +02002837def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002838 var lines =<< trim END
2839 var Xsetlist: func
2840 Xsetlist = function('setloclist', [0])
2841 Xsetlist([], ' ', {title: 'test'})
2842 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002843
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002844 Xsetlist = function('setloclist', [0, [], ' '])
2845 Xsetlist({title: 'test'})
2846 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002847
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002848 Xsetlist = function('setqflist')
2849 Xsetlist([], ' ', {title: 'test'})
2850 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002851
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002852 Xsetlist = function('setqflist', [[], ' '])
2853 Xsetlist({title: 'test'})
2854 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002855
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002856 var Len: func: number = function('len', ['word'])
2857 assert_equal(4, Len())
2858
2859 var RepeatFunc = function('repeat', ['o'])
2860 assert_equal('ooooo', RepeatFunc(5))
2861 END
2862 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002863
2864 lines =<< trim END
2865 vim9script
2866 def Foo(Parser: any)
2867 enddef
2868 var Expr: func(dict<any>): dict<any>
2869 const Call = Foo(Expr)
2870 END
2871 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002872enddef
2873
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002874def Test_cmd_modifier()
2875 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002876 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002877enddef
2878
2879def Test_restore_modifiers()
2880 # check that when compiling a :def function command modifiers are not messed
2881 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002882 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002883 vim9script
2884 set eventignore=
2885 autocmd QuickFixCmdPost * copen
2886 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002887 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002888 enddef
2889 func Func()
2890 noautocmd call s:AutocmdsDisabled()
2891 let g:ei_after = &eventignore
2892 endfunc
2893 Func()
2894 END
2895 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002896 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002897enddef
2898
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002899def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002900 eval 1 + 2
2901 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002902 # call not on fourth line
2903 StackBot()
2904enddef
2905
2906def StackBot()
2907 # throw an error
2908 eval [][0]
2909enddef
2910
2911def Test_callstack_def()
2912 try
2913 StackTop()
2914 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002915 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002916 endtry
2917enddef
2918
Bram Moolenaare8211a32020-10-09 22:04:29 +02002919" Re-using spot for variable used in block
2920def Test_block_scoped_var()
2921 var lines =<< trim END
2922 vim9script
2923 def Func()
2924 var x = ['a', 'b', 'c']
2925 if 1
2926 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002927 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002928 endif
2929 var z = x
2930 assert_equal(['x', 'x', 'x'], z)
2931 enddef
2932 Func()
2933 END
2934 CheckScriptSuccess(lines)
2935enddef
2936
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002937def Test_reset_did_emsg()
2938 var lines =<< trim END
2939 @s = 'blah'
2940 au BufWinLeave * #
2941 def Func()
2942 var winid = popup_create('popup', {})
2943 exe '*s'
2944 popup_close(winid)
2945 enddef
2946 Func()
2947 END
2948 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002949 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002950enddef
2951
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002952def Test_did_emsg_reset()
2953 # executing an autocommand resets did_emsg, this should not result in a
2954 # builtin function considered failing
2955 var lines =<< trim END
2956 vim9script
2957 au BufWinLeave * #
2958 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02002959 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002960 eval [][0]
2961 enddef
2962 nno <F3> <cmd>call <sid>Func()<cr>
2963 feedkeys("\<F3>\e", 'xt')
2964 END
2965 writefile(lines, 'XemsgReset')
2966 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
2967 delete('XemsgReset')
2968 nunmap <F3>
2969 au! BufWinLeave
2970enddef
2971
Bram Moolenaar56602ba2020-12-05 21:22:08 +01002972def Test_abort_with_silent_call()
2973 var lines =<< trim END
2974 vim9script
2975 g:result = 'none'
2976 def Func()
2977 g:result += 3
2978 g:result = 'yes'
2979 enddef
2980 # error is silenced, but function aborts on error
2981 silent! Func()
2982 assert_equal('none', g:result)
2983 unlet g:result
2984 END
2985 CheckScriptSuccess(lines)
2986enddef
2987
Bram Moolenaarf665e972020-12-05 19:17:16 +01002988def Test_continues_with_silent_error()
2989 var lines =<< trim END
2990 vim9script
2991 g:result = 'none'
2992 def Func()
2993 silent! g:result += 3
2994 g:result = 'yes'
2995 enddef
2996 # error is silenced, function does not abort
2997 Func()
2998 assert_equal('yes', g:result)
2999 unlet g:result
3000 END
3001 CheckScriptSuccess(lines)
3002enddef
3003
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003004def Test_abort_even_with_silent()
3005 var lines =<< trim END
3006 vim9script
3007 g:result = 'none'
3008 def Func()
3009 eval {-> ''}() .. '' .. {}['X']
3010 g:result = 'yes'
3011 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003012 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003013 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003014 unlet g:result
3015 END
3016 CheckScriptSuccess(lines)
3017enddef
3018
Bram Moolenaarf665e972020-12-05 19:17:16 +01003019def Test_cmdmod_silent_restored()
3020 var lines =<< trim END
3021 vim9script
3022 def Func()
3023 g:result = 'none'
3024 silent! g:result += 3
3025 g:result = 'none'
3026 g:result += 3
3027 enddef
3028 Func()
3029 END
3030 # can't use CheckScriptFailure, it ignores the :silent!
3031 var fname = 'Xdefsilent'
3032 writefile(lines, fname)
3033 var caught = 'no'
3034 try
3035 exe 'source ' .. fname
3036 catch /E1030:/
3037 caught = 'yes'
3038 assert_match('Func, line 4', v:throwpoint)
3039 endtry
3040 assert_equal('yes', caught)
3041 delete(fname)
3042enddef
3043
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003044def Test_cmdmod_silent_nested()
3045 var lines =<< trim END
3046 vim9script
3047 var result = ''
3048
3049 def Error()
3050 result ..= 'Eb'
3051 eval [][0]
3052 result ..= 'Ea'
3053 enddef
3054
3055 def Crash()
3056 result ..= 'Cb'
3057 sil! Error()
3058 result ..= 'Ca'
3059 enddef
3060
3061 Crash()
3062 assert_equal('CbEbEaCa', result)
3063 END
3064 CheckScriptSuccess(lines)
3065enddef
3066
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003067def Test_dict_member_with_silent()
3068 var lines =<< trim END
3069 vim9script
3070 g:result = 'none'
3071 var d: dict<any>
3072 def Func()
3073 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003074 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003075 catch
3076 endtry
3077 enddef
3078 silent! Func()
3079 assert_equal('0', g:result)
3080 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003081 END
3082 CheckScriptSuccess(lines)
3083enddef
3084
Bram Moolenaarf9041332021-01-21 19:41:16 +01003085def Test_skip_cmds_with_silent()
3086 var lines =<< trim END
3087 vim9script
3088
3089 def Func(b: bool)
3090 Crash()
3091 enddef
3092
3093 def Crash()
3094 sil! :/not found/d _
3095 sil! :/not found/put _
3096 enddef
3097
3098 Func(true)
3099 END
3100 CheckScriptSuccess(lines)
3101enddef
3102
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003103def Test_opfunc()
3104 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3105 def g:Opfunc(_: any): string
3106 setline(1, 'ASDF')
3107 return ''
3108 enddef
3109 new
3110 setline(1, 'asdf')
3111 feedkeys("\<F3>$", 'x')
3112 assert_equal('ASDF', getline(1))
3113
3114 bwipe!
3115 nunmap <F3>
3116enddef
3117
Bram Moolenaar077a4232020-12-22 18:33:27 +01003118" this was crashing on exit
3119def Test_nested_lambda_in_closure()
3120 var lines =<< trim END
3121 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003122 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003123 def Outer()
3124 def g:Inner()
3125 echo map([1, 2, 3], {_, v -> v + 1})
3126 enddef
3127 g:Inner()
3128 enddef
3129 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003130 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003131 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003132 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003133 return
3134 endif
3135 assert_equal(['Done'], readfile('XnestedDone'))
3136 delete('XnestedDone')
3137enddef
3138
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003139def Test_check_func_arg_types()
3140 var lines =<< trim END
3141 vim9script
3142 def F1(x: string): string
3143 return x
3144 enddef
3145
3146 def F2(x: number): number
3147 return x + 1
3148 enddef
3149
3150 def G(g: func): dict<func>
3151 return {f: g}
3152 enddef
3153
3154 def H(d: dict<func>): string
3155 return d.f('a')
3156 enddef
3157 END
3158
3159 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3160 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3161enddef
3162
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003163def Test_list_any_type_checked()
3164 var lines =<< trim END
3165 vim9script
3166 def Foo()
3167 --decl--
3168 Bar(l)
3169 enddef
3170 def Bar(ll: list<dict<any>>)
3171 enddef
3172 Foo()
3173 END
3174 lines[2] = 'var l: list<any>'
3175 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3176
3177 lines[2] = 'var l: list<any> = []'
3178 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3179
3180 lines[2] = 'var l: list<any> = [11]'
3181 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3182enddef
3183
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003184def Test_compile_error()
3185 var lines =<< trim END
3186 def g:Broken()
3187 echo 'a' + {}
3188 enddef
3189 call g:Broken()
3190 END
3191 # First call: compilation error
3192 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3193
3194 # Second call won't try compiling again
3195 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003196 delfunc g:Broken
3197
3198 # No error when compiling with :silent!
3199 lines =<< trim END
3200 def g:Broken()
3201 echo 'a' + []
3202 enddef
3203 silent! defcompile
3204 END
3205 CheckScriptSuccess(lines)
3206
3207 # Calling the function won't try compiling again
3208 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3209 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003210enddef
3211
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003212def Test_ignored_argument()
3213 var lines =<< trim END
3214 vim9script
3215 def Ignore(_, _): string
3216 return 'yes'
3217 enddef
3218 assert_equal('yes', Ignore(1, 2))
3219
3220 func Ok(_)
3221 return a:_
3222 endfunc
3223 assert_equal('ok', Ok('ok'))
3224
3225 func Oktoo()
3226 let _ = 'too'
3227 return _
3228 endfunc
3229 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003230
3231 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003232 END
3233 CheckScriptSuccess(lines)
3234
3235 lines =<< trim END
3236 def Ignore(_: string): string
3237 return _
3238 enddef
3239 defcompile
3240 END
3241 CheckScriptFailure(lines, 'E1181:', 1)
3242
3243 lines =<< trim END
3244 var _ = 1
3245 END
3246 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003247
3248 lines =<< trim END
3249 var x = _
3250 END
3251 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003252enddef
3253
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003254def Test_too_many_arguments()
3255 var lines =<< trim END
3256 echo [0, 1, 2]->map(() => 123)
3257 END
3258 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3259
3260 lines =<< trim END
3261 echo [0, 1, 2]->map((_) => 123)
3262 END
3263 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3264enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003265
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003266def Test_closing_brace_at_start_of_line()
3267 var lines =<< trim END
3268 def Func()
3269 enddef
3270 Func(
3271 )
3272 END
3273 call CheckDefAndScriptSuccess(lines)
3274enddef
3275
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003276func CreateMydict()
3277 let g:mydict = {}
3278 func g:mydict.afunc()
3279 let g:result = self.key
3280 endfunc
3281endfunc
3282
3283def Test_numbered_function_reference()
3284 CreateMydict()
3285 var output = execute('legacy func g:mydict.afunc')
3286 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3287 execute 'function(' .. funcName .. ', [], {key: 42})()'
3288 # check that the function still exists
3289 assert_equal(output, execute('legacy func g:mydict.afunc'))
3290 unlet g:mydict
3291enddef
3292
Bram Moolenaar20677332021-06-06 17:02:53 +02003293if has('python3')
3294 def Test_python3_heredoc()
3295 py3 << trim EOF
3296 import vim
3297 vim.vars['didit'] = 'yes'
3298 EOF
3299 assert_equal('yes', g:didit)
3300
3301 python3 << trim EOF
3302 import vim
3303 vim.vars['didit'] = 'again'
3304 EOF
3305 assert_equal('again', g:didit)
3306 enddef
3307endif
3308
3309" This messes up syntax highlight, keep near the end.
3310if has('lua')
3311 def Test_lua_heredoc()
3312 g:d = {}
3313 lua << trim EOF
3314 x = vim.eval('g:d')
3315 x['key'] = 'val'
3316 EOF
3317 assert_equal('val', g:d.key)
3318 enddef
3319endif
3320
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003321
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003322" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker