blob: 7a5f0f1a4012da267db4abd07a43d7f4c42672a1 [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000494 CheckDefAndScriptFailure(["call Test ('text')"], ['E476:', 'E1068:'])
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000550
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000551 # using script variable requires matching type or type cast when executed
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000552 lines =<< trim END
553 vim9script
554 var a: any
555 def Func(arg: string = a)
556 echo arg
557 enddef
558 defcompile
559 END
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000560 CheckScriptSuccess(lines + ['a = "text"', 'Func()'])
561 CheckScriptFailure(lines + ['a = 123', 'Func()'], 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000562
563 # using global variable does not require type cast
564 lines =<< trim END
565 vim9script
566 def Func(arg: string = g:str)
567 echo arg
568 enddef
569 g:str = 'works'
570 Func()
571 END
572 CheckScriptSuccess(lines)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200573enddef
574
Bram Moolenaarcef12702021-01-04 14:09:43 +0100575def FuncWithComment( # comment
576 a: number, #comment
577 b: bool, # comment
578 c: string) #comment
579 assert_equal(4, a)
580 assert_equal(true, b)
581 assert_equal('yes', c)
582enddef
583
584def Test_func_with_comments()
585 FuncWithComment(4, true, 'yes')
586
587 var lines =<< trim END
588 def Func(# comment
589 arg: string)
590 enddef
591 END
592 CheckScriptFailure(lines, 'E125:', 1)
593
594 lines =<< trim END
595 def Func(
596 arg: string# comment
597 )
598 enddef
599 END
600 CheckScriptFailure(lines, 'E475:', 2)
601
602 lines =<< trim END
603 def Func(
604 arg: string
605 )# comment
606 enddef
607 END
608 CheckScriptFailure(lines, 'E488:', 3)
609enddef
610
Bram Moolenaar04b12692020-05-04 23:24:44 +0200611def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000612 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200613 return 'nested ' .. arg
614 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000615 NestedDef(':def')->assert_equal('nested :def')
616
617 func NestedFunc(arg)
618 return 'nested ' .. a:arg
619 endfunc
620 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200621
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200622 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
623 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
624
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200625 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
626 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200627
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 var lines =<< trim END
629 def Outer()
630 def Inner()
631 # comment
632 enddef
633 def Inner()
634 enddef
635 enddef
636 END
637 CheckDefFailure(lines, 'E1073:')
638
639 lines =<< trim END
640 def Outer()
641 def Inner()
642 # comment
643 enddef
644 def! Inner()
645 enddef
646 enddef
647 END
648 CheckDefFailure(lines, 'E1117:')
649
Bram Moolenaardb8e5c22021-12-25 19:58:22 +0000650 lines =<< trim END
651 vim9script
652 def Outer()
653 def Inner()
654 g:result = 'ok'
655 enddef
656 Inner()
657 enddef
658 Outer()
659 Inner()
660 END
661 CheckScriptFailure(lines, 'E117: Unknown function: Inner')
662 assert_equal('ok', g:result)
663 unlet g:result
664
Bram Moolenaar54021752020-12-06 18:50:36 +0100665 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100666 lines =<< trim END
667 vim9script
668 var thecount = 0
669 if true
670 def Test(): number
671 def TheFunc(): number
672 thecount += 1
673 return thecount
674 enddef
675 return TheFunc()
676 enddef
677 endif
678 defcompile
679 assert_equal(1, Test())
680 assert_equal(2, Test())
681 END
682 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100683
684 # also works when "thecount" is inside the "if" block
685 lines =<< trim END
686 vim9script
687 if true
688 var thecount = 0
689 def Test(): number
690 def TheFunc(): number
691 thecount += 1
692 return thecount
693 enddef
694 return TheFunc()
695 enddef
696 endif
697 defcompile
698 assert_equal(1, Test())
699 assert_equal(2, Test())
700 END
701 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200702
703 lines =<< trim END
704 vim9script
705 def Outer()
706 def Inner()
707 echo 'hello'
708 enddef burp
709 enddef
710 defcompile
711 END
712 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200713enddef
714
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100715def Test_not_nested_function()
716 echo printf('%d',
717 function('len')('xxx'))
718enddef
719
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200720func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200721 call MyDefaultArgs()->assert_equal('string')
722 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200723 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200724endfunc
725
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200726def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200727 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200728 vim9script
729 def Outer()
730 def g:Inner(): string
731 return 'inner'
732 enddef
733 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200734 defcompile
735 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200736 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200737 delfunc g:Inner
738 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200739 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200740 delfunc g:Inner
741 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200742 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200743 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200744 END
745 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200746
747 lines =<< trim END
748 vim9script
749 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000750 func g:Inner()
751 return 'inner'
752 endfunc
753 enddef
754 defcompile
755 Outer()
756 g:Inner()->assert_equal('inner')
757 delfunc g:Inner
758 Outer()
759 g:Inner()->assert_equal('inner')
760 delfunc g:Inner
761 Outer()
762 g:Inner()->assert_equal('inner')
763 delfunc g:Inner
764 END
765 CheckScriptSuccess(lines)
766
767 lines =<< trim END
768 vim9script
769 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200770 def g:Inner(): string
771 return 'inner'
772 enddef
773 enddef
774 defcompile
775 Outer()
776 Outer()
777 END
778 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100779 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200780
781 lines =<< trim END
782 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100783 def Outer()
784 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100785 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100786 enddef
787 g:Inner()
788 enddef
789 Outer()
790 END
791 CheckScriptSuccess(lines)
792 delfunc g:Inner
793
794 lines =<< trim END
795 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200796 def Func()
797 echo 'script'
798 enddef
799 def Outer()
800 def Func()
801 echo 'inner'
802 enddef
803 enddef
804 defcompile
805 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000806 CheckScriptFailure(lines, "E1073:", 1)
807
808 lines =<< trim END
809 vim9script
810 def Func()
811 echo 'script'
812 enddef
813 def Func()
814 echo 'script'
815 enddef
816 END
817 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200818enddef
819
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100820def DefListAll()
821 def
822enddef
823
824def DefListOne()
825 def DefListOne
826enddef
827
828def DefListMatches()
829 def /DefList
830enddef
831
832def Test_nested_def_list()
833 var funcs = split(execute('call DefListAll()'), "\n")
834 assert_true(len(funcs) > 10)
835 assert_true(funcs->index('def DefListAll()') >= 0)
836
837 funcs = split(execute('call DefListOne()'), "\n")
838 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
839
840 funcs = split(execute('call DefListMatches()'), "\n")
841 assert_true(len(funcs) >= 3)
842 assert_true(funcs->index('def DefListAll()') >= 0)
843 assert_true(funcs->index('def DefListOne()') >= 0)
844 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100845
846 var lines =<< trim END
847 vim9script
848 def Func()
849 def +Func+
850 enddef
851 defcompile
852 END
853 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100854enddef
855
Bram Moolenaar333894b2020-08-01 18:53:07 +0200856def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200857 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200858 vim9script
859 def g:Func(): string
860 return 'global'
861 enddef
862 def Func(): string
863 return 'local'
864 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200865 g:Func()->assert_equal('global')
866 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100867 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200868 END
869 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200870
871 lines =<< trim END
872 vim9script
873 def g:Funcy()
874 echo 'funcy'
875 enddef
876 s:Funcy()
877 END
878 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200879enddef
880
Bram Moolenaar0f769812020-09-12 18:32:34 +0200881def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200882 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200883 vim9script
884 def g:Gfunc(): string
885 return 'global'
886 enddef
887 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200888 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200889 return Gfunc('testing')
890 enddef
891 g:Gfunc()->assert_equal('global')
892 AnotherFunc()->assert_equal(7)
893 delfunc g:Gfunc
894 END
895 CheckScriptSuccess(lines)
896
897 lines =<< trim END
898 vim9script
899 def g:Func(): string
900 return 'global'
901 enddef
902 def AnotherFunc()
903 g:Func = function('len')
904 enddef
905 AnotherFunc()
906 END
907 CheckScriptFailure(lines, 'E705:')
908 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200909
910 # global function is found without g: prefix
911 lines =<< trim END
912 vim9script
913 def g:Func(): string
914 return 'global'
915 enddef
916 def AnotherFunc(): string
917 return Func()
918 enddef
919 assert_equal('global', AnotherFunc())
920 delfunc g:Func
921 END
922 CheckScriptSuccess(lines)
923
924 lines =<< trim END
925 vim9script
926 def g:Func(): string
927 return 'global'
928 enddef
929 assert_equal('global', Func())
930 delfunc g:Func
931 END
932 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200933enddef
934
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200935func TakesOneArg(arg)
936 echo a:arg
937endfunc
938
939def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200940 CheckDefFailure(['TakesOneArg()'], 'E119:')
941 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
942 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
943 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200944
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200945 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200946 vim9script
947 def Func(s: string)
948 echo s
949 enddef
950 Func([])
951 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200952 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200953
954 lines =<< trim END
955 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100956 var name = 'piet'
957 def FuncOne(name: string)
958 echo nr
959 enddef
960 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100961 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100962
963 lines =<< trim END
964 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200965 def FuncOne(nr: number)
966 echo nr
967 enddef
968 def FuncTwo()
969 FuncOne()
970 enddef
971 defcompile
972 END
973 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200974 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200975 try
976 source Xscript
977 catch
978 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
979 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
980 didCatch = true
981 endtry
982 assert_true(didCatch)
983
984 lines =<< trim END
985 vim9script
986 def FuncOne(nr: number)
987 echo nr
988 enddef
989 def FuncTwo()
990 FuncOne(1, 2)
991 enddef
992 defcompile
993 END
994 writefile(lines, 'Xscript')
995 didCatch = false
996 try
997 source Xscript
998 catch
999 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
1000 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
1001 didCatch = true
1002 endtry
1003 assert_true(didCatch)
1004
1005 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001006enddef
1007
Bram Moolenaar50824712020-12-20 21:10:17 +01001008def Test_call_funcref_wrong_args()
1009 var head =<< trim END
1010 vim9script
1011 def Func3(a1: string, a2: number, a3: list<number>)
1012 echo a1 .. a2 .. a3[0]
1013 enddef
1014 def Testme()
1015 var funcMap: dict<func> = {func: Func3}
1016 END
1017 var tail =<< trim END
1018 enddef
1019 Testme()
1020 END
1021 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
1022
1023 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
1024 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001025
1026 var lines =<< trim END
1027 vim9script
1028 var Ref: func(number): any
1029 Ref = (j) => !j
1030 echo Ref(false)
1031 END
1032 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
1033
1034 lines =<< trim END
1035 vim9script
1036 var Ref: func(number): any
1037 Ref = (j) => !j
1038 call Ref(false)
1039 END
1040 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001041enddef
1042
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001043def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001044 var lines =<< trim END
1045 var Callback = (..._) => 'anything'
1046 assert_equal('anything', Callback())
1047 assert_equal('anything', Callback(1))
1048 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001049
1050 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001051 END
1052 CheckDefAndScriptSuccess(lines)
1053
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001054 CheckDefFailure(['echo ((i) => 0)()'],
1055 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001056
Bram Moolenaar2a389082021-04-09 20:24:31 +02001057 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001058 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001059 echo Ref(1, 'x')
1060 END
1061 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001062
1063 lines =<< trim END
1064 var Ref: func(job, string, number)
1065 Ref = (x, y) => 0
1066 END
1067 CheckDefAndScriptFailure(lines, 'E1012:')
1068
1069 lines =<< trim END
1070 var Ref: func(job, string)
1071 Ref = (x, y, z) => 0
1072 END
1073 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001074
1075 lines =<< trim END
1076 var one = 1
1077 var l = [1, 2, 3]
1078 echo map(l, (one) => one)
1079 END
1080 CheckDefFailure(lines, 'E1167:')
1081 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1082
1083 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001084 var Ref: func(any, ?any): bool
1085 Ref = (_, y = 1) => false
1086 END
1087 CheckDefAndScriptFailure(lines, 'E1172:')
1088
1089 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001090 var a = 0
1091 var b = (a == 0 ? 1 : 2)
1092 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001093 var txt = 'a'
1094 b = (txt =~ 'x' ? 1 : 2)
1095 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001096 END
1097 CheckDefAndScriptSuccess(lines)
1098
1099 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001100 def ShadowLocal()
1101 var one = 1
1102 var l = [1, 2, 3]
1103 echo map(l, (one) => one)
1104 enddef
1105 END
1106 CheckDefFailure(lines, 'E1167:')
1107
1108 lines =<< trim END
1109 def Shadowarg(one: number)
1110 var l = [1, 2, 3]
1111 echo map(l, (one) => one)
1112 enddef
1113 END
1114 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001115
1116 lines =<< trim END
1117 echo ((a) => a)('aa', 'bb')
1118 END
1119 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001120
1121 lines =<< trim END
1122 echo 'aa'->((a) => a)('bb')
1123 END
1124 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1125 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001126enddef
1127
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001128def Test_lambda_line_nr()
1129 var lines =<< trim END
1130 vim9script
1131 # comment
1132 # comment
1133 var id = timer_start(1'000, (_) => 0)
1134 var out = execute('verbose ' .. timer_info(id)[0].callback
1135 ->string()
1136 ->substitute("('\\|')", ' ', 'g'))
1137 assert_match('Last set from .* line 4', out)
1138 END
1139 CheckScriptSuccess(lines)
1140enddef
1141
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001142def FilterWithCond(x: string, Cond: func(string): bool): bool
1143 return Cond(x)
1144enddef
1145
Bram Moolenaar0346b792021-01-31 22:18:29 +01001146def Test_lambda_return_type()
1147 var lines =<< trim END
1148 var Ref = (): => 123
1149 END
1150 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001151
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001152 # no space before the return type
1153 lines =<< trim END
1154 var Ref = (x):number => x + 1
1155 END
1156 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1157
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001158 # this works
1159 for x in ['foo', 'boo']
1160 echo FilterWithCond(x, (v) => v =~ '^b')
1161 endfor
1162
1163 # this fails
1164 lines =<< trim END
1165 echo FilterWithCond('foo', (v) => v .. '^b')
1166 END
1167 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001168
1169 lines =<< trim END
1170 var Lambda1 = (x) => {
1171 return x
1172 }
1173 assert_equal('asdf', Lambda1('asdf'))
1174 var Lambda2 = (x): string => {
1175 return x
1176 }
1177 assert_equal('foo', Lambda2('foo'))
1178 END
1179 CheckDefAndScriptSuccess(lines)
1180
1181 lines =<< trim END
1182 var Lambda = (x): string => {
1183 return x
1184 }
1185 echo Lambda(['foo'])
1186 END
1187 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001188enddef
1189
Bram Moolenaar709664c2020-12-12 14:33:41 +01001190def Test_lambda_uses_assigned_var()
1191 CheckDefSuccess([
1192 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001193 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001194enddef
1195
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001196def Test_pass_legacy_lambda_to_def_func()
1197 var lines =<< trim END
1198 vim9script
1199 func Foo()
1200 eval s:Bar({x -> 0})
1201 endfunc
1202 def Bar(y: any)
1203 enddef
1204 Foo()
1205 END
1206 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001207
1208 lines =<< trim END
1209 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001210 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001211 enddef
1212 legacy call g:TestFunc({-> 0})
1213 delfunc g:TestFunc
1214
1215 def g:TestFunc(f: func(number))
1216 enddef
1217 legacy call g:TestFunc({nr -> 0})
1218 delfunc g:TestFunc
1219 END
1220 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001221enddef
1222
Bram Moolenaar844fb642021-10-23 13:32:30 +01001223def Test_lambda_in_reduce_line_break()
1224 # this was using freed memory
1225 var lines =<< trim END
1226 vim9script
1227 const result: dict<number> =
1228 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1229 ->reduce((acc, val) => {
1230 if has_key(acc, val)
1231 acc[val] += 1
1232 return acc
1233 else
1234 acc[val] = 1
1235 return acc
1236 endif
1237 }, {})
1238 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1239 END
1240 CheckScriptSuccess(lines)
1241enddef
1242
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001243def Test_set_opfunc_to_lambda()
1244 var lines =<< trim END
1245 vim9script
1246 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1247 def CountSpaces(type = ''): string
1248 if type == ''
1249 &operatorfunc = (t) => CountSpaces(t)
1250 return 'g@'
1251 endif
1252 normal! '[V']y
1253 g:result = getreg('"')->count(' ')
1254 return ''
1255 enddef
1256 new
1257 'a b c d e'->setline(1)
1258 feedkeys("\<F4>", 'x')
1259 assert_equal(4, g:result)
1260 bwipe!
1261 END
1262 CheckScriptSuccess(lines)
1263enddef
1264
Bram Moolenaaref082e12021-12-12 21:02:03 +00001265def Test_set_opfunc_to_global_function()
1266 var lines =<< trim END
1267 vim9script
1268 def g:CountSpaces(type = ''): string
1269 normal! '[V']y
1270 g:result = getreg('"')->count(' ')
1271 return ''
1272 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001273 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001274 &operatorfunc = g:CountSpaces
1275 new
1276 'a b c d e'->setline(1)
1277 feedkeys("g@_", 'x')
1278 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001279
1280 &operatorfunc = ''
1281 g:result = 0
1282 # global function works in :def function
1283 def Func()
1284 &operatorfunc = g:CountSpaces
1285 enddef
1286 Func()
1287 feedkeys("g@_", 'x')
1288 assert_equal(4, g:result)
1289
Bram Moolenaaref082e12021-12-12 21:02:03 +00001290 bwipe!
1291 END
1292 CheckScriptSuccess(lines)
1293 &operatorfunc = ''
1294enddef
1295
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001296def Test_use_script_func_name_with_prefix()
1297 var lines =<< trim END
1298 vim9script
1299 func s:Getit()
1300 return 'it'
1301 endfunc
1302 var Fn = s:Getit
1303 assert_equal('it', Fn())
1304 END
1305 CheckScriptSuccess(lines)
1306enddef
1307
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001308def Test_lambda_type_allocated()
1309 # Check that unreferencing a partial using a lambda can use the variable type
1310 # after the lambda has been freed and does not leak memory.
1311 var lines =<< trim END
1312 vim9script
1313
1314 func MyomniFunc1(val, findstart, base)
1315 return a:findstart ? 0 : []
1316 endfunc
1317
1318 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1319 &omnifunc = Lambda
1320 Lambda = (a, b) => MyomniFunc1(20, a, b)
1321 &omnifunc = string(Lambda)
1322 Lambda = (a, b) => strlen(a)
1323 END
1324 CheckScriptSuccess(lines)
1325enddef
1326
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001327" Default arg and varargs
1328def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001329 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001330 for s in rest
1331 res ..= ',' .. s
1332 endfor
1333 return res
1334enddef
1335
1336def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001337 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001338 MyDefVarargs('one')->assert_equal('one,foo')
1339 MyDefVarargs('one', 'two')->assert_equal('one,two')
1340 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001341 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001342 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001343 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001344 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001345
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001346 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001347 vim9script
1348 def Func(...l: list<string>)
1349 echo l
1350 enddef
1351 Func('a', 'b', 'c')
1352 END
1353 CheckScriptSuccess(lines)
1354
1355 lines =<< trim END
1356 vim9script
1357 def Func(...l: list<string>)
1358 echo l
1359 enddef
1360 Func()
1361 END
1362 CheckScriptSuccess(lines)
1363
1364 lines =<< trim END
1365 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001366 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001367 echo l
1368 enddef
1369 Func(0)
1370 END
1371 CheckScriptSuccess(lines)
1372
1373 lines =<< trim END
1374 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001375 def Func(...l: any)
1376 echo l
1377 enddef
1378 Func(0)
1379 END
1380 CheckScriptFailure(lines, 'E1180:', 2)
1381
1382 lines =<< trim END
1383 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001384 def Func(..._l: list<string>)
1385 echo _l
1386 enddef
1387 Func('a', 'b', 'c')
1388 END
1389 CheckScriptSuccess(lines)
1390
1391 lines =<< trim END
1392 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001393 def Func(...l: list<string>)
1394 echo l
1395 enddef
1396 Func(1, 2, 3)
1397 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001398 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001399
1400 lines =<< trim END
1401 vim9script
1402 def Func(...l: list<string>)
1403 echo l
1404 enddef
1405 Func('a', 9)
1406 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001407 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001408
1409 lines =<< trim END
1410 vim9script
1411 def Func(...l: list<string>)
1412 echo l
1413 enddef
1414 Func(1, 'a')
1415 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001416 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001417
1418 lines =<< trim END
1419 vim9script
1420 def Func( # some comment
1421 ...l = []
1422 )
1423 echo l
1424 enddef
1425 END
1426 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001427
1428 lines =<< trim END
1429 vim9script
1430 def DoIt()
1431 g:Later('')
1432 enddef
1433 defcompile
1434 def g:Later(...l: list<number>)
1435 enddef
1436 DoIt()
1437 END
1438 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001439enddef
1440
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001441let s:value = ''
1442
1443def FuncOneDefArg(opt = 'text')
1444 s:value = opt
1445enddef
1446
1447def FuncTwoDefArg(nr = 123, opt = 'text'): string
1448 return nr .. opt
1449enddef
1450
1451def FuncVarargs(...arg: list<string>): string
1452 return join(arg, ',')
1453enddef
1454
1455def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001456 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001457 RefDefArg = FuncOneDefArg
1458 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001459 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001460 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001461 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001462
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001463 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001464 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001465 RefDef2Arg()->assert_equal('123text')
1466 RefDef2Arg(99)->assert_equal('99text')
1467 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001468
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001469 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1470 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001471
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001472 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001473 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001474 RefVarargs()->assert_equal('')
1475 RefVarargs('one')->assert_equal('one')
1476 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001477
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001478 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1479 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001480enddef
1481
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001482" Only varargs
1483def MyVarargsOnly(...args: list<string>): string
1484 return join(args, ',')
1485enddef
1486
1487def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001488 MyVarargsOnly()->assert_equal('')
1489 MyVarargsOnly('one')->assert_equal('one')
1490 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001491 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1492 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001493enddef
1494
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001495def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001496 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001497 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001498 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001499enddef
1500
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001501def DictArg(arg: dict<string>)
1502 arg['key'] = 'value'
1503enddef
1504
1505def ListArg(arg: list<string>)
1506 arg[0] = 'value'
1507enddef
1508
1509def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001510 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001511 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001512 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001513 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001514 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001515 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001516 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001517
Bram Moolenaard2c61702020-09-06 15:58:36 +02001518 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001519 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001520enddef
1521
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001522" These argument names are reserved in legacy functions.
1523def WithReservedNames(firstline: string, lastline: string): string
1524 return firstline .. lastline
1525enddef
1526
1527def Test_argument_names()
1528 assert_equal('OK', WithReservedNames('O', 'K'))
1529enddef
1530
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001531def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001532 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001533 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001534enddef
1535
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001536func DefinedLater(arg)
1537 return a:arg
1538endfunc
1539
1540def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001541 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001542 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001543 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001544
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001545 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001546 vim9script
1547 def RetNumber(): number
1548 return 123
1549 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001550 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001551 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001552 END
1553 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001554
1555 lines =<< trim END
1556 vim9script
1557 def RetNumber(): number
1558 return 123
1559 enddef
1560 def Bar(F: func: number): number
1561 return F()
1562 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001563 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001564 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001565 END
1566 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001567
1568 lines =<< trim END
1569 vim9script
1570 def UseNumber(nr: number)
1571 echo nr
1572 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001573 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001574 Funcref(123)
1575 END
1576 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001577
1578 lines =<< trim END
1579 vim9script
1580 def UseNumber(nr: number)
1581 echo nr
1582 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001583 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001584 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001585 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001586
1587 lines =<< trim END
1588 vim9script
1589 def EchoNr(nr = 34)
1590 g:echo = nr
1591 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001592 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001593 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001594 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001595 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001596 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001597 END
1598 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001599
1600 lines =<< trim END
1601 vim9script
1602 def EchoList(...l: list<number>)
1603 g:echo = l
1604 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001605 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001606 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001607 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001608 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001609 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001610 END
1611 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001612
1613 lines =<< trim END
1614 vim9script
1615 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1616 g:optarg = opt
1617 g:listarg = l
1618 return nr
1619 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001620 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001621 Funcref(10)->assert_equal(10)
1622 g:optarg->assert_equal(12)
1623 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001624
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001625 Funcref(11, 22)->assert_equal(11)
1626 g:optarg->assert_equal(22)
1627 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001628
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001629 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1630 g:optarg->assert_equal(18)
1631 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001632 END
1633 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001634enddef
1635
1636let SomeFunc = function('len')
1637let NotAFunc = 'text'
1638
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001639def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001640 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001641 var Ref1: func(bool): string
1642 var Ref2: func(bool): number
1643 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001644 Ref3 = g:cond ? Ref1 : Ref2
1645
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001646 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001647 var Refa1: func(bool): number
1648 var Refa2: func(bool, number): number
1649 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001650 Refa3 = g:cond ? Refa1 : Refa2
1651
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001652 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001653 var Refb1: func(bool, string): number
1654 var Refb2: func(string, number): number
1655 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001656 Refb3 = g:cond ? Refb1 : Refb2
1657enddef
1658
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001659def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001660 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001661enddef
1662
1663def DefinedEvenLater(arg: string): string
1664 return arg
1665enddef
1666
1667def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001668 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001669 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001670enddef
1671
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001672def Test_nested_function_with_nextcmd()
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001673 var lines =<< trim END
1674 vim9script
1675 # Define an outer function
1676 def FirstFunction()
1677 # Define an inner function
1678 def SecondFunction()
1679 # the function has a body, a double free is detected.
1680 AAAAA
1681
1682 # enddef followed by | or } followed by # one or more characters
1683 enddef|BBBB
1684 enddef
1685
1686 # Compile all functions
1687 defcompile
1688 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001689 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001690enddef
1691
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001692def Test_nested_function_with_args_split()
1693 var lines =<< trim END
1694 vim9script
1695 def FirstFunction()
1696 def SecondFunction(
1697 )
1698 # had a double free if the right parenthesis of the nested function is
1699 # on the next line
1700
1701 enddef|BBBB
1702 enddef
1703 # Compile all functions
1704 defcompile
1705 END
Bram Moolenaar7473a842021-12-28 17:55:26 +00001706 CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
1707
1708 lines =<< trim END
1709 vim9script
1710 def FirstFunction()
1711 func SecondFunction()
1712 endfunc|BBBB
1713 enddef
1714 defcompile
1715 END
1716 CheckScriptFailure(lines, 'E1173: Text found after endfunction: BBBB')
Bram Moolenaar4bf10062021-12-28 17:23:12 +00001717enddef
1718
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001719def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001720 CheckScriptFailure([
1721 'def Func(): number',
1722 'return "a"',
1723 'enddef',
1724 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001725 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001726 CheckScriptFailure([
1727 'def Func(): string',
1728 'return 1',
1729 'enddef',
1730 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001731 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001732 CheckScriptFailure([
1733 'def Func(): void',
1734 'return "a"',
1735 'enddef',
1736 'defcompile'],
1737 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001738 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001739 CheckScriptFailure([
1740 'def Func()',
1741 'return "a"',
1742 'enddef',
1743 'defcompile'],
1744 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001745 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001746
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001747 CheckScriptFailure([
1748 'def Func(): number',
1749 'return',
1750 'enddef',
1751 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001752 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001753
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001754 CheckScriptFailure([
1755 'def Func():number',
1756 'return 123',
1757 'enddef',
1758 'defcompile'], 'E1069:')
1759 delfunc! g:Func
1760
1761 CheckScriptFailure([
1762 'def Func() :number',
1763 'return 123',
1764 'enddef',
1765 'defcompile'], 'E1059:')
1766 delfunc! g:Func
1767
1768 CheckScriptFailure([
1769 'def Func() : number',
1770 'return 123',
1771 'enddef',
1772 'defcompile'], 'E1059:')
1773 delfunc! g:Func
1774
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001775 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001776 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001777 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001778 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001779 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001780 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001781
1782 CheckScriptFailure([
1783 'vim9script',
1784 'def FuncB()',
1785 ' return 123',
1786 'enddef',
1787 'def FuncA()',
1788 ' FuncB()',
1789 'enddef',
1790 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791enddef
1792
1793def Test_arg_type_wrong()
1794 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001795 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001796 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001797 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001798 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1799 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001800enddef
1801
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001802def Test_white_space_before_comma()
1803 var lines =<< trim END
1804 vim9script
1805 def Func(a: number , b: number)
1806 enddef
1807 END
1808 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001809 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001810enddef
1811
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001812def Test_white_space_after_comma()
1813 var lines =<< trim END
1814 vim9script
1815 def Func(a: number,b: number)
1816 enddef
1817 END
1818 CheckScriptFailure(lines, 'E1069:')
1819
1820 # OK in legacy function
1821 lines =<< trim END
1822 vim9script
1823 func Func(a,b)
1824 endfunc
1825 END
1826 CheckScriptSuccess(lines)
1827enddef
1828
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001829def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001830 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001831 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001832 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001834 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001835 enddef
1836 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001837 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001838
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001839 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001840 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001841 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001842
Bram Moolenaar67979662020-06-20 22:50:47 +02001843 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001844 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001845 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001846
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001847 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001848 def ListFunc(arg: list<number>)
1849 listvar = arg
1850 enddef
1851 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001852 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001853
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001854 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001855 def DictFunc(arg: dict<number>)
1856 dictvar = arg
1857 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001858 {a: 1, b: 2}->DictFunc()
1859 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001860 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001861 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001862 enddef
1863 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001864 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001865
Bram Moolenaare0de1712020-12-02 17:36:54 +01001866 {a: 3, b: 4}->DictFunc()
1867 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001868
1869 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001870 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001871 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001872 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001873
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001874 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001875 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001876 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001877 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001878 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001879 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001880
1881 def UseString()
1882 'xyork'->MyFunc()
1883 enddef
1884 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001885 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001886
Bram Moolenaar10409562020-07-29 20:00:38 +02001887 def UseString2()
1888 "knife"->MyFunc()
1889 enddef
1890 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001891 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001892
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001893 # prepending a colon makes it a mark
1894 new
1895 setline(1, ['aaa', 'bbb', 'ccc'])
1896 normal! 3Gmt1G
1897 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001898 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001899 bwipe!
1900
Bram Moolenaare6b53242020-07-01 17:28:33 +02001901 MyFunc(
1902 'continued'
1903 )
1904 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001905 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001906 )
1907
1908 call MyFunc(
1909 'more'
1910 ..
1911 'lines'
1912 )
1913 assert_equal(
1914 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001915 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916 END
1917 writefile(lines, 'Xcall.vim')
1918 source Xcall.vim
1919 delete('Xcall.vim')
1920enddef
1921
1922def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001923 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001924 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001925 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001926 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001927 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001928 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001929 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001930 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001931 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001932enddef
1933
Bram Moolenaar65b95452020-07-19 14:03:09 +02001934def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001935 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001936 vim9script
1937 def MyFunc(arg: string)
1938 echo arg
1939 enddef
1940 MyFunc(1234)
1941 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001942 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001943enddef
1944
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001945def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001946 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001947 vim9script
1948 const var = ''
1949 def MyFunc(arg: string)
1950 var = 'asdf'
1951 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001952 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001953 END
1954 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001955 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001956 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001957
1958 lines =<< trim END
1959 const g:Aconst = 77
1960 def Change()
1961 # comment
1962 g:Aconst = 99
1963 enddef
1964 call Change()
1965 unlet g:Aconst
1966 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001967 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001968enddef
1969
1970" Test that inside :function a Python function can be defined, :def is not
1971" recognized.
1972func Test_function_python()
1973 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001974 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001975 execute py "<< EOF"
1976def do_something():
1977 return 1
1978EOF
1979endfunc
1980
1981def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001982 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001983 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001984 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001985 echo 'hello'
1986 enddef
1987
1988 def CallGoneSoon()
1989 GoneSoon()
1990 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001991 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001992
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001993 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001994 CallGoneSoon()
1995 END
1996 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001997 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1998 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001999
2000 delete('XToDelFunc')
2001enddef
2002
Bram Moolenaar7509ad82021-12-14 18:14:37 +00002003func Test_free_dict_while_in_funcstack()
2004 " relies on the sleep command
2005 CheckUnix
2006 call Run_Test_free_dict_while_in_funcstack()
2007endfunc
2008
2009def Run_Test_free_dict_while_in_funcstack()
2010
2011 # this was freeing the TermRun() default argument dictionary while it was
2012 # still referenced in a funcstack_T
2013 var lines =<< trim END
2014 vim9script
2015
2016 &updatetime = 400
2017 def TermRun(_ = {})
2018 def Post()
2019 enddef
2020 def Exec()
2021 term_start('sleep 1', {
2022 term_finish: 'close',
2023 exit_cb: (_, _) => Post(),
2024 })
2025 enddef
2026 Exec()
2027 enddef
2028 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
2029 timer_start(100, (_) => feedkeys("\<F4>"))
2030 timer_start(1000, (_) => feedkeys("\<F4>"))
2031 sleep 1500m
2032 END
2033 CheckScriptSuccess(lines)
2034 nunmap <F4>
2035 set updatetime&
2036enddef
2037
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002038def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002039 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002040 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002041 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002042 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002043 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02002044 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002045 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002046 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002047 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002048
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002049 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002050 g:Func1()->assert_equal('Func1')
2051 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002052
2053 delfunc! Func0
2054 delfunc! Func1
2055 delfunc! Func2
2056enddef
2057
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002058def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002059 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002060 vim9script
2061 func Func(arg)
2062 echo a:arg
2063 endfunc
2064 Func('text')
2065 END
2066 writefile(lines, 'XVim9Func')
2067 so XVim9Func
2068
2069 delete('XVim9Func')
2070enddef
2071
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002072let s:funcResult = 0
2073
2074def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002075 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002076enddef
2077
2078def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002079 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002080 return 1234
2081enddef
2082
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002083def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002084 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002085 return 'text'
2086enddef
2087
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002088def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002089 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002090enddef
2091
2092def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002093 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002094 return arg
2095enddef
2096
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002097def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002098 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002099enddef
2100
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002101def FuncOneArgRetString(arg: string): string
2102 return arg
2103enddef
2104
Bram Moolenaar89228602020-04-05 22:14:54 +02002105def FuncOneArgRetAny(arg: any): any
2106 return arg
2107enddef
2108
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002109def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002110 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002111 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002112 Ref1 = FuncNoArgNoRet
2113 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002114 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002115
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002116 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002117 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002118 Ref2 = FuncNoArgNoRet
2119 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002120 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002121
Bram Moolenaar53900992020-08-22 19:02:02 +02002122 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002123 Ref2 = FuncOneArgNoRet
2124 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002125 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002126
Bram Moolenaar53900992020-08-22 19:02:02 +02002127 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002128 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002129 Ref2()->assert_equal(1234)
2130 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002131
Bram Moolenaar53900992020-08-22 19:02:02 +02002132 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002133 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002134 Ref2(13)->assert_equal(13)
2135 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002136enddef
2137
Bram Moolenaar9978d472020-07-05 16:01:56 +02002138def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002139 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002140 for n in repeat([1], 3)
2141 res += n
2142 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002143 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002144
2145 res = 0
2146 for n in add([1, 2], 3)
2147 res += n
2148 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002149 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002150enddef
2151
Bram Moolenaar846178a2020-07-05 17:04:13 +02002152def Test_argv_return_type()
2153 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002154 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002155 for name in argv()
2156 res ..= name
2157 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002158 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002159enddef
2160
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002161def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002162 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002163 RefVoid = FuncNoArgNoRet
2164 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002165 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2166 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002167
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002168 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002169 RefAny = FuncNoArgRetNumber
2170 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002171 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2172 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002173
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002174 var RefAnyNoArgs: func: any = RefAny
2175
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002176 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002177 RefNr = FuncNoArgRetNumber
2178 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002179 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2180 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002181
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002182 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002183 RefStr = FuncNoArgRetString
2184 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002185 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2186 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002187enddef
2188
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002189def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002190 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002191
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002192 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2193 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2194 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2195 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2196 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2197 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 +02002198
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002199 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2200 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2201 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:')
2202 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002203enddef
2204
Bram Moolenaar89228602020-04-05 22:14:54 +02002205def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002206 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002207 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002208 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002209
2210 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002211 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002212
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002213 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002214 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002215 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002216
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002217 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002218enddef
2219
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002220def Test_func_common_type()
2221 def FuncOne(n: number): number
2222 return n
2223 enddef
2224 def FuncTwo(s: string): number
2225 return len(s)
2226 enddef
2227 def FuncThree(n: number, s: string): number
2228 return n + len(s)
2229 enddef
2230 var list = [FuncOne, FuncTwo, FuncThree]
2231 assert_equal(8, list[0](8))
2232 assert_equal(4, list[1]('word'))
2233 assert_equal(7, list[2](3, 'word'))
2234enddef
2235
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002236def MultiLine(
2237 arg1: string,
2238 arg2 = 1234,
2239 ...rest: list<string>
2240 ): string
2241 return arg1 .. arg2 .. join(rest, '-')
2242enddef
2243
Bram Moolenaar2c330432020-04-13 14:41:35 +02002244def MultiLineComment(
2245 arg1: string, # comment
2246 arg2 = 1234, # comment
2247 ...rest: list<string> # comment
2248 ): string # comment
2249 return arg1 .. arg2 .. join(rest, '-')
2250enddef
2251
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002252def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002253 MultiLine('text')->assert_equal('text1234')
2254 MultiLine('text', 777)->assert_equal('text777')
2255 MultiLine('text', 777, 'one')->assert_equal('text777one')
2256 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002257enddef
2258
Bram Moolenaar23e03252020-04-12 22:22:31 +02002259func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002260 call MultiLine('text')->assert_equal('text1234')
2261 call MultiLine('text', 777)->assert_equal('text777')
2262 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2263 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002264endfunc
2265
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002266
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002267" When using CheckScriptFailure() for the below test, E1010 is generated instead
2268" of E1056.
2269func Test_E1056_1059()
2270 let caught_1056 = 0
2271 try
2272 def F():
2273 return 1
2274 enddef
2275 catch /E1056:/
2276 let caught_1056 = 1
2277 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002278 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002279
2280 let caught_1059 = 0
2281 try
2282 def F5(items : list)
2283 echo 'a'
2284 enddef
2285 catch /E1059:/
2286 let caught_1059 = 1
2287 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002288 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002289endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002290
Bram Moolenaar015f4262020-05-05 21:25:22 +02002291func DelMe()
2292 echo 'DelMe'
2293endfunc
2294
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002295def Test_error_reporting()
2296 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002297 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002298 " comment
2299 def Func()
2300 # comment
2301 # comment
2302 invalid
2303 enddef
2304 defcompile
2305 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002306 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002307 try
2308 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002309 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002310 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002311 v:exception->assert_match('Invalid command: invalid')
2312 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002313 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002314 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002315
2316 # comment lines after the start of the function
2317 lines =<< trim END
2318 " comment
2319 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002320 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002321 # comment
2322 # comment
2323 invalid
2324 enddef
2325 defcompile
2326 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002327 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002328 try
2329 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002330 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002331 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002332 v:exception->assert_match('Invalid command: invalid')
2333 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002334 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002335 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002336
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002337 lines =<< trim END
2338 vim9script
2339 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002340 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002341 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002342 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002343 enddef
2344 defcompile
2345 Func()
2346 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002347 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002348 try
2349 source Xdef
2350 assert_report('should have failed')
2351 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002352 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002353 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002354 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002355
Bram Moolenaar08052222020-09-14 17:04:31 +02002356 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002357enddef
2358
Bram Moolenaar015f4262020-05-05 21:25:22 +02002359def Test_deleted_function()
2360 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002361 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002362 'delfunc g:DelMe',
2363 'echo RefMe()'], 'E117:')
2364enddef
2365
2366def Test_unknown_function()
2367 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002368 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002369 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002370enddef
2371
Bram Moolenaar328eac22021-01-07 19:23:08 +01002372def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002373 return Ref('more')
2374enddef
2375
2376def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002377 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002378 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002379enddef
2380
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002381def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002382 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002383 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002384enddef
2385
2386def Test_closure_ref_after_return()
2387 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002388 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002389 unlet g:Ref
2390enddef
2391
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002392def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002393 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002394 g:Extend = (s) => local->add(s)
2395 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002396enddef
2397
2398def Test_closure_two_refs()
2399 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002400 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002401 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002402 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002403 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002404 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002405
2406 unlet g:Extend
2407 unlet g:Read
2408enddef
2409
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002410def ReadRef(Ref: func(): list<string>): string
2411 return join(Ref(), ' ')
2412enddef
2413
Bram Moolenaar5e654232020-09-16 15:22:00 +02002414def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002415 Ref(add)
2416enddef
2417
2418def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002419 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002420 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002421 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002422 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002423 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002424 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002425
2426 unlet g:Extend
2427 unlet g:Read
2428enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002429
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002430def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002431 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002432 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002433enddef
2434
2435def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002436 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002437 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002438enddef
2439
2440def Test_closure_using_argument()
2441 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002442 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002443
2444 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002445 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002446
2447 unlet g:UseArg
2448 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002449
2450 var lines =<< trim END
2451 vim9script
2452 def Test(Fun: func(number): number): list<number>
2453 return map([1, 2, 3], (_, i) => Fun(i))
2454 enddef
2455 def Inc(nr: number): number
2456 return nr + 2
2457 enddef
2458 assert_equal([3, 4, 5], Test(Inc))
2459 END
2460 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002461enddef
2462
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002463def MakeGetAndAppendRefs()
2464 var local = 'a'
2465
2466 def Append(arg: string)
2467 local ..= arg
2468 enddef
2469 g:Append = Append
2470
2471 def Get(): string
2472 return local
2473 enddef
2474 g:Get = Get
2475enddef
2476
2477def Test_closure_append_get()
2478 MakeGetAndAppendRefs()
2479 g:Get()->assert_equal('a')
2480 g:Append('-b')
2481 g:Get()->assert_equal('a-b')
2482 g:Append('-c')
2483 g:Get()->assert_equal('a-b-c')
2484
2485 unlet g:Append
2486 unlet g:Get
2487enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002488
Bram Moolenaar04b12692020-05-04 23:24:44 +02002489def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002490 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002491 def Closure(arg: string): string
2492 return local .. arg
2493 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002494 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002495enddef
2496
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002497func GetResult(Ref)
2498 return a:Ref('some')
2499endfunc
2500
2501def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002502 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002503 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002504 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002505enddef
2506
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002507def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002508 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002509 vim9script
2510 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002511 var name = 0
2512 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002513 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002514 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002515 enddef
2516 Func()
2517 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002518 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002519enddef
2520
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002521def Test_nested_closure_used()
2522 var lines =<< trim END
2523 vim9script
2524 def Func()
2525 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002526 var Closure = () => x
2527 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002528 enddef
2529 Func()
2530 assert_equal('hello', g:Myclosure())
2531 END
2532 CheckScriptSuccess(lines)
2533enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002534
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002535def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002536 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002537 vim9script
2538 def FuncA()
2539 FuncB(0)
2540 enddef
2541 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002542 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002543 enddef
2544 FuncA()
2545 END
2546 CheckScriptFailure(lines, 'E1012:')
2547enddef
2548
Bram Moolenaarf112f302020-12-20 17:47:52 +01002549def Test_global_closure()
2550 var lines =<< trim END
2551 vim9script
2552 def ReverseEveryNLines(n: number, line1: number, line2: number)
2553 var mods = 'sil keepj keepp lockm '
2554 var range = ':' .. line1 .. ',' .. line2
2555 def g:Offset(): number
2556 var offset = (line('.') - line1 + 1) % n
2557 return offset != 0 ? offset : n
2558 enddef
2559 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2560 enddef
2561
2562 new
2563 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2564 ReverseEveryNLines(3, 1, 9)
2565 END
2566 CheckScriptSuccess(lines)
2567 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2568 assert_equal(expected, getline(1, 9))
2569 bwipe!
2570enddef
2571
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002572def Test_global_closure_called_directly()
2573 var lines =<< trim END
2574 vim9script
2575 def Outer()
2576 var x = 1
2577 def g:Inner()
2578 var y = x
2579 x += 1
2580 assert_equal(1, y)
2581 enddef
2582 g:Inner()
2583 assert_equal(2, x)
2584 enddef
2585 Outer()
2586 END
2587 CheckScriptSuccess(lines)
2588 delfunc g:Inner
2589enddef
2590
Bram Moolenaar69c76172021-12-02 16:38:52 +00002591def Test_closure_called_from_legacy()
2592 var lines =<< trim END
2593 vim9script
2594 def Func()
2595 var outer = 'foo'
2596 var F = () => {
2597 outer = 'bar'
2598 }
2599 execute printf('call %s()', string(F))
2600 enddef
2601 Func()
2602 END
2603 CheckScriptFailure(lines, 'E1248')
2604enddef
2605
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002606def Test_failure_in_called_function()
2607 # this was using the frame index as the return value
2608 var lines =<< trim END
2609 vim9script
2610 au TerminalWinOpen * eval [][0]
2611 def PopupTerm(a: any)
2612 # make sure typvals on stack are string
2613 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2614 FireEvent()
2615 enddef
2616 def FireEvent()
2617 do TerminalWinOpen
2618 enddef
2619 # use try/catch to make eval fail
2620 try
2621 call PopupTerm(0)
2622 catch
2623 endtry
2624 au! TerminalWinOpen
2625 END
2626 CheckScriptSuccess(lines)
2627enddef
2628
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002629def Test_nested_lambda()
2630 var lines =<< trim END
2631 vim9script
2632 def Func()
2633 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002634 var Lambda1 = () => 7
2635 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002636 var res = Lambda2()
2637 assert_equal([7, 4], res)
2638 enddef
2639 Func()
2640 END
2641 CheckScriptSuccess(lines)
2642enddef
2643
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002644def Test_double_nested_lambda()
2645 var lines =<< trim END
2646 vim9script
2647 def F(head: string): func(string): func(string): string
2648 return (sep: string): func(string): string => ((tail: string): string => {
2649 return head .. sep .. tail
2650 })
2651 enddef
2652 assert_equal('hello-there', F('hello')('-')('there'))
2653 END
2654 CheckScriptSuccess(lines)
2655enddef
2656
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002657def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002658 var lines =<< trim END
2659 vim9script
2660 def F(text: string): func(string): func(string): string
2661 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002662 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002663 })
2664 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002665 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002666 END
2667 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002668
2669 lines =<< trim END
2670 vim9script
2671 echo range(4)->mapnew((_, v) => {
2672 return range(v) ->mapnew((_, s) => {
2673 return string(s)
2674 })
2675 })
2676 END
2677 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002678
2679 lines =<< trim END
2680 vim9script
2681
2682 def s:func()
2683 range(10)
2684 ->mapnew((_, _) => ({
2685 key: range(10)->mapnew((_, _) => {
2686 return ' '
2687 }),
2688 }))
2689 enddef
2690
2691 defcomp
2692 END
2693 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002694enddef
2695
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002696def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002697 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002698 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002699enddef
2700
2701def Test_lambda_arg_shadows_func()
2702 assert_equal([42], Shadowed())
2703enddef
2704
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002705def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002706 var path: string = empty(dir)
2707 \ ? 'empty'
2708 \ : 'full'
2709 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002710enddef
2711
2712def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002713 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002714enddef
2715
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002716def Test_script_var_in_lambda()
2717 var lines =<< trim END
2718 vim9script
2719 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002720 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002721 END
2722 CheckScriptSuccess(lines)
2723enddef
2724
Bram Moolenaar5e654232020-09-16 15:22:00 +02002725def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002726 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002727 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002728 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002729 ->reverse()
2730 return x
2731enddef
2732
2733def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002734 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002735
2736 var lines =<< trim END
2737 vim9script
2738 var res = [{n: 1, m: 2, s: 'xxx'}]
2739 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2740 v.n,
2741 v.m,
2742 substitute(v.s, '.*', 'yyy', '')
2743 ))
2744 assert_equal(['1:2:yyy'], res)
2745 END
2746 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002747enddef
2748
Bram Moolenaarb6571982021-01-08 22:24:19 +01002749def Test_list_lambda()
2750 timer_start(1000, (_) => 0)
2751 var body = execute(timer_info()[0].callback
2752 ->string()
2753 ->substitute("('", ' ', '')
2754 ->substitute("')", '', '')
2755 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002756 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002757enddef
2758
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002759def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002760 var lines =<< trim END
2761 vim9script
2762 var flist: list<func>
2763 for i in range(10)
2764 var inloop = i
2765 flist[i] = () => inloop
2766 endfor
2767 END
2768 CheckScriptSuccess(lines)
2769
2770 lines =<< trim END
2771 vim9script
2772 if true
2773 var outloop = 5
2774 var flist: list<func>
2775 for i in range(10)
2776 flist[i] = () => outloop
2777 endfor
2778 endif
2779 END
2780 CheckScriptSuccess(lines)
2781
2782 lines =<< trim END
2783 vim9script
2784 if true
2785 var outloop = 5
2786 endif
2787 var flist: list<func>
2788 for i in range(10)
2789 flist[i] = () => outloop
2790 endfor
2791 END
2792 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002793
2794 lines =<< trim END
2795 vim9script
2796 for i in range(10)
2797 var Ref = () => 0
2798 endfor
2799 assert_equal(0, ((i) => 0)(0))
2800 END
2801 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002802enddef
2803
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002804def Test_legacy_lambda()
2805 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002806
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002807 var lines =<< trim END
2808 echo {x -> 'hello ' .. x}('foo')
2809 END
2810 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002811
2812 lines =<< trim END
2813 vim9script
2814 def Func()
2815 echo (() => 'no error')()
2816 enddef
2817 legacy call s:Func()
2818 END
2819 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002820enddef
2821
Bram Moolenaarce024c32021-06-26 13:00:49 +02002822def Test_legacy()
2823 var lines =<< trim END
2824 vim9script
2825 func g:LegacyFunction()
2826 let g:legacyvar = 1
2827 endfunc
2828 def Testit()
2829 legacy call g:LegacyFunction()
2830 enddef
2831 Testit()
2832 assert_equal(1, g:legacyvar)
2833 unlet g:legacyvar
2834 delfunc g:LegacyFunction
2835 END
2836 CheckScriptSuccess(lines)
2837enddef
2838
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002839def Test_legacy_errors()
2840 for cmd in ['if', 'elseif', 'else', 'endif',
2841 'for', 'endfor', 'continue', 'break',
2842 'while', 'endwhile',
2843 'try', 'catch', 'finally', 'endtry']
2844 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2845 endfor
2846enddef
2847
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002848def Test_call_legacy_with_dict()
2849 var lines =<< trim END
2850 vim9script
2851 func Legacy() dict
2852 let g:result = self.value
2853 endfunc
2854 def TestDirect()
2855 var d = {value: 'yes', func: Legacy}
2856 d.func()
2857 enddef
2858 TestDirect()
2859 assert_equal('yes', g:result)
2860 unlet g:result
2861
2862 def TestIndirect()
2863 var d = {value: 'foo', func: Legacy}
2864 var Fi = d.func
2865 Fi()
2866 enddef
2867 TestIndirect()
2868 assert_equal('foo', g:result)
2869 unlet g:result
2870
2871 var d = {value: 'bar', func: Legacy}
2872 d.func()
2873 assert_equal('bar', g:result)
2874 unlet g:result
2875 END
2876 CheckScriptSuccess(lines)
2877enddef
2878
Bram Moolenaarab360522021-01-10 14:02:28 +01002879def DoFilterThis(a: string): list<string>
2880 # closure nested inside another closure using argument
2881 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2882 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2883enddef
2884
2885def Test_nested_closure_using_argument()
2886 assert_equal(['x', 'x2'], DoFilterThis('x'))
2887enddef
2888
Bram Moolenaar0186e582021-01-10 18:33:11 +01002889def Test_triple_nested_closure()
2890 var what = 'x'
2891 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2892 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2893 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2894enddef
2895
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002896func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002897 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002898 call Run_Test_silent_echo()
2899endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002900
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002901def Run_Test_silent_echo()
2902 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002903 vim9script
2904 def EchoNothing()
2905 silent echo ''
2906 enddef
2907 defcompile
2908 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002909 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002910
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002911 # Check that the balloon shows up after a mouse move
2912 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2913 term_sendkeys(buf, ":abc")
2914 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002915
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002916 # clean up
2917 StopVimInTerminal(buf)
2918 delete('XTest_silent_echo')
2919enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002920
Bram Moolenaar171fb922020-10-28 16:54:47 +01002921def SilentlyError()
2922 execute('silent! invalid')
2923 g:did_it = 'yes'
2924enddef
2925
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002926func UserError()
2927 silent! invalid
2928endfunc
2929
2930def SilentlyUserError()
2931 UserError()
2932 g:did_it = 'yes'
2933enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002934
2935" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002936func Test_ignore_silent_error()
2937 let g:did_it = 'no'
2938 call SilentlyError()
2939 call assert_equal('yes', g:did_it)
2940
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002941 let g:did_it = 'no'
2942 call SilentlyUserError()
2943 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002944
2945 unlet g:did_it
2946endfunc
2947
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002948def Test_ignore_silent_error_in_filter()
2949 var lines =<< trim END
2950 vim9script
2951 def Filter(winid: number, key: string): bool
2952 if key == 'o'
2953 silent! eval [][0]
2954 return true
2955 endif
2956 return popup_filter_menu(winid, key)
2957 enddef
2958
Bram Moolenaare0de1712020-12-02 17:36:54 +01002959 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002960 feedkeys("o\r", 'xnt')
2961 END
2962 CheckScriptSuccess(lines)
2963enddef
2964
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002965def Fibonacci(n: number): number
2966 if n < 2
2967 return n
2968 else
2969 return Fibonacci(n - 1) + Fibonacci(n - 2)
2970 endif
2971enddef
2972
Bram Moolenaar985116a2020-07-12 17:31:09 +02002973def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002974 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002975enddef
2976
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002977def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002978 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002979 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002980 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002981 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002982 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002983enddef
2984
2985def Test_closure_in_map()
2986 mkdir('XclosureDir/tdir', 'p')
2987 writefile(['111'], 'XclosureDir/file1')
2988 writefile(['222'], 'XclosureDir/file2')
2989 writefile(['333'], 'XclosureDir/tdir/file3')
2990
Bram Moolenaare0de1712020-12-02 17:36:54 +01002991 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002992
2993 delete('XclosureDir', 'rf')
2994enddef
2995
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002996def Test_invalid_function_name()
2997 var lines =<< trim END
2998 vim9script
2999 def s: list<string>
3000 END
3001 CheckScriptFailure(lines, 'E129:')
3002
3003 lines =<< trim END
3004 vim9script
3005 def g: list<string>
3006 END
3007 CheckScriptFailure(lines, 'E129:')
3008
3009 lines =<< trim END
3010 vim9script
3011 def <SID>: list<string>
3012 END
3013 CheckScriptFailure(lines, 'E884:')
3014
3015 lines =<< trim END
3016 vim9script
3017 def F list<string>
3018 END
3019 CheckScriptFailure(lines, 'E488:')
3020enddef
3021
Bram Moolenaara90afb92020-07-15 22:38:56 +02003022def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003023 var lines =<< trim END
3024 var Xsetlist: func
3025 Xsetlist = function('setloclist', [0])
3026 Xsetlist([], ' ', {title: 'test'})
3027 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003028
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003029 Xsetlist = function('setloclist', [0, [], ' '])
3030 Xsetlist({title: 'test'})
3031 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003032
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003033 Xsetlist = function('setqflist')
3034 Xsetlist([], ' ', {title: 'test'})
3035 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02003036
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003037 Xsetlist = function('setqflist', [[], ' '])
3038 Xsetlist({title: 'test'})
3039 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02003040
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02003041 var Len: func: number = function('len', ['word'])
3042 assert_equal(4, Len())
3043
3044 var RepeatFunc = function('repeat', ['o'])
3045 assert_equal('ooooo', RepeatFunc(5))
3046 END
3047 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003048
3049 lines =<< trim END
3050 vim9script
3051 def Foo(Parser: any)
3052 enddef
3053 var Expr: func(dict<any>): dict<any>
3054 const Call = Foo(Expr)
3055 END
3056 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003057enddef
3058
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003059def Test_cmd_modifier()
3060 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003061 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003062enddef
3063
3064def Test_restore_modifiers()
3065 # check that when compiling a :def function command modifiers are not messed
3066 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003067 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003068 vim9script
3069 set eventignore=
3070 autocmd QuickFixCmdPost * copen
3071 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003072 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003073 enddef
3074 func Func()
3075 noautocmd call s:AutocmdsDisabled()
3076 let g:ei_after = &eventignore
3077 endfunc
3078 Func()
3079 END
3080 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003081 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003082enddef
3083
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003084def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003085 eval 1 + 2
3086 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003087 # call not on fourth line
3088 StackBot()
3089enddef
3090
3091def StackBot()
3092 # throw an error
3093 eval [][0]
3094enddef
3095
3096def Test_callstack_def()
3097 try
3098 StackTop()
3099 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003100 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003101 endtry
3102enddef
3103
Bram Moolenaare8211a32020-10-09 22:04:29 +02003104" Re-using spot for variable used in block
3105def Test_block_scoped_var()
3106 var lines =<< trim END
3107 vim9script
3108 def Func()
3109 var x = ['a', 'b', 'c']
3110 if 1
3111 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003112 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003113 endif
3114 var z = x
3115 assert_equal(['x', 'x', 'x'], z)
3116 enddef
3117 Func()
3118 END
3119 CheckScriptSuccess(lines)
3120enddef
3121
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003122def Test_reset_did_emsg()
3123 var lines =<< trim END
3124 @s = 'blah'
3125 au BufWinLeave * #
3126 def Func()
3127 var winid = popup_create('popup', {})
3128 exe '*s'
3129 popup_close(winid)
3130 enddef
3131 Func()
3132 END
3133 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003134 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003135enddef
3136
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003137def Test_did_emsg_reset()
3138 # executing an autocommand resets did_emsg, this should not result in a
3139 # builtin function considered failing
3140 var lines =<< trim END
3141 vim9script
3142 au BufWinLeave * #
3143 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003144 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003145 eval [][0]
3146 enddef
3147 nno <F3> <cmd>call <sid>Func()<cr>
3148 feedkeys("\<F3>\e", 'xt')
3149 END
3150 writefile(lines, 'XemsgReset')
3151 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3152 delete('XemsgReset')
3153 nunmap <F3>
3154 au! BufWinLeave
3155enddef
3156
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003157def Test_abort_with_silent_call()
3158 var lines =<< trim END
3159 vim9script
3160 g:result = 'none'
3161 def Func()
3162 g:result += 3
3163 g:result = 'yes'
3164 enddef
3165 # error is silenced, but function aborts on error
3166 silent! Func()
3167 assert_equal('none', g:result)
3168 unlet g:result
3169 END
3170 CheckScriptSuccess(lines)
3171enddef
3172
Bram Moolenaarf665e972020-12-05 19:17:16 +01003173def Test_continues_with_silent_error()
3174 var lines =<< trim END
3175 vim9script
3176 g:result = 'none'
3177 def Func()
3178 silent! g:result += 3
3179 g:result = 'yes'
3180 enddef
3181 # error is silenced, function does not abort
3182 Func()
3183 assert_equal('yes', g:result)
3184 unlet g:result
3185 END
3186 CheckScriptSuccess(lines)
3187enddef
3188
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003189def Test_abort_even_with_silent()
3190 var lines =<< trim END
3191 vim9script
3192 g:result = 'none'
3193 def Func()
3194 eval {-> ''}() .. '' .. {}['X']
3195 g:result = 'yes'
3196 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003197 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003198 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003199 unlet g:result
3200 END
3201 CheckScriptSuccess(lines)
3202enddef
3203
Bram Moolenaarf665e972020-12-05 19:17:16 +01003204def Test_cmdmod_silent_restored()
3205 var lines =<< trim END
3206 vim9script
3207 def Func()
3208 g:result = 'none'
3209 silent! g:result += 3
3210 g:result = 'none'
3211 g:result += 3
3212 enddef
3213 Func()
3214 END
3215 # can't use CheckScriptFailure, it ignores the :silent!
3216 var fname = 'Xdefsilent'
3217 writefile(lines, fname)
3218 var caught = 'no'
3219 try
3220 exe 'source ' .. fname
3221 catch /E1030:/
3222 caught = 'yes'
3223 assert_match('Func, line 4', v:throwpoint)
3224 endtry
3225 assert_equal('yes', caught)
3226 delete(fname)
3227enddef
3228
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003229def Test_cmdmod_silent_nested()
3230 var lines =<< trim END
3231 vim9script
3232 var result = ''
3233
3234 def Error()
3235 result ..= 'Eb'
3236 eval [][0]
3237 result ..= 'Ea'
3238 enddef
3239
3240 def Crash()
3241 result ..= 'Cb'
3242 sil! Error()
3243 result ..= 'Ca'
3244 enddef
3245
3246 Crash()
3247 assert_equal('CbEbEaCa', result)
3248 END
3249 CheckScriptSuccess(lines)
3250enddef
3251
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003252def Test_dict_member_with_silent()
3253 var lines =<< trim END
3254 vim9script
3255 g:result = 'none'
3256 var d: dict<any>
3257 def Func()
3258 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003259 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003260 catch
3261 endtry
3262 enddef
3263 silent! Func()
3264 assert_equal('0', g:result)
3265 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003266 END
3267 CheckScriptSuccess(lines)
3268enddef
3269
Bram Moolenaarf9041332021-01-21 19:41:16 +01003270def Test_skip_cmds_with_silent()
3271 var lines =<< trim END
3272 vim9script
3273
3274 def Func(b: bool)
3275 Crash()
3276 enddef
3277
3278 def Crash()
3279 sil! :/not found/d _
3280 sil! :/not found/put _
3281 enddef
3282
3283 Func(true)
3284 END
3285 CheckScriptSuccess(lines)
3286enddef
3287
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003288def Test_opfunc()
3289 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3290 def g:Opfunc(_: any): string
3291 setline(1, 'ASDF')
3292 return ''
3293 enddef
3294 new
3295 setline(1, 'asdf')
3296 feedkeys("\<F3>$", 'x')
3297 assert_equal('ASDF', getline(1))
3298
3299 bwipe!
3300 nunmap <F3>
3301enddef
3302
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003303func Test_opfunc_error()
3304 CheckScreendump
3305 call Run_Test_opfunc_error()
3306endfunc
3307
3308def Run_Test_opfunc_error()
3309 # test that the error from Opfunc() is displayed right away
3310 var lines =<< trim END
3311 vim9script
3312
3313 def Opfunc(type: string)
3314 try
3315 eval [][0]
3316 catch /nothing/ # error not caught
3317 endtry
3318 enddef
3319 &operatorfunc = Opfunc
3320 nnoremap <expr> l <SID>L()
3321 def L(): string
3322 return 'l'
3323 enddef
3324 'x'->repeat(10)->setline(1)
3325 feedkeys('g@l', 'n')
3326 feedkeys('llll')
3327 END
3328 call writefile(lines, 'XTest_opfunc_error')
3329
3330 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003331 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003332 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003333
3334 # clean up
3335 StopVimInTerminal(buf)
3336 delete('XTest_opfunc_error')
3337enddef
3338
Bram Moolenaar077a4232020-12-22 18:33:27 +01003339" this was crashing on exit
3340def Test_nested_lambda_in_closure()
3341 var lines =<< trim END
3342 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003343 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003344 def Outer()
3345 def g:Inner()
3346 echo map([1, 2, 3], {_, v -> v + 1})
3347 enddef
3348 g:Inner()
3349 enddef
3350 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003351 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003352 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003353 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003354 return
3355 endif
3356 assert_equal(['Done'], readfile('XnestedDone'))
3357 delete('XnestedDone')
3358enddef
3359
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003360def Test_check_func_arg_types()
3361 var lines =<< trim END
3362 vim9script
3363 def F1(x: string): string
3364 return x
3365 enddef
3366
3367 def F2(x: number): number
3368 return x + 1
3369 enddef
3370
3371 def G(g: func): dict<func>
3372 return {f: g}
3373 enddef
3374
3375 def H(d: dict<func>): string
3376 return d.f('a')
3377 enddef
3378 END
3379
3380 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3381 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3382enddef
3383
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003384def Test_list_any_type_checked()
3385 var lines =<< trim END
3386 vim9script
3387 def Foo()
3388 --decl--
3389 Bar(l)
3390 enddef
3391 def Bar(ll: list<dict<any>>)
3392 enddef
3393 Foo()
3394 END
3395 lines[2] = 'var l: list<any>'
3396 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3397
3398 lines[2] = 'var l: list<any> = []'
3399 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3400
3401 lines[2] = 'var l: list<any> = [11]'
3402 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3403enddef
3404
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003405def Test_compile_error()
3406 var lines =<< trim END
3407 def g:Broken()
3408 echo 'a' + {}
3409 enddef
3410 call g:Broken()
3411 END
3412 # First call: compilation error
3413 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3414
3415 # Second call won't try compiling again
3416 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003417 delfunc g:Broken
3418
3419 # No error when compiling with :silent!
3420 lines =<< trim END
3421 def g:Broken()
3422 echo 'a' + []
3423 enddef
3424 silent! defcompile
3425 END
3426 CheckScriptSuccess(lines)
3427
3428 # Calling the function won't try compiling again
3429 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3430 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003431enddef
3432
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003433def Test_ignored_argument()
3434 var lines =<< trim END
3435 vim9script
3436 def Ignore(_, _): string
3437 return 'yes'
3438 enddef
3439 assert_equal('yes', Ignore(1, 2))
3440
3441 func Ok(_)
3442 return a:_
3443 endfunc
3444 assert_equal('ok', Ok('ok'))
3445
3446 func Oktoo()
3447 let _ = 'too'
3448 return _
3449 endfunc
3450 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003451
3452 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003453 END
3454 CheckScriptSuccess(lines)
3455
3456 lines =<< trim END
3457 def Ignore(_: string): string
3458 return _
3459 enddef
3460 defcompile
3461 END
3462 CheckScriptFailure(lines, 'E1181:', 1)
3463
3464 lines =<< trim END
3465 var _ = 1
3466 END
3467 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003468
3469 lines =<< trim END
3470 var x = _
3471 END
3472 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003473enddef
3474
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003475def Test_too_many_arguments()
3476 var lines =<< trim END
3477 echo [0, 1, 2]->map(() => 123)
3478 END
3479 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3480
3481 lines =<< trim END
3482 echo [0, 1, 2]->map((_) => 123)
3483 END
3484 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3485enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003486
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003487def Test_closing_brace_at_start_of_line()
3488 var lines =<< trim END
3489 def Func()
3490 enddef
3491 Func(
3492 )
3493 END
3494 call CheckDefAndScriptSuccess(lines)
3495enddef
3496
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003497func CreateMydict()
3498 let g:mydict = {}
3499 func g:mydict.afunc()
3500 let g:result = self.key
3501 endfunc
3502endfunc
3503
3504def Test_numbered_function_reference()
3505 CreateMydict()
3506 var output = execute('legacy func g:mydict.afunc')
3507 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3508 execute 'function(' .. funcName .. ', [], {key: 42})()'
3509 # check that the function still exists
3510 assert_equal(output, execute('legacy func g:mydict.afunc'))
3511 unlet g:mydict
3512enddef
3513
Bram Moolenaar20677332021-06-06 17:02:53 +02003514if has('python3')
3515 def Test_python3_heredoc()
3516 py3 << trim EOF
3517 import vim
3518 vim.vars['didit'] = 'yes'
3519 EOF
3520 assert_equal('yes', g:didit)
3521
3522 python3 << trim EOF
3523 import vim
3524 vim.vars['didit'] = 'again'
3525 EOF
3526 assert_equal('again', g:didit)
3527 enddef
3528endif
3529
3530" This messes up syntax highlight, keep near the end.
3531if has('lua')
3532 def Test_lua_heredoc()
3533 g:d = {}
3534 lua << trim EOF
3535 x = vim.eval('g:d')
3536 x['key'] = 'val'
3537 EOF
3538 assert_equal('val', g:d.key)
3539 enddef
3540endif
3541
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003542
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003543" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker