blob: de7b980fde946ef837a685e02c980a3e82ba26c4 [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 Moolenaar0ba48e82020-11-17 18:23:19 +0100163def CallRecursive(n: number): number
164 return CallRecursive(n + 1)
165enddef
166
167def CallMapRecursive(l: list<number>): number
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100168 return map(l, (_, v) => CallMapRecursive([v]))[0]
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100169enddef
170
171def Test_funcdepth_error()
172 set maxfuncdepth=10
173
174 var caught = false
175 try
176 CallRecursive(1)
177 catch /E132:/
178 caught = true
179 endtry
180 assert_true(caught)
181
182 caught = false
183 try
184 CallMapRecursive([1])
185 catch /E132:/
186 caught = true
187 endtry
188 assert_true(caught)
189
190 set maxfuncdepth&
191enddef
192
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100193def Test_endfunc_enddef()
194 var lines =<< trim END
195 def Test()
196 echo 'test'
197 endfunc
198 enddef
199 END
200 CheckScriptFailure(lines, 'E1151:', 3)
201
202 lines =<< trim END
203 def Test()
204 func Nested()
205 echo 'test'
206 enddef
207 enddef
208 END
209 CheckScriptFailure(lines, 'E1152:', 4)
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100210
211 lines =<< trim END
212 def Ok()
213 echo 'hello'
214 enddef | echo 'there'
215 def Bad()
216 echo 'hello'
217 enddef there
218 END
219 CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
Bram Moolenaar5178b1b2021-01-01 18:43:51 +0100220enddef
221
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +0100222def Test_missing_endfunc_enddef()
223 var lines =<< trim END
224 vim9script
225 def Test()
226 echo 'test'
227 endef
228 END
229 CheckScriptFailure(lines, 'E1057:', 2)
230
231 lines =<< trim END
232 vim9script
233 func Some()
234 echo 'test'
235 enfffunc
236 END
237 CheckScriptFailure(lines, 'E126:', 2)
238enddef
239
Bram Moolenaar4efd9942021-01-24 21:14:20 +0100240def Test_white_space_before_paren()
241 var lines =<< trim END
242 vim9script
243 def Test ()
244 echo 'test'
245 enddef
246 END
247 CheckScriptFailure(lines, 'E1068:', 2)
248
249 lines =<< trim END
250 vim9script
251 func Test ()
252 echo 'test'
253 endfunc
254 END
255 CheckScriptFailure(lines, 'E1068:', 2)
256
257 lines =<< trim END
258 def Test ()
259 echo 'test'
260 enddef
261 END
262 CheckScriptFailure(lines, 'E1068:', 1)
263
264 lines =<< trim END
265 func Test ()
266 echo 'test'
267 endfunc
268 END
269 CheckScriptSuccess(lines)
270enddef
271
Bram Moolenaar832ea892021-01-08 21:55:26 +0100272def Test_enddef_dict_key()
273 var d = {
274 enddef: 'x',
275 endfunc: 'y',
276 }
277 assert_equal({enddef: 'x', endfunc: 'y'}, d)
278enddef
279
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200280def ReturnString(): string
281 return 'string'
282enddef
283
284def ReturnNumber(): number
285 return 123
286enddef
287
288let g:notNumber = 'string'
289
290def ReturnGlobal(): number
291 return g:notNumber
292enddef
293
294def Test_return_something()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200295 ReturnString()->assert_equal('string')
296 ReturnNumber()->assert_equal(123)
Bram Moolenaar5e654232020-09-16 15:22:00 +0200297 assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200298enddef
299
Bram Moolenaare32e5162021-01-21 20:21:29 +0100300def Test_check_argument_type()
301 var lines =<< trim END
302 vim9script
303 def Val(a: number, b: number): number
304 return 0
305 enddef
306 def Func()
307 var x: any = true
308 Val(0, x)
309 enddef
310 disass Func
311 Func()
312 END
313 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
314enddef
315
Bram Moolenaarefd88552020-06-18 20:50:10 +0200316def Test_missing_return()
317 CheckDefFailure(['def Missing(): number',
318 ' if g:cond',
319 ' echo "no return"',
320 ' else',
321 ' return 0',
322 ' endif'
323 'enddef'], 'E1027:')
324 CheckDefFailure(['def Missing(): number',
325 ' if g:cond',
326 ' return 1',
327 ' else',
328 ' echo "no return"',
329 ' endif'
330 'enddef'], 'E1027:')
331 CheckDefFailure(['def Missing(): number',
332 ' if g:cond',
333 ' return 1',
334 ' else',
335 ' return 2',
336 ' endif'
337 ' return 3'
338 'enddef'], 'E1095:')
339enddef
340
Bram Moolenaar403dc312020-10-17 19:29:51 +0200341def Test_return_bool()
342 var lines =<< trim END
343 vim9script
344 def MenuFilter(id: number, key: string): bool
345 return popup_filter_menu(id, key)
346 enddef
347 def YesnoFilter(id: number, key: string): bool
348 return popup_filter_yesno(id, key)
349 enddef
350 defcompile
351 END
352 CheckScriptSuccess(lines)
353enddef
354
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200355let s:nothing = 0
356def ReturnNothing()
357 s:nothing = 1
358 if true
359 return
360 endif
361 s:nothing = 2
362enddef
363
364def Test_return_nothing()
365 ReturnNothing()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200366 s:nothing->assert_equal(1)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200367enddef
368
Bram Moolenaar648ea762021-01-15 19:04:32 +0100369def Test_return_invalid()
370 var lines =<< trim END
371 vim9script
372 def Func(): invalid
373 return xxx
374 enddef
375 defcompile
376 END
377 CheckScriptFailure(lines, 'E1010:', 2)
Bram Moolenaar31842cd2021-02-12 22:10:21 +0100378
379 lines =<< trim END
380 vim9script
381 def Test(Fun: func(number): number): list<number>
382 return map([1, 2, 3], (_, i) => Fun(i))
383 enddef
384 defcompile
385 def Inc(nr: number): nr
386 return nr + 2
387 enddef
388 echo Test(Inc)
389 END
390 # doing this twice was leaking memory
391 CheckScriptFailure(lines, 'E1010:')
392 CheckScriptFailure(lines, 'E1010:')
Bram Moolenaar648ea762021-01-15 19:04:32 +0100393enddef
394
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200395func Increment()
396 let g:counter += 1
397endfunc
398
399def Test_call_ufunc_count()
400 g:counter = 1
401 Increment()
402 Increment()
403 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200404 # works with and without :call
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200405 g:counter->assert_equal(4)
406 eval g:counter->assert_equal(4)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200407 unlet g:counter
408enddef
409
410def MyVarargs(arg: string, ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200411 var res = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200412 for s in rest
413 res ..= ',' .. s
414 endfor
415 return res
416enddef
417
418def Test_call_varargs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200419 MyVarargs('one')->assert_equal('one')
420 MyVarargs('one', 'two')->assert_equal('one,two')
421 MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200422enddef
423
424def MyDefaultArgs(name = 'string'): string
425 return name
426enddef
427
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200428def MyDefaultSecond(name: string, second: bool = true): string
429 return second ? name : 'none'
430enddef
431
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200432
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200433def Test_call_default_args()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200434 MyDefaultArgs()->assert_equal('string')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200435 MyDefaultArgs(v:none)->assert_equal('string')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200436 MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200437 assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200438
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200439 MyDefaultSecond('test')->assert_equal('test')
440 MyDefaultSecond('test', true)->assert_equal('test')
441 MyDefaultSecond('test', false)->assert_equal('none')
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200442
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200443 var lines =<< trim END
444 def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
445 return name .. aa .. bb
446 enddef
447
448 MyDefaultThird('->')->assert_equal('->aabb')
449 MyDefaultThird('->', v:none)->assert_equal('->aabb')
450 MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
451 MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
452 MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
453 MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
454 MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200455
456 def DefArg(mandatory: any, optional = mandatory): string
457 return mandatory .. optional
458 enddef
459 DefArg(1234)->assert_equal('12341234')
460 DefArg("ok")->assert_equal('okok')
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200461 END
462 CheckDefAndScriptSuccess(lines)
463
Bram Moolenaar822ba242020-05-24 23:00:18 +0200464 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100465 delfunc g:Func
Bram Moolenaar77072282020-09-16 17:55:40 +0200466 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 +0100467 delfunc g:Func
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200468 CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
Bram Moolenaar12bce952021-03-11 20:04:04 +0100469
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200470 lines =<< trim END
Bram Moolenaar12bce952021-03-11 20:04:04 +0100471 vim9script
472 def Func(a = b == 0 ? 1 : 2, b = 0)
473 enddef
474 defcompile
475 END
476 CheckScriptFailure(lines, 'E1001: Variable not found: b')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200477enddef
478
Bram Moolenaarcef12702021-01-04 14:09:43 +0100479def FuncWithComment( # comment
480 a: number, #comment
481 b: bool, # comment
482 c: string) #comment
483 assert_equal(4, a)
484 assert_equal(true, b)
485 assert_equal('yes', c)
486enddef
487
488def Test_func_with_comments()
489 FuncWithComment(4, true, 'yes')
490
491 var lines =<< trim END
492 def Func(# comment
493 arg: string)
494 enddef
495 END
496 CheckScriptFailure(lines, 'E125:', 1)
497
498 lines =<< trim END
499 def Func(
500 arg: string# comment
501 )
502 enddef
503 END
504 CheckScriptFailure(lines, 'E475:', 2)
505
506 lines =<< trim END
507 def Func(
508 arg: string
509 )# comment
510 enddef
511 END
512 CheckScriptFailure(lines, 'E488:', 3)
513enddef
514
Bram Moolenaar04b12692020-05-04 23:24:44 +0200515def Test_nested_function()
516 def Nested(arg: string): string
517 return 'nested ' .. arg
518 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200519 Nested('function')->assert_equal('nested function')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200520
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200521 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
522 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
523
Bram Moolenaar04b12692020-05-04 23:24:44 +0200524 CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200525 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
526 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200527
Bram Moolenaar54021752020-12-06 18:50:36 +0100528 var lines =<< trim END
529 def Outer()
530 def Inner()
531 # comment
532 enddef
533 def Inner()
534 enddef
535 enddef
536 END
537 CheckDefFailure(lines, 'E1073:')
538
539 lines =<< trim END
540 def Outer()
541 def Inner()
542 # comment
543 enddef
544 def! Inner()
545 enddef
546 enddef
547 END
548 CheckDefFailure(lines, 'E1117:')
549
550 # nested function inside conditional
Bram Moolenaar54021752020-12-06 18:50:36 +0100551 lines =<< trim END
552 vim9script
553 var thecount = 0
554 if true
555 def Test(): number
556 def TheFunc(): number
557 thecount += 1
558 return thecount
559 enddef
560 return TheFunc()
561 enddef
562 endif
563 defcompile
564 assert_equal(1, Test())
565 assert_equal(2, Test())
566 END
567 CheckScriptSuccess(lines)
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100568
569 # also works when "thecount" is inside the "if" block
570 lines =<< trim END
571 vim9script
572 if true
573 var thecount = 0
574 def Test(): number
575 def TheFunc(): number
576 thecount += 1
577 return thecount
578 enddef
579 return TheFunc()
580 enddef
581 endif
582 defcompile
583 assert_equal(1, Test())
584 assert_equal(2, Test())
585 END
586 CheckScriptSuccess(lines)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200587enddef
588
Bram Moolenaaradc8e442020-12-31 18:28:18 +0100589def Test_not_nested_function()
590 echo printf('%d',
591 function('len')('xxx'))
592enddef
593
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200594func Test_call_default_args_from_func()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200595 call MyDefaultArgs()->assert_equal('string')
596 call MyDefaultArgs('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200597 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200598endfunc
599
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200600def Test_nested_global_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200601 var lines =<< trim END
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200602 vim9script
603 def Outer()
604 def g:Inner(): string
605 return 'inner'
606 enddef
607 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200608 defcompile
609 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200610 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200611 delfunc g:Inner
612 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200613 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200614 delfunc g:Inner
615 Outer()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200616 g:Inner()->assert_equal('inner')
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200617 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200618 END
619 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200620
621 lines =<< trim END
622 vim9script
623 def Outer()
624 def g:Inner(): string
625 return 'inner'
626 enddef
627 enddef
628 defcompile
629 Outer()
630 Outer()
631 END
632 CheckScriptFailure(lines, "E122:")
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100633 delfunc g:Inner
Bram Moolenaarad486a02020-08-01 23:22:18 +0200634
635 lines =<< trim END
636 vim9script
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100637 def Outer()
638 def g:Inner()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100639 echo map([1, 2, 3], (_, v) => v + 1)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100640 enddef
641 g:Inner()
642 enddef
643 Outer()
644 END
645 CheckScriptSuccess(lines)
646 delfunc g:Inner
647
648 lines =<< trim END
649 vim9script
Bram Moolenaarad486a02020-08-01 23:22:18 +0200650 def Func()
651 echo 'script'
652 enddef
653 def Outer()
654 def Func()
655 echo 'inner'
656 enddef
657 enddef
658 defcompile
659 END
660 CheckScriptFailure(lines, "E1073:")
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200661enddef
662
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100663def DefListAll()
664 def
665enddef
666
667def DefListOne()
668 def DefListOne
669enddef
670
671def DefListMatches()
672 def /DefList
673enddef
674
675def Test_nested_def_list()
676 var funcs = split(execute('call DefListAll()'), "\n")
677 assert_true(len(funcs) > 10)
678 assert_true(funcs->index('def DefListAll()') >= 0)
679
680 funcs = split(execute('call DefListOne()'), "\n")
681 assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
682
683 funcs = split(execute('call DefListMatches()'), "\n")
684 assert_true(len(funcs) >= 3)
685 assert_true(funcs->index('def DefListAll()') >= 0)
686 assert_true(funcs->index('def DefListOne()') >= 0)
687 assert_true(funcs->index('def DefListMatches()') >= 0)
Bram Moolenaar54021752020-12-06 18:50:36 +0100688
689 var lines =<< trim END
690 vim9script
691 def Func()
692 def +Func+
693 enddef
694 defcompile
695 END
696 CheckScriptFailure(lines, 'E476:', 1)
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100697enddef
698
Bram Moolenaar333894b2020-08-01 18:53:07 +0200699def Test_global_local_function()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200700 var lines =<< trim END
Bram Moolenaar333894b2020-08-01 18:53:07 +0200701 vim9script
702 def g:Func(): string
703 return 'global'
704 enddef
705 def Func(): string
706 return 'local'
707 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +0200708 g:Func()->assert_equal('global')
709 Func()->assert_equal('local')
Bram Moolenaar2d870f82020-12-05 13:41:01 +0100710 delfunc g:Func
Bram Moolenaar333894b2020-08-01 18:53:07 +0200711 END
712 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200713
714 lines =<< trim END
715 vim9script
716 def g:Funcy()
717 echo 'funcy'
718 enddef
719 s:Funcy()
720 END
721 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200722enddef
723
Bram Moolenaar0f769812020-09-12 18:32:34 +0200724def Test_local_function_shadows_global()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200725 var lines =<< trim END
Bram Moolenaar0f769812020-09-12 18:32:34 +0200726 vim9script
727 def g:Gfunc(): string
728 return 'global'
729 enddef
730 def AnotherFunc(): number
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200731 var Gfunc = function('len')
Bram Moolenaar0f769812020-09-12 18:32:34 +0200732 return Gfunc('testing')
733 enddef
734 g:Gfunc()->assert_equal('global')
735 AnotherFunc()->assert_equal(7)
736 delfunc g:Gfunc
737 END
738 CheckScriptSuccess(lines)
739
740 lines =<< trim END
741 vim9script
742 def g:Func(): string
743 return 'global'
744 enddef
745 def AnotherFunc()
746 g:Func = function('len')
747 enddef
748 AnotherFunc()
749 END
750 CheckScriptFailure(lines, 'E705:')
751 delfunc g:Func
Bram Moolenaar0865b152021-04-05 15:38:51 +0200752
753 # global function is found without g: prefix
754 lines =<< trim END
755 vim9script
756 def g:Func(): string
757 return 'global'
758 enddef
759 def AnotherFunc(): string
760 return Func()
761 enddef
762 assert_equal('global', AnotherFunc())
763 delfunc g:Func
764 END
765 CheckScriptSuccess(lines)
766
767 lines =<< trim END
768 vim9script
769 def g:Func(): string
770 return 'global'
771 enddef
772 assert_equal('global', Func())
773 delfunc g:Func
774 END
775 CheckScriptSuccess(lines)
Bram Moolenaar0f769812020-09-12 18:32:34 +0200776enddef
777
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200778func TakesOneArg(arg)
779 echo a:arg
780endfunc
781
782def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200783 CheckDefFailure(['TakesOneArg()'], 'E119:')
784 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
785 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
786 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200787
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200788 var lines =<< trim END
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200789 vim9script
790 def Func(s: string)
791 echo s
792 enddef
793 Func([])
794 END
Bram Moolenaar77072282020-09-16 17:55:40 +0200795 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaarb185a402020-09-18 22:42:00 +0200796
797 lines =<< trim END
798 vim9script
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100799 var name = 'piet'
800 def FuncOne(name: string)
801 echo nr
802 enddef
803 END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100804 CheckScriptFailure(lines, 'E1168:')
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100805
806 lines =<< trim END
807 vim9script
Bram Moolenaarb185a402020-09-18 22:42:00 +0200808 def FuncOne(nr: number)
809 echo nr
810 enddef
811 def FuncTwo()
812 FuncOne()
813 enddef
814 defcompile
815 END
816 writefile(lines, 'Xscript')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200817 var didCatch = false
Bram Moolenaarb185a402020-09-18 22:42:00 +0200818 try
819 source Xscript
820 catch
821 assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
822 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
823 didCatch = true
824 endtry
825 assert_true(didCatch)
826
827 lines =<< trim END
828 vim9script
829 def FuncOne(nr: number)
830 echo nr
831 enddef
832 def FuncTwo()
833 FuncOne(1, 2)
834 enddef
835 defcompile
836 END
837 writefile(lines, 'Xscript')
838 didCatch = false
839 try
840 source Xscript
841 catch
842 assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
843 assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
844 didCatch = true
845 endtry
846 assert_true(didCatch)
847
848 delete('Xscript')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200849enddef
850
Bram Moolenaar50824712020-12-20 21:10:17 +0100851def Test_call_funcref_wrong_args()
852 var head =<< trim END
853 vim9script
854 def Func3(a1: string, a2: number, a3: list<number>)
855 echo a1 .. a2 .. a3[0]
856 enddef
857 def Testme()
858 var funcMap: dict<func> = {func: Func3}
859 END
860 var tail =<< trim END
861 enddef
862 Testme()
863 END
864 CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
865
866 CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
867 CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100868
869 var lines =<< trim END
870 vim9script
871 var Ref: func(number): any
872 Ref = (j) => !j
873 echo Ref(false)
874 END
875 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
876
877 lines =<< trim END
878 vim9script
879 var Ref: func(number): any
880 Ref = (j) => !j
881 call Ref(false)
882 END
883 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
Bram Moolenaar50824712020-12-20 21:10:17 +0100884enddef
885
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100886def Test_call_lambda_args()
Bram Moolenaar2a389082021-04-09 20:24:31 +0200887 var lines =<< trim END
888 var Callback = (..._) => 'anything'
889 assert_equal('anything', Callback())
890 assert_equal('anything', Callback(1))
891 assert_equal('anything', Callback('a', 2))
Bram Moolenaar1088b692021-04-09 22:12:44 +0200892
893 assert_equal('xyz', ((a: string): string => a)('xyz'))
Bram Moolenaar2a389082021-04-09 20:24:31 +0200894 END
895 CheckDefAndScriptSuccess(lines)
896
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100897 CheckDefFailure(['echo ((i) => 0)()'],
898 'E119: Not enough arguments for function: ((i) => 0)()')
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100899
Bram Moolenaar2a389082021-04-09 20:24:31 +0200900 lines =<< trim END
Bram Moolenaar2949cfd2020-12-31 21:28:47 +0100901 var Ref = (x: number, y: number) => x + y
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100902 echo Ref(1, 'x')
903 END
904 CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
Bram Moolenaare68b02a2021-01-03 13:09:51 +0100905
906 lines =<< trim END
907 var Ref: func(job, string, number)
908 Ref = (x, y) => 0
909 END
910 CheckDefAndScriptFailure(lines, 'E1012:')
911
912 lines =<< trim END
913 var Ref: func(job, string)
914 Ref = (x, y, z) => 0
915 END
916 CheckDefAndScriptFailure(lines, 'E1012:')
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100917
918 lines =<< trim END
919 var one = 1
920 var l = [1, 2, 3]
921 echo map(l, (one) => one)
922 END
923 CheckDefFailure(lines, 'E1167:')
924 CheckScriptFailure(['vim9script'] + lines, 'E1168:')
925
926 lines =<< trim END
Bram Moolenaar14ded112021-06-26 19:25:49 +0200927 var Ref: func(any, ?any): bool
928 Ref = (_, y = 1) => false
929 END
930 CheckDefAndScriptFailure(lines, 'E1172:')
931
932 lines =<< trim END
Bram Moolenaar015cf102021-06-26 21:52:02 +0200933 var a = 0
934 var b = (a == 0 ? 1 : 2)
935 assert_equal(1, b)
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200936 var txt = 'a'
937 b = (txt =~ 'x' ? 1 : 2)
938 assert_equal(2, b)
Bram Moolenaar015cf102021-06-26 21:52:02 +0200939 END
940 CheckDefAndScriptSuccess(lines)
941
942 lines =<< trim END
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100943 def ShadowLocal()
944 var one = 1
945 var l = [1, 2, 3]
946 echo map(l, (one) => one)
947 enddef
948 END
949 CheckDefFailure(lines, 'E1167:')
950
951 lines =<< trim END
952 def Shadowarg(one: number)
953 var l = [1, 2, 3]
954 echo map(l, (one) => one)
955 enddef
956 END
957 CheckDefFailure(lines, 'E1167:')
Bram Moolenaar767034c2021-04-09 17:24:52 +0200958
959 lines =<< trim END
960 echo ((a) => a)('aa', 'bb')
961 END
962 CheckDefAndScriptFailure(lines, 'E118:', 1)
Bram Moolenaarc4c56422021-07-21 20:38:46 +0200963
964 lines =<< trim END
965 echo 'aa'->((a) => a)('bb')
966 END
967 CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
968 CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100969enddef
970
Bram Moolenaar5f91e742021-03-17 21:29:29 +0100971def FilterWithCond(x: string, Cond: func(string): bool): bool
972 return Cond(x)
973enddef
974
Bram Moolenaar0346b792021-01-31 22:18:29 +0100975def Test_lambda_return_type()
976 var lines =<< trim END
977 var Ref = (): => 123
978 END
979 CheckDefAndScriptFailure(lines, 'E1157:', 1)
Bram Moolenaar5f91e742021-03-17 21:29:29 +0100980
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +0200981 # no space before the return type
982 lines =<< trim END
983 var Ref = (x):number => x + 1
984 END
985 CheckDefAndScriptFailure(lines, 'E1069:', 1)
986
Bram Moolenaar5f91e742021-03-17 21:29:29 +0100987 # this works
988 for x in ['foo', 'boo']
989 echo FilterWithCond(x, (v) => v =~ '^b')
990 endfor
991
992 # this fails
993 lines =<< trim END
994 echo FilterWithCond('foo', (v) => v .. '^b')
995 END
996 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
Bram Moolenaara9931532021-06-12 15:58:16 +0200997
998 lines =<< trim END
999 var Lambda1 = (x) => {
1000 return x
1001 }
1002 assert_equal('asdf', Lambda1('asdf'))
1003 var Lambda2 = (x): string => {
1004 return x
1005 }
1006 assert_equal('foo', Lambda2('foo'))
1007 END
1008 CheckDefAndScriptSuccess(lines)
1009
1010 lines =<< trim END
1011 var Lambda = (x): string => {
1012 return x
1013 }
1014 echo Lambda(['foo'])
1015 END
1016 CheckDefExecAndScriptFailure(lines, 'E1012:')
Bram Moolenaar0346b792021-01-31 22:18:29 +01001017enddef
1018
Bram Moolenaar709664c2020-12-12 14:33:41 +01001019def Test_lambda_uses_assigned_var()
1020 CheckDefSuccess([
1021 'var x: any = "aaa"'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001022 'x = filter(["bbb"], (_, v) => v =~ x)'])
Bram Moolenaar709664c2020-12-12 14:33:41 +01001023enddef
1024
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001025def Test_pass_legacy_lambda_to_def_func()
1026 var lines =<< trim END
1027 vim9script
1028 func Foo()
1029 eval s:Bar({x -> 0})
1030 endfunc
1031 def Bar(y: any)
1032 enddef
1033 Foo()
1034 END
1035 CheckScriptSuccess(lines)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001036
1037 lines =<< trim END
1038 vim9script
Bram Moolenaar7a40ff02021-07-04 15:54:08 +02001039 def g:TestFunc(f: func)
Bram Moolenaar831bdf82021-06-22 19:32:17 +02001040 enddef
1041 legacy call g:TestFunc({-> 0})
1042 delfunc g:TestFunc
1043
1044 def g:TestFunc(f: func(number))
1045 enddef
1046 legacy call g:TestFunc({nr -> 0})
1047 delfunc g:TestFunc
1048 END
1049 CheckScriptSuccess(lines)
Bram Moolenaar18062fc2021-03-05 21:35:47 +01001050enddef
1051
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001052" Default arg and varargs
1053def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001054 var res = one .. ',' .. two
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001055 for s in rest
1056 res ..= ',' .. s
1057 endfor
1058 return res
1059enddef
1060
1061def Test_call_def_varargs()
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001062 assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001063 MyDefVarargs('one')->assert_equal('one,foo')
1064 MyDefVarargs('one', 'two')->assert_equal('one,two')
1065 MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001066 CheckDefFailure(['MyDefVarargs("one", 22)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001067 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001068 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
Bram Moolenaar77072282020-09-16 17:55:40 +02001069 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001070
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001071 var lines =<< trim END
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001072 vim9script
1073 def Func(...l: list<string>)
1074 echo l
1075 enddef
1076 Func('a', 'b', 'c')
1077 END
1078 CheckScriptSuccess(lines)
1079
1080 lines =<< trim END
1081 vim9script
1082 def Func(...l: list<string>)
1083 echo l
1084 enddef
1085 Func()
1086 END
1087 CheckScriptSuccess(lines)
1088
1089 lines =<< trim END
1090 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001091 def Func(...l: list<any>)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001092 echo l
1093 enddef
1094 Func(0)
1095 END
1096 CheckScriptSuccess(lines)
1097
1098 lines =<< trim END
1099 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02001100 def Func(...l: any)
1101 echo l
1102 enddef
1103 Func(0)
1104 END
1105 CheckScriptFailure(lines, 'E1180:', 2)
1106
1107 lines =<< trim END
1108 vim9script
Bram Moolenaar28022722020-09-21 22:02:49 +02001109 def Func(..._l: list<string>)
1110 echo _l
1111 enddef
1112 Func('a', 'b', 'c')
1113 END
1114 CheckScriptSuccess(lines)
1115
1116 lines =<< trim END
1117 vim9script
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001118 def Func(...l: list<string>)
1119 echo l
1120 enddef
1121 Func(1, 2, 3)
1122 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001123 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001124
1125 lines =<< trim END
1126 vim9script
1127 def Func(...l: list<string>)
1128 echo l
1129 enddef
1130 Func('a', 9)
1131 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001132 CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001133
1134 lines =<< trim END
1135 vim9script
1136 def Func(...l: list<string>)
1137 echo l
1138 enddef
1139 Func(1, 'a')
1140 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001141 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
Bram Moolenaar4f53b792021-02-07 15:59:49 +01001142
1143 lines =<< trim END
1144 vim9script
1145 def Func( # some comment
1146 ...l = []
1147 )
1148 echo l
1149 enddef
1150 END
1151 CheckScriptFailure(lines, 'E1160:')
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001152
1153 lines =<< trim END
1154 vim9script
1155 def DoIt()
1156 g:Later('')
1157 enddef
1158 defcompile
1159 def g:Later(...l: list<number>)
1160 enddef
1161 DoIt()
1162 END
1163 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001164enddef
1165
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001166let s:value = ''
1167
1168def FuncOneDefArg(opt = 'text')
1169 s:value = opt
1170enddef
1171
1172def FuncTwoDefArg(nr = 123, opt = 'text'): string
1173 return nr .. opt
1174enddef
1175
1176def FuncVarargs(...arg: list<string>): string
1177 return join(arg, ',')
1178enddef
1179
1180def Test_func_type_varargs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001181 var RefDefArg: func(?string)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001182 RefDefArg = FuncOneDefArg
1183 RefDefArg()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001184 s:value->assert_equal('text')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001185 RefDefArg('some')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001186 s:value->assert_equal('some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001187
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001188 var RefDef2Arg: func(?number, ?string): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001189 RefDef2Arg = FuncTwoDefArg
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001190 RefDef2Arg()->assert_equal('123text')
1191 RefDef2Arg(99)->assert_equal('99text')
1192 RefDef2Arg(77, 'some')->assert_equal('77some')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001193
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001194 CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
1195 CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001196
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001197 var RefVarargs: func(...list<string>): string
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001198 RefVarargs = FuncVarargs
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001199 RefVarargs()->assert_equal('')
1200 RefVarargs('one')->assert_equal('one')
1201 RefVarargs('one', 'two')->assert_equal('one,two')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001202
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001203 CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
1204 CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001205enddef
1206
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001207" Only varargs
1208def MyVarargsOnly(...args: list<string>): string
1209 return join(args, ',')
1210enddef
1211
1212def Test_call_varargs_only()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001213 MyVarargsOnly()->assert_equal('')
1214 MyVarargsOnly('one')->assert_equal('one')
1215 MyVarargsOnly('one', 'two')->assert_equal('one,two')
Bram Moolenaar77072282020-09-16 17:55:40 +02001216 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1217 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001218enddef
1219
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001220def Test_using_var_as_arg()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001221 writefile(['def Func(x: number)', 'var x = 234', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001222 assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001223 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001224enddef
1225
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001226def DictArg(arg: dict<string>)
1227 arg['key'] = 'value'
1228enddef
1229
1230def ListArg(arg: list<string>)
1231 arg[0] = 'value'
1232enddef
1233
1234def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001235 # works for dict and list
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001236 var d: dict<string> = {}
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001237 DictArg(d)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001238 d['key']->assert_equal('value')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001239 var l: list<string> = []
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001240 ListArg(l)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001241 l[0]->assert_equal('value')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001242
Bram Moolenaard2c61702020-09-06 15:58:36 +02001243 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001244 delfunc! g:Func
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001245enddef
1246
Bram Moolenaarb816dae2020-09-20 22:04:00 +02001247" These argument names are reserved in legacy functions.
1248def WithReservedNames(firstline: string, lastline: string): string
1249 return firstline .. lastline
1250enddef
1251
1252def Test_argument_names()
1253 assert_equal('OK', WithReservedNames('O', 'K'))
1254enddef
1255
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001256def Test_call_func_defined_later()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001257 g:DefinedLater('one')->assert_equal('one')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001258 assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001259enddef
1260
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001261func DefinedLater(arg)
1262 return a:arg
1263endfunc
1264
1265def Test_call_funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001266 g:SomeFunc('abc')->assert_equal(3)
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001267 assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
1268 assert_fails('g:NotAFunc()', 'E117:', '', 3, 'Test_call_funcref')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001269
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001270 var lines =<< trim END
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001271 vim9script
1272 def RetNumber(): number
1273 return 123
1274 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001275 var Funcref: func: number = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001276 Funcref()->assert_equal(123)
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001277 END
1278 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001279
1280 lines =<< trim END
1281 vim9script
1282 def RetNumber(): number
1283 return 123
1284 enddef
1285 def Bar(F: func: number): number
1286 return F()
1287 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001288 var Funcref = function('RetNumber')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001289 Bar(Funcref)->assert_equal(123)
Bram Moolenaar0f60e802020-07-22 20:16:11 +02001290 END
1291 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001292
1293 lines =<< trim END
1294 vim9script
1295 def UseNumber(nr: number)
1296 echo nr
1297 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001298 var Funcref: func(number) = function('UseNumber')
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001299 Funcref(123)
1300 END
1301 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001302
1303 lines =<< trim END
1304 vim9script
1305 def UseNumber(nr: number)
1306 echo nr
1307 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001308 var Funcref: func(string) = function('UseNumber')
Bram Moolenaarb8070e32020-07-23 20:56:04 +02001309 END
Bram Moolenaar5e654232020-09-16 15:22:00 +02001310 CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001311
1312 lines =<< trim END
1313 vim9script
1314 def EchoNr(nr = 34)
1315 g:echo = nr
1316 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001317 var Funcref: func(?number) = function('EchoNr')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001318 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001319 g:echo->assert_equal(34)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001320 Funcref(123)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001321 g:echo->assert_equal(123)
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001322 END
1323 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +02001324
1325 lines =<< trim END
1326 vim9script
1327 def EchoList(...l: list<number>)
1328 g:echo = l
1329 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001330 var Funcref: func(...list<number>) = function('EchoList')
Bram Moolenaarace61322020-07-26 18:16:58 +02001331 Funcref()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001332 g:echo->assert_equal([])
Bram Moolenaarace61322020-07-26 18:16:58 +02001333 Funcref(1, 2, 3)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001334 g:echo->assert_equal([1, 2, 3])
Bram Moolenaarace61322020-07-26 18:16:58 +02001335 END
1336 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001337
1338 lines =<< trim END
1339 vim9script
1340 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
1341 g:optarg = opt
1342 g:listarg = l
1343 return nr
1344 enddef
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001345 var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001346 Funcref(10)->assert_equal(10)
1347 g:optarg->assert_equal(12)
1348 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001349
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001350 Funcref(11, 22)->assert_equal(11)
1351 g:optarg->assert_equal(22)
1352 g:listarg->assert_equal([])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001353
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001354 Funcref(17, 18, 1, 2, 3)->assert_equal(17)
1355 g:optarg->assert_equal(18)
1356 g:listarg->assert_equal([1, 2, 3])
Bram Moolenaar01865ad2020-07-26 18:33:09 +02001357 END
1358 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001359enddef
1360
1361let SomeFunc = function('len')
1362let NotAFunc = 'text'
1363
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001364def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001365 # same arguments, different return type
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001366 var Ref1: func(bool): string
1367 var Ref2: func(bool): number
1368 var Ref3: func(bool): any
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001369 Ref3 = g:cond ? Ref1 : Ref2
1370
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001371 # different number of arguments
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001372 var Refa1: func(bool): number
1373 var Refa2: func(bool, number): number
1374 var Refa3: func: number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001375 Refa3 = g:cond ? Refa1 : Refa2
1376
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001377 # different argument types
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001378 var Refb1: func(bool, string): number
1379 var Refb2: func(string, number): number
1380 var Refb3: func(any, any): number
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001381 Refb3 = g:cond ? Refb1 : Refb2
1382enddef
1383
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001384def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001385 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001386enddef
1387
1388def DefinedEvenLater(arg: string): string
1389 return arg
1390enddef
1391
1392def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001393 # Error in called function requires unwinding the call stack.
Bram Moolenaar44d66522020-09-06 22:26:57 +02001394 assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001395enddef
1396
1397def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001398 CheckScriptFailure([
1399 'def Func(): number',
1400 'return "a"',
1401 'enddef',
1402 'defcompile'], 'expected number but got string')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001403 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001404 CheckScriptFailure([
1405 'def Func(): string',
1406 'return 1',
1407 'enddef',
1408 'defcompile'], 'expected string but got number')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001409 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001410 CheckScriptFailure([
1411 'def Func(): void',
1412 'return "a"',
1413 'enddef',
1414 'defcompile'],
1415 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001416 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001417 CheckScriptFailure([
1418 'def Func()',
1419 'return "a"',
1420 'enddef',
1421 'defcompile'],
1422 'E1096: Returning a value in a function without a return type')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001423 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001424
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001425 CheckScriptFailure([
1426 'def Func(): number',
1427 'return',
1428 'enddef',
1429 'defcompile'], 'E1003:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001430 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001431
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02001432 CheckScriptFailure([
1433 'def Func():number',
1434 'return 123',
1435 'enddef',
1436 'defcompile'], 'E1069:')
1437 delfunc! g:Func
1438
1439 CheckScriptFailure([
1440 'def Func() :number',
1441 'return 123',
1442 'enddef',
1443 'defcompile'], 'E1059:')
1444 delfunc! g:Func
1445
1446 CheckScriptFailure([
1447 'def Func() : number',
1448 'return 123',
1449 'enddef',
1450 'defcompile'], 'E1059:')
1451 delfunc! g:Func
1452
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001453 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001454 delfunc! g:Func
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001455 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001456 delfunc! g:Func
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001457 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001458 delfunc! g:Func
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001459
1460 CheckScriptFailure([
1461 'vim9script',
1462 'def FuncB()',
1463 ' return 123',
1464 'enddef',
1465 'def FuncA()',
1466 ' FuncB()',
1467 'enddef',
1468 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001469enddef
1470
1471def Test_arg_type_wrong()
1472 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001473 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001474 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +02001475 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001476 CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
1477 CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001478enddef
1479
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001480def Test_white_space_before_comma()
1481 var lines =<< trim END
1482 vim9script
1483 def Func(a: number , b: number)
1484 enddef
1485 END
1486 CheckScriptFailure(lines, 'E1068:')
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001487 call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +02001488enddef
1489
Bram Moolenaar608d78f2021-03-06 22:33:12 +01001490def Test_white_space_after_comma()
1491 var lines =<< trim END
1492 vim9script
1493 def Func(a: number,b: number)
1494 enddef
1495 END
1496 CheckScriptFailure(lines, 'E1069:')
1497
1498 # OK in legacy function
1499 lines =<< trim END
1500 vim9script
1501 func Func(a,b)
1502 endfunc
1503 END
1504 CheckScriptSuccess(lines)
1505enddef
1506
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001507def Test_vim9script_call()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001508 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001509 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001510 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001511 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001512 name = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001513 enddef
1514 MyFunc('foobar')
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001515 name->assert_equal('foobar')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001516
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001517 var str = 'barfoo'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001518 str->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001519 name->assert_equal('barfoo')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001520
Bram Moolenaar67979662020-06-20 22:50:47 +02001521 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001522 g:value->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001523 name->assert_equal('value')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001524
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001525 var listvar = []
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001526 def ListFunc(arg: list<number>)
1527 listvar = arg
1528 enddef
1529 [1, 2, 3]->ListFunc()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001530 listvar->assert_equal([1, 2, 3])
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001531
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001532 var dictvar = {}
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001533 def DictFunc(arg: dict<number>)
1534 dictvar = arg
1535 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01001536 {a: 1, b: 2}->DictFunc()
1537 dictvar->assert_equal({a: 1, b: 2})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001538 def CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001539 {a: 3, b: 4}->DictFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001540 enddef
1541 CompiledDict()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001542 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001543
Bram Moolenaare0de1712020-12-02 17:36:54 +01001544 {a: 3, b: 4}->DictFunc()
1545 dictvar->assert_equal({a: 3, b: 4})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001546
1547 ('text')->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001548 name->assert_equal('text')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001549 ("some")->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001550 name->assert_equal('some')
Bram Moolenaare6b53242020-07-01 17:28:33 +02001551
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001552 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +02001553 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001554 'asdfasdf'->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001555 name->assert_equal('asdfasdf')
Bram Moolenaar10409562020-07-29 20:00:38 +02001556 "xyz"->MyFunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001557 name->assert_equal('xyz')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001558
1559 def UseString()
1560 'xyork'->MyFunc()
1561 enddef
1562 UseString()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001563 name->assert_equal('xyork')
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001564
Bram Moolenaar10409562020-07-29 20:00:38 +02001565 def UseString2()
1566 "knife"->MyFunc()
1567 enddef
1568 UseString2()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001569 name->assert_equal('knife')
Bram Moolenaar10409562020-07-29 20:00:38 +02001570
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001571 # prepending a colon makes it a mark
1572 new
1573 setline(1, ['aaa', 'bbb', 'ccc'])
1574 normal! 3Gmt1G
1575 :'t
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001576 getcurpos()[1]->assert_equal(3)
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001577 bwipe!
1578
Bram Moolenaare6b53242020-07-01 17:28:33 +02001579 MyFunc(
1580 'continued'
1581 )
1582 assert_equal('continued',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001583 name
Bram Moolenaare6b53242020-07-01 17:28:33 +02001584 )
1585
1586 call MyFunc(
1587 'more'
1588 ..
1589 'lines'
1590 )
1591 assert_equal(
1592 'morelines',
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001593 name)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001594 END
1595 writefile(lines, 'Xcall.vim')
1596 source Xcall.vim
1597 delete('Xcall.vim')
1598enddef
1599
1600def Test_vim9script_call_fail_decl()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001601 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001602 vim9script
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001603 var name = ''
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001604 def MyFunc(arg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001605 var name = 123
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001606 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001607 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001608 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02001609 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001610enddef
1611
Bram Moolenaar65b95452020-07-19 14:03:09 +02001612def Test_vim9script_call_fail_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001613 var lines =<< trim END
Bram Moolenaar65b95452020-07-19 14:03:09 +02001614 vim9script
1615 def MyFunc(arg: string)
1616 echo arg
1617 enddef
1618 MyFunc(1234)
1619 END
Bram Moolenaar77072282020-09-16 17:55:40 +02001620 CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +02001621enddef
1622
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001623def Test_vim9script_call_fail_const()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001624 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001625 vim9script
1626 const var = ''
1627 def MyFunc(arg: string)
1628 var = 'asdf'
1629 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001630 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001631 END
1632 writefile(lines, 'Xcall_const.vim')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001633 assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001634 delete('Xcall_const.vim')
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001635
1636 lines =<< trim END
1637 const g:Aconst = 77
1638 def Change()
1639 # comment
1640 g:Aconst = 99
1641 enddef
1642 call Change()
1643 unlet g:Aconst
1644 END
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001645 CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001646enddef
1647
1648" Test that inside :function a Python function can be defined, :def is not
1649" recognized.
1650func Test_function_python()
1651 CheckFeature python3
Bram Moolenaar727345e2020-09-27 23:33:59 +02001652 let py = 'python3'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001653 execute py "<< EOF"
1654def do_something():
1655 return 1
1656EOF
1657endfunc
1658
1659def Test_delfunc()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001660 var lines =<< trim END
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001661 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001662 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001663 echo 'hello'
1664 enddef
1665
1666 def CallGoneSoon()
1667 GoneSoon()
1668 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001669 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001670
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001671 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001672 CallGoneSoon()
1673 END
1674 writefile(lines, 'XToDelFunc')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001675 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
1676 assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001677
1678 delete('XToDelFunc')
1679enddef
1680
1681def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001682 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001683 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001684 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001685 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001686 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
Bram Moolenaar9bd5d872020-09-06 21:47:48 +02001687 assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001688 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001689 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +02001690 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001691
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001692 assert_fails('g:Func0()', 'E1091:')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001693 g:Func1()->assert_equal('Func1')
1694 g:Func2()->assert_equal('Func2')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001695
1696 delfunc! Func0
1697 delfunc! Func1
1698 delfunc! Func2
1699enddef
1700
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001701def Test_vim9script_func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001702 var lines =<< trim END
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001703 vim9script
1704 func Func(arg)
1705 echo a:arg
1706 endfunc
1707 Func('text')
1708 END
1709 writefile(lines, 'XVim9Func')
1710 so XVim9Func
1711
1712 delete('XVim9Func')
1713enddef
1714
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001715let s:funcResult = 0
1716
1717def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +02001718 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001719enddef
1720
1721def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001722 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001723 return 1234
1724enddef
1725
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001726def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +02001727 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001728 return 'text'
1729enddef
1730
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001731def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001732 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001733enddef
1734
1735def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +02001736 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001737 return arg
1738enddef
1739
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001740def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +02001741 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001742enddef
1743
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001744def FuncOneArgRetString(arg: string): string
1745 return arg
1746enddef
1747
Bram Moolenaar89228602020-04-05 22:14:54 +02001748def FuncOneArgRetAny(arg: any): any
1749 return arg
1750enddef
1751
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001752def Test_func_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001753 var Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +02001754 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001755 Ref1 = FuncNoArgNoRet
1756 Ref1()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001757 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001758
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001759 var Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +02001760 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001761 Ref2 = FuncNoArgNoRet
1762 Ref2()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001763 s:funcResult->assert_equal(11)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001764
Bram Moolenaar53900992020-08-22 19:02:02 +02001765 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001766 Ref2 = FuncOneArgNoRet
1767 Ref2(12)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001768 s:funcResult->assert_equal(12)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001769
Bram Moolenaar53900992020-08-22 19:02:02 +02001770 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001771 Ref2 = FuncNoArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001772 Ref2()->assert_equal(1234)
1773 s:funcResult->assert_equal(22)
Bram Moolenaar4c683752020-04-05 21:38:23 +02001774
Bram Moolenaar53900992020-08-22 19:02:02 +02001775 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +02001776 Ref2 = FuncOneArgRetNumber
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001777 Ref2(13)->assert_equal(13)
1778 s:funcResult->assert_equal(13)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001779enddef
1780
Bram Moolenaar9978d472020-07-05 16:01:56 +02001781def Test_repeat_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001782 var res = 0
Bram Moolenaar9978d472020-07-05 16:01:56 +02001783 for n in repeat([1], 3)
1784 res += n
1785 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001786 res->assert_equal(3)
Bram Moolenaarfce82b32020-07-05 16:07:21 +02001787
1788 res = 0
1789 for n in add([1, 2], 3)
1790 res += n
1791 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001792 res->assert_equal(6)
Bram Moolenaar9978d472020-07-05 16:01:56 +02001793enddef
1794
Bram Moolenaar846178a2020-07-05 17:04:13 +02001795def Test_argv_return_type()
1796 next fileone filetwo
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001797 var res = ''
Bram Moolenaar846178a2020-07-05 17:04:13 +02001798 for name in argv()
1799 res ..= name
1800 endfor
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001801 res->assert_equal('fileonefiletwo')
Bram Moolenaar846178a2020-07-05 17:04:13 +02001802enddef
1803
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001804def Test_func_type_part()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001805 var RefVoid: func: void
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001806 RefVoid = FuncNoArgNoRet
1807 RefVoid = FuncOneArgNoRet
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001808 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
1809 CheckDefFailure(['var RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001810
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001811 var RefAny: func(): any
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001812 RefAny = FuncNoArgRetNumber
1813 RefAny = FuncNoArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001814 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
1815 CheckDefFailure(['var RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001816
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001817 var RefAnyNoArgs: func: any = RefAny
1818
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001819 var RefNr: func: number
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001820 RefNr = FuncNoArgRetNumber
1821 RefNr = FuncOneArgRetNumber
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001822 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
1823 CheckDefFailure(['var RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001824
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001825 var RefStr: func: string
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001826 RefStr = FuncNoArgRetString
1827 RefStr = FuncOneArgRetString
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001828 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
1829 CheckDefFailure(['var RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001830enddef
1831
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001832def Test_func_type_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001833 CheckDefFailure(['var ref1: func()'], 'E704:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001834
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001835 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
1836 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
1837 CheckDefFailure(['var Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
1838 CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
1839 CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
1840 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 +02001841
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001842 CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
1843 CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
1844 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:')
1845 CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001846enddef
1847
Bram Moolenaar89228602020-04-05 22:14:54 +02001848def Test_func_return_type()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001849 var nr: number
Bram Moolenaar89228602020-04-05 22:14:54 +02001850 nr = FuncNoArgRetNumber()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001851 nr->assert_equal(1234)
Bram Moolenaar89228602020-04-05 22:14:54 +02001852
1853 nr = FuncOneArgRetAny(122)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001854 nr->assert_equal(122)
Bram Moolenaar89228602020-04-05 22:14:54 +02001855
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001856 var str: string
Bram Moolenaar89228602020-04-05 22:14:54 +02001857 str = FuncOneArgRetAny('yes')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001858 str->assert_equal('yes')
Bram Moolenaar89228602020-04-05 22:14:54 +02001859
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001860 CheckDefFailure(['var str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +02001861enddef
1862
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02001863def Test_func_common_type()
1864 def FuncOne(n: number): number
1865 return n
1866 enddef
1867 def FuncTwo(s: string): number
1868 return len(s)
1869 enddef
1870 def FuncThree(n: number, s: string): number
1871 return n + len(s)
1872 enddef
1873 var list = [FuncOne, FuncTwo, FuncThree]
1874 assert_equal(8, list[0](8))
1875 assert_equal(4, list[1]('word'))
1876 assert_equal(7, list[2](3, 'word'))
1877enddef
1878
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001879def MultiLine(
1880 arg1: string,
1881 arg2 = 1234,
1882 ...rest: list<string>
1883 ): string
1884 return arg1 .. arg2 .. join(rest, '-')
1885enddef
1886
Bram Moolenaar2c330432020-04-13 14:41:35 +02001887def MultiLineComment(
1888 arg1: string, # comment
1889 arg2 = 1234, # comment
1890 ...rest: list<string> # comment
1891 ): string # comment
1892 return arg1 .. arg2 .. join(rest, '-')
1893enddef
1894
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001895def Test_multiline()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001896 MultiLine('text')->assert_equal('text1234')
1897 MultiLine('text', 777)->assert_equal('text777')
1898 MultiLine('text', 777, 'one')->assert_equal('text777one')
1899 MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001900enddef
1901
Bram Moolenaar23e03252020-04-12 22:22:31 +02001902func Test_multiline_not_vim9()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001903 call MultiLine('text')->assert_equal('text1234')
1904 call MultiLine('text', 777)->assert_equal('text777')
1905 call MultiLine('text', 777, 'one')->assert_equal('text777one')
1906 call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
Bram Moolenaar23e03252020-04-12 22:22:31 +02001907endfunc
1908
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001909
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001910" When using CheckScriptFailure() for the below test, E1010 is generated instead
1911" of E1056.
1912func Test_E1056_1059()
1913 let caught_1056 = 0
1914 try
1915 def F():
1916 return 1
1917 enddef
1918 catch /E1056:/
1919 let caught_1056 = 1
1920 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001921 eval caught_1056->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001922
1923 let caught_1059 = 0
1924 try
1925 def F5(items : list)
1926 echo 'a'
1927 enddef
1928 catch /E1059:/
1929 let caught_1059 = 1
1930 endtry
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001931 eval caught_1059->assert_equal(1)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001932endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001933
Bram Moolenaar015f4262020-05-05 21:25:22 +02001934func DelMe()
1935 echo 'DelMe'
1936endfunc
1937
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001938def Test_error_reporting()
1939 # comment lines at the start of the function
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001940 var lines =<< trim END
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001941 " comment
1942 def Func()
1943 # comment
1944 # comment
1945 invalid
1946 enddef
1947 defcompile
1948 END
Bram Moolenaar08052222020-09-14 17:04:31 +02001949 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001950 try
1951 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001952 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001953 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001954 v:exception->assert_match('Invalid command: invalid')
1955 v:throwpoint->assert_match(', line 3$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001956 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001957 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001958
1959 # comment lines after the start of the function
1960 lines =<< trim END
1961 " comment
1962 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001963 var x = 1234
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001964 # comment
1965 # comment
1966 invalid
1967 enddef
1968 defcompile
1969 END
Bram Moolenaar08052222020-09-14 17:04:31 +02001970 writefile(lines, 'Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001971 try
1972 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001973 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001974 catch /E476:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001975 v:exception->assert_match('Invalid command: invalid')
1976 v:throwpoint->assert_match(', line 4$')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001977 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001978 delfunc! g:Func
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001979
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001980 lines =<< trim END
1981 vim9script
1982 def Func()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001983 var db = {foo: 1, bar: 2}
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001984 # comment
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02001985 var x = db.asdf
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001986 enddef
1987 defcompile
1988 Func()
1989 END
Bram Moolenaar08052222020-09-14 17:04:31 +02001990 writefile(lines, 'Xdef')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001991 try
1992 source Xdef
1993 assert_report('should have failed')
1994 catch /E716:/
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02001995 v:throwpoint->assert_match('_Func, line 3$')
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001996 endtry
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001997 delfunc! g:Func
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001998
Bram Moolenaar08052222020-09-14 17:04:31 +02001999 delete('Xdef')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02002000enddef
2001
Bram Moolenaar015f4262020-05-05 21:25:22 +02002002def Test_deleted_function()
2003 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002004 'var RefMe: func = function("g:DelMe")',
Bram Moolenaar015f4262020-05-05 21:25:22 +02002005 'delfunc g:DelMe',
2006 'echo RefMe()'], 'E117:')
2007enddef
2008
2009def Test_unknown_function()
2010 CheckDefExecFailure([
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002011 'var Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002012 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02002013enddef
2014
Bram Moolenaar328eac22021-01-07 19:23:08 +01002015def RefFunc(Ref: func(any): any): string
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002016 return Ref('more')
2017enddef
2018
2019def Test_closure_simple()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002020 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002021 RefFunc((s) => local .. s)->assert_equal('some more')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002022enddef
2023
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002024def MakeRef()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002025 var local = 'some '
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002026 g:Ref = (s) => local .. s
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002027enddef
2028
2029def Test_closure_ref_after_return()
2030 MakeRef()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002031 g:Ref('thing')->assert_equal('some thing')
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002032 unlet g:Ref
2033enddef
2034
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002035def MakeTwoRefs()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002036 var local = ['some']
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002037 g:Extend = (s) => local->add(s)
2038 g:Read = () => local
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002039enddef
2040
2041def Test_closure_two_refs()
2042 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002043 join(g:Read(), ' ')->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002044 g:Extend('more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002045 join(g:Read(), ' ')->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002046 g:Extend('even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002047 join(g:Read(), ' ')->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002048
2049 unlet g:Extend
2050 unlet g:Read
2051enddef
2052
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002053def ReadRef(Ref: func(): list<string>): string
2054 return join(Ref(), ' ')
2055enddef
2056
Bram Moolenaar5e654232020-09-16 15:22:00 +02002057def ExtendRef(Ref: func(string): list<string>, add: string)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002058 Ref(add)
2059enddef
2060
2061def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002062 MakeTwoRefs()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002063 ReadRef(g:Read)->assert_equal('some')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002064 ExtendRef(g:Extend, 'more')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002065 ReadRef(g:Read)->assert_equal('some more')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002066 ExtendRef(g:Extend, 'even')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002067 ReadRef(g:Read)->assert_equal('some more even')
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002068
2069 unlet g:Extend
2070 unlet g:Read
2071enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002072
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002073def MakeArgRefs(theArg: string)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002074 var local = 'loc_val'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002075 g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002076enddef
2077
2078def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002079 var local = 'the_loc'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002080 g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002081enddef
2082
2083def Test_closure_using_argument()
2084 MakeArgRefs('arg_val')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002085 g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002086
2087 MakeArgRefsVarargs('arg_val', 'one', 'two')
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002088 g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002089
2090 unlet g:UseArg
2091 unlet g:UseVararg
Bram Moolenaar44ec21c2021-02-12 21:50:57 +01002092
2093 var lines =<< trim END
2094 vim9script
2095 def Test(Fun: func(number): number): list<number>
2096 return map([1, 2, 3], (_, i) => Fun(i))
2097 enddef
2098 def Inc(nr: number): number
2099 return nr + 2
2100 enddef
2101 assert_equal([3, 4, 5], Test(Inc))
2102 END
2103 CheckScriptSuccess(lines)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002104enddef
2105
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002106def MakeGetAndAppendRefs()
2107 var local = 'a'
2108
2109 def Append(arg: string)
2110 local ..= arg
2111 enddef
2112 g:Append = Append
2113
2114 def Get(): string
2115 return local
2116 enddef
2117 g:Get = Get
2118enddef
2119
2120def Test_closure_append_get()
2121 MakeGetAndAppendRefs()
2122 g:Get()->assert_equal('a')
2123 g:Append('-b')
2124 g:Get()->assert_equal('a-b')
2125 g:Append('-c')
2126 g:Get()->assert_equal('a-b-c')
2127
2128 unlet g:Append
2129 unlet g:Get
2130enddef
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002131
Bram Moolenaar04b12692020-05-04 23:24:44 +02002132def Test_nested_closure()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002133 var local = 'text'
Bram Moolenaar04b12692020-05-04 23:24:44 +02002134 def Closure(arg: string): string
2135 return local .. arg
2136 enddef
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002137 Closure('!!!')->assert_equal('text!!!')
Bram Moolenaar04b12692020-05-04 23:24:44 +02002138enddef
2139
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002140func GetResult(Ref)
2141 return a:Ref('some')
2142endfunc
2143
2144def Test_call_closure_not_compiled()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002145 var text = 'text'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002146 g:Ref = (s) => s .. text
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002147 GetResult(g:Ref)->assert_equal('sometext')
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002148enddef
2149
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002150def Test_double_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002151 var lines =<< trim END
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002152 vim9script
2153 def Func()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002154 var name = 0
2155 for i in range(2)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002156 timer_start(0, () => name)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002157 endfor
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002158 enddef
2159 Func()
2160 END
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02002161 CheckScriptSuccess(lines)
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002162enddef
2163
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002164def Test_nested_closure_used()
2165 var lines =<< trim END
2166 vim9script
2167 def Func()
2168 var x = 'hello'
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002169 var Closure = () => x
2170 g:Myclosure = () => Closure()
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02002171 enddef
2172 Func()
2173 assert_equal('hello', g:Myclosure())
2174 END
2175 CheckScriptSuccess(lines)
2176enddef
Bram Moolenaar0876c782020-10-07 19:08:04 +02002177
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002178def Test_nested_closure_fails()
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002179 var lines =<< trim END
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002180 vim9script
2181 def FuncA()
2182 FuncB(0)
2183 enddef
2184 def FuncB(n: number): list<string>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002185 return map([0], (_, v) => n)
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002186 enddef
2187 FuncA()
2188 END
2189 CheckScriptFailure(lines, 'E1012:')
2190enddef
2191
Bram Moolenaarf112f302020-12-20 17:47:52 +01002192def Test_global_closure()
2193 var lines =<< trim END
2194 vim9script
2195 def ReverseEveryNLines(n: number, line1: number, line2: number)
2196 var mods = 'sil keepj keepp lockm '
2197 var range = ':' .. line1 .. ',' .. line2
2198 def g:Offset(): number
2199 var offset = (line('.') - line1 + 1) % n
2200 return offset != 0 ? offset : n
2201 enddef
2202 exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
2203 enddef
2204
2205 new
2206 repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
2207 ReverseEveryNLines(3, 1, 9)
2208 END
2209 CheckScriptSuccess(lines)
2210 var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
2211 assert_equal(expected, getline(1, 9))
2212 bwipe!
2213enddef
2214
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002215def Test_global_closure_called_directly()
2216 var lines =<< trim END
2217 vim9script
2218 def Outer()
2219 var x = 1
2220 def g:Inner()
2221 var y = x
2222 x += 1
2223 assert_equal(1, y)
2224 enddef
2225 g:Inner()
2226 assert_equal(2, x)
2227 enddef
2228 Outer()
2229 END
2230 CheckScriptSuccess(lines)
2231 delfunc g:Inner
2232enddef
2233
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01002234def Test_failure_in_called_function()
2235 # this was using the frame index as the return value
2236 var lines =<< trim END
2237 vim9script
2238 au TerminalWinOpen * eval [][0]
2239 def PopupTerm(a: any)
2240 # make sure typvals on stack are string
2241 ['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
2242 FireEvent()
2243 enddef
2244 def FireEvent()
2245 do TerminalWinOpen
2246 enddef
2247 # use try/catch to make eval fail
2248 try
2249 call PopupTerm(0)
2250 catch
2251 endtry
2252 au! TerminalWinOpen
2253 END
2254 CheckScriptSuccess(lines)
2255enddef
2256
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002257def Test_nested_lambda()
2258 var lines =<< trim END
2259 vim9script
2260 def Func()
2261 var x = 4
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002262 var Lambda1 = () => 7
2263 var Lambda2 = () => [Lambda1(), x]
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02002264 var res = Lambda2()
2265 assert_equal([7, 4], res)
2266 enddef
2267 Func()
2268 END
2269 CheckScriptSuccess(lines)
2270enddef
2271
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002272def Test_double_nested_lambda()
2273 var lines =<< trim END
2274 vim9script
2275 def F(head: string): func(string): func(string): string
2276 return (sep: string): func(string): string => ((tail: string): string => {
2277 return head .. sep .. tail
2278 })
2279 enddef
2280 assert_equal('hello-there', F('hello')('-')('there'))
2281 END
2282 CheckScriptSuccess(lines)
2283enddef
2284
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002285def Test_nested_inline_lambda()
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002286 var lines =<< trim END
2287 vim9script
2288 def F(text: string): func(string): func(string): string
2289 return (arg: string): func(string): string => ((sep: string): string => {
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002290 return sep .. arg .. text
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002291 })
2292 enddef
Bram Moolenaar23e2e112021-08-03 21:16:18 +02002293 assert_equal('--there++', F('++')('there')('--'))
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002294 END
2295 CheckScriptSuccess(lines)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02002296
2297 lines =<< trim END
2298 vim9script
2299 echo range(4)->mapnew((_, v) => {
2300 return range(v) ->mapnew((_, s) => {
2301 return string(s)
2302 })
2303 })
2304 END
2305 CheckScriptSuccess(lines)
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002306
2307 lines =<< trim END
2308 vim9script
2309
2310 def s:func()
2311 range(10)
2312 ->mapnew((_, _) => ({
2313 key: range(10)->mapnew((_, _) => {
2314 return ' '
2315 }),
2316 }))
2317 enddef
2318
2319 defcomp
2320 END
2321 CheckScriptSuccess(lines)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02002322enddef
2323
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002324def Shadowed(): list<number>
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002325 var FuncList: list<func: number> = [() => 42]
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002326 return FuncList->mapnew((_, Shadowed) => Shadowed())
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002327enddef
2328
2329def Test_lambda_arg_shadows_func()
2330 assert_equal([42], Shadowed())
2331enddef
2332
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002333def Line_continuation_in_def(dir: string = ''): string
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002334 var path: string = empty(dir)
2335 \ ? 'empty'
2336 \ : 'full'
2337 return path
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002338enddef
2339
2340def Test_line_continuation_in_def()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002341 Line_continuation_in_def('.')->assert_equal('full')
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002342enddef
2343
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002344def Test_script_var_in_lambda()
2345 var lines =<< trim END
2346 vim9script
2347 var script = 'test'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002348 assert_equal(['test'], map(['one'], (_, _) => script))
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002349 END
2350 CheckScriptSuccess(lines)
2351enddef
2352
Bram Moolenaar5e654232020-09-16 15:22:00 +02002353def Line_continuation_in_lambda(): list<string>
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002354 var x = range(97, 100)
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002355 ->mapnew((_, v) => nr2char(v)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002356 ->toupper())
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002357 ->reverse()
2358 return x
2359enddef
2360
2361def Test_line_continuation_in_lambda()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002362 Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01002363
2364 var lines =<< trim END
2365 vim9script
2366 var res = [{n: 1, m: 2, s: 'xxx'}]
2367 ->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
2368 v.n,
2369 v.m,
2370 substitute(v.s, '.*', 'yyy', '')
2371 ))
2372 assert_equal(['1:2:yyy'], res)
2373 END
2374 CheckScriptSuccess(lines)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002375enddef
2376
Bram Moolenaarb6571982021-01-08 22:24:19 +01002377def Test_list_lambda()
2378 timer_start(1000, (_) => 0)
2379 var body = execute(timer_info()[0].callback
2380 ->string()
2381 ->substitute("('", ' ', '')
2382 ->substitute("')", '', '')
2383 ->substitute('function\zs', ' ', ''))
Bram Moolenaar767034c2021-04-09 17:24:52 +02002384 assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
Bram Moolenaarb6571982021-01-08 22:24:19 +01002385enddef
2386
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002387def Test_lambda_block_variable()
Bram Moolenaar88421d62021-07-24 14:14:52 +02002388 var lines =<< trim END
2389 vim9script
2390 var flist: list<func>
2391 for i in range(10)
2392 var inloop = i
2393 flist[i] = () => inloop
2394 endfor
2395 END
2396 CheckScriptSuccess(lines)
2397
2398 lines =<< trim END
2399 vim9script
2400 if true
2401 var outloop = 5
2402 var flist: list<func>
2403 for i in range(10)
2404 flist[i] = () => outloop
2405 endfor
2406 endif
2407 END
2408 CheckScriptSuccess(lines)
2409
2410 lines =<< trim END
2411 vim9script
2412 if true
2413 var outloop = 5
2414 endif
2415 var flist: list<func>
2416 for i in range(10)
2417 flist[i] = () => outloop
2418 endfor
2419 END
2420 CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +02002421
2422 lines =<< trim END
2423 vim9script
2424 for i in range(10)
2425 var Ref = () => 0
2426 endfor
2427 assert_equal(0, ((i) => 0)(0))
2428 END
2429 CheckScriptSuccess(lines)
Bram Moolenaar88421d62021-07-24 14:14:52 +02002430enddef
2431
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002432def Test_legacy_lambda()
2433 legacy echo {x -> 'hello ' .. x}('foo')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002434
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002435 var lines =<< trim END
2436 echo {x -> 'hello ' .. x}('foo')
2437 END
2438 CheckDefAndScriptFailure(lines, 'E720:')
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002439
2440 lines =<< trim END
2441 vim9script
2442 def Func()
2443 echo (() => 'no error')()
2444 enddef
2445 legacy call s:Func()
2446 END
2447 CheckScriptSuccess(lines)
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002448enddef
2449
Bram Moolenaarce024c32021-06-26 13:00:49 +02002450def Test_legacy()
2451 var lines =<< trim END
2452 vim9script
2453 func g:LegacyFunction()
2454 let g:legacyvar = 1
2455 endfunc
2456 def Testit()
2457 legacy call g:LegacyFunction()
2458 enddef
2459 Testit()
2460 assert_equal(1, g:legacyvar)
2461 unlet g:legacyvar
2462 delfunc g:LegacyFunction
2463 END
2464 CheckScriptSuccess(lines)
2465enddef
2466
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002467def Test_legacy_errors()
2468 for cmd in ['if', 'elseif', 'else', 'endif',
2469 'for', 'endfor', 'continue', 'break',
2470 'while', 'endwhile',
2471 'try', 'catch', 'finally', 'endtry']
2472 CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
2473 endfor
2474enddef
2475
Bram Moolenaarab360522021-01-10 14:02:28 +01002476def DoFilterThis(a: string): list<string>
2477 # closure nested inside another closure using argument
2478 var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
2479 return ['x', 'y', 'a', 'x2', 'c']->Filter()
2480enddef
2481
2482def Test_nested_closure_using_argument()
2483 assert_equal(['x', 'x2'], DoFilterThis('x'))
2484enddef
2485
Bram Moolenaar0186e582021-01-10 18:33:11 +01002486def Test_triple_nested_closure()
2487 var what = 'x'
2488 var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
2489 var Filter = (l) => filter(l, (_, v) => Match(v, what))
2490 assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
2491enddef
2492
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002493func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002494 CheckScreendump
2495
2496 let lines =<< trim END
2497 vim9script
2498 def EchoNothing()
2499 silent echo ''
2500 enddef
2501 defcompile
2502 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002503 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002504
2505 " Check that the balloon shows up after a mouse move
2506 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002507 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002508 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
2509
2510 " clean up
2511 call StopVimInTerminal(buf)
2512 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02002513endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002514
Bram Moolenaar171fb922020-10-28 16:54:47 +01002515def SilentlyError()
2516 execute('silent! invalid')
2517 g:did_it = 'yes'
2518enddef
2519
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002520func UserError()
2521 silent! invalid
2522endfunc
2523
2524def SilentlyUserError()
2525 UserError()
2526 g:did_it = 'yes'
2527enddef
Bram Moolenaar171fb922020-10-28 16:54:47 +01002528
2529" This can't be a :def function, because the assert would not be reached.
Bram Moolenaar171fb922020-10-28 16:54:47 +01002530func Test_ignore_silent_error()
2531 let g:did_it = 'no'
2532 call SilentlyError()
2533 call assert_equal('yes', g:did_it)
2534
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002535 let g:did_it = 'no'
2536 call SilentlyUserError()
2537 call assert_equal('yes', g:did_it)
Bram Moolenaar171fb922020-10-28 16:54:47 +01002538
2539 unlet g:did_it
2540endfunc
2541
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002542def Test_ignore_silent_error_in_filter()
2543 var lines =<< trim END
2544 vim9script
2545 def Filter(winid: number, key: string): bool
2546 if key == 'o'
2547 silent! eval [][0]
2548 return true
2549 endif
2550 return popup_filter_menu(winid, key)
2551 enddef
2552
Bram Moolenaare0de1712020-12-02 17:36:54 +01002553 popup_create('popup', {filter: Filter})
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002554 feedkeys("o\r", 'xnt')
2555 END
2556 CheckScriptSuccess(lines)
2557enddef
2558
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02002559def Fibonacci(n: number): number
2560 if n < 2
2561 return n
2562 else
2563 return Fibonacci(n - 1) + Fibonacci(n - 2)
2564 endif
2565enddef
2566
Bram Moolenaar985116a2020-07-12 17:31:09 +02002567def Test_recursive_call()
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002568 Fibonacci(20)->assert_equal(6765)
Bram Moolenaar985116a2020-07-12 17:31:09 +02002569enddef
2570
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002571def TreeWalk(dir: string): list<any>
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002572 return readdir(dir)->mapnew((_, val) =>
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002573 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaar2bede172020-11-19 18:53:18 +01002574 ? {[val]: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002575 : val
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002576 )
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002577enddef
2578
2579def Test_closure_in_map()
2580 mkdir('XclosureDir/tdir', 'p')
2581 writefile(['111'], 'XclosureDir/file1')
2582 writefile(['222'], 'XclosureDir/file2')
2583 writefile(['333'], 'XclosureDir/tdir/file3')
2584
Bram Moolenaare0de1712020-12-02 17:36:54 +01002585 TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
Bram Moolenaar08f7a412020-07-13 20:41:08 +02002586
2587 delete('XclosureDir', 'rf')
2588enddef
2589
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002590def Test_invalid_function_name()
2591 var lines =<< trim END
2592 vim9script
2593 def s: list<string>
2594 END
2595 CheckScriptFailure(lines, 'E129:')
2596
2597 lines =<< trim END
2598 vim9script
2599 def g: list<string>
2600 END
2601 CheckScriptFailure(lines, 'E129:')
2602
2603 lines =<< trim END
2604 vim9script
2605 def <SID>: list<string>
2606 END
2607 CheckScriptFailure(lines, 'E884:')
2608
2609 lines =<< trim END
2610 vim9script
2611 def F list<string>
2612 END
2613 CheckScriptFailure(lines, 'E488:')
2614enddef
2615
Bram Moolenaara90afb92020-07-15 22:38:56 +02002616def Test_partial_call()
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002617 var lines =<< trim END
2618 var Xsetlist: func
2619 Xsetlist = function('setloclist', [0])
2620 Xsetlist([], ' ', {title: 'test'})
2621 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002622
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002623 Xsetlist = function('setloclist', [0, [], ' '])
2624 Xsetlist({title: 'test'})
2625 getloclist(0, {title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002626
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002627 Xsetlist = function('setqflist')
2628 Xsetlist([], ' ', {title: 'test'})
2629 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaara90afb92020-07-15 22:38:56 +02002630
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002631 Xsetlist = function('setqflist', [[], ' '])
2632 Xsetlist({title: 'test'})
2633 getqflist({title: 1})->assert_equal({title: 'test'})
Bram Moolenaar6abd3dc2020-10-04 14:17:32 +02002634
Bram Moolenaarf78da4f2021-08-01 15:40:31 +02002635 var Len: func: number = function('len', ['word'])
2636 assert_equal(4, Len())
2637
2638 var RepeatFunc = function('repeat', ['o'])
2639 assert_equal('ooooo', RepeatFunc(5))
2640 END
2641 CheckDefAndScriptSuccess(lines)
Bram Moolenaara90afb92020-07-15 22:38:56 +02002642enddef
2643
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002644def Test_cmd_modifier()
2645 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002646 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002647enddef
2648
2649def Test_restore_modifiers()
2650 # check that when compiling a :def function command modifiers are not messed
2651 # up.
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002652 var lines =<< trim END
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002653 vim9script
2654 set eventignore=
2655 autocmd QuickFixCmdPost * copen
2656 def AutocmdsDisabled()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002657 eval 1 + 2
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002658 enddef
2659 func Func()
2660 noautocmd call s:AutocmdsDisabled()
2661 let g:ei_after = &eventignore
2662 endfunc
2663 Func()
2664 END
2665 CheckScriptSuccess(lines)
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002666 g:ei_after->assert_equal('')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002667enddef
2668
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002669def StackTop()
Bram Moolenaarc3235272021-07-10 19:42:03 +02002670 eval 1 + 2
2671 eval 2 + 3
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002672 # call not on fourth line
2673 StackBot()
2674enddef
2675
2676def StackBot()
2677 # throw an error
2678 eval [][0]
2679enddef
2680
2681def Test_callstack_def()
2682 try
2683 StackTop()
2684 catch
Bram Moolenaarc0c71e92020-09-11 19:09:48 +02002685 v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002686 endtry
2687enddef
2688
Bram Moolenaare8211a32020-10-09 22:04:29 +02002689" Re-using spot for variable used in block
2690def Test_block_scoped_var()
2691 var lines =<< trim END
2692 vim9script
2693 def Func()
2694 var x = ['a', 'b', 'c']
2695 if 1
2696 var y = 'x'
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02002697 map(x, (_, _) => y)
Bram Moolenaare8211a32020-10-09 22:04:29 +02002698 endif
2699 var z = x
2700 assert_equal(['x', 'x', 'x'], z)
2701 enddef
2702 Func()
2703 END
2704 CheckScriptSuccess(lines)
2705enddef
2706
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002707def Test_reset_did_emsg()
2708 var lines =<< trim END
2709 @s = 'blah'
2710 au BufWinLeave * #
2711 def Func()
2712 var winid = popup_create('popup', {})
2713 exe '*s'
2714 popup_close(winid)
2715 enddef
2716 Func()
2717 END
2718 CheckScriptFailure(lines, 'E492:', 8)
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002719 delfunc! g:Func
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002720enddef
2721
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002722def Test_did_emsg_reset()
2723 # executing an autocommand resets did_emsg, this should not result in a
2724 # builtin function considered failing
2725 var lines =<< trim END
2726 vim9script
2727 au BufWinLeave * #
2728 def Func()
Bram Moolenaar767034c2021-04-09 17:24:52 +02002729 popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
Bram Moolenaar57f799e2020-12-12 20:42:19 +01002730 eval [][0]
2731 enddef
2732 nno <F3> <cmd>call <sid>Func()<cr>
2733 feedkeys("\<F3>\e", 'xt')
2734 END
2735 writefile(lines, 'XemsgReset')
2736 assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
2737 delete('XemsgReset')
2738 nunmap <F3>
2739 au! BufWinLeave
2740enddef
2741
Bram Moolenaar56602ba2020-12-05 21:22:08 +01002742def Test_abort_with_silent_call()
2743 var lines =<< trim END
2744 vim9script
2745 g:result = 'none'
2746 def Func()
2747 g:result += 3
2748 g:result = 'yes'
2749 enddef
2750 # error is silenced, but function aborts on error
2751 silent! Func()
2752 assert_equal('none', g:result)
2753 unlet g:result
2754 END
2755 CheckScriptSuccess(lines)
2756enddef
2757
Bram Moolenaarf665e972020-12-05 19:17:16 +01002758def Test_continues_with_silent_error()
2759 var lines =<< trim END
2760 vim9script
2761 g:result = 'none'
2762 def Func()
2763 silent! g:result += 3
2764 g:result = 'yes'
2765 enddef
2766 # error is silenced, function does not abort
2767 Func()
2768 assert_equal('yes', g:result)
2769 unlet g:result
2770 END
2771 CheckScriptSuccess(lines)
2772enddef
2773
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002774def Test_abort_even_with_silent()
2775 var lines =<< trim END
2776 vim9script
2777 g:result = 'none'
2778 def Func()
2779 eval {-> ''}() .. '' .. {}['X']
2780 g:result = 'yes'
2781 enddef
Bram Moolenaarf665e972020-12-05 19:17:16 +01002782 silent! Func()
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002783 assert_equal('none', g:result)
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002784 unlet g:result
2785 END
2786 CheckScriptSuccess(lines)
2787enddef
2788
Bram Moolenaarf665e972020-12-05 19:17:16 +01002789def Test_cmdmod_silent_restored()
2790 var lines =<< trim END
2791 vim9script
2792 def Func()
2793 g:result = 'none'
2794 silent! g:result += 3
2795 g:result = 'none'
2796 g:result += 3
2797 enddef
2798 Func()
2799 END
2800 # can't use CheckScriptFailure, it ignores the :silent!
2801 var fname = 'Xdefsilent'
2802 writefile(lines, fname)
2803 var caught = 'no'
2804 try
2805 exe 'source ' .. fname
2806 catch /E1030:/
2807 caught = 'yes'
2808 assert_match('Func, line 4', v:throwpoint)
2809 endtry
2810 assert_equal('yes', caught)
2811 delete(fname)
2812enddef
2813
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002814def Test_cmdmod_silent_nested()
2815 var lines =<< trim END
2816 vim9script
2817 var result = ''
2818
2819 def Error()
2820 result ..= 'Eb'
2821 eval [][0]
2822 result ..= 'Ea'
2823 enddef
2824
2825 def Crash()
2826 result ..= 'Cb'
2827 sil! Error()
2828 result ..= 'Ca'
2829 enddef
2830
2831 Crash()
2832 assert_equal('CbEbEaCa', result)
2833 END
2834 CheckScriptSuccess(lines)
2835enddef
2836
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002837def Test_dict_member_with_silent()
2838 var lines =<< trim END
2839 vim9script
2840 g:result = 'none'
2841 var d: dict<any>
2842 def Func()
2843 try
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002844 g:result = map([], (_, v) => ({}[v]))->join() .. d['']
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002845 catch
2846 endtry
2847 enddef
2848 silent! Func()
2849 assert_equal('0', g:result)
2850 unlet g:result
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002851 END
2852 CheckScriptSuccess(lines)
2853enddef
2854
Bram Moolenaarf9041332021-01-21 19:41:16 +01002855def Test_skip_cmds_with_silent()
2856 var lines =<< trim END
2857 vim9script
2858
2859 def Func(b: bool)
2860 Crash()
2861 enddef
2862
2863 def Crash()
2864 sil! :/not found/d _
2865 sil! :/not found/put _
2866 enddef
2867
2868 Func(true)
2869 END
2870 CheckScriptSuccess(lines)
2871enddef
2872
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01002873def Test_opfunc()
2874 nnoremap <F3> <cmd>set opfunc=Opfunc<cr>g@
2875 def g:Opfunc(_: any): string
2876 setline(1, 'ASDF')
2877 return ''
2878 enddef
2879 new
2880 setline(1, 'asdf')
2881 feedkeys("\<F3>$", 'x')
2882 assert_equal('ASDF', getline(1))
2883
2884 bwipe!
2885 nunmap <F3>
2886enddef
2887
Bram Moolenaar077a4232020-12-22 18:33:27 +01002888" this was crashing on exit
2889def Test_nested_lambda_in_closure()
2890 var lines =<< trim END
2891 vim9script
Bram Moolenaar227c58a2021-04-28 20:40:44 +02002892 command WriteDone writefile(['Done'], 'XnestedDone')
Bram Moolenaar077a4232020-12-22 18:33:27 +01002893 def Outer()
2894 def g:Inner()
2895 echo map([1, 2, 3], {_, v -> v + 1})
2896 enddef
2897 g:Inner()
2898 enddef
2899 defcompile
Bram Moolenaar227c58a2021-04-28 20:40:44 +02002900 # not reached
Bram Moolenaar077a4232020-12-22 18:33:27 +01002901 END
Bram Moolenaar227c58a2021-04-28 20:40:44 +02002902 if !RunVim([], lines, '--clean -c WriteDone -c quit')
Bram Moolenaar077a4232020-12-22 18:33:27 +01002903 return
2904 endif
2905 assert_equal(['Done'], readfile('XnestedDone'))
2906 delete('XnestedDone')
2907enddef
2908
Bram Moolenaar04947cc2021-03-06 19:26:46 +01002909def Test_check_func_arg_types()
2910 var lines =<< trim END
2911 vim9script
2912 def F1(x: string): string
2913 return x
2914 enddef
2915
2916 def F2(x: number): number
2917 return x + 1
2918 enddef
2919
2920 def G(g: func): dict<func>
2921 return {f: g}
2922 enddef
2923
2924 def H(d: dict<func>): string
2925 return d.f('a')
2926 enddef
2927 END
2928
2929 CheckScriptSuccess(lines + ['echo H(G(F1))'])
2930 CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
2931enddef
2932
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002933def Test_compile_error()
2934 var lines =<< trim END
2935 def g:Broken()
2936 echo 'a' + {}
2937 enddef
2938 call g:Broken()
2939 END
2940 # First call: compilation error
2941 CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
2942
2943 # Second call won't try compiling again
2944 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
Bram Moolenaar599410c2021-04-10 14:03:43 +02002945 delfunc g:Broken
2946
2947 # No error when compiling with :silent!
2948 lines =<< trim END
2949 def g:Broken()
2950 echo 'a' + []
2951 enddef
2952 silent! defcompile
2953 END
2954 CheckScriptSuccess(lines)
2955
2956 # Calling the function won't try compiling again
2957 assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
2958 delfunc g:Broken
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002959enddef
2960
Bram Moolenaar962c43b2021-04-10 17:18:09 +02002961def Test_ignored_argument()
2962 var lines =<< trim END
2963 vim9script
2964 def Ignore(_, _): string
2965 return 'yes'
2966 enddef
2967 assert_equal('yes', Ignore(1, 2))
2968
2969 func Ok(_)
2970 return a:_
2971 endfunc
2972 assert_equal('ok', Ok('ok'))
2973
2974 func Oktoo()
2975 let _ = 'too'
2976 return _
2977 endfunc
2978 assert_equal('too', Oktoo())
Bram Moolenaarda479c72021-04-10 21:01:38 +02002979
2980 assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
Bram Moolenaar962c43b2021-04-10 17:18:09 +02002981 END
2982 CheckScriptSuccess(lines)
2983
2984 lines =<< trim END
2985 def Ignore(_: string): string
2986 return _
2987 enddef
2988 defcompile
2989 END
2990 CheckScriptFailure(lines, 'E1181:', 1)
2991
2992 lines =<< trim END
2993 var _ = 1
2994 END
2995 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02002996
2997 lines =<< trim END
2998 var x = _
2999 END
3000 CheckDefAndScriptFailure(lines, 'E1181:', 1)
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003001enddef
3002
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02003003def Test_too_many_arguments()
3004 var lines =<< trim END
3005 echo [0, 1, 2]->map(() => 123)
3006 END
3007 CheckDefExecAndScriptFailure(lines, 'E1106: 2 arguments too many', 1)
3008
3009 lines =<< trim END
3010 echo [0, 1, 2]->map((_) => 123)
3011 END
3012 CheckDefExecAndScriptFailure(lines, 'E1106: One argument too many', 1)
3013enddef
Bram Moolenaar077a4232020-12-22 18:33:27 +01003014
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003015def Test_closing_brace_at_start_of_line()
3016 var lines =<< trim END
3017 def Func()
3018 enddef
3019 Func(
3020 )
3021 END
3022 call CheckDefAndScriptSuccess(lines)
3023enddef
3024
Bram Moolenaar20677332021-06-06 17:02:53 +02003025if has('python3')
3026 def Test_python3_heredoc()
3027 py3 << trim EOF
3028 import vim
3029 vim.vars['didit'] = 'yes'
3030 EOF
3031 assert_equal('yes', g:didit)
3032
3033 python3 << trim EOF
3034 import vim
3035 vim.vars['didit'] = 'again'
3036 EOF
3037 assert_equal('again', g:didit)
3038 enddef
3039endif
3040
3041" This messes up syntax highlight, keep near the end.
3042if has('lua')
3043 def Test_lua_heredoc()
3044 g:d = {}
3045 lua << trim EOF
3046 x = vim.eval('g:d')
3047 x['key'] = 'val'
3048 EOF
3049 assert_equal('val', g:d.key)
3050 enddef
3051endif
3052
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003053
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003054" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker