blob: 935079f7ae35a62e8ca43a20e20ff9d1b0eedfbf [file] [log] [blame]
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001" Test various aspects of the Vim9 script language.
2
3source check.vim
Bram Moolenaarad304702020-09-06 18:22:53 +02004source term_util.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005source view_util.vim
Bram Moolenaar04b12692020-05-04 23:24:44 +02006source vim9.vim
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007source screendump.vim
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008
9func Test_def_basic()
10 def SomeFunc(): string
11 return 'yes'
12 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +020013 call SomeFunc()->assert_equal('yes')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020014endfunc
15
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020016func Test_compiling_error()
17 " use a terminal to see the whole error message
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020018 CheckRunVimInTerminal
19
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020020 call TestCompilingError()
Bram Moolenaare8c46602021-04-05 22:27:37 +020021 call TestCompilingErrorInTry()
Bram Moolenaar2b9b17e2020-10-13 18:38:11 +020022endfunc
23
24def TestCompilingError()
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020025 var lines =<< trim END
26 vim9script
27 def Fails()
28 echo nothing
29 enddef
30 defcompile
31 END
Bram Moolenaare8c46602021-04-05 22:27:37 +020032 writefile(lines, 'XTest_compile_error')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020033 var buf = RunVimInTerminal('-S XTest_compile_error',
Bram Moolenaare0de1712020-12-02 17:36:54 +010034 {rows: 10, wait_for_ruler: 0})
Bram Moolenaare8c46602021-04-05 22:27:37 +020035 WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
Bram Moolenaar03dfde22021-02-14 13:17:22 +010036 Term_getlines(buf, range(1, 9))))
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020037
38 # clean up
Bram Moolenaare8c46602021-04-05 22:27:37 +020039 StopVimInTerminal(buf)
40 delete('XTest_compile_error')
41enddef
42
43def TestCompilingErrorInTry()
44 var dir = 'Xdir/autoload'
45 mkdir(dir, 'p')
46
47 var lines =<< trim END
48 vim9script
49 def script#OnlyCompiled()
50 g:runtime = 'yes'
51 invalid
52 enddef
53 END
54 writefile(lines, dir .. '/script.vim')
55
56 lines =<< trim END
57 vim9script
58 todo
59 try
60 script#OnlyCompiled()
61 catch /nothing/
62 endtry
63 END
64 lines[1] = 'set rtp=' .. getcwd() .. '/Xdir'
65 writefile(lines, 'XTest_compile_error')
66
67 var buf = RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
68 WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
69 Term_getlines(buf, range(1, 9))))
70
71 # clean up
72 StopVimInTerminal(buf)
73 delete('XTest_compile_error')
74 delete('Xdir', 'rf')
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020075enddef
76
Bram Moolenaarb55d6182021-06-08 22:01:53 +020077def Test_compile_error_in_called_function()
78 var lines =<< trim END
79 vim9script
80 var n: number
81 def Foo()
82 &hls = n
83 enddef
84 def Bar()
85 Foo()
86 enddef
87 silent! Foo()
88 Bar()
89 END
90 CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
91enddef
92
Bram Moolenaar22f17a22021-06-21 20:48:58 +020093def Test_wrong_function_name()
94 var lines =<< trim END
95 vim9script
96 func _Foo()
97 echo 'foo'
98 endfunc
99 END
100 CheckScriptFailure(lines, 'E128:')
101
102 lines =<< trim END
103 vim9script
104 def _Foo()
105 echo 'foo'
106 enddef
107 END
108 CheckScriptFailure(lines, 'E128:')
109enddef
110
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +0200111def Test_autoload_name_mismatch()
112 var dir = 'Xdir/autoload'
113 mkdir(dir, 'p')
114
115 var lines =<< trim END
116 vim9script
117 def scriptX#Function()
118 # comment
119 g:runtime = 'yes'
120 enddef
121 END
122 writefile(lines, dir .. '/script.vim')
123
124 var save_rtp = &rtp
125 exe 'set rtp=' .. getcwd() .. '/Xdir'
126 lines =<< trim END
127 call script#Function()
128 END
129 CheckScriptFailure(lines, 'E746:', 2)
130
131 &rtp = save_rtp
132 delete(dir, 'rf')
133enddef
134
Bram Moolenaarf0a40692021-06-11 22:05:47 +0200135def Test_autoload_names()
136 var dir = 'Xdir/autoload'
137 mkdir(dir, 'p')
138
139 var lines =<< trim END
140 func foobar#function()
141 return 'yes'
142 endfunc
143 let foobar#var = 'no'
144 END
145 writefile(lines, dir .. '/foobar.vim')
146
147 var save_rtp = &rtp
148 exe 'set rtp=' .. getcwd() .. '/Xdir'
149
150 lines =<< trim END
151 assert_equal('yes', foobar#function())
152 var Function = foobar#function
153 assert_equal('yes', Function())
154
155 assert_equal('no', foobar#var)
156 END
157 CheckDefAndScriptSuccess(lines)
158
159 &rtp = save_rtp
160 delete(dir, 'rf')
161enddef
162
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200163def Test_autoload_error_in_script()
164 var dir = 'Xdir/autoload'
165 mkdir(dir, 'p')
166
167 var lines =<< trim END
168 func scripterror#function()
169 let g:called_function = 'yes'
170 endfunc
171 let 0 = 1
172 END
173 writefile(lines, dir .. '/scripterror.vim')
174
175 var save_rtp = &rtp
176 exe 'set rtp=' .. getcwd() .. '/Xdir'
177
178 g:called_function = 'no'
179 # The error in the autoload script cannot be checked with assert_fails(), use
180 # CheckDefSuccess() instead of CheckDefFailure()
181 try
182 CheckDefSuccess(['scripterror#function()'])
183 catch
184 assert_match('E121: Undefined variable: 0', v:exception)
185 endtry
186 assert_equal('no', g:called_function)
187
188 lines =<< trim END
189 func scriptcaught#function()
190 let g:called_function = 'yes'
191 endfunc
192 try
193 let 0 = 1
194 catch
195 let g:caught = v:exception
196 endtry
197 END
198 writefile(lines, dir .. '/scriptcaught.vim')
199
200 g:called_function = 'no'
201 CheckDefSuccess(['scriptcaught#function()'])
202 assert_match('E121: Undefined variable: 0', g:caught)
203 assert_equal('yes', g:called_function)
204
205 &rtp = save_rtp
206 delete(dir, 'rf')
207enddef
208
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100209def CallRecursive(n: number): number
210 return CallRecursive(n + 1)
211enddef
212
213def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100214 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100215enddef
216
217def Test_funcdepth_error()
218 set maxfuncdepth=10
219
220 var caught = false
221 try
222 CallRecursive(1)
223 catch /E132:/
224 caught = true
225 endtry
226 assert_true(caught)
227
228 caught = false
229 try
230 CallMapRecursive([1])
231 catch /E132:/
232 caught = true
233 endtry
234 assert_true(caught)
235
236 set maxfuncdepth&
237enddef
238
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100239def Test_endfunc_enddef()
240 var lines =<< trim END
241 def Test()
242 echo 'test'
243 endfunc
244 enddef
245 END
246 CheckScriptFailure(lines, 'E1151:', 3)
247
248 lines =<< trim END
249 def Test()
250 func Nested()
251 echo 'test'
252 enddef
253 enddef
254 END
255 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100256
257 lines =<< trim END
258 def Ok()
259 echo 'hello'
260 enddef | echo 'there'
261 def Bad()
262 echo 'hello'
263 enddef there
264 END
265 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100266enddef
267
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100268def Test_missing_endfunc_enddef()
269 var lines =<< trim END
270 vim9script
271 def Test()
272 echo 'test'
273 endef
274 END
275 CheckScriptFailure(lines, 'E1057:', 2)
276
277 lines =<< trim END
278 vim9script
279 func Some()
280 echo 'test'
281 enfffunc
282 END
283 CheckScriptFailure(lines, 'E126:', 2)
284enddef
285
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100286def Test_white_space_before_paren()
287 var lines =<< trim END
288 vim9script
289 def Test ()
290 echo 'test'
291 enddef
292 END
293 CheckScriptFailure(lines, 'E1068:', 2)
294
295 lines =<< trim END
296 vim9script
297 func Test ()
298 echo 'test'
299 endfunc
300 END
301 CheckScriptFailure(lines, 'E1068:', 2)
302
303 lines =<< trim END
304 def Test ()
305 echo 'test'
306 enddef
307 END
308 CheckScriptFailure(lines, 'E1068:', 1)
309
310 lines =<< trim END
311 func Test ()
312 echo 'test'
313 endfunc
314 END
315 CheckScriptSuccess(lines)
316enddef
317
Bram Moolenaar832ea892021-01-08 21:55:26 +0100318def Test_enddef_dict_key()
319 var d = {
320 enddef: 'x',
321 endfunc: 'y',
322 }
323 assert_equal({enddef: 'x', endfunc: 'y'}, d)
324enddef
325
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200326def ReturnString(): string
327 return 'string'
328enddef
329
330def ReturnNumber(): number
331 return 123
332enddef
333
334let g:notNumber = 'string'
335
336def ReturnGlobal(): number
337 return g:notNumber
338enddef
339
340def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200341 ReturnString()->assert_equal('string')
342 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200343 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200344enddef
345
Bram Moolenaare32e5162021-01-21 20:21:29 +0100346def Test_check_argument_type()
347 var lines =<< trim END
348 vim9script
349 def Val(a: number, b: number): number
350 return 0
351 enddef
352 def Func()
353 var x: any = true
354 Val(0, x)
355 enddef
356 disass Func
357 Func()
358 END
359 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
360enddef
361
Bram Moolenaarefd88552020-06-18 20:50:10 +0200362def Test_missing_return()
363 CheckDefFailure(['def Missing(): number',
364 ' if g:cond',
365 ' echo "no return"',
366 ' else',
367 ' return 0',
368 ' endif'
369 'enddef'], 'E1027:')
370 CheckDefFailure(['def Missing(): number',
371 ' if g:cond',
372 ' return 1',
373 ' else',
374 ' echo "no return"',
375 ' endif'
376 'enddef'], 'E1027:')
377 CheckDefFailure(['def Missing(): number',
378 ' if g:cond',
379 ' return 1',
380 ' else',
381 ' return 2',
382 ' endif'
383 ' return 3'
384 'enddef'], 'E1095:')
385enddef
386
Bram Moolenaar403dc312020-10-17 19:29:51 +0200387def Test_return_bool()
388 var lines =<< trim END
389 vim9script
390 def MenuFilter(id: number, key: string): bool
391 return popup_filter_menu(id, key)
392 enddef
393 def YesnoFilter(id: number, key: string): bool
394 return popup_filter_yesno(id, key)
395 enddef
396 defcompile
397 END
398 CheckScriptSuccess(lines)
399enddef
400
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200401let s:nothing = 0
402def ReturnNothing()
403 s:nothing = 1
404 if true
405 return
406 endif
407 s:nothing = 2
408enddef
409
410def Test_return_nothing()
411 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200412 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200413enddef
414
Bram Moolenaar648ea762021-01-15 19:04:32 +0100415def Test_return_invalid()
416 var lines =<< trim END
417 vim9script
418 def Func(): invalid
419 return xxx
420 enddef
421 defcompile
422 END
423 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100424
425 lines =<< trim END
426 vim9script
427 def Test(Fun: func(number): number): list<number>
428 return map([1, 2, 3], (_, i) => Fun(i))
429 enddef
430 defcompile
431 def Inc(nr: number): nr
432 return nr + 2
433 enddef
434 echo Test(Inc)
435 END
436 # doing this twice was leaking memory
437 CheckScriptFailure(lines, 'E1010:')
438 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100439enddef
440
Bram Moolenaarefc084e2021-09-09 22:30:52 +0200441def Test_return_list_any()
442 var lines =<< trim END
443 vim9script
444 def Func(): list<string>
445 var l: list<any>
446 l->add('string')
447 return l
448 enddef
449 echo Func()
450 END
451 CheckScriptFailure(lines, 'E1012:')
452 lines =<< trim END
453 vim9script
454 def Func(): list<string>
455 var l: list<any>
456 l += ['string']
457 return l
458 enddef
459 echo Func()
460 END
461 CheckScriptFailure(lines, 'E1012:')
462enddef
463
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464func Increment()
465 let g:counter += 1
466endfunc
467
468def Test_call_ufunc_count()
469 g:counter = 1
470 Increment()
471 Increment()
472 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200473 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200474 g:counter->assert_equal(4)
475 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 unlet g:counter
477enddef
478
479def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200480 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200481 for s in rest
482 res ..= ',' .. s
483 endfor
484 return res
485enddef
486
487def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200488 MyVarargs('one')->assert_equal('one')
489 MyVarargs('one', 'two')->assert_equal('one,two')
490 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200491enddef
492
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200493def Test_call_white_space()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +0000494 CheckDefAndScriptFailure(["call Test ('text')"], ['E476:', 'E1068:'])
Bram Moolenaar01dd6c32021-09-05 16:36:23 +0200495enddef
496
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200497def MyDefaultArgs(name = 'string'): string
498 return name
499enddef
500
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200501def MyDefaultSecond(name: string, second: bool = true): string
502 return second ? name : 'none'
503enddef
504
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200505
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200506def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200507 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200508 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200509 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200510 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200511
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200512 MyDefaultSecond('test')->assert_equal('test')
513 MyDefaultSecond('test', true)->assert_equal('test')
514 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200515
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200516 var lines =<< trim END
517 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
518 return name .. aa .. bb
519 enddef
520
521 MyDefaultThird('->')->assert_equal('->aabb')
522 MyDefaultThird('->', v:none)->assert_equal('->aabb')
523 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
524 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
525 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
526 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
527 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200528
529 def DefArg(mandatory: any, optional = mandatory): string
530 return mandatory .. optional
531 enddef
532 DefArg(1234)->assert_equal('12341234')
533 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200534 END
535 CheckDefAndScriptSuccess(lines)
536
Bram Moolenaar822ba242020-05-24 23:00:18 +0200537 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100538 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200539 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100540 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200541 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200543 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100544 vim9script
545 def Func(a = b == 0 ? 1 : 2, b = 0)
546 enddef
547 defcompile
548 END
549 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000550
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000551 # using script variable requires matching type or type cast when executed
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000552 lines =<< trim END
553 vim9script
554 var a: any
555 def Func(arg: string = a)
556 echo arg
557 enddef
558 defcompile
559 END
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000560 CheckScriptSuccess(lines + ['a = "text"', 'Func()'])
561 CheckScriptFailure(lines + ['a = 123', 'Func()'], 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000562
563 # using global variable does not require type cast
564 lines =<< trim END
565 vim9script
566 def Func(arg: string = g:str)
567 echo arg
568 enddef
569 g:str = 'works'
570 Func()
571 END
572 CheckScriptSuccess(lines)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200573enddef
574
Bram Moolenaarcef12702021-01-04 14:09:43 +0100575def FuncWithComment( # comment
576 a: number, #comment
577 b: bool, # comment
578 c: string) #comment
579 assert_equal(4, a)
580 assert_equal(true, b)
581 assert_equal('yes', c)
582enddef
583
584def Test_func_with_comments()
585 FuncWithComment(4, true, 'yes')
586
587 var lines =<< trim END
588 def Func(# comment
589 arg: string)
590 enddef
591 END
592 CheckScriptFailure(lines, 'E125:', 1)
593
594 lines =<< trim END
595 def Func(
596 arg: string# comment
597 )
598 enddef
599 END
600 CheckScriptFailure(lines, 'E475:', 2)
601
602 lines =<< trim END
603 def Func(
604 arg: string
605 )# comment
606 enddef
607 END
608 CheckScriptFailure(lines, 'E488:', 3)
609enddef
610
Bram Moolenaar04b12692020-05-04 23:24:44 +0200611def Test_nested_function()
Bram Moolenaar38453522021-11-28 22:00:12 +0000612 def NestedDef(arg: string): string
Bram Moolenaar04b12692020-05-04 23:24:44 +0200613 return 'nested ' .. arg
614 enddef
Bram Moolenaar38453522021-11-28 22:00:12 +0000615 NestedDef(':def')->assert_equal('nested :def')
616
617 func NestedFunc(arg)
618 return 'nested ' .. a:arg
619 endfunc
620 NestedFunc(':func')->assert_equal('nested :func')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200621
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200622 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
623 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
624
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200625 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
626 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200627
Bram Moolenaar54021752020-12-06 18:50:36 +0100628 var lines =<< trim END
629 def Outer()
630 def Inner()
631 # comment
632 enddef
633 def Inner()
634 enddef
635 enddef
636 END
637 CheckDefFailure(lines, 'E1073:')
638
639 lines =<< trim END
640 def Outer()
641 def Inner()
642 # comment
643 enddef
644 def! Inner()
645 enddef
646 enddef
647 END
648 CheckDefFailure(lines, 'E1117:')
649
650 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100651 lines =<< trim END
652 vim9script
653 var thecount = 0
654 if true
655 def Test(): number
656 def TheFunc(): number
657 thecount += 1
658 return thecount
659 enddef
660 return TheFunc()
661 enddef
662 endif
663 defcompile
664 assert_equal(1, Test())
665 assert_equal(2, Test())
666 END
667 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100668
669 # also works when "thecount" is inside the "if" block
670 lines =<< trim END
671 vim9script
672 if true
673 var thecount = 0
674 def Test(): number
675 def TheFunc(): number
676 thecount += 1
677 return thecount
678 enddef
679 return TheFunc()
680 enddef
681 endif
682 defcompile
683 assert_equal(1, Test())
684 assert_equal(2, Test())
685 END
686 CheckScriptSuccess(lines)
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200687
688 lines =<< trim END
689 vim9script
690 def Outer()
691 def Inner()
692 echo 'hello'
693 enddef burp
694 enddef
695 defcompile
696 END
697 CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200698enddef
699
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100700def Test_not_nested_function()
701 echo printf('%d',
702 function('len')('xxx'))
703enddef
704
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200705func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200706 call MyDefaultArgs()->assert_equal('string')
707 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200708 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200709endfunc
710
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200711def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200712 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200713 vim9script
714 def Outer()
715 def g:Inner(): string
716 return 'inner'
717 enddef
718 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200719 defcompile
720 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200721 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200722 delfunc g:Inner
723 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200724 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200725 delfunc g:Inner
726 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200727 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200728 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200729 END
730 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200731
732 lines =<< trim END
733 vim9script
734 def Outer()
Bram Moolenaar38453522021-11-28 22:00:12 +0000735 func g:Inner()
736 return 'inner'
737 endfunc
738 enddef
739 defcompile
740 Outer()
741 g:Inner()->assert_equal('inner')
742 delfunc g:Inner
743 Outer()
744 g:Inner()->assert_equal('inner')
745 delfunc g:Inner
746 Outer()
747 g:Inner()->assert_equal('inner')
748 delfunc g:Inner
749 END
750 CheckScriptSuccess(lines)
751
752 lines =<< trim END
753 vim9script
754 def Outer()
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200755 def g:Inner(): string
756 return 'inner'
757 enddef
758 enddef
759 defcompile
760 Outer()
761 Outer()
762 END
763 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100764 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200765
766 lines =<< trim END
767 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100768 def Outer()
769 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100770 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100771 enddef
772 g:Inner()
773 enddef
774 Outer()
775 END
776 CheckScriptSuccess(lines)
777 delfunc g:Inner
778
779 lines =<< trim END
780 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200781 def Func()
782 echo 'script'
783 enddef
784 def Outer()
785 def Func()
786 echo 'inner'
787 enddef
788 enddef
789 defcompile
790 END
Bram Moolenaard604d782021-11-20 21:46:20 +0000791 CheckScriptFailure(lines, "E1073:", 1)
792
793 lines =<< trim END
794 vim9script
795 def Func()
796 echo 'script'
797 enddef
798 def Func()
799 echo 'script'
800 enddef
801 END
802 CheckScriptFailure(lines, "E1073:", 5)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200803enddef
804
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100805def DefListAll()
806 def
807enddef
808
809def DefListOne()
810 def DefListOne
811enddef
812
813def DefListMatches()
814 def /DefList
815enddef
816
817def Test_nested_def_list()
818 var funcs = split(execute('call DefListAll()'), "\n")
819 assert_true(len(funcs) > 10)
820 assert_true(funcs->index('def DefListAll()') >= 0)
821
822 funcs = split(execute('call DefListOne()'), "\n")
823 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
824
825 funcs = split(execute('call DefListMatches()'), "\n")
826 assert_true(len(funcs) >= 3)
827 assert_true(funcs->index('def DefListAll()') >= 0)
828 assert_true(funcs->index('def DefListOne()') >= 0)
829 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100830
831 var lines =<< trim END
832 vim9script
833 def Func()
834 def +Func+
835 enddef
836 defcompile
837 END
838 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100839enddef
840
Bram Moolenaar333894b2020-08-01 18:53:07 +0200841def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200842 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200843 vim9script
844 def g:Func(): string
845 return 'global'
846 enddef
847 def Func(): string
848 return 'local'
849 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200850 g:Func()->assert_equal('global')
851 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100852 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200853 END
854 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200855
856 lines =<< trim END
857 vim9script
858 def g:Funcy()
859 echo 'funcy'
860 enddef
861 s:Funcy()
862 END
863 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200864enddef
865
Bram Moolenaar0f769812020-09-12 18:32:34 +0200866def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200867 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200868 vim9script
869 def g:Gfunc(): string
870 return 'global'
871 enddef
872 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200873 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200874 return Gfunc('testing')
875 enddef
876 g:Gfunc()->assert_equal('global')
877 AnotherFunc()->assert_equal(7)
878 delfunc g:Gfunc
879 END
880 CheckScriptSuccess(lines)
881
882 lines =<< trim END
883 vim9script
884 def g:Func(): string
885 return 'global'
886 enddef
887 def AnotherFunc()
888 g:Func = function('len')
889 enddef
890 AnotherFunc()
891 END
892 CheckScriptFailure(lines, 'E705:')
893 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200894
895 # global function is found without g: prefix
896 lines =<< trim END
897 vim9script
898 def g:Func(): string
899 return 'global'
900 enddef
901 def AnotherFunc(): string
902 return Func()
903 enddef
904 assert_equal('global', AnotherFunc())
905 delfunc g:Func
906 END
907 CheckScriptSuccess(lines)
908
909 lines =<< trim END
910 vim9script
911 def g:Func(): string
912 return 'global'
913 enddef
914 assert_equal('global', Func())
915 delfunc g:Func
916 END
917 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200918enddef
919
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200920func TakesOneArg(arg)
921 echo a:arg
922endfunc
923
924def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200925 CheckDefFailure(['TakesOneArg()'], 'E119:')
926 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
927 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
928 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200929
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200930 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200931 vim9script
932 def Func(s: string)
933 echo s
934 enddef
935 Func([])
936 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200937 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200938
939 lines =<< trim END
940 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100941 var name = 'piet'
942 def FuncOne(name: string)
943 echo nr
944 enddef
945 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100946 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100947
948 lines =<< trim END
949 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200950 def FuncOne(nr: number)
951 echo nr
952 enddef
953 def FuncTwo()
954 FuncOne()
955 enddef
956 defcompile
957 END
958 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200959 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200960 try
961 source Xscript
962 catch
963 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
964 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
965 didCatch = true
966 endtry
967 assert_true(didCatch)
968
969 lines =<< trim END
970 vim9script
971 def FuncOne(nr: number)
972 echo nr
973 enddef
974 def FuncTwo()
975 FuncOne(1, 2)
976 enddef
977 defcompile
978 END
979 writefile(lines, 'Xscript')
980 didCatch = false
981 try
982 source Xscript
983 catch
984 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
985 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
986 didCatch = true
987 endtry
988 assert_true(didCatch)
989
990 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200991enddef
992
Bram Moolenaar50824712020-12-20 21:10:17 +0100993def Test_call_funcref_wrong_args()
994 var head =<< trim END
995 vim9script
996 def Func3(a1: string, a2: number, a3: list<number>)
997 echo a1 .. a2 .. a3[0]
998 enddef
999 def Testme()
1000 var funcMap: dict<func> = {func: Func3}
1001 END
1002 var tail =<< trim END
1003 enddef
1004 Testme()
1005 END
1006 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
1007
1008 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
1009 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001010
1011 var lines =<< trim END
1012 vim9script
1013 var Ref: func(number): any
1014 Ref = (j) => !j
1015 echo Ref(false)
1016 END
1017 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
1018
1019 lines =<< trim END
1020 vim9script
1021 var Ref: func(number): any
1022 Ref = (j) => !j
1023 call Ref(false)
1024 END
1025 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +01001026enddef
1027
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001028def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +02001029 var lines =<< trim END
1030 var Callback = (..._) => 'anything'
1031 assert_equal('anything', Callback())
1032 assert_equal('anything', Callback(1))
1033 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +02001034
1035 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +02001036 END
1037 CheckDefAndScriptSuccess(lines)
1038
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001039 CheckDefFailure(['echo ((i) => 0)()'],
1040 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001041
Bram Moolenaar2a389082021-04-09 20:24:31 +02001042 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001043 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001044 echo Ref(1, 'x')
1045 END
1046 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +01001047
1048 lines =<< trim END
1049 var Ref: func(job, string, number)
1050 Ref = (x, y) => 0
1051 END
1052 CheckDefAndScriptFailure(lines, 'E1012:')
1053
1054 lines =<< trim END
1055 var Ref: func(job, string)
1056 Ref = (x, y, z) => 0
1057 END
1058 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001059
1060 lines =<< trim END
1061 var one = 1
1062 var l = [1, 2, 3]
1063 echo map(l, (one) => one)
1064 END
1065 CheckDefFailure(lines, 'E1167:')
1066 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
1067
1068 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +02001069 var Ref: func(any, ?any): bool
1070 Ref = (_, y = 1) => false
1071 END
1072 CheckDefAndScriptFailure(lines, 'E1172:')
1073
1074 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +02001075 var a = 0
1076 var b = (a == 0 ? 1 : 2)
1077 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +02001078 var txt = 'a'
1079 b = (txt =~ 'x' ? 1 : 2)
1080 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +02001081 END
1082 CheckDefAndScriptSuccess(lines)
1083
1084 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001085 def ShadowLocal()
1086 var one = 1
1087 var l = [1, 2, 3]
1088 echo map(l, (one) => one)
1089 enddef
1090 END
1091 CheckDefFailure(lines, 'E1167:')
1092
1093 lines =<< trim END
1094 def Shadowarg(one: number)
1095 var l = [1, 2, 3]
1096 echo map(l, (one) => one)
1097 enddef
1098 END
1099 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +02001100
1101 lines =<< trim END
1102 echo ((a) => a)('aa', 'bb')
1103 END
1104 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +02001105
1106 lines =<< trim END
1107 echo 'aa'->((a) => a)('bb')
1108 END
1109 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
1110 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001111enddef
1112
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001113def Test_lambda_line_nr()
1114 var lines =<< trim END
1115 vim9script
1116 # comment
1117 # comment
1118 var id = timer_start(1'000, (_) => 0)
1119 var out = execute('verbose ' .. timer_info(id)[0].callback
1120 ->string()
1121 ->substitute("('\\|')", ' ', 'g'))
1122 assert_match('Last set from .* line 4', out)
1123 END
1124 CheckScriptSuccess(lines)
1125enddef
1126
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001127def FilterWithCond(x: string, Cond: func(string): bool): bool
1128 return Cond(x)
1129enddef
1130
Bram Moolenaar0346b792021-01-31 22:18:29 +01001131def Test_lambda_return_type()
1132 var lines =<< trim END
1133 var Ref = (): => 123
1134 END
1135 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001136
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001137 # no space before the return type
1138 lines =<< trim END
1139 var Ref = (x):number => x + 1
1140 END
1141 CheckDefAndScriptFailure(lines, 'E1069:', 1)
1142
Bram Moolenaar5f91e742021-03-17 21:29:29 +01001143 # this works
1144 for x in ['foo', 'boo']
1145 echo FilterWithCond(x, (v) => v =~ '^b')
1146 endfor
1147
1148 # this fails
1149 lines =<< trim END
1150 echo FilterWithCond('foo', (v) => v .. '^b')
1151 END
1152 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +02001153
1154 lines =<< trim END
1155 var Lambda1 = (x) => {
1156 return x
1157 }
1158 assert_equal('asdf', Lambda1('asdf'))
1159 var Lambda2 = (x): string => {
1160 return x
1161 }
1162 assert_equal('foo', Lambda2('foo'))
1163 END
1164 CheckDefAndScriptSuccess(lines)
1165
1166 lines =<< trim END
1167 var Lambda = (x): string => {
1168 return x
1169 }
1170 echo Lambda(['foo'])
1171 END
1172 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001173enddef
1174
Bram Moolenaar709664c2020-12-12 14:33:41 +01001175def Test_lambda_uses_assigned_var()
1176 CheckDefSuccess([
1177 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001178 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001179enddef
1180
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001181def Test_pass_legacy_lambda_to_def_func()
1182 var lines =<< trim END
1183 vim9script
1184 func Foo()
1185 eval s:Bar({x -> 0})
1186 endfunc
1187 def Bar(y: any)
1188 enddef
1189 Foo()
1190 END
1191 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001192
1193 lines =<< trim END
1194 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001195 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001196 enddef
1197 legacy call g:TestFunc({-> 0})
1198 delfunc g:TestFunc
1199
1200 def g:TestFunc(f: func(number))
1201 enddef
1202 legacy call g:TestFunc({nr -> 0})
1203 delfunc g:TestFunc
1204 END
1205 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001206enddef
1207
Bram Moolenaar844fb642021-10-23 13:32:30 +01001208def Test_lambda_in_reduce_line_break()
1209 # this was using freed memory
1210 var lines =<< trim END
1211 vim9script
1212 const result: dict<number> =
1213 ['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
1214 ->reduce((acc, val) => {
1215 if has_key(acc, val)
1216 acc[val] += 1
1217 return acc
1218 else
1219 acc[val] = 1
1220 return acc
1221 endif
1222 }, {})
1223 assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
1224 END
1225 CheckScriptSuccess(lines)
1226enddef
1227
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001228def Test_set_opfunc_to_lambda()
1229 var lines =<< trim END
1230 vim9script
1231 nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
1232 def CountSpaces(type = ''): string
1233 if type == ''
1234 &operatorfunc = (t) => CountSpaces(t)
1235 return 'g@'
1236 endif
1237 normal! '[V']y
1238 g:result = getreg('"')->count(' ')
1239 return ''
1240 enddef
1241 new
1242 'a b c d e'->setline(1)
1243 feedkeys("\<F4>", 'x')
1244 assert_equal(4, g:result)
1245 bwipe!
1246 END
1247 CheckScriptSuccess(lines)
1248enddef
1249
Bram Moolenaaref082e12021-12-12 21:02:03 +00001250def Test_set_opfunc_to_global_function()
1251 var lines =<< trim END
1252 vim9script
1253 def g:CountSpaces(type = ''): string
1254 normal! '[V']y
1255 g:result = getreg('"')->count(' ')
1256 return ''
1257 enddef
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001258 # global function works at script level
Bram Moolenaaref082e12021-12-12 21:02:03 +00001259 &operatorfunc = g:CountSpaces
1260 new
1261 'a b c d e'->setline(1)
1262 feedkeys("g@_", 'x')
1263 assert_equal(4, g:result)
Bram Moolenaarb15cf442021-12-16 15:49:43 +00001264
1265 &operatorfunc = ''
1266 g:result = 0
1267 # global function works in :def function
1268 def Func()
1269 &operatorfunc = g:CountSpaces
1270 enddef
1271 Func()
1272 feedkeys("g@_", 'x')
1273 assert_equal(4, g:result)
1274
Bram Moolenaaref082e12021-12-12 21:02:03 +00001275 bwipe!
1276 END
1277 CheckScriptSuccess(lines)
1278 &operatorfunc = ''
1279enddef
1280
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001281def Test_use_script_func_name_with_prefix()
1282 var lines =<< trim END
1283 vim9script
1284 func s:Getit()
1285 return 'it'
1286 endfunc
1287 var Fn = s:Getit
1288 assert_equal('it', Fn())
1289 END
1290 CheckScriptSuccess(lines)
1291enddef
1292
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001293def Test_lambda_type_allocated()
1294 # Check that unreferencing a partial using a lambda can use the variable type
1295 # after the lambda has been freed and does not leak memory.
1296 var lines =<< trim END
1297 vim9script
1298
1299 func MyomniFunc1(val, findstart, base)
1300 return a:findstart ? 0 : []
1301 endfunc
1302
1303 var Lambda = (a, b) => MyomniFunc1(19, a, b)
1304 &omnifunc = Lambda
1305 Lambda = (a, b) => MyomniFunc1(20, a, b)
1306 &omnifunc = string(Lambda)
1307 Lambda = (a, b) => strlen(a)
1308 END
1309 CheckScriptSuccess(lines)
1310enddef
1311
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001312" Default arg and varargs
1313def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001314 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001315 for s in rest
1316 res ..= ',' .. s
1317 endfor
1318 return res
1319enddef
1320
1321def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001322 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001323 MyDefVarargs('one')->assert_equal('one,foo')
1324 MyDefVarargs('one', 'two')->assert_equal('one,two')
1325 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001326 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001327 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001328 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001329 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001330
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001331 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001332 vim9script
1333 def Func(...l: list<string>)
1334 echo l
1335 enddef
1336 Func('a', 'b', 'c')
1337 END
1338 CheckScriptSuccess(lines)
1339
1340 lines =<< trim END
1341 vim9script
1342 def Func(...l: list<string>)
1343 echo l
1344 enddef
1345 Func()
1346 END
1347 CheckScriptSuccess(lines)
1348
1349 lines =<< trim END
1350 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001351 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001352 echo l
1353 enddef
1354 Func(0)
1355 END
1356 CheckScriptSuccess(lines)
1357
1358 lines =<< trim END
1359 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001360 def Func(...l: any)
1361 echo l
1362 enddef
1363 Func(0)
1364 END
1365 CheckScriptFailure(lines, 'E1180:', 2)
1366
1367 lines =<< trim END
1368 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001369 def Func(..._l: list<string>)
1370 echo _l
1371 enddef
1372 Func('a', 'b', 'c')
1373 END
1374 CheckScriptSuccess(lines)
1375
1376 lines =<< trim END
1377 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001378 def Func(...l: list<string>)
1379 echo l
1380 enddef
1381 Func(1, 2, 3)
1382 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001383 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001384
1385 lines =<< trim END
1386 vim9script
1387 def Func(...l: list<string>)
1388 echo l
1389 enddef
1390 Func('a', 9)
1391 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001392 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001393
1394 lines =<< trim END
1395 vim9script
1396 def Func(...l: list<string>)
1397 echo l
1398 enddef
1399 Func(1, 'a')
1400 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001401 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001402
1403 lines =<< trim END
1404 vim9script
1405 def Func( # some comment
1406 ...l = []
1407 )
1408 echo l
1409 enddef
1410 END
1411 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001412
1413 lines =<< trim END
1414 vim9script
1415 def DoIt()
1416 g:Later('')
1417 enddef
1418 defcompile
1419 def g:Later(...l: list<number>)
1420 enddef
1421 DoIt()
1422 END
1423 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001424enddef
1425
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001426let s:value = ''
1427
1428def FuncOneDefArg(opt = 'text')
1429 s:value = opt
1430enddef
1431
1432def FuncTwoDefArg(nr = 123, opt = 'text'): string
1433 return nr .. opt
1434enddef
1435
1436def FuncVarargs(...arg: list<string>): string
1437 return join(arg, ',')
1438enddef
1439
1440def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001441 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001442 RefDefArg = FuncOneDefArg
1443 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001444 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001445 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001446 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001447
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001448 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001449 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001450 RefDef2Arg()->assert_equal('123text')
1451 RefDef2Arg(99)->assert_equal('99text')
1452 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001453
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001454 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1455 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001456
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001457 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001458 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001459 RefVarargs()->assert_equal('')
1460 RefVarargs('one')->assert_equal('one')
1461 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001462
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001463 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1464 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001465enddef
1466
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001467" Only varargs
1468def MyVarargsOnly(...args: list<string>): string
1469 return join(args, ',')
1470enddef
1471
1472def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001473 MyVarargsOnly()->assert_equal('')
1474 MyVarargsOnly('one')->assert_equal('one')
1475 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001476 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1477 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001478enddef
1479
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001480def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001481 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001482 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001483 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001484enddef
1485
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001486def DictArg(arg: dict<string>)
1487 arg['key'] = 'value'
1488enddef
1489
1490def ListArg(arg: list<string>)
1491 arg[0] = 'value'
1492enddef
1493
1494def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001495 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001496 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001497 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001498 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001499 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001500 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001501 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001502
Bram Moolenaard2c61702020-09-06 15:58:36 +02001503 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001504 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001505enddef
1506
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001507" These argument names are reserved in legacy functions.
1508def WithReservedNames(firstline: string, lastline: string): string
1509 return firstline .. lastline
1510enddef
1511
1512def Test_argument_names()
1513 assert_equal('OK', WithReservedNames('O', 'K'))
1514enddef
1515
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001516def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001517 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001518 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001519enddef
1520
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001521func DefinedLater(arg)
1522 return a:arg
1523endfunc
1524
1525def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001526 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001527 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001528 assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001529
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001530 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001531 vim9script
1532 def RetNumber(): number
1533 return 123
1534 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001535 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001536 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001537 END
1538 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001539
1540 lines =<< trim END
1541 vim9script
1542 def RetNumber(): number
1543 return 123
1544 enddef
1545 def Bar(F: func: number): number
1546 return F()
1547 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001548 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001549 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001550 END
1551 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001552
1553 lines =<< trim END
1554 vim9script
1555 def UseNumber(nr: number)
1556 echo nr
1557 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001558 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001559 Funcref(123)
1560 END
1561 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001562
1563 lines =<< trim END
1564 vim9script
1565 def UseNumber(nr: number)
1566 echo nr
1567 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001568 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001569 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001570 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001571
1572 lines =<< trim END
1573 vim9script
1574 def EchoNr(nr = 34)
1575 g:echo = nr
1576 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001577 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001578 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001579 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001580 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001581 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001582 END
1583 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001584
1585 lines =<< trim END
1586 vim9script
1587 def EchoList(...l: list<number>)
1588 g:echo = l
1589 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001590 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001591 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001592 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001593 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001594 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001595 END
1596 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001597
1598 lines =<< trim END
1599 vim9script
1600 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1601 g:optarg = opt
1602 g:listarg = l
1603 return nr
1604 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001605 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001606 Funcref(10)->assert_equal(10)
1607 g:optarg->assert_equal(12)
1608 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001609
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001610 Funcref(11, 22)->assert_equal(11)
1611 g:optarg->assert_equal(22)
1612 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001613
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001614 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1615 g:optarg->assert_equal(18)
1616 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001617 END
1618 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001619enddef
1620
1621let SomeFunc = function('len')
1622let NotAFunc = 'text'
1623
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001624def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001625 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001626 var Ref1: func(bool): string
1627 var Ref2: func(bool): number
1628 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001629 Ref3 = g:cond ? Ref1 : Ref2
1630
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001631 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001632 var Refa1: func(bool): number
1633 var Refa2: func(bool, number): number
1634 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001635 Refa3 = g:cond ? Refa1 : Refa2
1636
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001637 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001638 var Refb1: func(bool, string): number
1639 var Refb2: func(string, number): number
1640 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001641 Refb3 = g:cond ? Refb1 : Refb2
1642enddef
1643
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001644def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001645 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001646enddef
1647
1648def DefinedEvenLater(arg: string): string
1649 return arg
1650enddef
1651
1652def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001653 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001654 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001655enddef
1656
1657def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001658 CheckScriptFailure([
1659 'def Func(): number',
1660 'return "a"',
1661 'enddef',
1662 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001663 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001664 CheckScriptFailure([
1665 'def Func(): string',
1666 'return 1',
1667 'enddef',
1668 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001669 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001670 CheckScriptFailure([
1671 'def Func(): void',
1672 'return "a"',
1673 'enddef',
1674 'defcompile'],
1675 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001676 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001677 CheckScriptFailure([
1678 'def Func()',
1679 'return "a"',
1680 'enddef',
1681 'defcompile'],
1682 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001683 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001684
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001685 CheckScriptFailure([
1686 'def Func(): number',
1687 'return',
1688 'enddef',
1689 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001690 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001691
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001692 CheckScriptFailure([
1693 'def Func():number',
1694 'return 123',
1695 'enddef',
1696 'defcompile'], 'E1069:')
1697 delfunc! g:Func
1698
1699 CheckScriptFailure([
1700 'def Func() :number',
1701 'return 123',
1702 'enddef',
1703 'defcompile'], 'E1059:')
1704 delfunc! g:Func
1705
1706 CheckScriptFailure([
1707 'def Func() : number',
1708 'return 123',
1709 'enddef',
1710 'defcompile'], 'E1059:')
1711 delfunc! g:Func
1712
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001713 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001714 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001715 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001716 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001717 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001718 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001719
1720 CheckScriptFailure([
1721 'vim9script',
1722 'def FuncB()',
1723 ' return 123',
1724 'enddef',
1725 'def FuncA()',
1726 ' FuncB()',
1727 'enddef',
1728 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001729enddef
1730
1731def Test_arg_type_wrong()
1732 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001733 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001734 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001735 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001736 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1737 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001738enddef
1739
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001740def Test_white_space_before_comma()
1741 var lines =<< trim END
1742 vim9script
1743 def Func(a: number , b: number)
1744 enddef
1745 END
1746 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001747 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001748enddef
1749
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001750def Test_white_space_after_comma()
1751 var lines =<< trim END
1752 vim9script
1753 def Func(a: number,b: number)
1754 enddef
1755 END
1756 CheckScriptFailure(lines, 'E1069:')
1757
1758 # OK in legacy function
1759 lines =<< trim END
1760 vim9script
1761 func Func(a,b)
1762 endfunc
1763 END
1764 CheckScriptSuccess(lines)
1765enddef
1766
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001767def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001768 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001769 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001770 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001771 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001772 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001773 enddef
1774 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001775 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001776
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001777 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001778 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001779 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001780
Bram Moolenaar67979662020-06-20 22:50:47 +02001781 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001782 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001783 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001784
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001785 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001786 def ListFunc(arg: list<number>)
1787 listvar = arg
1788 enddef
1789 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001790 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001792 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001793 def DictFunc(arg: dict<number>)
1794 dictvar = arg
1795 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001796 {a: 1, b: 2}->DictFunc()
1797 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001798 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001799 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001800 enddef
1801 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001802 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001803
Bram Moolenaare0de1712020-12-02 17:36:54 +01001804 {a: 3, b: 4}->DictFunc()
1805 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001806
1807 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001808 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001809 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001810 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001811
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001812 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001813 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001814 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001815 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001816 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001817 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001818
1819 def UseString()
1820 'xyork'->MyFunc()
1821 enddef
1822 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001823 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001824
Bram Moolenaar10409562020-07-29 20:00:38 +02001825 def UseString2()
1826 "knife"->MyFunc()
1827 enddef
1828 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001829 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001830
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001831 # prepending a colon makes it a mark
1832 new
1833 setline(1, ['aaa', 'bbb', 'ccc'])
1834 normal! 3Gmt1G
1835 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001836 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001837 bwipe!
1838
Bram Moolenaare6b53242020-07-01 17:28:33 +02001839 MyFunc(
1840 'continued'
1841 )
1842 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001843 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001844 )
1845
1846 call MyFunc(
1847 'more'
1848 ..
1849 'lines'
1850 )
1851 assert_equal(
1852 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001853 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001854 END
1855 writefile(lines, 'Xcall.vim')
1856 source Xcall.vim
1857 delete('Xcall.vim')
1858enddef
1859
1860def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001861 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001862 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001863 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001864 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001865 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001867 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001868 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001869 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870enddef
1871
Bram Moolenaar65b95452020-07-19 14:03:09 +02001872def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001873 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001874 vim9script
1875 def MyFunc(arg: string)
1876 echo arg
1877 enddef
1878 MyFunc(1234)
1879 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001880 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001881enddef
1882
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001883def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001884 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001885 vim9script
1886 const var = ''
1887 def MyFunc(arg: string)
1888 var = 'asdf'
1889 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001890 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001891 END
1892 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001893 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001894 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001895
1896 lines =<< trim END
1897 const g:Aconst = 77
1898 def Change()
1899 # comment
1900 g:Aconst = 99
1901 enddef
1902 call Change()
1903 unlet g:Aconst
1904 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001905 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001906enddef
1907
1908" Test that inside :function a Python function can be defined, :def is not
1909" recognized.
1910func Test_function_python()
1911 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001912 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001913 execute py "<< EOF"
1914def do_something():
1915 return 1
1916EOF
1917endfunc
1918
1919def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001920 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001921 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001922 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001923 echo 'hello'
1924 enddef
1925
1926 def CallGoneSoon()
1927 GoneSoon()
1928 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001929 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001930
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001931 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001932 CallGoneSoon()
1933 END
1934 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001935 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1936 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001937
1938 delete('XToDelFunc')
1939enddef
1940
Bram Moolenaar7509ad82021-12-14 18:14:37 +00001941func Test_free_dict_while_in_funcstack()
1942 " relies on the sleep command
1943 CheckUnix
1944 call Run_Test_free_dict_while_in_funcstack()
1945endfunc
1946
1947def Run_Test_free_dict_while_in_funcstack()
1948
1949 # this was freeing the TermRun() default argument dictionary while it was
1950 # still referenced in a funcstack_T
1951 var lines =<< trim END
1952 vim9script
1953
1954 &updatetime = 400
1955 def TermRun(_ = {})
1956 def Post()
1957 enddef
1958 def Exec()
1959 term_start('sleep 1', {
1960 term_finish: 'close',
1961 exit_cb: (_, _) => Post(),
1962 })
1963 enddef
1964 Exec()
1965 enddef
1966 nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
1967 timer_start(100, (_) => feedkeys("\<F4>"))
1968 timer_start(1000, (_) => feedkeys("\<F4>"))
1969 sleep 1500m
1970 END
1971 CheckScriptSuccess(lines)
1972 nunmap <F4>
1973 set updatetime&
1974enddef
1975
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001976def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001977 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001978 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001979 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001980 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001981 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001982 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001983 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001984 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001985 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001986
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001987 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001988 g:Func1()->assert_equal('Func1')
1989 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001990
1991 delfunc! Func0
1992 delfunc! Func1
1993 delfunc! Func2
1994enddef
1995
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001996def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001997 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001998 vim9script
1999 func Func(arg)
2000 echo a:arg
2001 endfunc
2002 Func('text')
2003 END
2004 writefile(lines, 'XVim9Func')
2005 so XVim9Func
2006
2007 delete('XVim9Func')
2008enddef
2009
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002010let s:funcResult = 0
2011
2012def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02002013 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002014enddef
2015
2016def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002017 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002018 return 1234
2019enddef
2020
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002021def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02002022 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002023 return 'text'
2024enddef
2025
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002026def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002027 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002028enddef
2029
2030def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02002031 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002032 return arg
2033enddef
2034
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002035def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02002036 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002037enddef
2038
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002039def FuncOneArgRetString(arg: string): string
2040 return arg
2041enddef
2042
Bram Moolenaar89228602020-04-05 22:14:54 +02002043def FuncOneArgRetAny(arg: any): any
2044 return arg
2045enddef
2046
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002047def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002048 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02002049 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002050 Ref1 = FuncNoArgNoRet
2051 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002052 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002053
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002054 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02002055 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002056 Ref2 = FuncNoArgNoRet
2057 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002058 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002059
Bram Moolenaar53900992020-08-22 19:02:02 +02002060 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002061 Ref2 = FuncOneArgNoRet
2062 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002063 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002064
Bram Moolenaar53900992020-08-22 19:02:02 +02002065 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002066 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002067 Ref2()->assert_equal(1234)
2068 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02002069
Bram Moolenaar53900992020-08-22 19:02:02 +02002070 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02002071 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002072 Ref2(13)->assert_equal(13)
2073 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002074enddef
2075
Bram Moolenaar9978d472020-07-05 16:01:56 +02002076def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002077 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02002078 for n in repeat([1], 3)
2079 res += n
2080 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002081 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02002082
2083 res = 0
2084 for n in add([1, 2], 3)
2085 res += n
2086 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002087 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02002088enddef
2089
Bram Moolenaar846178a2020-07-05 17:04:13 +02002090def Test_argv_return_type()
2091 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002092 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02002093 for name in argv()
2094 res ..= name
2095 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002096 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02002097enddef
2098
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002099def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002100 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002101 RefVoid = FuncNoArgNoRet
2102 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002103 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
2104 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002105
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002106 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002107 RefAny = FuncNoArgRetNumber
2108 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002109 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
2110 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002111
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002112 var RefAnyNoArgs: func: any = RefAny
2113
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002114 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002115 RefNr = FuncNoArgRetNumber
2116 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002117 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
2118 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002119
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002120 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002121 RefStr = FuncNoArgRetString
2122 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002123 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
2124 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002125enddef
2126
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002127def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002128 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002129
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002130 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
2131 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
2132 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
2133 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
2134 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
2135 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 +02002136
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002137 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
2138 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
2139 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:')
2140 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002141enddef
2142
Bram Moolenaar89228602020-04-05 22:14:54 +02002143def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002144 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02002145 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002146 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02002147
2148 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002149 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02002150
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002151 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02002152 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002153 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02002154
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002155 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02002156enddef
2157
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002158def Test_func_common_type()
2159 def FuncOne(n: number): number
2160 return n
2161 enddef
2162 def FuncTwo(s: string): number
2163 return len(s)
2164 enddef
2165 def FuncThree(n: number, s: string): number
2166 return n + len(s)
2167 enddef
2168 var list = [FuncOne, FuncTwo, FuncThree]
2169 assert_equal(8, list[0](8))
2170 assert_equal(4, list[1]('word'))
2171 assert_equal(7, list[2](3, 'word'))
2172enddef
2173
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002174def MultiLine(
2175 arg1: string,
2176 arg2 = 1234,
2177 ...rest: list<string>
2178 ): string
2179 return arg1 .. arg2 .. join(rest, '-')
2180enddef
2181
Bram Moolenaar2c330432020-04-13 14:41:35 +02002182def MultiLineComment(
2183 arg1: string, # comment
2184 arg2 = 1234, # comment
2185 ...rest: list<string> # comment
2186 ): string # comment
2187 return arg1 .. arg2 .. join(rest, '-')
2188enddef
2189
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002190def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002191 MultiLine('text')->assert_equal('text1234')
2192 MultiLine('text', 777)->assert_equal('text777')
2193 MultiLine('text', 777, 'one')->assert_equal('text777one')
2194 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002195enddef
2196
Bram Moolenaar23e03252020-04-12 22:22:31 +02002197func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002198 call MultiLine('text')->assert_equal('text1234')
2199 call MultiLine('text', 777)->assert_equal('text777')
2200 call MultiLine('text', 777, 'one')->assert_equal('text777one')
2201 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02002202endfunc
2203
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002204
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002205" When using CheckScriptFailure() for the below test, E1010 is generated instead
2206" of E1056.
2207func Test_E1056_1059()
2208 let caught_1056 = 0
2209 try
2210 def F():
2211 return 1
2212 enddef
2213 catch /E1056:/
2214 let caught_1056 = 1
2215 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002216 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002217
2218 let caught_1059 = 0
2219 try
2220 def F5(items : list)
2221 echo 'a'
2222 enddef
2223 catch /E1059:/
2224 let caught_1059 = 1
2225 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002226 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002227endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002228
Bram Moolenaar015f4262020-05-05 21:25:22 +02002229func DelMe()
2230 echo 'DelMe'
2231endfunc
2232
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002233def Test_error_reporting()
2234 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002235 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002236 " comment
2237 def Func()
2238 # comment
2239 # comment
2240 invalid
2241 enddef
2242 defcompile
2243 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002244 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002245 try
2246 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002247 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002248 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002249 v:exception->assert_match('Invalid command: invalid')
2250 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002251 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002252 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002253
2254 # comment lines after the start of the function
2255 lines =<< trim END
2256 " comment
2257 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002258 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002259 # comment
2260 # comment
2261 invalid
2262 enddef
2263 defcompile
2264 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002265 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002266 try
2267 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002268 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002269 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002270 v:exception->assert_match('Invalid command: invalid')
2271 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002272 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002273 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002274
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002275 lines =<< trim END
2276 vim9script
2277 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002278 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002279 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002280 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002281 enddef
2282 defcompile
2283 Func()
2284 END
Bram Moolenaar08052222020-09-14 17:04:31 +02002285 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002286 try
2287 source Xdef
2288 assert_report('should have failed')
2289 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002290 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002291 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002292 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002293
Bram Moolenaar08052222020-09-14 17:04:31 +02002294 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002295enddef
2296
Bram Moolenaar015f4262020-05-05 21:25:22 +02002297def Test_deleted_function()
2298 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002299 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002300 'delfunc g:DelMe',
2301 'echo RefMe()'], 'E117:')
2302enddef
2303
2304def Test_unknown_function()
2305 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002306 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002307 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002308enddef
2309
Bram Moolenaar328eac22021-01-07 19:23:08 +01002310def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002311 return Ref('more')
2312enddef
2313
2314def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002315 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002316 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002317enddef
2318
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002319def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002320 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002321 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002322enddef
2323
2324def Test_closure_ref_after_return()
2325 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002326 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002327 unlet g:Ref
2328enddef
2329
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002330def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002331 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002332 g:Extend = (s) => local->add(s)
2333 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002334enddef
2335
2336def Test_closure_two_refs()
2337 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002338 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002339 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002340 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002341 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002342 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002343
2344 unlet g:Extend
2345 unlet g:Read
2346enddef
2347
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002348def ReadRef(Ref: func(): list<string>): string
2349 return join(Ref(), ' ')
2350enddef
2351
Bram Moolenaar5e654232020-09-16 15:22:00 +02002352def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002353 Ref(add)
2354enddef
2355
2356def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002357 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002358 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002359 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002360 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002361 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002362 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002363
2364 unlet g:Extend
2365 unlet g:Read
2366enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002367
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002368def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002369 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002370 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002371enddef
2372
2373def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002374 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002375 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002376enddef
2377
2378def Test_closure_using_argument()
2379 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002380 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002381
2382 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002383 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002384
2385 unlet g:UseArg
2386 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002387
2388 var lines =<< trim END
2389 vim9script
2390 def Test(Fun: func(number): number): list<number>
2391 return map([1, 2, 3], (_, i) => Fun(i))
2392 enddef
2393 def Inc(nr: number): number
2394 return nr + 2
2395 enddef
2396 assert_equal([3, 4, 5], Test(Inc))
2397 END
2398 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002399enddef
2400
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002401def MakeGetAndAppendRefs()
2402 var local = 'a'
2403
2404 def Append(arg: string)
2405 local ..= arg
2406 enddef
2407 g:Append = Append
2408
2409 def Get(): string
2410 return local
2411 enddef
2412 g:Get = Get
2413enddef
2414
2415def Test_closure_append_get()
2416 MakeGetAndAppendRefs()
2417 g:Get()->assert_equal('a')
2418 g:Append('-b')
2419 g:Get()->assert_equal('a-b')
2420 g:Append('-c')
2421 g:Get()->assert_equal('a-b-c')
2422
2423 unlet g:Append
2424 unlet g:Get
2425enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002426
Bram Moolenaar04b12692020-05-04 23:24:44 +02002427def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002428 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002429 def Closure(arg: string): string
2430 return local .. arg
2431 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002432 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002433enddef
2434
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002435func GetResult(Ref)
2436 return a:Ref('some')
2437endfunc
2438
2439def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002440 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002441 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002442 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002443enddef
2444
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002445def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002446 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002447 vim9script
2448 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002449 var name = 0
2450 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002451 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002452 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002453 enddef
2454 Func()
2455 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002456 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002457enddef
2458
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002459def Test_nested_closure_used()
2460 var lines =<< trim END
2461 vim9script
2462 def Func()
2463 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002464 var Closure = () => x
2465 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002466 enddef
2467 Func()
2468 assert_equal('hello', g:Myclosure())
2469 END
2470 CheckScriptSuccess(lines)
2471enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002472
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002473def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002474 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002475 vim9script
2476 def FuncA()
2477 FuncB(0)
2478 enddef
2479 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002480 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002481 enddef
2482 FuncA()
2483 END
2484 CheckScriptFailure(lines, 'E1012:')
2485enddef
2486
Bram Moolenaarf112f302020-12-20 17:47:52 +01002487def Test_global_closure()
2488 var lines =<< trim END
2489 vim9script
2490 def ReverseEveryNLines(n: number, line1: number, line2: number)
2491 var mods = 'sil keepj keepp lockm '
2492 var range = ':' .. line1 .. ',' .. line2
2493 def g:Offset(): number
2494 var offset = (line('.') - line1 + 1) % n
2495 return offset != 0 ? offset : n
2496 enddef
2497 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2498 enddef
2499
2500 new
2501 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2502 ReverseEveryNLines(3, 1, 9)
2503 END
2504 CheckScriptSuccess(lines)
2505 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2506 assert_equal(expected, getline(1, 9))
2507 bwipe!
2508enddef
2509
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002510def Test_global_closure_called_directly()
2511 var lines =<< trim END
2512 vim9script
2513 def Outer()
2514 var x = 1
2515 def g:Inner()
2516 var y = x
2517 x += 1
2518 assert_equal(1, y)
2519 enddef
2520 g:Inner()
2521 assert_equal(2, x)
2522 enddef
2523 Outer()
2524 END
2525 CheckScriptSuccess(lines)
2526 delfunc g:Inner
2527enddef
2528
Bram Moolenaar69c76172021-12-02 16:38:52 +00002529def Test_closure_called_from_legacy()
2530 var lines =<< trim END
2531 vim9script
2532 def Func()
2533 var outer = 'foo'
2534 var F = () => {
2535 outer = 'bar'
2536 }
2537 execute printf('call %s()', string(F))
2538 enddef
2539 Func()
2540 END
2541 CheckScriptFailure(lines, 'E1248')
2542enddef
2543
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002544def Test_failure_in_called_function()
2545 # this was using the frame index as the return value
2546 var lines =<< trim END
2547 vim9script
2548 au TerminalWinOpen * eval [][0]
2549 def PopupTerm(a: any)
2550 # make sure typvals on stack are string
2551 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2552 FireEvent()
2553 enddef
2554 def FireEvent()
2555 do TerminalWinOpen
2556 enddef
2557 # use try/catch to make eval fail
2558 try
2559 call PopupTerm(0)
2560 catch
2561 endtry
2562 au! TerminalWinOpen
2563 END
2564 CheckScriptSuccess(lines)
2565enddef
2566
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002567def Test_nested_lambda()
2568 var lines =<< trim END
2569 vim9script
2570 def Func()
2571 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002572 var Lambda1 = () => 7
2573 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002574 var res = Lambda2()
2575 assert_equal([7, 4], res)
2576 enddef
2577 Func()
2578 END
2579 CheckScriptSuccess(lines)
2580enddef
2581
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002582def Test_double_nested_lambda()
2583 var lines =<< trim END
2584 vim9script
2585 def F(head: string): func(string): func(string): string
2586 return (sep: string): func(string): string => ((tail: string): string => {
2587 return head .. sep .. tail
2588 })
2589 enddef
2590 assert_equal('hello-there', F('hello')('-')('there'))
2591 END
2592 CheckScriptSuccess(lines)
2593enddef
2594
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002595def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002596 var lines =<< trim END
2597 vim9script
2598 def F(text: string): func(string): func(string): string
2599 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002600 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002601 })
2602 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002603 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002604 END
2605 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002606
2607 lines =<< trim END
2608 vim9script
2609 echo range(4)->mapnew((_, v) => {
2610 return range(v) ->mapnew((_, s) => {
2611 return string(s)
2612 })
2613 })
2614 END
2615 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002616
2617 lines =<< trim END
2618 vim9script
2619
2620 def s:func()
2621 range(10)
2622 ->mapnew((_, _) => ({
2623 key: range(10)->mapnew((_, _) => {
2624 return ' '
2625 }),
2626 }))
2627 enddef
2628
2629 defcomp
2630 END
2631 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002632enddef
2633
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002634def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002635 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002636 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002637enddef
2638
2639def Test_lambda_arg_shadows_func()
2640 assert_equal([42], Shadowed())
2641enddef
2642
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002643def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002644 var path: string = empty(dir)
2645 \ ? 'empty'
2646 \ : 'full'
2647 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002648enddef
2649
2650def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002651 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002652enddef
2653
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002654def Test_script_var_in_lambda()
2655 var lines =<< trim END
2656 vim9script
2657 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002658 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002659 END
2660 CheckScriptSuccess(lines)
2661enddef
2662
Bram Moolenaar5e654232020-09-16 15:22:00 +02002663def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002664 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002665 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002666 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002667 ->reverse()
2668 return x
2669enddef
2670
2671def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002672 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002673
2674 var lines =<< trim END
2675 vim9script
2676 var res = [{n: 1, m: 2, s: 'xxx'}]
2677 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2678 v.n,
2679 v.m,
2680 substitute(v.s, '.*', 'yyy', '')
2681 ))
2682 assert_equal(['1:2:yyy'], res)
2683 END
2684 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002685enddef
2686
Bram Moolenaarb6571982021-01-08 22:24:19 +01002687def Test_list_lambda()
2688 timer_start(1000, (_) => 0)
2689 var body = execute(timer_info()[0].callback
2690 ->string()
2691 ->substitute("('", ' ', '')
2692 ->substitute("')", '', '')
2693 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002694 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002695enddef
2696
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002697def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002698 var lines =<< trim END
2699 vim9script
2700 var flist: list<func>
2701 for i in range(10)
2702 var inloop = i
2703 flist[i] = () => inloop
2704 endfor
2705 END
2706 CheckScriptSuccess(lines)
2707
2708 lines =<< trim END
2709 vim9script
2710 if true
2711 var outloop = 5
2712 var flist: list<func>
2713 for i in range(10)
2714 flist[i] = () => outloop
2715 endfor
2716 endif
2717 END
2718 CheckScriptSuccess(lines)
2719
2720 lines =<< trim END
2721 vim9script
2722 if true
2723 var outloop = 5
2724 endif
2725 var flist: list<func>
2726 for i in range(10)
2727 flist[i] = () => outloop
2728 endfor
2729 END
2730 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002731
2732 lines =<< trim END
2733 vim9script
2734 for i in range(10)
2735 var Ref = () => 0
2736 endfor
2737 assert_equal(0, ((i) => 0)(0))
2738 END
2739 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002740enddef
2741
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002742def Test_legacy_lambda()
2743 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002744
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002745 var lines =<< trim END
2746 echo {x -> 'hello ' .. x}('foo')
2747 END
2748 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002749
2750 lines =<< trim END
2751 vim9script
2752 def Func()
2753 echo (() => 'no error')()
2754 enddef
2755 legacy call s:Func()
2756 END
2757 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002758enddef
2759
Bram Moolenaarce024c32021-06-26 13:00:49 +02002760def Test_legacy()
2761 var lines =<< trim END
2762 vim9script
2763 func g:LegacyFunction()
2764 let g:legacyvar = 1
2765 endfunc
2766 def Testit()
2767 legacy call g:LegacyFunction()
2768 enddef
2769 Testit()
2770 assert_equal(1, g:legacyvar)
2771 unlet g:legacyvar
2772 delfunc g:LegacyFunction
2773 END
2774 CheckScriptSuccess(lines)
2775enddef
2776
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002777def Test_legacy_errors()
2778 for cmd in ['if', 'elseif', 'else', 'endif',
2779 'for', 'endfor', 'continue', 'break',
2780 'while', 'endwhile',
2781 'try', 'catch', 'finally', 'endtry']
2782 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2783 endfor
2784enddef
2785
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002786def Test_call_legacy_with_dict()
2787 var lines =<< trim END
2788 vim9script
2789 func Legacy() dict
2790 let g:result = self.value
2791 endfunc
2792 def TestDirect()
2793 var d = {value: 'yes', func: Legacy}
2794 d.func()
2795 enddef
2796 TestDirect()
2797 assert_equal('yes', g:result)
2798 unlet g:result
2799
2800 def TestIndirect()
2801 var d = {value: 'foo', func: Legacy}
2802 var Fi = d.func
2803 Fi()
2804 enddef
2805 TestIndirect()
2806 assert_equal('foo', g:result)
2807 unlet g:result
2808
2809 var d = {value: 'bar', func: Legacy}
2810 d.func()
2811 assert_equal('bar', g:result)
2812 unlet g:result
2813 END
2814 CheckScriptSuccess(lines)
2815enddef
2816
Bram Moolenaarab360522021-01-10 14:02:28 +01002817def DoFilterThis(a: string): list<string>
2818 # closure nested inside another closure using argument
2819 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2820 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2821enddef
2822
2823def Test_nested_closure_using_argument()
2824 assert_equal(['x', 'x2'], DoFilterThis('x'))
2825enddef
2826
Bram Moolenaar0186e582021-01-10 18:33:11 +01002827def Test_triple_nested_closure()
2828 var what = 'x'
2829 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2830 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2831 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2832enddef
2833
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002834func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002835 CheckScreendump
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002836 call Run_Test_silent_echo()
2837endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002838
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002839def Run_Test_silent_echo()
2840 var lines =<< trim END
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002841 vim9script
2842 def EchoNothing()
2843 silent echo ''
2844 enddef
2845 defcompile
2846 END
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002847 writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002848
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002849 # Check that the balloon shows up after a mouse move
2850 var buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
2851 term_sendkeys(buf, ":abc")
2852 VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002853
Bram Moolenaar3b309f12021-12-13 18:19:55 +00002854 # clean up
2855 StopVimInTerminal(buf)
2856 delete('XTest_silent_echo')
2857enddef
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002858
Bram Moolenaar171fb922020-10-28 16:54:47 +01002859def SilentlyError()
2860 execute('silent! invalid')
2861 g:did_it = 'yes'
2862enddef
2863
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002864func UserError()
2865 silent! invalid
2866endfunc
2867
2868def SilentlyUserError()
2869 UserError()
2870 g:did_it = 'yes'
2871enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002872
2873" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002874func Test_ignore_silent_error()
2875 let g:did_it = 'no'
2876 call SilentlyError()
2877 call assert_equal('yes', g:did_it)
2878
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002879 let g:did_it = 'no'
2880 call SilentlyUserError()
2881 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002882
2883 unlet g:did_it
2884endfunc
2885
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002886def Test_ignore_silent_error_in_filter()
2887 var lines =<< trim END
2888 vim9script
2889 def Filter(winid: number, key: string): bool
2890 if key == 'o'
2891 silent! eval [][0]
2892 return true
2893 endif
2894 return popup_filter_menu(winid, key)
2895 enddef
2896
Bram Moolenaare0de1712020-12-02 17:36:54 +01002897 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002898 feedkeys("o\r", 'xnt')
2899 END
2900 CheckScriptSuccess(lines)
2901enddef
2902
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002903def Fibonacci(n: number): number
2904 if n < 2
2905 return n
2906 else
2907 return Fibonacci(n - 1) + Fibonacci(n - 2)
2908 endif
2909enddef
2910
Bram Moolenaar985116a2020-07-12 17:31:09 +02002911def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002912 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002913enddef
2914
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002915def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002916 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002917 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002918 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002919 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002920 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002921enddef
2922
2923def Test_closure_in_map()
2924 mkdir('XclosureDir/tdir', 'p')
2925 writefile(['111'], 'XclosureDir/file1')
2926 writefile(['222'], 'XclosureDir/file2')
2927 writefile(['333'], 'XclosureDir/tdir/file3')
2928
Bram Moolenaare0de1712020-12-02 17:36:54 +01002929 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002930
2931 delete('XclosureDir', 'rf')
2932enddef
2933
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002934def Test_invalid_function_name()
2935 var lines =<< trim END
2936 vim9script
2937 def s: list<string>
2938 END
2939 CheckScriptFailure(lines, 'E129:')
2940
2941 lines =<< trim END
2942 vim9script
2943 def g: list<string>
2944 END
2945 CheckScriptFailure(lines, 'E129:')
2946
2947 lines =<< trim END
2948 vim9script
2949 def <SID>: list<string>
2950 END
2951 CheckScriptFailure(lines, 'E884:')
2952
2953 lines =<< trim END
2954 vim9script
2955 def F list<string>
2956 END
2957 CheckScriptFailure(lines, 'E488:')
2958enddef
2959
Bram Moolenaara90afb92020-07-15 22:38:56 +02002960def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002961 var lines =<< trim END
2962 var Xsetlist: func
2963 Xsetlist = function('setloclist', [0])
2964 Xsetlist([], ' ', {title: 'test'})
2965 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002966
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002967 Xsetlist = function('setloclist', [0, [], ' '])
2968 Xsetlist({title: 'test'})
2969 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002970
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002971 Xsetlist = function('setqflist')
2972 Xsetlist([], ' ', {title: 'test'})
2973 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002974
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002975 Xsetlist = function('setqflist', [[], ' '])
2976 Xsetlist({title: 'test'})
2977 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002978
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002979 var Len: func: number = function('len', ['word'])
2980 assert_equal(4, Len())
2981
2982 var RepeatFunc = function('repeat', ['o'])
2983 assert_equal('ooooo', RepeatFunc(5))
2984 END
2985 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc66f6452021-08-19 21:08:30 +02002986
2987 lines =<< trim END
2988 vim9script
2989 def Foo(Parser: any)
2990 enddef
2991 var Expr: func(dict<any>): dict<any>
2992 const Call = Foo(Expr)
2993 END
2994 CheckScriptFailure(lines, 'E1235:')
Bram Moolenaara90afb92020-07-15 22:38:56 +02002995enddef
2996
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002997def Test_cmd_modifier()
2998 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002999 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003000enddef
3001
3002def Test_restore_modifiers()
3003 # check that when compiling a :def function command modifiers are not messed
3004 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02003005 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003006 vim9script
3007 set eventignore=
3008 autocmd QuickFixCmdPost * copen
3009 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003010 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003011 enddef
3012 func Func()
3013 noautocmd call s:AutocmdsDisabled()
3014 let g:ei_after = &eventignore
3015 endfunc
3016 Func()
3017 END
3018 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003019 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003020enddef
3021
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003022def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02003023 eval 1 + 2
3024 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003025 # call not on fourth line
3026 StackBot()
3027enddef
3028
3029def StackBot()
3030 # throw an error
3031 eval [][0]
3032enddef
3033
3034def Test_callstack_def()
3035 try
3036 StackTop()
3037 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02003038 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003039 endtry
3040enddef
3041
Bram Moolenaare8211a32020-10-09 22:04:29 +02003042" Re-using spot for variable used in block
3043def Test_block_scoped_var()
3044 var lines =<< trim END
3045 vim9script
3046 def Func()
3047 var x = ['a', 'b', 'c']
3048 if 1
3049 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003050 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02003051 endif
3052 var z = x
3053 assert_equal(['x', 'x', 'x'], z)
3054 enddef
3055 Func()
3056 END
3057 CheckScriptSuccess(lines)
3058enddef
3059
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003060def Test_reset_did_emsg()
3061 var lines =<< trim END
3062 @s = 'blah'
3063 au BufWinLeave * #
3064 def Func()
3065 var winid = popup_create('popup', {})
3066 exe '*s'
3067 popup_close(winid)
3068 enddef
3069 Func()
3070 END
3071 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003072 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003073enddef
3074
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003075def Test_did_emsg_reset()
3076 # executing an autocommand resets did_emsg, this should not result in a
3077 # builtin function considered failing
3078 var lines =<< trim END
3079 vim9script
3080 au BufWinLeave * #
3081 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02003082 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01003083 eval [][0]
3084 enddef
3085 nno <F3> <cmd>call <sid>Func()<cr>
3086 feedkeys("\<F3>\e", 'xt')
3087 END
3088 writefile(lines, 'XemsgReset')
3089 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
3090 delete('XemsgReset')
3091 nunmap <F3>
3092 au! BufWinLeave
3093enddef
3094
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003095def Test_abort_with_silent_call()
3096 var lines =<< trim END
3097 vim9script
3098 g:result = 'none'
3099 def Func()
3100 g:result += 3
3101 g:result = 'yes'
3102 enddef
3103 # error is silenced, but function aborts on error
3104 silent! Func()
3105 assert_equal('none', g:result)
3106 unlet g:result
3107 END
3108 CheckScriptSuccess(lines)
3109enddef
3110
Bram Moolenaarf665e972020-12-05 19:17:16 +01003111def Test_continues_with_silent_error()
3112 var lines =<< trim END
3113 vim9script
3114 g:result = 'none'
3115 def Func()
3116 silent! g:result += 3
3117 g:result = 'yes'
3118 enddef
3119 # error is silenced, function does not abort
3120 Func()
3121 assert_equal('yes', g:result)
3122 unlet g:result
3123 END
3124 CheckScriptSuccess(lines)
3125enddef
3126
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003127def Test_abort_even_with_silent()
3128 var lines =<< trim END
3129 vim9script
3130 g:result = 'none'
3131 def Func()
3132 eval {-> ''}() .. '' .. {}['X']
3133 g:result = 'yes'
3134 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01003135 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003136 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003137 unlet g:result
3138 END
3139 CheckScriptSuccess(lines)
3140enddef
3141
Bram Moolenaarf665e972020-12-05 19:17:16 +01003142def Test_cmdmod_silent_restored()
3143 var lines =<< trim END
3144 vim9script
3145 def Func()
3146 g:result = 'none'
3147 silent! g:result += 3
3148 g:result = 'none'
3149 g:result += 3
3150 enddef
3151 Func()
3152 END
3153 # can't use CheckScriptFailure, it ignores the :silent!
3154 var fname = 'Xdefsilent'
3155 writefile(lines, fname)
3156 var caught = 'no'
3157 try
3158 exe 'source ' .. fname
3159 catch /E1030:/
3160 caught = 'yes'
3161 assert_match('Func, line 4', v:throwpoint)
3162 endtry
3163 assert_equal('yes', caught)
3164 delete(fname)
3165enddef
3166
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003167def Test_cmdmod_silent_nested()
3168 var lines =<< trim END
3169 vim9script
3170 var result = ''
3171
3172 def Error()
3173 result ..= 'Eb'
3174 eval [][0]
3175 result ..= 'Ea'
3176 enddef
3177
3178 def Crash()
3179 result ..= 'Cb'
3180 sil! Error()
3181 result ..= 'Ca'
3182 enddef
3183
3184 Crash()
3185 assert_equal('CbEbEaCa', result)
3186 END
3187 CheckScriptSuccess(lines)
3188enddef
3189
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003190def Test_dict_member_with_silent()
3191 var lines =<< trim END
3192 vim9script
3193 g:result = 'none'
3194 var d: dict<any>
3195 def Func()
3196 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003197 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003198 catch
3199 endtry
3200 enddef
3201 silent! Func()
3202 assert_equal('0', g:result)
3203 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003204 END
3205 CheckScriptSuccess(lines)
3206enddef
3207
Bram Moolenaarf9041332021-01-21 19:41:16 +01003208def Test_skip_cmds_with_silent()
3209 var lines =<< trim END
3210 vim9script
3211
3212 def Func(b: bool)
3213 Crash()
3214 enddef
3215
3216 def Crash()
3217 sil! :/not found/d _
3218 sil! :/not found/put _
3219 enddef
3220
3221 Func(true)
3222 END
3223 CheckScriptSuccess(lines)
3224enddef
3225
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003226def Test_opfunc()
3227 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
3228 def g:Opfunc(_: any): string
3229 setline(1, 'ASDF')
3230 return ''
3231 enddef
3232 new
3233 setline(1, 'asdf')
3234 feedkeys("\<F3>$", 'x')
3235 assert_equal('ASDF', getline(1))
3236
3237 bwipe!
3238 nunmap <F3>
3239enddef
3240
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003241func Test_opfunc_error()
3242 CheckScreendump
3243 call Run_Test_opfunc_error()
3244endfunc
3245
3246def Run_Test_opfunc_error()
3247 # test that the error from Opfunc() is displayed right away
3248 var lines =<< trim END
3249 vim9script
3250
3251 def Opfunc(type: string)
3252 try
3253 eval [][0]
3254 catch /nothing/ # error not caught
3255 endtry
3256 enddef
3257 &operatorfunc = Opfunc
3258 nnoremap <expr> l <SID>L()
3259 def L(): string
3260 return 'l'
3261 enddef
3262 'x'->repeat(10)->setline(1)
3263 feedkeys('g@l', 'n')
3264 feedkeys('llll')
3265 END
3266 call writefile(lines, 'XTest_opfunc_error')
3267
3268 var buf = RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
Bram Moolenaar7c0fb802021-12-14 20:26:53 +00003269 WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
Bram Moolenaar23e72362021-12-16 21:07:35 +00003270 WaitForAssert(() => assert_match('E684: list index out of range: 0', term_getline(buf, 5)))
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003271
3272 # clean up
3273 StopVimInTerminal(buf)
3274 delete('XTest_opfunc_error')
3275enddef
3276
Bram Moolenaar077a4232020-12-22 18:33:27 +01003277" this was crashing on exit
3278def Test_nested_lambda_in_closure()
3279 var lines =<< trim END
3280 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003281 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003282 def Outer()
3283 def g:Inner()
3284 echo map([1, 2, 3], {_, v -> v + 1})
3285 enddef
3286 g:Inner()
3287 enddef
3288 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003289 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01003290 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003291 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01003292 return
3293 endif
3294 assert_equal(['Done'], readfile('XnestedDone'))
3295 delete('XnestedDone')
3296enddef
3297
Bram Moolenaar04947cc2021-03-06 19:26:46 +01003298def Test_check_func_arg_types()
3299 var lines =<< trim END
3300 vim9script
3301 def F1(x: string): string
3302 return x
3303 enddef
3304
3305 def F2(x: number): number
3306 return x + 1
3307 enddef
3308
3309 def G(g: func): dict<func>
3310 return {f: g}
3311 enddef
3312
3313 def H(d: dict<func>): string
3314 return d.f('a')
3315 enddef
3316 END
3317
3318 CheckScriptSuccess(lines + ['echo H(G(F1))'])
3319 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
3320enddef
3321
Bram Moolenaar6e48b842021-08-10 22:52:02 +02003322def Test_list_any_type_checked()
3323 var lines =<< trim END
3324 vim9script
3325 def Foo()
3326 --decl--
3327 Bar(l)
3328 enddef
3329 def Bar(ll: list<dict<any>>)
3330 enddef
3331 Foo()
3332 END
3333 lines[2] = 'var l: list<any>'
3334 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3335
3336 lines[2] = 'var l: list<any> = []'
3337 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<any>', 2)
3338
3339 lines[2] = 'var l: list<any> = [11]'
3340 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
3341enddef
3342
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003343def Test_compile_error()
3344 var lines =<< trim END
3345 def g:Broken()
3346 echo 'a' + {}
3347 enddef
3348 call g:Broken()
3349 END
3350 # First call: compilation error
3351 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
3352
3353 # Second call won't try compiling again
3354 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02003355 delfunc g:Broken
3356
3357 # No error when compiling with :silent!
3358 lines =<< trim END
3359 def g:Broken()
3360 echo 'a' + []
3361 enddef
3362 silent! defcompile
3363 END
3364 CheckScriptSuccess(lines)
3365
3366 # Calling the function won't try compiling again
3367 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
3368 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003369enddef
3370
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003371def Test_ignored_argument()
3372 var lines =<< trim END
3373 vim9script
3374 def Ignore(_, _): string
3375 return 'yes'
3376 enddef
3377 assert_equal('yes', Ignore(1, 2))
3378
3379 func Ok(_)
3380 return a:_
3381 endfunc
3382 assert_equal('ok', Ok('ok'))
3383
3384 func Oktoo()
3385 let _ = 'too'
3386 return _
3387 endfunc
3388 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02003389
3390 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003391 END
3392 CheckScriptSuccess(lines)
3393
3394 lines =<< trim END
3395 def Ignore(_: string): string
3396 return _
3397 enddef
3398 defcompile
3399 END
3400 CheckScriptFailure(lines, 'E1181:', 1)
3401
3402 lines =<< trim END
3403 var _ = 1
3404 END
3405 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003406
3407 lines =<< trim END
3408 var x = _
3409 END
3410 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003411enddef
3412
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003413def Test_too_many_arguments()
3414 var lines =<< trim END
3415 echo [0, 1, 2]->map(() => 123)
3416 END
3417 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3418
3419 lines =<< trim END
3420 echo [0, 1, 2]->map((_) => 123)
3421 END
3422 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3423enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003424
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003425def Test_closing_brace_at_start_of_line()
3426 var lines =<< trim END
3427 def Func()
3428 enddef
3429 Func(
3430 )
3431 END
3432 call CheckDefAndScriptSuccess(lines)
3433enddef
3434
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003435func CreateMydict()
3436 let g:mydict = {}
3437 func g:mydict.afunc()
3438 let g:result = self.key
3439 endfunc
3440endfunc
3441
3442def Test_numbered_function_reference()
3443 CreateMydict()
3444 var output = execute('legacy func g:mydict.afunc')
3445 var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3446 execute 'function(' .. funcName .. ', [], {key: 42})()'
3447 # check that the function still exists
3448 assert_equal(output, execute('legacy func g:mydict.afunc'))
3449 unlet g:mydict
3450enddef
3451
Bram Moolenaar20677332021-06-06 17:02:53 +02003452if has('python3')
3453 def Test_python3_heredoc()
3454 py3 << trim EOF
3455 import vim
3456 vim.vars['didit'] = 'yes'
3457 EOF
3458 assert_equal('yes', g:didit)
3459
3460 python3 << trim EOF
3461 import vim
3462 vim.vars['didit'] = 'again'
3463 EOF
3464 assert_equal('again', g:didit)
3465 enddef
3466endif
3467
3468" This messes up syntax highlight, keep near the end.
3469if has('lua')
3470 def Test_lua_heredoc()
3471 g:d = {}
3472 lua << trim EOF
3473 x = vim.eval('g:d')
3474 x['key'] = 'val'
3475 EOF
3476 assert_equal('val', g:d.key)
3477 enddef
3478endif
3479
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003480
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003481" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker