blob: f0c4c0d91c91c0e0412d15278a84b337ea95e09e [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 Moolenaar5deeb3f2020-04-05 17:08:17 +0200441func Increment()
442 let g:counter += 1
443endfunc
444
445def Test_call_ufunc_count()
446 g:counter = 1
447 Increment()
448 Increment()
449 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200450 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200451 g:counter->assert_equal(4)
452 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200453 unlet g:counter
454enddef
455
456def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200457 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200458 for s in rest
459 res ..= ',' .. s
460 endfor
461 return res
462enddef
463
464def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200465 MyVarargs('one')->assert_equal('one')
466 MyVarargs('one', 'two')->assert_equal('one,two')
467 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200468enddef
469
470def MyDefaultArgs(name = 'string'): string
471 return name
472enddef
473
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200474def MyDefaultSecond(name: string, second: bool = true): string
475 return second ? name : 'none'
476enddef
477
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200478
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200479def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200480 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200481 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200482 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200483 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200484
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200485 MyDefaultSecond('test')->assert_equal('test')
486 MyDefaultSecond('test', true)->assert_equal('test')
487 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200488
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200489 var lines =<< trim END
490 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
491 return name .. aa .. bb
492 enddef
493
494 MyDefaultThird('->')->assert_equal('->aabb')
495 MyDefaultThird('->', v:none)->assert_equal('->aabb')
496 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
497 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
498 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
499 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
500 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200501
502 def DefArg(mandatory: any, optional = mandatory): string
503 return mandatory .. optional
504 enddef
505 DefArg(1234)->assert_equal('12341234')
506 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200507 END
508 CheckDefAndScriptSuccess(lines)
509
Bram Moolenaar822ba242020-05-24 23:00:18 +0200510 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100511 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200512 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 +0100513 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200514 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100517 vim9script
518 def Func(a = b == 0 ? 1 : 2, b = 0)
519 enddef
520 defcompile
521 END
522 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200523enddef
524
Bram Moolenaarcef12702021-01-04 14:09:43 +0100525def FuncWithComment( # comment
526 a: number, #comment
527 b: bool, # comment
528 c: string) #comment
529 assert_equal(4, a)
530 assert_equal(true, b)
531 assert_equal('yes', c)
532enddef
533
534def Test_func_with_comments()
535 FuncWithComment(4, true, 'yes')
536
537 var lines =<< trim END
538 def Func(# comment
539 arg: string)
540 enddef
541 END
542 CheckScriptFailure(lines, 'E125:', 1)
543
544 lines =<< trim END
545 def Func(
546 arg: string# comment
547 )
548 enddef
549 END
550 CheckScriptFailure(lines, 'E475:', 2)
551
552 lines =<< trim END
553 def Func(
554 arg: string
555 )# comment
556 enddef
557 END
558 CheckScriptFailure(lines, 'E488:', 3)
559enddef
560
Bram Moolenaar04b12692020-05-04 23:24:44 +0200561def Test_nested_function()
562 def Nested(arg: string): string
563 return 'nested ' .. arg
564 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200565 Nested('function')->assert_equal('nested function')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200566
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200567 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
568 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
569
Bram Moolenaar04b12692020-05-04 23:24:44 +0200570 CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200571 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
572 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200573
Bram Moolenaar54021752020-12-06 18:50:36 +0100574 var lines =<< trim END
575 def Outer()
576 def Inner()
577 # comment
578 enddef
579 def Inner()
580 enddef
581 enddef
582 END
583 CheckDefFailure(lines, 'E1073:')
584
585 lines =<< trim END
586 def Outer()
587 def Inner()
588 # comment
589 enddef
590 def! Inner()
591 enddef
592 enddef
593 END
594 CheckDefFailure(lines, 'E1117:')
595
596 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100597 lines =<< trim END
598 vim9script
599 var thecount = 0
600 if true
601 def Test(): number
602 def TheFunc(): number
603 thecount += 1
604 return thecount
605 enddef
606 return TheFunc()
607 enddef
608 endif
609 defcompile
610 assert_equal(1, Test())
611 assert_equal(2, Test())
612 END
613 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100614
615 # also works when "thecount" is inside the "if" block
616 lines =<< trim END
617 vim9script
618 if true
619 var thecount = 0
620 def Test(): number
621 def TheFunc(): number
622 thecount += 1
623 return thecount
624 enddef
625 return TheFunc()
626 enddef
627 endif
628 defcompile
629 assert_equal(1, Test())
630 assert_equal(2, Test())
631 END
632 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200633
634 lines =<< trim END
635 vim9script
636 def Outer()
637 def Inner()
638 echo 'hello'
639 enddef burp
640 enddef
641 defcompile
642 END
643 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200644enddef
645
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100646def Test_not_nested_function()
647 echo printf('%d',
648 function('len')('xxx'))
649enddef
650
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200651func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200652 call MyDefaultArgs()->assert_equal('string')
653 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200654 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200655endfunc
656
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200657def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200658 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200659 vim9script
660 def Outer()
661 def g:Inner(): string
662 return 'inner'
663 enddef
664 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200665 defcompile
666 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200667 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200668 delfunc g:Inner
669 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200670 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200671 delfunc g:Inner
672 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200673 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200674 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200675 END
676 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200677
678 lines =<< trim END
679 vim9script
680 def Outer()
681 def g:Inner(): string
682 return 'inner'
683 enddef
684 enddef
685 defcompile
686 Outer()
687 Outer()
688 END
689 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100690 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200691
692 lines =<< trim END
693 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100694 def Outer()
695 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100696 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100697 enddef
698 g:Inner()
699 enddef
700 Outer()
701 END
702 CheckScriptSuccess(lines)
703 delfunc g:Inner
704
705 lines =<< trim END
706 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200707 def Func()
708 echo 'script'
709 enddef
710 def Outer()
711 def Func()
712 echo 'inner'
713 enddef
714 enddef
715 defcompile
716 END
717 CheckScriptFailure(lines, "E1073:")
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200718enddef
719
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100720def DefListAll()
721 def
722enddef
723
724def DefListOne()
725 def DefListOne
726enddef
727
728def DefListMatches()
729 def /DefList
730enddef
731
732def Test_nested_def_list()
733 var funcs = split(execute('call DefListAll()'), "\n")
734 assert_true(len(funcs) > 10)
735 assert_true(funcs->index('def DefListAll()') >= 0)
736
737 funcs = split(execute('call DefListOne()'), "\n")
738 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
739
740 funcs = split(execute('call DefListMatches()'), "\n")
741 assert_true(len(funcs) >= 3)
742 assert_true(funcs->index('def DefListAll()') >= 0)
743 assert_true(funcs->index('def DefListOne()') >= 0)
744 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100745
746 var lines =<< trim END
747 vim9script
748 def Func()
749 def +Func+
750 enddef
751 defcompile
752 END
753 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100754enddef
755
Bram Moolenaar333894b2020-08-01 18:53:07 +0200756def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200757 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200758 vim9script
759 def g:Func(): string
760 return 'global'
761 enddef
762 def Func(): string
763 return 'local'
764 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200765 g:Func()->assert_equal('global')
766 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100767 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200768 END
769 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200770
771 lines =<< trim END
772 vim9script
773 def g:Funcy()
774 echo 'funcy'
775 enddef
776 s:Funcy()
777 END
778 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200779enddef
780
Bram Moolenaar0f769812020-09-12 18:32:34 +0200781def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200782 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200783 vim9script
784 def g:Gfunc(): string
785 return 'global'
786 enddef
787 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200788 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200789 return Gfunc('testing')
790 enddef
791 g:Gfunc()->assert_equal('global')
792 AnotherFunc()->assert_equal(7)
793 delfunc g:Gfunc
794 END
795 CheckScriptSuccess(lines)
796
797 lines =<< trim END
798 vim9script
799 def g:Func(): string
800 return 'global'
801 enddef
802 def AnotherFunc()
803 g:Func = function('len')
804 enddef
805 AnotherFunc()
806 END
807 CheckScriptFailure(lines, 'E705:')
808 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200809
810 # global function is found without g: prefix
811 lines =<< trim END
812 vim9script
813 def g:Func(): string
814 return 'global'
815 enddef
816 def AnotherFunc(): string
817 return Func()
818 enddef
819 assert_equal('global', AnotherFunc())
820 delfunc g:Func
821 END
822 CheckScriptSuccess(lines)
823
824 lines =<< trim END
825 vim9script
826 def g:Func(): string
827 return 'global'
828 enddef
829 assert_equal('global', Func())
830 delfunc g:Func
831 END
832 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200833enddef
834
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200835func TakesOneArg(arg)
836 echo a:arg
837endfunc
838
839def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200840 CheckDefFailure(['TakesOneArg()'], 'E119:')
841 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
842 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
843 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200844
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200845 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200846 vim9script
847 def Func(s: string)
848 echo s
849 enddef
850 Func([])
851 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200852 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200853
854 lines =<< trim END
855 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100856 var name = 'piet'
857 def FuncOne(name: string)
858 echo nr
859 enddef
860 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100861 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100862
863 lines =<< trim END
864 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200865 def FuncOne(nr: number)
866 echo nr
867 enddef
868 def FuncTwo()
869 FuncOne()
870 enddef
871 defcompile
872 END
873 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200874 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200875 try
876 source Xscript
877 catch
878 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
879 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
880 didCatch = true
881 endtry
882 assert_true(didCatch)
883
884 lines =<< trim END
885 vim9script
886 def FuncOne(nr: number)
887 echo nr
888 enddef
889 def FuncTwo()
890 FuncOne(1, 2)
891 enddef
892 defcompile
893 END
894 writefile(lines, 'Xscript')
895 didCatch = false
896 try
897 source Xscript
898 catch
899 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
900 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
901 didCatch = true
902 endtry
903 assert_true(didCatch)
904
905 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200906enddef
907
Bram Moolenaar50824712020-12-20 21:10:17 +0100908def Test_call_funcref_wrong_args()
909 var head =<< trim END
910 vim9script
911 def Func3(a1: string, a2: number, a3: list<number>)
912 echo a1 .. a2 .. a3[0]
913 enddef
914 def Testme()
915 var funcMap: dict<func> = {func: Func3}
916 END
917 var tail =<< trim END
918 enddef
919 Testme()
920 END
921 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
922
923 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
924 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100925
926 var lines =<< trim END
927 vim9script
928 var Ref: func(number): any
929 Ref = (j) => !j
930 echo Ref(false)
931 END
932 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
933
934 lines =<< trim END
935 vim9script
936 var Ref: func(number): any
937 Ref = (j) => !j
938 call Ref(false)
939 END
940 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +0100941enddef
942
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100943def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +0200944 var lines =<< trim END
945 var Callback = (..._) => 'anything'
946 assert_equal('anything', Callback())
947 assert_equal('anything', Callback(1))
948 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +0200949
950 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +0200951 END
952 CheckDefAndScriptSuccess(lines)
953
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100954 CheckDefFailure(['echo ((i) => 0)()'],
955 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100956
Bram Moolenaar2a389082021-04-09 20:24:31 +0200957 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100958 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100959 echo Ref(1, 'x')
960 END
961 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +0100962
963 lines =<< trim END
964 var Ref: func(job, string, number)
965 Ref = (x, y) => 0
966 END
967 CheckDefAndScriptFailure(lines, 'E1012:')
968
969 lines =<< trim END
970 var Ref: func(job, string)
971 Ref = (x, y, z) => 0
972 END
973 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100974
975 lines =<< trim END
976 var one = 1
977 var l = [1, 2, 3]
978 echo map(l, (one) => one)
979 END
980 CheckDefFailure(lines, 'E1167:')
981 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
982
983 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +0200984 var Ref: func(any, ?any): bool
985 Ref = (_, y = 1) => false
986 END
987 CheckDefAndScriptFailure(lines, 'E1172:')
988
989 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +0200990 var a = 0
991 var b = (a == 0 ? 1 : 2)
992 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200993 var txt = 'a'
994 b = (txt =~ 'x' ? 1 : 2)
995 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +0200996 END
997 CheckDefAndScriptSuccess(lines)
998
999 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001000 def ShadowLocal()
1001 var one = 1
1002 var l = [1, 2, 3]
1003 echo map(l, (one) => one)
1004 enddef
1005 END
1006 CheckDefFailure(lines, 'E1167:')
1007
1008 lines =<< trim END
1009 def Shadowarg(one: number)
1010 var l = [1, 2, 3]
1011 echo map(l, (one) => one)
1012 enddef
1013 END
1014 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001015
1016 lines =<< trim END
1017 echo ((a) => a)('aa', 'bb')
1018 END
1019 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001020
1021 lines =<< trim END
1022 echo 'aa'->((a) => a)('bb')
1023 END
1024 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1025 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001026enddef
1027
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001028def FilterWithCond(x: string, Cond: func(string): bool): bool
1029 return Cond(x)
1030enddef
1031
Bram Moolenaar0346b792021-01-31 22:18:29 +01001032def Test_lambda_return_type()
1033 var lines =<< trim END
1034 var Ref = (): => 123
1035 END
1036 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001037
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001038 # no space before the return type
1039 lines =<< trim END
1040 var Ref = (x):number => x + 1
1041 END
1042 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1043
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001044 # this works
1045 for x in ['foo', 'boo']
1046 echo FilterWithCond(x, (v) => v =~ '^b')
1047 endfor
1048
1049 # this fails
1050 lines =<< trim END
1051 echo FilterWithCond('foo', (v) => v .. '^b')
1052 END
1053 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001054
1055 lines =<< trim END
1056 var Lambda1 = (x) => {
1057 return x
1058 }
1059 assert_equal('asdf', Lambda1('asdf'))
1060 var Lambda2 = (x): string => {
1061 return x
1062 }
1063 assert_equal('foo', Lambda2('foo'))
1064 END
1065 CheckDefAndScriptSuccess(lines)
1066
1067 lines =<< trim END
1068 var Lambda = (x): string => {
1069 return x
1070 }
1071 echo Lambda(['foo'])
1072 END
1073 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001074enddef
1075
Bram Moolenaar709664c2020-12-12 14:33:41 +01001076def Test_lambda_uses_assigned_var()
1077 CheckDefSuccess([
1078 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001079 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001080enddef
1081
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001082def Test_pass_legacy_lambda_to_def_func()
1083 var lines =<< trim END
1084 vim9script
1085 func Foo()
1086 eval s:Bar({x -> 0})
1087 endfunc
1088 def Bar(y: any)
1089 enddef
1090 Foo()
1091 END
1092 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001093
1094 lines =<< trim END
1095 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001096 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001097 enddef
1098 legacy call g:TestFunc({-> 0})
1099 delfunc g:TestFunc
1100
1101 def g:TestFunc(f: func(number))
1102 enddef
1103 legacy call g:TestFunc({nr -> 0})
1104 delfunc g:TestFunc
1105 END
1106 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001107enddef
1108
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001109" Default arg and varargs
1110def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001111 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001112 for s in rest
1113 res ..= ',' .. s
1114 endfor
1115 return res
1116enddef
1117
1118def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001119 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001120 MyDefVarargs('one')->assert_equal('one,foo')
1121 MyDefVarargs('one', 'two')->assert_equal('one,two')
1122 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001123 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001124 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001125 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001126 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001127
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001128 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001129 vim9script
1130 def Func(...l: list<string>)
1131 echo l
1132 enddef
1133 Func('a', 'b', 'c')
1134 END
1135 CheckScriptSuccess(lines)
1136
1137 lines =<< trim END
1138 vim9script
1139 def Func(...l: list<string>)
1140 echo l
1141 enddef
1142 Func()
1143 END
1144 CheckScriptSuccess(lines)
1145
1146 lines =<< trim END
1147 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001148 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001149 echo l
1150 enddef
1151 Func(0)
1152 END
1153 CheckScriptSuccess(lines)
1154
1155 lines =<< trim END
1156 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001157 def Func(...l: any)
1158 echo l
1159 enddef
1160 Func(0)
1161 END
1162 CheckScriptFailure(lines, 'E1180:', 2)
1163
1164 lines =<< trim END
1165 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001166 def Func(..._l: list<string>)
1167 echo _l
1168 enddef
1169 Func('a', 'b', 'c')
1170 END
1171 CheckScriptSuccess(lines)
1172
1173 lines =<< trim END
1174 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001175 def Func(...l: list<string>)
1176 echo l
1177 enddef
1178 Func(1, 2, 3)
1179 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001180 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001181
1182 lines =<< trim END
1183 vim9script
1184 def Func(...l: list<string>)
1185 echo l
1186 enddef
1187 Func('a', 9)
1188 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001189 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001190
1191 lines =<< trim END
1192 vim9script
1193 def Func(...l: list<string>)
1194 echo l
1195 enddef
1196 Func(1, 'a')
1197 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001198 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001199
1200 lines =<< trim END
1201 vim9script
1202 def Func( # some comment
1203 ...l = []
1204 )
1205 echo l
1206 enddef
1207 END
1208 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001209
1210 lines =<< trim END
1211 vim9script
1212 def DoIt()
1213 g:Later('')
1214 enddef
1215 defcompile
1216 def g:Later(...l: list<number>)
1217 enddef
1218 DoIt()
1219 END
1220 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001221enddef
1222
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001223let s:value = ''
1224
1225def FuncOneDefArg(opt = 'text')
1226 s:value = opt
1227enddef
1228
1229def FuncTwoDefArg(nr = 123, opt = 'text'): string
1230 return nr .. opt
1231enddef
1232
1233def FuncVarargs(...arg: list<string>): string
1234 return join(arg, ',')
1235enddef
1236
1237def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001238 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001239 RefDefArg = FuncOneDefArg
1240 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001241 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001242 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001243 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001244
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001245 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001246 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001247 RefDef2Arg()->assert_equal('123text')
1248 RefDef2Arg(99)->assert_equal('99text')
1249 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001250
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001251 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1252 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001253
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001254 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001255 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001256 RefVarargs()->assert_equal('')
1257 RefVarargs('one')->assert_equal('one')
1258 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001259
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001260 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1261 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001262enddef
1263
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001264" Only varargs
1265def MyVarargsOnly(...args: list<string>): string
1266 return join(args, ',')
1267enddef
1268
1269def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001270 MyVarargsOnly()->assert_equal('')
1271 MyVarargsOnly('one')->assert_equal('one')
1272 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001273 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1274 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001275enddef
1276
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001277def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001278 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001279 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001280 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001281enddef
1282
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001283def DictArg(arg: dict<string>)
1284 arg['key'] = 'value'
1285enddef
1286
1287def ListArg(arg: list<string>)
1288 arg[0] = 'value'
1289enddef
1290
1291def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001292 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001293 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001294 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001295 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001296 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001297 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001298 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001299
Bram Moolenaard2c61702020-09-06 15:58:36 +02001300 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001301 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001302enddef
1303
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001304" These argument names are reserved in legacy functions.
1305def WithReservedNames(firstline: string, lastline: string): string
1306 return firstline .. lastline
1307enddef
1308
1309def Test_argument_names()
1310 assert_equal('OK', WithReservedNames('O', 'K'))
1311enddef
1312
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001313def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001314 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001315 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001316enddef
1317
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001318func DefinedLater(arg)
1319 return a:arg
1320endfunc
1321
1322def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001323 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001324 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
1325 assert_fails('g:NotAFunc()', 'E117:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001326
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001327 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001328 vim9script
1329 def RetNumber(): number
1330 return 123
1331 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001332 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001333 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001334 END
1335 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001336
1337 lines =<< trim END
1338 vim9script
1339 def RetNumber(): number
1340 return 123
1341 enddef
1342 def Bar(F: func: number): number
1343 return F()
1344 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001345 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001346 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001347 END
1348 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001349
1350 lines =<< trim END
1351 vim9script
1352 def UseNumber(nr: number)
1353 echo nr
1354 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001355 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001356 Funcref(123)
1357 END
1358 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001359
1360 lines =<< trim END
1361 vim9script
1362 def UseNumber(nr: number)
1363 echo nr
1364 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001365 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001366 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001367 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001368
1369 lines =<< trim END
1370 vim9script
1371 def EchoNr(nr = 34)
1372 g:echo = nr
1373 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001374 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001375 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001376 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001377 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001378 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001379 END
1380 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001381
1382 lines =<< trim END
1383 vim9script
1384 def EchoList(...l: list<number>)
1385 g:echo = l
1386 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001387 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001388 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001389 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001390 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001391 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001392 END
1393 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001394
1395 lines =<< trim END
1396 vim9script
1397 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1398 g:optarg = opt
1399 g:listarg = l
1400 return nr
1401 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001402 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001403 Funcref(10)->assert_equal(10)
1404 g:optarg->assert_equal(12)
1405 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001406
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001407 Funcref(11, 22)->assert_equal(11)
1408 g:optarg->assert_equal(22)
1409 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001410
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001411 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1412 g:optarg->assert_equal(18)
1413 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001414 END
1415 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001416enddef
1417
1418let SomeFunc = function('len')
1419let NotAFunc = 'text'
1420
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001421def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001422 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001423 var Ref1: func(bool): string
1424 var Ref2: func(bool): number
1425 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001426 Ref3 = g:cond ? Ref1 : Ref2
1427
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001428 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001429 var Refa1: func(bool): number
1430 var Refa2: func(bool, number): number
1431 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001432 Refa3 = g:cond ? Refa1 : Refa2
1433
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001434 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001435 var Refb1: func(bool, string): number
1436 var Refb2: func(string, number): number
1437 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001438 Refb3 = g:cond ? Refb1 : Refb2
1439enddef
1440
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001441def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001442 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001443enddef
1444
1445def DefinedEvenLater(arg: string): string
1446 return arg
1447enddef
1448
1449def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001450 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001451 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001452enddef
1453
1454def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001455 CheckScriptFailure([
1456 'def Func(): number',
1457 'return "a"',
1458 'enddef',
1459 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001460 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001461 CheckScriptFailure([
1462 'def Func(): string',
1463 'return 1',
1464 'enddef',
1465 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001466 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001467 CheckScriptFailure([
1468 'def Func(): void',
1469 'return "a"',
1470 'enddef',
1471 'defcompile'],
1472 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001473 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001474 CheckScriptFailure([
1475 'def Func()',
1476 'return "a"',
1477 'enddef',
1478 'defcompile'],
1479 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001480 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001481
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001482 CheckScriptFailure([
1483 'def Func(): number',
1484 'return',
1485 'enddef',
1486 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001487 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001488
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001489 CheckScriptFailure([
1490 'def Func():number',
1491 'return 123',
1492 'enddef',
1493 'defcompile'], 'E1069:')
1494 delfunc! g:Func
1495
1496 CheckScriptFailure([
1497 'def Func() :number',
1498 'return 123',
1499 'enddef',
1500 'defcompile'], 'E1059:')
1501 delfunc! g:Func
1502
1503 CheckScriptFailure([
1504 'def Func() : number',
1505 'return 123',
1506 'enddef',
1507 'defcompile'], 'E1059:')
1508 delfunc! g:Func
1509
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001510 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001511 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001512 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001513 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001514 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001515 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001516
1517 CheckScriptFailure([
1518 'vim9script',
1519 'def FuncB()',
1520 ' return 123',
1521 'enddef',
1522 'def FuncA()',
1523 ' FuncB()',
1524 'enddef',
1525 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001526enddef
1527
1528def Test_arg_type_wrong()
1529 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001530 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001531 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001532 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001533 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1534 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001535enddef
1536
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001537def Test_white_space_before_comma()
1538 var lines =<< trim END
1539 vim9script
1540 def Func(a: number , b: number)
1541 enddef
1542 END
1543 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001544 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001545enddef
1546
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001547def Test_white_space_after_comma()
1548 var lines =<< trim END
1549 vim9script
1550 def Func(a: number,b: number)
1551 enddef
1552 END
1553 CheckScriptFailure(lines, 'E1069:')
1554
1555 # OK in legacy function
1556 lines =<< trim END
1557 vim9script
1558 func Func(a,b)
1559 endfunc
1560 END
1561 CheckScriptSuccess(lines)
1562enddef
1563
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001564def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001565 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001566 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001567 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001568 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001569 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001570 enddef
1571 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001572 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001573
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001574 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001575 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001576 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001577
Bram Moolenaar67979662020-06-20 22:50:47 +02001578 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001579 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001580 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001581
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001582 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001583 def ListFunc(arg: list<number>)
1584 listvar = arg
1585 enddef
1586 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001587 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001588
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001589 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001590 def DictFunc(arg: dict<number>)
1591 dictvar = arg
1592 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001593 {a: 1, b: 2}->DictFunc()
1594 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001595 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001596 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001597 enddef
1598 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001599 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001600
Bram Moolenaare0de1712020-12-02 17:36:54 +01001601 {a: 3, b: 4}->DictFunc()
1602 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001603
1604 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001605 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001606 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001607 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001608
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001609 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001610 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001611 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001612 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001613 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001614 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001615
1616 def UseString()
1617 'xyork'->MyFunc()
1618 enddef
1619 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001620 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001621
Bram Moolenaar10409562020-07-29 20:00:38 +02001622 def UseString2()
1623 "knife"->MyFunc()
1624 enddef
1625 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001626 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001627
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001628 # prepending a colon makes it a mark
1629 new
1630 setline(1, ['aaa', 'bbb', 'ccc'])
1631 normal! 3Gmt1G
1632 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001633 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001634 bwipe!
1635
Bram Moolenaare6b53242020-07-01 17:28:33 +02001636 MyFunc(
1637 'continued'
1638 )
1639 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001640 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001641 )
1642
1643 call MyFunc(
1644 'more'
1645 ..
1646 'lines'
1647 )
1648 assert_equal(
1649 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001650 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001651 END
1652 writefile(lines, 'Xcall.vim')
1653 source Xcall.vim
1654 delete('Xcall.vim')
1655enddef
1656
1657def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001658 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001659 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001660 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001661 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001662 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001663 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001664 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001665 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001666 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001667enddef
1668
Bram Moolenaar65b95452020-07-19 14:03:09 +02001669def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001670 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001671 vim9script
1672 def MyFunc(arg: string)
1673 echo arg
1674 enddef
1675 MyFunc(1234)
1676 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001677 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001678enddef
1679
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001680def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001681 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001682 vim9script
1683 const var = ''
1684 def MyFunc(arg: string)
1685 var = 'asdf'
1686 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001687 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001688 END
1689 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001690 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001691 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001692
1693 lines =<< trim END
1694 const g:Aconst = 77
1695 def Change()
1696 # comment
1697 g:Aconst = 99
1698 enddef
1699 call Change()
1700 unlet g:Aconst
1701 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001702 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001703enddef
1704
1705" Test that inside :function a Python function can be defined, :def is not
1706" recognized.
1707func Test_function_python()
1708 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001709 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001710 execute py "<< EOF"
1711def do_something():
1712 return 1
1713EOF
1714endfunc
1715
1716def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001717 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001718 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001719 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001720 echo 'hello'
1721 enddef
1722
1723 def CallGoneSoon()
1724 GoneSoon()
1725 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001726 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001727
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001728 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001729 CallGoneSoon()
1730 END
1731 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001732 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1733 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001734
1735 delete('XToDelFunc')
1736enddef
1737
1738def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001739 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001740 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001741 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001742 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001743 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001744 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001745 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001746 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001747 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001748
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001749 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001750 g:Func1()->assert_equal('Func1')
1751 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001752
1753 delfunc! Func0
1754 delfunc! Func1
1755 delfunc! Func2
1756enddef
1757
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001758def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001759 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001760 vim9script
1761 func Func(arg)
1762 echo a:arg
1763 endfunc
1764 Func('text')
1765 END
1766 writefile(lines, 'XVim9Func')
1767 so XVim9Func
1768
1769 delete('XVim9Func')
1770enddef
1771
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001772let s:funcResult = 0
1773
1774def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001775 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001776enddef
1777
1778def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001779 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001780 return 1234
1781enddef
1782
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001783def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001784 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001785 return 'text'
1786enddef
1787
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001788def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001789 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001790enddef
1791
1792def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001793 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001794 return arg
1795enddef
1796
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001797def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001798 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001799enddef
1800
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001801def FuncOneArgRetString(arg: string): string
1802 return arg
1803enddef
1804
Bram Moolenaar89228602020-04-05 22:14:54 +02001805def FuncOneArgRetAny(arg: any): any
1806 return arg
1807enddef
1808
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001809def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001810 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001811 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001812 Ref1 = FuncNoArgNoRet
1813 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001814 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001815
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001816 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001817 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001818 Ref2 = FuncNoArgNoRet
1819 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001820 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001821
Bram Moolenaar53900992020-08-22 19:02:02 +02001822 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001823 Ref2 = FuncOneArgNoRet
1824 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001825 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001826
Bram Moolenaar53900992020-08-22 19:02:02 +02001827 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001828 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001829 Ref2()->assert_equal(1234)
1830 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001831
Bram Moolenaar53900992020-08-22 19:02:02 +02001832 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001833 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001834 Ref2(13)->assert_equal(13)
1835 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001836enddef
1837
Bram Moolenaar9978d472020-07-05 16:01:56 +02001838def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001839 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02001840 for n in repeat([1], 3)
1841 res += n
1842 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001843 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02001844
1845 res = 0
1846 for n in add([1, 2], 3)
1847 res += n
1848 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001849 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02001850enddef
1851
Bram Moolenaar846178a2020-07-05 17:04:13 +02001852def Test_argv_return_type()
1853 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001854 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02001855 for name in argv()
1856 res ..= name
1857 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001858 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02001859enddef
1860
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001861def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001862 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001863 RefVoid = FuncNoArgNoRet
1864 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001865 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
1866 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001867
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001868 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001869 RefAny = FuncNoArgRetNumber
1870 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001871 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
1872 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001873
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001874 var RefAnyNoArgs: func: any = RefAny
1875
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001876 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001877 RefNr = FuncNoArgRetNumber
1878 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001879 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
1880 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001881
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001882 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001883 RefStr = FuncNoArgRetString
1884 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001885 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
1886 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001887enddef
1888
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001889def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001890 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001891
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001892 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
1893 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
1894 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
1895 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
1896 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
1897 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 +02001898
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001899 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
1900 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
1901 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:')
1902 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001903enddef
1904
Bram Moolenaar89228602020-04-05 22:14:54 +02001905def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001906 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02001907 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001908 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02001909
1910 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001911 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02001912
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001913 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02001914 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001915 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02001916
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001917 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02001918enddef
1919
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001920def Test_func_common_type()
1921 def FuncOne(n: number): number
1922 return n
1923 enddef
1924 def FuncTwo(s: string): number
1925 return len(s)
1926 enddef
1927 def FuncThree(n: number, s: string): number
1928 return n + len(s)
1929 enddef
1930 var list = [FuncOne, FuncTwo, FuncThree]
1931 assert_equal(8, list[0](8))
1932 assert_equal(4, list[1]('word'))
1933 assert_equal(7, list[2](3, 'word'))
1934enddef
1935
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001936def MultiLine(
1937 arg1: string,
1938 arg2 = 1234,
1939 ...rest: list<string>
1940 ): string
1941 return arg1 .. arg2 .. join(rest, '-')
1942enddef
1943
Bram Moolenaar2c330432020-04-13 14:41:35 +02001944def MultiLineComment(
1945 arg1: string, # comment
1946 arg2 = 1234, # comment
1947 ...rest: list<string> # comment
1948 ): string # comment
1949 return arg1 .. arg2 .. join(rest, '-')
1950enddef
1951
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001952def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001953 MultiLine('text')->assert_equal('text1234')
1954 MultiLine('text', 777)->assert_equal('text777')
1955 MultiLine('text', 777, 'one')->assert_equal('text777one')
1956 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001957enddef
1958
Bram Moolenaar23e03252020-04-12 22:22:31 +02001959func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001960 call MultiLine('text')->assert_equal('text1234')
1961 call MultiLine('text', 777)->assert_equal('text777')
1962 call MultiLine('text', 777, 'one')->assert_equal('text777one')
1963 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02001964endfunc
1965
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001966
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001967" When using CheckScriptFailure() for the below test, E1010 is generated instead
1968" of E1056.
1969func Test_E1056_1059()
1970 let caught_1056 = 0
1971 try
1972 def F():
1973 return 1
1974 enddef
1975 catch /E1056:/
1976 let caught_1056 = 1
1977 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001978 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001979
1980 let caught_1059 = 0
1981 try
1982 def F5(items : list)
1983 echo 'a'
1984 enddef
1985 catch /E1059:/
1986 let caught_1059 = 1
1987 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001988 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001989endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001990
Bram Moolenaar015f4262020-05-05 21:25:22 +02001991func DelMe()
1992 echo 'DelMe'
1993endfunc
1994
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001995def Test_error_reporting()
1996 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001997 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001998 " comment
1999 def Func()
2000 # comment
2001 # comment
2002 invalid
2003 enddef
2004 defcompile
2005 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002006 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002007 try
2008 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002009 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002010 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002011 v:exception->assert_match('Invalid command: invalid')
2012 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002013 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002014 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002015
2016 # comment lines after the start of the function
2017 lines =<< trim END
2018 " comment
2019 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002020 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002021 # comment
2022 # comment
2023 invalid
2024 enddef
2025 defcompile
2026 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002027 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002028 try
2029 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002030 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002031 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002032 v:exception->assert_match('Invalid command: invalid')
2033 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002034 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002035 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002036
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002037 lines =<< trim END
2038 vim9script
2039 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002040 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002041 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002042 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002043 enddef
2044 defcompile
2045 Func()
2046 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002047 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002048 try
2049 source Xdef
2050 assert_report('should have failed')
2051 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002052 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002053 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002054 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002055
Bram Moolenaar08052222020-09-14 17:04:31 +02002056 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002057enddef
2058
Bram Moolenaar015f4262020-05-05 21:25:22 +02002059def Test_deleted_function()
2060 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002061 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002062 'delfunc g:DelMe',
2063 'echo RefMe()'], 'E117:')
2064enddef
2065
2066def Test_unknown_function()
2067 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002068 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002069 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002070enddef
2071
Bram Moolenaar328eac22021-01-07 19:23:08 +01002072def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002073 return Ref('more')
2074enddef
2075
2076def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002077 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002078 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002079enddef
2080
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002081def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002082 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002083 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002084enddef
2085
2086def Test_closure_ref_after_return()
2087 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002088 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002089 unlet g:Ref
2090enddef
2091
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002092def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002093 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002094 g:Extend = (s) => local->add(s)
2095 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002096enddef
2097
2098def Test_closure_two_refs()
2099 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002100 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002101 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002102 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002103 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002104 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002105
2106 unlet g:Extend
2107 unlet g:Read
2108enddef
2109
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002110def ReadRef(Ref: func(): list<string>): string
2111 return join(Ref(), ' ')
2112enddef
2113
Bram Moolenaar5e654232020-09-16 15:22:00 +02002114def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002115 Ref(add)
2116enddef
2117
2118def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002119 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002120 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002121 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002122 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002123 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002124 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002125
2126 unlet g:Extend
2127 unlet g:Read
2128enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002129
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002130def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002131 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002132 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002133enddef
2134
2135def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002136 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002137 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002138enddef
2139
2140def Test_closure_using_argument()
2141 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002142 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002143
2144 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002145 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002146
2147 unlet g:UseArg
2148 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002149
2150 var lines =<< trim END
2151 vim9script
2152 def Test(Fun: func(number): number): list<number>
2153 return map([1, 2, 3], (_, i) => Fun(i))
2154 enddef
2155 def Inc(nr: number): number
2156 return nr + 2
2157 enddef
2158 assert_equal([3, 4, 5], Test(Inc))
2159 END
2160 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002161enddef
2162
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002163def MakeGetAndAppendRefs()
2164 var local = 'a'
2165
2166 def Append(arg: string)
2167 local ..= arg
2168 enddef
2169 g:Append = Append
2170
2171 def Get(): string
2172 return local
2173 enddef
2174 g:Get = Get
2175enddef
2176
2177def Test_closure_append_get()
2178 MakeGetAndAppendRefs()
2179 g:Get()->assert_equal('a')
2180 g:Append('-b')
2181 g:Get()->assert_equal('a-b')
2182 g:Append('-c')
2183 g:Get()->assert_equal('a-b-c')
2184
2185 unlet g:Append
2186 unlet g:Get
2187enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002188
Bram Moolenaar04b12692020-05-04 23:24:44 +02002189def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002190 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002191 def Closure(arg: string): string
2192 return local .. arg
2193 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002194 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002195enddef
2196
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002197func GetResult(Ref)
2198 return a:Ref('some')
2199endfunc
2200
2201def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002202 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002203 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002204 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002205enddef
2206
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002207def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002208 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002209 vim9script
2210 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002211 var name = 0
2212 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002213 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002214 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002215 enddef
2216 Func()
2217 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002218 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002219enddef
2220
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002221def Test_nested_closure_used()
2222 var lines =<< trim END
2223 vim9script
2224 def Func()
2225 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002226 var Closure = () => x
2227 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002228 enddef
2229 Func()
2230 assert_equal('hello', g:Myclosure())
2231 END
2232 CheckScriptSuccess(lines)
2233enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002234
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002235def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002236 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002237 vim9script
2238 def FuncA()
2239 FuncB(0)
2240 enddef
2241 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002242 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002243 enddef
2244 FuncA()
2245 END
2246 CheckScriptFailure(lines, 'E1012:')
2247enddef
2248
Bram Moolenaarf112f302020-12-20 17:47:52 +01002249def Test_global_closure()
2250 var lines =<< trim END
2251 vim9script
2252 def ReverseEveryNLines(n: number, line1: number, line2: number)
2253 var mods = 'sil keepj keepp lockm '
2254 var range = ':' .. line1 .. ',' .. line2
2255 def g:Offset(): number
2256 var offset = (line('.') - line1 + 1) % n
2257 return offset != 0 ? offset : n
2258 enddef
2259 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2260 enddef
2261
2262 new
2263 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2264 ReverseEveryNLines(3, 1, 9)
2265 END
2266 CheckScriptSuccess(lines)
2267 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2268 assert_equal(expected, getline(1, 9))
2269 bwipe!
2270enddef
2271
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002272def Test_global_closure_called_directly()
2273 var lines =<< trim END
2274 vim9script
2275 def Outer()
2276 var x = 1
2277 def g:Inner()
2278 var y = x
2279 x += 1
2280 assert_equal(1, y)
2281 enddef
2282 g:Inner()
2283 assert_equal(2, x)
2284 enddef
2285 Outer()
2286 END
2287 CheckScriptSuccess(lines)
2288 delfunc g:Inner
2289enddef
2290
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002291def Test_failure_in_called_function()
2292 # this was using the frame index as the return value
2293 var lines =<< trim END
2294 vim9script
2295 au TerminalWinOpen * eval [][0]
2296 def PopupTerm(a: any)
2297 # make sure typvals on stack are string
2298 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2299 FireEvent()
2300 enddef
2301 def FireEvent()
2302 do TerminalWinOpen
2303 enddef
2304 # use try/catch to make eval fail
2305 try
2306 call PopupTerm(0)
2307 catch
2308 endtry
2309 au! TerminalWinOpen
2310 END
2311 CheckScriptSuccess(lines)
2312enddef
2313
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002314def Test_nested_lambda()
2315 var lines =<< trim END
2316 vim9script
2317 def Func()
2318 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002319 var Lambda1 = () => 7
2320 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002321 var res = Lambda2()
2322 assert_equal([7, 4], res)
2323 enddef
2324 Func()
2325 END
2326 CheckScriptSuccess(lines)
2327enddef
2328
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002329def Test_double_nested_lambda()
2330 var lines =<< trim END
2331 vim9script
2332 def F(head: string): func(string): func(string): string
2333 return (sep: string): func(string): string => ((tail: string): string => {
2334 return head .. sep .. tail
2335 })
2336 enddef
2337 assert_equal('hello-there', F('hello')('-')('there'))
2338 END
2339 CheckScriptSuccess(lines)
2340enddef
2341
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002342def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002343 var lines =<< trim END
2344 vim9script
2345 def F(text: string): func(string): func(string): string
2346 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002347 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002348 })
2349 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002350 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002351 END
2352 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002353
2354 lines =<< trim END
2355 vim9script
2356 echo range(4)->mapnew((_, v) => {
2357 return range(v) ->mapnew((_, s) => {
2358 return string(s)
2359 })
2360 })
2361 END
2362 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002363
2364 lines =<< trim END
2365 vim9script
2366
2367 def s:func()
2368 range(10)
2369 ->mapnew((_, _) => ({
2370 key: range(10)->mapnew((_, _) => {
2371 return ' '
2372 }),
2373 }))
2374 enddef
2375
2376 defcomp
2377 END
2378 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002379enddef
2380
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002381def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002382 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002383 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002384enddef
2385
2386def Test_lambda_arg_shadows_func()
2387 assert_equal([42], Shadowed())
2388enddef
2389
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002390def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002391 var path: string = empty(dir)
2392 \ ? 'empty'
2393 \ : 'full'
2394 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002395enddef
2396
2397def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002398 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002399enddef
2400
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002401def Test_script_var_in_lambda()
2402 var lines =<< trim END
2403 vim9script
2404 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002405 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002406 END
2407 CheckScriptSuccess(lines)
2408enddef
2409
Bram Moolenaar5e654232020-09-16 15:22:00 +02002410def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002411 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002412 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002413 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002414 ->reverse()
2415 return x
2416enddef
2417
2418def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002419 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002420
2421 var lines =<< trim END
2422 vim9script
2423 var res = [{n: 1, m: 2, s: 'xxx'}]
2424 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2425 v.n,
2426 v.m,
2427 substitute(v.s, '.*', 'yyy', '')
2428 ))
2429 assert_equal(['1:2:yyy'], res)
2430 END
2431 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002432enddef
2433
Bram Moolenaarb6571982021-01-08 22:24:19 +01002434def Test_list_lambda()
2435 timer_start(1000, (_) => 0)
2436 var body = execute(timer_info()[0].callback
2437 ->string()
2438 ->substitute("('", ' ', '')
2439 ->substitute("')", '', '')
2440 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002441 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002442enddef
2443
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002444def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002445 var lines =<< trim END
2446 vim9script
2447 var flist: list<func>
2448 for i in range(10)
2449 var inloop = i
2450 flist[i] = () => inloop
2451 endfor
2452 END
2453 CheckScriptSuccess(lines)
2454
2455 lines =<< trim END
2456 vim9script
2457 if true
2458 var outloop = 5
2459 var flist: list<func>
2460 for i in range(10)
2461 flist[i] = () => outloop
2462 endfor
2463 endif
2464 END
2465 CheckScriptSuccess(lines)
2466
2467 lines =<< trim END
2468 vim9script
2469 if true
2470 var outloop = 5
2471 endif
2472 var flist: list<func>
2473 for i in range(10)
2474 flist[i] = () => outloop
2475 endfor
2476 END
2477 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002478
2479 lines =<< trim END
2480 vim9script
2481 for i in range(10)
2482 var Ref = () => 0
2483 endfor
2484 assert_equal(0, ((i) => 0)(0))
2485 END
2486 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002487enddef
2488
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002489def Test_legacy_lambda()
2490 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002491
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002492 var lines =<< trim END
2493 echo {x -> 'hello ' .. x}('foo')
2494 END
2495 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002496
2497 lines =<< trim END
2498 vim9script
2499 def Func()
2500 echo (() => 'no error')()
2501 enddef
2502 legacy call s:Func()
2503 END
2504 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002505enddef
2506
Bram Moolenaarce024c32021-06-26 13:00:49 +02002507def Test_legacy()
2508 var lines =<< trim END
2509 vim9script
2510 func g:LegacyFunction()
2511 let g:legacyvar = 1
2512 endfunc
2513 def Testit()
2514 legacy call g:LegacyFunction()
2515 enddef
2516 Testit()
2517 assert_equal(1, g:legacyvar)
2518 unlet g:legacyvar
2519 delfunc g:LegacyFunction
2520 END
2521 CheckScriptSuccess(lines)
2522enddef
2523
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002524def Test_legacy_errors()
2525 for cmd in ['if', 'elseif', 'else', 'endif',
2526 'for', 'endfor', 'continue', 'break',
2527 'while', 'endwhile',
2528 'try', 'catch', 'finally', 'endtry']
2529 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2530 endfor
2531enddef
2532
Bram Moolenaarab360522021-01-10 14:02:28 +01002533def DoFilterThis(a: string): list<string>
2534 # closure nested inside another closure using argument
2535 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2536 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2537enddef
2538
2539def Test_nested_closure_using_argument()
2540 assert_equal(['x', 'x2'], DoFilterThis('x'))
2541enddef
2542
Bram Moolenaar0186e582021-01-10 18:33:11 +01002543def Test_triple_nested_closure()
2544 var what = 'x'
2545 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2546 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2547 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2548enddef
2549
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002550func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002551 CheckScreendump
2552
2553 let lines =<< trim END
2554 vim9script
2555 def EchoNothing()
2556 silent echo ''
2557 enddef
2558 defcompile
2559 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002560 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002561
2562 " Check that the balloon shows up after a mouse move
2563 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002564 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002565 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2566
2567 " clean up
2568 call StopVimInTerminal(buf)
2569 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002570endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002571
Bram Moolenaar171fb922020-10-28 16:54:47 +01002572def SilentlyError()
2573 execute('silent! invalid')
2574 g:did_it = 'yes'
2575enddef
2576
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002577func UserError()
2578 silent! invalid
2579endfunc
2580
2581def SilentlyUserError()
2582 UserError()
2583 g:did_it = 'yes'
2584enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002585
2586" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002587func Test_ignore_silent_error()
2588 let g:did_it = 'no'
2589 call SilentlyError()
2590 call assert_equal('yes', g:did_it)
2591
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002592 let g:did_it = 'no'
2593 call SilentlyUserError()
2594 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002595
2596 unlet g:did_it
2597endfunc
2598
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002599def Test_ignore_silent_error_in_filter()
2600 var lines =<< trim END
2601 vim9script
2602 def Filter(winid: number, key: string): bool
2603 if key == 'o'
2604 silent! eval [][0]
2605 return true
2606 endif
2607 return popup_filter_menu(winid, key)
2608 enddef
2609
Bram Moolenaare0de1712020-12-02 17:36:54 +01002610 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002611 feedkeys("o\r", 'xnt')
2612 END
2613 CheckScriptSuccess(lines)
2614enddef
2615
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002616def Fibonacci(n: number): number
2617 if n < 2
2618 return n
2619 else
2620 return Fibonacci(n - 1) + Fibonacci(n - 2)
2621 endif
2622enddef
2623
Bram Moolenaar985116a2020-07-12 17:31:09 +02002624def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002625 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002626enddef
2627
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002628def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002629 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002630 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002631 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002632 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002633 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002634enddef
2635
2636def Test_closure_in_map()
2637 mkdir('XclosureDir/tdir', 'p')
2638 writefile(['111'], 'XclosureDir/file1')
2639 writefile(['222'], 'XclosureDir/file2')
2640 writefile(['333'], 'XclosureDir/tdir/file3')
2641
Bram Moolenaare0de1712020-12-02 17:36:54 +01002642 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002643
2644 delete('XclosureDir', 'rf')
2645enddef
2646
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002647def Test_invalid_function_name()
2648 var lines =<< trim END
2649 vim9script
2650 def s: list<string>
2651 END
2652 CheckScriptFailure(lines, 'E129:')
2653
2654 lines =<< trim END
2655 vim9script
2656 def g: list<string>
2657 END
2658 CheckScriptFailure(lines, 'E129:')
2659
2660 lines =<< trim END
2661 vim9script
2662 def <SID>: list<string>
2663 END
2664 CheckScriptFailure(lines, 'E884:')
2665
2666 lines =<< trim END
2667 vim9script
2668 def F list<string>
2669 END
2670 CheckScriptFailure(lines, 'E488:')
2671enddef
2672
Bram Moolenaara90afb92020-07-15 22:38:56 +02002673def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002674 var lines =<< trim END
2675 var Xsetlist: func
2676 Xsetlist = function('setloclist', [0])
2677 Xsetlist([], ' ', {title: 'test'})
2678 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002679
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002680 Xsetlist = function('setloclist', [0, [], ' '])
2681 Xsetlist({title: 'test'})
2682 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002683
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002684 Xsetlist = function('setqflist')
2685 Xsetlist([], ' ', {title: 'test'})
2686 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002687
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002688 Xsetlist = function('setqflist', [[], ' '])
2689 Xsetlist({title: 'test'})
2690 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002691
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002692 var Len: func: number = function('len', ['word'])
2693 assert_equal(4, Len())
2694
2695 var RepeatFunc = function('repeat', ['o'])
2696 assert_equal('ooooo', RepeatFunc(5))
2697 END
2698 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002699
2700 lines =<< trim END
2701 vim9script
2702 def Foo(Parser: any)
2703 enddef
2704 var Expr: func(dict<any>): dict<any>
2705 const Call = Foo(Expr)
2706 END
2707 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002708enddef
2709
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002710def Test_cmd_modifier()
2711 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002712 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002713enddef
2714
2715def Test_restore_modifiers()
2716 # check that when compiling a :def function command modifiers are not messed
2717 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002718 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002719 vim9script
2720 set eventignore=
2721 autocmd QuickFixCmdPost * copen
2722 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002723 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002724 enddef
2725 func Func()
2726 noautocmd call s:AutocmdsDisabled()
2727 let g:ei_after = &eventignore
2728 endfunc
2729 Func()
2730 END
2731 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002732 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002733enddef
2734
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002735def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002736 eval 1 + 2
2737 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002738 # call not on fourth line
2739 StackBot()
2740enddef
2741
2742def StackBot()
2743 # throw an error
2744 eval [][0]
2745enddef
2746
2747def Test_callstack_def()
2748 try
2749 StackTop()
2750 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002751 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002752 endtry
2753enddef
2754
Bram Moolenaare8211a32020-10-09 22:04:29 +02002755" Re-using spot for variable used in block
2756def Test_block_scoped_var()
2757 var lines =<< trim END
2758 vim9script
2759 def Func()
2760 var x = ['a', 'b', 'c']
2761 if 1
2762 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002763 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002764 endif
2765 var z = x
2766 assert_equal(['x', 'x', 'x'], z)
2767 enddef
2768 Func()
2769 END
2770 CheckScriptSuccess(lines)
2771enddef
2772
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002773def Test_reset_did_emsg()
2774 var lines =<< trim END
2775 @s = 'blah'
2776 au BufWinLeave * #
2777 def Func()
2778 var winid = popup_create('popup', {})
2779 exe '*s'
2780 popup_close(winid)
2781 enddef
2782 Func()
2783 END
2784 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002785 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002786enddef
2787
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002788def Test_did_emsg_reset()
2789 # executing an autocommand resets did_emsg, this should not result in a
2790 # builtin function considered failing
2791 var lines =<< trim END
2792 vim9script
2793 au BufWinLeave * #
2794 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02002795 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002796 eval [][0]
2797 enddef
2798 nno <F3> <cmd>call <sid>Func()<cr>
2799 feedkeys("\<F3>\e", 'xt')
2800 END
2801 writefile(lines, 'XemsgReset')
2802 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
2803 delete('XemsgReset')
2804 nunmap <F3>
2805 au! BufWinLeave
2806enddef
2807
Bram Moolenaar56602ba2020-12-05 21:22:08 +01002808def Test_abort_with_silent_call()
2809 var lines =<< trim END
2810 vim9script
2811 g:result = 'none'
2812 def Func()
2813 g:result += 3
2814 g:result = 'yes'
2815 enddef
2816 # error is silenced, but function aborts on error
2817 silent! Func()
2818 assert_equal('none', g:result)
2819 unlet g:result
2820 END
2821 CheckScriptSuccess(lines)
2822enddef
2823
Bram Moolenaarf665e972020-12-05 19:17:16 +01002824def Test_continues_with_silent_error()
2825 var lines =<< trim END
2826 vim9script
2827 g:result = 'none'
2828 def Func()
2829 silent! g:result += 3
2830 g:result = 'yes'
2831 enddef
2832 # error is silenced, function does not abort
2833 Func()
2834 assert_equal('yes', g:result)
2835 unlet g:result
2836 END
2837 CheckScriptSuccess(lines)
2838enddef
2839
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002840def Test_abort_even_with_silent()
2841 var lines =<< trim END
2842 vim9script
2843 g:result = 'none'
2844 def Func()
2845 eval {-> ''}() .. '' .. {}['X']
2846 g:result = 'yes'
2847 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01002848 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002849 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002850 unlet g:result
2851 END
2852 CheckScriptSuccess(lines)
2853enddef
2854
Bram Moolenaarf665e972020-12-05 19:17:16 +01002855def Test_cmdmod_silent_restored()
2856 var lines =<< trim END
2857 vim9script
2858 def Func()
2859 g:result = 'none'
2860 silent! g:result += 3
2861 g:result = 'none'
2862 g:result += 3
2863 enddef
2864 Func()
2865 END
2866 # can't use CheckScriptFailure, it ignores the :silent!
2867 var fname = 'Xdefsilent'
2868 writefile(lines, fname)
2869 var caught = 'no'
2870 try
2871 exe 'source ' .. fname
2872 catch /E1030:/
2873 caught = 'yes'
2874 assert_match('Func, line 4', v:throwpoint)
2875 endtry
2876 assert_equal('yes', caught)
2877 delete(fname)
2878enddef
2879
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002880def Test_cmdmod_silent_nested()
2881 var lines =<< trim END
2882 vim9script
2883 var result = ''
2884
2885 def Error()
2886 result ..= 'Eb'
2887 eval [][0]
2888 result ..= 'Ea'
2889 enddef
2890
2891 def Crash()
2892 result ..= 'Cb'
2893 sil! Error()
2894 result ..= 'Ca'
2895 enddef
2896
2897 Crash()
2898 assert_equal('CbEbEaCa', result)
2899 END
2900 CheckScriptSuccess(lines)
2901enddef
2902
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002903def Test_dict_member_with_silent()
2904 var lines =<< trim END
2905 vim9script
2906 g:result = 'none'
2907 var d: dict<any>
2908 def Func()
2909 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002910 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002911 catch
2912 endtry
2913 enddef
2914 silent! Func()
2915 assert_equal('0', g:result)
2916 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002917 END
2918 CheckScriptSuccess(lines)
2919enddef
2920
Bram Moolenaarf9041332021-01-21 19:41:16 +01002921def Test_skip_cmds_with_silent()
2922 var lines =<< trim END
2923 vim9script
2924
2925 def Func(b: bool)
2926 Crash()
2927 enddef
2928
2929 def Crash()
2930 sil! :/not found/d _
2931 sil! :/not found/put _
2932 enddef
2933
2934 Func(true)
2935 END
2936 CheckScriptSuccess(lines)
2937enddef
2938
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01002939def Test_opfunc()
2940 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
2941 def g:Opfunc(_: any): string
2942 setline(1, 'ASDF')
2943 return ''
2944 enddef
2945 new
2946 setline(1, 'asdf')
2947 feedkeys("\<F3>$", 'x')
2948 assert_equal('ASDF', getline(1))
2949
2950 bwipe!
2951 nunmap <F3>
2952enddef
2953
Bram Moolenaar077a4232020-12-22 18:33:27 +01002954" this was crashing on exit
2955def Test_nested_lambda_in_closure()
2956 var lines =<< trim END
2957 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02002958 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01002959 def Outer()
2960 def g:Inner()
2961 echo map([1, 2, 3], {_, v -> v + 1})
2962 enddef
2963 g:Inner()
2964 enddef
2965 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02002966 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01002967 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02002968 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01002969 return
2970 endif
2971 assert_equal(['Done'], readfile('XnestedDone'))
2972 delete('XnestedDone')
2973enddef
2974
Bram Moolenaar04947cc2021-03-06 19:26:46 +01002975def Test_check_func_arg_types()
2976 var lines =<< trim END
2977 vim9script
2978 def F1(x: string): string
2979 return x
2980 enddef
2981
2982 def F2(x: number): number
2983 return x + 1
2984 enddef
2985
2986 def G(g: func): dict<func>
2987 return {f: g}
2988 enddef
2989
2990 def H(d: dict<func>): string
2991 return d.f('a')
2992 enddef
2993 END
2994
2995 CheckScriptSuccess(lines + ['echo H(G(F1))'])
2996 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
2997enddef
2998
Bram Moolenaar6e48b842021-08-10 22:52:02 +02002999def Test_list_any_type_checked()
3000 var lines =<< trim END
3001 vim9script
3002 def Foo()
3003 --decl--
3004 Bar(l)
3005 enddef
3006 def Bar(ll: list<dict<any>>)
3007 enddef
3008 Foo()
3009 END
3010 lines[2] = 'var l: list<any>'
3011 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3012
3013 lines[2] = 'var l: list<any> = []'
3014 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3015
3016 lines[2] = 'var l: list<any> = [11]'
3017 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3018enddef
3019
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003020def Test_compile_error()
3021 var lines =<< trim END
3022 def g:Broken()
3023 echo 'a' + {}
3024 enddef
3025 call g:Broken()
3026 END
3027 # First call: compilation error
3028 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3029
3030 # Second call won't try compiling again
3031 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003032 delfunc g:Broken
3033
3034 # No error when compiling with :silent!
3035 lines =<< trim END
3036 def g:Broken()
3037 echo 'a' + []
3038 enddef
3039 silent! defcompile
3040 END
3041 CheckScriptSuccess(lines)
3042
3043 # Calling the function won't try compiling again
3044 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3045 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003046enddef
3047
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003048def Test_ignored_argument()
3049 var lines =<< trim END
3050 vim9script
3051 def Ignore(_, _): string
3052 return 'yes'
3053 enddef
3054 assert_equal('yes', Ignore(1, 2))
3055
3056 func Ok(_)
3057 return a:_
3058 endfunc
3059 assert_equal('ok', Ok('ok'))
3060
3061 func Oktoo()
3062 let _ = 'too'
3063 return _
3064 endfunc
3065 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003066
3067 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003068 END
3069 CheckScriptSuccess(lines)
3070
3071 lines =<< trim END
3072 def Ignore(_: string): string
3073 return _
3074 enddef
3075 defcompile
3076 END
3077 CheckScriptFailure(lines, 'E1181:', 1)
3078
3079 lines =<< trim END
3080 var _ = 1
3081 END
3082 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003083
3084 lines =<< trim END
3085 var x = _
3086 END
3087 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003088enddef
3089
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003090def Test_too_many_arguments()
3091 var lines =<< trim END
3092 echo [0, 1, 2]->map(() => 123)
3093 END
3094 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3095
3096 lines =<< trim END
3097 echo [0, 1, 2]->map((_) => 123)
3098 END
3099 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3100enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003101
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003102def Test_closing_brace_at_start_of_line()
3103 var lines =<< trim END
3104 def Func()
3105 enddef
3106 Func(
3107 )
3108 END
3109 call CheckDefAndScriptSuccess(lines)
3110enddef
3111
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003112func CreateMydict()
3113 let g:mydict = {}
3114 func g:mydict.afunc()
3115 let g:result = self.key
3116 endfunc
3117endfunc
3118
3119def Test_numbered_function_reference()
3120 CreateMydict()
3121 var output = execute('legacy func g:mydict.afunc')
3122 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3123 execute 'function(' .. funcName .. ', [], {key: 42})()'
3124 # check that the function still exists
3125 assert_equal(output, execute('legacy func g:mydict.afunc'))
3126 unlet g:mydict
3127enddef
3128
Bram Moolenaar20677332021-06-06 17:02:53 +02003129if has('python3')
3130 def Test_python3_heredoc()
3131 py3 << trim EOF
3132 import vim
3133 vim.vars['didit'] = 'yes'
3134 EOF
3135 assert_equal('yes', g:didit)
3136
3137 python3 << trim EOF
3138 import vim
3139 vim.vars['didit'] = 'again'
3140 EOF
3141 assert_equal('again', g:didit)
3142 enddef
3143endif
3144
3145" This messes up syntax highlight, keep near the end.
3146if has('lua')
3147 def Test_lua_heredoc()
3148 g:d = {}
3149 lua << trim EOF
3150 x = vim.eval('g:d')
3151 x['key'] = 'val'
3152 EOF
3153 assert_equal('val', g:d.key)
3154 enddef
3155endif
3156
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003157
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003158" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker