blob: f97f902f75c60121e1ec80eba5f0175dab04a95c [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
494 CheckDefAndScriptFailure2(["call Test ('text')"], 'E476:', 'E1068:')
495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200550enddef
551
Bram Moolenaarcef12702021-01-04 14:09:43 +0100552def FuncWithComment( # comment
553 a: number, #comment
554 b: bool, # comment
555 c: string) #comment
556 assert_equal(4, a)
557 assert_equal(true, b)
558 assert_equal('yes', c)
559enddef
560
561def Test_func_with_comments()
562 FuncWithComment(4, true, 'yes')
563
564 var lines =<< trim END
565 def Func(# comment
566 arg: string)
567 enddef
568 END
569 CheckScriptFailure(lines, 'E125:', 1)
570
571 lines =<< trim END
572 def Func(
573 arg: string# comment
574 )
575 enddef
576 END
577 CheckScriptFailure(lines, 'E475:', 2)
578
579 lines =<< trim END
580 def Func(
581 arg: string
582 )# comment
583 enddef
584 END
585 CheckScriptFailure(lines, 'E488:', 3)
586enddef
587
Bram Moolenaar04b12692020-05-04 23:24:44 +0200588def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000589 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200590 return 'nested ' .. arg
591 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000592 NestedDef(':def')->assert_equal('nested :def')
593
594 func NestedFunc(arg)
595 return 'nested ' .. a:arg
596 endfunc
597 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200598
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200599 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
600 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
601
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200602 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
603 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200604
Bram Moolenaar54021752020-12-06 18:50:36 +0100605 var lines =<< trim END
606 def Outer()
607 def Inner()
608 # comment
609 enddef
610 def Inner()
611 enddef
612 enddef
613 END
614 CheckDefFailure(lines, 'E1073:')
615
616 lines =<< trim END
617 def Outer()
618 def Inner()
619 # comment
620 enddef
621 def! Inner()
622 enddef
623 enddef
624 END
625 CheckDefFailure(lines, 'E1117:')
626
627 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 lines =<< trim END
629 vim9script
630 var thecount = 0
631 if true
632 def Test(): number
633 def TheFunc(): number
634 thecount += 1
635 return thecount
636 enddef
637 return TheFunc()
638 enddef
639 endif
640 defcompile
641 assert_equal(1, Test())
642 assert_equal(2, Test())
643 END
644 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100645
646 # also works when "thecount" is inside the "if" block
647 lines =<< trim END
648 vim9script
649 if true
650 var thecount = 0
651 def Test(): number
652 def TheFunc(): number
653 thecount += 1
654 return thecount
655 enddef
656 return TheFunc()
657 enddef
658 endif
659 defcompile
660 assert_equal(1, Test())
661 assert_equal(2, Test())
662 END
663 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200664
665 lines =<< trim END
666 vim9script
667 def Outer()
668 def Inner()
669 echo 'hello'
670 enddef burp
671 enddef
672 defcompile
673 END
674 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200675enddef
676
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100677def Test_not_nested_function()
678 echo printf('%d',
679 function('len')('xxx'))
680enddef
681
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200682func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200683 call MyDefaultArgs()->assert_equal('string')
684 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200685 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200686endfunc
687
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200688def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200689 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200690 vim9script
691 def Outer()
692 def g:Inner(): string
693 return 'inner'
694 enddef
695 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200696 defcompile
697 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200698 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200699 delfunc g:Inner
700 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200701 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200702 delfunc g:Inner
703 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200704 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200705 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200706 END
707 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200708
709 lines =<< trim END
710 vim9script
711 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000712 func g:Inner()
713 return 'inner'
714 endfunc
715 enddef
716 defcompile
717 Outer()
718 g:Inner()->assert_equal('inner')
719 delfunc g:Inner
720 Outer()
721 g:Inner()->assert_equal('inner')
722 delfunc g:Inner
723 Outer()
724 g:Inner()->assert_equal('inner')
725 delfunc g:Inner
726 END
727 CheckScriptSuccess(lines)
728
729 lines =<< trim END
730 vim9script
731 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200732 def g:Inner(): string
733 return 'inner'
734 enddef
735 enddef
736 defcompile
737 Outer()
738 Outer()
739 END
740 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100741 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200742
743 lines =<< trim END
744 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100745 def Outer()
746 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100747 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100748 enddef
749 g:Inner()
750 enddef
751 Outer()
752 END
753 CheckScriptSuccess(lines)
754 delfunc g:Inner
755
756 lines =<< trim END
757 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200758 def Func()
759 echo 'script'
760 enddef
761 def Outer()
762 def Func()
763 echo 'inner'
764 enddef
765 enddef
766 defcompile
767 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000768 CheckScriptFailure(lines, "E1073:", 1)
769
770 lines =<< trim END
771 vim9script
772 def Func()
773 echo 'script'
774 enddef
775 def Func()
776 echo 'script'
777 enddef
778 END
779 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200780enddef
781
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100782def DefListAll()
783 def
784enddef
785
786def DefListOne()
787 def DefListOne
788enddef
789
790def DefListMatches()
791 def /DefList
792enddef
793
794def Test_nested_def_list()
795 var funcs = split(execute('call DefListAll()'), "\n")
796 assert_true(len(funcs) > 10)
797 assert_true(funcs->index('def DefListAll()') >= 0)
798
799 funcs = split(execute('call DefListOne()'), "\n")
800 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
801
802 funcs = split(execute('call DefListMatches()'), "\n")
803 assert_true(len(funcs) >= 3)
804 assert_true(funcs->index('def DefListAll()') >= 0)
805 assert_true(funcs->index('def DefListOne()') >= 0)
806 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100807
808 var lines =<< trim END
809 vim9script
810 def Func()
811 def +Func+
812 enddef
813 defcompile
814 END
815 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100816enddef
817
Bram Moolenaar333894b2020-08-01 18:53:07 +0200818def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200819 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200820 vim9script
821 def g:Func(): string
822 return 'global'
823 enddef
824 def Func(): string
825 return 'local'
826 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200827 g:Func()->assert_equal('global')
828 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100829 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200830 END
831 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200832
833 lines =<< trim END
834 vim9script
835 def g:Funcy()
836 echo 'funcy'
837 enddef
838 s:Funcy()
839 END
840 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200841enddef
842
Bram Moolenaar0f769812020-09-12 18:32:34 +0200843def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200844 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200845 vim9script
846 def g:Gfunc(): string
847 return 'global'
848 enddef
849 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200850 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200851 return Gfunc('testing')
852 enddef
853 g:Gfunc()->assert_equal('global')
854 AnotherFunc()->assert_equal(7)
855 delfunc g:Gfunc
856 END
857 CheckScriptSuccess(lines)
858
859 lines =<< trim END
860 vim9script
861 def g:Func(): string
862 return 'global'
863 enddef
864 def AnotherFunc()
865 g:Func = function('len')
866 enddef
867 AnotherFunc()
868 END
869 CheckScriptFailure(lines, 'E705:')
870 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200871
872 # global function is found without g: prefix
873 lines =<< trim END
874 vim9script
875 def g:Func(): string
876 return 'global'
877 enddef
878 def AnotherFunc(): string
879 return Func()
880 enddef
881 assert_equal('global', AnotherFunc())
882 delfunc g:Func
883 END
884 CheckScriptSuccess(lines)
885
886 lines =<< trim END
887 vim9script
888 def g:Func(): string
889 return 'global'
890 enddef
891 assert_equal('global', Func())
892 delfunc g:Func
893 END
894 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200895enddef
896
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200897func TakesOneArg(arg)
898 echo a:arg
899endfunc
900
901def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200902 CheckDefFailure(['TakesOneArg()'], 'E119:')
903 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
904 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
905 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200906
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200907 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200908 vim9script
909 def Func(s: string)
910 echo s
911 enddef
912 Func([])
913 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200914 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200915
916 lines =<< trim END
917 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100918 var name = 'piet'
919 def FuncOne(name: string)
920 echo nr
921 enddef
922 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100923 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100924
925 lines =<< trim END
926 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200927 def FuncOne(nr: number)
928 echo nr
929 enddef
930 def FuncTwo()
931 FuncOne()
932 enddef
933 defcompile
934 END
935 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200936 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200937 try
938 source Xscript
939 catch
940 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
941 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
942 didCatch = true
943 endtry
944 assert_true(didCatch)
945
946 lines =<< trim END
947 vim9script
948 def FuncOne(nr: number)
949 echo nr
950 enddef
951 def FuncTwo()
952 FuncOne(1, 2)
953 enddef
954 defcompile
955 END
956 writefile(lines, 'Xscript')
957 didCatch = false
958 try
959 source Xscript
960 catch
961 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
962 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
963 didCatch = true
964 endtry
965 assert_true(didCatch)
966
967 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200968enddef
969
Bram Moolenaar50824712020-12-20 21:10:17 +0100970def Test_call_funcref_wrong_args()
971 var head =<< trim END
972 vim9script
973 def Func3(a1: string, a2: number, a3: list<number>)
974 echo a1 .. a2 .. a3[0]
975 enddef
976 def Testme()
977 var funcMap: dict<func> = {func: Func3}
978 END
979 var tail =<< trim END
980 enddef
981 Testme()
982 END
983 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
984
985 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
986 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100987
988 var lines =<< trim END
989 vim9script
990 var Ref: func(number): any
991 Ref = (j) => !j
992 echo Ref(false)
993 END
994 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
995
996 lines =<< trim END
997 vim9script
998 var Ref: func(number): any
999 Ref = (j) => !j
1000 call Ref(false)
1001 END
1002 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001003enddef
1004
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001005def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001006 var lines =<< trim END
1007 var Callback = (..._) => 'anything'
1008 assert_equal('anything', Callback())
1009 assert_equal('anything', Callback(1))
1010 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001011
1012 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001013 END
1014 CheckDefAndScriptSuccess(lines)
1015
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001016 CheckDefFailure(['echo ((i) => 0)()'],
1017 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001018
Bram Moolenaar2a389082021-04-09 20:24:31 +02001019 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001020 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001021 echo Ref(1, 'x')
1022 END
1023 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001024
1025 lines =<< trim END
1026 var Ref: func(job, string, number)
1027 Ref = (x, y) => 0
1028 END
1029 CheckDefAndScriptFailure(lines, 'E1012:')
1030
1031 lines =<< trim END
1032 var Ref: func(job, string)
1033 Ref = (x, y, z) => 0
1034 END
1035 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001036
1037 lines =<< trim END
1038 var one = 1
1039 var l = [1, 2, 3]
1040 echo map(l, (one) => one)
1041 END
1042 CheckDefFailure(lines, 'E1167:')
1043 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1044
1045 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001046 var Ref: func(any, ?any): bool
1047 Ref = (_, y = 1) => false
1048 END
1049 CheckDefAndScriptFailure(lines, 'E1172:')
1050
1051 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001052 var a = 0
1053 var b = (a == 0 ? 1 : 2)
1054 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001055 var txt = 'a'
1056 b = (txt =~ 'x' ? 1 : 2)
1057 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001058 END
1059 CheckDefAndScriptSuccess(lines)
1060
1061 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001062 def ShadowLocal()
1063 var one = 1
1064 var l = [1, 2, 3]
1065 echo map(l, (one) => one)
1066 enddef
1067 END
1068 CheckDefFailure(lines, 'E1167:')
1069
1070 lines =<< trim END
1071 def Shadowarg(one: number)
1072 var l = [1, 2, 3]
1073 echo map(l, (one) => one)
1074 enddef
1075 END
1076 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001077
1078 lines =<< trim END
1079 echo ((a) => a)('aa', 'bb')
1080 END
1081 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001082
1083 lines =<< trim END
1084 echo 'aa'->((a) => a)('bb')
1085 END
1086 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1087 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001088enddef
1089
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001090def Test_lambda_line_nr()
1091 var lines =<< trim END
1092 vim9script
1093 # comment
1094 # comment
1095 var id = timer_start(1'000, (_) => 0)
1096 var out = execute('verbose ' .. timer_info(id)[0].callback
1097 ->string()
1098 ->substitute("('\\|')", ' ', 'g'))
1099 assert_match('Last set from .* line 4', out)
1100 END
1101 CheckScriptSuccess(lines)
1102enddef
1103
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001104def FilterWithCond(x: string, Cond: func(string): bool): bool
1105 return Cond(x)
1106enddef
1107
Bram Moolenaar0346b792021-01-31 22:18:29 +01001108def Test_lambda_return_type()
1109 var lines =<< trim END
1110 var Ref = (): => 123
1111 END
1112 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001113
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001114 # no space before the return type
1115 lines =<< trim END
1116 var Ref = (x):number => x + 1
1117 END
1118 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1119
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001120 # this works
1121 for x in ['foo', 'boo']
1122 echo FilterWithCond(x, (v) => v =~ '^b')
1123 endfor
1124
1125 # this fails
1126 lines =<< trim END
1127 echo FilterWithCond('foo', (v) => v .. '^b')
1128 END
1129 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001130
1131 lines =<< trim END
1132 var Lambda1 = (x) => {
1133 return x
1134 }
1135 assert_equal('asdf', Lambda1('asdf'))
1136 var Lambda2 = (x): string => {
1137 return x
1138 }
1139 assert_equal('foo', Lambda2('foo'))
1140 END
1141 CheckDefAndScriptSuccess(lines)
1142
1143 lines =<< trim END
1144 var Lambda = (x): string => {
1145 return x
1146 }
1147 echo Lambda(['foo'])
1148 END
1149 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001150enddef
1151
Bram Moolenaar709664c2020-12-12 14:33:41 +01001152def Test_lambda_uses_assigned_var()
1153 CheckDefSuccess([
1154 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001155 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001156enddef
1157
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001158def Test_pass_legacy_lambda_to_def_func()
1159 var lines =<< trim END
1160 vim9script
1161 func Foo()
1162 eval s:Bar({x -> 0})
1163 endfunc
1164 def Bar(y: any)
1165 enddef
1166 Foo()
1167 END
1168 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001169
1170 lines =<< trim END
1171 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001172 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001173 enddef
1174 legacy call g:TestFunc({-> 0})
1175 delfunc g:TestFunc
1176
1177 def g:TestFunc(f: func(number))
1178 enddef
1179 legacy call g:TestFunc({nr -> 0})
1180 delfunc g:TestFunc
1181 END
1182 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001183enddef
1184
Bram Moolenaar844fb642021-10-23 13:32:30 +01001185def Test_lambda_in_reduce_line_break()
1186 # this was using freed memory
1187 var lines =<< trim END
1188 vim9script
1189 const result: dict<number> =
1190 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1191 ->reduce((acc, val) => {
1192 if has_key(acc, val)
1193 acc[val] += 1
1194 return acc
1195 else
1196 acc[val] = 1
1197 return acc
1198 endif
1199 }, {})
1200 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1201 END
1202 CheckScriptSuccess(lines)
1203enddef
1204
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001205def Test_set_opfunc_to_lambda()
1206 var lines =<< trim END
1207 vim9script
1208 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1209 def CountSpaces(type = ''): string
1210 if type == ''
1211 &operatorfunc = (t) => CountSpaces(t)
1212 return 'g@'
1213 endif
1214 normal! '[V']y
1215 g:result = getreg('"')->count(' ')
1216 return ''
1217 enddef
1218 new
1219 'a b c d e'->setline(1)
1220 feedkeys("\<F4>", 'x')
1221 assert_equal(4, g:result)
1222 bwipe!
1223 END
1224 CheckScriptSuccess(lines)
1225enddef
1226
Bram Moolenaaref082e12021-12-12 21:02:03 +00001227def Test_set_opfunc_to_global_function()
1228 var lines =<< trim END
1229 vim9script
1230 def g:CountSpaces(type = ''): string
1231 normal! '[V']y
1232 g:result = getreg('"')->count(' ')
1233 return ''
1234 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001235 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001236 &operatorfunc = g:CountSpaces
1237 new
1238 'a b c d e'->setline(1)
1239 feedkeys("g@_", 'x')
1240 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001241
1242 &operatorfunc = ''
1243 g:result = 0
1244 # global function works in :def function
1245 def Func()
1246 &operatorfunc = g:CountSpaces
1247 enddef
1248 Func()
1249 feedkeys("g@_", 'x')
1250 assert_equal(4, g:result)
1251
Bram Moolenaaref082e12021-12-12 21:02:03 +00001252 bwipe!
1253 END
1254 CheckScriptSuccess(lines)
1255 &operatorfunc = ''
1256enddef
1257
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001258def Test_use_script_func_name_with_prefix()
1259 var lines =<< trim END
1260 vim9script
1261 func s:Getit()
1262 return 'it'
1263 endfunc
1264 var Fn = s:Getit
1265 assert_equal('it', Fn())
1266 END
1267 CheckScriptSuccess(lines)
1268enddef
1269
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001270def Test_lambda_type_allocated()
1271 # Check that unreferencing a partial using a lambda can use the variable type
1272 # after the lambda has been freed and does not leak memory.
1273 var lines =<< trim END
1274 vim9script
1275
1276 func MyomniFunc1(val, findstart, base)
1277 return a:findstart ? 0 : []
1278 endfunc
1279
1280 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1281 &omnifunc = Lambda
1282 Lambda = (a, b) => MyomniFunc1(20, a, b)
1283 &omnifunc = string(Lambda)
1284 Lambda = (a, b) => strlen(a)
1285 END
1286 CheckScriptSuccess(lines)
1287enddef
1288
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001289" Default arg and varargs
1290def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001291 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001292 for s in rest
1293 res ..= ',' .. s
1294 endfor
1295 return res
1296enddef
1297
1298def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001299 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001300 MyDefVarargs('one')->assert_equal('one,foo')
1301 MyDefVarargs('one', 'two')->assert_equal('one,two')
1302 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001303 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001304 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001305 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001306 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001307
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001308 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001309 vim9script
1310 def Func(...l: list<string>)
1311 echo l
1312 enddef
1313 Func('a', 'b', 'c')
1314 END
1315 CheckScriptSuccess(lines)
1316
1317 lines =<< trim END
1318 vim9script
1319 def Func(...l: list<string>)
1320 echo l
1321 enddef
1322 Func()
1323 END
1324 CheckScriptSuccess(lines)
1325
1326 lines =<< trim END
1327 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001328 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001329 echo l
1330 enddef
1331 Func(0)
1332 END
1333 CheckScriptSuccess(lines)
1334
1335 lines =<< trim END
1336 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001337 def Func(...l: any)
1338 echo l
1339 enddef
1340 Func(0)
1341 END
1342 CheckScriptFailure(lines, 'E1180:', 2)
1343
1344 lines =<< trim END
1345 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001346 def Func(..._l: list<string>)
1347 echo _l
1348 enddef
1349 Func('a', 'b', 'c')
1350 END
1351 CheckScriptSuccess(lines)
1352
1353 lines =<< trim END
1354 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001355 def Func(...l: list<string>)
1356 echo l
1357 enddef
1358 Func(1, 2, 3)
1359 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001360 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001361
1362 lines =<< trim END
1363 vim9script
1364 def Func(...l: list<string>)
1365 echo l
1366 enddef
1367 Func('a', 9)
1368 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001369 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001370
1371 lines =<< trim END
1372 vim9script
1373 def Func(...l: list<string>)
1374 echo l
1375 enddef
1376 Func(1, 'a')
1377 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001378 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001379
1380 lines =<< trim END
1381 vim9script
1382 def Func( # some comment
1383 ...l = []
1384 )
1385 echo l
1386 enddef
1387 END
1388 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001389
1390 lines =<< trim END
1391 vim9script
1392 def DoIt()
1393 g:Later('')
1394 enddef
1395 defcompile
1396 def g:Later(...l: list<number>)
1397 enddef
1398 DoIt()
1399 END
1400 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001401enddef
1402
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001403let s:value = ''
1404
1405def FuncOneDefArg(opt = 'text')
1406 s:value = opt
1407enddef
1408
1409def FuncTwoDefArg(nr = 123, opt = 'text'): string
1410 return nr .. opt
1411enddef
1412
1413def FuncVarargs(...arg: list<string>): string
1414 return join(arg, ',')
1415enddef
1416
1417def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001418 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001419 RefDefArg = FuncOneDefArg
1420 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001421 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001422 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001423 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001424
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001425 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001426 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001427 RefDef2Arg()->assert_equal('123text')
1428 RefDef2Arg(99)->assert_equal('99text')
1429 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001430
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001431 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1432 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001433
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001434 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001435 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001436 RefVarargs()->assert_equal('')
1437 RefVarargs('one')->assert_equal('one')
1438 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001439
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001440 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1441 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001442enddef
1443
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001444" Only varargs
1445def MyVarargsOnly(...args: list<string>): string
1446 return join(args, ',')
1447enddef
1448
1449def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001450 MyVarargsOnly()->assert_equal('')
1451 MyVarargsOnly('one')->assert_equal('one')
1452 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001453 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1454 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001455enddef
1456
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001457def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001458 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001459 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001460 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001461enddef
1462
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001463def DictArg(arg: dict<string>)
1464 arg['key'] = 'value'
1465enddef
1466
1467def ListArg(arg: list<string>)
1468 arg[0] = 'value'
1469enddef
1470
1471def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001472 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001473 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001474 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001475 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001476 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001477 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001478 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001479
Bram Moolenaard2c61702020-09-06 15:58:36 +02001480 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001481 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001482enddef
1483
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001484" These argument names are reserved in legacy functions.
1485def WithReservedNames(firstline: string, lastline: string): string
1486 return firstline .. lastline
1487enddef
1488
1489def Test_argument_names()
1490 assert_equal('OK', WithReservedNames('O', 'K'))
1491enddef
1492
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001493def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001494 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001495 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001496enddef
1497
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001498func DefinedLater(arg)
1499 return a:arg
1500endfunc
1501
1502def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001503 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001504 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001505 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001506
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001507 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001508 vim9script
1509 def RetNumber(): number
1510 return 123
1511 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001512 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001513 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001514 END
1515 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001516
1517 lines =<< trim END
1518 vim9script
1519 def RetNumber(): number
1520 return 123
1521 enddef
1522 def Bar(F: func: number): number
1523 return F()
1524 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001525 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001526 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001527 END
1528 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001529
1530 lines =<< trim END
1531 vim9script
1532 def UseNumber(nr: number)
1533 echo nr
1534 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001535 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001536 Funcref(123)
1537 END
1538 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001539
1540 lines =<< trim END
1541 vim9script
1542 def UseNumber(nr: number)
1543 echo nr
1544 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001545 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001546 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001547 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001548
1549 lines =<< trim END
1550 vim9script
1551 def EchoNr(nr = 34)
1552 g:echo = nr
1553 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001554 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001555 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001556 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001557 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001558 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001559 END
1560 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001561
1562 lines =<< trim END
1563 vim9script
1564 def EchoList(...l: list<number>)
1565 g:echo = l
1566 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001567 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001568 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001569 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001570 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001571 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001572 END
1573 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001574
1575 lines =<< trim END
1576 vim9script
1577 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1578 g:optarg = opt
1579 g:listarg = l
1580 return nr
1581 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001582 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001583 Funcref(10)->assert_equal(10)
1584 g:optarg->assert_equal(12)
1585 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001586
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001587 Funcref(11, 22)->assert_equal(11)
1588 g:optarg->assert_equal(22)
1589 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001590
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001591 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1592 g:optarg->assert_equal(18)
1593 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001594 END
1595 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001596enddef
1597
1598let SomeFunc = function('len')
1599let NotAFunc = 'text'
1600
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001601def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001602 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001603 var Ref1: func(bool): string
1604 var Ref2: func(bool): number
1605 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001606 Ref3 = g:cond ? Ref1 : Ref2
1607
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001608 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001609 var Refa1: func(bool): number
1610 var Refa2: func(bool, number): number
1611 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001612 Refa3 = g:cond ? Refa1 : Refa2
1613
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001614 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001615 var Refb1: func(bool, string): number
1616 var Refb2: func(string, number): number
1617 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001618 Refb3 = g:cond ? Refb1 : Refb2
1619enddef
1620
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001621def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001622 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001623enddef
1624
1625def DefinedEvenLater(arg: string): string
1626 return arg
1627enddef
1628
1629def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001630 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001631 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001632enddef
1633
1634def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001635 CheckScriptFailure([
1636 'def Func(): number',
1637 'return "a"',
1638 'enddef',
1639 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001640 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001641 CheckScriptFailure([
1642 'def Func(): string',
1643 'return 1',
1644 'enddef',
1645 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001646 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001647 CheckScriptFailure([
1648 'def Func(): void',
1649 'return "a"',
1650 'enddef',
1651 'defcompile'],
1652 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001653 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001654 CheckScriptFailure([
1655 'def Func()',
1656 'return "a"',
1657 'enddef',
1658 'defcompile'],
1659 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001660 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001661
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001662 CheckScriptFailure([
1663 'def Func(): number',
1664 'return',
1665 'enddef',
1666 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001667 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001668
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001669 CheckScriptFailure([
1670 'def Func():number',
1671 'return 123',
1672 'enddef',
1673 'defcompile'], 'E1069:')
1674 delfunc! g:Func
1675
1676 CheckScriptFailure([
1677 'def Func() :number',
1678 'return 123',
1679 'enddef',
1680 'defcompile'], 'E1059:')
1681 delfunc! g:Func
1682
1683 CheckScriptFailure([
1684 'def Func() : number',
1685 'return 123',
1686 'enddef',
1687 'defcompile'], 'E1059:')
1688 delfunc! g:Func
1689
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001690 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001691 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001692 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001693 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001694 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001695 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001696
1697 CheckScriptFailure([
1698 'vim9script',
1699 'def FuncB()',
1700 ' return 123',
1701 'enddef',
1702 'def FuncA()',
1703 ' FuncB()',
1704 'enddef',
1705 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001706enddef
1707
1708def Test_arg_type_wrong()
1709 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001710 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001711 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001712 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001713 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1714 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001715enddef
1716
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001717def Test_white_space_before_comma()
1718 var lines =<< trim END
1719 vim9script
1720 def Func(a: number , b: number)
1721 enddef
1722 END
1723 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001724 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001725enddef
1726
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001727def Test_white_space_after_comma()
1728 var lines =<< trim END
1729 vim9script
1730 def Func(a: number,b: number)
1731 enddef
1732 END
1733 CheckScriptFailure(lines, 'E1069:')
1734
1735 # OK in legacy function
1736 lines =<< trim END
1737 vim9script
1738 func Func(a,b)
1739 endfunc
1740 END
1741 CheckScriptSuccess(lines)
1742enddef
1743
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001744def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001745 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001746 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001747 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001748 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001749 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001750 enddef
1751 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001752 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001753
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001754 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001755 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001756 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001757
Bram Moolenaar67979662020-06-20 22:50:47 +02001758 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001759 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001760 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001761
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001762 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001763 def ListFunc(arg: list<number>)
1764 listvar = arg
1765 enddef
1766 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001767 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001768
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001769 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001770 def DictFunc(arg: dict<number>)
1771 dictvar = arg
1772 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001773 {a: 1, b: 2}->DictFunc()
1774 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001775 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001776 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001777 enddef
1778 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001779 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001780
Bram Moolenaare0de1712020-12-02 17:36:54 +01001781 {a: 3, b: 4}->DictFunc()
1782 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001783
1784 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001785 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001786 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001787 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001788
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001789 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001790 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001791 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001792 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001793 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001794 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001795
1796 def UseString()
1797 'xyork'->MyFunc()
1798 enddef
1799 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001800 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001801
Bram Moolenaar10409562020-07-29 20:00:38 +02001802 def UseString2()
1803 "knife"->MyFunc()
1804 enddef
1805 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001806 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001807
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001808 # prepending a colon makes it a mark
1809 new
1810 setline(1, ['aaa', 'bbb', 'ccc'])
1811 normal! 3Gmt1G
1812 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001813 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001814 bwipe!
1815
Bram Moolenaare6b53242020-07-01 17:28:33 +02001816 MyFunc(
1817 'continued'
1818 )
1819 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001820 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001821 )
1822
1823 call MyFunc(
1824 'more'
1825 ..
1826 'lines'
1827 )
1828 assert_equal(
1829 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001830 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001831 END
1832 writefile(lines, 'Xcall.vim')
1833 source Xcall.vim
1834 delete('Xcall.vim')
1835enddef
1836
1837def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001838 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001839 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001840 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001841 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001842 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001843 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001844 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001845 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001846 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001847enddef
1848
Bram Moolenaar65b95452020-07-19 14:03:09 +02001849def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001850 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001851 vim9script
1852 def MyFunc(arg: string)
1853 echo arg
1854 enddef
1855 MyFunc(1234)
1856 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001857 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001858enddef
1859
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001860def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001861 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001862 vim9script
1863 const var = ''
1864 def MyFunc(arg: string)
1865 var = 'asdf'
1866 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001867 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001868 END
1869 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001870 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001871 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001872
1873 lines =<< trim END
1874 const g:Aconst = 77
1875 def Change()
1876 # comment
1877 g:Aconst = 99
1878 enddef
1879 call Change()
1880 unlet g:Aconst
1881 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001882 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001883enddef
1884
1885" Test that inside :function a Python function can be defined, :def is not
1886" recognized.
1887func Test_function_python()
1888 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001889 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001890 execute py "<< EOF"
1891def do_something():
1892 return 1
1893EOF
1894endfunc
1895
1896def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001897 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001898 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001899 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001900 echo 'hello'
1901 enddef
1902
1903 def CallGoneSoon()
1904 GoneSoon()
1905 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001906 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001907
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001908 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001909 CallGoneSoon()
1910 END
1911 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001912 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1913 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001914
1915 delete('XToDelFunc')
1916enddef
1917
Bram Moolenaar7509ad82021-12-14 18:14:37 +00001918func Test_free_dict_while_in_funcstack()
1919 " relies on the sleep command
1920 CheckUnix
1921 call Run_Test_free_dict_while_in_funcstack()
1922endfunc
1923
1924def Run_Test_free_dict_while_in_funcstack()
1925
1926 # this was freeing the TermRun() default argument dictionary while it was
1927 # still referenced in a funcstack_T
1928 var lines =<< trim END
1929 vim9script
1930
1931 &updatetime = 400
1932 def TermRun(_ = {})
1933 def Post()
1934 enddef
1935 def Exec()
1936 term_start('sleep 1', {
1937 term_finish: 'close',
1938 exit_cb: (_, _) => Post(),
1939 })
1940 enddef
1941 Exec()
1942 enddef
1943 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
1944 timer_start(100, (_) => feedkeys("\<F4>"))
1945 timer_start(1000, (_) => feedkeys("\<F4>"))
1946 sleep 1500m
1947 END
1948 CheckScriptSuccess(lines)
1949 nunmap <F4>
1950 set updatetime&
1951enddef
1952
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001953def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001954 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001955 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001956 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001957 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001958 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001959 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001960 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001961 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001962 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001963
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001964 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001965 g:Func1()->assert_equal('Func1')
1966 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001967
1968 delfunc! Func0
1969 delfunc! Func1
1970 delfunc! Func2
1971enddef
1972
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001973def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001974 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001975 vim9script
1976 func Func(arg)
1977 echo a:arg
1978 endfunc
1979 Func('text')
1980 END
1981 writefile(lines, 'XVim9Func')
1982 so XVim9Func
1983
1984 delete('XVim9Func')
1985enddef
1986
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001987let s:funcResult = 0
1988
1989def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001990 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001991enddef
1992
1993def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001994 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001995 return 1234
1996enddef
1997
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001998def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001999 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002000 return 'text'
2001enddef
2002
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002003def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002004 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002005enddef
2006
2007def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002008 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002009 return arg
2010enddef
2011
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002012def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002013 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002014enddef
2015
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002016def FuncOneArgRetString(arg: string): string
2017 return arg
2018enddef
2019
Bram Moolenaar89228602020-04-05 22:14:54 +02002020def FuncOneArgRetAny(arg: any): any
2021 return arg
2022enddef
2023
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002024def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002025 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002026 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002027 Ref1 = FuncNoArgNoRet
2028 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002029 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002030
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002031 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002032 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002033 Ref2 = FuncNoArgNoRet
2034 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002035 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002036
Bram Moolenaar53900992020-08-22 19:02:02 +02002037 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002038 Ref2 = FuncOneArgNoRet
2039 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002040 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002041
Bram Moolenaar53900992020-08-22 19:02:02 +02002042 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002043 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002044 Ref2()->assert_equal(1234)
2045 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002046
Bram Moolenaar53900992020-08-22 19:02:02 +02002047 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002048 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002049 Ref2(13)->assert_equal(13)
2050 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002051enddef
2052
Bram Moolenaar9978d472020-07-05 16:01:56 +02002053def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002054 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002055 for n in repeat([1], 3)
2056 res += n
2057 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002058 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002059
2060 res = 0
2061 for n in add([1, 2], 3)
2062 res += n
2063 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002064 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002065enddef
2066
Bram Moolenaar846178a2020-07-05 17:04:13 +02002067def Test_argv_return_type()
2068 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002069 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002070 for name in argv()
2071 res ..= name
2072 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002073 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002074enddef
2075
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002076def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002077 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002078 RefVoid = FuncNoArgNoRet
2079 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002080 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2081 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002082
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002083 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002084 RefAny = FuncNoArgRetNumber
2085 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002086 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2087 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002088
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002089 var RefAnyNoArgs: func: any = RefAny
2090
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002091 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002092 RefNr = FuncNoArgRetNumber
2093 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002094 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2095 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002096
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002097 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002098 RefStr = FuncNoArgRetString
2099 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002100 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2101 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002102enddef
2103
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002104def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002105 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002106
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002107 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2108 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2109 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2110 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2111 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2112 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 +02002113
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002114 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2115 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2116 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:')
2117 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002118enddef
2119
Bram Moolenaar89228602020-04-05 22:14:54 +02002120def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002121 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002122 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002123 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002124
2125 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002126 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002127
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002128 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002129 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002130 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002131
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002132 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002133enddef
2134
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002135def Test_func_common_type()
2136 def FuncOne(n: number): number
2137 return n
2138 enddef
2139 def FuncTwo(s: string): number
2140 return len(s)
2141 enddef
2142 def FuncThree(n: number, s: string): number
2143 return n + len(s)
2144 enddef
2145 var list = [FuncOne, FuncTwo, FuncThree]
2146 assert_equal(8, list[0](8))
2147 assert_equal(4, list[1]('word'))
2148 assert_equal(7, list[2](3, 'word'))
2149enddef
2150
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002151def MultiLine(
2152 arg1: string,
2153 arg2 = 1234,
2154 ...rest: list<string>
2155 ): string
2156 return arg1 .. arg2 .. join(rest, '-')
2157enddef
2158
Bram Moolenaar2c330432020-04-13 14:41:35 +02002159def MultiLineComment(
2160 arg1: string, # comment
2161 arg2 = 1234, # comment
2162 ...rest: list<string> # comment
2163 ): string # comment
2164 return arg1 .. arg2 .. join(rest, '-')
2165enddef
2166
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002167def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002168 MultiLine('text')->assert_equal('text1234')
2169 MultiLine('text', 777)->assert_equal('text777')
2170 MultiLine('text', 777, 'one')->assert_equal('text777one')
2171 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002172enddef
2173
Bram Moolenaar23e03252020-04-12 22:22:31 +02002174func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002175 call MultiLine('text')->assert_equal('text1234')
2176 call MultiLine('text', 777)->assert_equal('text777')
2177 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2178 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002179endfunc
2180
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002181
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002182" When using CheckScriptFailure() for the below test, E1010 is generated instead
2183" of E1056.
2184func Test_E1056_1059()
2185 let caught_1056 = 0
2186 try
2187 def F():
2188 return 1
2189 enddef
2190 catch /E1056:/
2191 let caught_1056 = 1
2192 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002193 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002194
2195 let caught_1059 = 0
2196 try
2197 def F5(items : list)
2198 echo 'a'
2199 enddef
2200 catch /E1059:/
2201 let caught_1059 = 1
2202 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002203 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002204endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002205
Bram Moolenaar015f4262020-05-05 21:25:22 +02002206func DelMe()
2207 echo 'DelMe'
2208endfunc
2209
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002210def Test_error_reporting()
2211 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002212 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002213 " comment
2214 def Func()
2215 # comment
2216 # comment
2217 invalid
2218 enddef
2219 defcompile
2220 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002221 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002222 try
2223 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002224 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002225 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002226 v:exception->assert_match('Invalid command: invalid')
2227 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002228 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002229 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002230
2231 # comment lines after the start of the function
2232 lines =<< trim END
2233 " comment
2234 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002235 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002236 # comment
2237 # comment
2238 invalid
2239 enddef
2240 defcompile
2241 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002242 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002243 try
2244 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002245 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002246 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002247 v:exception->assert_match('Invalid command: invalid')
2248 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002249 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002250 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002251
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002252 lines =<< trim END
2253 vim9script
2254 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002255 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002256 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002257 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002258 enddef
2259 defcompile
2260 Func()
2261 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002262 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002263 try
2264 source Xdef
2265 assert_report('should have failed')
2266 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002267 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002268 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002269 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002270
Bram Moolenaar08052222020-09-14 17:04:31 +02002271 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002272enddef
2273
Bram Moolenaar015f4262020-05-05 21:25:22 +02002274def Test_deleted_function()
2275 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002276 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002277 'delfunc g:DelMe',
2278 'echo RefMe()'], 'E117:')
2279enddef
2280
2281def Test_unknown_function()
2282 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002283 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002284 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002285enddef
2286
Bram Moolenaar328eac22021-01-07 19:23:08 +01002287def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002288 return Ref('more')
2289enddef
2290
2291def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002292 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002293 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002294enddef
2295
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002296def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002297 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002298 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002299enddef
2300
2301def Test_closure_ref_after_return()
2302 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002303 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002304 unlet g:Ref
2305enddef
2306
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002307def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002308 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002309 g:Extend = (s) => local->add(s)
2310 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002311enddef
2312
2313def Test_closure_two_refs()
2314 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002315 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002316 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002317 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002318 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002319 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002320
2321 unlet g:Extend
2322 unlet g:Read
2323enddef
2324
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002325def ReadRef(Ref: func(): list<string>): string
2326 return join(Ref(), ' ')
2327enddef
2328
Bram Moolenaar5e654232020-09-16 15:22:00 +02002329def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002330 Ref(add)
2331enddef
2332
2333def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002334 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002335 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002336 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002337 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002338 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002339 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002340
2341 unlet g:Extend
2342 unlet g:Read
2343enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002344
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002345def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002346 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002347 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002348enddef
2349
2350def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002351 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002352 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002353enddef
2354
2355def Test_closure_using_argument()
2356 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002357 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002358
2359 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002360 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002361
2362 unlet g:UseArg
2363 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002364
2365 var lines =<< trim END
2366 vim9script
2367 def Test(Fun: func(number): number): list<number>
2368 return map([1, 2, 3], (_, i) => Fun(i))
2369 enddef
2370 def Inc(nr: number): number
2371 return nr + 2
2372 enddef
2373 assert_equal([3, 4, 5], Test(Inc))
2374 END
2375 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002376enddef
2377
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002378def MakeGetAndAppendRefs()
2379 var local = 'a'
2380
2381 def Append(arg: string)
2382 local ..= arg
2383 enddef
2384 g:Append = Append
2385
2386 def Get(): string
2387 return local
2388 enddef
2389 g:Get = Get
2390enddef
2391
2392def Test_closure_append_get()
2393 MakeGetAndAppendRefs()
2394 g:Get()->assert_equal('a')
2395 g:Append('-b')
2396 g:Get()->assert_equal('a-b')
2397 g:Append('-c')
2398 g:Get()->assert_equal('a-b-c')
2399
2400 unlet g:Append
2401 unlet g:Get
2402enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002403
Bram Moolenaar04b12692020-05-04 23:24:44 +02002404def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002405 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002406 def Closure(arg: string): string
2407 return local .. arg
2408 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002409 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002410enddef
2411
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002412func GetResult(Ref)
2413 return a:Ref('some')
2414endfunc
2415
2416def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002417 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002418 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002419 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002420enddef
2421
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002422def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002423 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002424 vim9script
2425 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002426 var name = 0
2427 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002428 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002429 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002430 enddef
2431 Func()
2432 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002433 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002434enddef
2435
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002436def Test_nested_closure_used()
2437 var lines =<< trim END
2438 vim9script
2439 def Func()
2440 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002441 var Closure = () => x
2442 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002443 enddef
2444 Func()
2445 assert_equal('hello', g:Myclosure())
2446 END
2447 CheckScriptSuccess(lines)
2448enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002449
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002450def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002451 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002452 vim9script
2453 def FuncA()
2454 FuncB(0)
2455 enddef
2456 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002457 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002458 enddef
2459 FuncA()
2460 END
2461 CheckScriptFailure(lines, 'E1012:')
2462enddef
2463
Bram Moolenaarf112f302020-12-20 17:47:52 +01002464def Test_global_closure()
2465 var lines =<< trim END
2466 vim9script
2467 def ReverseEveryNLines(n: number, line1: number, line2: number)
2468 var mods = 'sil keepj keepp lockm '
2469 var range = ':' .. line1 .. ',' .. line2
2470 def g:Offset(): number
2471 var offset = (line('.') - line1 + 1) % n
2472 return offset != 0 ? offset : n
2473 enddef
2474 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2475 enddef
2476
2477 new
2478 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2479 ReverseEveryNLines(3, 1, 9)
2480 END
2481 CheckScriptSuccess(lines)
2482 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2483 assert_equal(expected, getline(1, 9))
2484 bwipe!
2485enddef
2486
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002487def Test_global_closure_called_directly()
2488 var lines =<< trim END
2489 vim9script
2490 def Outer()
2491 var x = 1
2492 def g:Inner()
2493 var y = x
2494 x += 1
2495 assert_equal(1, y)
2496 enddef
2497 g:Inner()
2498 assert_equal(2, x)
2499 enddef
2500 Outer()
2501 END
2502 CheckScriptSuccess(lines)
2503 delfunc g:Inner
2504enddef
2505
Bram Moolenaar69c76172021-12-02 16:38:52 +00002506def Test_closure_called_from_legacy()
2507 var lines =<< trim END
2508 vim9script
2509 def Func()
2510 var outer = 'foo'
2511 var F = () => {
2512 outer = 'bar'
2513 }
2514 execute printf('call %s()', string(F))
2515 enddef
2516 Func()
2517 END
2518 CheckScriptFailure(lines, 'E1248')
2519enddef
2520
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002521def Test_failure_in_called_function()
2522 # this was using the frame index as the return value
2523 var lines =<< trim END
2524 vim9script
2525 au TerminalWinOpen * eval [][0]
2526 def PopupTerm(a: any)
2527 # make sure typvals on stack are string
2528 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2529 FireEvent()
2530 enddef
2531 def FireEvent()
2532 do TerminalWinOpen
2533 enddef
2534 # use try/catch to make eval fail
2535 try
2536 call PopupTerm(0)
2537 catch
2538 endtry
2539 au! TerminalWinOpen
2540 END
2541 CheckScriptSuccess(lines)
2542enddef
2543
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002544def Test_nested_lambda()
2545 var lines =<< trim END
2546 vim9script
2547 def Func()
2548 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002549 var Lambda1 = () => 7
2550 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002551 var res = Lambda2()
2552 assert_equal([7, 4], res)
2553 enddef
2554 Func()
2555 END
2556 CheckScriptSuccess(lines)
2557enddef
2558
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002559def Test_double_nested_lambda()
2560 var lines =<< trim END
2561 vim9script
2562 def F(head: string): func(string): func(string): string
2563 return (sep: string): func(string): string => ((tail: string): string => {
2564 return head .. sep .. tail
2565 })
2566 enddef
2567 assert_equal('hello-there', F('hello')('-')('there'))
2568 END
2569 CheckScriptSuccess(lines)
2570enddef
2571
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002572def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002573 var lines =<< trim END
2574 vim9script
2575 def F(text: string): func(string): func(string): string
2576 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002577 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002578 })
2579 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002580 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002581 END
2582 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002583
2584 lines =<< trim END
2585 vim9script
2586 echo range(4)->mapnew((_, v) => {
2587 return range(v) ->mapnew((_, s) => {
2588 return string(s)
2589 })
2590 })
2591 END
2592 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002593
2594 lines =<< trim END
2595 vim9script
2596
2597 def s:func()
2598 range(10)
2599 ->mapnew((_, _) => ({
2600 key: range(10)->mapnew((_, _) => {
2601 return ' '
2602 }),
2603 }))
2604 enddef
2605
2606 defcomp
2607 END
2608 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002609enddef
2610
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002611def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002612 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002613 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002614enddef
2615
2616def Test_lambda_arg_shadows_func()
2617 assert_equal([42], Shadowed())
2618enddef
2619
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002620def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002621 var path: string = empty(dir)
2622 \ ? 'empty'
2623 \ : 'full'
2624 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002625enddef
2626
2627def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002628 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002629enddef
2630
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002631def Test_script_var_in_lambda()
2632 var lines =<< trim END
2633 vim9script
2634 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002635 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002636 END
2637 CheckScriptSuccess(lines)
2638enddef
2639
Bram Moolenaar5e654232020-09-16 15:22:00 +02002640def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002641 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002642 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002643 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002644 ->reverse()
2645 return x
2646enddef
2647
2648def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002649 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002650
2651 var lines =<< trim END
2652 vim9script
2653 var res = [{n: 1, m: 2, s: 'xxx'}]
2654 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2655 v.n,
2656 v.m,
2657 substitute(v.s, '.*', 'yyy', '')
2658 ))
2659 assert_equal(['1:2:yyy'], res)
2660 END
2661 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002662enddef
2663
Bram Moolenaarb6571982021-01-08 22:24:19 +01002664def Test_list_lambda()
2665 timer_start(1000, (_) => 0)
2666 var body = execute(timer_info()[0].callback
2667 ->string()
2668 ->substitute("('", ' ', '')
2669 ->substitute("')", '', '')
2670 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002671 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002672enddef
2673
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002674def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002675 var lines =<< trim END
2676 vim9script
2677 var flist: list<func>
2678 for i in range(10)
2679 var inloop = i
2680 flist[i] = () => inloop
2681 endfor
2682 END
2683 CheckScriptSuccess(lines)
2684
2685 lines =<< trim END
2686 vim9script
2687 if true
2688 var outloop = 5
2689 var flist: list<func>
2690 for i in range(10)
2691 flist[i] = () => outloop
2692 endfor
2693 endif
2694 END
2695 CheckScriptSuccess(lines)
2696
2697 lines =<< trim END
2698 vim9script
2699 if true
2700 var outloop = 5
2701 endif
2702 var flist: list<func>
2703 for i in range(10)
2704 flist[i] = () => outloop
2705 endfor
2706 END
2707 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002708
2709 lines =<< trim END
2710 vim9script
2711 for i in range(10)
2712 var Ref = () => 0
2713 endfor
2714 assert_equal(0, ((i) => 0)(0))
2715 END
2716 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002717enddef
2718
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002719def Test_legacy_lambda()
2720 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002721
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002722 var lines =<< trim END
2723 echo {x -> 'hello ' .. x}('foo')
2724 END
2725 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002726
2727 lines =<< trim END
2728 vim9script
2729 def Func()
2730 echo (() => 'no error')()
2731 enddef
2732 legacy call s:Func()
2733 END
2734 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002735enddef
2736
Bram Moolenaarce024c32021-06-26 13:00:49 +02002737def Test_legacy()
2738 var lines =<< trim END
2739 vim9script
2740 func g:LegacyFunction()
2741 let g:legacyvar = 1
2742 endfunc
2743 def Testit()
2744 legacy call g:LegacyFunction()
2745 enddef
2746 Testit()
2747 assert_equal(1, g:legacyvar)
2748 unlet g:legacyvar
2749 delfunc g:LegacyFunction
2750 END
2751 CheckScriptSuccess(lines)
2752enddef
2753
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002754def Test_legacy_errors()
2755 for cmd in ['if', 'elseif', 'else', 'endif',
2756 'for', 'endfor', 'continue', 'break',
2757 'while', 'endwhile',
2758 'try', 'catch', 'finally', 'endtry']
2759 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2760 endfor
2761enddef
2762
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002763def Test_call_legacy_with_dict()
2764 var lines =<< trim END
2765 vim9script
2766 func Legacy() dict
2767 let g:result = self.value
2768 endfunc
2769 def TestDirect()
2770 var d = {value: 'yes', func: Legacy}
2771 d.func()
2772 enddef
2773 TestDirect()
2774 assert_equal('yes', g:result)
2775 unlet g:result
2776
2777 def TestIndirect()
2778 var d = {value: 'foo', func: Legacy}
2779 var Fi = d.func
2780 Fi()
2781 enddef
2782 TestIndirect()
2783 assert_equal('foo', g:result)
2784 unlet g:result
2785
2786 var d = {value: 'bar', func: Legacy}
2787 d.func()
2788 assert_equal('bar', g:result)
2789 unlet g:result
2790 END
2791 CheckScriptSuccess(lines)
2792enddef
2793
Bram Moolenaarab360522021-01-10 14:02:28 +01002794def DoFilterThis(a: string): list<string>
2795 # closure nested inside another closure using argument
2796 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2797 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2798enddef
2799
2800def Test_nested_closure_using_argument()
2801 assert_equal(['x', 'x2'], DoFilterThis('x'))
2802enddef
2803
Bram Moolenaar0186e582021-01-10 18:33:11 +01002804def Test_triple_nested_closure()
2805 var what = 'x'
2806 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2807 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2808 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2809enddef
2810
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002811func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002812 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002813 call Run_Test_silent_echo()
2814endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002815
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002816def Run_Test_silent_echo()
2817 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002818 vim9script
2819 def EchoNothing()
2820 silent echo ''
2821 enddef
2822 defcompile
2823 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002824 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002825
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002826 # Check that the balloon shows up after a mouse move
2827 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2828 term_sendkeys(buf, ":abc")
2829 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002830
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002831 # clean up
2832 StopVimInTerminal(buf)
2833 delete('XTest_silent_echo')
2834enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002835
Bram Moolenaar171fb922020-10-28 16:54:47 +01002836def SilentlyError()
2837 execute('silent! invalid')
2838 g:did_it = 'yes'
2839enddef
2840
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002841func UserError()
2842 silent! invalid
2843endfunc
2844
2845def SilentlyUserError()
2846 UserError()
2847 g:did_it = 'yes'
2848enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002849
2850" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002851func Test_ignore_silent_error()
2852 let g:did_it = 'no'
2853 call SilentlyError()
2854 call assert_equal('yes', g:did_it)
2855
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002856 let g:did_it = 'no'
2857 call SilentlyUserError()
2858 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002859
2860 unlet g:did_it
2861endfunc
2862
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002863def Test_ignore_silent_error_in_filter()
2864 var lines =<< trim END
2865 vim9script
2866 def Filter(winid: number, key: string): bool
2867 if key == 'o'
2868 silent! eval [][0]
2869 return true
2870 endif
2871 return popup_filter_menu(winid, key)
2872 enddef
2873
Bram Moolenaare0de1712020-12-02 17:36:54 +01002874 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002875 feedkeys("o\r", 'xnt')
2876 END
2877 CheckScriptSuccess(lines)
2878enddef
2879
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002880def Fibonacci(n: number): number
2881 if n < 2
2882 return n
2883 else
2884 return Fibonacci(n - 1) + Fibonacci(n - 2)
2885 endif
2886enddef
2887
Bram Moolenaar985116a2020-07-12 17:31:09 +02002888def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002889 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002890enddef
2891
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002892def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002893 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002894 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002895 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002896 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002897 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002898enddef
2899
2900def Test_closure_in_map()
2901 mkdir('XclosureDir/tdir', 'p')
2902 writefile(['111'], 'XclosureDir/file1')
2903 writefile(['222'], 'XclosureDir/file2')
2904 writefile(['333'], 'XclosureDir/tdir/file3')
2905
Bram Moolenaare0de1712020-12-02 17:36:54 +01002906 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002907
2908 delete('XclosureDir', 'rf')
2909enddef
2910
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002911def Test_invalid_function_name()
2912 var lines =<< trim END
2913 vim9script
2914 def s: list<string>
2915 END
2916 CheckScriptFailure(lines, 'E129:')
2917
2918 lines =<< trim END
2919 vim9script
2920 def g: list<string>
2921 END
2922 CheckScriptFailure(lines, 'E129:')
2923
2924 lines =<< trim END
2925 vim9script
2926 def <SID>: list<string>
2927 END
2928 CheckScriptFailure(lines, 'E884:')
2929
2930 lines =<< trim END
2931 vim9script
2932 def F list<string>
2933 END
2934 CheckScriptFailure(lines, 'E488:')
2935enddef
2936
Bram Moolenaara90afb92020-07-15 22:38:56 +02002937def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002938 var lines =<< trim END
2939 var Xsetlist: func
2940 Xsetlist = function('setloclist', [0])
2941 Xsetlist([], ' ', {title: 'test'})
2942 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002943
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002944 Xsetlist = function('setloclist', [0, [], ' '])
2945 Xsetlist({title: 'test'})
2946 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002947
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002948 Xsetlist = function('setqflist')
2949 Xsetlist([], ' ', {title: 'test'})
2950 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002951
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002952 Xsetlist = function('setqflist', [[], ' '])
2953 Xsetlist({title: 'test'})
2954 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002955
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002956 var Len: func: number = function('len', ['word'])
2957 assert_equal(4, Len())
2958
2959 var RepeatFunc = function('repeat', ['o'])
2960 assert_equal('ooooo', RepeatFunc(5))
2961 END
2962 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002963
2964 lines =<< trim END
2965 vim9script
2966 def Foo(Parser: any)
2967 enddef
2968 var Expr: func(dict<any>): dict<any>
2969 const Call = Foo(Expr)
2970 END
2971 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002972enddef
2973
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002974def Test_cmd_modifier()
2975 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002976 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002977enddef
2978
2979def Test_restore_modifiers()
2980 # check that when compiling a :def function command modifiers are not messed
2981 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002982 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002983 vim9script
2984 set eventignore=
2985 autocmd QuickFixCmdPost * copen
2986 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002987 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002988 enddef
2989 func Func()
2990 noautocmd call s:AutocmdsDisabled()
2991 let g:ei_after = &eventignore
2992 endfunc
2993 Func()
2994 END
2995 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002996 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002997enddef
2998
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002999def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003000 eval 1 + 2
3001 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003002 # call not on fourth line
3003 StackBot()
3004enddef
3005
3006def StackBot()
3007 # throw an error
3008 eval [][0]
3009enddef
3010
3011def Test_callstack_def()
3012 try
3013 StackTop()
3014 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003015 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003016 endtry
3017enddef
3018
Bram Moolenaare8211a32020-10-09 22:04:29 +02003019" Re-using spot for variable used in block
3020def Test_block_scoped_var()
3021 var lines =<< trim END
3022 vim9script
3023 def Func()
3024 var x = ['a', 'b', 'c']
3025 if 1
3026 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003027 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003028 endif
3029 var z = x
3030 assert_equal(['x', 'x', 'x'], z)
3031 enddef
3032 Func()
3033 END
3034 CheckScriptSuccess(lines)
3035enddef
3036
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003037def Test_reset_did_emsg()
3038 var lines =<< trim END
3039 @s = 'blah'
3040 au BufWinLeave * #
3041 def Func()
3042 var winid = popup_create('popup', {})
3043 exe '*s'
3044 popup_close(winid)
3045 enddef
3046 Func()
3047 END
3048 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003049 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003050enddef
3051
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003052def Test_did_emsg_reset()
3053 # executing an autocommand resets did_emsg, this should not result in a
3054 # builtin function considered failing
3055 var lines =<< trim END
3056 vim9script
3057 au BufWinLeave * #
3058 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003059 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003060 eval [][0]
3061 enddef
3062 nno <F3> <cmd>call <sid>Func()<cr>
3063 feedkeys("\<F3>\e", 'xt')
3064 END
3065 writefile(lines, 'XemsgReset')
3066 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3067 delete('XemsgReset')
3068 nunmap <F3>
3069 au! BufWinLeave
3070enddef
3071
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003072def Test_abort_with_silent_call()
3073 var lines =<< trim END
3074 vim9script
3075 g:result = 'none'
3076 def Func()
3077 g:result += 3
3078 g:result = 'yes'
3079 enddef
3080 # error is silenced, but function aborts on error
3081 silent! Func()
3082 assert_equal('none', g:result)
3083 unlet g:result
3084 END
3085 CheckScriptSuccess(lines)
3086enddef
3087
Bram Moolenaarf665e972020-12-05 19:17:16 +01003088def Test_continues_with_silent_error()
3089 var lines =<< trim END
3090 vim9script
3091 g:result = 'none'
3092 def Func()
3093 silent! g:result += 3
3094 g:result = 'yes'
3095 enddef
3096 # error is silenced, function does not abort
3097 Func()
3098 assert_equal('yes', g:result)
3099 unlet g:result
3100 END
3101 CheckScriptSuccess(lines)
3102enddef
3103
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003104def Test_abort_even_with_silent()
3105 var lines =<< trim END
3106 vim9script
3107 g:result = 'none'
3108 def Func()
3109 eval {-> ''}() .. '' .. {}['X']
3110 g:result = 'yes'
3111 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003112 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003113 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003114 unlet g:result
3115 END
3116 CheckScriptSuccess(lines)
3117enddef
3118
Bram Moolenaarf665e972020-12-05 19:17:16 +01003119def Test_cmdmod_silent_restored()
3120 var lines =<< trim END
3121 vim9script
3122 def Func()
3123 g:result = 'none'
3124 silent! g:result += 3
3125 g:result = 'none'
3126 g:result += 3
3127 enddef
3128 Func()
3129 END
3130 # can't use CheckScriptFailure, it ignores the :silent!
3131 var fname = 'Xdefsilent'
3132 writefile(lines, fname)
3133 var caught = 'no'
3134 try
3135 exe 'source ' .. fname
3136 catch /E1030:/
3137 caught = 'yes'
3138 assert_match('Func, line 4', v:throwpoint)
3139 endtry
3140 assert_equal('yes', caught)
3141 delete(fname)
3142enddef
3143
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003144def Test_cmdmod_silent_nested()
3145 var lines =<< trim END
3146 vim9script
3147 var result = ''
3148
3149 def Error()
3150 result ..= 'Eb'
3151 eval [][0]
3152 result ..= 'Ea'
3153 enddef
3154
3155 def Crash()
3156 result ..= 'Cb'
3157 sil! Error()
3158 result ..= 'Ca'
3159 enddef
3160
3161 Crash()
3162 assert_equal('CbEbEaCa', result)
3163 END
3164 CheckScriptSuccess(lines)
3165enddef
3166
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003167def Test_dict_member_with_silent()
3168 var lines =<< trim END
3169 vim9script
3170 g:result = 'none'
3171 var d: dict<any>
3172 def Func()
3173 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003174 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003175 catch
3176 endtry
3177 enddef
3178 silent! Func()
3179 assert_equal('0', g:result)
3180 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003181 END
3182 CheckScriptSuccess(lines)
3183enddef
3184
Bram Moolenaarf9041332021-01-21 19:41:16 +01003185def Test_skip_cmds_with_silent()
3186 var lines =<< trim END
3187 vim9script
3188
3189 def Func(b: bool)
3190 Crash()
3191 enddef
3192
3193 def Crash()
3194 sil! :/not found/d _
3195 sil! :/not found/put _
3196 enddef
3197
3198 Func(true)
3199 END
3200 CheckScriptSuccess(lines)
3201enddef
3202
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003203def Test_opfunc()
3204 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3205 def g:Opfunc(_: any): string
3206 setline(1, 'ASDF')
3207 return ''
3208 enddef
3209 new
3210 setline(1, 'asdf')
3211 feedkeys("\<F3>$", 'x')
3212 assert_equal('ASDF', getline(1))
3213
3214 bwipe!
3215 nunmap <F3>
3216enddef
3217
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003218func Test_opfunc_error()
3219 CheckScreendump
3220 call Run_Test_opfunc_error()
3221endfunc
3222
3223def Run_Test_opfunc_error()
3224 # test that the error from Opfunc() is displayed right away
3225 var lines =<< trim END
3226 vim9script
3227
3228 def Opfunc(type: string)
3229 try
3230 eval [][0]
3231 catch /nothing/ # error not caught
3232 endtry
3233 enddef
3234 &operatorfunc = Opfunc
3235 nnoremap <expr> l <SID>L()
3236 def L(): string
3237 return 'l'
3238 enddef
3239 'x'->repeat(10)->setline(1)
3240 feedkeys('g@l', 'n')
3241 feedkeys('llll')
3242 END
3243 call writefile(lines, 'XTest_opfunc_error')
3244
3245 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003246 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003247 VerifyScreenDump(buf, 'Test_opfunc_error', {})
3248
3249 # clean up
3250 StopVimInTerminal(buf)
3251 delete('XTest_opfunc_error')
3252enddef
3253
Bram Moolenaar077a4232020-12-22 18:33:27 +01003254" this was crashing on exit
3255def Test_nested_lambda_in_closure()
3256 var lines =<< trim END
3257 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003258 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003259 def Outer()
3260 def g:Inner()
3261 echo map([1, 2, 3], {_, v -> v + 1})
3262 enddef
3263 g:Inner()
3264 enddef
3265 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003266 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003267 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003268 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003269 return
3270 endif
3271 assert_equal(['Done'], readfile('XnestedDone'))
3272 delete('XnestedDone')
3273enddef
3274
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003275def Test_check_func_arg_types()
3276 var lines =<< trim END
3277 vim9script
3278 def F1(x: string): string
3279 return x
3280 enddef
3281
3282 def F2(x: number): number
3283 return x + 1
3284 enddef
3285
3286 def G(g: func): dict<func>
3287 return {f: g}
3288 enddef
3289
3290 def H(d: dict<func>): string
3291 return d.f('a')
3292 enddef
3293 END
3294
3295 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3296 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3297enddef
3298
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003299def Test_list_any_type_checked()
3300 var lines =<< trim END
3301 vim9script
3302 def Foo()
3303 --decl--
3304 Bar(l)
3305 enddef
3306 def Bar(ll: list<dict<any>>)
3307 enddef
3308 Foo()
3309 END
3310 lines[2] = 'var l: list<any>'
3311 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3312
3313 lines[2] = 'var l: list<any> = []'
3314 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3315
3316 lines[2] = 'var l: list<any> = [11]'
3317 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3318enddef
3319
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003320def Test_compile_error()
3321 var lines =<< trim END
3322 def g:Broken()
3323 echo 'a' + {}
3324 enddef
3325 call g:Broken()
3326 END
3327 # First call: compilation error
3328 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3329
3330 # Second call won't try compiling again
3331 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003332 delfunc g:Broken
3333
3334 # No error when compiling with :silent!
3335 lines =<< trim END
3336 def g:Broken()
3337 echo 'a' + []
3338 enddef
3339 silent! defcompile
3340 END
3341 CheckScriptSuccess(lines)
3342
3343 # Calling the function won't try compiling again
3344 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3345 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003346enddef
3347
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003348def Test_ignored_argument()
3349 var lines =<< trim END
3350 vim9script
3351 def Ignore(_, _): string
3352 return 'yes'
3353 enddef
3354 assert_equal('yes', Ignore(1, 2))
3355
3356 func Ok(_)
3357 return a:_
3358 endfunc
3359 assert_equal('ok', Ok('ok'))
3360
3361 func Oktoo()
3362 let _ = 'too'
3363 return _
3364 endfunc
3365 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003366
3367 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003368 END
3369 CheckScriptSuccess(lines)
3370
3371 lines =<< trim END
3372 def Ignore(_: string): string
3373 return _
3374 enddef
3375 defcompile
3376 END
3377 CheckScriptFailure(lines, 'E1181:', 1)
3378
3379 lines =<< trim END
3380 var _ = 1
3381 END
3382 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003383
3384 lines =<< trim END
3385 var x = _
3386 END
3387 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003388enddef
3389
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003390def Test_too_many_arguments()
3391 var lines =<< trim END
3392 echo [0, 1, 2]->map(() => 123)
3393 END
3394 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3395
3396 lines =<< trim END
3397 echo [0, 1, 2]->map((_) => 123)
3398 END
3399 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3400enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003401
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003402def Test_closing_brace_at_start_of_line()
3403 var lines =<< trim END
3404 def Func()
3405 enddef
3406 Func(
3407 )
3408 END
3409 call CheckDefAndScriptSuccess(lines)
3410enddef
3411
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003412func CreateMydict()
3413 let g:mydict = {}
3414 func g:mydict.afunc()
3415 let g:result = self.key
3416 endfunc
3417endfunc
3418
3419def Test_numbered_function_reference()
3420 CreateMydict()
3421 var output = execute('legacy func g:mydict.afunc')
3422 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3423 execute 'function(' .. funcName .. ', [], {key: 42})()'
3424 # check that the function still exists
3425 assert_equal(output, execute('legacy func g:mydict.afunc'))
3426 unlet g:mydict
3427enddef
3428
Bram Moolenaar20677332021-06-06 17:02:53 +02003429if has('python3')
3430 def Test_python3_heredoc()
3431 py3 << trim EOF
3432 import vim
3433 vim.vars['didit'] = 'yes'
3434 EOF
3435 assert_equal('yes', g:didit)
3436
3437 python3 << trim EOF
3438 import vim
3439 vim.vars['didit'] = 'again'
3440 EOF
3441 assert_equal('again', g:didit)
3442 enddef
3443endif
3444
3445" This messes up syntax highlight, keep near the end.
3446if has('lua')
3447 def Test_lua_heredoc()
3448 g:d = {}
3449 lua << trim EOF
3450 x = vim.eval('g:d')
3451 x['key'] = 'val'
3452 EOF
3453 assert_equal('val', g:d.key)
3454 enddef
3455endif
3456
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003457
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003458" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker