blob: 51b95ab6cf769c63a7462b7824e4fef4f8781bf5 [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
494 CheckDefAndScriptFailure2(["call Test ('text')"], 'E476:', 'E1068:')
495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200550enddef
551
Bram Moolenaarcef12702021-01-04 14:09:43 +0100552def FuncWithComment( # comment
553 a: number, #comment
554 b: bool, # comment
555 c: string) #comment
556 assert_equal(4, a)
557 assert_equal(true, b)
558 assert_equal('yes', c)
559enddef
560
561def Test_func_with_comments()
562 FuncWithComment(4, true, 'yes')
563
564 var lines =<< trim END
565 def Func(# comment
566 arg: string)
567 enddef
568 END
569 CheckScriptFailure(lines, 'E125:', 1)
570
571 lines =<< trim END
572 def Func(
573 arg: string# comment
574 )
575 enddef
576 END
577 CheckScriptFailure(lines, 'E475:', 2)
578
579 lines =<< trim END
580 def Func(
581 arg: string
582 )# comment
583 enddef
584 END
585 CheckScriptFailure(lines, 'E488:', 3)
586enddef
587
Bram Moolenaar04b12692020-05-04 23:24:44 +0200588def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000589 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200590 return 'nested ' .. arg
591 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000592 NestedDef(':def')->assert_equal('nested :def')
593
594 func NestedFunc(arg)
595 return 'nested ' .. a:arg
596 endfunc
597 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200598
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200599 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
600 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
601
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200602 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
603 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200604
Bram Moolenaar54021752020-12-06 18:50:36 +0100605 var lines =<< trim END
606 def Outer()
607 def Inner()
608 # comment
609 enddef
610 def Inner()
611 enddef
612 enddef
613 END
614 CheckDefFailure(lines, 'E1073:')
615
616 lines =<< trim END
617 def Outer()
618 def Inner()
619 # comment
620 enddef
621 def! Inner()
622 enddef
623 enddef
624 END
625 CheckDefFailure(lines, 'E1117:')
626
627 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 lines =<< trim END
629 vim9script
630 var thecount = 0
631 if true
632 def Test(): number
633 def TheFunc(): number
634 thecount += 1
635 return thecount
636 enddef
637 return TheFunc()
638 enddef
639 endif
640 defcompile
641 assert_equal(1, Test())
642 assert_equal(2, Test())
643 END
644 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100645
646 # also works when "thecount" is inside the "if" block
647 lines =<< trim END
648 vim9script
649 if true
650 var thecount = 0
651 def Test(): number
652 def TheFunc(): number
653 thecount += 1
654 return thecount
655 enddef
656 return TheFunc()
657 enddef
658 endif
659 defcompile
660 assert_equal(1, Test())
661 assert_equal(2, Test())
662 END
663 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200664
665 lines =<< trim END
666 vim9script
667 def Outer()
668 def Inner()
669 echo 'hello'
670 enddef burp
671 enddef
672 defcompile
673 END
674 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200675enddef
676
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100677def Test_not_nested_function()
678 echo printf('%d',
679 function('len')('xxx'))
680enddef
681
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200682func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200683 call MyDefaultArgs()->assert_equal('string')
684 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200685 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200686endfunc
687
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200688def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200689 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200690 vim9script
691 def Outer()
692 def g:Inner(): string
693 return 'inner'
694 enddef
695 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200696 defcompile
697 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200698 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200699 delfunc g:Inner
700 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200701 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200702 delfunc g:Inner
703 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200704 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200705 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200706 END
707 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200708
709 lines =<< trim END
710 vim9script
711 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000712 func g:Inner()
713 return 'inner'
714 endfunc
715 enddef
716 defcompile
717 Outer()
718 g:Inner()->assert_equal('inner')
719 delfunc g:Inner
720 Outer()
721 g:Inner()->assert_equal('inner')
722 delfunc g:Inner
723 Outer()
724 g:Inner()->assert_equal('inner')
725 delfunc g:Inner
726 END
727 CheckScriptSuccess(lines)
728
729 lines =<< trim END
730 vim9script
731 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200732 def g:Inner(): string
733 return 'inner'
734 enddef
735 enddef
736 defcompile
737 Outer()
738 Outer()
739 END
740 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100741 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200742
743 lines =<< trim END
744 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100745 def Outer()
746 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100747 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100748 enddef
749 g:Inner()
750 enddef
751 Outer()
752 END
753 CheckScriptSuccess(lines)
754 delfunc g:Inner
755
756 lines =<< trim END
757 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200758 def Func()
759 echo 'script'
760 enddef
761 def Outer()
762 def Func()
763 echo 'inner'
764 enddef
765 enddef
766 defcompile
767 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000768 CheckScriptFailure(lines, "E1073:", 1)
769
770 lines =<< trim END
771 vim9script
772 def Func()
773 echo 'script'
774 enddef
775 def Func()
776 echo 'script'
777 enddef
778 END
779 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200780enddef
781
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100782def DefListAll()
783 def
784enddef
785
786def DefListOne()
787 def DefListOne
788enddef
789
790def DefListMatches()
791 def /DefList
792enddef
793
794def Test_nested_def_list()
795 var funcs = split(execute('call DefListAll()'), "\n")
796 assert_true(len(funcs) > 10)
797 assert_true(funcs->index('def DefListAll()') >= 0)
798
799 funcs = split(execute('call DefListOne()'), "\n")
800 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
801
802 funcs = split(execute('call DefListMatches()'), "\n")
803 assert_true(len(funcs) >= 3)
804 assert_true(funcs->index('def DefListAll()') >= 0)
805 assert_true(funcs->index('def DefListOne()') >= 0)
806 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100807
808 var lines =<< trim END
809 vim9script
810 def Func()
811 def +Func+
812 enddef
813 defcompile
814 END
815 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100816enddef
817
Bram Moolenaar333894b2020-08-01 18:53:07 +0200818def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200819 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200820 vim9script
821 def g:Func(): string
822 return 'global'
823 enddef
824 def Func(): string
825 return 'local'
826 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200827 g:Func()->assert_equal('global')
828 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100829 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200830 END
831 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200832
833 lines =<< trim END
834 vim9script
835 def g:Funcy()
836 echo 'funcy'
837 enddef
838 s:Funcy()
839 END
840 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200841enddef
842
Bram Moolenaar0f769812020-09-12 18:32:34 +0200843def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200844 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200845 vim9script
846 def g:Gfunc(): string
847 return 'global'
848 enddef
849 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200850 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200851 return Gfunc('testing')
852 enddef
853 g:Gfunc()->assert_equal('global')
854 AnotherFunc()->assert_equal(7)
855 delfunc g:Gfunc
856 END
857 CheckScriptSuccess(lines)
858
859 lines =<< trim END
860 vim9script
861 def g:Func(): string
862 return 'global'
863 enddef
864 def AnotherFunc()
865 g:Func = function('len')
866 enddef
867 AnotherFunc()
868 END
869 CheckScriptFailure(lines, 'E705:')
870 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200871
872 # global function is found without g: prefix
873 lines =<< trim END
874 vim9script
875 def g:Func(): string
876 return 'global'
877 enddef
878 def AnotherFunc(): string
879 return Func()
880 enddef
881 assert_equal('global', AnotherFunc())
882 delfunc g:Func
883 END
884 CheckScriptSuccess(lines)
885
886 lines =<< trim END
887 vim9script
888 def g:Func(): string
889 return 'global'
890 enddef
891 assert_equal('global', Func())
892 delfunc g:Func
893 END
894 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200895enddef
896
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200897func TakesOneArg(arg)
898 echo a:arg
899endfunc
900
901def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200902 CheckDefFailure(['TakesOneArg()'], 'E119:')
903 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
904 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
905 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200906
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200907 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200908 vim9script
909 def Func(s: string)
910 echo s
911 enddef
912 Func([])
913 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200914 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200915
916 lines =<< trim END
917 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100918 var name = 'piet'
919 def FuncOne(name: string)
920 echo nr
921 enddef
922 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100923 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100924
925 lines =<< trim END
926 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200927 def FuncOne(nr: number)
928 echo nr
929 enddef
930 def FuncTwo()
931 FuncOne()
932 enddef
933 defcompile
934 END
935 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200936 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200937 try
938 source Xscript
939 catch
940 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
941 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
942 didCatch = true
943 endtry
944 assert_true(didCatch)
945
946 lines =<< trim END
947 vim9script
948 def FuncOne(nr: number)
949 echo nr
950 enddef
951 def FuncTwo()
952 FuncOne(1, 2)
953 enddef
954 defcompile
955 END
956 writefile(lines, 'Xscript')
957 didCatch = false
958 try
959 source Xscript
960 catch
961 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
962 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
963 didCatch = true
964 endtry
965 assert_true(didCatch)
966
967 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200968enddef
969
Bram Moolenaar50824712020-12-20 21:10:17 +0100970def Test_call_funcref_wrong_args()
971 var head =<< trim END
972 vim9script
973 def Func3(a1: string, a2: number, a3: list<number>)
974 echo a1 .. a2 .. a3[0]
975 enddef
976 def Testme()
977 var funcMap: dict<func> = {func: Func3}
978 END
979 var tail =<< trim END
980 enddef
981 Testme()
982 END
983 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
984
985 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
986 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100987
988 var lines =<< trim END
989 vim9script
990 var Ref: func(number): any
991 Ref = (j) => !j
992 echo Ref(false)
993 END
994 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
995
996 lines =<< trim END
997 vim9script
998 var Ref: func(number): any
999 Ref = (j) => !j
1000 call Ref(false)
1001 END
1002 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001003enddef
1004
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001005def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001006 var lines =<< trim END
1007 var Callback = (..._) => 'anything'
1008 assert_equal('anything', Callback())
1009 assert_equal('anything', Callback(1))
1010 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001011
1012 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001013 END
1014 CheckDefAndScriptSuccess(lines)
1015
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001016 CheckDefFailure(['echo ((i) => 0)()'],
1017 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001018
Bram Moolenaar2a389082021-04-09 20:24:31 +02001019 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001020 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001021 echo Ref(1, 'x')
1022 END
1023 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001024
1025 lines =<< trim END
1026 var Ref: func(job, string, number)
1027 Ref = (x, y) => 0
1028 END
1029 CheckDefAndScriptFailure(lines, 'E1012:')
1030
1031 lines =<< trim END
1032 var Ref: func(job, string)
1033 Ref = (x, y, z) => 0
1034 END
1035 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001036
1037 lines =<< trim END
1038 var one = 1
1039 var l = [1, 2, 3]
1040 echo map(l, (one) => one)
1041 END
1042 CheckDefFailure(lines, 'E1167:')
1043 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1044
1045 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001046 var Ref: func(any, ?any): bool
1047 Ref = (_, y = 1) => false
1048 END
1049 CheckDefAndScriptFailure(lines, 'E1172:')
1050
1051 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001052 var a = 0
1053 var b = (a == 0 ? 1 : 2)
1054 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001055 var txt = 'a'
1056 b = (txt =~ 'x' ? 1 : 2)
1057 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001058 END
1059 CheckDefAndScriptSuccess(lines)
1060
1061 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001062 def ShadowLocal()
1063 var one = 1
1064 var l = [1, 2, 3]
1065 echo map(l, (one) => one)
1066 enddef
1067 END
1068 CheckDefFailure(lines, 'E1167:')
1069
1070 lines =<< trim END
1071 def Shadowarg(one: number)
1072 var l = [1, 2, 3]
1073 echo map(l, (one) => one)
1074 enddef
1075 END
1076 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001077
1078 lines =<< trim END
1079 echo ((a) => a)('aa', 'bb')
1080 END
1081 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001082
1083 lines =<< trim END
1084 echo 'aa'->((a) => a)('bb')
1085 END
1086 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1087 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001088enddef
1089
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001090def Test_lambda_line_nr()
1091 var lines =<< trim END
1092 vim9script
1093 # comment
1094 # comment
1095 var id = timer_start(1'000, (_) => 0)
1096 var out = execute('verbose ' .. timer_info(id)[0].callback
1097 ->string()
1098 ->substitute("('\\|')", ' ', 'g'))
1099 assert_match('Last set from .* line 4', out)
1100 END
1101 CheckScriptSuccess(lines)
1102enddef
1103
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001104def FilterWithCond(x: string, Cond: func(string): bool): bool
1105 return Cond(x)
1106enddef
1107
Bram Moolenaar0346b792021-01-31 22:18:29 +01001108def Test_lambda_return_type()
1109 var lines =<< trim END
1110 var Ref = (): => 123
1111 END
1112 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001113
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001114 # no space before the return type
1115 lines =<< trim END
1116 var Ref = (x):number => x + 1
1117 END
1118 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1119
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001120 # this works
1121 for x in ['foo', 'boo']
1122 echo FilterWithCond(x, (v) => v =~ '^b')
1123 endfor
1124
1125 # this fails
1126 lines =<< trim END
1127 echo FilterWithCond('foo', (v) => v .. '^b')
1128 END
1129 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001130
1131 lines =<< trim END
1132 var Lambda1 = (x) => {
1133 return x
1134 }
1135 assert_equal('asdf', Lambda1('asdf'))
1136 var Lambda2 = (x): string => {
1137 return x
1138 }
1139 assert_equal('foo', Lambda2('foo'))
1140 END
1141 CheckDefAndScriptSuccess(lines)
1142
1143 lines =<< trim END
1144 var Lambda = (x): string => {
1145 return x
1146 }
1147 echo Lambda(['foo'])
1148 END
1149 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001150enddef
1151
Bram Moolenaar709664c2020-12-12 14:33:41 +01001152def Test_lambda_uses_assigned_var()
1153 CheckDefSuccess([
1154 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001155 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001156enddef
1157
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001158def Test_pass_legacy_lambda_to_def_func()
1159 var lines =<< trim END
1160 vim9script
1161 func Foo()
1162 eval s:Bar({x -> 0})
1163 endfunc
1164 def Bar(y: any)
1165 enddef
1166 Foo()
1167 END
1168 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001169
1170 lines =<< trim END
1171 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001172 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001173 enddef
1174 legacy call g:TestFunc({-> 0})
1175 delfunc g:TestFunc
1176
1177 def g:TestFunc(f: func(number))
1178 enddef
1179 legacy call g:TestFunc({nr -> 0})
1180 delfunc g:TestFunc
1181 END
1182 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001183enddef
1184
Bram Moolenaar844fb642021-10-23 13:32:30 +01001185def Test_lambda_in_reduce_line_break()
1186 # this was using freed memory
1187 var lines =<< trim END
1188 vim9script
1189 const result: dict<number> =
1190 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1191 ->reduce((acc, val) => {
1192 if has_key(acc, val)
1193 acc[val] += 1
1194 return acc
1195 else
1196 acc[val] = 1
1197 return acc
1198 endif
1199 }, {})
1200 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1201 END
1202 CheckScriptSuccess(lines)
1203enddef
1204
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001205" Default arg and varargs
1206def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001207 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001208 for s in rest
1209 res ..= ',' .. s
1210 endfor
1211 return res
1212enddef
1213
1214def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001215 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001216 MyDefVarargs('one')->assert_equal('one,foo')
1217 MyDefVarargs('one', 'two')->assert_equal('one,two')
1218 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001219 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001220 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001221 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001222 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001223
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001224 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001225 vim9script
1226 def Func(...l: list<string>)
1227 echo l
1228 enddef
1229 Func('a', 'b', 'c')
1230 END
1231 CheckScriptSuccess(lines)
1232
1233 lines =<< trim END
1234 vim9script
1235 def Func(...l: list<string>)
1236 echo l
1237 enddef
1238 Func()
1239 END
1240 CheckScriptSuccess(lines)
1241
1242 lines =<< trim END
1243 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001244 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001245 echo l
1246 enddef
1247 Func(0)
1248 END
1249 CheckScriptSuccess(lines)
1250
1251 lines =<< trim END
1252 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001253 def Func(...l: any)
1254 echo l
1255 enddef
1256 Func(0)
1257 END
1258 CheckScriptFailure(lines, 'E1180:', 2)
1259
1260 lines =<< trim END
1261 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001262 def Func(..._l: list<string>)
1263 echo _l
1264 enddef
1265 Func('a', 'b', 'c')
1266 END
1267 CheckScriptSuccess(lines)
1268
1269 lines =<< trim END
1270 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001271 def Func(...l: list<string>)
1272 echo l
1273 enddef
1274 Func(1, 2, 3)
1275 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001276 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001277
1278 lines =<< trim END
1279 vim9script
1280 def Func(...l: list<string>)
1281 echo l
1282 enddef
1283 Func('a', 9)
1284 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001285 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001286
1287 lines =<< trim END
1288 vim9script
1289 def Func(...l: list<string>)
1290 echo l
1291 enddef
1292 Func(1, 'a')
1293 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001294 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001295
1296 lines =<< trim END
1297 vim9script
1298 def Func( # some comment
1299 ...l = []
1300 )
1301 echo l
1302 enddef
1303 END
1304 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001305
1306 lines =<< trim END
1307 vim9script
1308 def DoIt()
1309 g:Later('')
1310 enddef
1311 defcompile
1312 def g:Later(...l: list<number>)
1313 enddef
1314 DoIt()
1315 END
1316 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001317enddef
1318
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001319let s:value = ''
1320
1321def FuncOneDefArg(opt = 'text')
1322 s:value = opt
1323enddef
1324
1325def FuncTwoDefArg(nr = 123, opt = 'text'): string
1326 return nr .. opt
1327enddef
1328
1329def FuncVarargs(...arg: list<string>): string
1330 return join(arg, ',')
1331enddef
1332
1333def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001334 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001335 RefDefArg = FuncOneDefArg
1336 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001337 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001338 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001339 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001340
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001341 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001342 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001343 RefDef2Arg()->assert_equal('123text')
1344 RefDef2Arg(99)->assert_equal('99text')
1345 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001346
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001347 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1348 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001349
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001350 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001351 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001352 RefVarargs()->assert_equal('')
1353 RefVarargs('one')->assert_equal('one')
1354 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001355
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001356 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1357 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001358enddef
1359
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001360" Only varargs
1361def MyVarargsOnly(...args: list<string>): string
1362 return join(args, ',')
1363enddef
1364
1365def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001366 MyVarargsOnly()->assert_equal('')
1367 MyVarargsOnly('one')->assert_equal('one')
1368 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001369 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1370 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001371enddef
1372
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001373def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001374 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001375 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001376 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001377enddef
1378
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001379def DictArg(arg: dict<string>)
1380 arg['key'] = 'value'
1381enddef
1382
1383def ListArg(arg: list<string>)
1384 arg[0] = 'value'
1385enddef
1386
1387def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001388 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001389 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001390 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001391 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001392 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001393 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001394 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001395
Bram Moolenaard2c61702020-09-06 15:58:36 +02001396 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001397 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001398enddef
1399
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001400" These argument names are reserved in legacy functions.
1401def WithReservedNames(firstline: string, lastline: string): string
1402 return firstline .. lastline
1403enddef
1404
1405def Test_argument_names()
1406 assert_equal('OK', WithReservedNames('O', 'K'))
1407enddef
1408
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001409def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001410 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001411 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001412enddef
1413
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001414func DefinedLater(arg)
1415 return a:arg
1416endfunc
1417
1418def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001419 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001420 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
1421 assert_fails('g:NotAFunc()', 'E117:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001422
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001423 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001424 vim9script
1425 def RetNumber(): number
1426 return 123
1427 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001428 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001429 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001430 END
1431 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001432
1433 lines =<< trim END
1434 vim9script
1435 def RetNumber(): number
1436 return 123
1437 enddef
1438 def Bar(F: func: number): number
1439 return F()
1440 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001441 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001442 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001443 END
1444 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001445
1446 lines =<< trim END
1447 vim9script
1448 def UseNumber(nr: number)
1449 echo nr
1450 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001451 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001452 Funcref(123)
1453 END
1454 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001455
1456 lines =<< trim END
1457 vim9script
1458 def UseNumber(nr: number)
1459 echo nr
1460 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001461 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001462 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001463 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001464
1465 lines =<< trim END
1466 vim9script
1467 def EchoNr(nr = 34)
1468 g:echo = nr
1469 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001470 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001471 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001472 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001473 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001474 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001475 END
1476 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001477
1478 lines =<< trim END
1479 vim9script
1480 def EchoList(...l: list<number>)
1481 g:echo = l
1482 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001483 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001484 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001485 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001486 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001487 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001488 END
1489 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001490
1491 lines =<< trim END
1492 vim9script
1493 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1494 g:optarg = opt
1495 g:listarg = l
1496 return nr
1497 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001498 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001499 Funcref(10)->assert_equal(10)
1500 g:optarg->assert_equal(12)
1501 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001502
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001503 Funcref(11, 22)->assert_equal(11)
1504 g:optarg->assert_equal(22)
1505 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001506
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001507 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1508 g:optarg->assert_equal(18)
1509 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001510 END
1511 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001512enddef
1513
1514let SomeFunc = function('len')
1515let NotAFunc = 'text'
1516
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001517def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001518 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001519 var Ref1: func(bool): string
1520 var Ref2: func(bool): number
1521 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001522 Ref3 = g:cond ? Ref1 : Ref2
1523
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001524 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001525 var Refa1: func(bool): number
1526 var Refa2: func(bool, number): number
1527 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001528 Refa3 = g:cond ? Refa1 : Refa2
1529
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001530 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001531 var Refb1: func(bool, string): number
1532 var Refb2: func(string, number): number
1533 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001534 Refb3 = g:cond ? Refb1 : Refb2
1535enddef
1536
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001537def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001538 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001539enddef
1540
1541def DefinedEvenLater(arg: string): string
1542 return arg
1543enddef
1544
1545def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001546 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001547 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001548enddef
1549
1550def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001551 CheckScriptFailure([
1552 'def Func(): number',
1553 'return "a"',
1554 'enddef',
1555 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001556 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001557 CheckScriptFailure([
1558 'def Func(): string',
1559 'return 1',
1560 'enddef',
1561 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001562 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001563 CheckScriptFailure([
1564 'def Func(): void',
1565 'return "a"',
1566 'enddef',
1567 'defcompile'],
1568 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001569 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001570 CheckScriptFailure([
1571 'def Func()',
1572 'return "a"',
1573 'enddef',
1574 'defcompile'],
1575 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001576 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001577
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001578 CheckScriptFailure([
1579 'def Func(): number',
1580 'return',
1581 'enddef',
1582 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001583 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001584
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001585 CheckScriptFailure([
1586 'def Func():number',
1587 'return 123',
1588 'enddef',
1589 'defcompile'], 'E1069:')
1590 delfunc! g:Func
1591
1592 CheckScriptFailure([
1593 'def Func() :number',
1594 'return 123',
1595 'enddef',
1596 'defcompile'], 'E1059:')
1597 delfunc! g:Func
1598
1599 CheckScriptFailure([
1600 'def Func() : number',
1601 'return 123',
1602 'enddef',
1603 'defcompile'], 'E1059:')
1604 delfunc! g:Func
1605
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001606 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001607 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001608 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001609 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001610 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001611 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001612
1613 CheckScriptFailure([
1614 'vim9script',
1615 'def FuncB()',
1616 ' return 123',
1617 'enddef',
1618 'def FuncA()',
1619 ' FuncB()',
1620 'enddef',
1621 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001622enddef
1623
1624def Test_arg_type_wrong()
1625 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001626 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001627 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001628 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001629 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1630 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001631enddef
1632
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001633def Test_white_space_before_comma()
1634 var lines =<< trim END
1635 vim9script
1636 def Func(a: number , b: number)
1637 enddef
1638 END
1639 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001640 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001641enddef
1642
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001643def Test_white_space_after_comma()
1644 var lines =<< trim END
1645 vim9script
1646 def Func(a: number,b: number)
1647 enddef
1648 END
1649 CheckScriptFailure(lines, 'E1069:')
1650
1651 # OK in legacy function
1652 lines =<< trim END
1653 vim9script
1654 func Func(a,b)
1655 endfunc
1656 END
1657 CheckScriptSuccess(lines)
1658enddef
1659
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001660def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001661 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001662 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001663 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001664 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001665 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001666 enddef
1667 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001668 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001669
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001670 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001671 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001672 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001673
Bram Moolenaar67979662020-06-20 22:50:47 +02001674 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001675 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001676 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001677
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001678 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001679 def ListFunc(arg: list<number>)
1680 listvar = arg
1681 enddef
1682 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001683 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001684
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001685 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001686 def DictFunc(arg: dict<number>)
1687 dictvar = arg
1688 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001689 {a: 1, b: 2}->DictFunc()
1690 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001691 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001692 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001693 enddef
1694 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001695 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001696
Bram Moolenaare0de1712020-12-02 17:36:54 +01001697 {a: 3, b: 4}->DictFunc()
1698 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001699
1700 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001701 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001702 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001703 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001704
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001705 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001706 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001707 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001708 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001709 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001710 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001711
1712 def UseString()
1713 'xyork'->MyFunc()
1714 enddef
1715 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001716 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001717
Bram Moolenaar10409562020-07-29 20:00:38 +02001718 def UseString2()
1719 "knife"->MyFunc()
1720 enddef
1721 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001722 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001723
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001724 # prepending a colon makes it a mark
1725 new
1726 setline(1, ['aaa', 'bbb', 'ccc'])
1727 normal! 3Gmt1G
1728 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001729 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001730 bwipe!
1731
Bram Moolenaare6b53242020-07-01 17:28:33 +02001732 MyFunc(
1733 'continued'
1734 )
1735 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001736 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001737 )
1738
1739 call MyFunc(
1740 'more'
1741 ..
1742 'lines'
1743 )
1744 assert_equal(
1745 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001746 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001747 END
1748 writefile(lines, 'Xcall.vim')
1749 source Xcall.vim
1750 delete('Xcall.vim')
1751enddef
1752
1753def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001754 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001755 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001756 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001757 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001758 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001759 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001760 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001761 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001762 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001763enddef
1764
Bram Moolenaar65b95452020-07-19 14:03:09 +02001765def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001766 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001767 vim9script
1768 def MyFunc(arg: string)
1769 echo arg
1770 enddef
1771 MyFunc(1234)
1772 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001773 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001774enddef
1775
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001776def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001777 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001778 vim9script
1779 const var = ''
1780 def MyFunc(arg: string)
1781 var = 'asdf'
1782 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001783 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001784 END
1785 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001786 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001787 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001788
1789 lines =<< trim END
1790 const g:Aconst = 77
1791 def Change()
1792 # comment
1793 g:Aconst = 99
1794 enddef
1795 call Change()
1796 unlet g:Aconst
1797 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001798 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001799enddef
1800
1801" Test that inside :function a Python function can be defined, :def is not
1802" recognized.
1803func Test_function_python()
1804 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001805 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001806 execute py "<< EOF"
1807def do_something():
1808 return 1
1809EOF
1810endfunc
1811
1812def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001813 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001814 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001815 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001816 echo 'hello'
1817 enddef
1818
1819 def CallGoneSoon()
1820 GoneSoon()
1821 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001822 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001823
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001824 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001825 CallGoneSoon()
1826 END
1827 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001828 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1829 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001830
1831 delete('XToDelFunc')
1832enddef
1833
1834def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001835 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001836 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001837 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001838 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001839 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001840 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001841 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001842 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001843 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001844
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001845 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001846 g:Func1()->assert_equal('Func1')
1847 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001848
1849 delfunc! Func0
1850 delfunc! Func1
1851 delfunc! Func2
1852enddef
1853
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001854def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001855 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001856 vim9script
1857 func Func(arg)
1858 echo a:arg
1859 endfunc
1860 Func('text')
1861 END
1862 writefile(lines, 'XVim9Func')
1863 so XVim9Func
1864
1865 delete('XVim9Func')
1866enddef
1867
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001868let s:funcResult = 0
1869
1870def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001871 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001872enddef
1873
1874def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001875 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876 return 1234
1877enddef
1878
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001879def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001880 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001881 return 'text'
1882enddef
1883
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001884def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001885 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001886enddef
1887
1888def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001889 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001890 return arg
1891enddef
1892
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001893def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001894 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001895enddef
1896
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001897def FuncOneArgRetString(arg: string): string
1898 return arg
1899enddef
1900
Bram Moolenaar89228602020-04-05 22:14:54 +02001901def FuncOneArgRetAny(arg: any): any
1902 return arg
1903enddef
1904
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001905def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001906 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001907 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001908 Ref1 = FuncNoArgNoRet
1909 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001910 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001911
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001912 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001913 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001914 Ref2 = FuncNoArgNoRet
1915 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001916 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001917
Bram Moolenaar53900992020-08-22 19:02:02 +02001918 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001919 Ref2 = FuncOneArgNoRet
1920 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001921 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001922
Bram Moolenaar53900992020-08-22 19:02:02 +02001923 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001924 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001925 Ref2()->assert_equal(1234)
1926 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001927
Bram Moolenaar53900992020-08-22 19:02:02 +02001928 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001929 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001930 Ref2(13)->assert_equal(13)
1931 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001932enddef
1933
Bram Moolenaar9978d472020-07-05 16:01:56 +02001934def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001935 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02001936 for n in repeat([1], 3)
1937 res += n
1938 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001939 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02001940
1941 res = 0
1942 for n in add([1, 2], 3)
1943 res += n
1944 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001945 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02001946enddef
1947
Bram Moolenaar846178a2020-07-05 17:04:13 +02001948def Test_argv_return_type()
1949 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001950 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02001951 for name in argv()
1952 res ..= name
1953 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001954 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02001955enddef
1956
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001957def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001958 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001959 RefVoid = FuncNoArgNoRet
1960 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001961 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
1962 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001963
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001964 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001965 RefAny = FuncNoArgRetNumber
1966 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001967 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
1968 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001969
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001970 var RefAnyNoArgs: func: any = RefAny
1971
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001972 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001973 RefNr = FuncNoArgRetNumber
1974 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001975 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
1976 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001977
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001978 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001979 RefStr = FuncNoArgRetString
1980 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001981 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
1982 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001983enddef
1984
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001985def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001986 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001987
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001988 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
1989 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
1990 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
1991 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
1992 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
1993 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 +02001994
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001995 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
1996 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
1997 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:')
1998 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001999enddef
2000
Bram Moolenaar89228602020-04-05 22:14:54 +02002001def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002002 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002003 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002004 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002005
2006 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002007 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002008
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002009 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002010 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002011 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002012
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002013 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002014enddef
2015
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002016def Test_func_common_type()
2017 def FuncOne(n: number): number
2018 return n
2019 enddef
2020 def FuncTwo(s: string): number
2021 return len(s)
2022 enddef
2023 def FuncThree(n: number, s: string): number
2024 return n + len(s)
2025 enddef
2026 var list = [FuncOne, FuncTwo, FuncThree]
2027 assert_equal(8, list[0](8))
2028 assert_equal(4, list[1]('word'))
2029 assert_equal(7, list[2](3, 'word'))
2030enddef
2031
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002032def MultiLine(
2033 arg1: string,
2034 arg2 = 1234,
2035 ...rest: list<string>
2036 ): string
2037 return arg1 .. arg2 .. join(rest, '-')
2038enddef
2039
Bram Moolenaar2c330432020-04-13 14:41:35 +02002040def MultiLineComment(
2041 arg1: string, # comment
2042 arg2 = 1234, # comment
2043 ...rest: list<string> # comment
2044 ): string # comment
2045 return arg1 .. arg2 .. join(rest, '-')
2046enddef
2047
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002048def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002049 MultiLine('text')->assert_equal('text1234')
2050 MultiLine('text', 777)->assert_equal('text777')
2051 MultiLine('text', 777, 'one')->assert_equal('text777one')
2052 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002053enddef
2054
Bram Moolenaar23e03252020-04-12 22:22:31 +02002055func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002056 call MultiLine('text')->assert_equal('text1234')
2057 call MultiLine('text', 777)->assert_equal('text777')
2058 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2059 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002060endfunc
2061
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002062
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002063" When using CheckScriptFailure() for the below test, E1010 is generated instead
2064" of E1056.
2065func Test_E1056_1059()
2066 let caught_1056 = 0
2067 try
2068 def F():
2069 return 1
2070 enddef
2071 catch /E1056:/
2072 let caught_1056 = 1
2073 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002074 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002075
2076 let caught_1059 = 0
2077 try
2078 def F5(items : list)
2079 echo 'a'
2080 enddef
2081 catch /E1059:/
2082 let caught_1059 = 1
2083 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002084 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002085endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002086
Bram Moolenaar015f4262020-05-05 21:25:22 +02002087func DelMe()
2088 echo 'DelMe'
2089endfunc
2090
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002091def Test_error_reporting()
2092 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002093 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002094 " comment
2095 def Func()
2096 # comment
2097 # comment
2098 invalid
2099 enddef
2100 defcompile
2101 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002102 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002103 try
2104 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002105 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002106 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002107 v:exception->assert_match('Invalid command: invalid')
2108 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002109 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002110 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002111
2112 # comment lines after the start of the function
2113 lines =<< trim END
2114 " comment
2115 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002116 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002117 # comment
2118 # comment
2119 invalid
2120 enddef
2121 defcompile
2122 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002123 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002124 try
2125 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002126 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002127 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002128 v:exception->assert_match('Invalid command: invalid')
2129 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002130 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002131 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002132
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002133 lines =<< trim END
2134 vim9script
2135 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002136 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002137 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002138 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002139 enddef
2140 defcompile
2141 Func()
2142 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002143 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002144 try
2145 source Xdef
2146 assert_report('should have failed')
2147 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002148 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002149 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002150 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002151
Bram Moolenaar08052222020-09-14 17:04:31 +02002152 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002153enddef
2154
Bram Moolenaar015f4262020-05-05 21:25:22 +02002155def Test_deleted_function()
2156 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002157 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002158 'delfunc g:DelMe',
2159 'echo RefMe()'], 'E117:')
2160enddef
2161
2162def Test_unknown_function()
2163 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002164 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002165 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002166enddef
2167
Bram Moolenaar328eac22021-01-07 19:23:08 +01002168def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002169 return Ref('more')
2170enddef
2171
2172def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002173 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002174 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002175enddef
2176
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002177def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002178 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002179 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002180enddef
2181
2182def Test_closure_ref_after_return()
2183 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002184 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002185 unlet g:Ref
2186enddef
2187
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002188def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002189 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002190 g:Extend = (s) => local->add(s)
2191 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002192enddef
2193
2194def Test_closure_two_refs()
2195 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002196 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002197 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002198 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002199 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002200 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002201
2202 unlet g:Extend
2203 unlet g:Read
2204enddef
2205
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002206def ReadRef(Ref: func(): list<string>): string
2207 return join(Ref(), ' ')
2208enddef
2209
Bram Moolenaar5e654232020-09-16 15:22:00 +02002210def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002211 Ref(add)
2212enddef
2213
2214def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002215 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002216 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002217 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002218 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002219 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002220 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002221
2222 unlet g:Extend
2223 unlet g:Read
2224enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002226def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002227 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002228 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002229enddef
2230
2231def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002232 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002233 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002234enddef
2235
2236def Test_closure_using_argument()
2237 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002238 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002239
2240 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002241 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002242
2243 unlet g:UseArg
2244 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002245
2246 var lines =<< trim END
2247 vim9script
2248 def Test(Fun: func(number): number): list<number>
2249 return map([1, 2, 3], (_, i) => Fun(i))
2250 enddef
2251 def Inc(nr: number): number
2252 return nr + 2
2253 enddef
2254 assert_equal([3, 4, 5], Test(Inc))
2255 END
2256 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002257enddef
2258
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002259def MakeGetAndAppendRefs()
2260 var local = 'a'
2261
2262 def Append(arg: string)
2263 local ..= arg
2264 enddef
2265 g:Append = Append
2266
2267 def Get(): string
2268 return local
2269 enddef
2270 g:Get = Get
2271enddef
2272
2273def Test_closure_append_get()
2274 MakeGetAndAppendRefs()
2275 g:Get()->assert_equal('a')
2276 g:Append('-b')
2277 g:Get()->assert_equal('a-b')
2278 g:Append('-c')
2279 g:Get()->assert_equal('a-b-c')
2280
2281 unlet g:Append
2282 unlet g:Get
2283enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002284
Bram Moolenaar04b12692020-05-04 23:24:44 +02002285def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002286 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002287 def Closure(arg: string): string
2288 return local .. arg
2289 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002290 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002291enddef
2292
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002293func GetResult(Ref)
2294 return a:Ref('some')
2295endfunc
2296
2297def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002298 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002299 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002300 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002301enddef
2302
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002303def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002304 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002305 vim9script
2306 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002307 var name = 0
2308 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002309 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002310 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002311 enddef
2312 Func()
2313 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002314 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002315enddef
2316
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002317def Test_nested_closure_used()
2318 var lines =<< trim END
2319 vim9script
2320 def Func()
2321 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002322 var Closure = () => x
2323 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002324 enddef
2325 Func()
2326 assert_equal('hello', g:Myclosure())
2327 END
2328 CheckScriptSuccess(lines)
2329enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002330
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002331def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002332 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002333 vim9script
2334 def FuncA()
2335 FuncB(0)
2336 enddef
2337 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002338 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002339 enddef
2340 FuncA()
2341 END
2342 CheckScriptFailure(lines, 'E1012:')
2343enddef
2344
Bram Moolenaarf112f302020-12-20 17:47:52 +01002345def Test_global_closure()
2346 var lines =<< trim END
2347 vim9script
2348 def ReverseEveryNLines(n: number, line1: number, line2: number)
2349 var mods = 'sil keepj keepp lockm '
2350 var range = ':' .. line1 .. ',' .. line2
2351 def g:Offset(): number
2352 var offset = (line('.') - line1 + 1) % n
2353 return offset != 0 ? offset : n
2354 enddef
2355 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2356 enddef
2357
2358 new
2359 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2360 ReverseEveryNLines(3, 1, 9)
2361 END
2362 CheckScriptSuccess(lines)
2363 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2364 assert_equal(expected, getline(1, 9))
2365 bwipe!
2366enddef
2367
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002368def Test_global_closure_called_directly()
2369 var lines =<< trim END
2370 vim9script
2371 def Outer()
2372 var x = 1
2373 def g:Inner()
2374 var y = x
2375 x += 1
2376 assert_equal(1, y)
2377 enddef
2378 g:Inner()
2379 assert_equal(2, x)
2380 enddef
2381 Outer()
2382 END
2383 CheckScriptSuccess(lines)
2384 delfunc g:Inner
2385enddef
2386
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002387def Test_failure_in_called_function()
2388 # this was using the frame index as the return value
2389 var lines =<< trim END
2390 vim9script
2391 au TerminalWinOpen * eval [][0]
2392 def PopupTerm(a: any)
2393 # make sure typvals on stack are string
2394 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2395 FireEvent()
2396 enddef
2397 def FireEvent()
2398 do TerminalWinOpen
2399 enddef
2400 # use try/catch to make eval fail
2401 try
2402 call PopupTerm(0)
2403 catch
2404 endtry
2405 au! TerminalWinOpen
2406 END
2407 CheckScriptSuccess(lines)
2408enddef
2409
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002410def Test_nested_lambda()
2411 var lines =<< trim END
2412 vim9script
2413 def Func()
2414 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002415 var Lambda1 = () => 7
2416 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002417 var res = Lambda2()
2418 assert_equal([7, 4], res)
2419 enddef
2420 Func()
2421 END
2422 CheckScriptSuccess(lines)
2423enddef
2424
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002425def Test_double_nested_lambda()
2426 var lines =<< trim END
2427 vim9script
2428 def F(head: string): func(string): func(string): string
2429 return (sep: string): func(string): string => ((tail: string): string => {
2430 return head .. sep .. tail
2431 })
2432 enddef
2433 assert_equal('hello-there', F('hello')('-')('there'))
2434 END
2435 CheckScriptSuccess(lines)
2436enddef
2437
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002438def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002439 var lines =<< trim END
2440 vim9script
2441 def F(text: string): func(string): func(string): string
2442 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002443 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002444 })
2445 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002446 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002447 END
2448 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002449
2450 lines =<< trim END
2451 vim9script
2452 echo range(4)->mapnew((_, v) => {
2453 return range(v) ->mapnew((_, s) => {
2454 return string(s)
2455 })
2456 })
2457 END
2458 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002459
2460 lines =<< trim END
2461 vim9script
2462
2463 def s:func()
2464 range(10)
2465 ->mapnew((_, _) => ({
2466 key: range(10)->mapnew((_, _) => {
2467 return ' '
2468 }),
2469 }))
2470 enddef
2471
2472 defcomp
2473 END
2474 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002475enddef
2476
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002477def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002478 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002479 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002480enddef
2481
2482def Test_lambda_arg_shadows_func()
2483 assert_equal([42], Shadowed())
2484enddef
2485
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002486def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002487 var path: string = empty(dir)
2488 \ ? 'empty'
2489 \ : 'full'
2490 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002491enddef
2492
2493def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002494 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002495enddef
2496
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002497def Test_script_var_in_lambda()
2498 var lines =<< trim END
2499 vim9script
2500 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002501 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002502 END
2503 CheckScriptSuccess(lines)
2504enddef
2505
Bram Moolenaar5e654232020-09-16 15:22:00 +02002506def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002507 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002508 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002509 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002510 ->reverse()
2511 return x
2512enddef
2513
2514def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002515 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002516
2517 var lines =<< trim END
2518 vim9script
2519 var res = [{n: 1, m: 2, s: 'xxx'}]
2520 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2521 v.n,
2522 v.m,
2523 substitute(v.s, '.*', 'yyy', '')
2524 ))
2525 assert_equal(['1:2:yyy'], res)
2526 END
2527 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002528enddef
2529
Bram Moolenaarb6571982021-01-08 22:24:19 +01002530def Test_list_lambda()
2531 timer_start(1000, (_) => 0)
2532 var body = execute(timer_info()[0].callback
2533 ->string()
2534 ->substitute("('", ' ', '')
2535 ->substitute("')", '', '')
2536 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002537 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002538enddef
2539
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002540def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002541 var lines =<< trim END
2542 vim9script
2543 var flist: list<func>
2544 for i in range(10)
2545 var inloop = i
2546 flist[i] = () => inloop
2547 endfor
2548 END
2549 CheckScriptSuccess(lines)
2550
2551 lines =<< trim END
2552 vim9script
2553 if true
2554 var outloop = 5
2555 var flist: list<func>
2556 for i in range(10)
2557 flist[i] = () => outloop
2558 endfor
2559 endif
2560 END
2561 CheckScriptSuccess(lines)
2562
2563 lines =<< trim END
2564 vim9script
2565 if true
2566 var outloop = 5
2567 endif
2568 var flist: list<func>
2569 for i in range(10)
2570 flist[i] = () => outloop
2571 endfor
2572 END
2573 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002574
2575 lines =<< trim END
2576 vim9script
2577 for i in range(10)
2578 var Ref = () => 0
2579 endfor
2580 assert_equal(0, ((i) => 0)(0))
2581 END
2582 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002583enddef
2584
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002585def Test_legacy_lambda()
2586 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002587
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002588 var lines =<< trim END
2589 echo {x -> 'hello ' .. x}('foo')
2590 END
2591 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002592
2593 lines =<< trim END
2594 vim9script
2595 def Func()
2596 echo (() => 'no error')()
2597 enddef
2598 legacy call s:Func()
2599 END
2600 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002601enddef
2602
Bram Moolenaarce024c32021-06-26 13:00:49 +02002603def Test_legacy()
2604 var lines =<< trim END
2605 vim9script
2606 func g:LegacyFunction()
2607 let g:legacyvar = 1
2608 endfunc
2609 def Testit()
2610 legacy call g:LegacyFunction()
2611 enddef
2612 Testit()
2613 assert_equal(1, g:legacyvar)
2614 unlet g:legacyvar
2615 delfunc g:LegacyFunction
2616 END
2617 CheckScriptSuccess(lines)
2618enddef
2619
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002620def Test_legacy_errors()
2621 for cmd in ['if', 'elseif', 'else', 'endif',
2622 'for', 'endfor', 'continue', 'break',
2623 'while', 'endwhile',
2624 'try', 'catch', 'finally', 'endtry']
2625 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2626 endfor
2627enddef
2628
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002629def Test_call_legacy_with_dict()
2630 var lines =<< trim END
2631 vim9script
2632 func Legacy() dict
2633 let g:result = self.value
2634 endfunc
2635 def TestDirect()
2636 var d = {value: 'yes', func: Legacy}
2637 d.func()
2638 enddef
2639 TestDirect()
2640 assert_equal('yes', g:result)
2641 unlet g:result
2642
2643 def TestIndirect()
2644 var d = {value: 'foo', func: Legacy}
2645 var Fi = d.func
2646 Fi()
2647 enddef
2648 TestIndirect()
2649 assert_equal('foo', g:result)
2650 unlet g:result
2651
2652 var d = {value: 'bar', func: Legacy}
2653 d.func()
2654 assert_equal('bar', g:result)
2655 unlet g:result
2656 END
2657 CheckScriptSuccess(lines)
2658enddef
2659
Bram Moolenaarab360522021-01-10 14:02:28 +01002660def DoFilterThis(a: string): list<string>
2661 # closure nested inside another closure using argument
2662 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2663 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2664enddef
2665
2666def Test_nested_closure_using_argument()
2667 assert_equal(['x', 'x2'], DoFilterThis('x'))
2668enddef
2669
Bram Moolenaar0186e582021-01-10 18:33:11 +01002670def Test_triple_nested_closure()
2671 var what = 'x'
2672 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2673 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2674 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2675enddef
2676
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002677func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002678 CheckScreendump
2679
2680 let lines =<< trim END
2681 vim9script
2682 def EchoNothing()
2683 silent echo ''
2684 enddef
2685 defcompile
2686 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002687 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002688
2689 " Check that the balloon shows up after a mouse move
2690 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002691 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002692 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2693
2694 " clean up
2695 call StopVimInTerminal(buf)
2696 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002697endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002698
Bram Moolenaar171fb922020-10-28 16:54:47 +01002699def SilentlyError()
2700 execute('silent! invalid')
2701 g:did_it = 'yes'
2702enddef
2703
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002704func UserError()
2705 silent! invalid
2706endfunc
2707
2708def SilentlyUserError()
2709 UserError()
2710 g:did_it = 'yes'
2711enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002712
2713" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002714func Test_ignore_silent_error()
2715 let g:did_it = 'no'
2716 call SilentlyError()
2717 call assert_equal('yes', g:did_it)
2718
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002719 let g:did_it = 'no'
2720 call SilentlyUserError()
2721 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002722
2723 unlet g:did_it
2724endfunc
2725
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002726def Test_ignore_silent_error_in_filter()
2727 var lines =<< trim END
2728 vim9script
2729 def Filter(winid: number, key: string): bool
2730 if key == 'o'
2731 silent! eval [][0]
2732 return true
2733 endif
2734 return popup_filter_menu(winid, key)
2735 enddef
2736
Bram Moolenaare0de1712020-12-02 17:36:54 +01002737 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002738 feedkeys("o\r", 'xnt')
2739 END
2740 CheckScriptSuccess(lines)
2741enddef
2742
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002743def Fibonacci(n: number): number
2744 if n < 2
2745 return n
2746 else
2747 return Fibonacci(n - 1) + Fibonacci(n - 2)
2748 endif
2749enddef
2750
Bram Moolenaar985116a2020-07-12 17:31:09 +02002751def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002752 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002753enddef
2754
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002755def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002756 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002757 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002758 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002759 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002760 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002761enddef
2762
2763def Test_closure_in_map()
2764 mkdir('XclosureDir/tdir', 'p')
2765 writefile(['111'], 'XclosureDir/file1')
2766 writefile(['222'], 'XclosureDir/file2')
2767 writefile(['333'], 'XclosureDir/tdir/file3')
2768
Bram Moolenaare0de1712020-12-02 17:36:54 +01002769 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002770
2771 delete('XclosureDir', 'rf')
2772enddef
2773
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002774def Test_invalid_function_name()
2775 var lines =<< trim END
2776 vim9script
2777 def s: list<string>
2778 END
2779 CheckScriptFailure(lines, 'E129:')
2780
2781 lines =<< trim END
2782 vim9script
2783 def g: list<string>
2784 END
2785 CheckScriptFailure(lines, 'E129:')
2786
2787 lines =<< trim END
2788 vim9script
2789 def <SID>: list<string>
2790 END
2791 CheckScriptFailure(lines, 'E884:')
2792
2793 lines =<< trim END
2794 vim9script
2795 def F list<string>
2796 END
2797 CheckScriptFailure(lines, 'E488:')
2798enddef
2799
Bram Moolenaara90afb92020-07-15 22:38:56 +02002800def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002801 var lines =<< trim END
2802 var Xsetlist: func
2803 Xsetlist = function('setloclist', [0])
2804 Xsetlist([], ' ', {title: 'test'})
2805 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002806
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002807 Xsetlist = function('setloclist', [0, [], ' '])
2808 Xsetlist({title: 'test'})
2809 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002810
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002811 Xsetlist = function('setqflist')
2812 Xsetlist([], ' ', {title: 'test'})
2813 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002814
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002815 Xsetlist = function('setqflist', [[], ' '])
2816 Xsetlist({title: 'test'})
2817 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002818
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002819 var Len: func: number = function('len', ['word'])
2820 assert_equal(4, Len())
2821
2822 var RepeatFunc = function('repeat', ['o'])
2823 assert_equal('ooooo', RepeatFunc(5))
2824 END
2825 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002826
2827 lines =<< trim END
2828 vim9script
2829 def Foo(Parser: any)
2830 enddef
2831 var Expr: func(dict<any>): dict<any>
2832 const Call = Foo(Expr)
2833 END
2834 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002835enddef
2836
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002837def Test_cmd_modifier()
2838 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002839 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002840enddef
2841
2842def Test_restore_modifiers()
2843 # check that when compiling a :def function command modifiers are not messed
2844 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002845 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002846 vim9script
2847 set eventignore=
2848 autocmd QuickFixCmdPost * copen
2849 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002850 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002851 enddef
2852 func Func()
2853 noautocmd call s:AutocmdsDisabled()
2854 let g:ei_after = &eventignore
2855 endfunc
2856 Func()
2857 END
2858 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002859 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002860enddef
2861
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002862def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002863 eval 1 + 2
2864 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002865 # call not on fourth line
2866 StackBot()
2867enddef
2868
2869def StackBot()
2870 # throw an error
2871 eval [][0]
2872enddef
2873
2874def Test_callstack_def()
2875 try
2876 StackTop()
2877 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002878 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002879 endtry
2880enddef
2881
Bram Moolenaare8211a32020-10-09 22:04:29 +02002882" Re-using spot for variable used in block
2883def Test_block_scoped_var()
2884 var lines =<< trim END
2885 vim9script
2886 def Func()
2887 var x = ['a', 'b', 'c']
2888 if 1
2889 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002890 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002891 endif
2892 var z = x
2893 assert_equal(['x', 'x', 'x'], z)
2894 enddef
2895 Func()
2896 END
2897 CheckScriptSuccess(lines)
2898enddef
2899
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002900def Test_reset_did_emsg()
2901 var lines =<< trim END
2902 @s = 'blah'
2903 au BufWinLeave * #
2904 def Func()
2905 var winid = popup_create('popup', {})
2906 exe '*s'
2907 popup_close(winid)
2908 enddef
2909 Func()
2910 END
2911 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002912 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002913enddef
2914
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002915def Test_did_emsg_reset()
2916 # executing an autocommand resets did_emsg, this should not result in a
2917 # builtin function considered failing
2918 var lines =<< trim END
2919 vim9script
2920 au BufWinLeave * #
2921 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02002922 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002923 eval [][0]
2924 enddef
2925 nno <F3> <cmd>call <sid>Func()<cr>
2926 feedkeys("\<F3>\e", 'xt')
2927 END
2928 writefile(lines, 'XemsgReset')
2929 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
2930 delete('XemsgReset')
2931 nunmap <F3>
2932 au! BufWinLeave
2933enddef
2934
Bram Moolenaar56602ba2020-12-05 21:22:08 +01002935def Test_abort_with_silent_call()
2936 var lines =<< trim END
2937 vim9script
2938 g:result = 'none'
2939 def Func()
2940 g:result += 3
2941 g:result = 'yes'
2942 enddef
2943 # error is silenced, but function aborts on error
2944 silent! Func()
2945 assert_equal('none', g:result)
2946 unlet g:result
2947 END
2948 CheckScriptSuccess(lines)
2949enddef
2950
Bram Moolenaarf665e972020-12-05 19:17:16 +01002951def Test_continues_with_silent_error()
2952 var lines =<< trim END
2953 vim9script
2954 g:result = 'none'
2955 def Func()
2956 silent! g:result += 3
2957 g:result = 'yes'
2958 enddef
2959 # error is silenced, function does not abort
2960 Func()
2961 assert_equal('yes', g:result)
2962 unlet g:result
2963 END
2964 CheckScriptSuccess(lines)
2965enddef
2966
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002967def Test_abort_even_with_silent()
2968 var lines =<< trim END
2969 vim9script
2970 g:result = 'none'
2971 def Func()
2972 eval {-> ''}() .. '' .. {}['X']
2973 g:result = 'yes'
2974 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01002975 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002976 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002977 unlet g:result
2978 END
2979 CheckScriptSuccess(lines)
2980enddef
2981
Bram Moolenaarf665e972020-12-05 19:17:16 +01002982def Test_cmdmod_silent_restored()
2983 var lines =<< trim END
2984 vim9script
2985 def Func()
2986 g:result = 'none'
2987 silent! g:result += 3
2988 g:result = 'none'
2989 g:result += 3
2990 enddef
2991 Func()
2992 END
2993 # can't use CheckScriptFailure, it ignores the :silent!
2994 var fname = 'Xdefsilent'
2995 writefile(lines, fname)
2996 var caught = 'no'
2997 try
2998 exe 'source ' .. fname
2999 catch /E1030:/
3000 caught = 'yes'
3001 assert_match('Func, line 4', v:throwpoint)
3002 endtry
3003 assert_equal('yes', caught)
3004 delete(fname)
3005enddef
3006
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003007def Test_cmdmod_silent_nested()
3008 var lines =<< trim END
3009 vim9script
3010 var result = ''
3011
3012 def Error()
3013 result ..= 'Eb'
3014 eval [][0]
3015 result ..= 'Ea'
3016 enddef
3017
3018 def Crash()
3019 result ..= 'Cb'
3020 sil! Error()
3021 result ..= 'Ca'
3022 enddef
3023
3024 Crash()
3025 assert_equal('CbEbEaCa', result)
3026 END
3027 CheckScriptSuccess(lines)
3028enddef
3029
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003030def Test_dict_member_with_silent()
3031 var lines =<< trim END
3032 vim9script
3033 g:result = 'none'
3034 var d: dict<any>
3035 def Func()
3036 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003037 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003038 catch
3039 endtry
3040 enddef
3041 silent! Func()
3042 assert_equal('0', g:result)
3043 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003044 END
3045 CheckScriptSuccess(lines)
3046enddef
3047
Bram Moolenaarf9041332021-01-21 19:41:16 +01003048def Test_skip_cmds_with_silent()
3049 var lines =<< trim END
3050 vim9script
3051
3052 def Func(b: bool)
3053 Crash()
3054 enddef
3055
3056 def Crash()
3057 sil! :/not found/d _
3058 sil! :/not found/put _
3059 enddef
3060
3061 Func(true)
3062 END
3063 CheckScriptSuccess(lines)
3064enddef
3065
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003066def Test_opfunc()
3067 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3068 def g:Opfunc(_: any): string
3069 setline(1, 'ASDF')
3070 return ''
3071 enddef
3072 new
3073 setline(1, 'asdf')
3074 feedkeys("\<F3>$", 'x')
3075 assert_equal('ASDF', getline(1))
3076
3077 bwipe!
3078 nunmap <F3>
3079enddef
3080
Bram Moolenaar077a4232020-12-22 18:33:27 +01003081" this was crashing on exit
3082def Test_nested_lambda_in_closure()
3083 var lines =<< trim END
3084 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003085 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003086 def Outer()
3087 def g:Inner()
3088 echo map([1, 2, 3], {_, v -> v + 1})
3089 enddef
3090 g:Inner()
3091 enddef
3092 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003093 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003094 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003095 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003096 return
3097 endif
3098 assert_equal(['Done'], readfile('XnestedDone'))
3099 delete('XnestedDone')
3100enddef
3101
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003102def Test_check_func_arg_types()
3103 var lines =<< trim END
3104 vim9script
3105 def F1(x: string): string
3106 return x
3107 enddef
3108
3109 def F2(x: number): number
3110 return x + 1
3111 enddef
3112
3113 def G(g: func): dict<func>
3114 return {f: g}
3115 enddef
3116
3117 def H(d: dict<func>): string
3118 return d.f('a')
3119 enddef
3120 END
3121
3122 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3123 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3124enddef
3125
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003126def Test_list_any_type_checked()
3127 var lines =<< trim END
3128 vim9script
3129 def Foo()
3130 --decl--
3131 Bar(l)
3132 enddef
3133 def Bar(ll: list<dict<any>>)
3134 enddef
3135 Foo()
3136 END
3137 lines[2] = 'var l: list<any>'
3138 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3139
3140 lines[2] = 'var l: list<any> = []'
3141 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3142
3143 lines[2] = 'var l: list<any> = [11]'
3144 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3145enddef
3146
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003147def Test_compile_error()
3148 var lines =<< trim END
3149 def g:Broken()
3150 echo 'a' + {}
3151 enddef
3152 call g:Broken()
3153 END
3154 # First call: compilation error
3155 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3156
3157 # Second call won't try compiling again
3158 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003159 delfunc g:Broken
3160
3161 # No error when compiling with :silent!
3162 lines =<< trim END
3163 def g:Broken()
3164 echo 'a' + []
3165 enddef
3166 silent! defcompile
3167 END
3168 CheckScriptSuccess(lines)
3169
3170 # Calling the function won't try compiling again
3171 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3172 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003173enddef
3174
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003175def Test_ignored_argument()
3176 var lines =<< trim END
3177 vim9script
3178 def Ignore(_, _): string
3179 return 'yes'
3180 enddef
3181 assert_equal('yes', Ignore(1, 2))
3182
3183 func Ok(_)
3184 return a:_
3185 endfunc
3186 assert_equal('ok', Ok('ok'))
3187
3188 func Oktoo()
3189 let _ = 'too'
3190 return _
3191 endfunc
3192 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003193
3194 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003195 END
3196 CheckScriptSuccess(lines)
3197
3198 lines =<< trim END
3199 def Ignore(_: string): string
3200 return _
3201 enddef
3202 defcompile
3203 END
3204 CheckScriptFailure(lines, 'E1181:', 1)
3205
3206 lines =<< trim END
3207 var _ = 1
3208 END
3209 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003210
3211 lines =<< trim END
3212 var x = _
3213 END
3214 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003215enddef
3216
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003217def Test_too_many_arguments()
3218 var lines =<< trim END
3219 echo [0, 1, 2]->map(() => 123)
3220 END
3221 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3222
3223 lines =<< trim END
3224 echo [0, 1, 2]->map((_) => 123)
3225 END
3226 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3227enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003228
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003229def Test_closing_brace_at_start_of_line()
3230 var lines =<< trim END
3231 def Func()
3232 enddef
3233 Func(
3234 )
3235 END
3236 call CheckDefAndScriptSuccess(lines)
3237enddef
3238
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003239func CreateMydict()
3240 let g:mydict = {}
3241 func g:mydict.afunc()
3242 let g:result = self.key
3243 endfunc
3244endfunc
3245
3246def Test_numbered_function_reference()
3247 CreateMydict()
3248 var output = execute('legacy func g:mydict.afunc')
3249 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3250 execute 'function(' .. funcName .. ', [], {key: 42})()'
3251 # check that the function still exists
3252 assert_equal(output, execute('legacy func g:mydict.afunc'))
3253 unlet g:mydict
3254enddef
3255
Bram Moolenaar20677332021-06-06 17:02:53 +02003256if has('python3')
3257 def Test_python3_heredoc()
3258 py3 << trim EOF
3259 import vim
3260 vim.vars['didit'] = 'yes'
3261 EOF
3262 assert_equal('yes', g:didit)
3263
3264 python3 << trim EOF
3265 import vim
3266 vim.vars['didit'] = 'again'
3267 EOF
3268 assert_equal('again', g:didit)
3269 enddef
3270endif
3271
3272" This messes up syntax highlight, keep near the end.
3273if has('lua')
3274 def Test_lua_heredoc()
3275 g:d = {}
3276 lua << trim EOF
3277 x = vim.eval('g:d')
3278 x['key'] = 'val'
3279 EOF
3280 assert_equal('val', g:d.key)
3281 enddef
3282endif
3283
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003284
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003285" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker