blob: 6d098ffda5371b5dab1295a36d9716db5ca02ea9 [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
494 CheckDefAndScriptFailure2(["call Test ('text')"], 'E476:', 'E1068:')
495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200550enddef
551
Bram Moolenaarcef12702021-01-04 14:09:43 +0100552def FuncWithComment( # comment
553 a: number, #comment
554 b: bool, # comment
555 c: string) #comment
556 assert_equal(4, a)
557 assert_equal(true, b)
558 assert_equal('yes', c)
559enddef
560
561def Test_func_with_comments()
562 FuncWithComment(4, true, 'yes')
563
564 var lines =<< trim END
565 def Func(# comment
566 arg: string)
567 enddef
568 END
569 CheckScriptFailure(lines, 'E125:', 1)
570
571 lines =<< trim END
572 def Func(
573 arg: string# comment
574 )
575 enddef
576 END
577 CheckScriptFailure(lines, 'E475:', 2)
578
579 lines =<< trim END
580 def Func(
581 arg: string
582 )# comment
583 enddef
584 END
585 CheckScriptFailure(lines, 'E488:', 3)
586enddef
587
Bram Moolenaar04b12692020-05-04 23:24:44 +0200588def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000589 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200590 return 'nested ' .. arg
591 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000592 NestedDef(':def')->assert_equal('nested :def')
593
594 func NestedFunc(arg)
595 return 'nested ' .. a:arg
596 endfunc
597 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200598
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200599 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
600 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
601
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200602 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
603 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200604
Bram Moolenaar54021752020-12-06 18:50:36 +0100605 var lines =<< trim END
606 def Outer()
607 def Inner()
608 # comment
609 enddef
610 def Inner()
611 enddef
612 enddef
613 END
614 CheckDefFailure(lines, 'E1073:')
615
616 lines =<< trim END
617 def Outer()
618 def Inner()
619 # comment
620 enddef
621 def! Inner()
622 enddef
623 enddef
624 END
625 CheckDefFailure(lines, 'E1117:')
626
627 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 lines =<< trim END
629 vim9script
630 var thecount = 0
631 if true
632 def Test(): number
633 def TheFunc(): number
634 thecount += 1
635 return thecount
636 enddef
637 return TheFunc()
638 enddef
639 endif
640 defcompile
641 assert_equal(1, Test())
642 assert_equal(2, Test())
643 END
644 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100645
646 # also works when "thecount" is inside the "if" block
647 lines =<< trim END
648 vim9script
649 if true
650 var thecount = 0
651 def Test(): number
652 def TheFunc(): number
653 thecount += 1
654 return thecount
655 enddef
656 return TheFunc()
657 enddef
658 endif
659 defcompile
660 assert_equal(1, Test())
661 assert_equal(2, Test())
662 END
663 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200664
665 lines =<< trim END
666 vim9script
667 def Outer()
668 def Inner()
669 echo 'hello'
670 enddef burp
671 enddef
672 defcompile
673 END
674 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200675enddef
676
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100677def Test_not_nested_function()
678 echo printf('%d',
679 function('len')('xxx'))
680enddef
681
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200682func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200683 call MyDefaultArgs()->assert_equal('string')
684 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200685 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200686endfunc
687
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200688def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200689 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200690 vim9script
691 def Outer()
692 def g:Inner(): string
693 return 'inner'
694 enddef
695 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200696 defcompile
697 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200698 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200699 delfunc g:Inner
700 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200701 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200702 delfunc g:Inner
703 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200704 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200705 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200706 END
707 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200708
709 lines =<< trim END
710 vim9script
711 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000712 func g:Inner()
713 return 'inner'
714 endfunc
715 enddef
716 defcompile
717 Outer()
718 g:Inner()->assert_equal('inner')
719 delfunc g:Inner
720 Outer()
721 g:Inner()->assert_equal('inner')
722 delfunc g:Inner
723 Outer()
724 g:Inner()->assert_equal('inner')
725 delfunc g:Inner
726 END
727 CheckScriptSuccess(lines)
728
729 lines =<< trim END
730 vim9script
731 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200732 def g:Inner(): string
733 return 'inner'
734 enddef
735 enddef
736 defcompile
737 Outer()
738 Outer()
739 END
740 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100741 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200742
743 lines =<< trim END
744 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100745 def Outer()
746 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100747 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100748 enddef
749 g:Inner()
750 enddef
751 Outer()
752 END
753 CheckScriptSuccess(lines)
754 delfunc g:Inner
755
756 lines =<< trim END
757 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200758 def Func()
759 echo 'script'
760 enddef
761 def Outer()
762 def Func()
763 echo 'inner'
764 enddef
765 enddef
766 defcompile
767 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000768 CheckScriptFailure(lines, "E1073:", 1)
769
770 lines =<< trim END
771 vim9script
772 def Func()
773 echo 'script'
774 enddef
775 def Func()
776 echo 'script'
777 enddef
778 END
779 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200780enddef
781
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100782def DefListAll()
783 def
784enddef
785
786def DefListOne()
787 def DefListOne
788enddef
789
790def DefListMatches()
791 def /DefList
792enddef
793
794def Test_nested_def_list()
795 var funcs = split(execute('call DefListAll()'), "\n")
796 assert_true(len(funcs) > 10)
797 assert_true(funcs->index('def DefListAll()') >= 0)
798
799 funcs = split(execute('call DefListOne()'), "\n")
800 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
801
802 funcs = split(execute('call DefListMatches()'), "\n")
803 assert_true(len(funcs) >= 3)
804 assert_true(funcs->index('def DefListAll()') >= 0)
805 assert_true(funcs->index('def DefListOne()') >= 0)
806 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100807
808 var lines =<< trim END
809 vim9script
810 def Func()
811 def +Func+
812 enddef
813 defcompile
814 END
815 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100816enddef
817
Bram Moolenaar333894b2020-08-01 18:53:07 +0200818def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200819 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200820 vim9script
821 def g:Func(): string
822 return 'global'
823 enddef
824 def Func(): string
825 return 'local'
826 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200827 g:Func()->assert_equal('global')
828 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100829 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200830 END
831 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200832
833 lines =<< trim END
834 vim9script
835 def g:Funcy()
836 echo 'funcy'
837 enddef
838 s:Funcy()
839 END
840 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200841enddef
842
Bram Moolenaar0f769812020-09-12 18:32:34 +0200843def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200844 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200845 vim9script
846 def g:Gfunc(): string
847 return 'global'
848 enddef
849 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200850 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200851 return Gfunc('testing')
852 enddef
853 g:Gfunc()->assert_equal('global')
854 AnotherFunc()->assert_equal(7)
855 delfunc g:Gfunc
856 END
857 CheckScriptSuccess(lines)
858
859 lines =<< trim END
860 vim9script
861 def g:Func(): string
862 return 'global'
863 enddef
864 def AnotherFunc()
865 g:Func = function('len')
866 enddef
867 AnotherFunc()
868 END
869 CheckScriptFailure(lines, 'E705:')
870 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200871
872 # global function is found without g: prefix
873 lines =<< trim END
874 vim9script
875 def g:Func(): string
876 return 'global'
877 enddef
878 def AnotherFunc(): string
879 return Func()
880 enddef
881 assert_equal('global', AnotherFunc())
882 delfunc g:Func
883 END
884 CheckScriptSuccess(lines)
885
886 lines =<< trim END
887 vim9script
888 def g:Func(): string
889 return 'global'
890 enddef
891 assert_equal('global', Func())
892 delfunc g:Func
893 END
894 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200895enddef
896
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200897func TakesOneArg(arg)
898 echo a:arg
899endfunc
900
901def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200902 CheckDefFailure(['TakesOneArg()'], 'E119:')
903 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
904 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
905 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200906
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200907 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200908 vim9script
909 def Func(s: string)
910 echo s
911 enddef
912 Func([])
913 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200914 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200915
916 lines =<< trim END
917 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100918 var name = 'piet'
919 def FuncOne(name: string)
920 echo nr
921 enddef
922 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100923 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100924
925 lines =<< trim END
926 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200927 def FuncOne(nr: number)
928 echo nr
929 enddef
930 def FuncTwo()
931 FuncOne()
932 enddef
933 defcompile
934 END
935 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200936 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200937 try
938 source Xscript
939 catch
940 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
941 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
942 didCatch = true
943 endtry
944 assert_true(didCatch)
945
946 lines =<< trim END
947 vim9script
948 def FuncOne(nr: number)
949 echo nr
950 enddef
951 def FuncTwo()
952 FuncOne(1, 2)
953 enddef
954 defcompile
955 END
956 writefile(lines, 'Xscript')
957 didCatch = false
958 try
959 source Xscript
960 catch
961 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
962 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
963 didCatch = true
964 endtry
965 assert_true(didCatch)
966
967 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200968enddef
969
Bram Moolenaar50824712020-12-20 21:10:17 +0100970def Test_call_funcref_wrong_args()
971 var head =<< trim END
972 vim9script
973 def Func3(a1: string, a2: number, a3: list<number>)
974 echo a1 .. a2 .. a3[0]
975 enddef
976 def Testme()
977 var funcMap: dict<func> = {func: Func3}
978 END
979 var tail =<< trim END
980 enddef
981 Testme()
982 END
983 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
984
985 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
986 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100987
988 var lines =<< trim END
989 vim9script
990 var Ref: func(number): any
991 Ref = (j) => !j
992 echo Ref(false)
993 END
994 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
995
996 lines =<< trim END
997 vim9script
998 var Ref: func(number): any
999 Ref = (j) => !j
1000 call Ref(false)
1001 END
1002 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001003enddef
1004
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001005def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001006 var lines =<< trim END
1007 var Callback = (..._) => 'anything'
1008 assert_equal('anything', Callback())
1009 assert_equal('anything', Callback(1))
1010 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001011
1012 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001013 END
1014 CheckDefAndScriptSuccess(lines)
1015
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001016 CheckDefFailure(['echo ((i) => 0)()'],
1017 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001018
Bram Moolenaar2a389082021-04-09 20:24:31 +02001019 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001020 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001021 echo Ref(1, 'x')
1022 END
1023 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001024
1025 lines =<< trim END
1026 var Ref: func(job, string, number)
1027 Ref = (x, y) => 0
1028 END
1029 CheckDefAndScriptFailure(lines, 'E1012:')
1030
1031 lines =<< trim END
1032 var Ref: func(job, string)
1033 Ref = (x, y, z) => 0
1034 END
1035 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001036
1037 lines =<< trim END
1038 var one = 1
1039 var l = [1, 2, 3]
1040 echo map(l, (one) => one)
1041 END
1042 CheckDefFailure(lines, 'E1167:')
1043 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1044
1045 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001046 var Ref: func(any, ?any): bool
1047 Ref = (_, y = 1) => false
1048 END
1049 CheckDefAndScriptFailure(lines, 'E1172:')
1050
1051 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001052 var a = 0
1053 var b = (a == 0 ? 1 : 2)
1054 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001055 var txt = 'a'
1056 b = (txt =~ 'x' ? 1 : 2)
1057 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001058 END
1059 CheckDefAndScriptSuccess(lines)
1060
1061 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001062 def ShadowLocal()
1063 var one = 1
1064 var l = [1, 2, 3]
1065 echo map(l, (one) => one)
1066 enddef
1067 END
1068 CheckDefFailure(lines, 'E1167:')
1069
1070 lines =<< trim END
1071 def Shadowarg(one: number)
1072 var l = [1, 2, 3]
1073 echo map(l, (one) => one)
1074 enddef
1075 END
1076 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001077
1078 lines =<< trim END
1079 echo ((a) => a)('aa', 'bb')
1080 END
1081 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001082
1083 lines =<< trim END
1084 echo 'aa'->((a) => a)('bb')
1085 END
1086 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1087 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001088enddef
1089
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001090def Test_lambda_line_nr()
1091 var lines =<< trim END
1092 vim9script
1093 # comment
1094 # comment
1095 var id = timer_start(1'000, (_) => 0)
1096 var out = execute('verbose ' .. timer_info(id)[0].callback
1097 ->string()
1098 ->substitute("('\\|')", ' ', 'g'))
1099 assert_match('Last set from .* line 4', out)
1100 END
1101 CheckScriptSuccess(lines)
1102enddef
1103
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001104def FilterWithCond(x: string, Cond: func(string): bool): bool
1105 return Cond(x)
1106enddef
1107
Bram Moolenaar0346b792021-01-31 22:18:29 +01001108def Test_lambda_return_type()
1109 var lines =<< trim END
1110 var Ref = (): => 123
1111 END
1112 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001113
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001114 # no space before the return type
1115 lines =<< trim END
1116 var Ref = (x):number => x + 1
1117 END
1118 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1119
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001120 # this works
1121 for x in ['foo', 'boo']
1122 echo FilterWithCond(x, (v) => v =~ '^b')
1123 endfor
1124
1125 # this fails
1126 lines =<< trim END
1127 echo FilterWithCond('foo', (v) => v .. '^b')
1128 END
1129 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001130
1131 lines =<< trim END
1132 var Lambda1 = (x) => {
1133 return x
1134 }
1135 assert_equal('asdf', Lambda1('asdf'))
1136 var Lambda2 = (x): string => {
1137 return x
1138 }
1139 assert_equal('foo', Lambda2('foo'))
1140 END
1141 CheckDefAndScriptSuccess(lines)
1142
1143 lines =<< trim END
1144 var Lambda = (x): string => {
1145 return x
1146 }
1147 echo Lambda(['foo'])
1148 END
1149 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001150enddef
1151
Bram Moolenaar709664c2020-12-12 14:33:41 +01001152def Test_lambda_uses_assigned_var()
1153 CheckDefSuccess([
1154 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001155 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001156enddef
1157
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001158def Test_pass_legacy_lambda_to_def_func()
1159 var lines =<< trim END
1160 vim9script
1161 func Foo()
1162 eval s:Bar({x -> 0})
1163 endfunc
1164 def Bar(y: any)
1165 enddef
1166 Foo()
1167 END
1168 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001169
1170 lines =<< trim END
1171 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001172 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001173 enddef
1174 legacy call g:TestFunc({-> 0})
1175 delfunc g:TestFunc
1176
1177 def g:TestFunc(f: func(number))
1178 enddef
1179 legacy call g:TestFunc({nr -> 0})
1180 delfunc g:TestFunc
1181 END
1182 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001183enddef
1184
Bram Moolenaar844fb642021-10-23 13:32:30 +01001185def Test_lambda_in_reduce_line_break()
1186 # this was using freed memory
1187 var lines =<< trim END
1188 vim9script
1189 const result: dict<number> =
1190 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1191 ->reduce((acc, val) => {
1192 if has_key(acc, val)
1193 acc[val] += 1
1194 return acc
1195 else
1196 acc[val] = 1
1197 return acc
1198 endif
1199 }, {})
1200 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1201 END
1202 CheckScriptSuccess(lines)
1203enddef
1204
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001205def Test_set_opfunc_to_lambda()
1206 var lines =<< trim END
1207 vim9script
1208 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1209 def CountSpaces(type = ''): string
1210 if type == ''
1211 &operatorfunc = (t) => CountSpaces(t)
1212 return 'g@'
1213 endif
1214 normal! '[V']y
1215 g:result = getreg('"')->count(' ')
1216 return ''
1217 enddef
1218 new
1219 'a b c d e'->setline(1)
1220 feedkeys("\<F4>", 'x')
1221 assert_equal(4, g:result)
1222 bwipe!
1223 END
1224 CheckScriptSuccess(lines)
1225enddef
1226
Bram Moolenaaref082e12021-12-12 21:02:03 +00001227def Test_set_opfunc_to_global_function()
1228 var lines =<< trim END
1229 vim9script
1230 def g:CountSpaces(type = ''): string
1231 normal! '[V']y
1232 g:result = getreg('"')->count(' ')
1233 return ''
1234 enddef
1235 &operatorfunc = g:CountSpaces
1236 new
1237 'a b c d e'->setline(1)
1238 feedkeys("g@_", 'x')
1239 assert_equal(4, g:result)
1240 bwipe!
1241 END
1242 CheckScriptSuccess(lines)
1243 &operatorfunc = ''
1244enddef
1245
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001246def Test_lambda_type_allocated()
1247 # Check that unreferencing a partial using a lambda can use the variable type
1248 # after the lambda has been freed and does not leak memory.
1249 var lines =<< trim END
1250 vim9script
1251
1252 func MyomniFunc1(val, findstart, base)
1253 return a:findstart ? 0 : []
1254 endfunc
1255
1256 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1257 &omnifunc = Lambda
1258 Lambda = (a, b) => MyomniFunc1(20, a, b)
1259 &omnifunc = string(Lambda)
1260 Lambda = (a, b) => strlen(a)
1261 END
1262 CheckScriptSuccess(lines)
1263enddef
1264
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001265" Default arg and varargs
1266def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001267 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001268 for s in rest
1269 res ..= ',' .. s
1270 endfor
1271 return res
1272enddef
1273
1274def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001275 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001276 MyDefVarargs('one')->assert_equal('one,foo')
1277 MyDefVarargs('one', 'two')->assert_equal('one,two')
1278 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001279 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001280 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001281 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001282 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001283
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001284 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001285 vim9script
1286 def Func(...l: list<string>)
1287 echo l
1288 enddef
1289 Func('a', 'b', 'c')
1290 END
1291 CheckScriptSuccess(lines)
1292
1293 lines =<< trim END
1294 vim9script
1295 def Func(...l: list<string>)
1296 echo l
1297 enddef
1298 Func()
1299 END
1300 CheckScriptSuccess(lines)
1301
1302 lines =<< trim END
1303 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001304 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001305 echo l
1306 enddef
1307 Func(0)
1308 END
1309 CheckScriptSuccess(lines)
1310
1311 lines =<< trim END
1312 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001313 def Func(...l: any)
1314 echo l
1315 enddef
1316 Func(0)
1317 END
1318 CheckScriptFailure(lines, 'E1180:', 2)
1319
1320 lines =<< trim END
1321 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001322 def Func(..._l: list<string>)
1323 echo _l
1324 enddef
1325 Func('a', 'b', 'c')
1326 END
1327 CheckScriptSuccess(lines)
1328
1329 lines =<< trim END
1330 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001331 def Func(...l: list<string>)
1332 echo l
1333 enddef
1334 Func(1, 2, 3)
1335 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001336 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001337
1338 lines =<< trim END
1339 vim9script
1340 def Func(...l: list<string>)
1341 echo l
1342 enddef
1343 Func('a', 9)
1344 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001345 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001346
1347 lines =<< trim END
1348 vim9script
1349 def Func(...l: list<string>)
1350 echo l
1351 enddef
1352 Func(1, 'a')
1353 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001354 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001355
1356 lines =<< trim END
1357 vim9script
1358 def Func( # some comment
1359 ...l = []
1360 )
1361 echo l
1362 enddef
1363 END
1364 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001365
1366 lines =<< trim END
1367 vim9script
1368 def DoIt()
1369 g:Later('')
1370 enddef
1371 defcompile
1372 def g:Later(...l: list<number>)
1373 enddef
1374 DoIt()
1375 END
1376 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001377enddef
1378
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001379let s:value = ''
1380
1381def FuncOneDefArg(opt = 'text')
1382 s:value = opt
1383enddef
1384
1385def FuncTwoDefArg(nr = 123, opt = 'text'): string
1386 return nr .. opt
1387enddef
1388
1389def FuncVarargs(...arg: list<string>): string
1390 return join(arg, ',')
1391enddef
1392
1393def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001394 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001395 RefDefArg = FuncOneDefArg
1396 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001397 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001398 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001399 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001400
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001401 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001402 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001403 RefDef2Arg()->assert_equal('123text')
1404 RefDef2Arg(99)->assert_equal('99text')
1405 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001406
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001407 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1408 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001409
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001410 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001411 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001412 RefVarargs()->assert_equal('')
1413 RefVarargs('one')->assert_equal('one')
1414 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001415
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001416 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1417 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001418enddef
1419
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001420" Only varargs
1421def MyVarargsOnly(...args: list<string>): string
1422 return join(args, ',')
1423enddef
1424
1425def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001426 MyVarargsOnly()->assert_equal('')
1427 MyVarargsOnly('one')->assert_equal('one')
1428 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001429 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1430 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001431enddef
1432
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001433def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001434 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001435 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001436 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001437enddef
1438
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001439def DictArg(arg: dict<string>)
1440 arg['key'] = 'value'
1441enddef
1442
1443def ListArg(arg: list<string>)
1444 arg[0] = 'value'
1445enddef
1446
1447def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001448 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001449 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001450 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001451 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001452 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001453 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001454 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001455
Bram Moolenaard2c61702020-09-06 15:58:36 +02001456 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001457 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001458enddef
1459
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001460" These argument names are reserved in legacy functions.
1461def WithReservedNames(firstline: string, lastline: string): string
1462 return firstline .. lastline
1463enddef
1464
1465def Test_argument_names()
1466 assert_equal('OK', WithReservedNames('O', 'K'))
1467enddef
1468
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001469def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001470 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001471 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001472enddef
1473
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001474func DefinedLater(arg)
1475 return a:arg
1476endfunc
1477
1478def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001479 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001480 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001481 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001482
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001483 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001484 vim9script
1485 def RetNumber(): number
1486 return 123
1487 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001488 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001489 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001490 END
1491 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001492
1493 lines =<< trim END
1494 vim9script
1495 def RetNumber(): number
1496 return 123
1497 enddef
1498 def Bar(F: func: number): number
1499 return F()
1500 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001501 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001502 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001503 END
1504 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001505
1506 lines =<< trim END
1507 vim9script
1508 def UseNumber(nr: number)
1509 echo nr
1510 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001511 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001512 Funcref(123)
1513 END
1514 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001515
1516 lines =<< trim END
1517 vim9script
1518 def UseNumber(nr: number)
1519 echo nr
1520 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001521 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001522 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001523 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001524
1525 lines =<< trim END
1526 vim9script
1527 def EchoNr(nr = 34)
1528 g:echo = nr
1529 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001530 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001531 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001532 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001533 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001534 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001535 END
1536 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001537
1538 lines =<< trim END
1539 vim9script
1540 def EchoList(...l: list<number>)
1541 g:echo = l
1542 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001543 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001544 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001545 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001546 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001547 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001548 END
1549 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001550
1551 lines =<< trim END
1552 vim9script
1553 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1554 g:optarg = opt
1555 g:listarg = l
1556 return nr
1557 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001558 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001559 Funcref(10)->assert_equal(10)
1560 g:optarg->assert_equal(12)
1561 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001562
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001563 Funcref(11, 22)->assert_equal(11)
1564 g:optarg->assert_equal(22)
1565 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001566
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001567 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1568 g:optarg->assert_equal(18)
1569 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001570 END
1571 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001572enddef
1573
1574let SomeFunc = function('len')
1575let NotAFunc = 'text'
1576
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001577def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001578 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001579 var Ref1: func(bool): string
1580 var Ref2: func(bool): number
1581 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001582 Ref3 = g:cond ? Ref1 : Ref2
1583
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001584 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001585 var Refa1: func(bool): number
1586 var Refa2: func(bool, number): number
1587 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001588 Refa3 = g:cond ? Refa1 : Refa2
1589
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001590 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001591 var Refb1: func(bool, string): number
1592 var Refb2: func(string, number): number
1593 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001594 Refb3 = g:cond ? Refb1 : Refb2
1595enddef
1596
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001597def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001598 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001599enddef
1600
1601def DefinedEvenLater(arg: string): string
1602 return arg
1603enddef
1604
1605def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001606 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001607 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001608enddef
1609
1610def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001611 CheckScriptFailure([
1612 'def Func(): number',
1613 'return "a"',
1614 'enddef',
1615 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001616 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001617 CheckScriptFailure([
1618 'def Func(): string',
1619 'return 1',
1620 'enddef',
1621 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001622 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001623 CheckScriptFailure([
1624 'def Func(): void',
1625 'return "a"',
1626 'enddef',
1627 'defcompile'],
1628 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001629 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001630 CheckScriptFailure([
1631 'def Func()',
1632 'return "a"',
1633 'enddef',
1634 'defcompile'],
1635 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001636 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001637
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001638 CheckScriptFailure([
1639 'def Func(): number',
1640 'return',
1641 'enddef',
1642 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001643 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001644
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001645 CheckScriptFailure([
1646 'def Func():number',
1647 'return 123',
1648 'enddef',
1649 'defcompile'], 'E1069:')
1650 delfunc! g:Func
1651
1652 CheckScriptFailure([
1653 'def Func() :number',
1654 'return 123',
1655 'enddef',
1656 'defcompile'], 'E1059:')
1657 delfunc! g:Func
1658
1659 CheckScriptFailure([
1660 'def Func() : number',
1661 'return 123',
1662 'enddef',
1663 'defcompile'], 'E1059:')
1664 delfunc! g:Func
1665
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001666 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001667 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001668 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001669 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001670 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001671 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001672
1673 CheckScriptFailure([
1674 'vim9script',
1675 'def FuncB()',
1676 ' return 123',
1677 'enddef',
1678 'def FuncA()',
1679 ' FuncB()',
1680 'enddef',
1681 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001682enddef
1683
1684def Test_arg_type_wrong()
1685 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001686 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001687 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001688 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001689 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1690 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001691enddef
1692
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001693def Test_white_space_before_comma()
1694 var lines =<< trim END
1695 vim9script
1696 def Func(a: number , b: number)
1697 enddef
1698 END
1699 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001700 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001701enddef
1702
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001703def Test_white_space_after_comma()
1704 var lines =<< trim END
1705 vim9script
1706 def Func(a: number,b: number)
1707 enddef
1708 END
1709 CheckScriptFailure(lines, 'E1069:')
1710
1711 # OK in legacy function
1712 lines =<< trim END
1713 vim9script
1714 func Func(a,b)
1715 endfunc
1716 END
1717 CheckScriptSuccess(lines)
1718enddef
1719
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001720def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001721 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001722 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001723 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001724 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001725 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001726 enddef
1727 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001728 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001729
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001730 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001731 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001732 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001733
Bram Moolenaar67979662020-06-20 22:50:47 +02001734 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001735 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001736 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001737
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001738 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001739 def ListFunc(arg: list<number>)
1740 listvar = arg
1741 enddef
1742 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001743 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001744
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001745 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001746 def DictFunc(arg: dict<number>)
1747 dictvar = arg
1748 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001749 {a: 1, b: 2}->DictFunc()
1750 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001751 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001752 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001753 enddef
1754 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001755 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001756
Bram Moolenaare0de1712020-12-02 17:36:54 +01001757 {a: 3, b: 4}->DictFunc()
1758 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001759
1760 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001761 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001762 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001763 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001764
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001765 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001766 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001767 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001768 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001769 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001770 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001771
1772 def UseString()
1773 'xyork'->MyFunc()
1774 enddef
1775 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001776 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001777
Bram Moolenaar10409562020-07-29 20:00:38 +02001778 def UseString2()
1779 "knife"->MyFunc()
1780 enddef
1781 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001782 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001783
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001784 # prepending a colon makes it a mark
1785 new
1786 setline(1, ['aaa', 'bbb', 'ccc'])
1787 normal! 3Gmt1G
1788 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001789 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001790 bwipe!
1791
Bram Moolenaare6b53242020-07-01 17:28:33 +02001792 MyFunc(
1793 'continued'
1794 )
1795 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001796 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001797 )
1798
1799 call MyFunc(
1800 'more'
1801 ..
1802 'lines'
1803 )
1804 assert_equal(
1805 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001806 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001807 END
1808 writefile(lines, 'Xcall.vim')
1809 source Xcall.vim
1810 delete('Xcall.vim')
1811enddef
1812
1813def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001814 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001815 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001816 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001817 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001818 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001819 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001820 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001821 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001822 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001823enddef
1824
Bram Moolenaar65b95452020-07-19 14:03:09 +02001825def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001826 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001827 vim9script
1828 def MyFunc(arg: string)
1829 echo arg
1830 enddef
1831 MyFunc(1234)
1832 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001833 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001834enddef
1835
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001836def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001837 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001838 vim9script
1839 const var = ''
1840 def MyFunc(arg: string)
1841 var = 'asdf'
1842 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001843 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001844 END
1845 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001846 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001847 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001848
1849 lines =<< trim END
1850 const g:Aconst = 77
1851 def Change()
1852 # comment
1853 g:Aconst = 99
1854 enddef
1855 call Change()
1856 unlet g:Aconst
1857 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001858 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001859enddef
1860
1861" Test that inside :function a Python function can be defined, :def is not
1862" recognized.
1863func Test_function_python()
1864 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001865 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866 execute py "<< EOF"
1867def do_something():
1868 return 1
1869EOF
1870endfunc
1871
1872def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001873 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001874 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001875 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876 echo 'hello'
1877 enddef
1878
1879 def CallGoneSoon()
1880 GoneSoon()
1881 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001882 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001883
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001884 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001885 CallGoneSoon()
1886 END
1887 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001888 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1889 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001890
1891 delete('XToDelFunc')
1892enddef
1893
1894def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001895 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001896 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001897 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001898 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001899 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001900 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001901 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001902 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001903 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001904
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001905 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001906 g:Func1()->assert_equal('Func1')
1907 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001908
1909 delfunc! Func0
1910 delfunc! Func1
1911 delfunc! Func2
1912enddef
1913
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001914def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001915 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001916 vim9script
1917 func Func(arg)
1918 echo a:arg
1919 endfunc
1920 Func('text')
1921 END
1922 writefile(lines, 'XVim9Func')
1923 so XVim9Func
1924
1925 delete('XVim9Func')
1926enddef
1927
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001928let s:funcResult = 0
1929
1930def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001931 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001932enddef
1933
1934def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001935 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001936 return 1234
1937enddef
1938
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001939def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001940 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001941 return 'text'
1942enddef
1943
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001944def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001945 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001946enddef
1947
1948def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001949 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001950 return arg
1951enddef
1952
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001953def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001954 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001955enddef
1956
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001957def FuncOneArgRetString(arg: string): string
1958 return arg
1959enddef
1960
Bram Moolenaar89228602020-04-05 22:14:54 +02001961def FuncOneArgRetAny(arg: any): any
1962 return arg
1963enddef
1964
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001965def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001966 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001967 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001968 Ref1 = FuncNoArgNoRet
1969 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001970 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001971
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001972 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001973 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001974 Ref2 = FuncNoArgNoRet
1975 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001976 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001977
Bram Moolenaar53900992020-08-22 19:02:02 +02001978 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001979 Ref2 = FuncOneArgNoRet
1980 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001981 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001982
Bram Moolenaar53900992020-08-22 19:02:02 +02001983 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001984 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001985 Ref2()->assert_equal(1234)
1986 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001987
Bram Moolenaar53900992020-08-22 19:02:02 +02001988 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001989 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001990 Ref2(13)->assert_equal(13)
1991 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001992enddef
1993
Bram Moolenaar9978d472020-07-05 16:01:56 +02001994def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001995 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02001996 for n in repeat([1], 3)
1997 res += n
1998 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001999 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002000
2001 res = 0
2002 for n in add([1, 2], 3)
2003 res += n
2004 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002005 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002006enddef
2007
Bram Moolenaar846178a2020-07-05 17:04:13 +02002008def Test_argv_return_type()
2009 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002010 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002011 for name in argv()
2012 res ..= name
2013 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002014 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002015enddef
2016
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002017def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002018 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002019 RefVoid = FuncNoArgNoRet
2020 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002021 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2022 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002023
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002024 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002025 RefAny = FuncNoArgRetNumber
2026 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002027 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2028 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002029
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002030 var RefAnyNoArgs: func: any = RefAny
2031
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002032 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002033 RefNr = FuncNoArgRetNumber
2034 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002035 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2036 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002037
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002038 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002039 RefStr = FuncNoArgRetString
2040 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002041 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2042 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002043enddef
2044
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002045def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002046 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002047
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002048 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2049 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2050 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2051 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2052 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2053 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 +02002054
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002055 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2056 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2057 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:')
2058 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002059enddef
2060
Bram Moolenaar89228602020-04-05 22:14:54 +02002061def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002062 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002063 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002064 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002065
2066 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002067 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002068
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002069 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002070 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002071 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002072
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002073 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002074enddef
2075
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002076def Test_func_common_type()
2077 def FuncOne(n: number): number
2078 return n
2079 enddef
2080 def FuncTwo(s: string): number
2081 return len(s)
2082 enddef
2083 def FuncThree(n: number, s: string): number
2084 return n + len(s)
2085 enddef
2086 var list = [FuncOne, FuncTwo, FuncThree]
2087 assert_equal(8, list[0](8))
2088 assert_equal(4, list[1]('word'))
2089 assert_equal(7, list[2](3, 'word'))
2090enddef
2091
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002092def MultiLine(
2093 arg1: string,
2094 arg2 = 1234,
2095 ...rest: list<string>
2096 ): string
2097 return arg1 .. arg2 .. join(rest, '-')
2098enddef
2099
Bram Moolenaar2c330432020-04-13 14:41:35 +02002100def MultiLineComment(
2101 arg1: string, # comment
2102 arg2 = 1234, # comment
2103 ...rest: list<string> # comment
2104 ): string # comment
2105 return arg1 .. arg2 .. join(rest, '-')
2106enddef
2107
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002108def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002109 MultiLine('text')->assert_equal('text1234')
2110 MultiLine('text', 777)->assert_equal('text777')
2111 MultiLine('text', 777, 'one')->assert_equal('text777one')
2112 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002113enddef
2114
Bram Moolenaar23e03252020-04-12 22:22:31 +02002115func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002116 call MultiLine('text')->assert_equal('text1234')
2117 call MultiLine('text', 777)->assert_equal('text777')
2118 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2119 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002120endfunc
2121
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002122
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002123" When using CheckScriptFailure() for the below test, E1010 is generated instead
2124" of E1056.
2125func Test_E1056_1059()
2126 let caught_1056 = 0
2127 try
2128 def F():
2129 return 1
2130 enddef
2131 catch /E1056:/
2132 let caught_1056 = 1
2133 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002134 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002135
2136 let caught_1059 = 0
2137 try
2138 def F5(items : list)
2139 echo 'a'
2140 enddef
2141 catch /E1059:/
2142 let caught_1059 = 1
2143 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002144 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002145endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002146
Bram Moolenaar015f4262020-05-05 21:25:22 +02002147func DelMe()
2148 echo 'DelMe'
2149endfunc
2150
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002151def Test_error_reporting()
2152 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002153 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002154 " comment
2155 def Func()
2156 # comment
2157 # comment
2158 invalid
2159 enddef
2160 defcompile
2161 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002162 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002163 try
2164 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002165 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002166 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002167 v:exception->assert_match('Invalid command: invalid')
2168 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002169 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002170 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002171
2172 # comment lines after the start of the function
2173 lines =<< trim END
2174 " comment
2175 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002176 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002177 # comment
2178 # comment
2179 invalid
2180 enddef
2181 defcompile
2182 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002183 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002184 try
2185 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002186 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002187 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002188 v:exception->assert_match('Invalid command: invalid')
2189 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002190 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002191 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002192
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002193 lines =<< trim END
2194 vim9script
2195 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002196 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002197 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002198 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002199 enddef
2200 defcompile
2201 Func()
2202 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002203 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002204 try
2205 source Xdef
2206 assert_report('should have failed')
2207 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002208 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002209 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002210 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002211
Bram Moolenaar08052222020-09-14 17:04:31 +02002212 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002213enddef
2214
Bram Moolenaar015f4262020-05-05 21:25:22 +02002215def Test_deleted_function()
2216 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002217 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002218 'delfunc g:DelMe',
2219 'echo RefMe()'], 'E117:')
2220enddef
2221
2222def Test_unknown_function()
2223 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002224 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002225 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002226enddef
2227
Bram Moolenaar328eac22021-01-07 19:23:08 +01002228def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002229 return Ref('more')
2230enddef
2231
2232def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002233 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002234 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002235enddef
2236
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002237def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002238 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002239 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002240enddef
2241
2242def Test_closure_ref_after_return()
2243 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002244 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002245 unlet g:Ref
2246enddef
2247
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002248def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002249 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002250 g:Extend = (s) => local->add(s)
2251 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002252enddef
2253
2254def Test_closure_two_refs()
2255 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002256 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002257 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002258 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002259 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002260 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002261
2262 unlet g:Extend
2263 unlet g:Read
2264enddef
2265
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002266def ReadRef(Ref: func(): list<string>): string
2267 return join(Ref(), ' ')
2268enddef
2269
Bram Moolenaar5e654232020-09-16 15:22:00 +02002270def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002271 Ref(add)
2272enddef
2273
2274def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002275 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002276 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002277 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002278 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002279 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002280 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002281
2282 unlet g:Extend
2283 unlet g:Read
2284enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002285
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002286def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002287 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002288 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002289enddef
2290
2291def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002292 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002293 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002294enddef
2295
2296def Test_closure_using_argument()
2297 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002298 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002299
2300 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002301 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002302
2303 unlet g:UseArg
2304 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002305
2306 var lines =<< trim END
2307 vim9script
2308 def Test(Fun: func(number): number): list<number>
2309 return map([1, 2, 3], (_, i) => Fun(i))
2310 enddef
2311 def Inc(nr: number): number
2312 return nr + 2
2313 enddef
2314 assert_equal([3, 4, 5], Test(Inc))
2315 END
2316 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002317enddef
2318
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002319def MakeGetAndAppendRefs()
2320 var local = 'a'
2321
2322 def Append(arg: string)
2323 local ..= arg
2324 enddef
2325 g:Append = Append
2326
2327 def Get(): string
2328 return local
2329 enddef
2330 g:Get = Get
2331enddef
2332
2333def Test_closure_append_get()
2334 MakeGetAndAppendRefs()
2335 g:Get()->assert_equal('a')
2336 g:Append('-b')
2337 g:Get()->assert_equal('a-b')
2338 g:Append('-c')
2339 g:Get()->assert_equal('a-b-c')
2340
2341 unlet g:Append
2342 unlet g:Get
2343enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002344
Bram Moolenaar04b12692020-05-04 23:24:44 +02002345def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002346 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002347 def Closure(arg: string): string
2348 return local .. arg
2349 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002350 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002351enddef
2352
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002353func GetResult(Ref)
2354 return a:Ref('some')
2355endfunc
2356
2357def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002358 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002359 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002360 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002361enddef
2362
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002363def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002364 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002365 vim9script
2366 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002367 var name = 0
2368 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002369 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002370 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002371 enddef
2372 Func()
2373 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002374 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002375enddef
2376
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002377def Test_nested_closure_used()
2378 var lines =<< trim END
2379 vim9script
2380 def Func()
2381 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002382 var Closure = () => x
2383 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002384 enddef
2385 Func()
2386 assert_equal('hello', g:Myclosure())
2387 END
2388 CheckScriptSuccess(lines)
2389enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002390
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002391def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002392 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002393 vim9script
2394 def FuncA()
2395 FuncB(0)
2396 enddef
2397 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002398 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002399 enddef
2400 FuncA()
2401 END
2402 CheckScriptFailure(lines, 'E1012:')
2403enddef
2404
Bram Moolenaarf112f302020-12-20 17:47:52 +01002405def Test_global_closure()
2406 var lines =<< trim END
2407 vim9script
2408 def ReverseEveryNLines(n: number, line1: number, line2: number)
2409 var mods = 'sil keepj keepp lockm '
2410 var range = ':' .. line1 .. ',' .. line2
2411 def g:Offset(): number
2412 var offset = (line('.') - line1 + 1) % n
2413 return offset != 0 ? offset : n
2414 enddef
2415 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2416 enddef
2417
2418 new
2419 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2420 ReverseEveryNLines(3, 1, 9)
2421 END
2422 CheckScriptSuccess(lines)
2423 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2424 assert_equal(expected, getline(1, 9))
2425 bwipe!
2426enddef
2427
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002428def Test_global_closure_called_directly()
2429 var lines =<< trim END
2430 vim9script
2431 def Outer()
2432 var x = 1
2433 def g:Inner()
2434 var y = x
2435 x += 1
2436 assert_equal(1, y)
2437 enddef
2438 g:Inner()
2439 assert_equal(2, x)
2440 enddef
2441 Outer()
2442 END
2443 CheckScriptSuccess(lines)
2444 delfunc g:Inner
2445enddef
2446
Bram Moolenaar69c76172021-12-02 16:38:52 +00002447def Test_closure_called_from_legacy()
2448 var lines =<< trim END
2449 vim9script
2450 def Func()
2451 var outer = 'foo'
2452 var F = () => {
2453 outer = 'bar'
2454 }
2455 execute printf('call %s()', string(F))
2456 enddef
2457 Func()
2458 END
2459 CheckScriptFailure(lines, 'E1248')
2460enddef
2461
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002462def Test_failure_in_called_function()
2463 # this was using the frame index as the return value
2464 var lines =<< trim END
2465 vim9script
2466 au TerminalWinOpen * eval [][0]
2467 def PopupTerm(a: any)
2468 # make sure typvals on stack are string
2469 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2470 FireEvent()
2471 enddef
2472 def FireEvent()
2473 do TerminalWinOpen
2474 enddef
2475 # use try/catch to make eval fail
2476 try
2477 call PopupTerm(0)
2478 catch
2479 endtry
2480 au! TerminalWinOpen
2481 END
2482 CheckScriptSuccess(lines)
2483enddef
2484
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002485def Test_nested_lambda()
2486 var lines =<< trim END
2487 vim9script
2488 def Func()
2489 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002490 var Lambda1 = () => 7
2491 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002492 var res = Lambda2()
2493 assert_equal([7, 4], res)
2494 enddef
2495 Func()
2496 END
2497 CheckScriptSuccess(lines)
2498enddef
2499
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002500def Test_double_nested_lambda()
2501 var lines =<< trim END
2502 vim9script
2503 def F(head: string): func(string): func(string): string
2504 return (sep: string): func(string): string => ((tail: string): string => {
2505 return head .. sep .. tail
2506 })
2507 enddef
2508 assert_equal('hello-there', F('hello')('-')('there'))
2509 END
2510 CheckScriptSuccess(lines)
2511enddef
2512
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002513def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002514 var lines =<< trim END
2515 vim9script
2516 def F(text: string): func(string): func(string): string
2517 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002518 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002519 })
2520 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002521 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002522 END
2523 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002524
2525 lines =<< trim END
2526 vim9script
2527 echo range(4)->mapnew((_, v) => {
2528 return range(v) ->mapnew((_, s) => {
2529 return string(s)
2530 })
2531 })
2532 END
2533 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002534
2535 lines =<< trim END
2536 vim9script
2537
2538 def s:func()
2539 range(10)
2540 ->mapnew((_, _) => ({
2541 key: range(10)->mapnew((_, _) => {
2542 return ' '
2543 }),
2544 }))
2545 enddef
2546
2547 defcomp
2548 END
2549 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002550enddef
2551
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002552def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002553 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002554 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002555enddef
2556
2557def Test_lambda_arg_shadows_func()
2558 assert_equal([42], Shadowed())
2559enddef
2560
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002561def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002562 var path: string = empty(dir)
2563 \ ? 'empty'
2564 \ : 'full'
2565 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002566enddef
2567
2568def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002569 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002570enddef
2571
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002572def Test_script_var_in_lambda()
2573 var lines =<< trim END
2574 vim9script
2575 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002576 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002577 END
2578 CheckScriptSuccess(lines)
2579enddef
2580
Bram Moolenaar5e654232020-09-16 15:22:00 +02002581def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002582 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002583 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002584 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002585 ->reverse()
2586 return x
2587enddef
2588
2589def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002590 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002591
2592 var lines =<< trim END
2593 vim9script
2594 var res = [{n: 1, m: 2, s: 'xxx'}]
2595 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2596 v.n,
2597 v.m,
2598 substitute(v.s, '.*', 'yyy', '')
2599 ))
2600 assert_equal(['1:2:yyy'], res)
2601 END
2602 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002603enddef
2604
Bram Moolenaarb6571982021-01-08 22:24:19 +01002605def Test_list_lambda()
2606 timer_start(1000, (_) => 0)
2607 var body = execute(timer_info()[0].callback
2608 ->string()
2609 ->substitute("('", ' ', '')
2610 ->substitute("')", '', '')
2611 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002612 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002613enddef
2614
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002615def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002616 var lines =<< trim END
2617 vim9script
2618 var flist: list<func>
2619 for i in range(10)
2620 var inloop = i
2621 flist[i] = () => inloop
2622 endfor
2623 END
2624 CheckScriptSuccess(lines)
2625
2626 lines =<< trim END
2627 vim9script
2628 if true
2629 var outloop = 5
2630 var flist: list<func>
2631 for i in range(10)
2632 flist[i] = () => outloop
2633 endfor
2634 endif
2635 END
2636 CheckScriptSuccess(lines)
2637
2638 lines =<< trim END
2639 vim9script
2640 if true
2641 var outloop = 5
2642 endif
2643 var flist: list<func>
2644 for i in range(10)
2645 flist[i] = () => outloop
2646 endfor
2647 END
2648 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002649
2650 lines =<< trim END
2651 vim9script
2652 for i in range(10)
2653 var Ref = () => 0
2654 endfor
2655 assert_equal(0, ((i) => 0)(0))
2656 END
2657 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002658enddef
2659
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002660def Test_legacy_lambda()
2661 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002662
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002663 var lines =<< trim END
2664 echo {x -> 'hello ' .. x}('foo')
2665 END
2666 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002667
2668 lines =<< trim END
2669 vim9script
2670 def Func()
2671 echo (() => 'no error')()
2672 enddef
2673 legacy call s:Func()
2674 END
2675 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002676enddef
2677
Bram Moolenaarce024c32021-06-26 13:00:49 +02002678def Test_legacy()
2679 var lines =<< trim END
2680 vim9script
2681 func g:LegacyFunction()
2682 let g:legacyvar = 1
2683 endfunc
2684 def Testit()
2685 legacy call g:LegacyFunction()
2686 enddef
2687 Testit()
2688 assert_equal(1, g:legacyvar)
2689 unlet g:legacyvar
2690 delfunc g:LegacyFunction
2691 END
2692 CheckScriptSuccess(lines)
2693enddef
2694
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002695def Test_legacy_errors()
2696 for cmd in ['if', 'elseif', 'else', 'endif',
2697 'for', 'endfor', 'continue', 'break',
2698 'while', 'endwhile',
2699 'try', 'catch', 'finally', 'endtry']
2700 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2701 endfor
2702enddef
2703
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002704def Test_call_legacy_with_dict()
2705 var lines =<< trim END
2706 vim9script
2707 func Legacy() dict
2708 let g:result = self.value
2709 endfunc
2710 def TestDirect()
2711 var d = {value: 'yes', func: Legacy}
2712 d.func()
2713 enddef
2714 TestDirect()
2715 assert_equal('yes', g:result)
2716 unlet g:result
2717
2718 def TestIndirect()
2719 var d = {value: 'foo', func: Legacy}
2720 var Fi = d.func
2721 Fi()
2722 enddef
2723 TestIndirect()
2724 assert_equal('foo', g:result)
2725 unlet g:result
2726
2727 var d = {value: 'bar', func: Legacy}
2728 d.func()
2729 assert_equal('bar', g:result)
2730 unlet g:result
2731 END
2732 CheckScriptSuccess(lines)
2733enddef
2734
Bram Moolenaarab360522021-01-10 14:02:28 +01002735def DoFilterThis(a: string): list<string>
2736 # closure nested inside another closure using argument
2737 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2738 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2739enddef
2740
2741def Test_nested_closure_using_argument()
2742 assert_equal(['x', 'x2'], DoFilterThis('x'))
2743enddef
2744
Bram Moolenaar0186e582021-01-10 18:33:11 +01002745def Test_triple_nested_closure()
2746 var what = 'x'
2747 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2748 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2749 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2750enddef
2751
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002752func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002753 CheckScreendump
2754
2755 let lines =<< trim END
2756 vim9script
2757 def EchoNothing()
2758 silent echo ''
2759 enddef
2760 defcompile
2761 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002762 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002763
2764 " Check that the balloon shows up after a mouse move
2765 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002766 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002767 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2768
2769 " clean up
2770 call StopVimInTerminal(buf)
2771 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002772endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002773
Bram Moolenaar171fb922020-10-28 16:54:47 +01002774def SilentlyError()
2775 execute('silent! invalid')
2776 g:did_it = 'yes'
2777enddef
2778
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002779func UserError()
2780 silent! invalid
2781endfunc
2782
2783def SilentlyUserError()
2784 UserError()
2785 g:did_it = 'yes'
2786enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002787
2788" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002789func Test_ignore_silent_error()
2790 let g:did_it = 'no'
2791 call SilentlyError()
2792 call assert_equal('yes', g:did_it)
2793
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002794 let g:did_it = 'no'
2795 call SilentlyUserError()
2796 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002797
2798 unlet g:did_it
2799endfunc
2800
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002801def Test_ignore_silent_error_in_filter()
2802 var lines =<< trim END
2803 vim9script
2804 def Filter(winid: number, key: string): bool
2805 if key == 'o'
2806 silent! eval [][0]
2807 return true
2808 endif
2809 return popup_filter_menu(winid, key)
2810 enddef
2811
Bram Moolenaare0de1712020-12-02 17:36:54 +01002812 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002813 feedkeys("o\r", 'xnt')
2814 END
2815 CheckScriptSuccess(lines)
2816enddef
2817
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002818def Fibonacci(n: number): number
2819 if n < 2
2820 return n
2821 else
2822 return Fibonacci(n - 1) + Fibonacci(n - 2)
2823 endif
2824enddef
2825
Bram Moolenaar985116a2020-07-12 17:31:09 +02002826def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002827 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002828enddef
2829
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002830def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002831 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002832 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002833 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002834 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002835 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002836enddef
2837
2838def Test_closure_in_map()
2839 mkdir('XclosureDir/tdir', 'p')
2840 writefile(['111'], 'XclosureDir/file1')
2841 writefile(['222'], 'XclosureDir/file2')
2842 writefile(['333'], 'XclosureDir/tdir/file3')
2843
Bram Moolenaare0de1712020-12-02 17:36:54 +01002844 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002845
2846 delete('XclosureDir', 'rf')
2847enddef
2848
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002849def Test_invalid_function_name()
2850 var lines =<< trim END
2851 vim9script
2852 def s: list<string>
2853 END
2854 CheckScriptFailure(lines, 'E129:')
2855
2856 lines =<< trim END
2857 vim9script
2858 def g: list<string>
2859 END
2860 CheckScriptFailure(lines, 'E129:')
2861
2862 lines =<< trim END
2863 vim9script
2864 def <SID>: list<string>
2865 END
2866 CheckScriptFailure(lines, 'E884:')
2867
2868 lines =<< trim END
2869 vim9script
2870 def F list<string>
2871 END
2872 CheckScriptFailure(lines, 'E488:')
2873enddef
2874
Bram Moolenaara90afb92020-07-15 22:38:56 +02002875def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002876 var lines =<< trim END
2877 var Xsetlist: func
2878 Xsetlist = function('setloclist', [0])
2879 Xsetlist([], ' ', {title: 'test'})
2880 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002881
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002882 Xsetlist = function('setloclist', [0, [], ' '])
2883 Xsetlist({title: 'test'})
2884 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002885
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002886 Xsetlist = function('setqflist')
2887 Xsetlist([], ' ', {title: 'test'})
2888 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002889
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002890 Xsetlist = function('setqflist', [[], ' '])
2891 Xsetlist({title: 'test'})
2892 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002893
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002894 var Len: func: number = function('len', ['word'])
2895 assert_equal(4, Len())
2896
2897 var RepeatFunc = function('repeat', ['o'])
2898 assert_equal('ooooo', RepeatFunc(5))
2899 END
2900 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002901
2902 lines =<< trim END
2903 vim9script
2904 def Foo(Parser: any)
2905 enddef
2906 var Expr: func(dict<any>): dict<any>
2907 const Call = Foo(Expr)
2908 END
2909 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002910enddef
2911
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002912def Test_cmd_modifier()
2913 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002914 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002915enddef
2916
2917def Test_restore_modifiers()
2918 # check that when compiling a :def function command modifiers are not messed
2919 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002920 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002921 vim9script
2922 set eventignore=
2923 autocmd QuickFixCmdPost * copen
2924 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002925 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002926 enddef
2927 func Func()
2928 noautocmd call s:AutocmdsDisabled()
2929 let g:ei_after = &eventignore
2930 endfunc
2931 Func()
2932 END
2933 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002934 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002935enddef
2936
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002937def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002938 eval 1 + 2
2939 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002940 # call not on fourth line
2941 StackBot()
2942enddef
2943
2944def StackBot()
2945 # throw an error
2946 eval [][0]
2947enddef
2948
2949def Test_callstack_def()
2950 try
2951 StackTop()
2952 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002953 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002954 endtry
2955enddef
2956
Bram Moolenaare8211a32020-10-09 22:04:29 +02002957" Re-using spot for variable used in block
2958def Test_block_scoped_var()
2959 var lines =<< trim END
2960 vim9script
2961 def Func()
2962 var x = ['a', 'b', 'c']
2963 if 1
2964 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002965 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002966 endif
2967 var z = x
2968 assert_equal(['x', 'x', 'x'], z)
2969 enddef
2970 Func()
2971 END
2972 CheckScriptSuccess(lines)
2973enddef
2974
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002975def Test_reset_did_emsg()
2976 var lines =<< trim END
2977 @s = 'blah'
2978 au BufWinLeave * #
2979 def Func()
2980 var winid = popup_create('popup', {})
2981 exe '*s'
2982 popup_close(winid)
2983 enddef
2984 Func()
2985 END
2986 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002987 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002988enddef
2989
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002990def Test_did_emsg_reset()
2991 # executing an autocommand resets did_emsg, this should not result in a
2992 # builtin function considered failing
2993 var lines =<< trim END
2994 vim9script
2995 au BufWinLeave * #
2996 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02002997 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002998 eval [][0]
2999 enddef
3000 nno <F3> <cmd>call <sid>Func()<cr>
3001 feedkeys("\<F3>\e", 'xt')
3002 END
3003 writefile(lines, 'XemsgReset')
3004 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3005 delete('XemsgReset')
3006 nunmap <F3>
3007 au! BufWinLeave
3008enddef
3009
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003010def Test_abort_with_silent_call()
3011 var lines =<< trim END
3012 vim9script
3013 g:result = 'none'
3014 def Func()
3015 g:result += 3
3016 g:result = 'yes'
3017 enddef
3018 # error is silenced, but function aborts on error
3019 silent! Func()
3020 assert_equal('none', g:result)
3021 unlet g:result
3022 END
3023 CheckScriptSuccess(lines)
3024enddef
3025
Bram Moolenaarf665e972020-12-05 19:17:16 +01003026def Test_continues_with_silent_error()
3027 var lines =<< trim END
3028 vim9script
3029 g:result = 'none'
3030 def Func()
3031 silent! g:result += 3
3032 g:result = 'yes'
3033 enddef
3034 # error is silenced, function does not abort
3035 Func()
3036 assert_equal('yes', g:result)
3037 unlet g:result
3038 END
3039 CheckScriptSuccess(lines)
3040enddef
3041
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003042def Test_abort_even_with_silent()
3043 var lines =<< trim END
3044 vim9script
3045 g:result = 'none'
3046 def Func()
3047 eval {-> ''}() .. '' .. {}['X']
3048 g:result = 'yes'
3049 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003050 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003051 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003052 unlet g:result
3053 END
3054 CheckScriptSuccess(lines)
3055enddef
3056
Bram Moolenaarf665e972020-12-05 19:17:16 +01003057def Test_cmdmod_silent_restored()
3058 var lines =<< trim END
3059 vim9script
3060 def Func()
3061 g:result = 'none'
3062 silent! g:result += 3
3063 g:result = 'none'
3064 g:result += 3
3065 enddef
3066 Func()
3067 END
3068 # can't use CheckScriptFailure, it ignores the :silent!
3069 var fname = 'Xdefsilent'
3070 writefile(lines, fname)
3071 var caught = 'no'
3072 try
3073 exe 'source ' .. fname
3074 catch /E1030:/
3075 caught = 'yes'
3076 assert_match('Func, line 4', v:throwpoint)
3077 endtry
3078 assert_equal('yes', caught)
3079 delete(fname)
3080enddef
3081
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003082def Test_cmdmod_silent_nested()
3083 var lines =<< trim END
3084 vim9script
3085 var result = ''
3086
3087 def Error()
3088 result ..= 'Eb'
3089 eval [][0]
3090 result ..= 'Ea'
3091 enddef
3092
3093 def Crash()
3094 result ..= 'Cb'
3095 sil! Error()
3096 result ..= 'Ca'
3097 enddef
3098
3099 Crash()
3100 assert_equal('CbEbEaCa', result)
3101 END
3102 CheckScriptSuccess(lines)
3103enddef
3104
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003105def Test_dict_member_with_silent()
3106 var lines =<< trim END
3107 vim9script
3108 g:result = 'none'
3109 var d: dict<any>
3110 def Func()
3111 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003112 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003113 catch
3114 endtry
3115 enddef
3116 silent! Func()
3117 assert_equal('0', g:result)
3118 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003119 END
3120 CheckScriptSuccess(lines)
3121enddef
3122
Bram Moolenaarf9041332021-01-21 19:41:16 +01003123def Test_skip_cmds_with_silent()
3124 var lines =<< trim END
3125 vim9script
3126
3127 def Func(b: bool)
3128 Crash()
3129 enddef
3130
3131 def Crash()
3132 sil! :/not found/d _
3133 sil! :/not found/put _
3134 enddef
3135
3136 Func(true)
3137 END
3138 CheckScriptSuccess(lines)
3139enddef
3140
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003141def Test_opfunc()
3142 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3143 def g:Opfunc(_: any): string
3144 setline(1, 'ASDF')
3145 return ''
3146 enddef
3147 new
3148 setline(1, 'asdf')
3149 feedkeys("\<F3>$", 'x')
3150 assert_equal('ASDF', getline(1))
3151
3152 bwipe!
3153 nunmap <F3>
3154enddef
3155
Bram Moolenaar077a4232020-12-22 18:33:27 +01003156" this was crashing on exit
3157def Test_nested_lambda_in_closure()
3158 var lines =<< trim END
3159 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003160 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003161 def Outer()
3162 def g:Inner()
3163 echo map([1, 2, 3], {_, v -> v + 1})
3164 enddef
3165 g:Inner()
3166 enddef
3167 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003168 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003169 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003170 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003171 return
3172 endif
3173 assert_equal(['Done'], readfile('XnestedDone'))
3174 delete('XnestedDone')
3175enddef
3176
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003177def Test_check_func_arg_types()
3178 var lines =<< trim END
3179 vim9script
3180 def F1(x: string): string
3181 return x
3182 enddef
3183
3184 def F2(x: number): number
3185 return x + 1
3186 enddef
3187
3188 def G(g: func): dict<func>
3189 return {f: g}
3190 enddef
3191
3192 def H(d: dict<func>): string
3193 return d.f('a')
3194 enddef
3195 END
3196
3197 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3198 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3199enddef
3200
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003201def Test_list_any_type_checked()
3202 var lines =<< trim END
3203 vim9script
3204 def Foo()
3205 --decl--
3206 Bar(l)
3207 enddef
3208 def Bar(ll: list<dict<any>>)
3209 enddef
3210 Foo()
3211 END
3212 lines[2] = 'var l: list<any>'
3213 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3214
3215 lines[2] = 'var l: list<any> = []'
3216 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3217
3218 lines[2] = 'var l: list<any> = [11]'
3219 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3220enddef
3221
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003222def Test_compile_error()
3223 var lines =<< trim END
3224 def g:Broken()
3225 echo 'a' + {}
3226 enddef
3227 call g:Broken()
3228 END
3229 # First call: compilation error
3230 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3231
3232 # Second call won't try compiling again
3233 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003234 delfunc g:Broken
3235
3236 # No error when compiling with :silent!
3237 lines =<< trim END
3238 def g:Broken()
3239 echo 'a' + []
3240 enddef
3241 silent! defcompile
3242 END
3243 CheckScriptSuccess(lines)
3244
3245 # Calling the function won't try compiling again
3246 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3247 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003248enddef
3249
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003250def Test_ignored_argument()
3251 var lines =<< trim END
3252 vim9script
3253 def Ignore(_, _): string
3254 return 'yes'
3255 enddef
3256 assert_equal('yes', Ignore(1, 2))
3257
3258 func Ok(_)
3259 return a:_
3260 endfunc
3261 assert_equal('ok', Ok('ok'))
3262
3263 func Oktoo()
3264 let _ = 'too'
3265 return _
3266 endfunc
3267 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003268
3269 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003270 END
3271 CheckScriptSuccess(lines)
3272
3273 lines =<< trim END
3274 def Ignore(_: string): string
3275 return _
3276 enddef
3277 defcompile
3278 END
3279 CheckScriptFailure(lines, 'E1181:', 1)
3280
3281 lines =<< trim END
3282 var _ = 1
3283 END
3284 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003285
3286 lines =<< trim END
3287 var x = _
3288 END
3289 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003290enddef
3291
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003292def Test_too_many_arguments()
3293 var lines =<< trim END
3294 echo [0, 1, 2]->map(() => 123)
3295 END
3296 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3297
3298 lines =<< trim END
3299 echo [0, 1, 2]->map((_) => 123)
3300 END
3301 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3302enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003303
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003304def Test_closing_brace_at_start_of_line()
3305 var lines =<< trim END
3306 def Func()
3307 enddef
3308 Func(
3309 )
3310 END
3311 call CheckDefAndScriptSuccess(lines)
3312enddef
3313
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003314func CreateMydict()
3315 let g:mydict = {}
3316 func g:mydict.afunc()
3317 let g:result = self.key
3318 endfunc
3319endfunc
3320
3321def Test_numbered_function_reference()
3322 CreateMydict()
3323 var output = execute('legacy func g:mydict.afunc')
3324 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3325 execute 'function(' .. funcName .. ', [], {key: 42})()'
3326 # check that the function still exists
3327 assert_equal(output, execute('legacy func g:mydict.afunc'))
3328 unlet g:mydict
3329enddef
3330
Bram Moolenaar20677332021-06-06 17:02:53 +02003331if has('python3')
3332 def Test_python3_heredoc()
3333 py3 << trim EOF
3334 import vim
3335 vim.vars['didit'] = 'yes'
3336 EOF
3337 assert_equal('yes', g:didit)
3338
3339 python3 << trim EOF
3340 import vim
3341 vim.vars['didit'] = 'again'
3342 EOF
3343 assert_equal('again', g:didit)
3344 enddef
3345endif
3346
3347" This messes up syntax highlight, keep near the end.
3348if has('lua')
3349 def Test_lua_heredoc()
3350 g:d = {}
3351 lua << trim EOF
3352 x = vim.eval('g:d')
3353 x['key'] = 'val'
3354 EOF
3355 assert_equal('val', g:d.key)
3356 enddef
3357endif
3358
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003359
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003360" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker