blob: 74df0f4394e8ae0fa6ca839036e38ca4a5fccc8d [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
51 endclass school's out
52 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 Moolenaar00b28d62022-12-08 15:32:33 +0000158 END
159 v9.CheckScriptSuccess(lines)
160enddef
161
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000162def Test_class_member_initializer()
163 var lines =<< trim END
164 vim9script
165
166 class TextPosition
167 this.lnum: number = 1
168 this.col: number = 1
169
Bram Moolenaar418b5472022-12-20 13:38:22 +0000170 # constructor with only the line number
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000171 def new(lnum: number)
172 this.lnum = lnum
173 enddef
174 endclass
175
176 var pos = TextPosition.new(3)
177 assert_equal(3, pos.lnum)
178 assert_equal(1, pos.col)
179
180 var instr = execute('disassemble TextPosition.new')
181 assert_match('new\_s*' ..
Bram Moolenaar3ea8a1b2022-12-10 19:03:51 +0000182 '0 NEW TextPosition size \d\+\_s*' ..
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000183 '\d PUSHNR 1\_s*' ..
184 '\d STORE_THIS 0\_s*' ..
185 '\d PUSHNR 1\_s*' ..
186 '\d STORE_THIS 1\_s*' ..
187 'this.lnum = lnum\_s*' ..
188 '\d LOAD arg\[-1]\_s*' ..
189 '\d PUSHNR 0\_s*' ..
190 '\d LOAD $0\_s*' ..
191 '\d\+ STOREINDEX object\_s*' ..
192 '\d\+ RETURN object.*',
193 instr)
194 END
195 v9.CheckScriptSuccess(lines)
196enddef
197
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000198def Test_class_default_new()
199 var lines =<< trim END
200 vim9script
201
202 class TextPosition
203 this.lnum: number = 1
204 this.col: number = 1
205 endclass
206
207 var pos = TextPosition.new()
208 assert_equal(1, pos.lnum)
209 assert_equal(1, pos.col)
210
211 pos = TextPosition.new(v:none, v:none)
212 assert_equal(1, pos.lnum)
213 assert_equal(1, pos.col)
214
215 pos = TextPosition.new(3, 22)
216 assert_equal(3, pos.lnum)
217 assert_equal(22, pos.col)
218
219 pos = TextPosition.new(v:none, 33)
220 assert_equal(1, pos.lnum)
221 assert_equal(33, pos.col)
222 END
223 v9.CheckScriptSuccess(lines)
224
225 lines =<< trim END
226 vim9script
227 class Person
228 this.name: string
229 this.age: number = 42
230 this.education: string = "unknown"
231
232 def new(this.name, this.age = v:none, this.education = v:none)
233 enddef
234 endclass
235
236 var piet = Person.new("Piet")
237 assert_equal("Piet", piet.name)
238 assert_equal(42, piet.age)
239 assert_equal("unknown", piet.education)
240
241 var chris = Person.new("Chris", 4, "none")
242 assert_equal("Chris", chris.name)
243 assert_equal(4, chris.age)
244 assert_equal("none", chris.education)
245 END
246 v9.CheckScriptSuccess(lines)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000247
248 lines =<< trim END
249 vim9script
250 class Person
251 this.name: string
252 this.age: number = 42
253 this.education: string = "unknown"
254
255 def new(this.name, this.age = v:none, this.education = v:none)
256 enddef
257 endclass
258
259 var missing = Person.new()
260 END
261 v9.CheckScriptFailure(lines, 'E119:')
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000262enddef
263
Bram Moolenaar74e12742022-12-13 21:14:28 +0000264def Test_class_object_member_inits()
265 var lines =<< trim END
266 vim9script
267 class TextPosition
268 this.lnum: number
269 this.col = 1
270 this.addcol: number = 2
271 endclass
272
273 var pos = TextPosition.new()
274 assert_equal(0, pos.lnum)
275 assert_equal(1, pos.col)
276 assert_equal(2, pos.addcol)
277 END
278 v9.CheckScriptSuccess(lines)
279
280 lines =<< trim END
281 vim9script
282 class TextPosition
283 this.lnum
284 this.col = 1
285 endclass
286 END
287 v9.CheckScriptFailure(lines, 'E1022:')
288
289 lines =<< trim END
290 vim9script
291 class TextPosition
292 this.lnum = v:none
293 this.col = 1
294 endclass
295 END
296 v9.CheckScriptFailure(lines, 'E1330:')
297enddef
298
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000299def Test_class_object_member_access()
300 var lines =<< trim END
301 vim9script
302 class Triple
303 this._one = 1
304 this.two = 2
305 public this.three = 3
306
307 def GetOne(): number
308 return this._one
309 enddef
310 endclass
311
312 var trip = Triple.new()
313 assert_equal(1, trip.GetOne())
314 assert_equal(2, trip.two)
315 assert_equal(3, trip.three)
316 assert_fails('echo trip._one', 'E1333')
317
318 assert_fails('trip._one = 11', 'E1333')
319 assert_fails('trip.two = 22', 'E1335')
320 trip.three = 33
321 assert_equal(33, trip.three)
Bram Moolenaard505d172022-12-18 21:42:55 +0000322
323 assert_fails('trip.four = 4', 'E1334')
324 END
325 v9.CheckScriptSuccess(lines)
Bram Moolenaar590162c2022-12-24 21:24:06 +0000326
327 lines =<< trim END
328 vim9script
329
330 class MyCar
331 this.make: string
Bram Moolenaar574950d2023-01-03 19:08:50 +0000332 this.age = 5
Bram Moolenaar590162c2022-12-24 21:24:06 +0000333
334 def new(make_arg: string)
335 this.make = make_arg
336 enddef
337
338 def GetMake(): string
339 return $"make = {this.make}"
340 enddef
Bram Moolenaar574950d2023-01-03 19:08:50 +0000341 def GetAge(): number
342 return this.age
343 enddef
Bram Moolenaar590162c2022-12-24 21:24:06 +0000344 endclass
345
346 var c = MyCar.new("abc")
347 assert_equal('make = abc', c.GetMake())
348
349 c = MyCar.new("def")
350 assert_equal('make = def', c.GetMake())
351
352 var c2 = MyCar.new("123")
353 assert_equal('make = 123', c2.GetMake())
Bram Moolenaar574950d2023-01-03 19:08:50 +0000354
355 def CheckCar()
356 assert_equal("make = def", c.GetMake())
357 assert_equal(5, c.GetAge())
358 enddef
359 CheckCar()
Bram Moolenaar590162c2022-12-24 21:24:06 +0000360 END
361 v9.CheckScriptSuccess(lines)
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000362
363 lines =<< trim END
364 vim9script
365
366 class MyCar
367 this.make: string
368
369 def new(make_arg: string)
370 this.make = make_arg
371 enddef
372 endclass
373
374 var c = MyCar.new("abc")
375 var c = MyCar.new("def")
376 END
377 v9.CheckScriptFailure(lines, 'E1041:')
Bram Moolenaard505d172022-12-18 21:42:55 +0000378enddef
379
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000380def Test_class_object_compare()
381 var class_lines =<< trim END
382 vim9script
383 class Item
384 this.nr = 0
385 this.name = 'xx'
386 endclass
387 END
388
389 # used at the script level and in a compiled function
390 var test_lines =<< trim END
391 var i1 = Item.new()
392 assert_equal(i1, i1)
393 assert_true(i1 is i1)
394 var i2 = Item.new()
395 assert_equal(i1, i2)
396 assert_false(i1 is i2)
397 var i3 = Item.new(0, 'xx')
398 assert_equal(i1, i3)
399
400 var io1 = Item.new(1, 'xx')
401 assert_notequal(i1, io1)
402 var io2 = Item.new(0, 'yy')
403 assert_notequal(i1, io2)
404 END
405
406 v9.CheckScriptSuccess(class_lines + test_lines)
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000407 v9.CheckScriptSuccess(
408 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000409
410 for op in ['>', '>=', '<', '<=', '=~', '!~']
411 var op_lines = [
412 'var i1 = Item.new()',
413 'var i2 = Item.new()',
414 'echo i1 ' .. op .. ' i2',
415 ]
416 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000417 v9.CheckScriptFailure(class_lines
418 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000419 endfor
420enddef
421
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000422def Test_object_type()
423 var lines =<< trim END
424 vim9script
425
426 class One
427 this.one = 1
428 endclass
429 class Two
430 this.two = 2
431 endclass
432 class TwoMore extends Two
433 this.more = 9
434 endclass
435
436 var o: One = One.new()
437 var t: Two = Two.new()
438 var m: TwoMore = TwoMore.new()
439 var tm: Two = TwoMore.new()
440
441 t = m
442 END
443 v9.CheckScriptSuccess(lines)
444
445 lines =<< trim END
446 vim9script
447
448 class One
449 this.one = 1
450 endclass
451 class Two
452 this.two = 2
453 endclass
454
455 var o: One = Two.new()
456 END
457 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>')
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000458
459 lines =<< trim END
460 vim9script
461
462 interface One
463 def GetMember(): number
464 endinterface
465 class Two implements One
466 this.one = 1
467 def GetMember(): number
468 return this.one
469 enddef
470 endclass
471
472 var o: One = Two.new(5)
473 assert_equal(5, o.GetMember())
474 END
475 v9.CheckScriptSuccess(lines)
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000476enddef
477
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000478def Test_class_member()
479 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000480 var lines =<< trim END
481 vim9script
482 class TextPos
483 this.lnum = 1
484 this.col = 1
485 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000486 static _secret = 7
487 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000488
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000489 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000490 counter += nr
491 enddef
492 endclass
493
494 assert_equal(0, TextPos.counter)
495 TextPos.AddToCounter(3)
496 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000497 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000498
499 def GetCounter(): number
500 return TextPos.counter
501 enddef
502 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000503
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000504 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000505 assert_fails('TextPos.counter = 5', 'E1335:')
506 assert_fails('TextPos.counter += 5', 'E1335:')
507
508 assert_fails('echo TextPos._secret', 'E1333:')
509 assert_fails('TextPos._secret = 8', 'E1333:')
510
511 assert_equal(42, TextPos.anybody)
512 TextPos.anybody = 12
513 assert_equal(12, TextPos.anybody)
514 TextPos.anybody += 5
515 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000516 END
517 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000518
519 # check shadowing
520 lines =<< trim END
521 vim9script
522
523 class Some
524 static count = 0
525 def Method(count: number)
526 echo count
527 enddef
528 endclass
529
530 var s = Some.new()
531 s.Method(7)
532 END
533 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
534
535 lines =<< trim END
536 vim9script
537
538 class Some
539 static count = 0
540 def Method(arg: number)
541 var count = 3
542 echo arg count
543 enddef
544 endclass
545
546 var s = Some.new()
547 s.Method(7)
548 END
549 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000550enddef
551
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000552func Test_class_garbagecollect()
553 let lines =<< trim END
554 vim9script
555
556 class Point
557 this.p = [2, 3]
558 static pl = ['a', 'b']
559 static pd = {a: 'a', b: 'b'}
560 endclass
561
562 echo Point.pl Point.pd
563 call test_garbagecollect_now()
564 echo Point.pl Point.pd
565 END
566 call v9.CheckScriptSuccess(lines)
567endfunc
568
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000569def Test_class_function()
570 var lines =<< trim END
571 vim9script
572 class Value
573 this.value = 0
574 static objects = 0
575
576 def new(v: number)
577 this.value = v
578 ++objects
579 enddef
580
581 static def GetCount(): number
582 return objects
583 enddef
584 endclass
585
586 assert_equal(0, Value.GetCount())
587 var v1 = Value.new(2)
588 assert_equal(1, Value.GetCount())
589 var v2 = Value.new(7)
590 assert_equal(2, Value.GetCount())
591 END
592 v9.CheckScriptSuccess(lines)
593enddef
594
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000595def Test_class_object_to_string()
596 var lines =<< trim END
597 vim9script
598 class TextPosition
599 this.lnum = 1
600 this.col = 22
601 endclass
602
603 assert_equal("class TextPosition", string(TextPosition))
604
605 var pos = TextPosition.new()
606 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
607 END
608 v9.CheckScriptSuccess(lines)
609enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000610
Bram Moolenaar554d0312023-01-05 19:59:18 +0000611def Test_interface_basics()
612 var lines =<< trim END
613 vim9script
614 interface Something
615 this.value: string
616 static count: number
617 def GetCount(): number
618 endinterface
619 END
620 v9.CheckScriptSuccess(lines)
621
622 lines =<< trim END
623 interface SomethingWrong
624 static count = 7
625 endinterface
626 END
627 v9.CheckScriptFailure(lines, 'E1342:')
628
629 lines =<< trim END
630 vim9script
631
632 interface Some
633 static count: number
634 def Method(count: number)
635 endinterface
636 END
637 # TODO: this should give an error for "count" shadowing
638 v9.CheckScriptSuccess(lines)
639
640 lines =<< trim END
641 vim9script
642 interface somethingWrong
643 static count = 7
644 endinterface
645 END
646 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
647
648 lines =<< trim END
649 vim9script
650 interface SomethingWrong
651 this.value: string
652 static count = 7
653 def GetCount(): number
654 endinterface
655 END
656 v9.CheckScriptFailure(lines, 'E1344:')
657
658 lines =<< trim END
659 vim9script
660 interface SomethingWrong
661 this.value: string
662 static count: number
663 def GetCount(): number
664 return 5
665 enddef
666 endinterface
667 END
668 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
669enddef
670
Bram Moolenaar94674f22023-01-06 18:42:20 +0000671def Test_class_implements_interface()
672 var lines =<< trim END
673 vim9script
674
675 interface Some
676 static count: number
677 def Method(nr: number)
678 endinterface
679
680 class SomeImpl implements Some
681 static count: number
682 def Method(nr: number)
683 echo nr
684 enddef
685 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000686
687 interface Another
688 this.member: string
689 endinterface
690
691 class SomeImpl implements Some, Another
692 this.member = 'abc'
693 static count: number
694 def Method(nr: number)
695 echo nr
696 enddef
697 endclass
698
Bram Moolenaar94674f22023-01-06 18:42:20 +0000699 END
700 v9.CheckScriptSuccess(lines)
701
702 lines =<< trim END
703 vim9script
704
705 interface Some
706 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000707 endinterface
708
709 class SomeImpl implements Some implements Some
710 static count: number
711 endclass
712 END
713 v9.CheckScriptFailure(lines, 'E1350:')
714
715 lines =<< trim END
716 vim9script
717
718 interface Some
719 static counter: number
720 endinterface
721
722 class SomeImpl implements Some, Some
723 static count: number
724 endclass
725 END
726 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
727
728 lines =<< trim END
729 vim9script
730
731 interface Some
732 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +0000733 def Method(nr: number)
734 endinterface
735
736 class SomeImpl implements Some
737 static count: number
738 def Method(nr: number)
739 echo nr
740 enddef
741 endclass
742 END
743 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
744
745 lines =<< trim END
746 vim9script
747
748 interface Some
749 static count: number
750 def Methods(nr: number)
751 endinterface
752
753 class SomeImpl implements Some
754 static count: number
755 def Method(nr: number)
756 echo nr
757 enddef
758 endclass
759 END
760 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
761enddef
762
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000763def Test_class_used_as_type()
764 var lines =<< trim END
765 vim9script
766
767 class Point
768 this.x = 0
769 this.y = 0
770 endclass
771
772 var p: Point
773 p = Point.new(2, 33)
774 assert_equal(2, p.x)
775 assert_equal(33, p.y)
776 END
777 v9.CheckScriptSuccess(lines)
778
779 lines =<< trim END
780 vim9script
781
782 interface HasX
783 this.x: number
784 endinterface
785
786 class Point implements HasX
787 this.x = 0
788 this.y = 0
789 endclass
790
791 var p: Point
792 p = Point.new(2, 33)
793 var hx = p
794 assert_equal(2, hx.x)
795 END
796 v9.CheckScriptSuccess(lines)
797
798 lines =<< trim END
799 vim9script
800
801 class Point
802 this.x = 0
803 this.y = 0
804 endclass
805
806 var p: Point
807 p = 'text'
808 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000809 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000810enddef
811
Bram Moolenaar83677162023-01-08 19:54:10 +0000812def Test_class_extends()
813 var lines =<< trim END
814 vim9script
815 class Base
816 this.one = 1
817 def GetOne(): number
818 return this.one
819 enddef
820 endclass
821 class Child extends Base
822 this.two = 2
823 def GetTotal(): number
824 return this.one + this.two
825 enddef
826 endclass
827 var o = Child.new()
828 assert_equal(1, o.one)
829 assert_equal(2, o.two)
830 assert_equal(1, o.GetOne())
831 assert_equal(3, o.GetTotal())
832 END
833 v9.CheckScriptSuccess(lines)
834
835 lines =<< trim END
836 vim9script
837 class Base
838 this.one = 1
839 endclass
840 class Child extends Base
841 this.two = 2
842 endclass
843 var o = Child.new(3, 44)
844 assert_equal(3, o.one)
845 assert_equal(44, o.two)
846 END
847 v9.CheckScriptSuccess(lines)
848
849 lines =<< trim END
850 vim9script
851 class Base
852 this.one = 1
853 endclass
854 class Child extends Base extends Base
855 this.two = 2
856 endclass
857 END
858 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
859
860 lines =<< trim END
861 vim9script
862 class Child extends BaseClass
863 this.two = 2
864 endclass
865 END
866 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
867
868 lines =<< trim END
869 vim9script
870 var SomeVar = 99
871 class Child extends SomeVar
872 this.two = 2
873 endclass
874 END
875 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +0000876
877 lines =<< trim END
878 vim9script
879 class Base
880 this.name: string
881 def ToString(): string
882 return this.name
883 enddef
884 endclass
885
886 class Child extends Base
887 this.age: number
888 def ToString(): string
889 return super.ToString() .. ': ' .. this.age
890 enddef
891 endclass
892
893 var o = Child.new('John', 42)
894 assert_equal('John: 42', o.ToString())
895 END
896 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000897
898 lines =<< trim END
899 vim9script
900 class Child
901 this.age: number
902 def ToString(): number
903 return this.age
904 enddef
905 def ToString(): string
906 return this.age
907 enddef
908 endclass
909 END
910 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
911
912 lines =<< trim END
913 vim9script
914 class Child
915 this.age: number
916 def ToString(): string
917 return super .ToString() .. ': ' .. this.age
918 enddef
919 endclass
920 var o = Child.new(42)
921 echo o.ToString()
922 END
923 v9.CheckScriptFailure(lines, 'E1356:')
924
925 lines =<< trim END
926 vim9script
927 class Base
928 this.name: string
929 def ToString(): string
930 return this.name
931 enddef
932 endclass
933
934 var age = 42
935 def ToString(): string
936 return super.ToString() .. ': ' .. age
937 enddef
938 echo ToString()
939 END
940 v9.CheckScriptFailure(lines, 'E1357:')
941
942 lines =<< trim END
943 vim9script
944 class Child
945 this.age: number
946 def ToString(): string
947 return super.ToString() .. ': ' .. this.age
948 enddef
949 endclass
950 var o = Child.new(42)
951 echo o.ToString()
952 END
953 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000954
955 lines =<< trim END
956 vim9script
957 class Base
958 this.name: string
959 static def ToString(): string
960 return 'Base class'
961 enddef
962 endclass
963
964 class Child extends Base
965 this.age: number
966 def ToString(): string
967 return Base.ToString() .. ': ' .. this.age
968 enddef
969 endclass
970
971 var o = Child.new('John', 42)
972 assert_equal('Base class: 42', o.ToString())
973 END
974 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +0000975enddef
976
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000977
978" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker