blob: 1aee9e08dc12d42d64122d9f0426834e87d8c9c2 [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)
Bram Moolenaar22363c62023-04-24 17:15:25 +0100404
405 def AddToFoo(obj: Foo)
406 obj.x += 3
407 enddef
408
409 AddToFoo(f)
410 assert_equal(23, f.x)
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000411 END
412 v9.CheckScriptSuccess(lines)
413enddef
414
Bram Moolenaarf4508042023-01-15 16:54:57 +0000415def Test_list_of_objects()
416 var lines =<< trim END
417 vim9script
418
419 class Foo
420 def Add()
421 enddef
422 endclass
423
424 def ProcessList(fooList: list<Foo>)
425 for foo in fooList
426 foo.Add()
427 endfor
428 enddef
429
430 var l: list<Foo> = [Foo.new()]
431 ProcessList(l)
432 END
433 v9.CheckScriptSuccess(lines)
434enddef
435
Bram Moolenaar912bfee2023-01-15 20:18:55 +0000436def Test_expr_after_using_object()
437 var lines =<< trim END
438 vim9script
439
440 class Something
441 this.label: string = ''
442 endclass
443
444 def Foo(): Something
445 var v = Something.new()
446 echo 'in Foo(): ' .. typename(v)
447 return v
448 enddef
449
450 Foo()
451 END
452 v9.CheckScriptSuccess(lines)
453enddef
454
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000455def Test_class_default_new()
456 var lines =<< trim END
457 vim9script
458
459 class TextPosition
460 this.lnum: number = 1
461 this.col: number = 1
462 endclass
463
464 var pos = TextPosition.new()
465 assert_equal(1, pos.lnum)
466 assert_equal(1, pos.col)
467
468 pos = TextPosition.new(v:none, v:none)
469 assert_equal(1, pos.lnum)
470 assert_equal(1, pos.col)
471
472 pos = TextPosition.new(3, 22)
473 assert_equal(3, pos.lnum)
474 assert_equal(22, pos.col)
475
476 pos = TextPosition.new(v:none, 33)
477 assert_equal(1, pos.lnum)
478 assert_equal(33, pos.col)
479 END
480 v9.CheckScriptSuccess(lines)
481
482 lines =<< trim END
483 vim9script
484 class Person
485 this.name: string
486 this.age: number = 42
487 this.education: string = "unknown"
488
489 def new(this.name, this.age = v:none, this.education = v:none)
490 enddef
491 endclass
492
493 var piet = Person.new("Piet")
494 assert_equal("Piet", piet.name)
495 assert_equal(42, piet.age)
496 assert_equal("unknown", piet.education)
497
498 var chris = Person.new("Chris", 4, "none")
499 assert_equal("Chris", chris.name)
500 assert_equal(4, chris.age)
501 assert_equal("none", chris.education)
502 END
503 v9.CheckScriptSuccess(lines)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000504
505 lines =<< trim END
506 vim9script
507 class Person
508 this.name: string
509 this.age: number = 42
510 this.education: string = "unknown"
511
512 def new(this.name, this.age = v:none, this.education = v:none)
513 enddef
514 endclass
515
516 var missing = Person.new()
517 END
518 v9.CheckScriptFailure(lines, 'E119:')
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000519enddef
520
Bram Moolenaar74e12742022-12-13 21:14:28 +0000521def Test_class_object_member_inits()
522 var lines =<< trim END
523 vim9script
524 class TextPosition
525 this.lnum: number
526 this.col = 1
527 this.addcol: number = 2
528 endclass
529
530 var pos = TextPosition.new()
531 assert_equal(0, pos.lnum)
532 assert_equal(1, pos.col)
533 assert_equal(2, pos.addcol)
534 END
535 v9.CheckScriptSuccess(lines)
536
537 lines =<< trim END
538 vim9script
539 class TextPosition
540 this.lnum
541 this.col = 1
542 endclass
543 END
544 v9.CheckScriptFailure(lines, 'E1022:')
545
546 lines =<< trim END
547 vim9script
548 class TextPosition
549 this.lnum = v:none
550 this.col = 1
551 endclass
552 END
553 v9.CheckScriptFailure(lines, 'E1330:')
554enddef
555
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000556def Test_class_object_member_access()
557 var lines =<< trim END
558 vim9script
559 class Triple
560 this._one = 1
561 this.two = 2
562 public this.three = 3
563
564 def GetOne(): number
565 return this._one
566 enddef
567 endclass
568
569 var trip = Triple.new()
570 assert_equal(1, trip.GetOne())
571 assert_equal(2, trip.two)
572 assert_equal(3, trip.three)
573 assert_fails('echo trip._one', 'E1333')
574
575 assert_fails('trip._one = 11', 'E1333')
576 assert_fails('trip.two = 22', 'E1335')
577 trip.three = 33
578 assert_equal(33, trip.three)
Bram Moolenaard505d172022-12-18 21:42:55 +0000579
580 assert_fails('trip.four = 4', 'E1334')
581 END
582 v9.CheckScriptSuccess(lines)
Bram Moolenaar590162c2022-12-24 21:24:06 +0000583
584 lines =<< trim END
585 vim9script
586
587 class MyCar
588 this.make: string
Bram Moolenaar574950d2023-01-03 19:08:50 +0000589 this.age = 5
Bram Moolenaar590162c2022-12-24 21:24:06 +0000590
591 def new(make_arg: string)
592 this.make = make_arg
593 enddef
594
595 def GetMake(): string
596 return $"make = {this.make}"
597 enddef
Bram Moolenaar574950d2023-01-03 19:08:50 +0000598 def GetAge(): number
599 return this.age
600 enddef
Bram Moolenaar590162c2022-12-24 21:24:06 +0000601 endclass
602
603 var c = MyCar.new("abc")
604 assert_equal('make = abc', c.GetMake())
605
606 c = MyCar.new("def")
607 assert_equal('make = def', c.GetMake())
608
609 var c2 = MyCar.new("123")
610 assert_equal('make = 123', c2.GetMake())
Bram Moolenaar574950d2023-01-03 19:08:50 +0000611
612 def CheckCar()
613 assert_equal("make = def", c.GetMake())
614 assert_equal(5, c.GetAge())
615 enddef
616 CheckCar()
Bram Moolenaar590162c2022-12-24 21:24:06 +0000617 END
618 v9.CheckScriptSuccess(lines)
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000619
620 lines =<< trim END
621 vim9script
622
623 class MyCar
624 this.make: string
625
626 def new(make_arg: string)
627 this.make = make_arg
628 enddef
629 endclass
630
631 var c = MyCar.new("abc")
632 var c = MyCar.new("def")
633 END
634 v9.CheckScriptFailure(lines, 'E1041:')
Bram Moolenaarb149d222023-01-24 13:03:37 +0000635
636 lines =<< trim END
637 vim9script
638
639 class Foo
640 this.x: list<number> = []
641
642 def Add(n: number): any
643 this.x->add(n)
644 return this
645 enddef
646 endclass
647
648 echo Foo.new().Add(1).Add(2).x
649 echo Foo.new().Add(1).Add(2)
650 .x
651 echo Foo.new().Add(1)
652 .Add(2).x
653 echo Foo.new()
654 .Add(1).Add(2).x
655 echo Foo.new()
656 .Add(1)
657 .Add(2)
658 .x
659 END
660 v9.CheckScriptSuccess(lines)
Bram Moolenaard505d172022-12-18 21:42:55 +0000661enddef
662
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000663def Test_class_object_compare()
664 var class_lines =<< trim END
665 vim9script
666 class Item
667 this.nr = 0
668 this.name = 'xx'
669 endclass
670 END
671
672 # used at the script level and in a compiled function
673 var test_lines =<< trim END
674 var i1 = Item.new()
675 assert_equal(i1, i1)
676 assert_true(i1 is i1)
677 var i2 = Item.new()
678 assert_equal(i1, i2)
679 assert_false(i1 is i2)
680 var i3 = Item.new(0, 'xx')
681 assert_equal(i1, i3)
682
683 var io1 = Item.new(1, 'xx')
684 assert_notequal(i1, io1)
685 var io2 = Item.new(0, 'yy')
686 assert_notequal(i1, io2)
687 END
688
689 v9.CheckScriptSuccess(class_lines + test_lines)
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000690 v9.CheckScriptSuccess(
691 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000692
693 for op in ['>', '>=', '<', '<=', '=~', '!~']
694 var op_lines = [
695 'var i1 = Item.new()',
696 'var i2 = Item.new()',
697 'echo i1 ' .. op .. ' i2',
698 ]
699 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000700 v9.CheckScriptFailure(class_lines
701 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000702 endfor
703enddef
704
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000705def Test_object_type()
706 var lines =<< trim END
707 vim9script
708
709 class One
710 this.one = 1
711 endclass
712 class Two
713 this.two = 2
714 endclass
715 class TwoMore extends Two
716 this.more = 9
717 endclass
718
719 var o: One = One.new()
720 var t: Two = Two.new()
721 var m: TwoMore = TwoMore.new()
722 var tm: Two = TwoMore.new()
723
724 t = m
725 END
726 v9.CheckScriptSuccess(lines)
727
728 lines =<< trim END
729 vim9script
730
731 class One
732 this.one = 1
733 endclass
734 class Two
735 this.two = 2
736 endclass
737
738 var o: One = Two.new()
739 END
740 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>')
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000741
742 lines =<< trim END
743 vim9script
744
745 interface One
746 def GetMember(): number
747 endinterface
748 class Two implements One
749 this.one = 1
750 def GetMember(): number
751 return this.one
752 enddef
753 endclass
754
755 var o: One = Two.new(5)
756 assert_equal(5, o.GetMember())
757 END
758 v9.CheckScriptSuccess(lines)
Bram Moolenaar450c7a92023-01-16 16:39:37 +0000759
760 lines =<< trim END
761 vim9script
762
763 class Num
764 this.n: number = 0
765 endclass
766
767 def Ref(name: string): func(Num): Num
768 return (arg: Num): Num => {
769 return eval(name)(arg)
770 }
771 enddef
772
773 const Fn = Ref('Double')
774 var Double = (m: Num): Num => Num.new(m.n * 2)
775
776 echo Fn(Num.new(4))
777 END
778 v9.CheckScriptSuccess(lines)
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000779enddef
780
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000781def Test_class_member()
782 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000783 var lines =<< trim END
784 vim9script
785 class TextPos
786 this.lnum = 1
787 this.col = 1
788 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000789 static _secret = 7
790 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000791
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000792 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000793 counter += nr
794 enddef
795 endclass
796
797 assert_equal(0, TextPos.counter)
798 TextPos.AddToCounter(3)
799 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000800 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000801
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000802 def GetCounter(): number
803 return TextPos.counter
804 enddef
805 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000806
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000807 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000808 assert_fails('TextPos.counter = 5', 'E1335:')
809 assert_fails('TextPos.counter += 5', 'E1335:')
810
811 assert_fails('echo TextPos._secret', 'E1333:')
812 assert_fails('TextPos._secret = 8', 'E1333:')
813
814 assert_equal(42, TextPos.anybody)
815 TextPos.anybody = 12
816 assert_equal(12, TextPos.anybody)
817 TextPos.anybody += 5
818 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000819 END
820 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000821
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000822 # example in the help
823 lines =<< trim END
824 vim9script
825 class OtherThing
826 this.size: number
827 static totalSize: number
828
829 def new(this.size)
830 totalSize += this.size
831 enddef
832 endclass
833 assert_equal(0, OtherThing.totalSize)
834 var to3 = OtherThing.new(3)
835 assert_equal(3, OtherThing.totalSize)
836 var to7 = OtherThing.new(7)
837 assert_equal(10, OtherThing.totalSize)
838 END
839 v9.CheckScriptSuccess(lines)
840
Bram Moolenaar62a69232023-01-24 15:07:04 +0000841 # access private member in lambda
842 lines =<< trim END
843 vim9script
844
845 class Foo
846 this._x: number = 0
847
848 def Add(n: number): number
849 const F = (): number => this._x + n
850 return F()
851 enddef
852 endclass
853
854 var foo = Foo.new()
855 assert_equal(5, foo.Add(5))
856 END
857 v9.CheckScriptSuccess(lines)
858
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000859 # check shadowing
860 lines =<< trim END
861 vim9script
862
863 class Some
864 static count = 0
865 def Method(count: number)
866 echo count
867 enddef
868 endclass
869
870 var s = Some.new()
871 s.Method(7)
872 END
873 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
874
875 lines =<< trim END
876 vim9script
877
878 class Some
879 static count = 0
880 def Method(arg: number)
881 var count = 3
882 echo arg count
883 enddef
884 endclass
885
886 var s = Some.new()
887 s.Method(7)
888 END
889 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000890enddef
891
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000892func Test_class_garbagecollect()
893 let lines =<< trim END
894 vim9script
895
896 class Point
897 this.p = [2, 3]
898 static pl = ['a', 'b']
899 static pd = {a: 'a', b: 'b'}
900 endclass
901
902 echo Point.pl Point.pd
903 call test_garbagecollect_now()
904 echo Point.pl Point.pd
905 END
906 call v9.CheckScriptSuccess(lines)
907endfunc
908
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000909def Test_class_function()
910 var lines =<< trim END
911 vim9script
912 class Value
913 this.value = 0
914 static objects = 0
915
916 def new(v: number)
917 this.value = v
918 ++objects
919 enddef
920
921 static def GetCount(): number
922 return objects
923 enddef
924 endclass
925
926 assert_equal(0, Value.GetCount())
927 var v1 = Value.new(2)
928 assert_equal(1, Value.GetCount())
929 var v2 = Value.new(7)
930 assert_equal(2, Value.GetCount())
931 END
932 v9.CheckScriptSuccess(lines)
933enddef
934
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +0000935def Test_class_defcompile()
936 var lines =<< trim END
937 vim9script
938
939 class C
940 def Fo(i: number): string
941 return i
942 enddef
943 endclass
944
945 defcompile C.Fo
946 END
947 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got number')
948
949 lines =<< trim END
950 vim9script
951
952 class C
953 static def Fc(): number
954 return 'x'
955 enddef
956 endclass
957
958 defcompile C.Fc
959 END
960 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected number but got string')
961enddef
962
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000963def Test_class_object_to_string()
964 var lines =<< trim END
965 vim9script
966 class TextPosition
967 this.lnum = 1
968 this.col = 22
969 endclass
970
971 assert_equal("class TextPosition", string(TextPosition))
972
973 var pos = TextPosition.new()
974 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
975 END
976 v9.CheckScriptSuccess(lines)
977enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000978
Bram Moolenaar554d0312023-01-05 19:59:18 +0000979def Test_interface_basics()
980 var lines =<< trim END
981 vim9script
982 interface Something
983 this.value: string
984 static count: number
985 def GetCount(): number
986 endinterface
987 END
988 v9.CheckScriptSuccess(lines)
989
990 lines =<< trim END
991 interface SomethingWrong
992 static count = 7
993 endinterface
994 END
995 v9.CheckScriptFailure(lines, 'E1342:')
996
997 lines =<< trim END
998 vim9script
999
1000 interface Some
1001 static count: number
1002 def Method(count: number)
1003 endinterface
1004 END
h-east61378a12023-04-18 19:07:29 +01001005 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count', 5)
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001006
1007 lines =<< trim END
1008 vim9script
1009
1010 interface Some
1011 this.value: number
1012 def Method(value: number)
1013 endinterface
1014 END
h-east61378a12023-04-18 19:07:29 +01001015 # The argument name and the object member name are the same, but this is not a
1016 # problem because object members are always accessed with the "this." prefix.
1017 v9.CheckScriptSuccess(lines)
Bram Moolenaar554d0312023-01-05 19:59:18 +00001018
1019 lines =<< trim END
1020 vim9script
1021 interface somethingWrong
1022 static count = 7
1023 endinterface
1024 END
1025 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
1026
1027 lines =<< trim END
1028 vim9script
1029 interface SomethingWrong
1030 this.value: string
1031 static count = 7
1032 def GetCount(): number
1033 endinterface
1034 END
1035 v9.CheckScriptFailure(lines, 'E1344:')
1036
1037 lines =<< trim END
1038 vim9script
1039 interface SomethingWrong
1040 this.value: string
1041 static count: number
1042 def GetCount(): number
1043 return 5
1044 enddef
1045 endinterface
1046 END
1047 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
Bram Moolenaar53f54e42023-01-26 20:36:56 +00001048
1049 lines =<< trim END
1050 vim9script
1051 export interface EnterExit
1052 def Enter(): void
1053 def Exit(): void
1054 endinterface
1055 END
1056 writefile(lines, 'XdefIntf.vim', 'D')
1057
1058 lines =<< trim END
1059 vim9script
1060 import './XdefIntf.vim' as defIntf
1061 export def With(ee: defIntf.EnterExit, F: func)
1062 ee.Enter()
1063 try
1064 F()
1065 finally
1066 ee.Exit()
1067 endtry
1068 enddef
1069 END
1070 v9.CheckScriptSuccess(lines)
Bram Moolenaar657aea72023-01-27 13:16:19 +00001071
1072 var imported =<< trim END
1073 vim9script
1074 export abstract class EnterExit
1075 def Enter(): void
1076 enddef
1077 def Exit(): void
1078 enddef
1079 endclass
1080 END
1081 writefile(imported, 'XdefIntf2.vim', 'D')
1082
1083 lines[1] = " import './XdefIntf2.vim' as defIntf"
1084 v9.CheckScriptSuccess(lines)
Bram Moolenaar554d0312023-01-05 19:59:18 +00001085enddef
1086
Bram Moolenaar94674f22023-01-06 18:42:20 +00001087def Test_class_implements_interface()
1088 var lines =<< trim END
1089 vim9script
1090
1091 interface Some
1092 static count: number
1093 def Method(nr: number)
1094 endinterface
1095
1096 class SomeImpl implements Some
1097 static count: number
1098 def Method(nr: number)
1099 echo nr
1100 enddef
1101 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001102
1103 interface Another
1104 this.member: string
1105 endinterface
1106
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001107 class AnotherImpl implements Some, Another
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001108 this.member = 'abc'
1109 static count: number
1110 def Method(nr: number)
1111 echo nr
1112 enddef
1113 endclass
Bram Moolenaar94674f22023-01-06 18:42:20 +00001114 END
1115 v9.CheckScriptSuccess(lines)
1116
1117 lines =<< trim END
1118 vim9script
1119
1120 interface Some
1121 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001122 endinterface
1123
1124 class SomeImpl implements Some implements Some
1125 static count: number
1126 endclass
1127 END
1128 v9.CheckScriptFailure(lines, 'E1350:')
1129
1130 lines =<< trim END
1131 vim9script
1132
1133 interface Some
1134 static counter: number
1135 endinterface
1136
1137 class SomeImpl implements Some, Some
1138 static count: number
1139 endclass
1140 END
1141 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
1142
1143 lines =<< trim END
1144 vim9script
1145
1146 interface Some
1147 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +00001148 def Method(nr: number)
1149 endinterface
1150
1151 class SomeImpl implements Some
1152 static count: number
1153 def Method(nr: number)
1154 echo nr
1155 enddef
1156 endclass
1157 END
1158 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
1159
1160 lines =<< trim END
1161 vim9script
1162
1163 interface Some
1164 static count: number
1165 def Methods(nr: number)
1166 endinterface
1167
1168 class SomeImpl implements Some
1169 static count: number
1170 def Method(nr: number)
1171 echo nr
1172 enddef
1173 endclass
1174 END
1175 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001176
1177 # Check different order of members in class and interface works.
1178 lines =<< trim END
1179 vim9script
1180
1181 interface Result
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001182 public this.label: string
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001183 this.errpos: number
1184 endinterface
1185
1186 # order of members is opposite of interface
1187 class Failure implements Result
1188 this.errpos: number = 42
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001189 public this.label: string = 'label'
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001190 endclass
1191
1192 def Test()
1193 var result: Result = Failure.new()
1194
1195 assert_equal('label', result.label)
1196 assert_equal(42, result.errpos)
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00001197
1198 result.label = 'different'
1199 assert_equal('different', result.label)
1200 assert_equal(42, result.errpos)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001201 enddef
1202
1203 Test()
1204 END
1205 v9.CheckScriptSuccess(lines)
Bram Moolenaar94674f22023-01-06 18:42:20 +00001206enddef
1207
Bram Moolenaard0200c82023-01-28 15:19:40 +00001208def Test_call_interface_method()
1209 var lines =<< trim END
1210 vim9script
1211 interface Base
1212 def Enter(): void
1213 endinterface
1214
1215 class Child implements Base
1216 def Enter(): void
1217 g:result ..= 'child'
1218 enddef
1219 endclass
1220
1221 def F(obj: Base)
1222 obj.Enter()
1223 enddef
1224
1225 g:result = ''
1226 F(Child.new())
1227 assert_equal('child', g:result)
1228 unlet g:result
1229 END
1230 v9.CheckScriptSuccess(lines)
1231
1232 lines =<< trim END
1233 vim9script
1234 class Base
1235 def Enter(): void
1236 g:result ..= 'base'
1237 enddef
1238 endclass
1239
1240 class Child extends Base
1241 def Enter(): void
1242 g:result ..= 'child'
1243 enddef
1244 endclass
1245
1246 def F(obj: Base)
1247 obj.Enter()
1248 enddef
1249
1250 g:result = ''
1251 F(Child.new())
1252 assert_equal('child', g:result)
1253 unlet g:result
1254 END
1255 v9.CheckScriptSuccess(lines)
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00001256
Bram Moolenaar7a1bdae2023-02-04 15:45:27 +00001257 # method of interface returns a value
1258 lines =<< trim END
1259 vim9script
1260 interface Base
1261 def Enter(): string
1262 endinterface
1263
1264 class Child implements Base
1265 def Enter(): string
1266 g:result ..= 'child'
1267 return "/resource"
1268 enddef
1269 endclass
1270
1271 def F(obj: Base)
1272 var r = obj.Enter()
1273 g:result ..= r
1274 enddef
1275
1276 g:result = ''
1277 F(Child.new())
1278 assert_equal('child/resource', g:result)
1279 unlet g:result
1280 END
1281 v9.CheckScriptSuccess(lines)
1282
1283 lines =<< trim END
1284 vim9script
1285 class Base
1286 def Enter(): string
1287 return null_string
1288 enddef
1289 endclass
1290
1291 class Child extends Base
1292 def Enter(): string
1293 g:result ..= 'child'
1294 return "/resource"
1295 enddef
1296 endclass
1297
1298 def F(obj: Base)
1299 var r = obj.Enter()
1300 g:result ..= r
1301 enddef
1302
1303 g:result = ''
1304 F(Child.new())
1305 assert_equal('child/resource', g:result)
1306 unlet g:result
1307 END
1308 v9.CheckScriptSuccess(lines)
1309
1310
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00001311 # No class that implements the interface.
1312 lines =<< trim END
1313 vim9script
1314
1315 interface IWithEE
1316 def Enter(): any
1317 def Exit(): void
1318 endinterface
1319
1320 def With1(ee: IWithEE, F: func)
1321 var r = ee.Enter()
1322 enddef
1323
1324 defcompile
1325 END
1326 v9.CheckScriptSuccess(lines)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001327enddef
1328
Bram Moolenaareca2c5f2023-01-07 12:08:41 +00001329def Test_class_used_as_type()
1330 var lines =<< trim END
1331 vim9script
1332
1333 class Point
1334 this.x = 0
1335 this.y = 0
1336 endclass
1337
1338 var p: Point
1339 p = Point.new(2, 33)
1340 assert_equal(2, p.x)
1341 assert_equal(33, p.y)
1342 END
1343 v9.CheckScriptSuccess(lines)
1344
1345 lines =<< trim END
1346 vim9script
1347
1348 interface HasX
1349 this.x: number
1350 endinterface
1351
1352 class Point implements HasX
1353 this.x = 0
1354 this.y = 0
1355 endclass
1356
1357 var p: Point
1358 p = Point.new(2, 33)
1359 var hx = p
1360 assert_equal(2, hx.x)
1361 END
1362 v9.CheckScriptSuccess(lines)
1363
1364 lines =<< trim END
1365 vim9script
1366
1367 class Point
1368 this.x = 0
1369 this.y = 0
1370 endclass
1371
1372 var p: Point
1373 p = 'text'
1374 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001375 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +00001376enddef
1377
Bram Moolenaar83677162023-01-08 19:54:10 +00001378def Test_class_extends()
1379 var lines =<< trim END
1380 vim9script
1381 class Base
1382 this.one = 1
1383 def GetOne(): number
1384 return this.one
1385 enddef
1386 endclass
1387 class Child extends Base
1388 this.two = 2
1389 def GetTotal(): number
1390 return this.one + this.two
1391 enddef
1392 endclass
1393 var o = Child.new()
1394 assert_equal(1, o.one)
1395 assert_equal(2, o.two)
1396 assert_equal(1, o.GetOne())
1397 assert_equal(3, o.GetTotal())
1398 END
1399 v9.CheckScriptSuccess(lines)
1400
1401 lines =<< trim END
1402 vim9script
1403 class Base
1404 this.one = 1
1405 endclass
1406 class Child extends Base
1407 this.two = 2
1408 endclass
1409 var o = Child.new(3, 44)
1410 assert_equal(3, o.one)
1411 assert_equal(44, o.two)
1412 END
1413 v9.CheckScriptSuccess(lines)
1414
1415 lines =<< trim END
1416 vim9script
1417 class Base
1418 this.one = 1
1419 endclass
1420 class Child extends Base extends Base
1421 this.two = 2
1422 endclass
1423 END
1424 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
1425
1426 lines =<< trim END
1427 vim9script
1428 class Child extends BaseClass
1429 this.two = 2
1430 endclass
1431 END
1432 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
1433
1434 lines =<< trim END
1435 vim9script
1436 var SomeVar = 99
1437 class Child extends SomeVar
1438 this.two = 2
1439 endclass
1440 END
1441 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +00001442
1443 lines =<< trim END
1444 vim9script
1445 class Base
1446 this.name: string
1447 def ToString(): string
1448 return this.name
1449 enddef
1450 endclass
1451
1452 class Child extends Base
1453 this.age: number
1454 def ToString(): string
1455 return super.ToString() .. ': ' .. this.age
1456 enddef
1457 endclass
1458
1459 var o = Child.new('John', 42)
1460 assert_equal('John: 42', o.ToString())
1461 END
1462 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +00001463
1464 lines =<< trim END
1465 vim9script
1466 class Child
1467 this.age: number
1468 def ToString(): number
1469 return this.age
1470 enddef
1471 def ToString(): string
1472 return this.age
1473 enddef
1474 endclass
1475 END
1476 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
1477
1478 lines =<< trim END
1479 vim9script
1480 class Child
1481 this.age: number
1482 def ToString(): string
1483 return super .ToString() .. ': ' .. this.age
1484 enddef
1485 endclass
1486 var o = Child.new(42)
1487 echo o.ToString()
1488 END
1489 v9.CheckScriptFailure(lines, 'E1356:')
1490
1491 lines =<< trim END
1492 vim9script
1493 class Base
1494 this.name: string
1495 def ToString(): string
1496 return this.name
1497 enddef
1498 endclass
1499
1500 var age = 42
1501 def ToString(): string
1502 return super.ToString() .. ': ' .. age
1503 enddef
1504 echo ToString()
1505 END
1506 v9.CheckScriptFailure(lines, 'E1357:')
1507
1508 lines =<< trim END
1509 vim9script
1510 class Child
1511 this.age: number
1512 def ToString(): string
1513 return super.ToString() .. ': ' .. this.age
1514 enddef
1515 endclass
1516 var o = Child.new(42)
1517 echo o.ToString()
1518 END
1519 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001520
1521 lines =<< trim END
1522 vim9script
1523 class Base
1524 this.name: string
1525 static def ToString(): string
1526 return 'Base class'
1527 enddef
1528 endclass
1529
1530 class Child extends Base
1531 this.age: number
1532 def ToString(): string
1533 return Base.ToString() .. ': ' .. this.age
1534 enddef
1535 endclass
1536
1537 var o = Child.new('John', 42)
1538 assert_equal('Base class: 42', o.ToString())
1539 END
1540 v9.CheckScriptSuccess(lines)
Bram Moolenaar4cae8452023-01-15 15:51:48 +00001541
1542 lines =<< trim END
1543 vim9script
1544 class Base
1545 this.value = 1
1546 def new(init: number)
1547 this.value = number + 1
1548 enddef
1549 endclass
1550 class Child extends Base
1551 def new()
1552 this.new(3)
1553 enddef
1554 endclass
1555 var c = Child.new()
1556 END
1557 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Child": new(')
Bram Moolenaarae3205a2023-01-15 20:49:00 +00001558
1559 # base class with more than one object member
1560 lines =<< trim END
1561 vim9script
1562
1563 class Result
1564 this.success: bool
1565 this.value: any = null
1566 endclass
1567
1568 class Success extends Result
1569 def new(this.value = v:none)
1570 this.success = true
1571 enddef
1572 endclass
1573
1574 var v = Success.new('asdf')
1575 assert_equal("object of Success {success: true, value: 'asdf'}", string(v))
1576 END
1577 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +00001578enddef
1579
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00001580def Test_using_base_class()
1581 var lines =<< trim END
1582 vim9script
1583
1584 class BaseEE
1585 def Enter(): any
1586 return null
1587 enddef
1588 def Exit(resource: any): void
1589 enddef
1590 endclass
1591
1592 class ChildEE extends BaseEE
1593 def Enter(): any
1594 return 42
1595 enddef
1596
1597 def Exit(resource: number): void
1598 g:result ..= '/exit'
1599 enddef
1600 endclass
1601
1602 def With(ee: BaseEE)
1603 var r = ee.Enter()
1604 try
1605 g:result ..= r
1606 finally
1607 g:result ..= '/finally'
1608 ee.Exit(r)
1609 endtry
1610 enddef
1611
1612 g:result = ''
1613 With(ChildEE.new())
1614 assert_equal('42/finally/exit', g:result)
1615 END
1616 v9.CheckScriptSuccess(lines)
1617 unlet g:result
1618enddef
1619
1620
Bram Moolenaara86655a2023-01-12 17:06:27 +00001621def Test_class_import()
1622 var lines =<< trim END
1623 vim9script
1624 export class Animal
1625 this.kind: string
1626 this.name: string
1627 endclass
1628 END
1629 writefile(lines, 'Xanimal.vim', 'D')
1630
1631 lines =<< trim END
1632 vim9script
1633 import './Xanimal.vim' as animal
1634
1635 var a: animal.Animal
1636 a = animal.Animal.new('fish', 'Eric')
1637 assert_equal('fish', a.kind)
1638 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001639
1640 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1641 assert_equal('cat', b.kind)
1642 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001643 END
1644 v9.CheckScriptSuccess(lines)
1645enddef
1646
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001647def Test_abstract_class()
1648 var lines =<< trim END
1649 vim9script
1650 abstract class Base
1651 this.name: string
1652 endclass
1653 class Person extends Base
1654 this.age: number
1655 endclass
1656 var p: Base = Person.new('Peter', 42)
1657 assert_equal('Peter', p.name)
1658 assert_equal(42, p.age)
1659 END
1660 v9.CheckScriptSuccess(lines)
1661
1662 lines =<< trim END
1663 vim9script
1664 abstract class Base
1665 this.name: string
1666 endclass
1667 class Person extends Base
1668 this.age: number
1669 endclass
1670 var p = Base.new('Peter')
1671 END
1672 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Base": new(')
1673
1674 lines =<< trim END
1675 abstract class Base
1676 this.name: string
1677 endclass
1678 END
1679 v9.CheckScriptFailure(lines, 'E1316:')
1680enddef
1681
Bram Moolenaar486fc252023-01-18 14:51:07 +00001682def Test_closure_in_class()
1683 var lines =<< trim END
1684 vim9script
1685
1686 class Foo
1687 this.y: list<string> = ['B']
1688
1689 def new()
1690 g:result = filter(['A', 'B'], (_, v) => index(this.y, v) == -1)
1691 enddef
1692 endclass
1693
1694 Foo.new()
1695 assert_equal(['A'], g:result)
1696 END
1697 v9.CheckScriptSuccess(lines)
1698enddef
1699
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001700def Test_defer_with_object()
1701 var lines =<< trim END
1702 vim9script
1703
1704 class CWithEE
1705 def Enter()
1706 g:result ..= "entered/"
1707 enddef
1708 def Exit()
1709 g:result ..= "exited"
1710 enddef
1711 endclass
1712
1713 def With(ee: CWithEE, F: func)
1714 ee.Enter()
1715 defer ee.Exit()
1716 F()
1717 enddef
1718
1719 g:result = ''
1720 var obj = CWithEE.new()
1721 obj->With(() => {
1722 g:result ..= "called/"
1723 })
1724 assert_equal('entered/called/exited', g:result)
1725 END
1726 v9.CheckScriptSuccess(lines)
1727 unlet g:result
Bram Moolenaar313e4722023-02-08 20:55:27 +00001728
1729 lines =<< trim END
1730 vim9script
1731
1732 class BaseWithEE
1733 def Enter()
1734 g:result ..= "entered-base/"
1735 enddef
1736 def Exit()
1737 g:result ..= "exited-base"
1738 enddef
1739 endclass
1740
1741 class CWithEE extends BaseWithEE
1742 def Enter()
1743 g:result ..= "entered-child/"
1744 enddef
1745 def Exit()
1746 g:result ..= "exited-child"
1747 enddef
1748 endclass
1749
1750 def With(ee: BaseWithEE, F: func)
1751 ee.Enter()
1752 defer ee.Exit()
1753 F()
1754 enddef
1755
1756 g:result = ''
1757 var obj = CWithEE.new()
1758 obj->With(() => {
1759 g:result ..= "called/"
1760 })
1761 assert_equal('entered-child/called/exited-child', g:result)
1762 END
1763 v9.CheckScriptSuccess(lines)
1764 unlet g:result
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001765enddef
1766
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001767
1768" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker