blob: 025edd5dafa332152a854b45c2a597b8484d155f [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()
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000442 # This used to fail but now the actual list type is checked, and since it has
443 # an item of type string it can be used as list<string>.
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200444 var lines =<< trim END
445 vim9script
446 def Func(): list<string>
447 var l: list<any>
448 l->add('string')
449 return l
450 enddef
451 echo Func()
452 END
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000453 CheckScriptSuccess(lines)
454
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200455 lines =<< trim END
456 vim9script
457 def Func(): list<string>
458 var l: list<any>
459 l += ['string']
460 return l
461 enddef
462 echo Func()
463 END
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000464 CheckScriptSuccess(lines)
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200465enddef
466
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200467func Increment()
468 let g:counter += 1
469endfunc
470
471def Test_call_ufunc_count()
472 g:counter = 1
473 Increment()
474 Increment()
475 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200476 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200477 g:counter->assert_equal(4)
478 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200479 unlet g:counter
480enddef
481
482def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200483 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200484 for s in rest
485 res ..= ',' .. s
486 endfor
487 return res
488enddef
489
490def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200491 MyVarargs('one')->assert_equal('one')
492 MyVarargs('one', 'two')->assert_equal('one,two')
493 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200494enddef
495
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200496def Test_call_white_space()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000497 CheckDefAndScriptFailure(["call Test ('text')"], ['E476:', 'E1068:'])
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200498enddef
499
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200500def MyDefaultArgs(name = 'string'): string
501 return name
502enddef
503
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200504def MyDefaultSecond(name: string, second: bool = true): string
505 return second ? name : 'none'
506enddef
507
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200509def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200510 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200511 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200513 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200514
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200515 MyDefaultSecond('test')->assert_equal('test')
516 MyDefaultSecond('test', true)->assert_equal('test')
517 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200518
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200519 var lines =<< trim END
520 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
521 return name .. aa .. bb
522 enddef
523
524 MyDefaultThird('->')->assert_equal('->aabb')
525 MyDefaultThird('->', v:none)->assert_equal('->aabb')
526 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
527 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
528 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
529 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
530 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200531
532 def DefArg(mandatory: any, optional = mandatory): string
533 return mandatory .. optional
534 enddef
535 DefArg(1234)->assert_equal('12341234')
536 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200537 END
538 CheckDefAndScriptSuccess(lines)
539
Bram Moolenaar822ba242020-05-24 23:00:18 +0200540 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100541 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200542 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 +0100543 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200544 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100545
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200546 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100547 vim9script
548 def Func(a = b == 0 ? 1 : 2, b = 0)
549 enddef
550 defcompile
551 END
552 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000553
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000554 # using script variable requires matching type or type cast when executed
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000555 lines =<< trim END
556 vim9script
557 var a: any
558 def Func(arg: string = a)
559 echo arg
560 enddef
561 defcompile
562 END
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000563 CheckScriptSuccess(lines + ['a = "text"', 'Func()'])
564 CheckScriptFailure(lines + ['a = 123', 'Func()'], 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000565
566 # using global variable does not require type cast
567 lines =<< trim END
568 vim9script
569 def Func(arg: string = g:str)
570 echo arg
571 enddef
572 g:str = 'works'
573 Func()
574 END
575 CheckScriptSuccess(lines)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200576enddef
577
Bram Moolenaarcef12702021-01-04 14:09:43 +0100578def FuncWithComment( # comment
579 a: number, #comment
580 b: bool, # comment
581 c: string) #comment
582 assert_equal(4, a)
583 assert_equal(true, b)
584 assert_equal('yes', c)
585enddef
586
587def Test_func_with_comments()
588 FuncWithComment(4, true, 'yes')
589
590 var lines =<< trim END
591 def Func(# comment
592 arg: string)
593 enddef
594 END
595 CheckScriptFailure(lines, 'E125:', 1)
596
597 lines =<< trim END
598 def Func(
599 arg: string# comment
600 )
601 enddef
602 END
603 CheckScriptFailure(lines, 'E475:', 2)
604
605 lines =<< trim END
606 def Func(
607 arg: string
608 )# comment
609 enddef
610 END
611 CheckScriptFailure(lines, 'E488:', 3)
612enddef
613
Bram Moolenaar04b12692020-05-04 23:24:44 +0200614def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000615 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200616 return 'nested ' .. arg
617 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000618 NestedDef(':def')->assert_equal('nested :def')
619
620 func NestedFunc(arg)
621 return 'nested ' .. a:arg
622 endfunc
623 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200624
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200625 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
626 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
627
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200628 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
629 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200630
Bram Moolenaar54021752020-12-06 18:50:36 +0100631 var lines =<< trim END
632 def Outer()
633 def Inner()
634 # comment
635 enddef
636 def Inner()
637 enddef
638 enddef
639 END
640 CheckDefFailure(lines, 'E1073:')
641
642 lines =<< trim END
643 def Outer()
644 def Inner()
645 # comment
646 enddef
647 def! Inner()
648 enddef
649 enddef
650 END
651 CheckDefFailure(lines, 'E1117:')
652
Bram Moolenaardb8e5c22021-12-25 19:58:22 +0000653 lines =<< trim END
654 vim9script
655 def Outer()
656 def Inner()
657 g:result = 'ok'
658 enddef
659 Inner()
660 enddef
661 Outer()
662 Inner()
663 END
664 CheckScriptFailure(lines, 'E117: Unknown function: Inner')
665 assert_equal('ok', g:result)
666 unlet g:result
667
Bram Moolenaar54021752020-12-06 18:50:36 +0100668 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100669 lines =<< trim END
670 vim9script
671 var thecount = 0
672 if true
673 def Test(): number
674 def TheFunc(): number
675 thecount += 1
676 return thecount
677 enddef
678 return TheFunc()
679 enddef
680 endif
681 defcompile
682 assert_equal(1, Test())
683 assert_equal(2, Test())
684 END
685 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100686
687 # also works when "thecount" is inside the "if" block
688 lines =<< trim END
689 vim9script
690 if true
691 var thecount = 0
692 def Test(): number
693 def TheFunc(): number
694 thecount += 1
695 return thecount
696 enddef
697 return TheFunc()
698 enddef
699 endif
700 defcompile
701 assert_equal(1, Test())
702 assert_equal(2, Test())
703 END
704 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200705
706 lines =<< trim END
707 vim9script
708 def Outer()
709 def Inner()
710 echo 'hello'
711 enddef burp
712 enddef
713 defcompile
714 END
715 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200716enddef
717
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100718def Test_not_nested_function()
719 echo printf('%d',
720 function('len')('xxx'))
721enddef
722
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200723func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200724 call MyDefaultArgs()->assert_equal('string')
725 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200726 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200727endfunc
728
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200729def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200730 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200731 vim9script
732 def Outer()
733 def g:Inner(): string
734 return 'inner'
735 enddef
736 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200737 defcompile
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
744 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200745 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200746 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200747 END
748 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200749
750 lines =<< trim END
751 vim9script
752 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000753 func g:Inner()
754 return 'inner'
755 endfunc
756 enddef
757 defcompile
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 Outer()
765 g:Inner()->assert_equal('inner')
766 delfunc g:Inner
767 END
768 CheckScriptSuccess(lines)
769
770 lines =<< trim END
771 vim9script
772 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200773 def g:Inner(): string
774 return 'inner'
775 enddef
776 enddef
777 defcompile
778 Outer()
779 Outer()
780 END
781 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100782 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200783
784 lines =<< trim END
785 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100786 def Outer()
787 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100788 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100789 enddef
790 g:Inner()
791 enddef
792 Outer()
793 END
794 CheckScriptSuccess(lines)
795 delfunc g:Inner
796
797 lines =<< trim END
798 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200799 def Func()
800 echo 'script'
801 enddef
802 def Outer()
803 def Func()
804 echo 'inner'
805 enddef
806 enddef
807 defcompile
808 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000809 CheckScriptFailure(lines, "E1073:", 1)
810
811 lines =<< trim END
812 vim9script
813 def Func()
814 echo 'script'
815 enddef
816 def Func()
817 echo 'script'
818 enddef
819 END
820 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200821enddef
822
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100823def DefListAll()
824 def
825enddef
826
827def DefListOne()
828 def DefListOne
829enddef
830
831def DefListMatches()
832 def /DefList
833enddef
834
835def Test_nested_def_list()
836 var funcs = split(execute('call DefListAll()'), "\n")
837 assert_true(len(funcs) > 10)
838 assert_true(funcs->index('def DefListAll()') >= 0)
839
840 funcs = split(execute('call DefListOne()'), "\n")
841 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
842
843 funcs = split(execute('call DefListMatches()'), "\n")
844 assert_true(len(funcs) >= 3)
845 assert_true(funcs->index('def DefListAll()') >= 0)
846 assert_true(funcs->index('def DefListOne()') >= 0)
847 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100848
849 var lines =<< trim END
850 vim9script
851 def Func()
852 def +Func+
853 enddef
854 defcompile
855 END
856 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100857enddef
858
Bram Moolenaar333894b2020-08-01 18:53:07 +0200859def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200860 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200861 vim9script
862 def g:Func(): string
863 return 'global'
864 enddef
865 def Func(): string
866 return 'local'
867 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200868 g:Func()->assert_equal('global')
869 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100870 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200871 END
872 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200873
874 lines =<< trim END
875 vim9script
876 def g:Funcy()
877 echo 'funcy'
878 enddef
879 s:Funcy()
880 END
881 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200882enddef
883
Bram Moolenaar0f769812020-09-12 18:32:34 +0200884def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200885 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200886 vim9script
887 def g:Gfunc(): string
888 return 'global'
889 enddef
890 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200891 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200892 return Gfunc('testing')
893 enddef
894 g:Gfunc()->assert_equal('global')
895 AnotherFunc()->assert_equal(7)
896 delfunc g:Gfunc
897 END
898 CheckScriptSuccess(lines)
899
900 lines =<< trim END
901 vim9script
902 def g:Func(): string
903 return 'global'
904 enddef
905 def AnotherFunc()
906 g:Func = function('len')
907 enddef
908 AnotherFunc()
909 END
910 CheckScriptFailure(lines, 'E705:')
911 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200912
913 # global function is found without g: prefix
914 lines =<< trim END
915 vim9script
916 def g:Func(): string
917 return 'global'
918 enddef
919 def AnotherFunc(): string
920 return Func()
921 enddef
922 assert_equal('global', AnotherFunc())
923 delfunc g:Func
924 END
925 CheckScriptSuccess(lines)
926
927 lines =<< trim END
928 vim9script
929 def g:Func(): string
930 return 'global'
931 enddef
932 assert_equal('global', Func())
933 delfunc g:Func
934 END
935 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200936enddef
937
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200938func TakesOneArg(arg)
939 echo a:arg
940endfunc
941
942def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200943 CheckDefFailure(['TakesOneArg()'], 'E119:')
944 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
945 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
946 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200947
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200948 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200949 vim9script
950 def Func(s: string)
951 echo s
952 enddef
953 Func([])
954 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200955 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200956
Bram Moolenaar9a015112021-12-31 14:06:45 +0000957 # argument name declared earlier is found when declaring a function
Bram Moolenaarb185a402020-09-18 22:42:00 +0200958 lines =<< trim END
959 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100960 var name = 'piet'
961 def FuncOne(name: string)
962 echo nr
963 enddef
964 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100965 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100966
Bram Moolenaar9a015112021-12-31 14:06:45 +0000967 # argument name declared later is only found when compiling
968 lines =<< trim END
969 vim9script
970 def FuncOne(name: string)
971 echo nr
972 enddef
973 var name = 'piet'
974 END
975 CheckScriptSuccess(lines)
976 CheckScriptFailure(lines + ['defcompile'], 'E1168:')
977
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100978 lines =<< trim END
979 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200980 def FuncOne(nr: number)
981 echo nr
982 enddef
983 def FuncTwo()
984 FuncOne()
985 enddef
986 defcompile
987 END
988 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200989 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200990 try
991 source Xscript
992 catch
993 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
994 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
995 didCatch = true
996 endtry
997 assert_true(didCatch)
998
999 lines =<< trim END
1000 vim9script
1001 def FuncOne(nr: number)
1002 echo nr
1003 enddef
1004 def FuncTwo()
1005 FuncOne(1, 2)
1006 enddef
1007 defcompile
1008 END
1009 writefile(lines, 'Xscript')
1010 didCatch = false
1011 try
1012 source Xscript
1013 catch
1014 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
1015 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
1016 didCatch = true
1017 endtry
1018 assert_true(didCatch)
1019
1020 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001021enddef
1022
Bram Moolenaar50824712020-12-20 21:10:17 +01001023def Test_call_funcref_wrong_args()
1024 var head =<< trim END
1025 vim9script
1026 def Func3(a1: string, a2: number, a3: list<number>)
1027 echo a1 .. a2 .. a3[0]
1028 enddef
1029 def Testme()
1030 var funcMap: dict<func> = {func: Func3}
1031 END
1032 var tail =<< trim END
1033 enddef
1034 Testme()
1035 END
1036 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
1037
1038 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
1039 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001040
1041 var lines =<< trim END
1042 vim9script
1043 var Ref: func(number): any
1044 Ref = (j) => !j
1045 echo Ref(false)
1046 END
1047 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
1048
1049 lines =<< trim END
1050 vim9script
1051 var Ref: func(number): any
1052 Ref = (j) => !j
1053 call Ref(false)
1054 END
1055 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001056enddef
1057
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001058def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001059 var lines =<< trim END
1060 var Callback = (..._) => 'anything'
1061 assert_equal('anything', Callback())
1062 assert_equal('anything', Callback(1))
1063 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001064
1065 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001066 END
1067 CheckDefAndScriptSuccess(lines)
1068
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001069 CheckDefFailure(['echo ((i) => 0)()'],
1070 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001071
Bram Moolenaar2a389082021-04-09 20:24:31 +02001072 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001073 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001074 echo Ref(1, 'x')
1075 END
1076 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001077
1078 lines =<< trim END
1079 var Ref: func(job, string, number)
1080 Ref = (x, y) => 0
1081 END
1082 CheckDefAndScriptFailure(lines, 'E1012:')
1083
1084 lines =<< trim END
1085 var Ref: func(job, string)
1086 Ref = (x, y, z) => 0
1087 END
1088 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001089
1090 lines =<< trim END
1091 var one = 1
1092 var l = [1, 2, 3]
1093 echo map(l, (one) => one)
1094 END
1095 CheckDefFailure(lines, 'E1167:')
1096 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1097
1098 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001099 var Ref: func(any, ?any): bool
1100 Ref = (_, y = 1) => false
1101 END
1102 CheckDefAndScriptFailure(lines, 'E1172:')
1103
1104 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001105 var a = 0
1106 var b = (a == 0 ? 1 : 2)
1107 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001108 var txt = 'a'
1109 b = (txt =~ 'x' ? 1 : 2)
1110 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001111 END
1112 CheckDefAndScriptSuccess(lines)
1113
1114 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001115 def ShadowLocal()
1116 var one = 1
1117 var l = [1, 2, 3]
1118 echo map(l, (one) => one)
1119 enddef
1120 END
1121 CheckDefFailure(lines, 'E1167:')
1122
1123 lines =<< trim END
1124 def Shadowarg(one: number)
1125 var l = [1, 2, 3]
1126 echo map(l, (one) => one)
1127 enddef
1128 END
1129 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001130
1131 lines =<< trim END
1132 echo ((a) => a)('aa', 'bb')
1133 END
1134 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001135
1136 lines =<< trim END
1137 echo 'aa'->((a) => a)('bb')
1138 END
1139 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1140 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001141enddef
1142
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001143def Test_lambda_line_nr()
1144 var lines =<< trim END
1145 vim9script
1146 # comment
1147 # comment
1148 var id = timer_start(1'000, (_) => 0)
1149 var out = execute('verbose ' .. timer_info(id)[0].callback
1150 ->string()
1151 ->substitute("('\\|')", ' ', 'g'))
1152 assert_match('Last set from .* line 4', out)
1153 END
1154 CheckScriptSuccess(lines)
1155enddef
1156
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001157def FilterWithCond(x: string, Cond: func(string): bool): bool
1158 return Cond(x)
1159enddef
1160
Bram Moolenaar0346b792021-01-31 22:18:29 +01001161def Test_lambda_return_type()
1162 var lines =<< trim END
1163 var Ref = (): => 123
1164 END
1165 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001166
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001167 # no space before the return type
1168 lines =<< trim END
1169 var Ref = (x):number => x + 1
1170 END
1171 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1172
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001173 # this works
1174 for x in ['foo', 'boo']
1175 echo FilterWithCond(x, (v) => v =~ '^b')
1176 endfor
1177
1178 # this fails
1179 lines =<< trim END
1180 echo FilterWithCond('foo', (v) => v .. '^b')
1181 END
1182 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001183
1184 lines =<< trim END
1185 var Lambda1 = (x) => {
1186 return x
1187 }
1188 assert_equal('asdf', Lambda1('asdf'))
1189 var Lambda2 = (x): string => {
1190 return x
1191 }
1192 assert_equal('foo', Lambda2('foo'))
1193 END
1194 CheckDefAndScriptSuccess(lines)
1195
1196 lines =<< trim END
1197 var Lambda = (x): string => {
1198 return x
1199 }
1200 echo Lambda(['foo'])
1201 END
1202 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001203enddef
1204
Bram Moolenaar709664c2020-12-12 14:33:41 +01001205def Test_lambda_uses_assigned_var()
1206 CheckDefSuccess([
1207 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001208 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001209enddef
1210
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001211def Test_pass_legacy_lambda_to_def_func()
1212 var lines =<< trim END
1213 vim9script
1214 func Foo()
1215 eval s:Bar({x -> 0})
1216 endfunc
1217 def Bar(y: any)
1218 enddef
1219 Foo()
1220 END
1221 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001222
1223 lines =<< trim END
1224 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001225 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001226 enddef
1227 legacy call g:TestFunc({-> 0})
1228 delfunc g:TestFunc
1229
1230 def g:TestFunc(f: func(number))
1231 enddef
1232 legacy call g:TestFunc({nr -> 0})
1233 delfunc g:TestFunc
1234 END
1235 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001236enddef
1237
Bram Moolenaar844fb642021-10-23 13:32:30 +01001238def Test_lambda_in_reduce_line_break()
1239 # this was using freed memory
1240 var lines =<< trim END
1241 vim9script
1242 const result: dict<number> =
1243 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1244 ->reduce((acc, val) => {
1245 if has_key(acc, val)
1246 acc[val] += 1
1247 return acc
1248 else
1249 acc[val] = 1
1250 return acc
1251 endif
1252 }, {})
1253 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1254 END
1255 CheckScriptSuccess(lines)
1256enddef
1257
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001258def Test_set_opfunc_to_lambda()
1259 var lines =<< trim END
1260 vim9script
1261 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1262 def CountSpaces(type = ''): string
1263 if type == ''
1264 &operatorfunc = (t) => CountSpaces(t)
1265 return 'g@'
1266 endif
1267 normal! '[V']y
1268 g:result = getreg('"')->count(' ')
1269 return ''
1270 enddef
1271 new
1272 'a b c d e'->setline(1)
1273 feedkeys("\<F4>", 'x')
1274 assert_equal(4, g:result)
1275 bwipe!
1276 END
1277 CheckScriptSuccess(lines)
1278enddef
1279
Bram Moolenaaref082e12021-12-12 21:02:03 +00001280def Test_set_opfunc_to_global_function()
1281 var lines =<< trim END
1282 vim9script
1283 def g:CountSpaces(type = ''): string
1284 normal! '[V']y
1285 g:result = getreg('"')->count(' ')
1286 return ''
1287 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001288 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001289 &operatorfunc = g:CountSpaces
1290 new
1291 'a b c d e'->setline(1)
1292 feedkeys("g@_", 'x')
1293 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001294
1295 &operatorfunc = ''
1296 g:result = 0
1297 # global function works in :def function
1298 def Func()
1299 &operatorfunc = g:CountSpaces
1300 enddef
1301 Func()
1302 feedkeys("g@_", 'x')
1303 assert_equal(4, g:result)
1304
Bram Moolenaaref082e12021-12-12 21:02:03 +00001305 bwipe!
1306 END
1307 CheckScriptSuccess(lines)
1308 &operatorfunc = ''
1309enddef
1310
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001311def Test_use_script_func_name_with_prefix()
1312 var lines =<< trim END
1313 vim9script
1314 func s:Getit()
1315 return 'it'
1316 endfunc
1317 var Fn = s:Getit
1318 assert_equal('it', Fn())
1319 END
1320 CheckScriptSuccess(lines)
1321enddef
1322
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001323def Test_lambda_type_allocated()
1324 # Check that unreferencing a partial using a lambda can use the variable type
1325 # after the lambda has been freed and does not leak memory.
1326 var lines =<< trim END
1327 vim9script
1328
1329 func MyomniFunc1(val, findstart, base)
1330 return a:findstart ? 0 : []
1331 endfunc
1332
1333 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1334 &omnifunc = Lambda
1335 Lambda = (a, b) => MyomniFunc1(20, a, b)
1336 &omnifunc = string(Lambda)
1337 Lambda = (a, b) => strlen(a)
1338 END
1339 CheckScriptSuccess(lines)
1340enddef
1341
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001342" Default arg and varargs
1343def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001344 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001345 for s in rest
1346 res ..= ',' .. s
1347 endfor
1348 return res
1349enddef
1350
1351def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001352 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001353 MyDefVarargs('one')->assert_equal('one,foo')
1354 MyDefVarargs('one', 'two')->assert_equal('one,two')
1355 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001356 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001357 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001358 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001359 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001360
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001361 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001362 vim9script
1363 def Func(...l: list<string>)
1364 echo l
1365 enddef
1366 Func('a', 'b', 'c')
1367 END
1368 CheckScriptSuccess(lines)
1369
1370 lines =<< trim END
1371 vim9script
1372 def Func(...l: list<string>)
1373 echo l
1374 enddef
1375 Func()
1376 END
1377 CheckScriptSuccess(lines)
1378
1379 lines =<< trim END
1380 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001381 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001382 echo l
1383 enddef
1384 Func(0)
1385 END
1386 CheckScriptSuccess(lines)
1387
1388 lines =<< trim END
1389 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001390 def Func(...l: any)
1391 echo l
1392 enddef
1393 Func(0)
1394 END
1395 CheckScriptFailure(lines, 'E1180:', 2)
1396
1397 lines =<< trim END
1398 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001399 def Func(..._l: list<string>)
1400 echo _l
1401 enddef
1402 Func('a', 'b', 'c')
1403 END
1404 CheckScriptSuccess(lines)
1405
1406 lines =<< trim END
1407 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001408 def Func(...l: list<string>)
1409 echo l
1410 enddef
1411 Func(1, 2, 3)
1412 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001413 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001414
1415 lines =<< trim END
1416 vim9script
1417 def Func(...l: list<string>)
1418 echo l
1419 enddef
1420 Func('a', 9)
1421 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001422 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001423
1424 lines =<< trim END
1425 vim9script
1426 def Func(...l: list<string>)
1427 echo l
1428 enddef
1429 Func(1, 'a')
1430 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001431 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001432
1433 lines =<< trim END
1434 vim9script
1435 def Func( # some comment
1436 ...l = []
1437 )
1438 echo l
1439 enddef
1440 END
1441 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001442
1443 lines =<< trim END
1444 vim9script
1445 def DoIt()
1446 g:Later('')
1447 enddef
1448 defcompile
1449 def g:Later(...l: list<number>)
1450 enddef
1451 DoIt()
1452 END
1453 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001454enddef
1455
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001456let s:value = ''
1457
1458def FuncOneDefArg(opt = 'text')
1459 s:value = opt
1460enddef
1461
1462def FuncTwoDefArg(nr = 123, opt = 'text'): string
1463 return nr .. opt
1464enddef
1465
1466def FuncVarargs(...arg: list<string>): string
1467 return join(arg, ',')
1468enddef
1469
1470def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001471 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001472 RefDefArg = FuncOneDefArg
1473 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001474 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001475 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001476 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001477
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001478 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001479 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001480 RefDef2Arg()->assert_equal('123text')
1481 RefDef2Arg(99)->assert_equal('99text')
1482 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001483
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001484 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1485 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001486
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001487 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001488 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001489 RefVarargs()->assert_equal('')
1490 RefVarargs('one')->assert_equal('one')
1491 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001492
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001493 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1494 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001495enddef
1496
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001497" Only varargs
1498def MyVarargsOnly(...args: list<string>): string
1499 return join(args, ',')
1500enddef
1501
1502def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001503 MyVarargsOnly()->assert_equal('')
1504 MyVarargsOnly('one')->assert_equal('one')
1505 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001506 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1507 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001508enddef
1509
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001510def Test_using_var_as_arg()
Bram Moolenaard2939812021-12-30 17:09:05 +00001511 var lines =<< trim END
1512 def Func(x: number)
1513 var x = 234
1514 enddef
1515 END
1516 CheckDefFailure(lines, 'E1006:')
1517
1518 lines =<< trim END
1519 def Func(Ref: number)
1520 def Ref()
1521 enddef
1522 enddef
1523 END
1524 CheckDefFailure(lines, 'E1073:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001525enddef
1526
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001527def DictArg(arg: dict<string>)
1528 arg['key'] = 'value'
1529enddef
1530
1531def ListArg(arg: list<string>)
1532 arg[0] = 'value'
1533enddef
1534
1535def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001536 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001537 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001538 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001539 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001540 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001541 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001542 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001543
Bram Moolenaard2c61702020-09-06 15:58:36 +02001544 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001545 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001546enddef
1547
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001548" These argument names are reserved in legacy functions.
1549def WithReservedNames(firstline: string, lastline: string): string
1550 return firstline .. lastline
1551enddef
1552
1553def Test_argument_names()
1554 assert_equal('OK', WithReservedNames('O', 'K'))
1555enddef
1556
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001557def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001558 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001559 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001560enddef
1561
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001562func DefinedLater(arg)
1563 return a:arg
1564endfunc
1565
1566def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001567 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001568 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001569 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001570
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001571 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001572 vim9script
1573 def RetNumber(): number
1574 return 123
1575 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001576 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001577 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001578 END
1579 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001580
1581 lines =<< trim END
1582 vim9script
1583 def RetNumber(): number
1584 return 123
1585 enddef
1586 def Bar(F: func: number): number
1587 return F()
1588 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001589 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001590 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001591 END
1592 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001593
1594 lines =<< trim END
1595 vim9script
1596 def UseNumber(nr: number)
1597 echo nr
1598 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001599 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001600 Funcref(123)
1601 END
1602 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001603
1604 lines =<< trim END
1605 vim9script
1606 def UseNumber(nr: number)
1607 echo nr
1608 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001609 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001610 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001611 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001612
1613 lines =<< trim END
1614 vim9script
1615 def EchoNr(nr = 34)
1616 g:echo = nr
1617 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001618 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001619 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001620 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001621 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001622 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001623 END
1624 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001625
1626 lines =<< trim END
1627 vim9script
1628 def EchoList(...l: list<number>)
1629 g:echo = l
1630 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001631 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001632 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001633 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001634 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001635 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001636 END
1637 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001638
1639 lines =<< trim END
1640 vim9script
1641 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1642 g:optarg = opt
1643 g:listarg = l
1644 return nr
1645 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001646 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001647 Funcref(10)->assert_equal(10)
1648 g:optarg->assert_equal(12)
1649 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001650
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001651 Funcref(11, 22)->assert_equal(11)
1652 g:optarg->assert_equal(22)
1653 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001654
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001655 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1656 g:optarg->assert_equal(18)
1657 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001658 END
1659 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001660enddef
1661
1662let SomeFunc = function('len')
1663let NotAFunc = 'text'
1664
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001665def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001666 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001667 var Ref1: func(bool): string
1668 var Ref2: func(bool): number
1669 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001670 Ref3 = g:cond ? Ref1 : Ref2
1671
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001672 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001673 var Refa1: func(bool): number
1674 var Refa2: func(bool, number): number
1675 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001676 Refa3 = g:cond ? Refa1 : Refa2
1677
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001678 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001679 var Refb1: func(bool, string): number
1680 var Refb2: func(string, number): number
1681 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001682 Refb3 = g:cond ? Refb1 : Refb2
1683enddef
1684
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001685def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001686 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001687enddef
1688
1689def DefinedEvenLater(arg: string): string
1690 return arg
1691enddef
1692
1693def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001694 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001695 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001696enddef
1697
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001698def Test_nested_function_with_nextcmd()
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001699 var lines =<< trim END
1700 vim9script
1701 # Define an outer function
1702 def FirstFunction()
1703 # Define an inner function
1704 def SecondFunction()
1705 # the function has a body, a double free is detected.
1706 AAAAA
1707
1708 # enddef followed by | or } followed by # one or more characters
1709 enddef|BBBB
1710 enddef
1711
1712 # Compile all functions
1713 defcompile
1714 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001715 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001716enddef
1717
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001718def Test_nested_function_with_args_split()
1719 var lines =<< trim END
1720 vim9script
1721 def FirstFunction()
1722 def SecondFunction(
1723 )
1724 # had a double free if the right parenthesis of the nested function is
1725 # on the next line
1726
1727 enddef|BBBB
1728 enddef
1729 # Compile all functions
1730 defcompile
1731 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001732 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
1733
1734 lines =<< trim END
1735 vim9script
1736 def FirstFunction()
1737 func SecondFunction()
1738 endfunc|BBBB
1739 enddef
1740 defcompile
1741 END
1742 CheckScriptFailure(lines, 'E1173: Text found after endfunction: BBBB')
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001743enddef
1744
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001745def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001746 CheckScriptFailure([
1747 'def Func(): number',
1748 'return "a"',
1749 'enddef',
1750 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001751 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001752 CheckScriptFailure([
1753 'def Func(): string',
1754 'return 1',
1755 'enddef',
1756 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001757 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001758 CheckScriptFailure([
1759 'def Func(): void',
1760 'return "a"',
1761 'enddef',
1762 'defcompile'],
1763 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001764 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001765 CheckScriptFailure([
1766 'def Func()',
1767 'return "a"',
1768 'enddef',
1769 'defcompile'],
1770 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001771 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001772
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001773 CheckScriptFailure([
1774 'def Func(): number',
1775 'return',
1776 'enddef',
1777 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001778 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001779
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001780 CheckScriptFailure([
1781 'def Func():number',
1782 'return 123',
1783 'enddef',
1784 'defcompile'], 'E1069:')
1785 delfunc! g:Func
1786
1787 CheckScriptFailure([
1788 'def Func() :number',
1789 'return 123',
1790 'enddef',
1791 'defcompile'], 'E1059:')
1792 delfunc! g:Func
1793
1794 CheckScriptFailure([
1795 'def Func() : number',
1796 'return 123',
1797 'enddef',
1798 'defcompile'], 'E1059:')
1799 delfunc! g:Func
1800
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001801 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001802 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001803 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001804 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001805 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001806 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001807
1808 CheckScriptFailure([
1809 'vim9script',
1810 'def FuncB()',
1811 ' return 123',
1812 'enddef',
1813 'def FuncA()',
1814 ' FuncB()',
1815 'enddef',
1816 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001817enddef
1818
1819def Test_arg_type_wrong()
1820 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001821 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001822 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001823 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001824 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1825 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001826enddef
1827
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001828def Test_white_space_before_comma()
1829 var lines =<< trim END
1830 vim9script
1831 def Func(a: number , b: number)
1832 enddef
1833 END
1834 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001835 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001836enddef
1837
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001838def Test_white_space_after_comma()
1839 var lines =<< trim END
1840 vim9script
1841 def Func(a: number,b: number)
1842 enddef
1843 END
1844 CheckScriptFailure(lines, 'E1069:')
1845
1846 # OK in legacy function
1847 lines =<< trim END
1848 vim9script
1849 func Func(a,b)
1850 endfunc
1851 END
1852 CheckScriptSuccess(lines)
1853enddef
1854
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001855def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001856 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001857 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001858 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001859 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001860 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001861 enddef
1862 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001863 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001864
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001865 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001867 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001868
Bram Moolenaar67979662020-06-20 22:50:47 +02001869 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001871 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001872
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001873 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001874 def ListFunc(arg: list<number>)
1875 listvar = arg
1876 enddef
1877 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001878 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001879
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001880 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001881 def DictFunc(arg: dict<number>)
1882 dictvar = arg
1883 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001884 {a: 1, b: 2}->DictFunc()
1885 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001886 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001887 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001888 enddef
1889 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001890 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001891
Bram Moolenaare0de1712020-12-02 17:36:54 +01001892 {a: 3, b: 4}->DictFunc()
1893 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001894
1895 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001896 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001898 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001899
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001900 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001901 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001902 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001903 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001904 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001905 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001906
1907 def UseString()
1908 'xyork'->MyFunc()
1909 enddef
1910 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001911 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001912
Bram Moolenaar10409562020-07-29 20:00:38 +02001913 def UseString2()
1914 "knife"->MyFunc()
1915 enddef
1916 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001917 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001918
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001919 # prepending a colon makes it a mark
1920 new
1921 setline(1, ['aaa', 'bbb', 'ccc'])
1922 normal! 3Gmt1G
1923 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001924 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001925 bwipe!
1926
Bram Moolenaare6b53242020-07-01 17:28:33 +02001927 MyFunc(
1928 'continued'
1929 )
1930 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001931 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001932 )
1933
1934 call MyFunc(
1935 'more'
1936 ..
1937 'lines'
1938 )
1939 assert_equal(
1940 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001941 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001942 END
1943 writefile(lines, 'Xcall.vim')
1944 source Xcall.vim
1945 delete('Xcall.vim')
1946enddef
1947
1948def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001949 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001950 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001951 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001952 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001953 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001954 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001955 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001956 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001957 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958enddef
1959
Bram Moolenaar65b95452020-07-19 14:03:09 +02001960def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001961 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001962 vim9script
1963 def MyFunc(arg: string)
1964 echo arg
1965 enddef
1966 MyFunc(1234)
1967 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001968 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001969enddef
1970
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001971def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001972 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001973 vim9script
1974 const var = ''
1975 def MyFunc(arg: string)
1976 var = 'asdf'
1977 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001978 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001979 END
1980 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001981 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001982 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001983
1984 lines =<< trim END
1985 const g:Aconst = 77
1986 def Change()
1987 # comment
1988 g:Aconst = 99
1989 enddef
1990 call Change()
1991 unlet g:Aconst
1992 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001993 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001994enddef
1995
1996" Test that inside :function a Python function can be defined, :def is not
1997" recognized.
1998func Test_function_python()
1999 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02002000 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002001 execute py "<< EOF"
2002def do_something():
2003 return 1
2004EOF
2005endfunc
2006
2007def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002008 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002009 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002010 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002011 echo 'hello'
2012 enddef
2013
2014 def CallGoneSoon()
2015 GoneSoon()
2016 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02002017 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002018
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002019 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020 CallGoneSoon()
2021 END
2022 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002023 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
2024 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002025
2026 delete('XToDelFunc')
2027enddef
2028
Bram Moolenaar7509ad82021-12-14 18:14:37 +00002029func Test_free_dict_while_in_funcstack()
2030 " relies on the sleep command
2031 CheckUnix
2032 call Run_Test_free_dict_while_in_funcstack()
2033endfunc
2034
2035def Run_Test_free_dict_while_in_funcstack()
2036
2037 # this was freeing the TermRun() default argument dictionary while it was
2038 # still referenced in a funcstack_T
2039 var lines =<< trim END
2040 vim9script
2041
2042 &updatetime = 400
2043 def TermRun(_ = {})
2044 def Post()
2045 enddef
2046 def Exec()
2047 term_start('sleep 1', {
2048 term_finish: 'close',
2049 exit_cb: (_, _) => Post(),
2050 })
2051 enddef
2052 Exec()
2053 enddef
2054 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
2055 timer_start(100, (_) => feedkeys("\<F4>"))
2056 timer_start(1000, (_) => feedkeys("\<F4>"))
2057 sleep 1500m
2058 END
2059 CheckScriptSuccess(lines)
2060 nunmap <F4>
2061 set updatetime&
2062enddef
2063
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002064def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002065 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002066 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002067 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002068 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002069 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002070 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002071 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002072 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002073 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002074
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002075 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002076 g:Func1()->assert_equal('Func1')
2077 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002078
2079 delfunc! Func0
2080 delfunc! Func1
2081 delfunc! Func2
2082enddef
2083
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002084def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002085 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002086 vim9script
2087 func Func(arg)
2088 echo a:arg
2089 endfunc
2090 Func('text')
2091 END
2092 writefile(lines, 'XVim9Func')
2093 so XVim9Func
2094
2095 delete('XVim9Func')
2096enddef
2097
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002098let s:funcResult = 0
2099
2100def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002101 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002102enddef
2103
2104def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002105 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002106 return 1234
2107enddef
2108
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002109def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002110 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002111 return 'text'
2112enddef
2113
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002114def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002115 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002116enddef
2117
2118def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002119 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002120 return arg
2121enddef
2122
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002123def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002124 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002125enddef
2126
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002127def FuncOneArgRetString(arg: string): string
2128 return arg
2129enddef
2130
Bram Moolenaar89228602020-04-05 22:14:54 +02002131def FuncOneArgRetAny(arg: any): any
2132 return arg
2133enddef
2134
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002135def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002136 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002137 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002138 Ref1 = FuncNoArgNoRet
2139 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002140 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002141
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002142 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002143 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002144 Ref2 = FuncNoArgNoRet
2145 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002146 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002147
Bram Moolenaar53900992020-08-22 19:02:02 +02002148 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002149 Ref2 = FuncOneArgNoRet
2150 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002151 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002152
Bram Moolenaar53900992020-08-22 19:02:02 +02002153 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002154 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002155 Ref2()->assert_equal(1234)
2156 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002157
Bram Moolenaar53900992020-08-22 19:02:02 +02002158 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002159 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002160 Ref2(13)->assert_equal(13)
2161 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002162enddef
2163
Bram Moolenaar9978d472020-07-05 16:01:56 +02002164def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002165 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002166 for n in repeat([1], 3)
2167 res += n
2168 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002169 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002170
2171 res = 0
2172 for n in add([1, 2], 3)
2173 res += n
2174 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002175 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002176enddef
2177
Bram Moolenaar846178a2020-07-05 17:04:13 +02002178def Test_argv_return_type()
2179 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002180 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002181 for name in argv()
2182 res ..= name
2183 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002184 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002185enddef
2186
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002187def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002188 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002189 RefVoid = FuncNoArgNoRet
2190 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002191 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2192 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002193
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002194 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002195 RefAny = FuncNoArgRetNumber
2196 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002197 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2198 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002199
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002200 var RefAnyNoArgs: func: any = RefAny
2201
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002202 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002203 RefNr = FuncNoArgRetNumber
2204 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002205 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2206 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002207
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002208 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002209 RefStr = FuncNoArgRetString
2210 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002211 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2212 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002213enddef
2214
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002215def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002216 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002217
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002218 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2219 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2220 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2221 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2222 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2223 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 +02002224
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002225 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2226 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2227 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:')
2228 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002229enddef
2230
Bram Moolenaar89228602020-04-05 22:14:54 +02002231def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002232 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002233 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002234 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002235
2236 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002237 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002238
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002239 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002240 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002241 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002242
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002243 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002244enddef
2245
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002246def Test_func_common_type()
2247 def FuncOne(n: number): number
2248 return n
2249 enddef
2250 def FuncTwo(s: string): number
2251 return len(s)
2252 enddef
2253 def FuncThree(n: number, s: string): number
2254 return n + len(s)
2255 enddef
2256 var list = [FuncOne, FuncTwo, FuncThree]
2257 assert_equal(8, list[0](8))
2258 assert_equal(4, list[1]('word'))
2259 assert_equal(7, list[2](3, 'word'))
2260enddef
2261
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002262def MultiLine(
2263 arg1: string,
2264 arg2 = 1234,
2265 ...rest: list<string>
2266 ): string
2267 return arg1 .. arg2 .. join(rest, '-')
2268enddef
2269
Bram Moolenaar2c330432020-04-13 14:41:35 +02002270def MultiLineComment(
2271 arg1: string, # comment
2272 arg2 = 1234, # comment
2273 ...rest: list<string> # comment
2274 ): string # comment
2275 return arg1 .. arg2 .. join(rest, '-')
2276enddef
2277
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002278def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002279 MultiLine('text')->assert_equal('text1234')
2280 MultiLine('text', 777)->assert_equal('text777')
2281 MultiLine('text', 777, 'one')->assert_equal('text777one')
2282 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002283enddef
2284
Bram Moolenaar23e03252020-04-12 22:22:31 +02002285func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002286 call MultiLine('text')->assert_equal('text1234')
2287 call MultiLine('text', 777)->assert_equal('text777')
2288 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2289 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002290endfunc
2291
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002292
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002293" When using CheckScriptFailure() for the below test, E1010 is generated instead
2294" of E1056.
2295func Test_E1056_1059()
2296 let caught_1056 = 0
2297 try
2298 def F():
2299 return 1
2300 enddef
2301 catch /E1056:/
2302 let caught_1056 = 1
2303 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002304 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002305
2306 let caught_1059 = 0
2307 try
2308 def F5(items : list)
2309 echo 'a'
2310 enddef
2311 catch /E1059:/
2312 let caught_1059 = 1
2313 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002314 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002315endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002316
Bram Moolenaar015f4262020-05-05 21:25:22 +02002317func DelMe()
2318 echo 'DelMe'
2319endfunc
2320
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002321def Test_error_reporting()
2322 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002323 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002324 " comment
2325 def Func()
2326 # comment
2327 # comment
2328 invalid
2329 enddef
2330 defcompile
2331 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002332 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002333 try
2334 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002335 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002336 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002337 v:exception->assert_match('Invalid command: invalid')
2338 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002339 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002340 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002341
2342 # comment lines after the start of the function
2343 lines =<< trim END
2344 " comment
2345 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002346 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002347 # comment
2348 # comment
2349 invalid
2350 enddef
2351 defcompile
2352 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002353 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002354 try
2355 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002356 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002357 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002358 v:exception->assert_match('Invalid command: invalid')
2359 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002360 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002361 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002362
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002363 lines =<< trim END
2364 vim9script
2365 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002366 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002367 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002368 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002369 enddef
2370 defcompile
2371 Func()
2372 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002373 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002374 try
2375 source Xdef
2376 assert_report('should have failed')
2377 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002378 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002379 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002380 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002381
Bram Moolenaar08052222020-09-14 17:04:31 +02002382 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002383enddef
2384
Bram Moolenaar015f4262020-05-05 21:25:22 +02002385def Test_deleted_function()
2386 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002387 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002388 'delfunc g:DelMe',
2389 'echo RefMe()'], 'E117:')
2390enddef
2391
2392def Test_unknown_function()
2393 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002394 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002395 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002396enddef
2397
Bram Moolenaar328eac22021-01-07 19:23:08 +01002398def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002399 return Ref('more')
2400enddef
2401
2402def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002403 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002404 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002405enddef
2406
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002407def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002408 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002409 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002410enddef
2411
2412def Test_closure_ref_after_return()
2413 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002414 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002415 unlet g:Ref
2416enddef
2417
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002418def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002419 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002420 g:Extend = (s) => local->add(s)
2421 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002422enddef
2423
2424def Test_closure_two_refs()
2425 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002426 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002427 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002428 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002429 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002430 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002431
2432 unlet g:Extend
2433 unlet g:Read
2434enddef
2435
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002436def ReadRef(Ref: func(): list<string>): string
2437 return join(Ref(), ' ')
2438enddef
2439
Bram Moolenaar5e654232020-09-16 15:22:00 +02002440def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002441 Ref(add)
2442enddef
2443
2444def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002445 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002446 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002447 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002448 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002449 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002450 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002451
2452 unlet g:Extend
2453 unlet g:Read
2454enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002455
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002456def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002457 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002458 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002459enddef
2460
2461def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002462 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002463 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002464enddef
2465
2466def Test_closure_using_argument()
2467 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002468 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002469
2470 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002471 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002472
2473 unlet g:UseArg
2474 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002475
2476 var lines =<< trim END
2477 vim9script
2478 def Test(Fun: func(number): number): list<number>
2479 return map([1, 2, 3], (_, i) => Fun(i))
2480 enddef
2481 def Inc(nr: number): number
2482 return nr + 2
2483 enddef
2484 assert_equal([3, 4, 5], Test(Inc))
2485 END
2486 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002487enddef
2488
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002489def MakeGetAndAppendRefs()
2490 var local = 'a'
2491
2492 def Append(arg: string)
2493 local ..= arg
2494 enddef
2495 g:Append = Append
2496
2497 def Get(): string
2498 return local
2499 enddef
2500 g:Get = Get
2501enddef
2502
2503def Test_closure_append_get()
2504 MakeGetAndAppendRefs()
2505 g:Get()->assert_equal('a')
2506 g:Append('-b')
2507 g:Get()->assert_equal('a-b')
2508 g:Append('-c')
2509 g:Get()->assert_equal('a-b-c')
2510
2511 unlet g:Append
2512 unlet g:Get
2513enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002514
Bram Moolenaar04b12692020-05-04 23:24:44 +02002515def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002516 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002517 def Closure(arg: string): string
2518 return local .. arg
2519 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002520 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002521enddef
2522
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002523func GetResult(Ref)
2524 return a:Ref('some')
2525endfunc
2526
2527def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002528 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002529 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002530 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002531enddef
2532
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002533def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002534 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002535 vim9script
2536 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002537 var name = 0
2538 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002539 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002540 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002541 enddef
2542 Func()
2543 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002544 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002545enddef
2546
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002547def Test_nested_closure_used()
2548 var lines =<< trim END
2549 vim9script
2550 def Func()
2551 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002552 var Closure = () => x
2553 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002554 enddef
2555 Func()
2556 assert_equal('hello', g:Myclosure())
2557 END
2558 CheckScriptSuccess(lines)
2559enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002560
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002561def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002562 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002563 vim9script
2564 def FuncA()
2565 FuncB(0)
2566 enddef
2567 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002568 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002569 enddef
2570 FuncA()
2571 END
2572 CheckScriptFailure(lines, 'E1012:')
2573enddef
2574
Bram Moolenaarf112f302020-12-20 17:47:52 +01002575def Test_global_closure()
2576 var lines =<< trim END
2577 vim9script
2578 def ReverseEveryNLines(n: number, line1: number, line2: number)
2579 var mods = 'sil keepj keepp lockm '
2580 var range = ':' .. line1 .. ',' .. line2
2581 def g:Offset(): number
2582 var offset = (line('.') - line1 + 1) % n
2583 return offset != 0 ? offset : n
2584 enddef
2585 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2586 enddef
2587
2588 new
2589 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2590 ReverseEveryNLines(3, 1, 9)
2591 END
2592 CheckScriptSuccess(lines)
2593 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2594 assert_equal(expected, getline(1, 9))
2595 bwipe!
2596enddef
2597
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002598def Test_global_closure_called_directly()
2599 var lines =<< trim END
2600 vim9script
2601 def Outer()
2602 var x = 1
2603 def g:Inner()
2604 var y = x
2605 x += 1
2606 assert_equal(1, y)
2607 enddef
2608 g:Inner()
2609 assert_equal(2, x)
2610 enddef
2611 Outer()
2612 END
2613 CheckScriptSuccess(lines)
2614 delfunc g:Inner
2615enddef
2616
Bram Moolenaar69c76172021-12-02 16:38:52 +00002617def Test_closure_called_from_legacy()
2618 var lines =<< trim END
2619 vim9script
2620 def Func()
2621 var outer = 'foo'
2622 var F = () => {
2623 outer = 'bar'
2624 }
2625 execute printf('call %s()', string(F))
2626 enddef
2627 Func()
2628 END
2629 CheckScriptFailure(lines, 'E1248')
2630enddef
2631
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002632def Test_failure_in_called_function()
2633 # this was using the frame index as the return value
2634 var lines =<< trim END
2635 vim9script
2636 au TerminalWinOpen * eval [][0]
2637 def PopupTerm(a: any)
2638 # make sure typvals on stack are string
2639 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2640 FireEvent()
2641 enddef
2642 def FireEvent()
2643 do TerminalWinOpen
2644 enddef
2645 # use try/catch to make eval fail
2646 try
2647 call PopupTerm(0)
2648 catch
2649 endtry
2650 au! TerminalWinOpen
2651 END
2652 CheckScriptSuccess(lines)
2653enddef
2654
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002655def Test_nested_lambda()
2656 var lines =<< trim END
2657 vim9script
2658 def Func()
2659 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002660 var Lambda1 = () => 7
2661 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002662 var res = Lambda2()
2663 assert_equal([7, 4], res)
2664 enddef
2665 Func()
2666 END
2667 CheckScriptSuccess(lines)
2668enddef
2669
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002670def Test_double_nested_lambda()
2671 var lines =<< trim END
2672 vim9script
2673 def F(head: string): func(string): func(string): string
2674 return (sep: string): func(string): string => ((tail: string): string => {
2675 return head .. sep .. tail
2676 })
2677 enddef
2678 assert_equal('hello-there', F('hello')('-')('there'))
2679 END
2680 CheckScriptSuccess(lines)
2681enddef
2682
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002683def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002684 var lines =<< trim END
2685 vim9script
2686 def F(text: string): func(string): func(string): string
2687 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002688 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002689 })
2690 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002691 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002692 END
2693 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002694
2695 lines =<< trim END
2696 vim9script
2697 echo range(4)->mapnew((_, v) => {
2698 return range(v) ->mapnew((_, s) => {
2699 return string(s)
2700 })
2701 })
2702 END
2703 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002704
2705 lines =<< trim END
2706 vim9script
2707
2708 def s:func()
2709 range(10)
2710 ->mapnew((_, _) => ({
2711 key: range(10)->mapnew((_, _) => {
2712 return ' '
2713 }),
2714 }))
2715 enddef
2716
2717 defcomp
2718 END
2719 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002720enddef
2721
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002722def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002723 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002724 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002725enddef
2726
2727def Test_lambda_arg_shadows_func()
2728 assert_equal([42], Shadowed())
2729enddef
2730
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002731def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002732 var path: string = empty(dir)
2733 \ ? 'empty'
2734 \ : 'full'
2735 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002736enddef
2737
2738def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002739 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002740enddef
2741
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002742def Test_script_var_in_lambda()
2743 var lines =<< trim END
2744 vim9script
2745 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002746 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002747 END
2748 CheckScriptSuccess(lines)
2749enddef
2750
Bram Moolenaar5e654232020-09-16 15:22:00 +02002751def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002752 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002753 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002754 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002755 ->reverse()
2756 return x
2757enddef
2758
2759def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002760 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002761
2762 var lines =<< trim END
2763 vim9script
2764 var res = [{n: 1, m: 2, s: 'xxx'}]
2765 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2766 v.n,
2767 v.m,
2768 substitute(v.s, '.*', 'yyy', '')
2769 ))
2770 assert_equal(['1:2:yyy'], res)
2771 END
2772 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002773enddef
2774
Bram Moolenaarb6571982021-01-08 22:24:19 +01002775def Test_list_lambda()
2776 timer_start(1000, (_) => 0)
2777 var body = execute(timer_info()[0].callback
2778 ->string()
2779 ->substitute("('", ' ', '')
2780 ->substitute("')", '', '')
2781 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002782 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002783enddef
2784
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002785def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002786 var lines =<< trim END
2787 vim9script
2788 var flist: list<func>
2789 for i in range(10)
2790 var inloop = i
2791 flist[i] = () => inloop
2792 endfor
2793 END
2794 CheckScriptSuccess(lines)
2795
2796 lines =<< trim END
2797 vim9script
2798 if true
2799 var outloop = 5
2800 var flist: list<func>
2801 for i in range(10)
2802 flist[i] = () => outloop
2803 endfor
2804 endif
2805 END
2806 CheckScriptSuccess(lines)
2807
2808 lines =<< trim END
2809 vim9script
2810 if true
2811 var outloop = 5
2812 endif
2813 var flist: list<func>
2814 for i in range(10)
2815 flist[i] = () => outloop
2816 endfor
2817 END
2818 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002819
2820 lines =<< trim END
2821 vim9script
2822 for i in range(10)
2823 var Ref = () => 0
2824 endfor
2825 assert_equal(0, ((i) => 0)(0))
2826 END
2827 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002828enddef
2829
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002830def Test_legacy_lambda()
2831 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002832
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002833 var lines =<< trim END
2834 echo {x -> 'hello ' .. x}('foo')
2835 END
2836 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002837
2838 lines =<< trim END
2839 vim9script
2840 def Func()
2841 echo (() => 'no error')()
2842 enddef
2843 legacy call s:Func()
2844 END
2845 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002846enddef
2847
Bram Moolenaarce024c32021-06-26 13:00:49 +02002848def Test_legacy()
2849 var lines =<< trim END
2850 vim9script
2851 func g:LegacyFunction()
2852 let g:legacyvar = 1
2853 endfunc
2854 def Testit()
2855 legacy call g:LegacyFunction()
2856 enddef
2857 Testit()
2858 assert_equal(1, g:legacyvar)
2859 unlet g:legacyvar
2860 delfunc g:LegacyFunction
2861 END
2862 CheckScriptSuccess(lines)
2863enddef
2864
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002865def Test_legacy_errors()
2866 for cmd in ['if', 'elseif', 'else', 'endif',
2867 'for', 'endfor', 'continue', 'break',
2868 'while', 'endwhile',
2869 'try', 'catch', 'finally', 'endtry']
2870 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2871 endfor
2872enddef
2873
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002874def Test_call_legacy_with_dict()
2875 var lines =<< trim END
2876 vim9script
2877 func Legacy() dict
2878 let g:result = self.value
2879 endfunc
2880 def TestDirect()
2881 var d = {value: 'yes', func: Legacy}
2882 d.func()
2883 enddef
2884 TestDirect()
2885 assert_equal('yes', g:result)
2886 unlet g:result
2887
2888 def TestIndirect()
2889 var d = {value: 'foo', func: Legacy}
2890 var Fi = d.func
2891 Fi()
2892 enddef
2893 TestIndirect()
2894 assert_equal('foo', g:result)
2895 unlet g:result
2896
2897 var d = {value: 'bar', func: Legacy}
2898 d.func()
2899 assert_equal('bar', g:result)
2900 unlet g:result
2901 END
2902 CheckScriptSuccess(lines)
2903enddef
2904
Bram Moolenaarab360522021-01-10 14:02:28 +01002905def DoFilterThis(a: string): list<string>
2906 # closure nested inside another closure using argument
2907 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2908 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2909enddef
2910
2911def Test_nested_closure_using_argument()
2912 assert_equal(['x', 'x2'], DoFilterThis('x'))
2913enddef
2914
Bram Moolenaar0186e582021-01-10 18:33:11 +01002915def Test_triple_nested_closure()
2916 var what = 'x'
2917 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2918 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2919 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2920enddef
2921
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002922func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002923 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002924 call Run_Test_silent_echo()
2925endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002926
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002927def Run_Test_silent_echo()
2928 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002929 vim9script
2930 def EchoNothing()
2931 silent echo ''
2932 enddef
2933 defcompile
2934 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002935 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002936
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002937 # Check that the balloon shows up after a mouse move
2938 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2939 term_sendkeys(buf, ":abc")
2940 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002941
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002942 # clean up
2943 StopVimInTerminal(buf)
2944 delete('XTest_silent_echo')
2945enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002946
Bram Moolenaar171fb922020-10-28 16:54:47 +01002947def SilentlyError()
2948 execute('silent! invalid')
2949 g:did_it = 'yes'
2950enddef
2951
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002952func UserError()
2953 silent! invalid
2954endfunc
2955
2956def SilentlyUserError()
2957 UserError()
2958 g:did_it = 'yes'
2959enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002960
2961" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002962func Test_ignore_silent_error()
2963 let g:did_it = 'no'
2964 call SilentlyError()
2965 call assert_equal('yes', g:did_it)
2966
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002967 let g:did_it = 'no'
2968 call SilentlyUserError()
2969 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002970
2971 unlet g:did_it
2972endfunc
2973
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002974def Test_ignore_silent_error_in_filter()
2975 var lines =<< trim END
2976 vim9script
2977 def Filter(winid: number, key: string): bool
2978 if key == 'o'
2979 silent! eval [][0]
2980 return true
2981 endif
2982 return popup_filter_menu(winid, key)
2983 enddef
2984
Bram Moolenaare0de1712020-12-02 17:36:54 +01002985 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002986 feedkeys("o\r", 'xnt')
2987 END
2988 CheckScriptSuccess(lines)
2989enddef
2990
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002991def Fibonacci(n: number): number
2992 if n < 2
2993 return n
2994 else
2995 return Fibonacci(n - 1) + Fibonacci(n - 2)
2996 endif
2997enddef
2998
Bram Moolenaar985116a2020-07-12 17:31:09 +02002999def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003000 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02003001enddef
3002
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003003def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01003004 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003005 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01003006 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003007 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003008 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003009enddef
3010
3011def Test_closure_in_map()
3012 mkdir('XclosureDir/tdir', 'p')
3013 writefile(['111'], 'XclosureDir/file1')
3014 writefile(['222'], 'XclosureDir/file2')
3015 writefile(['333'], 'XclosureDir/tdir/file3')
3016
Bram Moolenaare0de1712020-12-02 17:36:54 +01003017 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02003018
3019 delete('XclosureDir', 'rf')
3020enddef
3021
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003022def Test_invalid_function_name()
3023 var lines =<< trim END
3024 vim9script
3025 def s: list<string>
3026 END
3027 CheckScriptFailure(lines, 'E129:')
3028
3029 lines =<< trim END
3030 vim9script
3031 def g: list<string>
3032 END
3033 CheckScriptFailure(lines, 'E129:')
3034
3035 lines =<< trim END
3036 vim9script
3037 def <SID>: list<string>
3038 END
3039 CheckScriptFailure(lines, 'E884:')
3040
3041 lines =<< trim END
3042 vim9script
3043 def F list<string>
3044 END
3045 CheckScriptFailure(lines, 'E488:')
3046enddef
3047
Bram Moolenaara90afb92020-07-15 22:38:56 +02003048def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003049 var lines =<< trim END
3050 var Xsetlist: func
3051 Xsetlist = function('setloclist', [0])
3052 Xsetlist([], ' ', {title: 'test'})
3053 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003054
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003055 Xsetlist = function('setloclist', [0, [], ' '])
3056 Xsetlist({title: 'test'})
3057 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003058
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003059 Xsetlist = function('setqflist')
3060 Xsetlist([], ' ', {title: 'test'})
3061 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003062
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003063 Xsetlist = function('setqflist', [[], ' '])
3064 Xsetlist({title: 'test'})
3065 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02003066
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003067 var Len: func: number = function('len', ['word'])
3068 assert_equal(4, Len())
3069
3070 var RepeatFunc = function('repeat', ['o'])
3071 assert_equal('ooooo', RepeatFunc(5))
3072 END
3073 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003074
3075 lines =<< trim END
3076 vim9script
3077 def Foo(Parser: any)
3078 enddef
3079 var Expr: func(dict<any>): dict<any>
3080 const Call = Foo(Expr)
3081 END
3082 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003083enddef
3084
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003085def Test_cmd_modifier()
3086 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003087 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003088enddef
3089
3090def Test_restore_modifiers()
3091 # check that when compiling a :def function command modifiers are not messed
3092 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003093 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003094 vim9script
3095 set eventignore=
3096 autocmd QuickFixCmdPost * copen
3097 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003098 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003099 enddef
3100 func Func()
3101 noautocmd call s:AutocmdsDisabled()
3102 let g:ei_after = &eventignore
3103 endfunc
3104 Func()
3105 END
3106 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003107 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003108enddef
3109
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003110def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003111 eval 1 + 2
3112 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003113 # call not on fourth line
3114 StackBot()
3115enddef
3116
3117def StackBot()
3118 # throw an error
3119 eval [][0]
3120enddef
3121
3122def Test_callstack_def()
3123 try
3124 StackTop()
3125 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003126 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003127 endtry
3128enddef
3129
Bram Moolenaare8211a32020-10-09 22:04:29 +02003130" Re-using spot for variable used in block
3131def Test_block_scoped_var()
3132 var lines =<< trim END
3133 vim9script
3134 def Func()
3135 var x = ['a', 'b', 'c']
3136 if 1
3137 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003138 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003139 endif
3140 var z = x
3141 assert_equal(['x', 'x', 'x'], z)
3142 enddef
3143 Func()
3144 END
3145 CheckScriptSuccess(lines)
3146enddef
3147
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003148def Test_reset_did_emsg()
3149 var lines =<< trim END
3150 @s = 'blah'
3151 au BufWinLeave * #
3152 def Func()
3153 var winid = popup_create('popup', {})
3154 exe '*s'
3155 popup_close(winid)
3156 enddef
3157 Func()
3158 END
3159 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003160 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003161enddef
3162
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003163def Test_did_emsg_reset()
3164 # executing an autocommand resets did_emsg, this should not result in a
3165 # builtin function considered failing
3166 var lines =<< trim END
3167 vim9script
3168 au BufWinLeave * #
3169 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003170 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003171 eval [][0]
3172 enddef
3173 nno <F3> <cmd>call <sid>Func()<cr>
3174 feedkeys("\<F3>\e", 'xt')
3175 END
3176 writefile(lines, 'XemsgReset')
3177 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3178 delete('XemsgReset')
3179 nunmap <F3>
3180 au! BufWinLeave
3181enddef
3182
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003183def Test_abort_with_silent_call()
3184 var lines =<< trim END
3185 vim9script
3186 g:result = 'none'
3187 def Func()
3188 g:result += 3
3189 g:result = 'yes'
3190 enddef
3191 # error is silenced, but function aborts on error
3192 silent! Func()
3193 assert_equal('none', g:result)
3194 unlet g:result
3195 END
3196 CheckScriptSuccess(lines)
3197enddef
3198
Bram Moolenaarf665e972020-12-05 19:17:16 +01003199def Test_continues_with_silent_error()
3200 var lines =<< trim END
3201 vim9script
3202 g:result = 'none'
3203 def Func()
3204 silent! g:result += 3
3205 g:result = 'yes'
3206 enddef
3207 # error is silenced, function does not abort
3208 Func()
3209 assert_equal('yes', g:result)
3210 unlet g:result
3211 END
3212 CheckScriptSuccess(lines)
3213enddef
3214
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003215def Test_abort_even_with_silent()
3216 var lines =<< trim END
3217 vim9script
3218 g:result = 'none'
3219 def Func()
3220 eval {-> ''}() .. '' .. {}['X']
3221 g:result = 'yes'
3222 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003223 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003224 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003225 unlet g:result
3226 END
3227 CheckScriptSuccess(lines)
3228enddef
3229
Bram Moolenaarf665e972020-12-05 19:17:16 +01003230def Test_cmdmod_silent_restored()
3231 var lines =<< trim END
3232 vim9script
3233 def Func()
3234 g:result = 'none'
3235 silent! g:result += 3
3236 g:result = 'none'
3237 g:result += 3
3238 enddef
3239 Func()
3240 END
3241 # can't use CheckScriptFailure, it ignores the :silent!
3242 var fname = 'Xdefsilent'
3243 writefile(lines, fname)
3244 var caught = 'no'
3245 try
3246 exe 'source ' .. fname
3247 catch /E1030:/
3248 caught = 'yes'
3249 assert_match('Func, line 4', v:throwpoint)
3250 endtry
3251 assert_equal('yes', caught)
3252 delete(fname)
3253enddef
3254
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003255def Test_cmdmod_silent_nested()
3256 var lines =<< trim END
3257 vim9script
3258 var result = ''
3259
3260 def Error()
3261 result ..= 'Eb'
3262 eval [][0]
3263 result ..= 'Ea'
3264 enddef
3265
3266 def Crash()
3267 result ..= 'Cb'
3268 sil! Error()
3269 result ..= 'Ca'
3270 enddef
3271
3272 Crash()
3273 assert_equal('CbEbEaCa', result)
3274 END
3275 CheckScriptSuccess(lines)
3276enddef
3277
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003278def Test_dict_member_with_silent()
3279 var lines =<< trim END
3280 vim9script
3281 g:result = 'none'
3282 var d: dict<any>
3283 def Func()
3284 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003285 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003286 catch
3287 endtry
3288 enddef
3289 silent! Func()
3290 assert_equal('0', g:result)
3291 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003292 END
3293 CheckScriptSuccess(lines)
3294enddef
3295
Bram Moolenaarf9041332021-01-21 19:41:16 +01003296def Test_skip_cmds_with_silent()
3297 var lines =<< trim END
3298 vim9script
3299
3300 def Func(b: bool)
3301 Crash()
3302 enddef
3303
3304 def Crash()
3305 sil! :/not found/d _
3306 sil! :/not found/put _
3307 enddef
3308
3309 Func(true)
3310 END
3311 CheckScriptSuccess(lines)
3312enddef
3313
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003314def Test_opfunc()
3315 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3316 def g:Opfunc(_: any): string
3317 setline(1, 'ASDF')
3318 return ''
3319 enddef
3320 new
3321 setline(1, 'asdf')
3322 feedkeys("\<F3>$", 'x')
3323 assert_equal('ASDF', getline(1))
3324
3325 bwipe!
3326 nunmap <F3>
3327enddef
3328
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003329func Test_opfunc_error()
3330 CheckScreendump
3331 call Run_Test_opfunc_error()
3332endfunc
3333
3334def Run_Test_opfunc_error()
3335 # test that the error from Opfunc() is displayed right away
3336 var lines =<< trim END
3337 vim9script
3338
3339 def Opfunc(type: string)
3340 try
3341 eval [][0]
3342 catch /nothing/ # error not caught
3343 endtry
3344 enddef
3345 &operatorfunc = Opfunc
3346 nnoremap <expr> l <SID>L()
3347 def L(): string
3348 return 'l'
3349 enddef
3350 'x'->repeat(10)->setline(1)
3351 feedkeys('g@l', 'n')
3352 feedkeys('llll')
3353 END
3354 call writefile(lines, 'XTest_opfunc_error')
3355
3356 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003357 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003358 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003359
3360 # clean up
3361 StopVimInTerminal(buf)
3362 delete('XTest_opfunc_error')
3363enddef
3364
Bram Moolenaar077a4232020-12-22 18:33:27 +01003365" this was crashing on exit
3366def Test_nested_lambda_in_closure()
3367 var lines =<< trim END
3368 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003369 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003370 def Outer()
3371 def g:Inner()
3372 echo map([1, 2, 3], {_, v -> v + 1})
3373 enddef
3374 g:Inner()
3375 enddef
3376 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003377 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003378 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003379 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003380 return
3381 endif
3382 assert_equal(['Done'], readfile('XnestedDone'))
3383 delete('XnestedDone')
3384enddef
3385
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003386def Test_check_func_arg_types()
3387 var lines =<< trim END
3388 vim9script
3389 def F1(x: string): string
3390 return x
3391 enddef
3392
3393 def F2(x: number): number
3394 return x + 1
3395 enddef
3396
3397 def G(g: func): dict<func>
3398 return {f: g}
3399 enddef
3400
3401 def H(d: dict<func>): string
3402 return d.f('a')
3403 enddef
3404 END
3405
3406 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3407 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3408enddef
3409
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003410def Test_list_any_type_checked()
3411 var lines =<< trim END
3412 vim9script
3413 def Foo()
3414 --decl--
3415 Bar(l)
3416 enddef
3417 def Bar(ll: list<dict<any>>)
3418 enddef
3419 Foo()
3420 END
3421 lines[2] = 'var l: list<any>'
3422 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3423
3424 lines[2] = 'var l: list<any> = []'
3425 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3426
3427 lines[2] = 'var l: list<any> = [11]'
3428 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3429enddef
3430
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003431def Test_compile_error()
3432 var lines =<< trim END
3433 def g:Broken()
3434 echo 'a' + {}
3435 enddef
3436 call g:Broken()
3437 END
3438 # First call: compilation error
3439 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3440
3441 # Second call won't try compiling again
3442 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003443 delfunc g:Broken
3444
3445 # No error when compiling with :silent!
3446 lines =<< trim END
3447 def g:Broken()
3448 echo 'a' + []
3449 enddef
3450 silent! defcompile
3451 END
3452 CheckScriptSuccess(lines)
3453
3454 # Calling the function won't try compiling again
3455 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3456 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003457enddef
3458
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003459def Test_ignored_argument()
3460 var lines =<< trim END
3461 vim9script
3462 def Ignore(_, _): string
3463 return 'yes'
3464 enddef
3465 assert_equal('yes', Ignore(1, 2))
3466
3467 func Ok(_)
3468 return a:_
3469 endfunc
3470 assert_equal('ok', Ok('ok'))
3471
3472 func Oktoo()
3473 let _ = 'too'
3474 return _
3475 endfunc
3476 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003477
3478 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003479 END
3480 CheckScriptSuccess(lines)
3481
3482 lines =<< trim END
3483 def Ignore(_: string): string
3484 return _
3485 enddef
3486 defcompile
3487 END
3488 CheckScriptFailure(lines, 'E1181:', 1)
3489
3490 lines =<< trim END
3491 var _ = 1
3492 END
3493 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003494
3495 lines =<< trim END
3496 var x = _
3497 END
3498 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003499enddef
3500
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003501def Test_too_many_arguments()
3502 var lines =<< trim END
3503 echo [0, 1, 2]->map(() => 123)
3504 END
3505 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3506
3507 lines =<< trim END
3508 echo [0, 1, 2]->map((_) => 123)
3509 END
3510 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3511enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003512
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003513def Test_closing_brace_at_start_of_line()
3514 var lines =<< trim END
3515 def Func()
3516 enddef
3517 Func(
3518 )
3519 END
3520 call CheckDefAndScriptSuccess(lines)
3521enddef
3522
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003523func CreateMydict()
3524 let g:mydict = {}
3525 func g:mydict.afunc()
3526 let g:result = self.key
3527 endfunc
3528endfunc
3529
3530def Test_numbered_function_reference()
3531 CreateMydict()
3532 var output = execute('legacy func g:mydict.afunc')
3533 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3534 execute 'function(' .. funcName .. ', [], {key: 42})()'
3535 # check that the function still exists
3536 assert_equal(output, execute('legacy func g:mydict.afunc'))
3537 unlet g:mydict
3538enddef
3539
Bram Moolenaard3a11782022-01-05 16:50:40 +00003540def Test_go_beyond_end_of_cmd()
3541 # this was reading the byte after the end of the line
3542 var lines =<< trim END
3543 def F()
3544 cal
3545 enddef
3546 defcompile
3547 END
3548 CheckScriptFailure(lines, 'E476:')
3549enddef
3550
Bram Moolenaar20677332021-06-06 17:02:53 +02003551if has('python3')
3552 def Test_python3_heredoc()
3553 py3 << trim EOF
3554 import vim
3555 vim.vars['didit'] = 'yes'
3556 EOF
3557 assert_equal('yes', g:didit)
3558
3559 python3 << trim EOF
3560 import vim
3561 vim.vars['didit'] = 'again'
3562 EOF
3563 assert_equal('again', g:didit)
3564 enddef
3565endif
3566
3567" This messes up syntax highlight, keep near the end.
3568if has('lua')
3569 def Test_lua_heredoc()
3570 g:d = {}
3571 lua << trim EOF
3572 x = vim.eval('g:d')
3573 x['key'] = 'val'
3574 EOF
3575 assert_equal('val', g:d.key)
3576 enddef
3577endif
3578
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003579
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003580" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker