blob: a3a53e6b0fcf3ed343c439c15491f048fab2e492 [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
13 call assert_equal('yes', SomeFunc())
14endfunc
15
16def ReturnString(): string
17 return 'string'
18enddef
19
20def ReturnNumber(): number
21 return 123
22enddef
23
24let g:notNumber = 'string'
25
26def ReturnGlobal(): number
27 return g:notNumber
28enddef
29
30def Test_return_something()
31 assert_equal('string', ReturnString())
32 assert_equal(123, ReturnNumber())
Bram Moolenaard2c61702020-09-06 15:58:36 +020033 assert_fails('ReturnGlobal()', 'E1029: Expected number but got string')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020034enddef
35
Bram Moolenaarefd88552020-06-18 20:50:10 +020036def Test_missing_return()
37 CheckDefFailure(['def Missing(): number',
38 ' if g:cond',
39 ' echo "no return"',
40 ' else',
41 ' return 0',
42 ' endif'
43 'enddef'], 'E1027:')
44 CheckDefFailure(['def Missing(): number',
45 ' if g:cond',
46 ' return 1',
47 ' else',
48 ' echo "no return"',
49 ' endif'
50 'enddef'], 'E1027:')
51 CheckDefFailure(['def Missing(): number',
52 ' if g:cond',
53 ' return 1',
54 ' else',
55 ' return 2',
56 ' endif'
57 ' return 3'
58 'enddef'], 'E1095:')
59enddef
60
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020061let s:nothing = 0
62def ReturnNothing()
63 s:nothing = 1
64 if true
65 return
66 endif
67 s:nothing = 2
68enddef
69
70def Test_return_nothing()
71 ReturnNothing()
72 assert_equal(1, s:nothing)
73enddef
74
75func Increment()
76 let g:counter += 1
77endfunc
78
79def Test_call_ufunc_count()
80 g:counter = 1
81 Increment()
82 Increment()
83 Increment()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020084 # works with and without :call
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020085 assert_equal(4, g:counter)
86 call assert_equal(4, g:counter)
87 unlet g:counter
88enddef
89
90def MyVarargs(arg: string, ...rest: list<string>): string
91 let res = arg
92 for s in rest
93 res ..= ',' .. s
94 endfor
95 return res
96enddef
97
98def Test_call_varargs()
99 assert_equal('one', MyVarargs('one'))
100 assert_equal('one,two', MyVarargs('one', 'two'))
101 assert_equal('one,two,three', MyVarargs('one', 'two', 'three'))
102enddef
103
104def MyDefaultArgs(name = 'string'): string
105 return name
106enddef
107
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200108def MyDefaultSecond(name: string, second: bool = true): string
109 return second ? name : 'none'
110enddef
111
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200112def Test_call_default_args()
113 assert_equal('string', MyDefaultArgs())
114 assert_equal('one', MyDefaultArgs('one'))
Bram Moolenaard2c61702020-09-06 15:58:36 +0200115 assert_fails('MyDefaultArgs("one", "two")', 'E118:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200116
Bram Moolenaare30f64b2020-07-15 19:48:20 +0200117 assert_equal('test', MyDefaultSecond('test'))
118 assert_equal('test', MyDefaultSecond('test', true))
119 assert_equal('none', MyDefaultSecond('test', false))
120
Bram Moolenaar822ba242020-05-24 23:00:18 +0200121 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
122 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: argument 1: type mismatch, expected number but got string')
Bram Moolenaar04b12692020-05-04 23:24:44 +0200123enddef
124
125def Test_nested_function()
126 def Nested(arg: string): string
127 return 'nested ' .. arg
128 enddef
129 assert_equal('nested function', Nested('function'))
130
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +0200131 CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
132 CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
133
Bram Moolenaar04b12692020-05-04 23:24:44 +0200134 CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200135 CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
136 CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200137enddef
138
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200139func Test_call_default_args_from_func()
140 call assert_equal('string', MyDefaultArgs())
141 call assert_equal('one', MyDefaultArgs('one'))
142 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
143endfunc
144
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200145def Test_nested_global_function()
146 let lines =<< trim END
147 vim9script
148 def Outer()
149 def g:Inner(): string
150 return 'inner'
151 enddef
152 enddef
Bram Moolenaaraf8edbb2020-08-01 00:03:09 +0200153 defcompile
154 Outer()
155 assert_equal('inner', g:Inner())
156 delfunc g:Inner
157 Outer()
158 assert_equal('inner', g:Inner())
159 delfunc g:Inner
160 Outer()
161 assert_equal('inner', g:Inner())
162 delfunc g:Inner
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200163 END
164 CheckScriptSuccess(lines)
Bram Moolenaar2c79e9d2020-08-01 18:57:52 +0200165
166 lines =<< trim END
167 vim9script
168 def Outer()
169 def g:Inner(): string
170 return 'inner'
171 enddef
172 enddef
173 defcompile
174 Outer()
175 Outer()
176 END
177 CheckScriptFailure(lines, "E122:")
Bram Moolenaarad486a02020-08-01 23:22:18 +0200178
179 lines =<< trim END
180 vim9script
181 def Func()
182 echo 'script'
183 enddef
184 def Outer()
185 def Func()
186 echo 'inner'
187 enddef
188 enddef
189 defcompile
190 END
191 CheckScriptFailure(lines, "E1073:")
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200192enddef
193
Bram Moolenaar333894b2020-08-01 18:53:07 +0200194def Test_global_local_function()
195 let lines =<< trim END
196 vim9script
197 def g:Func(): string
198 return 'global'
199 enddef
200 def Func(): string
201 return 'local'
202 enddef
203 assert_equal('global', g:Func())
204 assert_equal('local', Func())
205 END
206 CheckScriptSuccess(lines)
Bram Moolenaar035d6e92020-08-11 22:30:42 +0200207
208 lines =<< trim END
209 vim9script
210 def g:Funcy()
211 echo 'funcy'
212 enddef
213 s:Funcy()
214 END
215 CheckScriptFailure(lines, 'E117:')
Bram Moolenaar333894b2020-08-01 18:53:07 +0200216enddef
217
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200218func TakesOneArg(arg)
219 echo a:arg
220endfunc
221
222def Test_call_wrong_args()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200223 CheckDefFailure(['TakesOneArg()'], 'E119:')
224 CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
225 CheckDefFailure(['bufnr(xxx)'], 'E1001:')
226 CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200227
228 let lines =<< trim END
229 vim9script
230 def Func(s: string)
231 echo s
232 enddef
233 Func([])
234 END
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200235 call CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected string but got list<unknown>', 5)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200236enddef
237
238" Default arg and varargs
239def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
240 let res = one .. ',' .. two
241 for s in rest
242 res ..= ',' .. s
243 endfor
244 return res
245enddef
246
247def Test_call_def_varargs()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200248 assert_fails('MyDefVarargs()', 'E119:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200249 assert_equal('one,foo', MyDefVarargs('one'))
250 assert_equal('one,two', MyDefVarargs('one', 'two'))
251 assert_equal('one,two,three', MyDefVarargs('one', 'two', 'three'))
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200252 CheckDefFailure(['MyDefVarargs("one", 22)'],
253 'E1013: argument 2: type mismatch, expected string but got number')
254 CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
255 'E1013: argument 3: type mismatch, expected string but got number')
256
257 let lines =<< trim END
258 vim9script
259 def Func(...l: list<string>)
260 echo l
261 enddef
262 Func('a', 'b', 'c')
263 END
264 CheckScriptSuccess(lines)
265
266 lines =<< trim END
267 vim9script
268 def Func(...l: list<string>)
269 echo l
270 enddef
271 Func()
272 END
273 CheckScriptSuccess(lines)
274
275 lines =<< trim END
276 vim9script
277 def Func(...l: list<string>)
278 echo l
279 enddef
280 Func(1, 2, 3)
281 END
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200282 CheckScriptFailure(lines, 'E1013: argument 1: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200283
284 lines =<< trim END
285 vim9script
286 def Func(...l: list<string>)
287 echo l
288 enddef
289 Func('a', 9)
290 END
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200291 CheckScriptFailure(lines, 'E1013: argument 2: type mismatch')
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200292
293 lines =<< trim END
294 vim9script
295 def Func(...l: list<string>)
296 echo l
297 enddef
298 Func(1, 'a')
299 END
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200300 CheckScriptFailure(lines, 'E1013: argument 1: type mismatch')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200301enddef
302
Bram Moolenaar575f24b2020-08-12 14:21:11 +0200303def Test_call_call()
304 let l = [3, 2, 1]
305 call('reverse', [l])
306 assert_equal([1, 2, 3], l)
307enddef
308
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200309let s:value = ''
310
311def FuncOneDefArg(opt = 'text')
312 s:value = opt
313enddef
314
315def FuncTwoDefArg(nr = 123, opt = 'text'): string
316 return nr .. opt
317enddef
318
319def FuncVarargs(...arg: list<string>): string
320 return join(arg, ',')
321enddef
322
323def Test_func_type_varargs()
324 let RefDefArg: func(?string)
325 RefDefArg = FuncOneDefArg
326 RefDefArg()
327 assert_equal('text', s:value)
328 RefDefArg('some')
329 assert_equal('some', s:value)
330
331 let RefDef2Arg: func(?number, ?string): string
332 RefDef2Arg = FuncTwoDefArg
333 assert_equal('123text', RefDef2Arg())
334 assert_equal('99text', RefDef2Arg(99))
335 assert_equal('77some', RefDef2Arg(77, 'some'))
336
Bram Moolenaard2c61702020-09-06 15:58:36 +0200337 CheckDefFailure(['let RefWrong: func(string?)'], 'E1010:')
338 CheckDefFailure(['let RefWrong: func(?string, string)'], 'E1007:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200339
340 let RefVarargs: func(...list<string>): string
341 RefVarargs = FuncVarargs
342 assert_equal('', RefVarargs())
343 assert_equal('one', RefVarargs('one'))
344 assert_equal('one,two', RefVarargs('one', 'two'))
345
Bram Moolenaard2c61702020-09-06 15:58:36 +0200346 CheckDefFailure(['let RefWrong: func(...list<string>, string)'], 'E110:')
347 CheckDefFailure(['let RefWrong: func(...list<string>, ?string)'], 'E110:')
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200348enddef
349
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200350" Only varargs
351def MyVarargsOnly(...args: list<string>): string
352 return join(args, ',')
353enddef
354
355def Test_call_varargs_only()
356 assert_equal('', MyVarargsOnly())
357 assert_equal('one', MyVarargsOnly('one'))
358 assert_equal('one,two', MyVarargsOnly('one', 'two'))
Bram Moolenaard2c61702020-09-06 15:58:36 +0200359 CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: argument 1: type mismatch, expected string but got number')
360 CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: argument 2: type mismatch, expected string but got number')
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200361enddef
362
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200363def Test_using_var_as_arg()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200364 writefile(['def Func(x: number)', 'let x = 234', 'enddef', 'defcompile'], 'Xdef')
365 assert_fails('so Xdef', 'E1006:')
366 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200367enddef
368
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200369def DictArg(arg: dict<string>)
370 arg['key'] = 'value'
371enddef
372
373def ListArg(arg: list<string>)
374 arg[0] = 'value'
375enddef
376
377def Test_assign_to_argument()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200378 # works for dict and list
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200379 let d: dict<string> = {}
380 DictArg(d)
381 assert_equal('value', d['key'])
382 let l: list<string> = []
383 ListArg(l)
384 assert_equal('value', l[0])
385
Bram Moolenaard2c61702020-09-06 15:58:36 +0200386 CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200387enddef
388
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200389def Test_call_func_defined_later()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200390 assert_equal('one', g:DefinedLater('one'))
391 assert_fails('NotDefined("one")', 'E117:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200392enddef
393
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200394func DefinedLater(arg)
395 return a:arg
396endfunc
397
398def Test_call_funcref()
399 assert_equal(3, g:SomeFunc('abc'))
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +0200400 assert_fails('NotAFunc()', 'E117:') # comment after call
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200401 assert_fails('g:NotAFunc()', 'E117:')
Bram Moolenaar2f1980f2020-07-22 19:30:06 +0200402
403 let lines =<< trim END
404 vim9script
405 def RetNumber(): number
406 return 123
407 enddef
408 let Funcref: func: number = function('RetNumber')
409 assert_equal(123, Funcref())
410 END
411 CheckScriptSuccess(lines)
Bram Moolenaar0f60e802020-07-22 20:16:11 +0200412
413 lines =<< trim END
414 vim9script
415 def RetNumber(): number
416 return 123
417 enddef
418 def Bar(F: func: number): number
419 return F()
420 enddef
421 let Funcref = function('RetNumber')
422 assert_equal(123, Bar(Funcref))
423 END
424 CheckScriptSuccess(lines)
Bram Moolenaarbfba8652020-07-23 20:09:10 +0200425
426 lines =<< trim END
427 vim9script
428 def UseNumber(nr: number)
429 echo nr
430 enddef
431 let Funcref: func(number) = function('UseNumber')
432 Funcref(123)
433 END
434 CheckScriptSuccess(lines)
Bram Moolenaarb8070e32020-07-23 20:56:04 +0200435
436 lines =<< trim END
437 vim9script
438 def UseNumber(nr: number)
439 echo nr
440 enddef
441 let Funcref: func(string) = function('UseNumber')
442 END
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200443 CheckScriptFailure(lines, 'E1012: type mismatch, expected func(string) but got func(number)')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200444
445 lines =<< trim END
446 vim9script
447 def EchoNr(nr = 34)
448 g:echo = nr
449 enddef
450 let Funcref: func(?number) = function('EchoNr')
451 Funcref()
452 assert_equal(34, g:echo)
453 Funcref(123)
454 assert_equal(123, g:echo)
455 END
456 CheckScriptSuccess(lines)
Bram Moolenaarace61322020-07-26 18:16:58 +0200457
458 lines =<< trim END
459 vim9script
460 def EchoList(...l: list<number>)
461 g:echo = l
462 enddef
463 let Funcref: func(...list<number>) = function('EchoList')
464 Funcref()
465 assert_equal([], g:echo)
466 Funcref(1, 2, 3)
467 assert_equal([1, 2, 3], g:echo)
468 END
469 CheckScriptSuccess(lines)
Bram Moolenaar01865ad2020-07-26 18:33:09 +0200470
471 lines =<< trim END
472 vim9script
473 def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
474 g:optarg = opt
475 g:listarg = l
476 return nr
477 enddef
478 let Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
479 assert_equal(10, Funcref(10))
480 assert_equal(12, g:optarg)
481 assert_equal([], g:listarg)
482
483 assert_equal(11, Funcref(11, 22))
484 assert_equal(22, g:optarg)
485 assert_equal([], g:listarg)
486
487 assert_equal(17, Funcref(17, 18, 1, 2, 3))
488 assert_equal(18, g:optarg)
489 assert_equal([1, 2, 3], g:listarg)
490 END
491 CheckScriptSuccess(lines)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200492enddef
493
494let SomeFunc = function('len')
495let NotAFunc = 'text'
496
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +0200497def CombineFuncrefTypes()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200498 # same arguments, different return type
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +0200499 let Ref1: func(bool): string
500 let Ref2: func(bool): number
501 let Ref3: func(bool): any
502 Ref3 = g:cond ? Ref1 : Ref2
503
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200504 # different number of arguments
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +0200505 let Refa1: func(bool): number
506 let Refa2: func(bool, number): number
507 let Refa3: func: number
508 Refa3 = g:cond ? Refa1 : Refa2
509
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200510 # different argument types
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +0200511 let Refb1: func(bool, string): number
512 let Refb2: func(string, number): number
513 let Refb3: func(any, any): number
514 Refb3 = g:cond ? Refb1 : Refb2
515enddef
516
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200517def FuncWithForwardCall()
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200518 return g:DefinedEvenLater("yes")
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200519enddef
520
521def DefinedEvenLater(arg: string): string
522 return arg
523enddef
524
525def Test_error_in_nested_function()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200526 # Error in called function requires unwinding the call stack.
Bram Moolenaard2c61702020-09-06 15:58:36 +0200527 assert_fails('FuncWithForwardCall()', 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200528enddef
529
530def Test_return_type_wrong()
Bram Moolenaar5a849da2020-08-08 16:47:30 +0200531 CheckScriptFailure([
532 'def Func(): number',
533 'return "a"',
534 'enddef',
535 'defcompile'], 'expected number but got string')
536 CheckScriptFailure([
537 'def Func(): string',
538 'return 1',
539 'enddef',
540 'defcompile'], 'expected string but got number')
541 CheckScriptFailure([
542 'def Func(): void',
543 'return "a"',
544 'enddef',
545 'defcompile'],
546 'E1096: Returning a value in a function without a return type')
547 CheckScriptFailure([
548 'def Func()',
549 'return "a"',
550 'enddef',
551 'defcompile'],
552 'E1096: Returning a value in a function without a return type')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200553
Bram Moolenaar5a849da2020-08-08 16:47:30 +0200554 CheckScriptFailure([
555 'def Func(): number',
556 'return',
557 'enddef',
558 'defcompile'], 'E1003:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200559
560 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
561 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200562 CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
Bram Moolenaar5a849da2020-08-08 16:47:30 +0200563
564 CheckScriptFailure([
565 'vim9script',
566 'def FuncB()',
567 ' return 123',
568 'enddef',
569 'def FuncA()',
570 ' FuncB()',
571 'enddef',
572 'defcompile'], 'E1096:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200573enddef
574
575def Test_arg_type_wrong()
576 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200577 CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200578 CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
Bram Moolenaar6e949782020-04-13 17:21:00 +0200579 CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200580enddef
581
582def Test_vim9script_call()
583 let lines =<< trim END
584 vim9script
585 let var = ''
586 def MyFunc(arg: string)
587 var = arg
588 enddef
589 MyFunc('foobar')
590 assert_equal('foobar', var)
591
592 let str = 'barfoo'
593 str->MyFunc()
594 assert_equal('barfoo', var)
595
Bram Moolenaar67979662020-06-20 22:50:47 +0200596 g:value = 'value'
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200597 g:value->MyFunc()
598 assert_equal('value', var)
599
600 let listvar = []
601 def ListFunc(arg: list<number>)
602 listvar = arg
603 enddef
604 [1, 2, 3]->ListFunc()
605 assert_equal([1, 2, 3], listvar)
606
607 let dictvar = {}
608 def DictFunc(arg: dict<number>)
609 dictvar = arg
610 enddef
611 {'a': 1, 'b': 2}->DictFunc()
612 assert_equal(#{a: 1, b: 2}, dictvar)
613 def CompiledDict()
614 {'a': 3, 'b': 4}->DictFunc()
615 enddef
616 CompiledDict()
617 assert_equal(#{a: 3, b: 4}, dictvar)
618
619 #{a: 3, b: 4}->DictFunc()
620 assert_equal(#{a: 3, b: 4}, dictvar)
621
622 ('text')->MyFunc()
623 assert_equal('text', var)
624 ("some")->MyFunc()
625 assert_equal('some', var)
Bram Moolenaare6b53242020-07-01 17:28:33 +0200626
Bram Moolenaar13e12b82020-07-24 18:47:22 +0200627 # line starting with single quote is not a mark
Bram Moolenaar10409562020-07-29 20:00:38 +0200628 # line starting with double quote can be a method call
Bram Moolenaar3d48e252020-07-15 14:15:52 +0200629 'asdfasdf'->MyFunc()
630 assert_equal('asdfasdf', var)
Bram Moolenaar10409562020-07-29 20:00:38 +0200631 "xyz"->MyFunc()
632 assert_equal('xyz', var)
Bram Moolenaar3d48e252020-07-15 14:15:52 +0200633
634 def UseString()
635 'xyork'->MyFunc()
636 enddef
637 UseString()
638 assert_equal('xyork', var)
639
Bram Moolenaar10409562020-07-29 20:00:38 +0200640 def UseString2()
641 "knife"->MyFunc()
642 enddef
643 UseString2()
644 assert_equal('knife', var)
645
Bram Moolenaar13e12b82020-07-24 18:47:22 +0200646 # prepending a colon makes it a mark
647 new
648 setline(1, ['aaa', 'bbb', 'ccc'])
649 normal! 3Gmt1G
650 :'t
651 assert_equal(3, getcurpos()[1])
652 bwipe!
653
Bram Moolenaare6b53242020-07-01 17:28:33 +0200654 MyFunc(
655 'continued'
656 )
657 assert_equal('continued',
658 var
659 )
660
661 call MyFunc(
662 'more'
663 ..
664 'lines'
665 )
666 assert_equal(
667 'morelines',
668 var)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200669 END
670 writefile(lines, 'Xcall.vim')
671 source Xcall.vim
672 delete('Xcall.vim')
673enddef
674
675def Test_vim9script_call_fail_decl()
676 let lines =<< trim END
677 vim9script
678 let var = ''
679 def MyFunc(arg: string)
680 let var = 123
681 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +0200682 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200683 END
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +0200684 CheckScriptFailure(lines, 'E1054:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200685enddef
686
Bram Moolenaar65b95452020-07-19 14:03:09 +0200687def Test_vim9script_call_fail_type()
688 let lines =<< trim END
689 vim9script
690 def MyFunc(arg: string)
691 echo arg
692 enddef
693 MyFunc(1234)
694 END
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200695 CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected string but got number')
Bram Moolenaar65b95452020-07-19 14:03:09 +0200696enddef
697
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200698def Test_vim9script_call_fail_const()
699 let lines =<< trim END
700 vim9script
701 const var = ''
702 def MyFunc(arg: string)
703 var = 'asdf'
704 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +0200705 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200706 END
707 writefile(lines, 'Xcall_const.vim')
708 assert_fails('source Xcall_const.vim', 'E46:')
709 delete('Xcall_const.vim')
710enddef
711
712" Test that inside :function a Python function can be defined, :def is not
713" recognized.
714func Test_function_python()
715 CheckFeature python3
716 let py = 'python3'
717 execute py "<< EOF"
718def do_something():
719 return 1
720EOF
721endfunc
722
723def Test_delfunc()
724 let lines =<< trim END
725 vim9script
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200726 def g:GoneSoon()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200727 echo 'hello'
728 enddef
729
730 def CallGoneSoon()
731 GoneSoon()
732 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +0200733 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200734
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200735 delfunc g:GoneSoon
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200736 CallGoneSoon()
737 END
738 writefile(lines, 'XToDelFunc')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200739 assert_fails('so XToDelFunc', 'E933:')
740 assert_fails('so XToDelFunc', 'E933:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200741
742 delete('XToDelFunc')
743enddef
744
745def Test_redef_failure()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200746 writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200747 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +0200748 writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200749 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +0200750 writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
751 assert_fails('so Xdef', 'E1027:')
752 writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200753 so Xdef
Bram Moolenaard2c61702020-09-06 15:58:36 +0200754 delete('Xdef')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200755
Bram Moolenaard2c61702020-09-06 15:58:36 +0200756 assert_equal(0, g:Func0())
757 assert_equal('Func1', g:Func1())
758 assert_equal('Func2', g:Func2())
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200759
760 delfunc! Func0
761 delfunc! Func1
762 delfunc! Func2
763enddef
764
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200765def Test_vim9script_func()
766 let lines =<< trim END
767 vim9script
768 func Func(arg)
769 echo a:arg
770 endfunc
771 Func('text')
772 END
773 writefile(lines, 'XVim9Func')
774 so XVim9Func
775
776 delete('XVim9Func')
777enddef
778
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200779" Test for internal functions returning different types
780func Test_InternalFuncRetType()
781 let lines =<< trim END
782 def RetFloat(): float
783 return ceil(1.456)
784 enddef
785
786 def RetListAny(): list<any>
Bram Moolenaar17a836c2020-08-12 17:35:58 +0200787 return items({'k': 'v'})
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200788 enddef
789
790 def RetListString(): list<string>
791 return split('a:b:c', ':')
792 enddef
793
794 def RetListDictAny(): list<dict<any>>
795 return getbufinfo()
796 enddef
797
798 def RetDictNumber(): dict<number>
799 return wordcount()
800 enddef
801
802 def RetDictString(): dict<string>
803 return environ()
804 enddef
805 END
806 call writefile(lines, 'Xscript')
807 source Xscript
808
809 call assert_equal(2.0, RetFloat())
810 call assert_equal([['k', 'v']], RetListAny())
811 call assert_equal(['a', 'b', 'c'], RetListString())
812 call assert_notequal([], RetListDictAny())
813 call assert_notequal({}, RetDictNumber())
814 call assert_notequal({}, RetDictString())
815 call delete('Xscript')
816endfunc
817
818" Test for passing too many or too few arguments to internal functions
819func Test_internalfunc_arg_error()
820 let l =<< trim END
821 def! FArgErr(): float
822 return ceil(1.1, 2)
823 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +0200824 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200825 END
826 call writefile(l, 'Xinvalidarg')
827 call assert_fails('so Xinvalidarg', 'E118:')
828 let l =<< trim END
829 def! FArgErr(): float
830 return ceil()
831 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +0200832 defcompile
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200833 END
834 call writefile(l, 'Xinvalidarg')
835 call assert_fails('so Xinvalidarg', 'E119:')
836 call delete('Xinvalidarg')
837endfunc
838
839let s:funcResult = 0
840
841def FuncNoArgNoRet()
Bram Moolenaar53900992020-08-22 19:02:02 +0200842 s:funcResult = 11
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200843enddef
844
845def FuncNoArgRetNumber(): number
Bram Moolenaar53900992020-08-22 19:02:02 +0200846 s:funcResult = 22
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200847 return 1234
848enddef
849
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200850def FuncNoArgRetString(): string
Bram Moolenaar53900992020-08-22 19:02:02 +0200851 s:funcResult = 45
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200852 return 'text'
853enddef
854
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200855def FuncOneArgNoRet(arg: number)
Bram Moolenaar53900992020-08-22 19:02:02 +0200856 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200857enddef
858
859def FuncOneArgRetNumber(arg: number): number
Bram Moolenaar53900992020-08-22 19:02:02 +0200860 s:funcResult = arg
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200861 return arg
862enddef
863
Bram Moolenaar08938ee2020-04-11 23:17:17 +0200864def FuncTwoArgNoRet(one: bool, two: number)
Bram Moolenaar53900992020-08-22 19:02:02 +0200865 s:funcResult = two
Bram Moolenaar08938ee2020-04-11 23:17:17 +0200866enddef
867
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200868def FuncOneArgRetString(arg: string): string
869 return arg
870enddef
871
Bram Moolenaar89228602020-04-05 22:14:54 +0200872def FuncOneArgRetAny(arg: any): any
873 return arg
874enddef
875
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200876def Test_func_type()
877 let Ref1: func()
Bram Moolenaar53900992020-08-22 19:02:02 +0200878 s:funcResult = 0
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200879 Ref1 = FuncNoArgNoRet
880 Ref1()
Bram Moolenaar53900992020-08-22 19:02:02 +0200881 assert_equal(11, s:funcResult)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200882
883 let Ref2: func
Bram Moolenaar53900992020-08-22 19:02:02 +0200884 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +0200885 Ref2 = FuncNoArgNoRet
886 Ref2()
Bram Moolenaar53900992020-08-22 19:02:02 +0200887 assert_equal(11, s:funcResult)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200888
Bram Moolenaar53900992020-08-22 19:02:02 +0200889 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +0200890 Ref2 = FuncOneArgNoRet
891 Ref2(12)
Bram Moolenaar53900992020-08-22 19:02:02 +0200892 assert_equal(12, s:funcResult)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200893
Bram Moolenaar53900992020-08-22 19:02:02 +0200894 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +0200895 Ref2 = FuncNoArgRetNumber
896 assert_equal(1234, Ref2())
Bram Moolenaar53900992020-08-22 19:02:02 +0200897 assert_equal(22, s:funcResult)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200898
Bram Moolenaar53900992020-08-22 19:02:02 +0200899 s:funcResult = 0
Bram Moolenaar4c683752020-04-05 21:38:23 +0200900 Ref2 = FuncOneArgRetNumber
901 assert_equal(13, Ref2(13))
Bram Moolenaar53900992020-08-22 19:02:02 +0200902 assert_equal(13, s:funcResult)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200903enddef
904
Bram Moolenaar9978d472020-07-05 16:01:56 +0200905def Test_repeat_return_type()
906 let res = 0
907 for n in repeat([1], 3)
908 res += n
909 endfor
910 assert_equal(3, res)
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200911
912 res = 0
913 for n in add([1, 2], 3)
914 res += n
915 endfor
916 assert_equal(6, res)
Bram Moolenaar9978d472020-07-05 16:01:56 +0200917enddef
918
Bram Moolenaar846178a2020-07-05 17:04:13 +0200919def Test_argv_return_type()
920 next fileone filetwo
921 let res = ''
922 for name in argv()
923 res ..= name
924 endfor
925 assert_equal('fileonefiletwo', res)
926enddef
927
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200928def Test_func_type_part()
929 let RefVoid: func: void
930 RefVoid = FuncNoArgNoRet
931 RefVoid = FuncOneArgNoRet
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200932 CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: type mismatch, expected func() but got func(): number')
933 CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: type mismatch, expected func() but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200934
935 let RefAny: func(): any
936 RefAny = FuncNoArgRetNumber
937 RefAny = FuncNoArgRetString
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200938 CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: type mismatch, expected func(): any but got func()')
939 CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: type mismatch, expected func(): any but got func(number)')
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200940
941 let RefNr: func: number
942 RefNr = FuncNoArgRetNumber
943 RefNr = FuncOneArgRetNumber
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200944 CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: type mismatch, expected func(): number but got func()')
945 CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: type mismatch, expected func(): number but got func(): string')
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200946
947 let RefStr: func: string
948 RefStr = FuncNoArgRetString
949 RefStr = FuncOneArgRetString
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200950 CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: type mismatch, expected func(): string but got func()')
951 CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: type mismatch, expected func(): string but got func(): number')
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200952enddef
953
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200954def Test_func_type_fails()
955 CheckDefFailure(['let ref1: func()'], 'E704:')
956
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200957 CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: type mismatch, expected func() but got func(): number')
958 CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: type mismatch, expected func() but got func(number)')
959 CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: type mismatch, expected func() but got func(number): number')
960 CheckDefFailure(['let Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: type mismatch, expected func(bool) but got func(bool, number)')
961 CheckDefFailure(['let Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: type mismatch, expected func(?bool) but got func(bool, number)')
962 CheckDefFailure(['let Ref1: func(...bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: type mismatch, expected func(...bool) but got func(bool, number)')
Bram Moolenaar08938ee2020-04-11 23:17:17 +0200963
Bram Moolenaard2c61702020-09-06 15:58:36 +0200964 CheckDefFailure(['let RefWrong: func(string ,number)'], 'E1068:')
965 CheckDefFailure(['let RefWrong: func(string,number)'], 'E1069:')
966 CheckDefFailure(['let RefWrong: func(bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool)'], 'E1005:')
967 CheckDefFailure(['let RefWrong: func(bool):string'], 'E1069:')
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200968enddef
969
Bram Moolenaar89228602020-04-05 22:14:54 +0200970def Test_func_return_type()
971 let nr: number
972 nr = FuncNoArgRetNumber()
973 assert_equal(1234, nr)
974
975 nr = FuncOneArgRetAny(122)
976 assert_equal(122, nr)
977
978 let str: string
979 str = FuncOneArgRetAny('yes')
980 assert_equal('yes', str)
981
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200982 CheckDefFailure(['let str: string', 'str = FuncNoArgRetNumber()'], 'E1012: type mismatch, expected string but got number')
Bram Moolenaar89228602020-04-05 22:14:54 +0200983enddef
984
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200985def MultiLine(
986 arg1: string,
987 arg2 = 1234,
988 ...rest: list<string>
989 ): string
990 return arg1 .. arg2 .. join(rest, '-')
991enddef
992
Bram Moolenaar2c330432020-04-13 14:41:35 +0200993def MultiLineComment(
994 arg1: string, # comment
995 arg2 = 1234, # comment
996 ...rest: list<string> # comment
997 ): string # comment
998 return arg1 .. arg2 .. join(rest, '-')
999enddef
1000
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001001def Test_multiline()
1002 assert_equal('text1234', MultiLine('text'))
1003 assert_equal('text777', MultiLine('text', 777))
1004 assert_equal('text777one', MultiLine('text', 777, 'one'))
1005 assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two'))
1006enddef
1007
Bram Moolenaar23e03252020-04-12 22:22:31 +02001008func Test_multiline_not_vim9()
1009 call assert_equal('text1234', MultiLine('text'))
1010 call assert_equal('text777', MultiLine('text', 777))
1011 call assert_equal('text777one', MultiLine('text', 777, 'one'))
1012 call assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two'))
1013endfunc
1014
Bram Moolenaar5e774c72020-04-12 21:53:00 +02001015
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001016" When using CheckScriptFailure() for the below test, E1010 is generated instead
1017" of E1056.
1018func Test_E1056_1059()
1019 let caught_1056 = 0
1020 try
1021 def F():
1022 return 1
1023 enddef
1024 catch /E1056:/
1025 let caught_1056 = 1
1026 endtry
1027 call assert_equal(1, caught_1056)
1028
1029 let caught_1059 = 0
1030 try
1031 def F5(items : list)
1032 echo 'a'
1033 enddef
1034 catch /E1059:/
1035 let caught_1059 = 1
1036 endtry
1037 call assert_equal(1, caught_1059)
1038endfunc
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001039
Bram Moolenaar015f4262020-05-05 21:25:22 +02001040func DelMe()
1041 echo 'DelMe'
1042endfunc
1043
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001044def Test_error_reporting()
1045 # comment lines at the start of the function
1046 let lines =<< trim END
1047 " comment
1048 def Func()
1049 # comment
1050 # comment
1051 invalid
1052 enddef
1053 defcompile
1054 END
1055 call writefile(lines, 'Xdef')
1056 try
1057 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001058 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001059 catch /E476:/
1060 assert_match('Invalid command: invalid', v:exception)
1061 assert_match(', line 3$', v:throwpoint)
1062 endtry
1063
1064 # comment lines after the start of the function
1065 lines =<< trim END
1066 " comment
1067 def Func()
1068 let x = 1234
1069 # comment
1070 # comment
1071 invalid
1072 enddef
1073 defcompile
1074 END
1075 call writefile(lines, 'Xdef')
1076 try
1077 source Xdef
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001078 assert_report('should have failed')
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001079 catch /E476:/
1080 assert_match('Invalid command: invalid', v:exception)
1081 assert_match(', line 4$', v:throwpoint)
1082 endtry
1083
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001084 lines =<< trim END
1085 vim9script
1086 def Func()
1087 let db = #{foo: 1, bar: 2}
1088 # comment
1089 let x = db.asdf
1090 enddef
1091 defcompile
1092 Func()
1093 END
1094 call writefile(lines, 'Xdef')
1095 try
1096 source Xdef
1097 assert_report('should have failed')
1098 catch /E716:/
1099 assert_match('_Func, line 3$', v:throwpoint)
1100 endtry
1101
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02001102 call delete('Xdef')
1103enddef
1104
Bram Moolenaar015f4262020-05-05 21:25:22 +02001105def Test_deleted_function()
1106 CheckDefExecFailure([
1107 'let RefMe: func = function("g:DelMe")',
1108 'delfunc g:DelMe',
1109 'echo RefMe()'], 'E117:')
1110enddef
1111
1112def Test_unknown_function()
1113 CheckDefExecFailure([
1114 'let Ref: func = function("NotExist")',
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001115 'delfunc g:NotExist'], 'E700:')
Bram Moolenaar015f4262020-05-05 21:25:22 +02001116enddef
1117
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001118def RefFunc(Ref: func(string): string): string
1119 return Ref('more')
1120enddef
1121
1122def Test_closure_simple()
1123 let local = 'some '
1124 assert_equal('some more', RefFunc({s -> local .. s}))
1125enddef
1126
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001127def MakeRef()
1128 let local = 'some '
1129 g:Ref = {s -> local .. s}
1130enddef
1131
1132def Test_closure_ref_after_return()
1133 MakeRef()
1134 assert_equal('some thing', g:Ref('thing'))
1135 unlet g:Ref
1136enddef
1137
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001138def MakeTwoRefs()
1139 let local = ['some']
1140 g:Extend = {s -> local->add(s)}
1141 g:Read = {-> local}
1142enddef
1143
1144def Test_closure_two_refs()
1145 MakeTwoRefs()
1146 assert_equal('some', join(g:Read(), ' '))
1147 g:Extend('more')
1148 assert_equal('some more', join(g:Read(), ' '))
1149 g:Extend('even')
1150 assert_equal('some more even', join(g:Read(), ' '))
1151
1152 unlet g:Extend
1153 unlet g:Read
1154enddef
1155
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001156def ReadRef(Ref: func(): list<string>): string
1157 return join(Ref(), ' ')
1158enddef
1159
1160def ExtendRef(Ref: func(string), add: string)
1161 Ref(add)
1162enddef
1163
1164def Test_closure_two_indirect_refs()
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001165 MakeTwoRefs()
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001166 assert_equal('some', ReadRef(g:Read))
1167 ExtendRef(g:Extend, 'more')
1168 assert_equal('some more', ReadRef(g:Read))
1169 ExtendRef(g:Extend, 'even')
1170 assert_equal('some more even', ReadRef(g:Read))
1171
1172 unlet g:Extend
1173 unlet g:Read
1174enddef
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001175
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001176def MakeArgRefs(theArg: string)
1177 let local = 'loc_val'
1178 g:UseArg = {s -> theArg .. '/' .. local .. '/' .. s}
1179enddef
1180
1181def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
1182 let local = 'the_loc'
1183 g:UseVararg = {s -> theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)}
1184enddef
1185
1186def Test_closure_using_argument()
1187 MakeArgRefs('arg_val')
1188 assert_equal('arg_val/loc_val/call_val', g:UseArg('call_val'))
1189
1190 MakeArgRefsVarargs('arg_val', 'one', 'two')
1191 assert_equal('arg_val/the_loc/call_val/one two', g:UseVararg('call_val'))
1192
1193 unlet g:UseArg
1194 unlet g:UseVararg
1195enddef
1196
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001197def MakeGetAndAppendRefs()
1198 let local = 'a'
1199
1200 def Append(arg: string)
1201 local ..= arg
1202 enddef
1203 g:Append = Append
1204
1205 def Get(): string
1206 return local
1207 enddef
1208 g:Get = Get
1209enddef
1210
1211def Test_closure_append_get()
1212 MakeGetAndAppendRefs()
1213 assert_equal('a', g:Get())
1214 g:Append('-b')
1215 assert_equal('a-b', g:Get())
1216 g:Append('-c')
1217 assert_equal('a-b-c', g:Get())
1218
1219 unlet g:Append
1220 unlet g:Get
1221enddef
1222
Bram Moolenaar04b12692020-05-04 23:24:44 +02001223def Test_nested_closure()
1224 let local = 'text'
1225 def Closure(arg: string): string
1226 return local .. arg
1227 enddef
1228 assert_equal('text!!!', Closure('!!!'))
1229enddef
1230
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001231func GetResult(Ref)
1232 return a:Ref('some')
1233endfunc
1234
1235def Test_call_closure_not_compiled()
1236 let text = 'text'
1237 g:Ref = {s -> s .. text}
1238 assert_equal('sometext', GetResult(g:Ref))
1239enddef
1240
Bram Moolenaar865af6b2020-06-18 18:45:49 +02001241def Test_sort_return_type()
1242 let res: list<number>
1243 res = [1, 2, 3]->sort()
1244enddef
1245
Bram Moolenaarf151ad12020-06-30 13:38:01 +02001246def Test_getqflist_return_type()
1247 let l = getqflist()
1248 assert_equal([], l)
1249
1250 let d = getqflist(#{items: 0})
1251 assert_equal(#{items: []}, d)
1252enddef
1253
1254def Test_getloclist_return_type()
1255 let l = getloclist(1)
1256 assert_equal([], l)
1257
1258 let d = getloclist(1, #{items: 0})
1259 assert_equal(#{items: []}, d)
1260enddef
1261
Bram Moolenaara66ba012020-07-05 18:41:08 +02001262def Test_copy_return_type()
1263 let l = copy([1, 2, 3])
1264 let res = 0
1265 for n in l
1266 res += n
1267 endfor
1268 assert_equal(6, res)
1269
1270 let dl = deepcopy([1, 2, 3])
1271 res = 0
1272 for n in dl
1273 res += n
1274 endfor
1275 assert_equal(6, res)
Bram Moolenaar44b4a242020-09-05 17:18:28 +02001276
1277 dl = deepcopy([1, 2, 3], true)
Bram Moolenaara66ba012020-07-05 18:41:08 +02001278enddef
1279
Bram Moolenaarb3c019c2020-07-05 20:08:39 +02001280def Test_extend_return_type()
1281 let l = extend([1, 2], [3])
1282 let res = 0
1283 for n in l
1284 res += n
1285 endfor
1286 assert_equal(6, res)
1287enddef
1288
Bram Moolenaar2df47312020-09-05 17:30:44 +02001289def Test_garbagecollect()
1290 garbagecollect(true)
1291enddef
1292
Bram Moolenaar252e88a2020-07-05 20:47:18 +02001293def Test_insert_return_type()
1294 let l = insert([2, 1], 3)
1295 let res = 0
1296 for n in l
1297 res += n
1298 endfor
1299 assert_equal(6, res)
1300enddef
1301
Bram Moolenaar32f335f2020-08-14 18:56:45 +02001302def Test_keys_return_type()
1303 const var: list<string> = #{a: 1, b: 2}->keys()
1304 assert_equal(['a', 'b'], var)
1305enddef
1306
Bram Moolenaar67627352020-07-05 21:10:24 +02001307def Test_reverse_return_type()
1308 let l = reverse([1, 2, 3])
1309 let res = 0
1310 for n in l
1311 res += n
1312 endfor
1313 assert_equal(6, res)
1314enddef
1315
Bram Moolenaarad7c2492020-07-05 20:55:29 +02001316def Test_remove_return_type()
1317 let l = remove(#{one: [1, 2], two: [3, 4]}, 'one')
1318 let res = 0
1319 for n in l
1320 res += n
1321 endfor
1322 assert_equal(3, res)
1323enddef
1324
Bram Moolenaar0d94ad62020-07-05 20:16:41 +02001325def Test_filter_return_type()
1326 let l = filter([1, 2, 3], {-> 1})
1327 let res = 0
1328 for n in l
1329 res += n
1330 endfor
1331 assert_equal(6, res)
1332enddef
1333
Bram Moolenaarf39397e2020-08-17 22:21:36 +02001334def Test_bufnr()
1335 let buf = bufnr()
1336 assert_equal(buf, bufnr('%'))
Bram Moolenaarfe136c92020-09-04 18:35:26 +02001337
1338 buf = bufnr('Xdummy', true)
1339 assert_notequal(-1, buf)
1340 exe 'bwipe! ' .. buf
Bram Moolenaarf39397e2020-08-17 22:21:36 +02001341enddef
1342
Bram Moolenaarec65d772020-08-20 22:29:12 +02001343def Test_col()
1344 new
1345 setline(1, 'asdf')
1346 assert_equal(5, col([1, '$']))
1347enddef
1348
Bram Moolenaar24f77502020-09-04 19:50:57 +02001349def Test_char2nr()
1350 assert_equal(12354, char2nr('あ', true))
1351enddef
1352
Bram Moolenaar3d945cc2020-08-06 21:26:59 +02001353def Test_getreg_return_type()
1354 let s1: string = getreg('"')
1355 let s2: string = getreg('"', 1)
1356 let s3: list<string> = getreg('"', 1, 1)
1357enddef
1358
Bram Moolenaarf1a23682020-07-13 18:55:48 +02001359def Wrong_dict_key_type(items: list<number>): list<number>
1360 return filter(items, {_, val -> get({val: 1}, 'x')})
1361enddef
1362
1363def Test_wrong_dict_key_type()
1364 assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1029:')
1365enddef
1366
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001367def Line_continuation_in_def(dir: string = ''): string
1368 let path: string = empty(dir)
1369 \ ? 'empty'
1370 \ : 'full'
1371 return path
1372enddef
1373
1374def Test_line_continuation_in_def()
1375 assert_equal('full', Line_continuation_in_def('.'))
1376enddef
1377
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001378def Line_continuation_in_lambda(): list<number>
1379 let x = range(97, 100)
Bram Moolenaar914e7ea2020-07-11 15:20:48 +02001380 ->map({_, v -> nr2char(v)
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001381 ->toupper()})
1382 ->reverse()
1383 return x
1384enddef
1385
1386def Test_line_continuation_in_lambda()
1387 assert_equal(['D', 'C', 'B', 'A'], Line_continuation_in_lambda())
1388enddef
1389
Bram Moolenaar8f510af2020-07-05 18:48:23 +02001390func Test_silent_echo()
Bram Moolenaar47e7d702020-07-05 18:18:42 +02001391 CheckScreendump
1392
1393 let lines =<< trim END
1394 vim9script
1395 def EchoNothing()
1396 silent echo ''
1397 enddef
1398 defcompile
1399 END
Bram Moolenaar8f510af2020-07-05 18:48:23 +02001400 call writefile(lines, 'XTest_silent_echo')
Bram Moolenaar47e7d702020-07-05 18:18:42 +02001401
1402 " Check that the balloon shows up after a mouse move
1403 let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
Bram Moolenaar8f510af2020-07-05 18:48:23 +02001404 call term_sendkeys(buf, ":abc")
Bram Moolenaar47e7d702020-07-05 18:18:42 +02001405 call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
1406
1407 " clean up
1408 call StopVimInTerminal(buf)
1409 call delete('XTest_silent_echo')
Bram Moolenaar8f510af2020-07-05 18:48:23 +02001410endfunc
Bram Moolenaar47e7d702020-07-05 18:18:42 +02001411
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001412""""""" builtin functions that behave differently in Vim9
Bram Moolenaare15eebd2020-08-18 19:11:38 +02001413
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001414def Test_bufname()
1415 split SomeFile
1416 assert_equal('SomeFile', bufname('%'))
1417 edit OtherFile
1418 assert_equal('SomeFile', bufname('#'))
1419 close
Bram Moolenaar191929b2020-08-19 21:20:49 +02001420enddef
1421
Bram Moolenaara5d38412020-09-02 21:02:35 +02001422def Test_bufwinid()
1423 let origwin = win_getid()
1424 below split SomeFile
1425 let SomeFileID = win_getid()
1426 below split OtherFile
1427 below split SomeFile
1428 assert_equal(SomeFileID, bufwinid('SomeFile'))
1429
1430 win_gotoid(origwin)
1431 only
1432 bwipe SomeFile
1433 bwipe OtherFile
1434enddef
1435
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001436def Test_count()
1437 assert_equal(3, count('ABC ABC ABC', 'b', true))
1438 assert_equal(0, count('ABC ABC ABC', 'b', false))
1439enddef
1440
1441def Test_expand()
1442 split SomeFile
1443 assert_equal(['SomeFile'], expand('%', true, true))
1444 close
1445enddef
1446
1447def Test_getbufinfo()
1448 let bufinfo = getbufinfo(bufnr())
1449 assert_equal(bufinfo, getbufinfo('%'))
1450
1451 edit Xtestfile1
1452 hide edit Xtestfile2
1453 hide enew
1454 getbufinfo(#{bufloaded: true, buflisted: true, bufmodified: false})
1455 ->len()->assert_equal(3)
1456 bwipe Xtestfile1 Xtestfile2
1457enddef
1458
Bram Moolenaara5d38412020-09-02 21:02:35 +02001459def Test_getbufline()
1460 e SomeFile
1461 let buf = bufnr()
1462 e #
1463 let lines = ['aaa', 'bbb', 'ccc']
1464 setbufline(buf, 1, lines)
1465 assert_equal(lines, getbufline('#', 1, '$'))
1466
1467 bwipe!
1468enddef
1469
1470def Test_getchangelist()
1471 new
1472 setline(1, 'some text')
1473 let changelist = bufnr()->getchangelist()
1474 assert_equal(changelist, getchangelist('%'))
1475 bwipe!
1476enddef
1477
Bram Moolenaarc08cc722020-09-05 17:51:23 +02001478def Test_getchar()
Bram Moolenaar636c5d52020-09-05 18:48:57 +02001479 while getchar(0)
1480 endwhile
Bram Moolenaarc08cc722020-09-05 17:51:23 +02001481 assert_equal(0, getchar(true))
1482enddef
1483
Bram Moolenaard217a872020-09-05 18:31:33 +02001484def Test_getcompletion()
1485 set wildignore=*.vim,*~
1486 let l = getcompletion('run', 'file', true)
1487 assert_equal([], l)
1488 set wildignore&
1489enddef
1490
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02001491def Test_getreg()
1492 let lines = ['aaa', 'bbb', 'ccc']
1493 setreg('a', lines)
1494 assert_equal(lines, getreg('a', true, true))
1495enddef
1496
Bram Moolenaar5892ea12020-09-02 21:53:11 +02001497def Test_glob()
1498 assert_equal(['runtest.vim'], glob('runtest.vim', true, true, true))
1499enddef
1500
Bram Moolenaarf966ce52020-09-02 21:57:07 +02001501def Test_globpath()
1502 assert_equal(['./runtest.vim'], globpath('.', 'runtest.vim', true, true, true))
1503enddef
1504
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001505def Test_has()
1506 assert_equal(1, has('eval', true))
1507enddef
1508
Bram Moolenaar04d594b2020-09-02 22:25:35 +02001509def Test_hasmapto()
1510 assert_equal(0, hasmapto('foobar', 'i', true))
1511 iabbrev foo foobar
1512 assert_equal(1, hasmapto('foobar', 'i', true))
1513 iunabbrev foo
1514enddef
1515
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001516def Test_index()
1517 assert_equal(3, index(['a', 'b', 'a', 'B'], 'b', 2, true))
1518enddef
1519
1520def Test_list2str_str2list_utf8()
1521 let s = "\u3042\u3044"
1522 let l = [0x3042, 0x3044]
1523 assert_equal(l, str2list(s, true))
1524 assert_equal(s, list2str(l, true))
1525enddef
1526
Bram Moolenaar04d594b2020-09-02 22:25:35 +02001527def SID(): number
1528 return expand('<SID>')
1529 ->matchstr('<SNR>\zs\d\+\ze_$')
1530 ->str2nr()
1531enddef
1532
1533def Test_maparg()
1534 let lnum = str2nr(expand('<sflnum>'))
1535 map foo bar
1536 assert_equal(#{
1537 lnum: lnum + 1,
1538 script: 0,
1539 mode: ' ',
1540 silent: 0,
1541 noremap: 0,
1542 lhs: 'foo',
1543 lhsraw: 'foo',
1544 nowait: 0,
1545 expr: 0,
1546 sid: SID(),
1547 rhs: 'bar',
1548 buffer: 0},
1549 maparg('foo', '', false, true))
1550 unmap foo
1551enddef
1552
1553def Test_mapcheck()
1554 iabbrev foo foobar
1555 assert_equal('foobar', mapcheck('foo', 'i', true))
1556 iunabbrev foo
1557enddef
1558
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001559def Test_nr2char()
1560 assert_equal('a', nr2char(97, true))
1561enddef
1562
1563def Test_readdir()
1564 eval expand('sautest')->readdir({e -> e[0] !=# '.'})
1565 eval expand('sautest')->readdirex({e -> e.name[0] !=# '.'})
1566enddef
1567
1568def Test_search()
1569 new
1570 setline(1, ['foo', 'bar'])
1571 let val = 0
1572 # skip expr returns boolean
1573 assert_equal(2, search('bar', 'W', 0, 0, {-> val == 1}))
1574 :1
1575 assert_equal(0, search('bar', 'W', 0, 0, {-> val == 0}))
1576 # skip expr returns number, only 0 and 1 are accepted
1577 :1
1578 assert_equal(2, search('bar', 'W', 0, 0, {-> 0}))
1579 :1
1580 assert_equal(0, search('bar', 'W', 0, 0, {-> 1}))
1581 assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:')
1582 assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:')
1583enddef
1584
1585def Test_searchcount()
1586 new
1587 setline(1, "foo bar")
1588 :/foo
1589 assert_equal(#{
1590 exact_match: 1,
1591 current: 1,
1592 total: 1,
1593 maxcount: 99,
1594 incomplete: 0,
1595 }, searchcount(#{recompute: true}))
1596 bwipe!
1597enddef
1598
1599def Test_searchdecl()
1600 assert_equal(1, searchdecl('blah', true, true))
1601enddef
1602
1603def Test_setbufvar()
1604 setbufvar(bufnr('%'), '&syntax', 'vim')
1605 assert_equal('vim', &syntax)
1606 setbufvar(bufnr('%'), '&ts', 16)
1607 assert_equal(16, &ts)
1608 settabwinvar(1, 1, '&syntax', 'vam')
1609 assert_equal('vam', &syntax)
1610 settabwinvar(1, 1, '&ts', 15)
1611 assert_equal(15, &ts)
1612 setlocal ts=8
1613
1614 setbufvar('%', 'myvar', 123)
1615 assert_equal(123, getbufvar('%', 'myvar'))
1616enddef
1617
Bram Moolenaar401f0c02020-09-05 22:37:39 +02001618def Test_setloclist()
1619 let items = [#{filename: '/tmp/file', lnum: 1, valid: true}]
1620 let what = #{items: items}
1621 setqflist([], ' ', what)
1622 setloclist(0, [], ' ', what)
1623enddef
1624
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001625def Test_setreg()
1626 setreg('a', ['aaa', 'bbb', 'ccc'])
1627 let reginfo = getreginfo('a')
1628 setreg('a', reginfo)
1629 assert_equal(reginfo, getreginfo('a'))
1630enddef
1631
Bram Moolenaar7c27f332020-09-05 22:45:55 +02001632def Test_spellsuggest()
1633 if !has('spell')
1634 MissingFeature 'spell'
1635 else
1636 spellsuggest('marrch', 1, true)->assert_equal(['March'])
1637 endif
1638enddef
1639
Bram Moolenaar3986b942020-09-06 16:09:04 +02001640def Test_split()
1641 split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', ''])
1642enddef
1643
1644def Test_str2nr()
1645 str2nr("1'000'000", 10, true)->assert_equal(1000000)
1646enddef
1647
1648def Test_strchars()
1649 strchars("A\u20dd", true)->assert_equal(1)
1650enddef
1651
Bram Moolenaarad304702020-09-06 18:22:53 +02001652def Test_submatch()
1653 let pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)'
1654 let Rep = {-> range(10)->map({_, v -> submatch(v, true)})->string()}
1655 let actual = substitute('A123456789', pat, Rep, '')
1656 let expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]"
1657 assert_equal(expected, actual)
1658enddef
1659
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001660def Test_synID()
1661 new
1662 setline(1, "text")
1663 assert_equal(0, synID(1, 1, true))
1664 bwipe!
1665enddef
1666
Bram Moolenaarad304702020-09-06 18:22:53 +02001667def Test_term_gettty()
1668 let buf = Run_shell_in_terminal({})
1669 assert_notequal('', term_gettty(buf, true))
1670 StopShellInTerminal(buf)
1671enddef
1672
1673def Test_term_start()
1674 botright new
1675 let winnr = winnr()
1676 term_start(&shell, #{curwin: true})
1677 assert_equal(winnr, winnr())
1678 bwipe!
1679enddef
1680
Bram Moolenaar418155d2020-09-06 18:39:38 +02001681def Test_timer_paused()
1682 let id = timer_start(50, {-> 0})
1683 timer_pause(id, true)
1684 let info = timer_info(id)
1685 assert_equal(1, info[0]['paused'])
1686 timer_stop(id)
1687enddef
1688
Bram Moolenaar4b9bd692020-09-05 21:57:53 +02001689def Test_win_splitmove()
1690 split
1691 win_splitmove(1, 2, #{vertical: true, rightbelow: true})
1692 close
1693enddef
1694
1695""""""" end of builtin functions
1696
1697def Fibonacci(n: number): number
1698 if n < 2
1699 return n
1700 else
1701 return Fibonacci(n - 1) + Fibonacci(n - 2)
1702 endif
1703enddef
1704
Bram Moolenaar985116a2020-07-12 17:31:09 +02001705def Test_recursive_call()
1706 assert_equal(6765, Fibonacci(20))
1707enddef
1708
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001709def TreeWalk(dir: string): list<any>
1710 return readdir(dir)->map({_, val ->
1711 fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001712 ? {val: TreeWalk(dir .. '/' .. val)}
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001713 : val
1714 })
1715enddef
1716
1717def Test_closure_in_map()
1718 mkdir('XclosureDir/tdir', 'p')
1719 writefile(['111'], 'XclosureDir/file1')
1720 writefile(['222'], 'XclosureDir/file2')
1721 writefile(['333'], 'XclosureDir/tdir/file3')
1722
1723 assert_equal(['file1', 'file2', {'tdir': ['file3']}], TreeWalk('XclosureDir'))
1724
1725 delete('XclosureDir', 'rf')
1726enddef
1727
Bram Moolenaara90afb92020-07-15 22:38:56 +02001728def Test_partial_call()
1729 let Xsetlist = function('setloclist', [0])
1730 Xsetlist([], ' ', {'title': 'test'})
1731 assert_equal({'title': 'test'}, getloclist(0, {'title': 1}))
1732
1733 Xsetlist = function('setloclist', [0, [], ' '])
1734 Xsetlist({'title': 'test'})
1735 assert_equal({'title': 'test'}, getloclist(0, {'title': 1}))
1736
1737 Xsetlist = function('setqflist')
1738 Xsetlist([], ' ', {'title': 'test'})
1739 assert_equal({'title': 'test'}, getqflist({'title': 1}))
1740
1741 Xsetlist = function('setqflist', [[], ' '])
1742 Xsetlist({'title': 'test'})
1743 assert_equal({'title': 'test'}, getqflist({'title': 1}))
1744enddef
1745
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001746def Test_cmd_modifier()
1747 tab echo '0'
Bram Moolenaard2c61702020-09-06 15:58:36 +02001748 CheckDefFailure(['5tab echo 3'], 'E16:')
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001749enddef
1750
1751def Test_restore_modifiers()
1752 # check that when compiling a :def function command modifiers are not messed
1753 # up.
1754 let lines =<< trim END
1755 vim9script
1756 set eventignore=
1757 autocmd QuickFixCmdPost * copen
1758 def AutocmdsDisabled()
1759 eval 0
1760 enddef
1761 func Func()
1762 noautocmd call s:AutocmdsDisabled()
1763 let g:ei_after = &eventignore
1764 endfunc
1765 Func()
1766 END
1767 CheckScriptSuccess(lines)
1768 assert_equal('', g:ei_after)
1769enddef
1770
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001771
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001772" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker