blob: 88c0af8e0da5a8dfd74bfb2c2138b86e67bb7dbf [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000494 CheckDefAndScriptFailure(["call Test ('text')"], ['E476:', 'E1068:'])
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000550
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000551 # using script variable requires matching type or type cast when executed
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000552 lines =<< trim END
553 vim9script
554 var a: any
555 def Func(arg: string = a)
556 echo arg
557 enddef
558 defcompile
559 END
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000560 CheckScriptSuccess(lines + ['a = "text"', 'Func()'])
561 CheckScriptFailure(lines + ['a = 123', 'Func()'], 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000562
563 # using global variable does not require type cast
564 lines =<< trim END
565 vim9script
566 def Func(arg: string = g:str)
567 echo arg
568 enddef
569 g:str = 'works'
570 Func()
571 END
572 CheckScriptSuccess(lines)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200573enddef
574
Bram Moolenaarcef12702021-01-04 14:09:43 +0100575def FuncWithComment( # comment
576 a: number, #comment
577 b: bool, # comment
578 c: string) #comment
579 assert_equal(4, a)
580 assert_equal(true, b)
581 assert_equal('yes', c)
582enddef
583
584def Test_func_with_comments()
585 FuncWithComment(4, true, 'yes')
586
587 var lines =<< trim END
588 def Func(# comment
589 arg: string)
590 enddef
591 END
592 CheckScriptFailure(lines, 'E125:', 1)
593
594 lines =<< trim END
595 def Func(
596 arg: string# comment
597 )
598 enddef
599 END
600 CheckScriptFailure(lines, 'E475:', 2)
601
602 lines =<< trim END
603 def Func(
604 arg: string
605 )# comment
606 enddef
607 END
608 CheckScriptFailure(lines, 'E488:', 3)
609enddef
610
Bram Moolenaar04b12692020-05-04 23:24:44 +0200611def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000612 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200613 return 'nested ' .. arg
614 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000615 NestedDef(':def')->assert_equal('nested :def')
616
617 func NestedFunc(arg)
618 return 'nested ' .. a:arg
619 endfunc
620 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200621
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200622 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
623 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
624
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200625 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
626 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200627
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 var lines =<< trim END
629 def Outer()
630 def Inner()
631 # comment
632 enddef
633 def Inner()
634 enddef
635 enddef
636 END
637 CheckDefFailure(lines, 'E1073:')
638
639 lines =<< trim END
640 def Outer()
641 def Inner()
642 # comment
643 enddef
644 def! Inner()
645 enddef
646 enddef
647 END
648 CheckDefFailure(lines, 'E1117:')
649
Bram Moolenaardb8e5c22021-12-25 19:58:22 +0000650 lines =<< trim END
651 vim9script
652 def Outer()
653 def Inner()
654 g:result = 'ok'
655 enddef
656 Inner()
657 enddef
658 Outer()
659 Inner()
660 END
661 CheckScriptFailure(lines, 'E117: Unknown function: Inner')
662 assert_equal('ok', g:result)
663 unlet g:result
664
Bram Moolenaar54021752020-12-06 18:50:36 +0100665 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100666 lines =<< trim END
667 vim9script
668 var thecount = 0
669 if true
670 def Test(): number
671 def TheFunc(): number
672 thecount += 1
673 return thecount
674 enddef
675 return TheFunc()
676 enddef
677 endif
678 defcompile
679 assert_equal(1, Test())
680 assert_equal(2, Test())
681 END
682 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100683
684 # also works when "thecount" is inside the "if" block
685 lines =<< trim END
686 vim9script
687 if true
688 var thecount = 0
689 def Test(): number
690 def TheFunc(): number
691 thecount += 1
692 return thecount
693 enddef
694 return TheFunc()
695 enddef
696 endif
697 defcompile
698 assert_equal(1, Test())
699 assert_equal(2, Test())
700 END
701 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200702
703 lines =<< trim END
704 vim9script
705 def Outer()
706 def Inner()
707 echo 'hello'
708 enddef burp
709 enddef
710 defcompile
711 END
712 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200713enddef
714
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100715def Test_not_nested_function()
716 echo printf('%d',
717 function('len')('xxx'))
718enddef
719
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200720func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200721 call MyDefaultArgs()->assert_equal('string')
722 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200723 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200724endfunc
725
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200726def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200727 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200728 vim9script
729 def Outer()
730 def g:Inner(): string
731 return 'inner'
732 enddef
733 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200734 defcompile
735 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200736 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200737 delfunc g:Inner
738 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200739 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200740 delfunc g:Inner
741 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200742 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200743 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200744 END
745 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200746
747 lines =<< trim END
748 vim9script
749 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000750 func g:Inner()
751 return 'inner'
752 endfunc
753 enddef
754 defcompile
755 Outer()
756 g:Inner()->assert_equal('inner')
757 delfunc g:Inner
758 Outer()
759 g:Inner()->assert_equal('inner')
760 delfunc g:Inner
761 Outer()
762 g:Inner()->assert_equal('inner')
763 delfunc g:Inner
764 END
765 CheckScriptSuccess(lines)
766
767 lines =<< trim END
768 vim9script
769 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200770 def g:Inner(): string
771 return 'inner'
772 enddef
773 enddef
774 defcompile
775 Outer()
776 Outer()
777 END
778 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100779 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200780
781 lines =<< trim END
782 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100783 def Outer()
784 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100785 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100786 enddef
787 g:Inner()
788 enddef
789 Outer()
790 END
791 CheckScriptSuccess(lines)
792 delfunc g:Inner
793
794 lines =<< trim END
795 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200796 def Func()
797 echo 'script'
798 enddef
799 def Outer()
800 def Func()
801 echo 'inner'
802 enddef
803 enddef
804 defcompile
805 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000806 CheckScriptFailure(lines, "E1073:", 1)
807
808 lines =<< trim END
809 vim9script
810 def Func()
811 echo 'script'
812 enddef
813 def Func()
814 echo 'script'
815 enddef
816 END
817 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200818enddef
819
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100820def DefListAll()
821 def
822enddef
823
824def DefListOne()
825 def DefListOne
826enddef
827
828def DefListMatches()
829 def /DefList
830enddef
831
832def Test_nested_def_list()
833 var funcs = split(execute('call DefListAll()'), "\n")
834 assert_true(len(funcs) > 10)
835 assert_true(funcs->index('def DefListAll()') >= 0)
836
837 funcs = split(execute('call DefListOne()'), "\n")
838 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
839
840 funcs = split(execute('call DefListMatches()'), "\n")
841 assert_true(len(funcs) >= 3)
842 assert_true(funcs->index('def DefListAll()') >= 0)
843 assert_true(funcs->index('def DefListOne()') >= 0)
844 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100845
846 var lines =<< trim END
847 vim9script
848 def Func()
849 def +Func+
850 enddef
851 defcompile
852 END
853 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100854enddef
855
Bram Moolenaar333894b2020-08-01 18:53:07 +0200856def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200857 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200858 vim9script
859 def g:Func(): string
860 return 'global'
861 enddef
862 def Func(): string
863 return 'local'
864 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200865 g:Func()->assert_equal('global')
866 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100867 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200868 END
869 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200870
871 lines =<< trim END
872 vim9script
873 def g:Funcy()
874 echo 'funcy'
875 enddef
876 s:Funcy()
877 END
878 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200879enddef
880
Bram Moolenaar0f769812020-09-12 18:32:34 +0200881def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200882 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200883 vim9script
884 def g:Gfunc(): string
885 return 'global'
886 enddef
887 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200888 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200889 return Gfunc('testing')
890 enddef
891 g:Gfunc()->assert_equal('global')
892 AnotherFunc()->assert_equal(7)
893 delfunc g:Gfunc
894 END
895 CheckScriptSuccess(lines)
896
897 lines =<< trim END
898 vim9script
899 def g:Func(): string
900 return 'global'
901 enddef
902 def AnotherFunc()
903 g:Func = function('len')
904 enddef
905 AnotherFunc()
906 END
907 CheckScriptFailure(lines, 'E705:')
908 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200909
910 # global function is found without g: prefix
911 lines =<< trim END
912 vim9script
913 def g:Func(): string
914 return 'global'
915 enddef
916 def AnotherFunc(): string
917 return Func()
918 enddef
919 assert_equal('global', AnotherFunc())
920 delfunc g:Func
921 END
922 CheckScriptSuccess(lines)
923
924 lines =<< trim END
925 vim9script
926 def g:Func(): string
927 return 'global'
928 enddef
929 assert_equal('global', Func())
930 delfunc g:Func
931 END
932 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200933enddef
934
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200935func TakesOneArg(arg)
936 echo a:arg
937endfunc
938
939def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200940 CheckDefFailure(['TakesOneArg()'], 'E119:')
941 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
942 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
943 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200944
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200945 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200946 vim9script
947 def Func(s: string)
948 echo s
949 enddef
950 Func([])
951 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200952 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200953
954 lines =<< trim END
955 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100956 var name = 'piet'
957 def FuncOne(name: string)
958 echo nr
959 enddef
960 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100961 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100962
963 lines =<< trim END
964 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200965 def FuncOne(nr: number)
966 echo nr
967 enddef
968 def FuncTwo()
969 FuncOne()
970 enddef
971 defcompile
972 END
973 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200974 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200975 try
976 source Xscript
977 catch
978 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
979 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
980 didCatch = true
981 endtry
982 assert_true(didCatch)
983
984 lines =<< trim END
985 vim9script
986 def FuncOne(nr: number)
987 echo nr
988 enddef
989 def FuncTwo()
990 FuncOne(1, 2)
991 enddef
992 defcompile
993 END
994 writefile(lines, 'Xscript')
995 didCatch = false
996 try
997 source Xscript
998 catch
999 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
1000 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
1001 didCatch = true
1002 endtry
1003 assert_true(didCatch)
1004
1005 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001006enddef
1007
Bram Moolenaar50824712020-12-20 21:10:17 +01001008def Test_call_funcref_wrong_args()
1009 var head =<< trim END
1010 vim9script
1011 def Func3(a1: string, a2: number, a3: list<number>)
1012 echo a1 .. a2 .. a3[0]
1013 enddef
1014 def Testme()
1015 var funcMap: dict<func> = {func: Func3}
1016 END
1017 var tail =<< trim END
1018 enddef
1019 Testme()
1020 END
1021 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
1022
1023 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
1024 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001025
1026 var lines =<< trim END
1027 vim9script
1028 var Ref: func(number): any
1029 Ref = (j) => !j
1030 echo Ref(false)
1031 END
1032 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
1033
1034 lines =<< trim END
1035 vim9script
1036 var Ref: func(number): any
1037 Ref = (j) => !j
1038 call Ref(false)
1039 END
1040 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001041enddef
1042
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001043def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001044 var lines =<< trim END
1045 var Callback = (..._) => 'anything'
1046 assert_equal('anything', Callback())
1047 assert_equal('anything', Callback(1))
1048 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001049
1050 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001051 END
1052 CheckDefAndScriptSuccess(lines)
1053
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001054 CheckDefFailure(['echo ((i) => 0)()'],
1055 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001056
Bram Moolenaar2a389082021-04-09 20:24:31 +02001057 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001058 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001059 echo Ref(1, 'x')
1060 END
1061 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001062
1063 lines =<< trim END
1064 var Ref: func(job, string, number)
1065 Ref = (x, y) => 0
1066 END
1067 CheckDefAndScriptFailure(lines, 'E1012:')
1068
1069 lines =<< trim END
1070 var Ref: func(job, string)
1071 Ref = (x, y, z) => 0
1072 END
1073 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001074
1075 lines =<< trim END
1076 var one = 1
1077 var l = [1, 2, 3]
1078 echo map(l, (one) => one)
1079 END
1080 CheckDefFailure(lines, 'E1167:')
1081 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1082
1083 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001084 var Ref: func(any, ?any): bool
1085 Ref = (_, y = 1) => false
1086 END
1087 CheckDefAndScriptFailure(lines, 'E1172:')
1088
1089 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001090 var a = 0
1091 var b = (a == 0 ? 1 : 2)
1092 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001093 var txt = 'a'
1094 b = (txt =~ 'x' ? 1 : 2)
1095 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001096 END
1097 CheckDefAndScriptSuccess(lines)
1098
1099 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001100 def ShadowLocal()
1101 var one = 1
1102 var l = [1, 2, 3]
1103 echo map(l, (one) => one)
1104 enddef
1105 END
1106 CheckDefFailure(lines, 'E1167:')
1107
1108 lines =<< trim END
1109 def Shadowarg(one: number)
1110 var l = [1, 2, 3]
1111 echo map(l, (one) => one)
1112 enddef
1113 END
1114 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001115
1116 lines =<< trim END
1117 echo ((a) => a)('aa', 'bb')
1118 END
1119 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001120
1121 lines =<< trim END
1122 echo 'aa'->((a) => a)('bb')
1123 END
1124 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1125 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001126enddef
1127
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001128def Test_lambda_line_nr()
1129 var lines =<< trim END
1130 vim9script
1131 # comment
1132 # comment
1133 var id = timer_start(1'000, (_) => 0)
1134 var out = execute('verbose ' .. timer_info(id)[0].callback
1135 ->string()
1136 ->substitute("('\\|')", ' ', 'g'))
1137 assert_match('Last set from .* line 4', out)
1138 END
1139 CheckScriptSuccess(lines)
1140enddef
1141
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001142def FilterWithCond(x: string, Cond: func(string): bool): bool
1143 return Cond(x)
1144enddef
1145
Bram Moolenaar0346b792021-01-31 22:18:29 +01001146def Test_lambda_return_type()
1147 var lines =<< trim END
1148 var Ref = (): => 123
1149 END
1150 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001151
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001152 # no space before the return type
1153 lines =<< trim END
1154 var Ref = (x):number => x + 1
1155 END
1156 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1157
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001158 # this works
1159 for x in ['foo', 'boo']
1160 echo FilterWithCond(x, (v) => v =~ '^b')
1161 endfor
1162
1163 # this fails
1164 lines =<< trim END
1165 echo FilterWithCond('foo', (v) => v .. '^b')
1166 END
1167 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001168
1169 lines =<< trim END
1170 var Lambda1 = (x) => {
1171 return x
1172 }
1173 assert_equal('asdf', Lambda1('asdf'))
1174 var Lambda2 = (x): string => {
1175 return x
1176 }
1177 assert_equal('foo', Lambda2('foo'))
1178 END
1179 CheckDefAndScriptSuccess(lines)
1180
1181 lines =<< trim END
1182 var Lambda = (x): string => {
1183 return x
1184 }
1185 echo Lambda(['foo'])
1186 END
1187 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001188enddef
1189
Bram Moolenaar709664c2020-12-12 14:33:41 +01001190def Test_lambda_uses_assigned_var()
1191 CheckDefSuccess([
1192 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001193 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001194enddef
1195
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001196def Test_pass_legacy_lambda_to_def_func()
1197 var lines =<< trim END
1198 vim9script
1199 func Foo()
1200 eval s:Bar({x -> 0})
1201 endfunc
1202 def Bar(y: any)
1203 enddef
1204 Foo()
1205 END
1206 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001207
1208 lines =<< trim END
1209 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001210 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001211 enddef
1212 legacy call g:TestFunc({-> 0})
1213 delfunc g:TestFunc
1214
1215 def g:TestFunc(f: func(number))
1216 enddef
1217 legacy call g:TestFunc({nr -> 0})
1218 delfunc g:TestFunc
1219 END
1220 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001221enddef
1222
Bram Moolenaar844fb642021-10-23 13:32:30 +01001223def Test_lambda_in_reduce_line_break()
1224 # this was using freed memory
1225 var lines =<< trim END
1226 vim9script
1227 const result: dict<number> =
1228 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1229 ->reduce((acc, val) => {
1230 if has_key(acc, val)
1231 acc[val] += 1
1232 return acc
1233 else
1234 acc[val] = 1
1235 return acc
1236 endif
1237 }, {})
1238 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1239 END
1240 CheckScriptSuccess(lines)
1241enddef
1242
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001243def Test_set_opfunc_to_lambda()
1244 var lines =<< trim END
1245 vim9script
1246 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1247 def CountSpaces(type = ''): string
1248 if type == ''
1249 &operatorfunc = (t) => CountSpaces(t)
1250 return 'g@'
1251 endif
1252 normal! '[V']y
1253 g:result = getreg('"')->count(' ')
1254 return ''
1255 enddef
1256 new
1257 'a b c d e'->setline(1)
1258 feedkeys("\<F4>", 'x')
1259 assert_equal(4, g:result)
1260 bwipe!
1261 END
1262 CheckScriptSuccess(lines)
1263enddef
1264
Bram Moolenaaref082e12021-12-12 21:02:03 +00001265def Test_set_opfunc_to_global_function()
1266 var lines =<< trim END
1267 vim9script
1268 def g:CountSpaces(type = ''): string
1269 normal! '[V']y
1270 g:result = getreg('"')->count(' ')
1271 return ''
1272 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001273 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001274 &operatorfunc = g:CountSpaces
1275 new
1276 'a b c d e'->setline(1)
1277 feedkeys("g@_", 'x')
1278 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001279
1280 &operatorfunc = ''
1281 g:result = 0
1282 # global function works in :def function
1283 def Func()
1284 &operatorfunc = g:CountSpaces
1285 enddef
1286 Func()
1287 feedkeys("g@_", 'x')
1288 assert_equal(4, g:result)
1289
Bram Moolenaaref082e12021-12-12 21:02:03 +00001290 bwipe!
1291 END
1292 CheckScriptSuccess(lines)
1293 &operatorfunc = ''
1294enddef
1295
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001296def Test_use_script_func_name_with_prefix()
1297 var lines =<< trim END
1298 vim9script
1299 func s:Getit()
1300 return 'it'
1301 endfunc
1302 var Fn = s:Getit
1303 assert_equal('it', Fn())
1304 END
1305 CheckScriptSuccess(lines)
1306enddef
1307
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001308def Test_lambda_type_allocated()
1309 # Check that unreferencing a partial using a lambda can use the variable type
1310 # after the lambda has been freed and does not leak memory.
1311 var lines =<< trim END
1312 vim9script
1313
1314 func MyomniFunc1(val, findstart, base)
1315 return a:findstart ? 0 : []
1316 endfunc
1317
1318 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1319 &omnifunc = Lambda
1320 Lambda = (a, b) => MyomniFunc1(20, a, b)
1321 &omnifunc = string(Lambda)
1322 Lambda = (a, b) => strlen(a)
1323 END
1324 CheckScriptSuccess(lines)
1325enddef
1326
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001327" Default arg and varargs
1328def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001329 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001330 for s in rest
1331 res ..= ',' .. s
1332 endfor
1333 return res
1334enddef
1335
1336def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001337 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001338 MyDefVarargs('one')->assert_equal('one,foo')
1339 MyDefVarargs('one', 'two')->assert_equal('one,two')
1340 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001341 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001342 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001343 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001344 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001345
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001346 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001347 vim9script
1348 def Func(...l: list<string>)
1349 echo l
1350 enddef
1351 Func('a', 'b', 'c')
1352 END
1353 CheckScriptSuccess(lines)
1354
1355 lines =<< trim END
1356 vim9script
1357 def Func(...l: list<string>)
1358 echo l
1359 enddef
1360 Func()
1361 END
1362 CheckScriptSuccess(lines)
1363
1364 lines =<< trim END
1365 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001366 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001367 echo l
1368 enddef
1369 Func(0)
1370 END
1371 CheckScriptSuccess(lines)
1372
1373 lines =<< trim END
1374 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001375 def Func(...l: any)
1376 echo l
1377 enddef
1378 Func(0)
1379 END
1380 CheckScriptFailure(lines, 'E1180:', 2)
1381
1382 lines =<< trim END
1383 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001384 def Func(..._l: list<string>)
1385 echo _l
1386 enddef
1387 Func('a', 'b', 'c')
1388 END
1389 CheckScriptSuccess(lines)
1390
1391 lines =<< trim END
1392 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001393 def Func(...l: list<string>)
1394 echo l
1395 enddef
1396 Func(1, 2, 3)
1397 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001398 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001399
1400 lines =<< trim END
1401 vim9script
1402 def Func(...l: list<string>)
1403 echo l
1404 enddef
1405 Func('a', 9)
1406 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001407 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001408
1409 lines =<< trim END
1410 vim9script
1411 def Func(...l: list<string>)
1412 echo l
1413 enddef
1414 Func(1, 'a')
1415 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001416 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001417
1418 lines =<< trim END
1419 vim9script
1420 def Func( # some comment
1421 ...l = []
1422 )
1423 echo l
1424 enddef
1425 END
1426 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001427
1428 lines =<< trim END
1429 vim9script
1430 def DoIt()
1431 g:Later('')
1432 enddef
1433 defcompile
1434 def g:Later(...l: list<number>)
1435 enddef
1436 DoIt()
1437 END
1438 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001439enddef
1440
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001441let s:value = ''
1442
1443def FuncOneDefArg(opt = 'text')
1444 s:value = opt
1445enddef
1446
1447def FuncTwoDefArg(nr = 123, opt = 'text'): string
1448 return nr .. opt
1449enddef
1450
1451def FuncVarargs(...arg: list<string>): string
1452 return join(arg, ',')
1453enddef
1454
1455def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001456 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001457 RefDefArg = FuncOneDefArg
1458 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001459 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001460 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001461 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001462
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001463 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001464 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001465 RefDef2Arg()->assert_equal('123text')
1466 RefDef2Arg(99)->assert_equal('99text')
1467 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001468
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001469 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1470 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001471
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001472 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001473 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001474 RefVarargs()->assert_equal('')
1475 RefVarargs('one')->assert_equal('one')
1476 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001477
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001478 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1479 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001480enddef
1481
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001482" Only varargs
1483def MyVarargsOnly(...args: list<string>): string
1484 return join(args, ',')
1485enddef
1486
1487def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001488 MyVarargsOnly()->assert_equal('')
1489 MyVarargsOnly('one')->assert_equal('one')
1490 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001491 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1492 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001493enddef
1494
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001495def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001496 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001497 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001498 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001499enddef
1500
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001501def DictArg(arg: dict<string>)
1502 arg['key'] = 'value'
1503enddef
1504
1505def ListArg(arg: list<string>)
1506 arg[0] = 'value'
1507enddef
1508
1509def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001510 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001511 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001512 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001513 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001514 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001515 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001516 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001517
Bram Moolenaard2c61702020-09-06 15:58:36 +02001518 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001519 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001520enddef
1521
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001522" These argument names are reserved in legacy functions.
1523def WithReservedNames(firstline: string, lastline: string): string
1524 return firstline .. lastline
1525enddef
1526
1527def Test_argument_names()
1528 assert_equal('OK', WithReservedNames('O', 'K'))
1529enddef
1530
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001531def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001532 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001533 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001534enddef
1535
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001536func DefinedLater(arg)
1537 return a:arg
1538endfunc
1539
1540def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001541 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001542 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001543 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001544
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001545 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001546 vim9script
1547 def RetNumber(): number
1548 return 123
1549 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001550 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001551 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001552 END
1553 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001554
1555 lines =<< trim END
1556 vim9script
1557 def RetNumber(): number
1558 return 123
1559 enddef
1560 def Bar(F: func: number): number
1561 return F()
1562 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001563 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001564 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001565 END
1566 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001567
1568 lines =<< trim END
1569 vim9script
1570 def UseNumber(nr: number)
1571 echo nr
1572 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001573 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001574 Funcref(123)
1575 END
1576 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001577
1578 lines =<< trim END
1579 vim9script
1580 def UseNumber(nr: number)
1581 echo nr
1582 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001583 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001584 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001585 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001586
1587 lines =<< trim END
1588 vim9script
1589 def EchoNr(nr = 34)
1590 g:echo = nr
1591 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001592 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001593 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001594 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001595 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001596 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001597 END
1598 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001599
1600 lines =<< trim END
1601 vim9script
1602 def EchoList(...l: list<number>)
1603 g:echo = l
1604 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001605 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001606 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001607 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001608 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001609 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001610 END
1611 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001612
1613 lines =<< trim END
1614 vim9script
1615 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1616 g:optarg = opt
1617 g:listarg = l
1618 return nr
1619 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001620 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001621 Funcref(10)->assert_equal(10)
1622 g:optarg->assert_equal(12)
1623 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001624
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001625 Funcref(11, 22)->assert_equal(11)
1626 g:optarg->assert_equal(22)
1627 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001628
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001629 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1630 g:optarg->assert_equal(18)
1631 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001632 END
1633 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001634enddef
1635
1636let SomeFunc = function('len')
1637let NotAFunc = 'text'
1638
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001639def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001640 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001641 var Ref1: func(bool): string
1642 var Ref2: func(bool): number
1643 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001644 Ref3 = g:cond ? Ref1 : Ref2
1645
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001646 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001647 var Refa1: func(bool): number
1648 var Refa2: func(bool, number): number
1649 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001650 Refa3 = g:cond ? Refa1 : Refa2
1651
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001652 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001653 var Refb1: func(bool, string): number
1654 var Refb2: func(string, number): number
1655 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001656 Refb3 = g:cond ? Refb1 : Refb2
1657enddef
1658
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001659def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001660 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001661enddef
1662
1663def DefinedEvenLater(arg: string): string
1664 return arg
1665enddef
1666
1667def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001668 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001669 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001670enddef
1671
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001672def Test_nested_function_with_nextcmd()
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001673 var lines =<< trim END
1674 vim9script
1675 # Define an outer function
1676 def FirstFunction()
1677 # Define an inner function
1678 def SecondFunction()
1679 # the function has a body, a double free is detected.
1680 AAAAA
1681
1682 # enddef followed by | or } followed by # one or more characters
1683 enddef|BBBB
1684 enddef
1685
1686 # Compile all functions
1687 defcompile
1688 END
1689 CheckScriptFailure(lines, 'E476: Invalid command: AAAAA')
1690enddef
1691
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001692def Test_nested_function_with_args_split()
1693 var lines =<< trim END
1694 vim9script
1695 def FirstFunction()
1696 def SecondFunction(
1697 )
1698 # had a double free if the right parenthesis of the nested function is
1699 # on the next line
1700
1701 enddef|BBBB
1702 enddef
1703 # Compile all functions
1704 defcompile
1705 END
1706 # FIXME: this should fail on the BBBB
1707 CheckScriptSuccess(lines)
1708enddef
1709
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001710def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001711 CheckScriptFailure([
1712 'def Func(): number',
1713 'return "a"',
1714 'enddef',
1715 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001716 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001717 CheckScriptFailure([
1718 'def Func(): string',
1719 'return 1',
1720 'enddef',
1721 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001722 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001723 CheckScriptFailure([
1724 'def Func(): void',
1725 'return "a"',
1726 'enddef',
1727 'defcompile'],
1728 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001729 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001730 CheckScriptFailure([
1731 'def Func()',
1732 'return "a"',
1733 'enddef',
1734 'defcompile'],
1735 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001736 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001737
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001738 CheckScriptFailure([
1739 'def Func(): number',
1740 'return',
1741 'enddef',
1742 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001743 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001744
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001745 CheckScriptFailure([
1746 'def Func():number',
1747 'return 123',
1748 'enddef',
1749 'defcompile'], 'E1069:')
1750 delfunc! g:Func
1751
1752 CheckScriptFailure([
1753 'def Func() :number',
1754 'return 123',
1755 'enddef',
1756 'defcompile'], 'E1059:')
1757 delfunc! g:Func
1758
1759 CheckScriptFailure([
1760 'def Func() : number',
1761 'return 123',
1762 'enddef',
1763 'defcompile'], 'E1059:')
1764 delfunc! g:Func
1765
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001766 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001767 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001768 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001769 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001770 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001771 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001772
1773 CheckScriptFailure([
1774 'vim9script',
1775 'def FuncB()',
1776 ' return 123',
1777 'enddef',
1778 'def FuncA()',
1779 ' FuncB()',
1780 'enddef',
1781 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001782enddef
1783
1784def Test_arg_type_wrong()
1785 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001786 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001787 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001788 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001789 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1790 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791enddef
1792
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001793def Test_white_space_before_comma()
1794 var lines =<< trim END
1795 vim9script
1796 def Func(a: number , b: number)
1797 enddef
1798 END
1799 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001800 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001801enddef
1802
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001803def Test_white_space_after_comma()
1804 var lines =<< trim END
1805 vim9script
1806 def Func(a: number,b: number)
1807 enddef
1808 END
1809 CheckScriptFailure(lines, 'E1069:')
1810
1811 # OK in legacy function
1812 lines =<< trim END
1813 vim9script
1814 func Func(a,b)
1815 endfunc
1816 END
1817 CheckScriptSuccess(lines)
1818enddef
1819
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001820def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001821 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001822 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001823 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001824 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001825 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001826 enddef
1827 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001828 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001829
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001830 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001831 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001832 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833
Bram Moolenaar67979662020-06-20 22:50:47 +02001834 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001835 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001836 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001837
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001838 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001839 def ListFunc(arg: list<number>)
1840 listvar = arg
1841 enddef
1842 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001843 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001844
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001845 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001846 def DictFunc(arg: dict<number>)
1847 dictvar = arg
1848 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001849 {a: 1, b: 2}->DictFunc()
1850 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001851 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001852 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001853 enddef
1854 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001855 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001856
Bram Moolenaare0de1712020-12-02 17:36:54 +01001857 {a: 3, b: 4}->DictFunc()
1858 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001859
1860 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001861 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001862 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001863 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001864
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001865 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001866 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001867 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001868 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001869 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001870 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001871
1872 def UseString()
1873 'xyork'->MyFunc()
1874 enddef
1875 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001876 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001877
Bram Moolenaar10409562020-07-29 20:00:38 +02001878 def UseString2()
1879 "knife"->MyFunc()
1880 enddef
1881 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001882 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001883
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001884 # prepending a colon makes it a mark
1885 new
1886 setline(1, ['aaa', 'bbb', 'ccc'])
1887 normal! 3Gmt1G
1888 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001889 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001890 bwipe!
1891
Bram Moolenaare6b53242020-07-01 17:28:33 +02001892 MyFunc(
1893 'continued'
1894 )
1895 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001896 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001897 )
1898
1899 call MyFunc(
1900 'more'
1901 ..
1902 'lines'
1903 )
1904 assert_equal(
1905 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001906 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001907 END
1908 writefile(lines, 'Xcall.vim')
1909 source Xcall.vim
1910 delete('Xcall.vim')
1911enddef
1912
1913def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001914 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001915 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001916 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001917 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001918 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001919 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001920 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001921 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001922 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001923enddef
1924
Bram Moolenaar65b95452020-07-19 14:03:09 +02001925def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001926 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001927 vim9script
1928 def MyFunc(arg: string)
1929 echo arg
1930 enddef
1931 MyFunc(1234)
1932 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001933 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001934enddef
1935
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001936def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001937 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001938 vim9script
1939 const var = ''
1940 def MyFunc(arg: string)
1941 var = 'asdf'
1942 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001943 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001944 END
1945 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001946 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001947 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001948
1949 lines =<< trim END
1950 const g:Aconst = 77
1951 def Change()
1952 # comment
1953 g:Aconst = 99
1954 enddef
1955 call Change()
1956 unlet g:Aconst
1957 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001958 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001959enddef
1960
1961" Test that inside :function a Python function can be defined, :def is not
1962" recognized.
1963func Test_function_python()
1964 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001965 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001966 execute py "<< EOF"
1967def do_something():
1968 return 1
1969EOF
1970endfunc
1971
1972def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001973 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001974 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001975 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001976 echo 'hello'
1977 enddef
1978
1979 def CallGoneSoon()
1980 GoneSoon()
1981 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001982 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001983
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001984 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001985 CallGoneSoon()
1986 END
1987 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001988 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1989 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001990
1991 delete('XToDelFunc')
1992enddef
1993
Bram Moolenaar7509ad82021-12-14 18:14:37 +00001994func Test_free_dict_while_in_funcstack()
1995 " relies on the sleep command
1996 CheckUnix
1997 call Run_Test_free_dict_while_in_funcstack()
1998endfunc
1999
2000def Run_Test_free_dict_while_in_funcstack()
2001
2002 # this was freeing the TermRun() default argument dictionary while it was
2003 # still referenced in a funcstack_T
2004 var lines =<< trim END
2005 vim9script
2006
2007 &updatetime = 400
2008 def TermRun(_ = {})
2009 def Post()
2010 enddef
2011 def Exec()
2012 term_start('sleep 1', {
2013 term_finish: 'close',
2014 exit_cb: (_, _) => Post(),
2015 })
2016 enddef
2017 Exec()
2018 enddef
2019 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
2020 timer_start(100, (_) => feedkeys("\<F4>"))
2021 timer_start(1000, (_) => feedkeys("\<F4>"))
2022 sleep 1500m
2023 END
2024 CheckScriptSuccess(lines)
2025 nunmap <F4>
2026 set updatetime&
2027enddef
2028
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002029def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002030 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002031 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002032 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002033 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002034 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002035 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002036 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002037 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002038 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002040 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002041 g:Func1()->assert_equal('Func1')
2042 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002043
2044 delfunc! Func0
2045 delfunc! Func1
2046 delfunc! Func2
2047enddef
2048
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002049def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002050 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002051 vim9script
2052 func Func(arg)
2053 echo a:arg
2054 endfunc
2055 Func('text')
2056 END
2057 writefile(lines, 'XVim9Func')
2058 so XVim9Func
2059
2060 delete('XVim9Func')
2061enddef
2062
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002063let s:funcResult = 0
2064
2065def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002066 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002067enddef
2068
2069def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002070 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002071 return 1234
2072enddef
2073
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002074def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002075 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002076 return 'text'
2077enddef
2078
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002079def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002080 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002081enddef
2082
2083def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002084 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002085 return arg
2086enddef
2087
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002088def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002089 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002090enddef
2091
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002092def FuncOneArgRetString(arg: string): string
2093 return arg
2094enddef
2095
Bram Moolenaar89228602020-04-05 22:14:54 +02002096def FuncOneArgRetAny(arg: any): any
2097 return arg
2098enddef
2099
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002100def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002101 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002102 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002103 Ref1 = FuncNoArgNoRet
2104 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002105 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002106
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002107 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002108 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002109 Ref2 = FuncNoArgNoRet
2110 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002111 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002112
Bram Moolenaar53900992020-08-22 19:02:02 +02002113 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002114 Ref2 = FuncOneArgNoRet
2115 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002116 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002117
Bram Moolenaar53900992020-08-22 19:02:02 +02002118 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002119 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002120 Ref2()->assert_equal(1234)
2121 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002122
Bram Moolenaar53900992020-08-22 19:02:02 +02002123 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002124 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002125 Ref2(13)->assert_equal(13)
2126 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002127enddef
2128
Bram Moolenaar9978d472020-07-05 16:01:56 +02002129def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002130 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002131 for n in repeat([1], 3)
2132 res += n
2133 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002134 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002135
2136 res = 0
2137 for n in add([1, 2], 3)
2138 res += n
2139 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002140 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002141enddef
2142
Bram Moolenaar846178a2020-07-05 17:04:13 +02002143def Test_argv_return_type()
2144 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002145 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002146 for name in argv()
2147 res ..= name
2148 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002149 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002150enddef
2151
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002152def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002153 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002154 RefVoid = FuncNoArgNoRet
2155 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002156 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2157 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002158
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002159 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002160 RefAny = FuncNoArgRetNumber
2161 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002162 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2163 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002164
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002165 var RefAnyNoArgs: func: any = RefAny
2166
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002167 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002168 RefNr = FuncNoArgRetNumber
2169 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002170 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2171 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002172
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002173 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002174 RefStr = FuncNoArgRetString
2175 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002176 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2177 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002178enddef
2179
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002180def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002181 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002182
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002183 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2184 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2185 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2186 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2187 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2188 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 +02002189
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002190 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2191 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2192 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:')
2193 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002194enddef
2195
Bram Moolenaar89228602020-04-05 22:14:54 +02002196def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002197 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002198 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002199 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002200
2201 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002202 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002203
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002204 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002205 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002206 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002207
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002208 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002209enddef
2210
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002211def Test_func_common_type()
2212 def FuncOne(n: number): number
2213 return n
2214 enddef
2215 def FuncTwo(s: string): number
2216 return len(s)
2217 enddef
2218 def FuncThree(n: number, s: string): number
2219 return n + len(s)
2220 enddef
2221 var list = [FuncOne, FuncTwo, FuncThree]
2222 assert_equal(8, list[0](8))
2223 assert_equal(4, list[1]('word'))
2224 assert_equal(7, list[2](3, 'word'))
2225enddef
2226
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002227def MultiLine(
2228 arg1: string,
2229 arg2 = 1234,
2230 ...rest: list<string>
2231 ): string
2232 return arg1 .. arg2 .. join(rest, '-')
2233enddef
2234
Bram Moolenaar2c330432020-04-13 14:41:35 +02002235def MultiLineComment(
2236 arg1: string, # comment
2237 arg2 = 1234, # comment
2238 ...rest: list<string> # comment
2239 ): string # comment
2240 return arg1 .. arg2 .. join(rest, '-')
2241enddef
2242
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002243def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002244 MultiLine('text')->assert_equal('text1234')
2245 MultiLine('text', 777)->assert_equal('text777')
2246 MultiLine('text', 777, 'one')->assert_equal('text777one')
2247 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002248enddef
2249
Bram Moolenaar23e03252020-04-12 22:22:31 +02002250func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002251 call MultiLine('text')->assert_equal('text1234')
2252 call MultiLine('text', 777)->assert_equal('text777')
2253 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2254 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002255endfunc
2256
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002257
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002258" When using CheckScriptFailure() for the below test, E1010 is generated instead
2259" of E1056.
2260func Test_E1056_1059()
2261 let caught_1056 = 0
2262 try
2263 def F():
2264 return 1
2265 enddef
2266 catch /E1056:/
2267 let caught_1056 = 1
2268 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002269 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002270
2271 let caught_1059 = 0
2272 try
2273 def F5(items : list)
2274 echo 'a'
2275 enddef
2276 catch /E1059:/
2277 let caught_1059 = 1
2278 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002279 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002280endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002281
Bram Moolenaar015f4262020-05-05 21:25:22 +02002282func DelMe()
2283 echo 'DelMe'
2284endfunc
2285
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002286def Test_error_reporting()
2287 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002288 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002289 " comment
2290 def Func()
2291 # comment
2292 # comment
2293 invalid
2294 enddef
2295 defcompile
2296 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002297 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002298 try
2299 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002300 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002301 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002302 v:exception->assert_match('Invalid command: invalid')
2303 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002304 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002305 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002306
2307 # comment lines after the start of the function
2308 lines =<< trim END
2309 " comment
2310 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002311 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002312 # comment
2313 # comment
2314 invalid
2315 enddef
2316 defcompile
2317 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002318 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002319 try
2320 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002321 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002322 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002323 v:exception->assert_match('Invalid command: invalid')
2324 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002325 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002326 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002327
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002328 lines =<< trim END
2329 vim9script
2330 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002331 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002332 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002333 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002334 enddef
2335 defcompile
2336 Func()
2337 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002338 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002339 try
2340 source Xdef
2341 assert_report('should have failed')
2342 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002343 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002344 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002345 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002346
Bram Moolenaar08052222020-09-14 17:04:31 +02002347 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002348enddef
2349
Bram Moolenaar015f4262020-05-05 21:25:22 +02002350def Test_deleted_function()
2351 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002352 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002353 'delfunc g:DelMe',
2354 'echo RefMe()'], 'E117:')
2355enddef
2356
2357def Test_unknown_function()
2358 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002359 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002360 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002361enddef
2362
Bram Moolenaar328eac22021-01-07 19:23:08 +01002363def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002364 return Ref('more')
2365enddef
2366
2367def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002368 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002369 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002370enddef
2371
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002372def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002373 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002374 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002375enddef
2376
2377def Test_closure_ref_after_return()
2378 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002379 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002380 unlet g:Ref
2381enddef
2382
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002383def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002384 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002385 g:Extend = (s) => local->add(s)
2386 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002387enddef
2388
2389def Test_closure_two_refs()
2390 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002391 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002392 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002393 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002394 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002395 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002396
2397 unlet g:Extend
2398 unlet g:Read
2399enddef
2400
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002401def ReadRef(Ref: func(): list<string>): string
2402 return join(Ref(), ' ')
2403enddef
2404
Bram Moolenaar5e654232020-09-16 15:22:00 +02002405def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002406 Ref(add)
2407enddef
2408
2409def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002410 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002411 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002412 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002413 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002414 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002415 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002416
2417 unlet g:Extend
2418 unlet g:Read
2419enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002420
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002421def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002422 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002423 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002424enddef
2425
2426def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002427 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002428 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002429enddef
2430
2431def Test_closure_using_argument()
2432 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002433 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002434
2435 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002436 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002437
2438 unlet g:UseArg
2439 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002440
2441 var lines =<< trim END
2442 vim9script
2443 def Test(Fun: func(number): number): list<number>
2444 return map([1, 2, 3], (_, i) => Fun(i))
2445 enddef
2446 def Inc(nr: number): number
2447 return nr + 2
2448 enddef
2449 assert_equal([3, 4, 5], Test(Inc))
2450 END
2451 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002452enddef
2453
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002454def MakeGetAndAppendRefs()
2455 var local = 'a'
2456
2457 def Append(arg: string)
2458 local ..= arg
2459 enddef
2460 g:Append = Append
2461
2462 def Get(): string
2463 return local
2464 enddef
2465 g:Get = Get
2466enddef
2467
2468def Test_closure_append_get()
2469 MakeGetAndAppendRefs()
2470 g:Get()->assert_equal('a')
2471 g:Append('-b')
2472 g:Get()->assert_equal('a-b')
2473 g:Append('-c')
2474 g:Get()->assert_equal('a-b-c')
2475
2476 unlet g:Append
2477 unlet g:Get
2478enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002479
Bram Moolenaar04b12692020-05-04 23:24:44 +02002480def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002481 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002482 def Closure(arg: string): string
2483 return local .. arg
2484 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002485 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002486enddef
2487
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002488func GetResult(Ref)
2489 return a:Ref('some')
2490endfunc
2491
2492def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002493 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002494 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002495 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002496enddef
2497
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002498def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002499 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002500 vim9script
2501 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002502 var name = 0
2503 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002504 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002505 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002506 enddef
2507 Func()
2508 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002509 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002510enddef
2511
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002512def Test_nested_closure_used()
2513 var lines =<< trim END
2514 vim9script
2515 def Func()
2516 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002517 var Closure = () => x
2518 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002519 enddef
2520 Func()
2521 assert_equal('hello', g:Myclosure())
2522 END
2523 CheckScriptSuccess(lines)
2524enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002525
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002526def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002527 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002528 vim9script
2529 def FuncA()
2530 FuncB(0)
2531 enddef
2532 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002533 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002534 enddef
2535 FuncA()
2536 END
2537 CheckScriptFailure(lines, 'E1012:')
2538enddef
2539
Bram Moolenaarf112f302020-12-20 17:47:52 +01002540def Test_global_closure()
2541 var lines =<< trim END
2542 vim9script
2543 def ReverseEveryNLines(n: number, line1: number, line2: number)
2544 var mods = 'sil keepj keepp lockm '
2545 var range = ':' .. line1 .. ',' .. line2
2546 def g:Offset(): number
2547 var offset = (line('.') - line1 + 1) % n
2548 return offset != 0 ? offset : n
2549 enddef
2550 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2551 enddef
2552
2553 new
2554 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2555 ReverseEveryNLines(3, 1, 9)
2556 END
2557 CheckScriptSuccess(lines)
2558 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2559 assert_equal(expected, getline(1, 9))
2560 bwipe!
2561enddef
2562
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002563def Test_global_closure_called_directly()
2564 var lines =<< trim END
2565 vim9script
2566 def Outer()
2567 var x = 1
2568 def g:Inner()
2569 var y = x
2570 x += 1
2571 assert_equal(1, y)
2572 enddef
2573 g:Inner()
2574 assert_equal(2, x)
2575 enddef
2576 Outer()
2577 END
2578 CheckScriptSuccess(lines)
2579 delfunc g:Inner
2580enddef
2581
Bram Moolenaar69c76172021-12-02 16:38:52 +00002582def Test_closure_called_from_legacy()
2583 var lines =<< trim END
2584 vim9script
2585 def Func()
2586 var outer = 'foo'
2587 var F = () => {
2588 outer = 'bar'
2589 }
2590 execute printf('call %s()', string(F))
2591 enddef
2592 Func()
2593 END
2594 CheckScriptFailure(lines, 'E1248')
2595enddef
2596
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002597def Test_failure_in_called_function()
2598 # this was using the frame index as the return value
2599 var lines =<< trim END
2600 vim9script
2601 au TerminalWinOpen * eval [][0]
2602 def PopupTerm(a: any)
2603 # make sure typvals on stack are string
2604 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2605 FireEvent()
2606 enddef
2607 def FireEvent()
2608 do TerminalWinOpen
2609 enddef
2610 # use try/catch to make eval fail
2611 try
2612 call PopupTerm(0)
2613 catch
2614 endtry
2615 au! TerminalWinOpen
2616 END
2617 CheckScriptSuccess(lines)
2618enddef
2619
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002620def Test_nested_lambda()
2621 var lines =<< trim END
2622 vim9script
2623 def Func()
2624 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002625 var Lambda1 = () => 7
2626 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002627 var res = Lambda2()
2628 assert_equal([7, 4], res)
2629 enddef
2630 Func()
2631 END
2632 CheckScriptSuccess(lines)
2633enddef
2634
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002635def Test_double_nested_lambda()
2636 var lines =<< trim END
2637 vim9script
2638 def F(head: string): func(string): func(string): string
2639 return (sep: string): func(string): string => ((tail: string): string => {
2640 return head .. sep .. tail
2641 })
2642 enddef
2643 assert_equal('hello-there', F('hello')('-')('there'))
2644 END
2645 CheckScriptSuccess(lines)
2646enddef
2647
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002648def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002649 var lines =<< trim END
2650 vim9script
2651 def F(text: string): func(string): func(string): string
2652 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002653 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002654 })
2655 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002656 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002657 END
2658 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002659
2660 lines =<< trim END
2661 vim9script
2662 echo range(4)->mapnew((_, v) => {
2663 return range(v) ->mapnew((_, s) => {
2664 return string(s)
2665 })
2666 })
2667 END
2668 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002669
2670 lines =<< trim END
2671 vim9script
2672
2673 def s:func()
2674 range(10)
2675 ->mapnew((_, _) => ({
2676 key: range(10)->mapnew((_, _) => {
2677 return ' '
2678 }),
2679 }))
2680 enddef
2681
2682 defcomp
2683 END
2684 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002685enddef
2686
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002687def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002688 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002689 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002690enddef
2691
2692def Test_lambda_arg_shadows_func()
2693 assert_equal([42], Shadowed())
2694enddef
2695
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002696def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002697 var path: string = empty(dir)
2698 \ ? 'empty'
2699 \ : 'full'
2700 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002701enddef
2702
2703def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002704 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002705enddef
2706
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002707def Test_script_var_in_lambda()
2708 var lines =<< trim END
2709 vim9script
2710 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002711 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002712 END
2713 CheckScriptSuccess(lines)
2714enddef
2715
Bram Moolenaar5e654232020-09-16 15:22:00 +02002716def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002717 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002718 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002719 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002720 ->reverse()
2721 return x
2722enddef
2723
2724def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002725 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002726
2727 var lines =<< trim END
2728 vim9script
2729 var res = [{n: 1, m: 2, s: 'xxx'}]
2730 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2731 v.n,
2732 v.m,
2733 substitute(v.s, '.*', 'yyy', '')
2734 ))
2735 assert_equal(['1:2:yyy'], res)
2736 END
2737 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002738enddef
2739
Bram Moolenaarb6571982021-01-08 22:24:19 +01002740def Test_list_lambda()
2741 timer_start(1000, (_) => 0)
2742 var body = execute(timer_info()[0].callback
2743 ->string()
2744 ->substitute("('", ' ', '')
2745 ->substitute("')", '', '')
2746 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002747 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002748enddef
2749
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002750def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002751 var lines =<< trim END
2752 vim9script
2753 var flist: list<func>
2754 for i in range(10)
2755 var inloop = i
2756 flist[i] = () => inloop
2757 endfor
2758 END
2759 CheckScriptSuccess(lines)
2760
2761 lines =<< trim END
2762 vim9script
2763 if true
2764 var outloop = 5
2765 var flist: list<func>
2766 for i in range(10)
2767 flist[i] = () => outloop
2768 endfor
2769 endif
2770 END
2771 CheckScriptSuccess(lines)
2772
2773 lines =<< trim END
2774 vim9script
2775 if true
2776 var outloop = 5
2777 endif
2778 var flist: list<func>
2779 for i in range(10)
2780 flist[i] = () => outloop
2781 endfor
2782 END
2783 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002784
2785 lines =<< trim END
2786 vim9script
2787 for i in range(10)
2788 var Ref = () => 0
2789 endfor
2790 assert_equal(0, ((i) => 0)(0))
2791 END
2792 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002793enddef
2794
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002795def Test_legacy_lambda()
2796 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002797
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002798 var lines =<< trim END
2799 echo {x -> 'hello ' .. x}('foo')
2800 END
2801 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002802
2803 lines =<< trim END
2804 vim9script
2805 def Func()
2806 echo (() => 'no error')()
2807 enddef
2808 legacy call s:Func()
2809 END
2810 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002811enddef
2812
Bram Moolenaarce024c32021-06-26 13:00:49 +02002813def Test_legacy()
2814 var lines =<< trim END
2815 vim9script
2816 func g:LegacyFunction()
2817 let g:legacyvar = 1
2818 endfunc
2819 def Testit()
2820 legacy call g:LegacyFunction()
2821 enddef
2822 Testit()
2823 assert_equal(1, g:legacyvar)
2824 unlet g:legacyvar
2825 delfunc g:LegacyFunction
2826 END
2827 CheckScriptSuccess(lines)
2828enddef
2829
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002830def Test_legacy_errors()
2831 for cmd in ['if', 'elseif', 'else', 'endif',
2832 'for', 'endfor', 'continue', 'break',
2833 'while', 'endwhile',
2834 'try', 'catch', 'finally', 'endtry']
2835 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2836 endfor
2837enddef
2838
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002839def Test_call_legacy_with_dict()
2840 var lines =<< trim END
2841 vim9script
2842 func Legacy() dict
2843 let g:result = self.value
2844 endfunc
2845 def TestDirect()
2846 var d = {value: 'yes', func: Legacy}
2847 d.func()
2848 enddef
2849 TestDirect()
2850 assert_equal('yes', g:result)
2851 unlet g:result
2852
2853 def TestIndirect()
2854 var d = {value: 'foo', func: Legacy}
2855 var Fi = d.func
2856 Fi()
2857 enddef
2858 TestIndirect()
2859 assert_equal('foo', g:result)
2860 unlet g:result
2861
2862 var d = {value: 'bar', func: Legacy}
2863 d.func()
2864 assert_equal('bar', g:result)
2865 unlet g:result
2866 END
2867 CheckScriptSuccess(lines)
2868enddef
2869
Bram Moolenaarab360522021-01-10 14:02:28 +01002870def DoFilterThis(a: string): list<string>
2871 # closure nested inside another closure using argument
2872 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2873 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2874enddef
2875
2876def Test_nested_closure_using_argument()
2877 assert_equal(['x', 'x2'], DoFilterThis('x'))
2878enddef
2879
Bram Moolenaar0186e582021-01-10 18:33:11 +01002880def Test_triple_nested_closure()
2881 var what = 'x'
2882 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2883 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2884 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2885enddef
2886
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002887func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002888 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002889 call Run_Test_silent_echo()
2890endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002891
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002892def Run_Test_silent_echo()
2893 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002894 vim9script
2895 def EchoNothing()
2896 silent echo ''
2897 enddef
2898 defcompile
2899 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002900 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002901
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002902 # Check that the balloon shows up after a mouse move
2903 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2904 term_sendkeys(buf, ":abc")
2905 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002906
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002907 # clean up
2908 StopVimInTerminal(buf)
2909 delete('XTest_silent_echo')
2910enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002911
Bram Moolenaar171fb922020-10-28 16:54:47 +01002912def SilentlyError()
2913 execute('silent! invalid')
2914 g:did_it = 'yes'
2915enddef
2916
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002917func UserError()
2918 silent! invalid
2919endfunc
2920
2921def SilentlyUserError()
2922 UserError()
2923 g:did_it = 'yes'
2924enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002925
2926" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002927func Test_ignore_silent_error()
2928 let g:did_it = 'no'
2929 call SilentlyError()
2930 call assert_equal('yes', g:did_it)
2931
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002932 let g:did_it = 'no'
2933 call SilentlyUserError()
2934 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002935
2936 unlet g:did_it
2937endfunc
2938
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002939def Test_ignore_silent_error_in_filter()
2940 var lines =<< trim END
2941 vim9script
2942 def Filter(winid: number, key: string): bool
2943 if key == 'o'
2944 silent! eval [][0]
2945 return true
2946 endif
2947 return popup_filter_menu(winid, key)
2948 enddef
2949
Bram Moolenaare0de1712020-12-02 17:36:54 +01002950 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002951 feedkeys("o\r", 'xnt')
2952 END
2953 CheckScriptSuccess(lines)
2954enddef
2955
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002956def Fibonacci(n: number): number
2957 if n < 2
2958 return n
2959 else
2960 return Fibonacci(n - 1) + Fibonacci(n - 2)
2961 endif
2962enddef
2963
Bram Moolenaar985116a2020-07-12 17:31:09 +02002964def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002965 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002966enddef
2967
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002968def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002969 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002970 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002971 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002972 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002973 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002974enddef
2975
2976def Test_closure_in_map()
2977 mkdir('XclosureDir/tdir', 'p')
2978 writefile(['111'], 'XclosureDir/file1')
2979 writefile(['222'], 'XclosureDir/file2')
2980 writefile(['333'], 'XclosureDir/tdir/file3')
2981
Bram Moolenaare0de1712020-12-02 17:36:54 +01002982 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002983
2984 delete('XclosureDir', 'rf')
2985enddef
2986
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002987def Test_invalid_function_name()
2988 var lines =<< trim END
2989 vim9script
2990 def s: list<string>
2991 END
2992 CheckScriptFailure(lines, 'E129:')
2993
2994 lines =<< trim END
2995 vim9script
2996 def g: list<string>
2997 END
2998 CheckScriptFailure(lines, 'E129:')
2999
3000 lines =<< trim END
3001 vim9script
3002 def <SID>: list<string>
3003 END
3004 CheckScriptFailure(lines, 'E884:')
3005
3006 lines =<< trim END
3007 vim9script
3008 def F list<string>
3009 END
3010 CheckScriptFailure(lines, 'E488:')
3011enddef
3012
Bram Moolenaara90afb92020-07-15 22:38:56 +02003013def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003014 var lines =<< trim END
3015 var Xsetlist: func
3016 Xsetlist = function('setloclist', [0])
3017 Xsetlist([], ' ', {title: 'test'})
3018 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003019
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003020 Xsetlist = function('setloclist', [0, [], ' '])
3021 Xsetlist({title: 'test'})
3022 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003023
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003024 Xsetlist = function('setqflist')
3025 Xsetlist([], ' ', {title: 'test'})
3026 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003027
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003028 Xsetlist = function('setqflist', [[], ' '])
3029 Xsetlist({title: 'test'})
3030 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02003031
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003032 var Len: func: number = function('len', ['word'])
3033 assert_equal(4, Len())
3034
3035 var RepeatFunc = function('repeat', ['o'])
3036 assert_equal('ooooo', RepeatFunc(5))
3037 END
3038 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003039
3040 lines =<< trim END
3041 vim9script
3042 def Foo(Parser: any)
3043 enddef
3044 var Expr: func(dict<any>): dict<any>
3045 const Call = Foo(Expr)
3046 END
3047 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003048enddef
3049
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003050def Test_cmd_modifier()
3051 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003052 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003053enddef
3054
3055def Test_restore_modifiers()
3056 # check that when compiling a :def function command modifiers are not messed
3057 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003058 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003059 vim9script
3060 set eventignore=
3061 autocmd QuickFixCmdPost * copen
3062 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003063 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003064 enddef
3065 func Func()
3066 noautocmd call s:AutocmdsDisabled()
3067 let g:ei_after = &eventignore
3068 endfunc
3069 Func()
3070 END
3071 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003072 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003073enddef
3074
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003075def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003076 eval 1 + 2
3077 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003078 # call not on fourth line
3079 StackBot()
3080enddef
3081
3082def StackBot()
3083 # throw an error
3084 eval [][0]
3085enddef
3086
3087def Test_callstack_def()
3088 try
3089 StackTop()
3090 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003091 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003092 endtry
3093enddef
3094
Bram Moolenaare8211a32020-10-09 22:04:29 +02003095" Re-using spot for variable used in block
3096def Test_block_scoped_var()
3097 var lines =<< trim END
3098 vim9script
3099 def Func()
3100 var x = ['a', 'b', 'c']
3101 if 1
3102 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003103 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003104 endif
3105 var z = x
3106 assert_equal(['x', 'x', 'x'], z)
3107 enddef
3108 Func()
3109 END
3110 CheckScriptSuccess(lines)
3111enddef
3112
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003113def Test_reset_did_emsg()
3114 var lines =<< trim END
3115 @s = 'blah'
3116 au BufWinLeave * #
3117 def Func()
3118 var winid = popup_create('popup', {})
3119 exe '*s'
3120 popup_close(winid)
3121 enddef
3122 Func()
3123 END
3124 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003125 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003126enddef
3127
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003128def Test_did_emsg_reset()
3129 # executing an autocommand resets did_emsg, this should not result in a
3130 # builtin function considered failing
3131 var lines =<< trim END
3132 vim9script
3133 au BufWinLeave * #
3134 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003135 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003136 eval [][0]
3137 enddef
3138 nno <F3> <cmd>call <sid>Func()<cr>
3139 feedkeys("\<F3>\e", 'xt')
3140 END
3141 writefile(lines, 'XemsgReset')
3142 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3143 delete('XemsgReset')
3144 nunmap <F3>
3145 au! BufWinLeave
3146enddef
3147
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003148def Test_abort_with_silent_call()
3149 var lines =<< trim END
3150 vim9script
3151 g:result = 'none'
3152 def Func()
3153 g:result += 3
3154 g:result = 'yes'
3155 enddef
3156 # error is silenced, but function aborts on error
3157 silent! Func()
3158 assert_equal('none', g:result)
3159 unlet g:result
3160 END
3161 CheckScriptSuccess(lines)
3162enddef
3163
Bram Moolenaarf665e972020-12-05 19:17:16 +01003164def Test_continues_with_silent_error()
3165 var lines =<< trim END
3166 vim9script
3167 g:result = 'none'
3168 def Func()
3169 silent! g:result += 3
3170 g:result = 'yes'
3171 enddef
3172 # error is silenced, function does not abort
3173 Func()
3174 assert_equal('yes', g:result)
3175 unlet g:result
3176 END
3177 CheckScriptSuccess(lines)
3178enddef
3179
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003180def Test_abort_even_with_silent()
3181 var lines =<< trim END
3182 vim9script
3183 g:result = 'none'
3184 def Func()
3185 eval {-> ''}() .. '' .. {}['X']
3186 g:result = 'yes'
3187 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003188 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003189 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003190 unlet g:result
3191 END
3192 CheckScriptSuccess(lines)
3193enddef
3194
Bram Moolenaarf665e972020-12-05 19:17:16 +01003195def Test_cmdmod_silent_restored()
3196 var lines =<< trim END
3197 vim9script
3198 def Func()
3199 g:result = 'none'
3200 silent! g:result += 3
3201 g:result = 'none'
3202 g:result += 3
3203 enddef
3204 Func()
3205 END
3206 # can't use CheckScriptFailure, it ignores the :silent!
3207 var fname = 'Xdefsilent'
3208 writefile(lines, fname)
3209 var caught = 'no'
3210 try
3211 exe 'source ' .. fname
3212 catch /E1030:/
3213 caught = 'yes'
3214 assert_match('Func, line 4', v:throwpoint)
3215 endtry
3216 assert_equal('yes', caught)
3217 delete(fname)
3218enddef
3219
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003220def Test_cmdmod_silent_nested()
3221 var lines =<< trim END
3222 vim9script
3223 var result = ''
3224
3225 def Error()
3226 result ..= 'Eb'
3227 eval [][0]
3228 result ..= 'Ea'
3229 enddef
3230
3231 def Crash()
3232 result ..= 'Cb'
3233 sil! Error()
3234 result ..= 'Ca'
3235 enddef
3236
3237 Crash()
3238 assert_equal('CbEbEaCa', result)
3239 END
3240 CheckScriptSuccess(lines)
3241enddef
3242
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003243def Test_dict_member_with_silent()
3244 var lines =<< trim END
3245 vim9script
3246 g:result = 'none'
3247 var d: dict<any>
3248 def Func()
3249 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003250 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003251 catch
3252 endtry
3253 enddef
3254 silent! Func()
3255 assert_equal('0', g:result)
3256 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003257 END
3258 CheckScriptSuccess(lines)
3259enddef
3260
Bram Moolenaarf9041332021-01-21 19:41:16 +01003261def Test_skip_cmds_with_silent()
3262 var lines =<< trim END
3263 vim9script
3264
3265 def Func(b: bool)
3266 Crash()
3267 enddef
3268
3269 def Crash()
3270 sil! :/not found/d _
3271 sil! :/not found/put _
3272 enddef
3273
3274 Func(true)
3275 END
3276 CheckScriptSuccess(lines)
3277enddef
3278
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003279def Test_opfunc()
3280 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3281 def g:Opfunc(_: any): string
3282 setline(1, 'ASDF')
3283 return ''
3284 enddef
3285 new
3286 setline(1, 'asdf')
3287 feedkeys("\<F3>$", 'x')
3288 assert_equal('ASDF', getline(1))
3289
3290 bwipe!
3291 nunmap <F3>
3292enddef
3293
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003294func Test_opfunc_error()
3295 CheckScreendump
3296 call Run_Test_opfunc_error()
3297endfunc
3298
3299def Run_Test_opfunc_error()
3300 # test that the error from Opfunc() is displayed right away
3301 var lines =<< trim END
3302 vim9script
3303
3304 def Opfunc(type: string)
3305 try
3306 eval [][0]
3307 catch /nothing/ # error not caught
3308 endtry
3309 enddef
3310 &operatorfunc = Opfunc
3311 nnoremap <expr> l <SID>L()
3312 def L(): string
3313 return 'l'
3314 enddef
3315 'x'->repeat(10)->setline(1)
3316 feedkeys('g@l', 'n')
3317 feedkeys('llll')
3318 END
3319 call writefile(lines, 'XTest_opfunc_error')
3320
3321 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003322 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003323 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003324
3325 # clean up
3326 StopVimInTerminal(buf)
3327 delete('XTest_opfunc_error')
3328enddef
3329
Bram Moolenaar077a4232020-12-22 18:33:27 +01003330" this was crashing on exit
3331def Test_nested_lambda_in_closure()
3332 var lines =<< trim END
3333 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003334 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003335 def Outer()
3336 def g:Inner()
3337 echo map([1, 2, 3], {_, v -> v + 1})
3338 enddef
3339 g:Inner()
3340 enddef
3341 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003342 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003343 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003344 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003345 return
3346 endif
3347 assert_equal(['Done'], readfile('XnestedDone'))
3348 delete('XnestedDone')
3349enddef
3350
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003351def Test_check_func_arg_types()
3352 var lines =<< trim END
3353 vim9script
3354 def F1(x: string): string
3355 return x
3356 enddef
3357
3358 def F2(x: number): number
3359 return x + 1
3360 enddef
3361
3362 def G(g: func): dict<func>
3363 return {f: g}
3364 enddef
3365
3366 def H(d: dict<func>): string
3367 return d.f('a')
3368 enddef
3369 END
3370
3371 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3372 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3373enddef
3374
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003375def Test_list_any_type_checked()
3376 var lines =<< trim END
3377 vim9script
3378 def Foo()
3379 --decl--
3380 Bar(l)
3381 enddef
3382 def Bar(ll: list<dict<any>>)
3383 enddef
3384 Foo()
3385 END
3386 lines[2] = 'var l: list<any>'
3387 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3388
3389 lines[2] = 'var l: list<any> = []'
3390 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3391
3392 lines[2] = 'var l: list<any> = [11]'
3393 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3394enddef
3395
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003396def Test_compile_error()
3397 var lines =<< trim END
3398 def g:Broken()
3399 echo 'a' + {}
3400 enddef
3401 call g:Broken()
3402 END
3403 # First call: compilation error
3404 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3405
3406 # Second call won't try compiling again
3407 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003408 delfunc g:Broken
3409
3410 # No error when compiling with :silent!
3411 lines =<< trim END
3412 def g:Broken()
3413 echo 'a' + []
3414 enddef
3415 silent! defcompile
3416 END
3417 CheckScriptSuccess(lines)
3418
3419 # Calling the function won't try compiling again
3420 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3421 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003422enddef
3423
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003424def Test_ignored_argument()
3425 var lines =<< trim END
3426 vim9script
3427 def Ignore(_, _): string
3428 return 'yes'
3429 enddef
3430 assert_equal('yes', Ignore(1, 2))
3431
3432 func Ok(_)
3433 return a:_
3434 endfunc
3435 assert_equal('ok', Ok('ok'))
3436
3437 func Oktoo()
3438 let _ = 'too'
3439 return _
3440 endfunc
3441 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003442
3443 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003444 END
3445 CheckScriptSuccess(lines)
3446
3447 lines =<< trim END
3448 def Ignore(_: string): string
3449 return _
3450 enddef
3451 defcompile
3452 END
3453 CheckScriptFailure(lines, 'E1181:', 1)
3454
3455 lines =<< trim END
3456 var _ = 1
3457 END
3458 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003459
3460 lines =<< trim END
3461 var x = _
3462 END
3463 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003464enddef
3465
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003466def Test_too_many_arguments()
3467 var lines =<< trim END
3468 echo [0, 1, 2]->map(() => 123)
3469 END
3470 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3471
3472 lines =<< trim END
3473 echo [0, 1, 2]->map((_) => 123)
3474 END
3475 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3476enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003477
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003478def Test_closing_brace_at_start_of_line()
3479 var lines =<< trim END
3480 def Func()
3481 enddef
3482 Func(
3483 )
3484 END
3485 call CheckDefAndScriptSuccess(lines)
3486enddef
3487
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003488func CreateMydict()
3489 let g:mydict = {}
3490 func g:mydict.afunc()
3491 let g:result = self.key
3492 endfunc
3493endfunc
3494
3495def Test_numbered_function_reference()
3496 CreateMydict()
3497 var output = execute('legacy func g:mydict.afunc')
3498 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3499 execute 'function(' .. funcName .. ', [], {key: 42})()'
3500 # check that the function still exists
3501 assert_equal(output, execute('legacy func g:mydict.afunc'))
3502 unlet g:mydict
3503enddef
3504
Bram Moolenaar20677332021-06-06 17:02:53 +02003505if has('python3')
3506 def Test_python3_heredoc()
3507 py3 << trim EOF
3508 import vim
3509 vim.vars['didit'] = 'yes'
3510 EOF
3511 assert_equal('yes', g:didit)
3512
3513 python3 << trim EOF
3514 import vim
3515 vim.vars['didit'] = 'again'
3516 EOF
3517 assert_equal('again', g:didit)
3518 enddef
3519endif
3520
3521" This messes up syntax highlight, keep near the end.
3522if has('lua')
3523 def Test_lua_heredoc()
3524 g:d = {}
3525 lua << trim EOF
3526 x = vim.eval('g:d')
3527 x['key'] = 'val'
3528 EOF
3529 assert_equal('val', g:d.key)
3530 enddef
3531endif
3532
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003533
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003534" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker