blob: a8f161d8980e31790f8a78fd0a3a739d40ef736c [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 Moolenaarc4e1b862023-02-26 18:58:23 +0000189def Test_returning_null_object()
190 # this was causing an internal error
191 var lines =<< trim END
192 vim9script
193
194 class BufferList
195 def Current(): any
196 return null_object
197 enddef
198 endclass
199
200 var buffers = BufferList.new()
201 echo buffers.Current()
202 END
203 v9.CheckScriptSuccess(lines)
204enddef
205
Bram Moolenaard13dd302023-03-11 20:56:35 +0000206def Test_using_null_class()
207 var lines =<< trim END
208 @_ = null_class.member
209 END
210 v9.CheckDefExecAndScriptFailure(lines, ['E715:', 'E1363:'])
211enddef
212
Bram Moolenaar657aea72023-01-27 13:16:19 +0000213def Test_class_interface_wrong_end()
214 var lines =<< trim END
215 vim9script
216 abstract class SomeName
217 this.member = 'text'
218 endinterface
219 END
220 v9.CheckScriptFailure(lines, 'E476: Invalid command: endinterface, expected endclass')
221
222 lines =<< trim END
223 vim9script
224 export interface AnotherName
225 this.member: string
226 endclass
227 END
228 v9.CheckScriptFailure(lines, 'E476: Invalid command: endclass, expected endinterface')
229enddef
230
Bram Moolenaar552bdca2023-02-17 21:08:50 +0000231def Test_object_not_set()
232 var lines =<< trim END
233 vim9script
234
235 class State
236 this.value = 'xyz'
237 endclass
238
Bram Moolenaarf2017f22023-02-17 21:29:57 +0000239 var state: State
Bram Moolenaar552bdca2023-02-17 21:08:50 +0000240 var db = {'xyz': 789}
241 echo db[state.value]
242 END
243 v9.CheckScriptFailure(lines, 'E1360:')
Bram Moolenaar0917e862023-02-18 14:42:44 +0000244
245 lines =<< trim END
246 vim9script
247
Bram Moolenaarc3f971f2023-03-02 17:38:33 +0000248 class Class
249 this.id: string
250 def Method1()
251 echo 'Method1' .. this.id
252 enddef
253 endclass
254
255 var obj: Class
256 def Func()
257 obj.Method1()
258 enddef
259 Func()
260 END
261 v9.CheckScriptFailure(lines, 'E1360:')
262
263 lines =<< trim END
264 vim9script
265
Bram Moolenaar0917e862023-02-18 14:42:44 +0000266 class Background
267 this.background = 'dark'
268 endclass
269
270 class Colorscheme
271 this._bg: Background
272
273 def GetBackground(): string
274 return this._bg.background
275 enddef
276 endclass
277
278 var bg: Background # UNINITIALIZED
279 echo Colorscheme.new(bg).GetBackground()
280 END
281 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Background> but got object<Unknown>')
Ernie Raelf77a7f72023-03-03 15:05:30 +0000282
283 # TODO: this should not give an error but be handled at runtime
284 lines =<< trim END
285 vim9script
286
287 class Class
288 this.id: string
289 def Method1()
290 echo 'Method1' .. this.id
291 enddef
292 endclass
293
294 var obj = null_object
295 def Func()
296 obj.Method1()
297 enddef
298 Func()
299 END
300 v9.CheckScriptFailure(lines, 'E1363:')
Bram Moolenaar552bdca2023-02-17 21:08:50 +0000301enddef
302
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000303def Test_class_member_initializer()
304 var lines =<< trim END
305 vim9script
306
307 class TextPosition
308 this.lnum: number = 1
309 this.col: number = 1
310
Bram Moolenaar418b5472022-12-20 13:38:22 +0000311 # constructor with only the line number
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000312 def new(lnum: number)
313 this.lnum = lnum
314 enddef
315 endclass
316
317 var pos = TextPosition.new(3)
318 assert_equal(3, pos.lnum)
319 assert_equal(1, pos.col)
320
321 var instr = execute('disassemble TextPosition.new')
322 assert_match('new\_s*' ..
Bram Moolenaar3ea8a1b2022-12-10 19:03:51 +0000323 '0 NEW TextPosition size \d\+\_s*' ..
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000324 '\d PUSHNR 1\_s*' ..
325 '\d STORE_THIS 0\_s*' ..
326 '\d PUSHNR 1\_s*' ..
327 '\d STORE_THIS 1\_s*' ..
328 'this.lnum = lnum\_s*' ..
329 '\d LOAD arg\[-1]\_s*' ..
330 '\d PUSHNR 0\_s*' ..
331 '\d LOAD $0\_s*' ..
332 '\d\+ STOREINDEX object\_s*' ..
333 '\d\+ RETURN object.*',
334 instr)
335 END
336 v9.CheckScriptSuccess(lines)
337enddef
338
Bram Moolenaar2c1c8032023-02-18 18:38:37 +0000339def Test_member_any_used_as_object()
340 var lines =<< trim END
341 vim9script
342
343 class Inner
344 this.value: number = 0
345 endclass
346
347 class Outer
348 this.inner: any
349 endclass
350
351 def F(outer: Outer)
352 outer.inner.value = 1
353 enddef
354
355 var inner_obj = Inner.new(0)
356 var outer_obj = Outer.new(inner_obj)
357 F(outer_obj)
358 assert_equal(1, inner_obj.value)
359 END
360 v9.CheckScriptSuccess(lines)
361
362 lines =<< trim END
363 vim9script
364
365 class Inner
366 this.value: number = 0
367 endclass
368
369 class Outer
370 this.inner: Inner
371 endclass
372
373 def F(outer: Outer)
374 outer.inner.value = 1
375 enddef
376
377 def Test_assign_to_nested_typed_member()
378 var inner = Inner.new(0)
379 var outer = Outer.new(inner)
380 F(outer)
381 assert_equal(1, inner.value)
382 enddef
383
384 Test_assign_to_nested_typed_member()
385 END
386 v9.CheckScriptSuccess(lines)
387enddef
388
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000389def Test_assignment_with_operator()
390 var lines =<< trim END
391 vim9script
392
393 class Foo
394 this.x: number
395
396 def Add(n: number)
397 this.x += n
398 enddef
399 endclass
400
401 var f = Foo.new(3)
402 f.Add(17)
403 assert_equal(20, f.x)
404 END
405 v9.CheckScriptSuccess(lines)
406enddef
407
Bram Moolenaarf4508042023-01-15 16:54:57 +0000408def Test_list_of_objects()
409 var lines =<< trim END
410 vim9script
411
412 class Foo
413 def Add()
414 enddef
415 endclass
416
417 def ProcessList(fooList: list<Foo>)
418 for foo in fooList
419 foo.Add()
420 endfor
421 enddef
422
423 var l: list<Foo> = [Foo.new()]
424 ProcessList(l)
425 END
426 v9.CheckScriptSuccess(lines)
427enddef
428
Bram Moolenaar912bfee2023-01-15 20:18:55 +0000429def Test_expr_after_using_object()
430 var lines =<< trim END
431 vim9script
432
433 class Something
434 this.label: string = ''
435 endclass
436
437 def Foo(): Something
438 var v = Something.new()
439 echo 'in Foo(): ' .. typename(v)
440 return v
441 enddef
442
443 Foo()
444 END
445 v9.CheckScriptSuccess(lines)
446enddef
447
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000448def Test_class_default_new()
449 var lines =<< trim END
450 vim9script
451
452 class TextPosition
453 this.lnum: number = 1
454 this.col: number = 1
455 endclass
456
457 var pos = TextPosition.new()
458 assert_equal(1, pos.lnum)
459 assert_equal(1, pos.col)
460
461 pos = TextPosition.new(v:none, v:none)
462 assert_equal(1, pos.lnum)
463 assert_equal(1, pos.col)
464
465 pos = TextPosition.new(3, 22)
466 assert_equal(3, pos.lnum)
467 assert_equal(22, pos.col)
468
469 pos = TextPosition.new(v:none, 33)
470 assert_equal(1, pos.lnum)
471 assert_equal(33, pos.col)
472 END
473 v9.CheckScriptSuccess(lines)
474
475 lines =<< trim END
476 vim9script
477 class Person
478 this.name: string
479 this.age: number = 42
480 this.education: string = "unknown"
481
482 def new(this.name, this.age = v:none, this.education = v:none)
483 enddef
484 endclass
485
486 var piet = Person.new("Piet")
487 assert_equal("Piet", piet.name)
488 assert_equal(42, piet.age)
489 assert_equal("unknown", piet.education)
490
491 var chris = Person.new("Chris", 4, "none")
492 assert_equal("Chris", chris.name)
493 assert_equal(4, chris.age)
494 assert_equal("none", chris.education)
495 END
496 v9.CheckScriptSuccess(lines)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000497
498 lines =<< trim END
499 vim9script
500 class Person
501 this.name: string
502 this.age: number = 42
503 this.education: string = "unknown"
504
505 def new(this.name, this.age = v:none, this.education = v:none)
506 enddef
507 endclass
508
509 var missing = Person.new()
510 END
511 v9.CheckScriptFailure(lines, 'E119:')
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000512enddef
513
Bram Moolenaar74e12742022-12-13 21:14:28 +0000514def Test_class_object_member_inits()
515 var lines =<< trim END
516 vim9script
517 class TextPosition
518 this.lnum: number
519 this.col = 1
520 this.addcol: number = 2
521 endclass
522
523 var pos = TextPosition.new()
524 assert_equal(0, pos.lnum)
525 assert_equal(1, pos.col)
526 assert_equal(2, pos.addcol)
527 END
528 v9.CheckScriptSuccess(lines)
529
530 lines =<< trim END
531 vim9script
532 class TextPosition
533 this.lnum
534 this.col = 1
535 endclass
536 END
537 v9.CheckScriptFailure(lines, 'E1022:')
538
539 lines =<< trim END
540 vim9script
541 class TextPosition
542 this.lnum = v:none
543 this.col = 1
544 endclass
545 END
546 v9.CheckScriptFailure(lines, 'E1330:')
547enddef
548
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000549def Test_class_object_member_access()
550 var lines =<< trim END
551 vim9script
552 class Triple
553 this._one = 1
554 this.two = 2
555 public this.three = 3
556
557 def GetOne(): number
558 return this._one
559 enddef
560 endclass
561
562 var trip = Triple.new()
563 assert_equal(1, trip.GetOne())
564 assert_equal(2, trip.two)
565 assert_equal(3, trip.three)
566 assert_fails('echo trip._one', 'E1333')
567
568 assert_fails('trip._one = 11', 'E1333')
569 assert_fails('trip.two = 22', 'E1335')
570 trip.three = 33
571 assert_equal(33, trip.three)
Bram Moolenaard505d172022-12-18 21:42:55 +0000572
573 assert_fails('trip.four = 4', 'E1334')
574 END
575 v9.CheckScriptSuccess(lines)
Bram Moolenaar590162c2022-12-24 21:24:06 +0000576
577 lines =<< trim END
578 vim9script
579
580 class MyCar
581 this.make: string
Bram Moolenaar574950d2023-01-03 19:08:50 +0000582 this.age = 5
Bram Moolenaar590162c2022-12-24 21:24:06 +0000583
584 def new(make_arg: string)
585 this.make = make_arg
586 enddef
587
588 def GetMake(): string
589 return $"make = {this.make}"
590 enddef
Bram Moolenaar574950d2023-01-03 19:08:50 +0000591 def GetAge(): number
592 return this.age
593 enddef
Bram Moolenaar590162c2022-12-24 21:24:06 +0000594 endclass
595
596 var c = MyCar.new("abc")
597 assert_equal('make = abc', c.GetMake())
598
599 c = MyCar.new("def")
600 assert_equal('make = def', c.GetMake())
601
602 var c2 = MyCar.new("123")
603 assert_equal('make = 123', c2.GetMake())
Bram Moolenaar574950d2023-01-03 19:08:50 +0000604
605 def CheckCar()
606 assert_equal("make = def", c.GetMake())
607 assert_equal(5, c.GetAge())
608 enddef
609 CheckCar()
Bram Moolenaar590162c2022-12-24 21:24:06 +0000610 END
611 v9.CheckScriptSuccess(lines)
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000612
613 lines =<< trim END
614 vim9script
615
616 class MyCar
617 this.make: string
618
619 def new(make_arg: string)
620 this.make = make_arg
621 enddef
622 endclass
623
624 var c = MyCar.new("abc")
625 var c = MyCar.new("def")
626 END
627 v9.CheckScriptFailure(lines, 'E1041:')
Bram Moolenaarb149d222023-01-24 13:03:37 +0000628
629 lines =<< trim END
630 vim9script
631
632 class Foo
633 this.x: list<number> = []
634
635 def Add(n: number): any
636 this.x->add(n)
637 return this
638 enddef
639 endclass
640
641 echo Foo.new().Add(1).Add(2).x
642 echo Foo.new().Add(1).Add(2)
643 .x
644 echo Foo.new().Add(1)
645 .Add(2).x
646 echo Foo.new()
647 .Add(1).Add(2).x
648 echo Foo.new()
649 .Add(1)
650 .Add(2)
651 .x
652 END
653 v9.CheckScriptSuccess(lines)
Bram Moolenaard505d172022-12-18 21:42:55 +0000654enddef
655
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000656def Test_class_object_compare()
657 var class_lines =<< trim END
658 vim9script
659 class Item
660 this.nr = 0
661 this.name = 'xx'
662 endclass
663 END
664
665 # used at the script level and in a compiled function
666 var test_lines =<< trim END
667 var i1 = Item.new()
668 assert_equal(i1, i1)
669 assert_true(i1 is i1)
670 var i2 = Item.new()
671 assert_equal(i1, i2)
672 assert_false(i1 is i2)
673 var i3 = Item.new(0, 'xx')
674 assert_equal(i1, i3)
675
676 var io1 = Item.new(1, 'xx')
677 assert_notequal(i1, io1)
678 var io2 = Item.new(0, 'yy')
679 assert_notequal(i1, io2)
680 END
681
682 v9.CheckScriptSuccess(class_lines + test_lines)
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000683 v9.CheckScriptSuccess(
684 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000685
686 for op in ['>', '>=', '<', '<=', '=~', '!~']
687 var op_lines = [
688 'var i1 = Item.new()',
689 'var i2 = Item.new()',
690 'echo i1 ' .. op .. ' i2',
691 ]
692 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000693 v9.CheckScriptFailure(class_lines
694 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000695 endfor
696enddef
697
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000698def Test_object_type()
699 var lines =<< trim END
700 vim9script
701
702 class One
703 this.one = 1
704 endclass
705 class Two
706 this.two = 2
707 endclass
708 class TwoMore extends Two
709 this.more = 9
710 endclass
711
712 var o: One = One.new()
713 var t: Two = Two.new()
714 var m: TwoMore = TwoMore.new()
715 var tm: Two = TwoMore.new()
716
717 t = m
718 END
719 v9.CheckScriptSuccess(lines)
720
721 lines =<< trim END
722 vim9script
723
724 class One
725 this.one = 1
726 endclass
727 class Two
728 this.two = 2
729 endclass
730
731 var o: One = Two.new()
732 END
733 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>')
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000734
735 lines =<< trim END
736 vim9script
737
738 interface One
739 def GetMember(): number
740 endinterface
741 class Two implements One
742 this.one = 1
743 def GetMember(): number
744 return this.one
745 enddef
746 endclass
747
748 var o: One = Two.new(5)
749 assert_equal(5, o.GetMember())
750 END
751 v9.CheckScriptSuccess(lines)
Bram Moolenaar450c7a92023-01-16 16:39:37 +0000752
753 lines =<< trim END
754 vim9script
755
756 class Num
757 this.n: number = 0
758 endclass
759
760 def Ref(name: string): func(Num): Num
761 return (arg: Num): Num => {
762 return eval(name)(arg)
763 }
764 enddef
765
766 const Fn = Ref('Double')
767 var Double = (m: Num): Num => Num.new(m.n * 2)
768
769 echo Fn(Num.new(4))
770 END
771 v9.CheckScriptSuccess(lines)
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000772enddef
773
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000774def Test_class_member()
775 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000776 var lines =<< trim END
777 vim9script
778 class TextPos
779 this.lnum = 1
780 this.col = 1
781 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000782 static _secret = 7
783 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000784
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000785 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000786 counter += nr
787 enddef
788 endclass
789
790 assert_equal(0, TextPos.counter)
791 TextPos.AddToCounter(3)
792 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000793 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000794
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000795 def GetCounter(): number
796 return TextPos.counter
797 enddef
798 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000799
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000800 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000801 assert_fails('TextPos.counter = 5', 'E1335:')
802 assert_fails('TextPos.counter += 5', 'E1335:')
803
804 assert_fails('echo TextPos._secret', 'E1333:')
805 assert_fails('TextPos._secret = 8', 'E1333:')
806
807 assert_equal(42, TextPos.anybody)
808 TextPos.anybody = 12
809 assert_equal(12, TextPos.anybody)
810 TextPos.anybody += 5
811 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000812 END
813 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000814
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000815 # example in the help
816 lines =<< trim END
817 vim9script
818 class OtherThing
819 this.size: number
820 static totalSize: number
821
822 def new(this.size)
823 totalSize += this.size
824 enddef
825 endclass
826 assert_equal(0, OtherThing.totalSize)
827 var to3 = OtherThing.new(3)
828 assert_equal(3, OtherThing.totalSize)
829 var to7 = OtherThing.new(7)
830 assert_equal(10, OtherThing.totalSize)
831 END
832 v9.CheckScriptSuccess(lines)
833
Bram Moolenaar62a69232023-01-24 15:07:04 +0000834 # access private member in lambda
835 lines =<< trim END
836 vim9script
837
838 class Foo
839 this._x: number = 0
840
841 def Add(n: number): number
842 const F = (): number => this._x + n
843 return F()
844 enddef
845 endclass
846
847 var foo = Foo.new()
848 assert_equal(5, foo.Add(5))
849 END
850 v9.CheckScriptSuccess(lines)
851
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000852 # check shadowing
853 lines =<< trim END
854 vim9script
855
856 class Some
857 static count = 0
858 def Method(count: number)
859 echo count
860 enddef
861 endclass
862
863 var s = Some.new()
864 s.Method(7)
865 END
866 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
867
868 lines =<< trim END
869 vim9script
870
871 class Some
872 static count = 0
873 def Method(arg: number)
874 var count = 3
875 echo arg count
876 enddef
877 endclass
878
879 var s = Some.new()
880 s.Method(7)
881 END
882 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000883enddef
884
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000885func Test_class_garbagecollect()
886 let lines =<< trim END
887 vim9script
888
889 class Point
890 this.p = [2, 3]
891 static pl = ['a', 'b']
892 static pd = {a: 'a', b: 'b'}
893 endclass
894
895 echo Point.pl Point.pd
896 call test_garbagecollect_now()
897 echo Point.pl Point.pd
898 END
899 call v9.CheckScriptSuccess(lines)
900endfunc
901
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000902def Test_class_function()
903 var lines =<< trim END
904 vim9script
905 class Value
906 this.value = 0
907 static objects = 0
908
909 def new(v: number)
910 this.value = v
911 ++objects
912 enddef
913
914 static def GetCount(): number
915 return objects
916 enddef
917 endclass
918
919 assert_equal(0, Value.GetCount())
920 var v1 = Value.new(2)
921 assert_equal(1, Value.GetCount())
922 var v2 = Value.new(7)
923 assert_equal(2, Value.GetCount())
924 END
925 v9.CheckScriptSuccess(lines)
926enddef
927
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +0000928def Test_class_defcompile()
929 var lines =<< trim END
930 vim9script
931
932 class C
933 def Fo(i: number): string
934 return i
935 enddef
936 endclass
937
938 defcompile C.Fo
939 END
940 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got number')
941
942 lines =<< trim END
943 vim9script
944
945 class C
946 static def Fc(): number
947 return 'x'
948 enddef
949 endclass
950
951 defcompile C.Fc
952 END
953 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected number but got string')
954enddef
955
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000956def Test_class_object_to_string()
957 var lines =<< trim END
958 vim9script
959 class TextPosition
960 this.lnum = 1
961 this.col = 22
962 endclass
963
964 assert_equal("class TextPosition", string(TextPosition))
965
966 var pos = TextPosition.new()
967 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
968 END
969 v9.CheckScriptSuccess(lines)
970enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000971
Bram Moolenaar554d0312023-01-05 19:59:18 +0000972def Test_interface_basics()
973 var lines =<< trim END
974 vim9script
975 interface Something
976 this.value: string
977 static count: number
978 def GetCount(): number
979 endinterface
980 END
981 v9.CheckScriptSuccess(lines)
982
983 lines =<< trim END
984 interface SomethingWrong
985 static count = 7
986 endinterface
987 END
988 v9.CheckScriptFailure(lines, 'E1342:')
989
990 lines =<< trim END
991 vim9script
992
993 interface Some
994 static count: number
995 def Method(count: number)
996 endinterface
997 END
h-east61378a12023-04-18 19:07:29 +0100998 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count', 5)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000999
1000 lines =<< trim END
1001 vim9script
1002
1003 interface Some
1004 this.value: number
1005 def Method(value: number)
1006 endinterface
1007 END
h-east61378a12023-04-18 19:07:29 +01001008 # The argument name and the object member name are the same, but this is not a
1009 # problem because object members are always accessed with the "this." prefix.
1010 v9.CheckScriptSuccess(lines)
Bram Moolenaar554d0312023-01-05 19:59:18 +00001011
1012 lines =<< trim END
1013 vim9script
1014 interface somethingWrong
1015 static count = 7
1016 endinterface
1017 END
1018 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
1019
1020 lines =<< trim END
1021 vim9script
1022 interface SomethingWrong
1023 this.value: string
1024 static count = 7
1025 def GetCount(): number
1026 endinterface
1027 END
1028 v9.CheckScriptFailure(lines, 'E1344:')
1029
1030 lines =<< trim END
1031 vim9script
1032 interface SomethingWrong
1033 this.value: string
1034 static count: number
1035 def GetCount(): number
1036 return 5
1037 enddef
1038 endinterface
1039 END
1040 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
Bram Moolenaar53f54e42023-01-26 20:36:56 +00001041
1042 lines =<< trim END
1043 vim9script
1044 export interface EnterExit
1045 def Enter(): void
1046 def Exit(): void
1047 endinterface
1048 END
1049 writefile(lines, 'XdefIntf.vim', 'D')
1050
1051 lines =<< trim END
1052 vim9script
1053 import './XdefIntf.vim' as defIntf
1054 export def With(ee: defIntf.EnterExit, F: func)
1055 ee.Enter()
1056 try
1057 F()
1058 finally
1059 ee.Exit()
1060 endtry
1061 enddef
1062 END
1063 v9.CheckScriptSuccess(lines)
Bram Moolenaar657aea72023-01-27 13:16:19 +00001064
1065 var imported =<< trim END
1066 vim9script
1067 export abstract class EnterExit
1068 def Enter(): void
1069 enddef
1070 def Exit(): void
1071 enddef
1072 endclass
1073 END
1074 writefile(imported, 'XdefIntf2.vim', 'D')
1075
1076 lines[1] = " import './XdefIntf2.vim' as defIntf"
1077 v9.CheckScriptSuccess(lines)
Bram Moolenaar554d0312023-01-05 19:59:18 +00001078enddef
1079
Bram Moolenaar94674f22023-01-06 18:42:20 +00001080def Test_class_implements_interface()
1081 var lines =<< trim END
1082 vim9script
1083
1084 interface Some
1085 static count: number
1086 def Method(nr: number)
1087 endinterface
1088
1089 class SomeImpl implements Some
1090 static count: number
1091 def Method(nr: number)
1092 echo nr
1093 enddef
1094 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001095
1096 interface Another
1097 this.member: string
1098 endinterface
1099
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001100 class AnotherImpl implements Some, Another
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001101 this.member = 'abc'
1102 static count: number
1103 def Method(nr: number)
1104 echo nr
1105 enddef
1106 endclass
Bram Moolenaar94674f22023-01-06 18:42:20 +00001107 END
1108 v9.CheckScriptSuccess(lines)
1109
1110 lines =<< trim END
1111 vim9script
1112
1113 interface Some
1114 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001115 endinterface
1116
1117 class SomeImpl implements Some implements Some
1118 static count: number
1119 endclass
1120 END
1121 v9.CheckScriptFailure(lines, 'E1350:')
1122
1123 lines =<< trim END
1124 vim9script
1125
1126 interface Some
1127 static counter: number
1128 endinterface
1129
1130 class SomeImpl implements Some, Some
1131 static count: number
1132 endclass
1133 END
1134 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
1135
1136 lines =<< trim END
1137 vim9script
1138
1139 interface Some
1140 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +00001141 def Method(nr: number)
1142 endinterface
1143
1144 class SomeImpl implements Some
1145 static count: number
1146 def Method(nr: number)
1147 echo nr
1148 enddef
1149 endclass
1150 END
1151 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
1152
1153 lines =<< trim END
1154 vim9script
1155
1156 interface Some
1157 static count: number
1158 def Methods(nr: number)
1159 endinterface
1160
1161 class SomeImpl implements Some
1162 static count: number
1163 def Method(nr: number)
1164 echo nr
1165 enddef
1166 endclass
1167 END
1168 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001169
1170 # Check different order of members in class and interface works.
1171 lines =<< trim END
1172 vim9script
1173
1174 interface Result
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001175 public this.label: string
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001176 this.errpos: number
1177 endinterface
1178
1179 # order of members is opposite of interface
1180 class Failure implements Result
1181 this.errpos: number = 42
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001182 public this.label: string = 'label'
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001183 endclass
1184
1185 def Test()
1186 var result: Result = Failure.new()
1187
1188 assert_equal('label', result.label)
1189 assert_equal(42, result.errpos)
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001190
1191 result.label = 'different'
1192 assert_equal('different', result.label)
1193 assert_equal(42, result.errpos)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001194 enddef
1195
1196 Test()
1197 END
1198 v9.CheckScriptSuccess(lines)
Bram Moolenaar94674f22023-01-06 18:42:20 +00001199enddef
1200
Bram Moolenaard0200c82023-01-28 15:19:40 +00001201def Test_call_interface_method()
1202 var lines =<< trim END
1203 vim9script
1204 interface Base
1205 def Enter(): void
1206 endinterface
1207
1208 class Child implements Base
1209 def Enter(): void
1210 g:result ..= 'child'
1211 enddef
1212 endclass
1213
1214 def F(obj: Base)
1215 obj.Enter()
1216 enddef
1217
1218 g:result = ''
1219 F(Child.new())
1220 assert_equal('child', g:result)
1221 unlet g:result
1222 END
1223 v9.CheckScriptSuccess(lines)
1224
1225 lines =<< trim END
1226 vim9script
1227 class Base
1228 def Enter(): void
1229 g:result ..= 'base'
1230 enddef
1231 endclass
1232
1233 class Child extends Base
1234 def Enter(): void
1235 g:result ..= 'child'
1236 enddef
1237 endclass
1238
1239 def F(obj: Base)
1240 obj.Enter()
1241 enddef
1242
1243 g:result = ''
1244 F(Child.new())
1245 assert_equal('child', g:result)
1246 unlet g:result
1247 END
1248 v9.CheckScriptSuccess(lines)
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00001249
Bram Moolenaar7a1bdae2023-02-04 15:45:27 +00001250 # method of interface returns a value
1251 lines =<< trim END
1252 vim9script
1253 interface Base
1254 def Enter(): string
1255 endinterface
1256
1257 class Child implements Base
1258 def Enter(): string
1259 g:result ..= 'child'
1260 return "/resource"
1261 enddef
1262 endclass
1263
1264 def F(obj: Base)
1265 var r = obj.Enter()
1266 g:result ..= r
1267 enddef
1268
1269 g:result = ''
1270 F(Child.new())
1271 assert_equal('child/resource', g:result)
1272 unlet g:result
1273 END
1274 v9.CheckScriptSuccess(lines)
1275
1276 lines =<< trim END
1277 vim9script
1278 class Base
1279 def Enter(): string
1280 return null_string
1281 enddef
1282 endclass
1283
1284 class Child extends Base
1285 def Enter(): string
1286 g:result ..= 'child'
1287 return "/resource"
1288 enddef
1289 endclass
1290
1291 def F(obj: Base)
1292 var r = obj.Enter()
1293 g:result ..= r
1294 enddef
1295
1296 g:result = ''
1297 F(Child.new())
1298 assert_equal('child/resource', g:result)
1299 unlet g:result
1300 END
1301 v9.CheckScriptSuccess(lines)
1302
1303
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00001304 # No class that implements the interface.
1305 lines =<< trim END
1306 vim9script
1307
1308 interface IWithEE
1309 def Enter(): any
1310 def Exit(): void
1311 endinterface
1312
1313 def With1(ee: IWithEE, F: func)
1314 var r = ee.Enter()
1315 enddef
1316
1317 defcompile
1318 END
1319 v9.CheckScriptSuccess(lines)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001320enddef
1321
Bram Moolenaareca2c5f2023-01-07 12:08:41 +00001322def Test_class_used_as_type()
1323 var lines =<< trim END
1324 vim9script
1325
1326 class Point
1327 this.x = 0
1328 this.y = 0
1329 endclass
1330
1331 var p: Point
1332 p = Point.new(2, 33)
1333 assert_equal(2, p.x)
1334 assert_equal(33, p.y)
1335 END
1336 v9.CheckScriptSuccess(lines)
1337
1338 lines =<< trim END
1339 vim9script
1340
1341 interface HasX
1342 this.x: number
1343 endinterface
1344
1345 class Point implements HasX
1346 this.x = 0
1347 this.y = 0
1348 endclass
1349
1350 var p: Point
1351 p = Point.new(2, 33)
1352 var hx = p
1353 assert_equal(2, hx.x)
1354 END
1355 v9.CheckScriptSuccess(lines)
1356
1357 lines =<< trim END
1358 vim9script
1359
1360 class Point
1361 this.x = 0
1362 this.y = 0
1363 endclass
1364
1365 var p: Point
1366 p = 'text'
1367 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001368 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +00001369enddef
1370
Bram Moolenaar83677162023-01-08 19:54:10 +00001371def Test_class_extends()
1372 var lines =<< trim END
1373 vim9script
1374 class Base
1375 this.one = 1
1376 def GetOne(): number
1377 return this.one
1378 enddef
1379 endclass
1380 class Child extends Base
1381 this.two = 2
1382 def GetTotal(): number
1383 return this.one + this.two
1384 enddef
1385 endclass
1386 var o = Child.new()
1387 assert_equal(1, o.one)
1388 assert_equal(2, o.two)
1389 assert_equal(1, o.GetOne())
1390 assert_equal(3, o.GetTotal())
1391 END
1392 v9.CheckScriptSuccess(lines)
1393
1394 lines =<< trim END
1395 vim9script
1396 class Base
1397 this.one = 1
1398 endclass
1399 class Child extends Base
1400 this.two = 2
1401 endclass
1402 var o = Child.new(3, 44)
1403 assert_equal(3, o.one)
1404 assert_equal(44, o.two)
1405 END
1406 v9.CheckScriptSuccess(lines)
1407
1408 lines =<< trim END
1409 vim9script
1410 class Base
1411 this.one = 1
1412 endclass
1413 class Child extends Base extends Base
1414 this.two = 2
1415 endclass
1416 END
1417 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
1418
1419 lines =<< trim END
1420 vim9script
1421 class Child extends BaseClass
1422 this.two = 2
1423 endclass
1424 END
1425 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
1426
1427 lines =<< trim END
1428 vim9script
1429 var SomeVar = 99
1430 class Child extends SomeVar
1431 this.two = 2
1432 endclass
1433 END
1434 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +00001435
1436 lines =<< trim END
1437 vim9script
1438 class Base
1439 this.name: string
1440 def ToString(): string
1441 return this.name
1442 enddef
1443 endclass
1444
1445 class Child extends Base
1446 this.age: number
1447 def ToString(): string
1448 return super.ToString() .. ': ' .. this.age
1449 enddef
1450 endclass
1451
1452 var o = Child.new('John', 42)
1453 assert_equal('John: 42', o.ToString())
1454 END
1455 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +00001456
1457 lines =<< trim END
1458 vim9script
1459 class Child
1460 this.age: number
1461 def ToString(): number
1462 return this.age
1463 enddef
1464 def ToString(): string
1465 return this.age
1466 enddef
1467 endclass
1468 END
1469 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
1470
1471 lines =<< trim END
1472 vim9script
1473 class Child
1474 this.age: number
1475 def ToString(): string
1476 return super .ToString() .. ': ' .. this.age
1477 enddef
1478 endclass
1479 var o = Child.new(42)
1480 echo o.ToString()
1481 END
1482 v9.CheckScriptFailure(lines, 'E1356:')
1483
1484 lines =<< trim END
1485 vim9script
1486 class Base
1487 this.name: string
1488 def ToString(): string
1489 return this.name
1490 enddef
1491 endclass
1492
1493 var age = 42
1494 def ToString(): string
1495 return super.ToString() .. ': ' .. age
1496 enddef
1497 echo ToString()
1498 END
1499 v9.CheckScriptFailure(lines, 'E1357:')
1500
1501 lines =<< trim END
1502 vim9script
1503 class Child
1504 this.age: number
1505 def ToString(): string
1506 return super.ToString() .. ': ' .. this.age
1507 enddef
1508 endclass
1509 var o = Child.new(42)
1510 echo o.ToString()
1511 END
1512 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001513
1514 lines =<< trim END
1515 vim9script
1516 class Base
1517 this.name: string
1518 static def ToString(): string
1519 return 'Base class'
1520 enddef
1521 endclass
1522
1523 class Child extends Base
1524 this.age: number
1525 def ToString(): string
1526 return Base.ToString() .. ': ' .. this.age
1527 enddef
1528 endclass
1529
1530 var o = Child.new('John', 42)
1531 assert_equal('Base class: 42', o.ToString())
1532 END
1533 v9.CheckScriptSuccess(lines)
Bram Moolenaar4cae8452023-01-15 15:51:48 +00001534
1535 lines =<< trim END
1536 vim9script
1537 class Base
1538 this.value = 1
1539 def new(init: number)
1540 this.value = number + 1
1541 enddef
1542 endclass
1543 class Child extends Base
1544 def new()
1545 this.new(3)
1546 enddef
1547 endclass
1548 var c = Child.new()
1549 END
1550 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Child": new(')
Bram Moolenaarae3205a2023-01-15 20:49:00 +00001551
1552 # base class with more than one object member
1553 lines =<< trim END
1554 vim9script
1555
1556 class Result
1557 this.success: bool
1558 this.value: any = null
1559 endclass
1560
1561 class Success extends Result
1562 def new(this.value = v:none)
1563 this.success = true
1564 enddef
1565 endclass
1566
1567 var v = Success.new('asdf')
1568 assert_equal("object of Success {success: true, value: 'asdf'}", string(v))
1569 END
1570 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +00001571enddef
1572
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00001573def Test_using_base_class()
1574 var lines =<< trim END
1575 vim9script
1576
1577 class BaseEE
1578 def Enter(): any
1579 return null
1580 enddef
1581 def Exit(resource: any): void
1582 enddef
1583 endclass
1584
1585 class ChildEE extends BaseEE
1586 def Enter(): any
1587 return 42
1588 enddef
1589
1590 def Exit(resource: number): void
1591 g:result ..= '/exit'
1592 enddef
1593 endclass
1594
1595 def With(ee: BaseEE)
1596 var r = ee.Enter()
1597 try
1598 g:result ..= r
1599 finally
1600 g:result ..= '/finally'
1601 ee.Exit(r)
1602 endtry
1603 enddef
1604
1605 g:result = ''
1606 With(ChildEE.new())
1607 assert_equal('42/finally/exit', g:result)
1608 END
1609 v9.CheckScriptSuccess(lines)
1610 unlet g:result
1611enddef
1612
1613
Bram Moolenaara86655a2023-01-12 17:06:27 +00001614def Test_class_import()
1615 var lines =<< trim END
1616 vim9script
1617 export class Animal
1618 this.kind: string
1619 this.name: string
1620 endclass
1621 END
1622 writefile(lines, 'Xanimal.vim', 'D')
1623
1624 lines =<< trim END
1625 vim9script
1626 import './Xanimal.vim' as animal
1627
1628 var a: animal.Animal
1629 a = animal.Animal.new('fish', 'Eric')
1630 assert_equal('fish', a.kind)
1631 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001632
1633 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1634 assert_equal('cat', b.kind)
1635 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001636 END
1637 v9.CheckScriptSuccess(lines)
1638enddef
1639
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001640def Test_abstract_class()
1641 var lines =<< trim END
1642 vim9script
1643 abstract class Base
1644 this.name: string
1645 endclass
1646 class Person extends Base
1647 this.age: number
1648 endclass
1649 var p: Base = Person.new('Peter', 42)
1650 assert_equal('Peter', p.name)
1651 assert_equal(42, p.age)
1652 END
1653 v9.CheckScriptSuccess(lines)
1654
1655 lines =<< trim END
1656 vim9script
1657 abstract class Base
1658 this.name: string
1659 endclass
1660 class Person extends Base
1661 this.age: number
1662 endclass
1663 var p = Base.new('Peter')
1664 END
1665 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Base": new(')
1666
1667 lines =<< trim END
1668 abstract class Base
1669 this.name: string
1670 endclass
1671 END
1672 v9.CheckScriptFailure(lines, 'E1316:')
1673enddef
1674
Bram Moolenaar486fc252023-01-18 14:51:07 +00001675def Test_closure_in_class()
1676 var lines =<< trim END
1677 vim9script
1678
1679 class Foo
1680 this.y: list<string> = ['B']
1681
1682 def new()
1683 g:result = filter(['A', 'B'], (_, v) => index(this.y, v) == -1)
1684 enddef
1685 endclass
1686
1687 Foo.new()
1688 assert_equal(['A'], g:result)
1689 END
1690 v9.CheckScriptSuccess(lines)
1691enddef
1692
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001693def Test_defer_with_object()
1694 var lines =<< trim END
1695 vim9script
1696
1697 class CWithEE
1698 def Enter()
1699 g:result ..= "entered/"
1700 enddef
1701 def Exit()
1702 g:result ..= "exited"
1703 enddef
1704 endclass
1705
1706 def With(ee: CWithEE, F: func)
1707 ee.Enter()
1708 defer ee.Exit()
1709 F()
1710 enddef
1711
1712 g:result = ''
1713 var obj = CWithEE.new()
1714 obj->With(() => {
1715 g:result ..= "called/"
1716 })
1717 assert_equal('entered/called/exited', g:result)
1718 END
1719 v9.CheckScriptSuccess(lines)
1720 unlet g:result
Bram Moolenaar313e4722023-02-08 20:55:27 +00001721
1722 lines =<< trim END
1723 vim9script
1724
1725 class BaseWithEE
1726 def Enter()
1727 g:result ..= "entered-base/"
1728 enddef
1729 def Exit()
1730 g:result ..= "exited-base"
1731 enddef
1732 endclass
1733
1734 class CWithEE extends BaseWithEE
1735 def Enter()
1736 g:result ..= "entered-child/"
1737 enddef
1738 def Exit()
1739 g:result ..= "exited-child"
1740 enddef
1741 endclass
1742
1743 def With(ee: BaseWithEE, F: func)
1744 ee.Enter()
1745 defer ee.Exit()
1746 F()
1747 enddef
1748
1749 g:result = ''
1750 var obj = CWithEE.new()
1751 obj->With(() => {
1752 g:result ..= "called/"
1753 })
1754 assert_equal('entered-child/called/exited-child', g:result)
1755 END
1756 v9.CheckScriptSuccess(lines)
1757 unlet g:result
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001758enddef
1759
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001760
1761" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker