blob: 5b8ad17c780cacd63c7b447b7e42b875a018f74f [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
1672def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001673 CheckScriptFailure([
1674 'def Func(): number',
1675 'return "a"',
1676 'enddef',
1677 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001678 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001679 CheckScriptFailure([
1680 'def Func(): string',
1681 'return 1',
1682 'enddef',
1683 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001684 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001685 CheckScriptFailure([
1686 'def Func(): void',
1687 'return "a"',
1688 'enddef',
1689 'defcompile'],
1690 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001691 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001692 CheckScriptFailure([
1693 'def Func()',
1694 'return "a"',
1695 'enddef',
1696 'defcompile'],
1697 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001698 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001699
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001700 CheckScriptFailure([
1701 'def Func(): number',
1702 'return',
1703 'enddef',
1704 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001705 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001706
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001707 CheckScriptFailure([
1708 'def Func():number',
1709 'return 123',
1710 'enddef',
1711 'defcompile'], 'E1069:')
1712 delfunc! g:Func
1713
1714 CheckScriptFailure([
1715 'def Func() :number',
1716 'return 123',
1717 'enddef',
1718 'defcompile'], 'E1059:')
1719 delfunc! g:Func
1720
1721 CheckScriptFailure([
1722 'def Func() : number',
1723 'return 123',
1724 'enddef',
1725 'defcompile'], 'E1059:')
1726 delfunc! g:Func
1727
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001728 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001729 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001730 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001731 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001732 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001733 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001734
1735 CheckScriptFailure([
1736 'vim9script',
1737 'def FuncB()',
1738 ' return 123',
1739 'enddef',
1740 'def FuncA()',
1741 ' FuncB()',
1742 'enddef',
1743 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001744enddef
1745
1746def Test_arg_type_wrong()
1747 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001748 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001749 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001750 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001751 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1752 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001753enddef
1754
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001755def Test_white_space_before_comma()
1756 var lines =<< trim END
1757 vim9script
1758 def Func(a: number , b: number)
1759 enddef
1760 END
1761 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001762 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001763enddef
1764
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001765def Test_white_space_after_comma()
1766 var lines =<< trim END
1767 vim9script
1768 def Func(a: number,b: number)
1769 enddef
1770 END
1771 CheckScriptFailure(lines, 'E1069:')
1772
1773 # OK in legacy function
1774 lines =<< trim END
1775 vim9script
1776 func Func(a,b)
1777 endfunc
1778 END
1779 CheckScriptSuccess(lines)
1780enddef
1781
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001782def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001783 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001784 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001785 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001786 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001787 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001788 enddef
1789 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001790 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001792 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001793 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001794 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001795
Bram Moolenaar67979662020-06-20 22:50:47 +02001796 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001797 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001798 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001799
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001800 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001801 def ListFunc(arg: list<number>)
1802 listvar = arg
1803 enddef
1804 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001805 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001806
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001807 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001808 def DictFunc(arg: dict<number>)
1809 dictvar = arg
1810 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001811 {a: 1, b: 2}->DictFunc()
1812 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001813 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001814 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001815 enddef
1816 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001817 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001818
Bram Moolenaare0de1712020-12-02 17:36:54 +01001819 {a: 3, b: 4}->DictFunc()
1820 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001821
1822 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001823 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001824 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001825 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001826
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001827 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001828 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001829 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001830 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001831 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001832 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001833
1834 def UseString()
1835 'xyork'->MyFunc()
1836 enddef
1837 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001838 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001839
Bram Moolenaar10409562020-07-29 20:00:38 +02001840 def UseString2()
1841 "knife"->MyFunc()
1842 enddef
1843 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001844 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001845
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001846 # prepending a colon makes it a mark
1847 new
1848 setline(1, ['aaa', 'bbb', 'ccc'])
1849 normal! 3Gmt1G
1850 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001851 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001852 bwipe!
1853
Bram Moolenaare6b53242020-07-01 17:28:33 +02001854 MyFunc(
1855 'continued'
1856 )
1857 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001858 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001859 )
1860
1861 call MyFunc(
1862 'more'
1863 ..
1864 'lines'
1865 )
1866 assert_equal(
1867 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001868 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001869 END
1870 writefile(lines, 'Xcall.vim')
1871 source Xcall.vim
1872 delete('Xcall.vim')
1873enddef
1874
1875def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001876 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001877 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001878 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001879 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001880 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001881 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001882 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001883 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001884 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001885enddef
1886
Bram Moolenaar65b95452020-07-19 14:03:09 +02001887def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001888 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001889 vim9script
1890 def MyFunc(arg: string)
1891 echo arg
1892 enddef
1893 MyFunc(1234)
1894 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001895 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001896enddef
1897
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001898def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001899 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001900 vim9script
1901 const var = ''
1902 def MyFunc(arg: string)
1903 var = 'asdf'
1904 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001905 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001906 END
1907 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001908 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001909 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001910
1911 lines =<< trim END
1912 const g:Aconst = 77
1913 def Change()
1914 # comment
1915 g:Aconst = 99
1916 enddef
1917 call Change()
1918 unlet g:Aconst
1919 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001920 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001921enddef
1922
1923" Test that inside :function a Python function can be defined, :def is not
1924" recognized.
1925func Test_function_python()
1926 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001927 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001928 execute py "<< EOF"
1929def do_something():
1930 return 1
1931EOF
1932endfunc
1933
1934def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001935 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001936 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001937 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001938 echo 'hello'
1939 enddef
1940
1941 def CallGoneSoon()
1942 GoneSoon()
1943 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001944 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001945
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001946 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001947 CallGoneSoon()
1948 END
1949 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001950 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1951 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001952
1953 delete('XToDelFunc')
1954enddef
1955
Bram Moolenaar7509ad82021-12-14 18:14:37 +00001956func Test_free_dict_while_in_funcstack()
1957 " relies on the sleep command
1958 CheckUnix
1959 call Run_Test_free_dict_while_in_funcstack()
1960endfunc
1961
1962def Run_Test_free_dict_while_in_funcstack()
1963
1964 # this was freeing the TermRun() default argument dictionary while it was
1965 # still referenced in a funcstack_T
1966 var lines =<< trim END
1967 vim9script
1968
1969 &updatetime = 400
1970 def TermRun(_ = {})
1971 def Post()
1972 enddef
1973 def Exec()
1974 term_start('sleep 1', {
1975 term_finish: 'close',
1976 exit_cb: (_, _) => Post(),
1977 })
1978 enddef
1979 Exec()
1980 enddef
1981 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
1982 timer_start(100, (_) => feedkeys("\<F4>"))
1983 timer_start(1000, (_) => feedkeys("\<F4>"))
1984 sleep 1500m
1985 END
1986 CheckScriptSuccess(lines)
1987 nunmap <F4>
1988 set updatetime&
1989enddef
1990
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001991def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001992 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001993 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001994 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001995 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001996 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001997 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001998 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001999 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02002000 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002001
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002002 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002003 g:Func1()->assert_equal('Func1')
2004 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002005
2006 delfunc! Func0
2007 delfunc! Func1
2008 delfunc! Func2
2009enddef
2010
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002011def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002012 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002013 vim9script
2014 func Func(arg)
2015 echo a:arg
2016 endfunc
2017 Func('text')
2018 END
2019 writefile(lines, 'XVim9Func')
2020 so XVim9Func
2021
2022 delete('XVim9Func')
2023enddef
2024
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002025let s:funcResult = 0
2026
2027def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002028 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002029enddef
2030
2031def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002032 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002033 return 1234
2034enddef
2035
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002036def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002037 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002038 return 'text'
2039enddef
2040
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002041def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002042 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002043enddef
2044
2045def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002046 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002047 return arg
2048enddef
2049
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002050def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002051 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002052enddef
2053
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002054def FuncOneArgRetString(arg: string): string
2055 return arg
2056enddef
2057
Bram Moolenaar89228602020-04-05 22:14:54 +02002058def FuncOneArgRetAny(arg: any): any
2059 return arg
2060enddef
2061
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002062def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002063 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002064 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002065 Ref1 = FuncNoArgNoRet
2066 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002067 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002068
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002069 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002070 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002071 Ref2 = FuncNoArgNoRet
2072 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002073 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002074
Bram Moolenaar53900992020-08-22 19:02:02 +02002075 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002076 Ref2 = FuncOneArgNoRet
2077 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002078 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002079
Bram Moolenaar53900992020-08-22 19:02:02 +02002080 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002081 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002082 Ref2()->assert_equal(1234)
2083 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002084
Bram Moolenaar53900992020-08-22 19:02:02 +02002085 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002086 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002087 Ref2(13)->assert_equal(13)
2088 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002089enddef
2090
Bram Moolenaar9978d472020-07-05 16:01:56 +02002091def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002092 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002093 for n in repeat([1], 3)
2094 res += n
2095 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002096 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002097
2098 res = 0
2099 for n in add([1, 2], 3)
2100 res += n
2101 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002102 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002103enddef
2104
Bram Moolenaar846178a2020-07-05 17:04:13 +02002105def Test_argv_return_type()
2106 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002107 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002108 for name in argv()
2109 res ..= name
2110 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002111 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002112enddef
2113
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002114def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002115 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002116 RefVoid = FuncNoArgNoRet
2117 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002118 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2119 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002120
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002121 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002122 RefAny = FuncNoArgRetNumber
2123 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002124 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2125 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002126
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002127 var RefAnyNoArgs: func: any = RefAny
2128
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002129 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002130 RefNr = FuncNoArgRetNumber
2131 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002132 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2133 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002134
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002135 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002136 RefStr = FuncNoArgRetString
2137 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002138 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2139 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002140enddef
2141
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002142def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002143 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002144
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002145 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2146 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2147 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2148 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2149 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2150 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 +02002151
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002152 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2153 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2154 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:')
2155 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002156enddef
2157
Bram Moolenaar89228602020-04-05 22:14:54 +02002158def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002159 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002160 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002161 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002162
2163 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002164 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002165
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002166 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002167 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002168 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002169
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002170 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002171enddef
2172
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002173def Test_func_common_type()
2174 def FuncOne(n: number): number
2175 return n
2176 enddef
2177 def FuncTwo(s: string): number
2178 return len(s)
2179 enddef
2180 def FuncThree(n: number, s: string): number
2181 return n + len(s)
2182 enddef
2183 var list = [FuncOne, FuncTwo, FuncThree]
2184 assert_equal(8, list[0](8))
2185 assert_equal(4, list[1]('word'))
2186 assert_equal(7, list[2](3, 'word'))
2187enddef
2188
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002189def MultiLine(
2190 arg1: string,
2191 arg2 = 1234,
2192 ...rest: list<string>
2193 ): string
2194 return arg1 .. arg2 .. join(rest, '-')
2195enddef
2196
Bram Moolenaar2c330432020-04-13 14:41:35 +02002197def MultiLineComment(
2198 arg1: string, # comment
2199 arg2 = 1234, # comment
2200 ...rest: list<string> # comment
2201 ): string # comment
2202 return arg1 .. arg2 .. join(rest, '-')
2203enddef
2204
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002205def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002206 MultiLine('text')->assert_equal('text1234')
2207 MultiLine('text', 777)->assert_equal('text777')
2208 MultiLine('text', 777, 'one')->assert_equal('text777one')
2209 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002210enddef
2211
Bram Moolenaar23e03252020-04-12 22:22:31 +02002212func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002213 call MultiLine('text')->assert_equal('text1234')
2214 call MultiLine('text', 777)->assert_equal('text777')
2215 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2216 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002217endfunc
2218
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002219
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002220" When using CheckScriptFailure() for the below test, E1010 is generated instead
2221" of E1056.
2222func Test_E1056_1059()
2223 let caught_1056 = 0
2224 try
2225 def F():
2226 return 1
2227 enddef
2228 catch /E1056:/
2229 let caught_1056 = 1
2230 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002231 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002232
2233 let caught_1059 = 0
2234 try
2235 def F5(items : list)
2236 echo 'a'
2237 enddef
2238 catch /E1059:/
2239 let caught_1059 = 1
2240 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002241 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002242endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002243
Bram Moolenaar015f4262020-05-05 21:25:22 +02002244func DelMe()
2245 echo 'DelMe'
2246endfunc
2247
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002248def Test_error_reporting()
2249 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002250 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002251 " comment
2252 def Func()
2253 # comment
2254 # comment
2255 invalid
2256 enddef
2257 defcompile
2258 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002259 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002260 try
2261 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002262 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002263 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002264 v:exception->assert_match('Invalid command: invalid')
2265 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002266 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002267 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002268
2269 # comment lines after the start of the function
2270 lines =<< trim END
2271 " comment
2272 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002273 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002274 # comment
2275 # comment
2276 invalid
2277 enddef
2278 defcompile
2279 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002280 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002281 try
2282 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002283 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002284 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002285 v:exception->assert_match('Invalid command: invalid')
2286 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002287 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002288 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002289
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002290 lines =<< trim END
2291 vim9script
2292 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002293 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002294 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002295 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002296 enddef
2297 defcompile
2298 Func()
2299 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002300 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002301 try
2302 source Xdef
2303 assert_report('should have failed')
2304 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002305 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002306 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002307 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002308
Bram Moolenaar08052222020-09-14 17:04:31 +02002309 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002310enddef
2311
Bram Moolenaar015f4262020-05-05 21:25:22 +02002312def Test_deleted_function()
2313 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002314 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002315 'delfunc g:DelMe',
2316 'echo RefMe()'], 'E117:')
2317enddef
2318
2319def Test_unknown_function()
2320 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002321 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002322 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002323enddef
2324
Bram Moolenaar328eac22021-01-07 19:23:08 +01002325def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002326 return Ref('more')
2327enddef
2328
2329def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002330 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002331 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002332enddef
2333
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002334def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002335 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002336 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002337enddef
2338
2339def Test_closure_ref_after_return()
2340 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002341 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002342 unlet g:Ref
2343enddef
2344
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002345def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002346 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002347 g:Extend = (s) => local->add(s)
2348 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002349enddef
2350
2351def Test_closure_two_refs()
2352 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002353 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002354 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002355 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002356 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002357 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002358
2359 unlet g:Extend
2360 unlet g:Read
2361enddef
2362
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002363def ReadRef(Ref: func(): list<string>): string
2364 return join(Ref(), ' ')
2365enddef
2366
Bram Moolenaar5e654232020-09-16 15:22:00 +02002367def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002368 Ref(add)
2369enddef
2370
2371def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002372 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002373 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002374 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002375 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002376 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002377 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002378
2379 unlet g:Extend
2380 unlet g:Read
2381enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002382
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002383def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002384 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002385 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002386enddef
2387
2388def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002389 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002390 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002391enddef
2392
2393def Test_closure_using_argument()
2394 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002395 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002396
2397 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002398 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002399
2400 unlet g:UseArg
2401 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002402
2403 var lines =<< trim END
2404 vim9script
2405 def Test(Fun: func(number): number): list<number>
2406 return map([1, 2, 3], (_, i) => Fun(i))
2407 enddef
2408 def Inc(nr: number): number
2409 return nr + 2
2410 enddef
2411 assert_equal([3, 4, 5], Test(Inc))
2412 END
2413 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002414enddef
2415
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002416def MakeGetAndAppendRefs()
2417 var local = 'a'
2418
2419 def Append(arg: string)
2420 local ..= arg
2421 enddef
2422 g:Append = Append
2423
2424 def Get(): string
2425 return local
2426 enddef
2427 g:Get = Get
2428enddef
2429
2430def Test_closure_append_get()
2431 MakeGetAndAppendRefs()
2432 g:Get()->assert_equal('a')
2433 g:Append('-b')
2434 g:Get()->assert_equal('a-b')
2435 g:Append('-c')
2436 g:Get()->assert_equal('a-b-c')
2437
2438 unlet g:Append
2439 unlet g:Get
2440enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002441
Bram Moolenaar04b12692020-05-04 23:24:44 +02002442def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002443 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002444 def Closure(arg: string): string
2445 return local .. arg
2446 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002447 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002448enddef
2449
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002450func GetResult(Ref)
2451 return a:Ref('some')
2452endfunc
2453
2454def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002455 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002456 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002457 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002458enddef
2459
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002460def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002461 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002462 vim9script
2463 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002464 var name = 0
2465 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002466 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002467 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002468 enddef
2469 Func()
2470 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002471 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002472enddef
2473
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002474def Test_nested_closure_used()
2475 var lines =<< trim END
2476 vim9script
2477 def Func()
2478 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002479 var Closure = () => x
2480 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002481 enddef
2482 Func()
2483 assert_equal('hello', g:Myclosure())
2484 END
2485 CheckScriptSuccess(lines)
2486enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002487
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002488def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002489 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002490 vim9script
2491 def FuncA()
2492 FuncB(0)
2493 enddef
2494 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002495 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002496 enddef
2497 FuncA()
2498 END
2499 CheckScriptFailure(lines, 'E1012:')
2500enddef
2501
Bram Moolenaarf112f302020-12-20 17:47:52 +01002502def Test_global_closure()
2503 var lines =<< trim END
2504 vim9script
2505 def ReverseEveryNLines(n: number, line1: number, line2: number)
2506 var mods = 'sil keepj keepp lockm '
2507 var range = ':' .. line1 .. ',' .. line2
2508 def g:Offset(): number
2509 var offset = (line('.') - line1 + 1) % n
2510 return offset != 0 ? offset : n
2511 enddef
2512 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2513 enddef
2514
2515 new
2516 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2517 ReverseEveryNLines(3, 1, 9)
2518 END
2519 CheckScriptSuccess(lines)
2520 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2521 assert_equal(expected, getline(1, 9))
2522 bwipe!
2523enddef
2524
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002525def Test_global_closure_called_directly()
2526 var lines =<< trim END
2527 vim9script
2528 def Outer()
2529 var x = 1
2530 def g:Inner()
2531 var y = x
2532 x += 1
2533 assert_equal(1, y)
2534 enddef
2535 g:Inner()
2536 assert_equal(2, x)
2537 enddef
2538 Outer()
2539 END
2540 CheckScriptSuccess(lines)
2541 delfunc g:Inner
2542enddef
2543
Bram Moolenaar69c76172021-12-02 16:38:52 +00002544def Test_closure_called_from_legacy()
2545 var lines =<< trim END
2546 vim9script
2547 def Func()
2548 var outer = 'foo'
2549 var F = () => {
2550 outer = 'bar'
2551 }
2552 execute printf('call %s()', string(F))
2553 enddef
2554 Func()
2555 END
2556 CheckScriptFailure(lines, 'E1248')
2557enddef
2558
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002559def Test_failure_in_called_function()
2560 # this was using the frame index as the return value
2561 var lines =<< trim END
2562 vim9script
2563 au TerminalWinOpen * eval [][0]
2564 def PopupTerm(a: any)
2565 # make sure typvals on stack are string
2566 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2567 FireEvent()
2568 enddef
2569 def FireEvent()
2570 do TerminalWinOpen
2571 enddef
2572 # use try/catch to make eval fail
2573 try
2574 call PopupTerm(0)
2575 catch
2576 endtry
2577 au! TerminalWinOpen
2578 END
2579 CheckScriptSuccess(lines)
2580enddef
2581
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002582def Test_nested_lambda()
2583 var lines =<< trim END
2584 vim9script
2585 def Func()
2586 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002587 var Lambda1 = () => 7
2588 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002589 var res = Lambda2()
2590 assert_equal([7, 4], res)
2591 enddef
2592 Func()
2593 END
2594 CheckScriptSuccess(lines)
2595enddef
2596
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002597def Test_double_nested_lambda()
2598 var lines =<< trim END
2599 vim9script
2600 def F(head: string): func(string): func(string): string
2601 return (sep: string): func(string): string => ((tail: string): string => {
2602 return head .. sep .. tail
2603 })
2604 enddef
2605 assert_equal('hello-there', F('hello')('-')('there'))
2606 END
2607 CheckScriptSuccess(lines)
2608enddef
2609
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002610def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002611 var lines =<< trim END
2612 vim9script
2613 def F(text: string): func(string): func(string): string
2614 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002615 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002616 })
2617 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002618 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002619 END
2620 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002621
2622 lines =<< trim END
2623 vim9script
2624 echo range(4)->mapnew((_, v) => {
2625 return range(v) ->mapnew((_, s) => {
2626 return string(s)
2627 })
2628 })
2629 END
2630 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002631
2632 lines =<< trim END
2633 vim9script
2634
2635 def s:func()
2636 range(10)
2637 ->mapnew((_, _) => ({
2638 key: range(10)->mapnew((_, _) => {
2639 return ' '
2640 }),
2641 }))
2642 enddef
2643
2644 defcomp
2645 END
2646 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002647enddef
2648
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002649def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002650 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002651 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002652enddef
2653
2654def Test_lambda_arg_shadows_func()
2655 assert_equal([42], Shadowed())
2656enddef
2657
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002658def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002659 var path: string = empty(dir)
2660 \ ? 'empty'
2661 \ : 'full'
2662 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002663enddef
2664
2665def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002666 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002667enddef
2668
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002669def Test_script_var_in_lambda()
2670 var lines =<< trim END
2671 vim9script
2672 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002673 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002674 END
2675 CheckScriptSuccess(lines)
2676enddef
2677
Bram Moolenaar5e654232020-09-16 15:22:00 +02002678def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002679 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002680 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002681 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002682 ->reverse()
2683 return x
2684enddef
2685
2686def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002687 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002688
2689 var lines =<< trim END
2690 vim9script
2691 var res = [{n: 1, m: 2, s: 'xxx'}]
2692 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2693 v.n,
2694 v.m,
2695 substitute(v.s, '.*', 'yyy', '')
2696 ))
2697 assert_equal(['1:2:yyy'], res)
2698 END
2699 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002700enddef
2701
Bram Moolenaarb6571982021-01-08 22:24:19 +01002702def Test_list_lambda()
2703 timer_start(1000, (_) => 0)
2704 var body = execute(timer_info()[0].callback
2705 ->string()
2706 ->substitute("('", ' ', '')
2707 ->substitute("')", '', '')
2708 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002709 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002710enddef
2711
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002712def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002713 var lines =<< trim END
2714 vim9script
2715 var flist: list<func>
2716 for i in range(10)
2717 var inloop = i
2718 flist[i] = () => inloop
2719 endfor
2720 END
2721 CheckScriptSuccess(lines)
2722
2723 lines =<< trim END
2724 vim9script
2725 if true
2726 var outloop = 5
2727 var flist: list<func>
2728 for i in range(10)
2729 flist[i] = () => outloop
2730 endfor
2731 endif
2732 END
2733 CheckScriptSuccess(lines)
2734
2735 lines =<< trim END
2736 vim9script
2737 if true
2738 var outloop = 5
2739 endif
2740 var flist: list<func>
2741 for i in range(10)
2742 flist[i] = () => outloop
2743 endfor
2744 END
2745 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002746
2747 lines =<< trim END
2748 vim9script
2749 for i in range(10)
2750 var Ref = () => 0
2751 endfor
2752 assert_equal(0, ((i) => 0)(0))
2753 END
2754 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002755enddef
2756
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002757def Test_legacy_lambda()
2758 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002759
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002760 var lines =<< trim END
2761 echo {x -> 'hello ' .. x}('foo')
2762 END
2763 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002764
2765 lines =<< trim END
2766 vim9script
2767 def Func()
2768 echo (() => 'no error')()
2769 enddef
2770 legacy call s:Func()
2771 END
2772 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002773enddef
2774
Bram Moolenaarce024c32021-06-26 13:00:49 +02002775def Test_legacy()
2776 var lines =<< trim END
2777 vim9script
2778 func g:LegacyFunction()
2779 let g:legacyvar = 1
2780 endfunc
2781 def Testit()
2782 legacy call g:LegacyFunction()
2783 enddef
2784 Testit()
2785 assert_equal(1, g:legacyvar)
2786 unlet g:legacyvar
2787 delfunc g:LegacyFunction
2788 END
2789 CheckScriptSuccess(lines)
2790enddef
2791
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002792def Test_legacy_errors()
2793 for cmd in ['if', 'elseif', 'else', 'endif',
2794 'for', 'endfor', 'continue', 'break',
2795 'while', 'endwhile',
2796 'try', 'catch', 'finally', 'endtry']
2797 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2798 endfor
2799enddef
2800
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002801def Test_call_legacy_with_dict()
2802 var lines =<< trim END
2803 vim9script
2804 func Legacy() dict
2805 let g:result = self.value
2806 endfunc
2807 def TestDirect()
2808 var d = {value: 'yes', func: Legacy}
2809 d.func()
2810 enddef
2811 TestDirect()
2812 assert_equal('yes', g:result)
2813 unlet g:result
2814
2815 def TestIndirect()
2816 var d = {value: 'foo', func: Legacy}
2817 var Fi = d.func
2818 Fi()
2819 enddef
2820 TestIndirect()
2821 assert_equal('foo', g:result)
2822 unlet g:result
2823
2824 var d = {value: 'bar', func: Legacy}
2825 d.func()
2826 assert_equal('bar', g:result)
2827 unlet g:result
2828 END
2829 CheckScriptSuccess(lines)
2830enddef
2831
Bram Moolenaarab360522021-01-10 14:02:28 +01002832def DoFilterThis(a: string): list<string>
2833 # closure nested inside another closure using argument
2834 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2835 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2836enddef
2837
2838def Test_nested_closure_using_argument()
2839 assert_equal(['x', 'x2'], DoFilterThis('x'))
2840enddef
2841
Bram Moolenaar0186e582021-01-10 18:33:11 +01002842def Test_triple_nested_closure()
2843 var what = 'x'
2844 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2845 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2846 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2847enddef
2848
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002849func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002850 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002851 call Run_Test_silent_echo()
2852endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002853
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002854def Run_Test_silent_echo()
2855 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002856 vim9script
2857 def EchoNothing()
2858 silent echo ''
2859 enddef
2860 defcompile
2861 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002862 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002863
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002864 # Check that the balloon shows up after a mouse move
2865 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2866 term_sendkeys(buf, ":abc")
2867 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002868
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002869 # clean up
2870 StopVimInTerminal(buf)
2871 delete('XTest_silent_echo')
2872enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002873
Bram Moolenaar171fb922020-10-28 16:54:47 +01002874def SilentlyError()
2875 execute('silent! invalid')
2876 g:did_it = 'yes'
2877enddef
2878
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002879func UserError()
2880 silent! invalid
2881endfunc
2882
2883def SilentlyUserError()
2884 UserError()
2885 g:did_it = 'yes'
2886enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002887
2888" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002889func Test_ignore_silent_error()
2890 let g:did_it = 'no'
2891 call SilentlyError()
2892 call assert_equal('yes', g:did_it)
2893
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002894 let g:did_it = 'no'
2895 call SilentlyUserError()
2896 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002897
2898 unlet g:did_it
2899endfunc
2900
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002901def Test_ignore_silent_error_in_filter()
2902 var lines =<< trim END
2903 vim9script
2904 def Filter(winid: number, key: string): bool
2905 if key == 'o'
2906 silent! eval [][0]
2907 return true
2908 endif
2909 return popup_filter_menu(winid, key)
2910 enddef
2911
Bram Moolenaare0de1712020-12-02 17:36:54 +01002912 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002913 feedkeys("o\r", 'xnt')
2914 END
2915 CheckScriptSuccess(lines)
2916enddef
2917
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002918def Fibonacci(n: number): number
2919 if n < 2
2920 return n
2921 else
2922 return Fibonacci(n - 1) + Fibonacci(n - 2)
2923 endif
2924enddef
2925
Bram Moolenaar985116a2020-07-12 17:31:09 +02002926def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002927 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002928enddef
2929
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002930def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002931 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002932 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002933 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002934 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002935 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002936enddef
2937
2938def Test_closure_in_map()
2939 mkdir('XclosureDir/tdir', 'p')
2940 writefile(['111'], 'XclosureDir/file1')
2941 writefile(['222'], 'XclosureDir/file2')
2942 writefile(['333'], 'XclosureDir/tdir/file3')
2943
Bram Moolenaare0de1712020-12-02 17:36:54 +01002944 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002945
2946 delete('XclosureDir', 'rf')
2947enddef
2948
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002949def Test_invalid_function_name()
2950 var lines =<< trim END
2951 vim9script
2952 def s: list<string>
2953 END
2954 CheckScriptFailure(lines, 'E129:')
2955
2956 lines =<< trim END
2957 vim9script
2958 def g: list<string>
2959 END
2960 CheckScriptFailure(lines, 'E129:')
2961
2962 lines =<< trim END
2963 vim9script
2964 def <SID>: list<string>
2965 END
2966 CheckScriptFailure(lines, 'E884:')
2967
2968 lines =<< trim END
2969 vim9script
2970 def F list<string>
2971 END
2972 CheckScriptFailure(lines, 'E488:')
2973enddef
2974
Bram Moolenaara90afb92020-07-15 22:38:56 +02002975def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002976 var lines =<< trim END
2977 var Xsetlist: func
2978 Xsetlist = function('setloclist', [0])
2979 Xsetlist([], ' ', {title: 'test'})
2980 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002981
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002982 Xsetlist = function('setloclist', [0, [], ' '])
2983 Xsetlist({title: 'test'})
2984 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002985
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002986 Xsetlist = function('setqflist')
2987 Xsetlist([], ' ', {title: 'test'})
2988 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002989
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002990 Xsetlist = function('setqflist', [[], ' '])
2991 Xsetlist({title: 'test'})
2992 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002993
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002994 var Len: func: number = function('len', ['word'])
2995 assert_equal(4, Len())
2996
2997 var RepeatFunc = function('repeat', ['o'])
2998 assert_equal('ooooo', RepeatFunc(5))
2999 END
3000 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02003001
3002 lines =<< trim END
3003 vim9script
3004 def Foo(Parser: any)
3005 enddef
3006 var Expr: func(dict<any>): dict<any>
3007 const Call = Foo(Expr)
3008 END
3009 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02003010enddef
3011
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003012def Test_cmd_modifier()
3013 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003014 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003015enddef
3016
3017def Test_restore_modifiers()
3018 # check that when compiling a :def function command modifiers are not messed
3019 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003020 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003021 vim9script
3022 set eventignore=
3023 autocmd QuickFixCmdPost * copen
3024 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003025 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003026 enddef
3027 func Func()
3028 noautocmd call s:AutocmdsDisabled()
3029 let g:ei_after = &eventignore
3030 endfunc
3031 Func()
3032 END
3033 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003034 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003035enddef
3036
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003037def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003038 eval 1 + 2
3039 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003040 # call not on fourth line
3041 StackBot()
3042enddef
3043
3044def StackBot()
3045 # throw an error
3046 eval [][0]
3047enddef
3048
3049def Test_callstack_def()
3050 try
3051 StackTop()
3052 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003053 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003054 endtry
3055enddef
3056
Bram Moolenaare8211a32020-10-09 22:04:29 +02003057" Re-using spot for variable used in block
3058def Test_block_scoped_var()
3059 var lines =<< trim END
3060 vim9script
3061 def Func()
3062 var x = ['a', 'b', 'c']
3063 if 1
3064 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003065 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003066 endif
3067 var z = x
3068 assert_equal(['x', 'x', 'x'], z)
3069 enddef
3070 Func()
3071 END
3072 CheckScriptSuccess(lines)
3073enddef
3074
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003075def Test_reset_did_emsg()
3076 var lines =<< trim END
3077 @s = 'blah'
3078 au BufWinLeave * #
3079 def Func()
3080 var winid = popup_create('popup', {})
3081 exe '*s'
3082 popup_close(winid)
3083 enddef
3084 Func()
3085 END
3086 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003087 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003088enddef
3089
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003090def Test_did_emsg_reset()
3091 # executing an autocommand resets did_emsg, this should not result in a
3092 # builtin function considered failing
3093 var lines =<< trim END
3094 vim9script
3095 au BufWinLeave * #
3096 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003097 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003098 eval [][0]
3099 enddef
3100 nno <F3> <cmd>call <sid>Func()<cr>
3101 feedkeys("\<F3>\e", 'xt')
3102 END
3103 writefile(lines, 'XemsgReset')
3104 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3105 delete('XemsgReset')
3106 nunmap <F3>
3107 au! BufWinLeave
3108enddef
3109
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003110def Test_abort_with_silent_call()
3111 var lines =<< trim END
3112 vim9script
3113 g:result = 'none'
3114 def Func()
3115 g:result += 3
3116 g:result = 'yes'
3117 enddef
3118 # error is silenced, but function aborts on error
3119 silent! Func()
3120 assert_equal('none', g:result)
3121 unlet g:result
3122 END
3123 CheckScriptSuccess(lines)
3124enddef
3125
Bram Moolenaarf665e972020-12-05 19:17:16 +01003126def Test_continues_with_silent_error()
3127 var lines =<< trim END
3128 vim9script
3129 g:result = 'none'
3130 def Func()
3131 silent! g:result += 3
3132 g:result = 'yes'
3133 enddef
3134 # error is silenced, function does not abort
3135 Func()
3136 assert_equal('yes', g:result)
3137 unlet g:result
3138 END
3139 CheckScriptSuccess(lines)
3140enddef
3141
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003142def Test_abort_even_with_silent()
3143 var lines =<< trim END
3144 vim9script
3145 g:result = 'none'
3146 def Func()
3147 eval {-> ''}() .. '' .. {}['X']
3148 g:result = 'yes'
3149 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003150 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003151 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003152 unlet g:result
3153 END
3154 CheckScriptSuccess(lines)
3155enddef
3156
Bram Moolenaarf665e972020-12-05 19:17:16 +01003157def Test_cmdmod_silent_restored()
3158 var lines =<< trim END
3159 vim9script
3160 def Func()
3161 g:result = 'none'
3162 silent! g:result += 3
3163 g:result = 'none'
3164 g:result += 3
3165 enddef
3166 Func()
3167 END
3168 # can't use CheckScriptFailure, it ignores the :silent!
3169 var fname = 'Xdefsilent'
3170 writefile(lines, fname)
3171 var caught = 'no'
3172 try
3173 exe 'source ' .. fname
3174 catch /E1030:/
3175 caught = 'yes'
3176 assert_match('Func, line 4', v:throwpoint)
3177 endtry
3178 assert_equal('yes', caught)
3179 delete(fname)
3180enddef
3181
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003182def Test_cmdmod_silent_nested()
3183 var lines =<< trim END
3184 vim9script
3185 var result = ''
3186
3187 def Error()
3188 result ..= 'Eb'
3189 eval [][0]
3190 result ..= 'Ea'
3191 enddef
3192
3193 def Crash()
3194 result ..= 'Cb'
3195 sil! Error()
3196 result ..= 'Ca'
3197 enddef
3198
3199 Crash()
3200 assert_equal('CbEbEaCa', result)
3201 END
3202 CheckScriptSuccess(lines)
3203enddef
3204
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003205def Test_dict_member_with_silent()
3206 var lines =<< trim END
3207 vim9script
3208 g:result = 'none'
3209 var d: dict<any>
3210 def Func()
3211 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003212 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003213 catch
3214 endtry
3215 enddef
3216 silent! Func()
3217 assert_equal('0', g:result)
3218 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003219 END
3220 CheckScriptSuccess(lines)
3221enddef
3222
Bram Moolenaarf9041332021-01-21 19:41:16 +01003223def Test_skip_cmds_with_silent()
3224 var lines =<< trim END
3225 vim9script
3226
3227 def Func(b: bool)
3228 Crash()
3229 enddef
3230
3231 def Crash()
3232 sil! :/not found/d _
3233 sil! :/not found/put _
3234 enddef
3235
3236 Func(true)
3237 END
3238 CheckScriptSuccess(lines)
3239enddef
3240
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003241def Test_opfunc()
3242 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3243 def g:Opfunc(_: any): string
3244 setline(1, 'ASDF')
3245 return ''
3246 enddef
3247 new
3248 setline(1, 'asdf')
3249 feedkeys("\<F3>$", 'x')
3250 assert_equal('ASDF', getline(1))
3251
3252 bwipe!
3253 nunmap <F3>
3254enddef
3255
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003256func Test_opfunc_error()
3257 CheckScreendump
3258 call Run_Test_opfunc_error()
3259endfunc
3260
3261def Run_Test_opfunc_error()
3262 # test that the error from Opfunc() is displayed right away
3263 var lines =<< trim END
3264 vim9script
3265
3266 def Opfunc(type: string)
3267 try
3268 eval [][0]
3269 catch /nothing/ # error not caught
3270 endtry
3271 enddef
3272 &operatorfunc = Opfunc
3273 nnoremap <expr> l <SID>L()
3274 def L(): string
3275 return 'l'
3276 enddef
3277 'x'->repeat(10)->setline(1)
3278 feedkeys('g@l', 'n')
3279 feedkeys('llll')
3280 END
3281 call writefile(lines, 'XTest_opfunc_error')
3282
3283 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003284 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003285 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003286
3287 # clean up
3288 StopVimInTerminal(buf)
3289 delete('XTest_opfunc_error')
3290enddef
3291
Bram Moolenaar077a4232020-12-22 18:33:27 +01003292" this was crashing on exit
3293def Test_nested_lambda_in_closure()
3294 var lines =<< trim END
3295 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003296 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003297 def Outer()
3298 def g:Inner()
3299 echo map([1, 2, 3], {_, v -> v + 1})
3300 enddef
3301 g:Inner()
3302 enddef
3303 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003304 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003305 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003306 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003307 return
3308 endif
3309 assert_equal(['Done'], readfile('XnestedDone'))
3310 delete('XnestedDone')
3311enddef
3312
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003313def Test_check_func_arg_types()
3314 var lines =<< trim END
3315 vim9script
3316 def F1(x: string): string
3317 return x
3318 enddef
3319
3320 def F2(x: number): number
3321 return x + 1
3322 enddef
3323
3324 def G(g: func): dict<func>
3325 return {f: g}
3326 enddef
3327
3328 def H(d: dict<func>): string
3329 return d.f('a')
3330 enddef
3331 END
3332
3333 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3334 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3335enddef
3336
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003337def Test_list_any_type_checked()
3338 var lines =<< trim END
3339 vim9script
3340 def Foo()
3341 --decl--
3342 Bar(l)
3343 enddef
3344 def Bar(ll: list<dict<any>>)
3345 enddef
3346 Foo()
3347 END
3348 lines[2] = 'var l: list<any>'
3349 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3350
3351 lines[2] = 'var l: list<any> = []'
3352 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3353
3354 lines[2] = 'var l: list<any> = [11]'
3355 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3356enddef
3357
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003358def Test_compile_error()
3359 var lines =<< trim END
3360 def g:Broken()
3361 echo 'a' + {}
3362 enddef
3363 call g:Broken()
3364 END
3365 # First call: compilation error
3366 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3367
3368 # Second call won't try compiling again
3369 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003370 delfunc g:Broken
3371
3372 # No error when compiling with :silent!
3373 lines =<< trim END
3374 def g:Broken()
3375 echo 'a' + []
3376 enddef
3377 silent! defcompile
3378 END
3379 CheckScriptSuccess(lines)
3380
3381 # Calling the function won't try compiling again
3382 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3383 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003384enddef
3385
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003386def Test_ignored_argument()
3387 var lines =<< trim END
3388 vim9script
3389 def Ignore(_, _): string
3390 return 'yes'
3391 enddef
3392 assert_equal('yes', Ignore(1, 2))
3393
3394 func Ok(_)
3395 return a:_
3396 endfunc
3397 assert_equal('ok', Ok('ok'))
3398
3399 func Oktoo()
3400 let _ = 'too'
3401 return _
3402 endfunc
3403 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003404
3405 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003406 END
3407 CheckScriptSuccess(lines)
3408
3409 lines =<< trim END
3410 def Ignore(_: string): string
3411 return _
3412 enddef
3413 defcompile
3414 END
3415 CheckScriptFailure(lines, 'E1181:', 1)
3416
3417 lines =<< trim END
3418 var _ = 1
3419 END
3420 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003421
3422 lines =<< trim END
3423 var x = _
3424 END
3425 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003426enddef
3427
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003428def Test_too_many_arguments()
3429 var lines =<< trim END
3430 echo [0, 1, 2]->map(() => 123)
3431 END
3432 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3433
3434 lines =<< trim END
3435 echo [0, 1, 2]->map((_) => 123)
3436 END
3437 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3438enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003439
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003440def Test_closing_brace_at_start_of_line()
3441 var lines =<< trim END
3442 def Func()
3443 enddef
3444 Func(
3445 )
3446 END
3447 call CheckDefAndScriptSuccess(lines)
3448enddef
3449
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003450func CreateMydict()
3451 let g:mydict = {}
3452 func g:mydict.afunc()
3453 let g:result = self.key
3454 endfunc
3455endfunc
3456
3457def Test_numbered_function_reference()
3458 CreateMydict()
3459 var output = execute('legacy func g:mydict.afunc')
3460 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3461 execute 'function(' .. funcName .. ', [], {key: 42})()'
3462 # check that the function still exists
3463 assert_equal(output, execute('legacy func g:mydict.afunc'))
3464 unlet g:mydict
3465enddef
3466
Bram Moolenaar20677332021-06-06 17:02:53 +02003467if has('python3')
3468 def Test_python3_heredoc()
3469 py3 << trim EOF
3470 import vim
3471 vim.vars['didit'] = 'yes'
3472 EOF
3473 assert_equal('yes', g:didit)
3474
3475 python3 << trim EOF
3476 import vim
3477 vim.vars['didit'] = 'again'
3478 EOF
3479 assert_equal('again', g:didit)
3480 enddef
3481endif
3482
3483" This messes up syntax highlight, keep near the end.
3484if has('lua')
3485 def Test_lua_heredoc()
3486 g:d = {}
3487 lua << trim EOF
3488 x = vim.eval('g:d')
3489 x['key'] = 'val'
3490 EOF
3491 assert_equal('val', g:d.key)
3492 enddef
3493endif
3494
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003495
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003496" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker