blob: 87c1d4a08c0a5f8d4f1c7addebc1d9ee54701387 [file] [log] [blame]
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001" Test Vim9 classes
2
3source check.vim
4import './vim9.vim' as v9
5
6def Test_class_basic()
7 var lines =<< trim END
8 class NotWorking
9 endclass
10 END
11 v9.CheckScriptFailure(lines, 'E1316:')
12
13 lines =<< trim END
14 vim9script
15 class notWorking
16 endclass
17 END
18 v9.CheckScriptFailure(lines, 'E1314:')
19
20 lines =<< trim END
21 vim9script
22 class Not@working
23 endclass
24 END
25 v9.CheckScriptFailure(lines, 'E1315:')
26
27 lines =<< trim END
28 vim9script
29 abstract noclass Something
30 endclass
31 END
32 v9.CheckScriptFailure(lines, 'E475:')
33
34 lines =<< trim END
35 vim9script
36 abstract classy Something
37 endclass
38 END
39 v9.CheckScriptFailure(lines, 'E475:')
40
41 lines =<< trim END
42 vim9script
43 class Something
44 endcl
45 END
46 v9.CheckScriptFailure(lines, 'E1065:')
47
48 lines =<< trim END
49 vim9script
50 class Something
Bram Moolenaar94722c52023-01-28 19:19:03 +000051 endclass school's out
Bram Moolenaar00b28d62022-12-08 15:32:33 +000052 END
53 v9.CheckScriptFailure(lines, 'E488:')
54
55 lines =<< trim END
56 vim9script
57 class Something
58 endclass | echo 'done'
59 END
60 v9.CheckScriptFailure(lines, 'E488:')
61
62 lines =<< trim END
63 vim9script
64 class Something
65 this
66 endclass
67 END
68 v9.CheckScriptFailure(lines, 'E1317:')
69
70 lines =<< trim END
71 vim9script
72 class Something
73 this.
74 endclass
75 END
76 v9.CheckScriptFailure(lines, 'E1317:')
77
78 lines =<< trim END
79 vim9script
80 class Something
81 this .count
82 endclass
83 END
84 v9.CheckScriptFailure(lines, 'E1317:')
85
86 lines =<< trim END
87 vim9script
88 class Something
89 this. count
90 endclass
91 END
92 v9.CheckScriptFailure(lines, 'E1317:')
93
94 lines =<< trim END
95 vim9script
96 class Something
97 this.count: number
98 that.count
99 endclass
100 END
101 v9.CheckScriptFailure(lines, 'E1318: Not a valid command in a class: that.count')
102
103 lines =<< trim END
104 vim9script
105 class Something
106 this.count
107 endclass
108 END
109 v9.CheckScriptFailure(lines, 'E1022:')
110
111 lines =<< trim END
112 vim9script
113 class Something
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000114 def new()
115 this.state = 0
116 enddef
117 endclass
118 var obj = Something.new()
119 END
120 v9.CheckScriptFailure(lines, 'E1089:')
121
122 lines =<< trim END
123 vim9script
124 class Something
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000125 this.count : number
126 endclass
127 END
128 v9.CheckScriptFailure(lines, 'E1059:')
129
130 lines =<< trim END
131 vim9script
132 class Something
133 this.count:number
134 endclass
135 END
136 v9.CheckScriptFailure(lines, 'E1069:')
137
138 lines =<< trim END
139 vim9script
140
141 class TextPosition
142 this.lnum: number
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000143 this.col: number
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000144
Bram Moolenaar418b5472022-12-20 13:38:22 +0000145 # make a nicely formatted string
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000146 def ToString(): string
147 return $'({this.lnum}, {this.col})'
148 enddef
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000149 endclass
150
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000151 # use the automatically generated new() method
152 var pos = TextPosition.new(2, 12)
153 assert_equal(2, pos.lnum)
154 assert_equal(12, pos.col)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000155
156 # call an object method
157 assert_equal('(2, 12)', pos.ToString())
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000158
159 assert_equal(v:t_class, type(TextPosition))
160 assert_equal(v:t_object, type(pos))
161 assert_equal('class<TextPosition>', typename(TextPosition))
162 assert_equal('object<TextPosition>', typename(pos))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000163 END
164 v9.CheckScriptSuccess(lines)
165enddef
166
Bram Moolenaar83ae6152023-02-25 19:59:31 +0000167def Test_class_defined_twice()
168 # class defined twice should fail
169 var lines =<< trim END
170 vim9script
171 class There
172 endclass
173 class There
174 endclass
175 END
176 v9.CheckScriptFailure(lines, 'E1041: Redefining script item: "There"')
177
178 # one class, reload same script twice is OK
179 lines =<< trim END
180 vim9script
181 class There
182 endclass
183 END
184 writefile(lines, 'XclassTwice.vim', 'D')
185 source XclassTwice.vim
186 source XclassTwice.vim
187enddef
188
Bram Moolenaar657aea72023-01-27 13:16:19 +0000189def Test_class_interface_wrong_end()
190 var lines =<< trim END
191 vim9script
192 abstract class SomeName
193 this.member = 'text'
194 endinterface
195 END
196 v9.CheckScriptFailure(lines, 'E476: Invalid command: endinterface, expected endclass')
197
198 lines =<< trim END
199 vim9script
200 export interface AnotherName
201 this.member: string
202 endclass
203 END
204 v9.CheckScriptFailure(lines, 'E476: Invalid command: endclass, expected endinterface')
205enddef
206
Bram Moolenaar552bdca2023-02-17 21:08:50 +0000207def Test_object_not_set()
208 var lines =<< trim END
209 vim9script
210
211 class State
212 this.value = 'xyz'
213 endclass
214
Bram Moolenaarf2017f22023-02-17 21:29:57 +0000215 var state: State
Bram Moolenaar552bdca2023-02-17 21:08:50 +0000216 var db = {'xyz': 789}
217 echo db[state.value]
218 END
219 v9.CheckScriptFailure(lines, 'E1360:')
Bram Moolenaar0917e862023-02-18 14:42:44 +0000220
221 lines =<< trim END
222 vim9script
223
224 class Background
225 this.background = 'dark'
226 endclass
227
228 class Colorscheme
229 this._bg: Background
230
231 def GetBackground(): string
232 return this._bg.background
233 enddef
234 endclass
235
236 var bg: Background # UNINITIALIZED
237 echo Colorscheme.new(bg).GetBackground()
238 END
239 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Background> but got object<Unknown>')
Bram Moolenaar552bdca2023-02-17 21:08:50 +0000240enddef
241
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000242def Test_class_member_initializer()
243 var lines =<< trim END
244 vim9script
245
246 class TextPosition
247 this.lnum: number = 1
248 this.col: number = 1
249
Bram Moolenaar418b5472022-12-20 13:38:22 +0000250 # constructor with only the line number
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000251 def new(lnum: number)
252 this.lnum = lnum
253 enddef
254 endclass
255
256 var pos = TextPosition.new(3)
257 assert_equal(3, pos.lnum)
258 assert_equal(1, pos.col)
259
260 var instr = execute('disassemble TextPosition.new')
261 assert_match('new\_s*' ..
Bram Moolenaar3ea8a1b2022-12-10 19:03:51 +0000262 '0 NEW TextPosition size \d\+\_s*' ..
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000263 '\d PUSHNR 1\_s*' ..
264 '\d STORE_THIS 0\_s*' ..
265 '\d PUSHNR 1\_s*' ..
266 '\d STORE_THIS 1\_s*' ..
267 'this.lnum = lnum\_s*' ..
268 '\d LOAD arg\[-1]\_s*' ..
269 '\d PUSHNR 0\_s*' ..
270 '\d LOAD $0\_s*' ..
271 '\d\+ STOREINDEX object\_s*' ..
272 '\d\+ RETURN object.*',
273 instr)
274 END
275 v9.CheckScriptSuccess(lines)
276enddef
277
Bram Moolenaar2c1c8032023-02-18 18:38:37 +0000278def Test_member_any_used_as_object()
279 var lines =<< trim END
280 vim9script
281
282 class Inner
283 this.value: number = 0
284 endclass
285
286 class Outer
287 this.inner: any
288 endclass
289
290 def F(outer: Outer)
291 outer.inner.value = 1
292 enddef
293
294 var inner_obj = Inner.new(0)
295 var outer_obj = Outer.new(inner_obj)
296 F(outer_obj)
297 assert_equal(1, inner_obj.value)
298 END
299 v9.CheckScriptSuccess(lines)
300
301 lines =<< trim END
302 vim9script
303
304 class Inner
305 this.value: number = 0
306 endclass
307
308 class Outer
309 this.inner: Inner
310 endclass
311
312 def F(outer: Outer)
313 outer.inner.value = 1
314 enddef
315
316 def Test_assign_to_nested_typed_member()
317 var inner = Inner.new(0)
318 var outer = Outer.new(inner)
319 F(outer)
320 assert_equal(1, inner.value)
321 enddef
322
323 Test_assign_to_nested_typed_member()
324 END
325 v9.CheckScriptSuccess(lines)
326enddef
327
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000328def Test_assignment_with_operator()
329 var lines =<< trim END
330 vim9script
331
332 class Foo
333 this.x: number
334
335 def Add(n: number)
336 this.x += n
337 enddef
338 endclass
339
340 var f = Foo.new(3)
341 f.Add(17)
342 assert_equal(20, f.x)
343 END
344 v9.CheckScriptSuccess(lines)
345enddef
346
Bram Moolenaarf4508042023-01-15 16:54:57 +0000347def Test_list_of_objects()
348 var lines =<< trim END
349 vim9script
350
351 class Foo
352 def Add()
353 enddef
354 endclass
355
356 def ProcessList(fooList: list<Foo>)
357 for foo in fooList
358 foo.Add()
359 endfor
360 enddef
361
362 var l: list<Foo> = [Foo.new()]
363 ProcessList(l)
364 END
365 v9.CheckScriptSuccess(lines)
366enddef
367
Bram Moolenaar912bfee2023-01-15 20:18:55 +0000368def Test_expr_after_using_object()
369 var lines =<< trim END
370 vim9script
371
372 class Something
373 this.label: string = ''
374 endclass
375
376 def Foo(): Something
377 var v = Something.new()
378 echo 'in Foo(): ' .. typename(v)
379 return v
380 enddef
381
382 Foo()
383 END
384 v9.CheckScriptSuccess(lines)
385enddef
386
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000387def Test_class_default_new()
388 var lines =<< trim END
389 vim9script
390
391 class TextPosition
392 this.lnum: number = 1
393 this.col: number = 1
394 endclass
395
396 var pos = TextPosition.new()
397 assert_equal(1, pos.lnum)
398 assert_equal(1, pos.col)
399
400 pos = TextPosition.new(v:none, v:none)
401 assert_equal(1, pos.lnum)
402 assert_equal(1, pos.col)
403
404 pos = TextPosition.new(3, 22)
405 assert_equal(3, pos.lnum)
406 assert_equal(22, pos.col)
407
408 pos = TextPosition.new(v:none, 33)
409 assert_equal(1, pos.lnum)
410 assert_equal(33, pos.col)
411 END
412 v9.CheckScriptSuccess(lines)
413
414 lines =<< trim END
415 vim9script
416 class Person
417 this.name: string
418 this.age: number = 42
419 this.education: string = "unknown"
420
421 def new(this.name, this.age = v:none, this.education = v:none)
422 enddef
423 endclass
424
425 var piet = Person.new("Piet")
426 assert_equal("Piet", piet.name)
427 assert_equal(42, piet.age)
428 assert_equal("unknown", piet.education)
429
430 var chris = Person.new("Chris", 4, "none")
431 assert_equal("Chris", chris.name)
432 assert_equal(4, chris.age)
433 assert_equal("none", chris.education)
434 END
435 v9.CheckScriptSuccess(lines)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000436
437 lines =<< trim END
438 vim9script
439 class Person
440 this.name: string
441 this.age: number = 42
442 this.education: string = "unknown"
443
444 def new(this.name, this.age = v:none, this.education = v:none)
445 enddef
446 endclass
447
448 var missing = Person.new()
449 END
450 v9.CheckScriptFailure(lines, 'E119:')
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000451enddef
452
Bram Moolenaar74e12742022-12-13 21:14:28 +0000453def Test_class_object_member_inits()
454 var lines =<< trim END
455 vim9script
456 class TextPosition
457 this.lnum: number
458 this.col = 1
459 this.addcol: number = 2
460 endclass
461
462 var pos = TextPosition.new()
463 assert_equal(0, pos.lnum)
464 assert_equal(1, pos.col)
465 assert_equal(2, pos.addcol)
466 END
467 v9.CheckScriptSuccess(lines)
468
469 lines =<< trim END
470 vim9script
471 class TextPosition
472 this.lnum
473 this.col = 1
474 endclass
475 END
476 v9.CheckScriptFailure(lines, 'E1022:')
477
478 lines =<< trim END
479 vim9script
480 class TextPosition
481 this.lnum = v:none
482 this.col = 1
483 endclass
484 END
485 v9.CheckScriptFailure(lines, 'E1330:')
486enddef
487
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000488def Test_class_object_member_access()
489 var lines =<< trim END
490 vim9script
491 class Triple
492 this._one = 1
493 this.two = 2
494 public this.three = 3
495
496 def GetOne(): number
497 return this._one
498 enddef
499 endclass
500
501 var trip = Triple.new()
502 assert_equal(1, trip.GetOne())
503 assert_equal(2, trip.two)
504 assert_equal(3, trip.three)
505 assert_fails('echo trip._one', 'E1333')
506
507 assert_fails('trip._one = 11', 'E1333')
508 assert_fails('trip.two = 22', 'E1335')
509 trip.three = 33
510 assert_equal(33, trip.three)
Bram Moolenaard505d172022-12-18 21:42:55 +0000511
512 assert_fails('trip.four = 4', 'E1334')
513 END
514 v9.CheckScriptSuccess(lines)
Bram Moolenaar590162c2022-12-24 21:24:06 +0000515
516 lines =<< trim END
517 vim9script
518
519 class MyCar
520 this.make: string
Bram Moolenaar574950d2023-01-03 19:08:50 +0000521 this.age = 5
Bram Moolenaar590162c2022-12-24 21:24:06 +0000522
523 def new(make_arg: string)
524 this.make = make_arg
525 enddef
526
527 def GetMake(): string
528 return $"make = {this.make}"
529 enddef
Bram Moolenaar574950d2023-01-03 19:08:50 +0000530 def GetAge(): number
531 return this.age
532 enddef
Bram Moolenaar590162c2022-12-24 21:24:06 +0000533 endclass
534
535 var c = MyCar.new("abc")
536 assert_equal('make = abc', c.GetMake())
537
538 c = MyCar.new("def")
539 assert_equal('make = def', c.GetMake())
540
541 var c2 = MyCar.new("123")
542 assert_equal('make = 123', c2.GetMake())
Bram Moolenaar574950d2023-01-03 19:08:50 +0000543
544 def CheckCar()
545 assert_equal("make = def", c.GetMake())
546 assert_equal(5, c.GetAge())
547 enddef
548 CheckCar()
Bram Moolenaar590162c2022-12-24 21:24:06 +0000549 END
550 v9.CheckScriptSuccess(lines)
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000551
552 lines =<< trim END
553 vim9script
554
555 class MyCar
556 this.make: string
557
558 def new(make_arg: string)
559 this.make = make_arg
560 enddef
561 endclass
562
563 var c = MyCar.new("abc")
564 var c = MyCar.new("def")
565 END
566 v9.CheckScriptFailure(lines, 'E1041:')
Bram Moolenaarb149d222023-01-24 13:03:37 +0000567
568 lines =<< trim END
569 vim9script
570
571 class Foo
572 this.x: list<number> = []
573
574 def Add(n: number): any
575 this.x->add(n)
576 return this
577 enddef
578 endclass
579
580 echo Foo.new().Add(1).Add(2).x
581 echo Foo.new().Add(1).Add(2)
582 .x
583 echo Foo.new().Add(1)
584 .Add(2).x
585 echo Foo.new()
586 .Add(1).Add(2).x
587 echo Foo.new()
588 .Add(1)
589 .Add(2)
590 .x
591 END
592 v9.CheckScriptSuccess(lines)
Bram Moolenaard505d172022-12-18 21:42:55 +0000593enddef
594
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000595def Test_class_object_compare()
596 var class_lines =<< trim END
597 vim9script
598 class Item
599 this.nr = 0
600 this.name = 'xx'
601 endclass
602 END
603
604 # used at the script level and in a compiled function
605 var test_lines =<< trim END
606 var i1 = Item.new()
607 assert_equal(i1, i1)
608 assert_true(i1 is i1)
609 var i2 = Item.new()
610 assert_equal(i1, i2)
611 assert_false(i1 is i2)
612 var i3 = Item.new(0, 'xx')
613 assert_equal(i1, i3)
614
615 var io1 = Item.new(1, 'xx')
616 assert_notequal(i1, io1)
617 var io2 = Item.new(0, 'yy')
618 assert_notequal(i1, io2)
619 END
620
621 v9.CheckScriptSuccess(class_lines + test_lines)
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000622 v9.CheckScriptSuccess(
623 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000624
625 for op in ['>', '>=', '<', '<=', '=~', '!~']
626 var op_lines = [
627 'var i1 = Item.new()',
628 'var i2 = Item.new()',
629 'echo i1 ' .. op .. ' i2',
630 ]
631 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000632 v9.CheckScriptFailure(class_lines
633 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000634 endfor
635enddef
636
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000637def Test_object_type()
638 var lines =<< trim END
639 vim9script
640
641 class One
642 this.one = 1
643 endclass
644 class Two
645 this.two = 2
646 endclass
647 class TwoMore extends Two
648 this.more = 9
649 endclass
650
651 var o: One = One.new()
652 var t: Two = Two.new()
653 var m: TwoMore = TwoMore.new()
654 var tm: Two = TwoMore.new()
655
656 t = m
657 END
658 v9.CheckScriptSuccess(lines)
659
660 lines =<< trim END
661 vim9script
662
663 class One
664 this.one = 1
665 endclass
666 class Two
667 this.two = 2
668 endclass
669
670 var o: One = Two.new()
671 END
672 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>')
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000673
674 lines =<< trim END
675 vim9script
676
677 interface One
678 def GetMember(): number
679 endinterface
680 class Two implements One
681 this.one = 1
682 def GetMember(): number
683 return this.one
684 enddef
685 endclass
686
687 var o: One = Two.new(5)
688 assert_equal(5, o.GetMember())
689 END
690 v9.CheckScriptSuccess(lines)
Bram Moolenaar450c7a92023-01-16 16:39:37 +0000691
692 lines =<< trim END
693 vim9script
694
695 class Num
696 this.n: number = 0
697 endclass
698
699 def Ref(name: string): func(Num): Num
700 return (arg: Num): Num => {
701 return eval(name)(arg)
702 }
703 enddef
704
705 const Fn = Ref('Double')
706 var Double = (m: Num): Num => Num.new(m.n * 2)
707
708 echo Fn(Num.new(4))
709 END
710 v9.CheckScriptSuccess(lines)
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000711enddef
712
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000713def Test_class_member()
714 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000715 var lines =<< trim END
716 vim9script
717 class TextPos
718 this.lnum = 1
719 this.col = 1
720 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000721 static _secret = 7
722 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000723
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000724 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000725 counter += nr
726 enddef
727 endclass
728
729 assert_equal(0, TextPos.counter)
730 TextPos.AddToCounter(3)
731 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000732 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000733
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000734 def GetCounter(): number
735 return TextPos.counter
736 enddef
737 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000738
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000739 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000740 assert_fails('TextPos.counter = 5', 'E1335:')
741 assert_fails('TextPos.counter += 5', 'E1335:')
742
743 assert_fails('echo TextPos._secret', 'E1333:')
744 assert_fails('TextPos._secret = 8', 'E1333:')
745
746 assert_equal(42, TextPos.anybody)
747 TextPos.anybody = 12
748 assert_equal(12, TextPos.anybody)
749 TextPos.anybody += 5
750 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000751 END
752 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000753
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000754 # example in the help
755 lines =<< trim END
756 vim9script
757 class OtherThing
758 this.size: number
759 static totalSize: number
760
761 def new(this.size)
762 totalSize += this.size
763 enddef
764 endclass
765 assert_equal(0, OtherThing.totalSize)
766 var to3 = OtherThing.new(3)
767 assert_equal(3, OtherThing.totalSize)
768 var to7 = OtherThing.new(7)
769 assert_equal(10, OtherThing.totalSize)
770 END
771 v9.CheckScriptSuccess(lines)
772
Bram Moolenaar62a69232023-01-24 15:07:04 +0000773 # access private member in lambda
774 lines =<< trim END
775 vim9script
776
777 class Foo
778 this._x: number = 0
779
780 def Add(n: number): number
781 const F = (): number => this._x + n
782 return F()
783 enddef
784 endclass
785
786 var foo = Foo.new()
787 assert_equal(5, foo.Add(5))
788 END
789 v9.CheckScriptSuccess(lines)
790
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000791 # check shadowing
792 lines =<< trim END
793 vim9script
794
795 class Some
796 static count = 0
797 def Method(count: number)
798 echo count
799 enddef
800 endclass
801
802 var s = Some.new()
803 s.Method(7)
804 END
805 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
806
807 lines =<< trim END
808 vim9script
809
810 class Some
811 static count = 0
812 def Method(arg: number)
813 var count = 3
814 echo arg count
815 enddef
816 endclass
817
818 var s = Some.new()
819 s.Method(7)
820 END
821 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000822enddef
823
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000824func Test_class_garbagecollect()
825 let lines =<< trim END
826 vim9script
827
828 class Point
829 this.p = [2, 3]
830 static pl = ['a', 'b']
831 static pd = {a: 'a', b: 'b'}
832 endclass
833
834 echo Point.pl Point.pd
835 call test_garbagecollect_now()
836 echo Point.pl Point.pd
837 END
838 call v9.CheckScriptSuccess(lines)
839endfunc
840
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000841def Test_class_function()
842 var lines =<< trim END
843 vim9script
844 class Value
845 this.value = 0
846 static objects = 0
847
848 def new(v: number)
849 this.value = v
850 ++objects
851 enddef
852
853 static def GetCount(): number
854 return objects
855 enddef
856 endclass
857
858 assert_equal(0, Value.GetCount())
859 var v1 = Value.new(2)
860 assert_equal(1, Value.GetCount())
861 var v2 = Value.new(7)
862 assert_equal(2, Value.GetCount())
863 END
864 v9.CheckScriptSuccess(lines)
865enddef
866
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +0000867def Test_class_defcompile()
868 var lines =<< trim END
869 vim9script
870
871 class C
872 def Fo(i: number): string
873 return i
874 enddef
875 endclass
876
877 defcompile C.Fo
878 END
879 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got number')
880
881 lines =<< trim END
882 vim9script
883
884 class C
885 static def Fc(): number
886 return 'x'
887 enddef
888 endclass
889
890 defcompile C.Fc
891 END
892 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected number but got string')
893enddef
894
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000895def Test_class_object_to_string()
896 var lines =<< trim END
897 vim9script
898 class TextPosition
899 this.lnum = 1
900 this.col = 22
901 endclass
902
903 assert_equal("class TextPosition", string(TextPosition))
904
905 var pos = TextPosition.new()
906 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
907 END
908 v9.CheckScriptSuccess(lines)
909enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000910
Bram Moolenaar554d0312023-01-05 19:59:18 +0000911def Test_interface_basics()
912 var lines =<< trim END
913 vim9script
914 interface Something
915 this.value: string
916 static count: number
917 def GetCount(): number
918 endinterface
919 END
920 v9.CheckScriptSuccess(lines)
921
922 lines =<< trim END
923 interface SomethingWrong
924 static count = 7
925 endinterface
926 END
927 v9.CheckScriptFailure(lines, 'E1342:')
928
929 lines =<< trim END
930 vim9script
931
932 interface Some
933 static count: number
934 def Method(count: number)
935 endinterface
936 END
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000937 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
938
939 lines =<< trim END
940 vim9script
941
942 interface Some
943 this.value: number
944 def Method(value: number)
945 endinterface
946 END
947 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: value')
Bram Moolenaar554d0312023-01-05 19:59:18 +0000948
949 lines =<< trim END
950 vim9script
951 interface somethingWrong
952 static count = 7
953 endinterface
954 END
955 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
956
957 lines =<< trim END
958 vim9script
959 interface SomethingWrong
960 this.value: string
961 static count = 7
962 def GetCount(): number
963 endinterface
964 END
965 v9.CheckScriptFailure(lines, 'E1344:')
966
967 lines =<< trim END
968 vim9script
969 interface SomethingWrong
970 this.value: string
971 static count: number
972 def GetCount(): number
973 return 5
974 enddef
975 endinterface
976 END
977 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
Bram Moolenaar53f54e42023-01-26 20:36:56 +0000978
979 lines =<< trim END
980 vim9script
981 export interface EnterExit
982 def Enter(): void
983 def Exit(): void
984 endinterface
985 END
986 writefile(lines, 'XdefIntf.vim', 'D')
987
988 lines =<< trim END
989 vim9script
990 import './XdefIntf.vim' as defIntf
991 export def With(ee: defIntf.EnterExit, F: func)
992 ee.Enter()
993 try
994 F()
995 finally
996 ee.Exit()
997 endtry
998 enddef
999 END
1000 v9.CheckScriptSuccess(lines)
Bram Moolenaar657aea72023-01-27 13:16:19 +00001001
1002 var imported =<< trim END
1003 vim9script
1004 export abstract class EnterExit
1005 def Enter(): void
1006 enddef
1007 def Exit(): void
1008 enddef
1009 endclass
1010 END
1011 writefile(imported, 'XdefIntf2.vim', 'D')
1012
1013 lines[1] = " import './XdefIntf2.vim' as defIntf"
1014 v9.CheckScriptSuccess(lines)
Bram Moolenaar554d0312023-01-05 19:59:18 +00001015enddef
1016
Bram Moolenaar94674f22023-01-06 18:42:20 +00001017def Test_class_implements_interface()
1018 var lines =<< trim END
1019 vim9script
1020
1021 interface Some
1022 static count: number
1023 def Method(nr: number)
1024 endinterface
1025
1026 class SomeImpl implements Some
1027 static count: number
1028 def Method(nr: number)
1029 echo nr
1030 enddef
1031 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001032
1033 interface Another
1034 this.member: string
1035 endinterface
1036
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001037 class AnotherImpl implements Some, Another
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001038 this.member = 'abc'
1039 static count: number
1040 def Method(nr: number)
1041 echo nr
1042 enddef
1043 endclass
Bram Moolenaar94674f22023-01-06 18:42:20 +00001044 END
1045 v9.CheckScriptSuccess(lines)
1046
1047 lines =<< trim END
1048 vim9script
1049
1050 interface Some
1051 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001052 endinterface
1053
1054 class SomeImpl implements Some implements Some
1055 static count: number
1056 endclass
1057 END
1058 v9.CheckScriptFailure(lines, 'E1350:')
1059
1060 lines =<< trim END
1061 vim9script
1062
1063 interface Some
1064 static counter: number
1065 endinterface
1066
1067 class SomeImpl implements Some, Some
1068 static count: number
1069 endclass
1070 END
1071 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
1072
1073 lines =<< trim END
1074 vim9script
1075
1076 interface Some
1077 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +00001078 def Method(nr: number)
1079 endinterface
1080
1081 class SomeImpl implements Some
1082 static count: number
1083 def Method(nr: number)
1084 echo nr
1085 enddef
1086 endclass
1087 END
1088 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
1089
1090 lines =<< trim END
1091 vim9script
1092
1093 interface Some
1094 static count: number
1095 def Methods(nr: number)
1096 endinterface
1097
1098 class SomeImpl implements Some
1099 static count: number
1100 def Method(nr: number)
1101 echo nr
1102 enddef
1103 endclass
1104 END
1105 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001106
1107 # Check different order of members in class and interface works.
1108 lines =<< trim END
1109 vim9script
1110
1111 interface Result
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001112 public this.label: string
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001113 this.errpos: number
1114 endinterface
1115
1116 # order of members is opposite of interface
1117 class Failure implements Result
1118 this.errpos: number = 42
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001119 public this.label: string = 'label'
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001120 endclass
1121
1122 def Test()
1123 var result: Result = Failure.new()
1124
1125 assert_equal('label', result.label)
1126 assert_equal(42, result.errpos)
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001127
1128 result.label = 'different'
1129 assert_equal('different', result.label)
1130 assert_equal(42, result.errpos)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001131 enddef
1132
1133 Test()
1134 END
1135 v9.CheckScriptSuccess(lines)
Bram Moolenaar94674f22023-01-06 18:42:20 +00001136enddef
1137
Bram Moolenaard0200c82023-01-28 15:19:40 +00001138def Test_call_interface_method()
1139 var lines =<< trim END
1140 vim9script
1141 interface Base
1142 def Enter(): void
1143 endinterface
1144
1145 class Child implements Base
1146 def Enter(): void
1147 g:result ..= 'child'
1148 enddef
1149 endclass
1150
1151 def F(obj: Base)
1152 obj.Enter()
1153 enddef
1154
1155 g:result = ''
1156 F(Child.new())
1157 assert_equal('child', g:result)
1158 unlet g:result
1159 END
1160 v9.CheckScriptSuccess(lines)
1161
1162 lines =<< trim END
1163 vim9script
1164 class Base
1165 def Enter(): void
1166 g:result ..= 'base'
1167 enddef
1168 endclass
1169
1170 class Child extends Base
1171 def Enter(): void
1172 g:result ..= 'child'
1173 enddef
1174 endclass
1175
1176 def F(obj: Base)
1177 obj.Enter()
1178 enddef
1179
1180 g:result = ''
1181 F(Child.new())
1182 assert_equal('child', g:result)
1183 unlet g:result
1184 END
1185 v9.CheckScriptSuccess(lines)
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00001186
Bram Moolenaar7a1bdae2023-02-04 15:45:27 +00001187 # method of interface returns a value
1188 lines =<< trim END
1189 vim9script
1190 interface Base
1191 def Enter(): string
1192 endinterface
1193
1194 class Child implements Base
1195 def Enter(): string
1196 g:result ..= 'child'
1197 return "/resource"
1198 enddef
1199 endclass
1200
1201 def F(obj: Base)
1202 var r = obj.Enter()
1203 g:result ..= r
1204 enddef
1205
1206 g:result = ''
1207 F(Child.new())
1208 assert_equal('child/resource', g:result)
1209 unlet g:result
1210 END
1211 v9.CheckScriptSuccess(lines)
1212
1213 lines =<< trim END
1214 vim9script
1215 class Base
1216 def Enter(): string
1217 return null_string
1218 enddef
1219 endclass
1220
1221 class Child extends Base
1222 def Enter(): string
1223 g:result ..= 'child'
1224 return "/resource"
1225 enddef
1226 endclass
1227
1228 def F(obj: Base)
1229 var r = obj.Enter()
1230 g:result ..= r
1231 enddef
1232
1233 g:result = ''
1234 F(Child.new())
1235 assert_equal('child/resource', g:result)
1236 unlet g:result
1237 END
1238 v9.CheckScriptSuccess(lines)
1239
1240
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00001241 # No class that implements the interface.
1242 lines =<< trim END
1243 vim9script
1244
1245 interface IWithEE
1246 def Enter(): any
1247 def Exit(): void
1248 endinterface
1249
1250 def With1(ee: IWithEE, F: func)
1251 var r = ee.Enter()
1252 enddef
1253
1254 defcompile
1255 END
1256 v9.CheckScriptSuccess(lines)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001257enddef
1258
Bram Moolenaareca2c5f2023-01-07 12:08:41 +00001259def Test_class_used_as_type()
1260 var lines =<< trim END
1261 vim9script
1262
1263 class Point
1264 this.x = 0
1265 this.y = 0
1266 endclass
1267
1268 var p: Point
1269 p = Point.new(2, 33)
1270 assert_equal(2, p.x)
1271 assert_equal(33, p.y)
1272 END
1273 v9.CheckScriptSuccess(lines)
1274
1275 lines =<< trim END
1276 vim9script
1277
1278 interface HasX
1279 this.x: number
1280 endinterface
1281
1282 class Point implements HasX
1283 this.x = 0
1284 this.y = 0
1285 endclass
1286
1287 var p: Point
1288 p = Point.new(2, 33)
1289 var hx = p
1290 assert_equal(2, hx.x)
1291 END
1292 v9.CheckScriptSuccess(lines)
1293
1294 lines =<< trim END
1295 vim9script
1296
1297 class Point
1298 this.x = 0
1299 this.y = 0
1300 endclass
1301
1302 var p: Point
1303 p = 'text'
1304 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001305 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +00001306enddef
1307
Bram Moolenaar83677162023-01-08 19:54:10 +00001308def Test_class_extends()
1309 var lines =<< trim END
1310 vim9script
1311 class Base
1312 this.one = 1
1313 def GetOne(): number
1314 return this.one
1315 enddef
1316 endclass
1317 class Child extends Base
1318 this.two = 2
1319 def GetTotal(): number
1320 return this.one + this.two
1321 enddef
1322 endclass
1323 var o = Child.new()
1324 assert_equal(1, o.one)
1325 assert_equal(2, o.two)
1326 assert_equal(1, o.GetOne())
1327 assert_equal(3, o.GetTotal())
1328 END
1329 v9.CheckScriptSuccess(lines)
1330
1331 lines =<< trim END
1332 vim9script
1333 class Base
1334 this.one = 1
1335 endclass
1336 class Child extends Base
1337 this.two = 2
1338 endclass
1339 var o = Child.new(3, 44)
1340 assert_equal(3, o.one)
1341 assert_equal(44, o.two)
1342 END
1343 v9.CheckScriptSuccess(lines)
1344
1345 lines =<< trim END
1346 vim9script
1347 class Base
1348 this.one = 1
1349 endclass
1350 class Child extends Base extends Base
1351 this.two = 2
1352 endclass
1353 END
1354 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
1355
1356 lines =<< trim END
1357 vim9script
1358 class Child extends BaseClass
1359 this.two = 2
1360 endclass
1361 END
1362 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
1363
1364 lines =<< trim END
1365 vim9script
1366 var SomeVar = 99
1367 class Child extends SomeVar
1368 this.two = 2
1369 endclass
1370 END
1371 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +00001372
1373 lines =<< trim END
1374 vim9script
1375 class Base
1376 this.name: string
1377 def ToString(): string
1378 return this.name
1379 enddef
1380 endclass
1381
1382 class Child extends Base
1383 this.age: number
1384 def ToString(): string
1385 return super.ToString() .. ': ' .. this.age
1386 enddef
1387 endclass
1388
1389 var o = Child.new('John', 42)
1390 assert_equal('John: 42', o.ToString())
1391 END
1392 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +00001393
1394 lines =<< trim END
1395 vim9script
1396 class Child
1397 this.age: number
1398 def ToString(): number
1399 return this.age
1400 enddef
1401 def ToString(): string
1402 return this.age
1403 enddef
1404 endclass
1405 END
1406 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
1407
1408 lines =<< trim END
1409 vim9script
1410 class Child
1411 this.age: number
1412 def ToString(): string
1413 return super .ToString() .. ': ' .. this.age
1414 enddef
1415 endclass
1416 var o = Child.new(42)
1417 echo o.ToString()
1418 END
1419 v9.CheckScriptFailure(lines, 'E1356:')
1420
1421 lines =<< trim END
1422 vim9script
1423 class Base
1424 this.name: string
1425 def ToString(): string
1426 return this.name
1427 enddef
1428 endclass
1429
1430 var age = 42
1431 def ToString(): string
1432 return super.ToString() .. ': ' .. age
1433 enddef
1434 echo ToString()
1435 END
1436 v9.CheckScriptFailure(lines, 'E1357:')
1437
1438 lines =<< trim END
1439 vim9script
1440 class Child
1441 this.age: number
1442 def ToString(): string
1443 return super.ToString() .. ': ' .. this.age
1444 enddef
1445 endclass
1446 var o = Child.new(42)
1447 echo o.ToString()
1448 END
1449 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001450
1451 lines =<< trim END
1452 vim9script
1453 class Base
1454 this.name: string
1455 static def ToString(): string
1456 return 'Base class'
1457 enddef
1458 endclass
1459
1460 class Child extends Base
1461 this.age: number
1462 def ToString(): string
1463 return Base.ToString() .. ': ' .. this.age
1464 enddef
1465 endclass
1466
1467 var o = Child.new('John', 42)
1468 assert_equal('Base class: 42', o.ToString())
1469 END
1470 v9.CheckScriptSuccess(lines)
Bram Moolenaar4cae8452023-01-15 15:51:48 +00001471
1472 lines =<< trim END
1473 vim9script
1474 class Base
1475 this.value = 1
1476 def new(init: number)
1477 this.value = number + 1
1478 enddef
1479 endclass
1480 class Child extends Base
1481 def new()
1482 this.new(3)
1483 enddef
1484 endclass
1485 var c = Child.new()
1486 END
1487 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Child": new(')
Bram Moolenaarae3205a2023-01-15 20:49:00 +00001488
1489 # base class with more than one object member
1490 lines =<< trim END
1491 vim9script
1492
1493 class Result
1494 this.success: bool
1495 this.value: any = null
1496 endclass
1497
1498 class Success extends Result
1499 def new(this.value = v:none)
1500 this.success = true
1501 enddef
1502 endclass
1503
1504 var v = Success.new('asdf')
1505 assert_equal("object of Success {success: true, value: 'asdf'}", string(v))
1506 END
1507 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +00001508enddef
1509
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00001510def Test_using_base_class()
1511 var lines =<< trim END
1512 vim9script
1513
1514 class BaseEE
1515 def Enter(): any
1516 return null
1517 enddef
1518 def Exit(resource: any): void
1519 enddef
1520 endclass
1521
1522 class ChildEE extends BaseEE
1523 def Enter(): any
1524 return 42
1525 enddef
1526
1527 def Exit(resource: number): void
1528 g:result ..= '/exit'
1529 enddef
1530 endclass
1531
1532 def With(ee: BaseEE)
1533 var r = ee.Enter()
1534 try
1535 g:result ..= r
1536 finally
1537 g:result ..= '/finally'
1538 ee.Exit(r)
1539 endtry
1540 enddef
1541
1542 g:result = ''
1543 With(ChildEE.new())
1544 assert_equal('42/finally/exit', g:result)
1545 END
1546 v9.CheckScriptSuccess(lines)
1547 unlet g:result
1548enddef
1549
1550
Bram Moolenaara86655a2023-01-12 17:06:27 +00001551def Test_class_import()
1552 var lines =<< trim END
1553 vim9script
1554 export class Animal
1555 this.kind: string
1556 this.name: string
1557 endclass
1558 END
1559 writefile(lines, 'Xanimal.vim', 'D')
1560
1561 lines =<< trim END
1562 vim9script
1563 import './Xanimal.vim' as animal
1564
1565 var a: animal.Animal
1566 a = animal.Animal.new('fish', 'Eric')
1567 assert_equal('fish', a.kind)
1568 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001569
1570 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1571 assert_equal('cat', b.kind)
1572 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001573 END
1574 v9.CheckScriptSuccess(lines)
1575enddef
1576
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001577def Test_abstract_class()
1578 var lines =<< trim END
1579 vim9script
1580 abstract class Base
1581 this.name: string
1582 endclass
1583 class Person extends Base
1584 this.age: number
1585 endclass
1586 var p: Base = Person.new('Peter', 42)
1587 assert_equal('Peter', p.name)
1588 assert_equal(42, p.age)
1589 END
1590 v9.CheckScriptSuccess(lines)
1591
1592 lines =<< trim END
1593 vim9script
1594 abstract class Base
1595 this.name: string
1596 endclass
1597 class Person extends Base
1598 this.age: number
1599 endclass
1600 var p = Base.new('Peter')
1601 END
1602 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Base": new(')
1603
1604 lines =<< trim END
1605 abstract class Base
1606 this.name: string
1607 endclass
1608 END
1609 v9.CheckScriptFailure(lines, 'E1316:')
1610enddef
1611
Bram Moolenaar486fc252023-01-18 14:51:07 +00001612def Test_closure_in_class()
1613 var lines =<< trim END
1614 vim9script
1615
1616 class Foo
1617 this.y: list<string> = ['B']
1618
1619 def new()
1620 g:result = filter(['A', 'B'], (_, v) => index(this.y, v) == -1)
1621 enddef
1622 endclass
1623
1624 Foo.new()
1625 assert_equal(['A'], g:result)
1626 END
1627 v9.CheckScriptSuccess(lines)
1628enddef
1629
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001630def Test_defer_with_object()
1631 var lines =<< trim END
1632 vim9script
1633
1634 class CWithEE
1635 def Enter()
1636 g:result ..= "entered/"
1637 enddef
1638 def Exit()
1639 g:result ..= "exited"
1640 enddef
1641 endclass
1642
1643 def With(ee: CWithEE, F: func)
1644 ee.Enter()
1645 defer ee.Exit()
1646 F()
1647 enddef
1648
1649 g:result = ''
1650 var obj = CWithEE.new()
1651 obj->With(() => {
1652 g:result ..= "called/"
1653 })
1654 assert_equal('entered/called/exited', g:result)
1655 END
1656 v9.CheckScriptSuccess(lines)
1657 unlet g:result
Bram Moolenaar313e4722023-02-08 20:55:27 +00001658
1659 lines =<< trim END
1660 vim9script
1661
1662 class BaseWithEE
1663 def Enter()
1664 g:result ..= "entered-base/"
1665 enddef
1666 def Exit()
1667 g:result ..= "exited-base"
1668 enddef
1669 endclass
1670
1671 class CWithEE extends BaseWithEE
1672 def Enter()
1673 g:result ..= "entered-child/"
1674 enddef
1675 def Exit()
1676 g:result ..= "exited-child"
1677 enddef
1678 endclass
1679
1680 def With(ee: BaseWithEE, F: func)
1681 ee.Enter()
1682 defer ee.Exit()
1683 F()
1684 enddef
1685
1686 g:result = ''
1687 var obj = CWithEE.new()
1688 obj->With(() => {
1689 g:result ..= "called/"
1690 })
1691 assert_equal('entered-child/called/exited-child', g:result)
1692 END
1693 v9.CheckScriptSuccess(lines)
1694 unlet g:result
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001695enddef
1696
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001697
1698" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker