blob: 4c10fefd9ebbd7422a1bb4380ebfb3d5c316e3e0 [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 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 Moolenaar7ce7daf2022-12-10 18:42:12 +0000167def Test_class_member_initializer()
168 var lines =<< trim END
169 vim9script
170
171 class TextPosition
172 this.lnum: number = 1
173 this.col: number = 1
174
Bram Moolenaar418b5472022-12-20 13:38:22 +0000175 # constructor with only the line number
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000176 def new(lnum: number)
177 this.lnum = lnum
178 enddef
179 endclass
180
181 var pos = TextPosition.new(3)
182 assert_equal(3, pos.lnum)
183 assert_equal(1, pos.col)
184
185 var instr = execute('disassemble TextPosition.new')
186 assert_match('new\_s*' ..
Bram Moolenaar3ea8a1b2022-12-10 19:03:51 +0000187 '0 NEW TextPosition size \d\+\_s*' ..
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000188 '\d PUSHNR 1\_s*' ..
189 '\d STORE_THIS 0\_s*' ..
190 '\d PUSHNR 1\_s*' ..
191 '\d STORE_THIS 1\_s*' ..
192 'this.lnum = lnum\_s*' ..
193 '\d LOAD arg\[-1]\_s*' ..
194 '\d PUSHNR 0\_s*' ..
195 '\d LOAD $0\_s*' ..
196 '\d\+ STOREINDEX object\_s*' ..
197 '\d\+ RETURN object.*',
198 instr)
199 END
200 v9.CheckScriptSuccess(lines)
201enddef
202
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000203def Test_class_default_new()
204 var lines =<< trim END
205 vim9script
206
207 class TextPosition
208 this.lnum: number = 1
209 this.col: number = 1
210 endclass
211
212 var pos = TextPosition.new()
213 assert_equal(1, pos.lnum)
214 assert_equal(1, pos.col)
215
216 pos = TextPosition.new(v:none, v:none)
217 assert_equal(1, pos.lnum)
218 assert_equal(1, pos.col)
219
220 pos = TextPosition.new(3, 22)
221 assert_equal(3, pos.lnum)
222 assert_equal(22, pos.col)
223
224 pos = TextPosition.new(v:none, 33)
225 assert_equal(1, pos.lnum)
226 assert_equal(33, pos.col)
227 END
228 v9.CheckScriptSuccess(lines)
229
230 lines =<< trim END
231 vim9script
232 class Person
233 this.name: string
234 this.age: number = 42
235 this.education: string = "unknown"
236
237 def new(this.name, this.age = v:none, this.education = v:none)
238 enddef
239 endclass
240
241 var piet = Person.new("Piet")
242 assert_equal("Piet", piet.name)
243 assert_equal(42, piet.age)
244 assert_equal("unknown", piet.education)
245
246 var chris = Person.new("Chris", 4, "none")
247 assert_equal("Chris", chris.name)
248 assert_equal(4, chris.age)
249 assert_equal("none", chris.education)
250 END
251 v9.CheckScriptSuccess(lines)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000252
253 lines =<< trim END
254 vim9script
255 class Person
256 this.name: string
257 this.age: number = 42
258 this.education: string = "unknown"
259
260 def new(this.name, this.age = v:none, this.education = v:none)
261 enddef
262 endclass
263
264 var missing = Person.new()
265 END
266 v9.CheckScriptFailure(lines, 'E119:')
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000267enddef
268
Bram Moolenaar74e12742022-12-13 21:14:28 +0000269def Test_class_object_member_inits()
270 var lines =<< trim END
271 vim9script
272 class TextPosition
273 this.lnum: number
274 this.col = 1
275 this.addcol: number = 2
276 endclass
277
278 var pos = TextPosition.new()
279 assert_equal(0, pos.lnum)
280 assert_equal(1, pos.col)
281 assert_equal(2, pos.addcol)
282 END
283 v9.CheckScriptSuccess(lines)
284
285 lines =<< trim END
286 vim9script
287 class TextPosition
288 this.lnum
289 this.col = 1
290 endclass
291 END
292 v9.CheckScriptFailure(lines, 'E1022:')
293
294 lines =<< trim END
295 vim9script
296 class TextPosition
297 this.lnum = v:none
298 this.col = 1
299 endclass
300 END
301 v9.CheckScriptFailure(lines, 'E1330:')
302enddef
303
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000304def Test_class_object_member_access()
305 var lines =<< trim END
306 vim9script
307 class Triple
308 this._one = 1
309 this.two = 2
310 public this.three = 3
311
312 def GetOne(): number
313 return this._one
314 enddef
315 endclass
316
317 var trip = Triple.new()
318 assert_equal(1, trip.GetOne())
319 assert_equal(2, trip.two)
320 assert_equal(3, trip.three)
321 assert_fails('echo trip._one', 'E1333')
322
323 assert_fails('trip._one = 11', 'E1333')
324 assert_fails('trip.two = 22', 'E1335')
325 trip.three = 33
326 assert_equal(33, trip.three)
Bram Moolenaard505d172022-12-18 21:42:55 +0000327
328 assert_fails('trip.four = 4', 'E1334')
329 END
330 v9.CheckScriptSuccess(lines)
Bram Moolenaar590162c2022-12-24 21:24:06 +0000331
332 lines =<< trim END
333 vim9script
334
335 class MyCar
336 this.make: string
Bram Moolenaar574950d2023-01-03 19:08:50 +0000337 this.age = 5
Bram Moolenaar590162c2022-12-24 21:24:06 +0000338
339 def new(make_arg: string)
340 this.make = make_arg
341 enddef
342
343 def GetMake(): string
344 return $"make = {this.make}"
345 enddef
Bram Moolenaar574950d2023-01-03 19:08:50 +0000346 def GetAge(): number
347 return this.age
348 enddef
Bram Moolenaar590162c2022-12-24 21:24:06 +0000349 endclass
350
351 var c = MyCar.new("abc")
352 assert_equal('make = abc', c.GetMake())
353
354 c = MyCar.new("def")
355 assert_equal('make = def', c.GetMake())
356
357 var c2 = MyCar.new("123")
358 assert_equal('make = 123', c2.GetMake())
Bram Moolenaar574950d2023-01-03 19:08:50 +0000359
360 def CheckCar()
361 assert_equal("make = def", c.GetMake())
362 assert_equal(5, c.GetAge())
363 enddef
364 CheckCar()
Bram Moolenaar590162c2022-12-24 21:24:06 +0000365 END
366 v9.CheckScriptSuccess(lines)
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000367
368 lines =<< trim END
369 vim9script
370
371 class MyCar
372 this.make: string
373
374 def new(make_arg: string)
375 this.make = make_arg
376 enddef
377 endclass
378
379 var c = MyCar.new("abc")
380 var c = MyCar.new("def")
381 END
382 v9.CheckScriptFailure(lines, 'E1041:')
Bram Moolenaard505d172022-12-18 21:42:55 +0000383enddef
384
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000385def Test_class_object_compare()
386 var class_lines =<< trim END
387 vim9script
388 class Item
389 this.nr = 0
390 this.name = 'xx'
391 endclass
392 END
393
394 # used at the script level and in a compiled function
395 var test_lines =<< trim END
396 var i1 = Item.new()
397 assert_equal(i1, i1)
398 assert_true(i1 is i1)
399 var i2 = Item.new()
400 assert_equal(i1, i2)
401 assert_false(i1 is i2)
402 var i3 = Item.new(0, 'xx')
403 assert_equal(i1, i3)
404
405 var io1 = Item.new(1, 'xx')
406 assert_notequal(i1, io1)
407 var io2 = Item.new(0, 'yy')
408 assert_notequal(i1, io2)
409 END
410
411 v9.CheckScriptSuccess(class_lines + test_lines)
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000412 v9.CheckScriptSuccess(
413 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000414
415 for op in ['>', '>=', '<', '<=', '=~', '!~']
416 var op_lines = [
417 'var i1 = Item.new()',
418 'var i2 = Item.new()',
419 'echo i1 ' .. op .. ' i2',
420 ]
421 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000422 v9.CheckScriptFailure(class_lines
423 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000424 endfor
425enddef
426
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000427def Test_object_type()
428 var lines =<< trim END
429 vim9script
430
431 class One
432 this.one = 1
433 endclass
434 class Two
435 this.two = 2
436 endclass
437 class TwoMore extends Two
438 this.more = 9
439 endclass
440
441 var o: One = One.new()
442 var t: Two = Two.new()
443 var m: TwoMore = TwoMore.new()
444 var tm: Two = TwoMore.new()
445
446 t = m
447 END
448 v9.CheckScriptSuccess(lines)
449
450 lines =<< trim END
451 vim9script
452
453 class One
454 this.one = 1
455 endclass
456 class Two
457 this.two = 2
458 endclass
459
460 var o: One = Two.new()
461 END
462 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>')
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000463
464 lines =<< trim END
465 vim9script
466
467 interface One
468 def GetMember(): number
469 endinterface
470 class Two implements One
471 this.one = 1
472 def GetMember(): number
473 return this.one
474 enddef
475 endclass
476
477 var o: One = Two.new(5)
478 assert_equal(5, o.GetMember())
479 END
480 v9.CheckScriptSuccess(lines)
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000481enddef
482
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000483def Test_class_member()
484 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000485 var lines =<< trim END
486 vim9script
487 class TextPos
488 this.lnum = 1
489 this.col = 1
490 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000491 static _secret = 7
492 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000493
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000494 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000495 counter += nr
496 enddef
497 endclass
498
499 assert_equal(0, TextPos.counter)
500 TextPos.AddToCounter(3)
501 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000502 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000503
504 def GetCounter(): number
505 return TextPos.counter
506 enddef
507 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000508
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000509 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000510 assert_fails('TextPos.counter = 5', 'E1335:')
511 assert_fails('TextPos.counter += 5', 'E1335:')
512
513 assert_fails('echo TextPos._secret', 'E1333:')
514 assert_fails('TextPos._secret = 8', 'E1333:')
515
516 assert_equal(42, TextPos.anybody)
517 TextPos.anybody = 12
518 assert_equal(12, TextPos.anybody)
519 TextPos.anybody += 5
520 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000521 END
522 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000523
524 # check shadowing
525 lines =<< trim END
526 vim9script
527
528 class Some
529 static count = 0
530 def Method(count: number)
531 echo count
532 enddef
533 endclass
534
535 var s = Some.new()
536 s.Method(7)
537 END
538 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
539
540 lines =<< trim END
541 vim9script
542
543 class Some
544 static count = 0
545 def Method(arg: number)
546 var count = 3
547 echo arg count
548 enddef
549 endclass
550
551 var s = Some.new()
552 s.Method(7)
553 END
554 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000555enddef
556
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000557func Test_class_garbagecollect()
558 let lines =<< trim END
559 vim9script
560
561 class Point
562 this.p = [2, 3]
563 static pl = ['a', 'b']
564 static pd = {a: 'a', b: 'b'}
565 endclass
566
567 echo Point.pl Point.pd
568 call test_garbagecollect_now()
569 echo Point.pl Point.pd
570 END
571 call v9.CheckScriptSuccess(lines)
572endfunc
573
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000574def Test_class_function()
575 var lines =<< trim END
576 vim9script
577 class Value
578 this.value = 0
579 static objects = 0
580
581 def new(v: number)
582 this.value = v
583 ++objects
584 enddef
585
586 static def GetCount(): number
587 return objects
588 enddef
589 endclass
590
591 assert_equal(0, Value.GetCount())
592 var v1 = Value.new(2)
593 assert_equal(1, Value.GetCount())
594 var v2 = Value.new(7)
595 assert_equal(2, Value.GetCount())
596 END
597 v9.CheckScriptSuccess(lines)
598enddef
599
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000600def Test_class_object_to_string()
601 var lines =<< trim END
602 vim9script
603 class TextPosition
604 this.lnum = 1
605 this.col = 22
606 endclass
607
608 assert_equal("class TextPosition", string(TextPosition))
609
610 var pos = TextPosition.new()
611 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
612 END
613 v9.CheckScriptSuccess(lines)
614enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000615
Bram Moolenaar554d0312023-01-05 19:59:18 +0000616def Test_interface_basics()
617 var lines =<< trim END
618 vim9script
619 interface Something
620 this.value: string
621 static count: number
622 def GetCount(): number
623 endinterface
624 END
625 v9.CheckScriptSuccess(lines)
626
627 lines =<< trim END
628 interface SomethingWrong
629 static count = 7
630 endinterface
631 END
632 v9.CheckScriptFailure(lines, 'E1342:')
633
634 lines =<< trim END
635 vim9script
636
637 interface Some
638 static count: number
639 def Method(count: number)
640 endinterface
641 END
642 # TODO: this should give an error for "count" shadowing
643 v9.CheckScriptSuccess(lines)
644
645 lines =<< trim END
646 vim9script
647 interface somethingWrong
648 static count = 7
649 endinterface
650 END
651 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
652
653 lines =<< trim END
654 vim9script
655 interface SomethingWrong
656 this.value: string
657 static count = 7
658 def GetCount(): number
659 endinterface
660 END
661 v9.CheckScriptFailure(lines, 'E1344:')
662
663 lines =<< trim END
664 vim9script
665 interface SomethingWrong
666 this.value: string
667 static count: number
668 def GetCount(): number
669 return 5
670 enddef
671 endinterface
672 END
673 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
674enddef
675
Bram Moolenaar94674f22023-01-06 18:42:20 +0000676def Test_class_implements_interface()
677 var lines =<< trim END
678 vim9script
679
680 interface Some
681 static count: number
682 def Method(nr: number)
683 endinterface
684
685 class SomeImpl implements Some
686 static count: number
687 def Method(nr: number)
688 echo nr
689 enddef
690 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000691
692 interface Another
693 this.member: string
694 endinterface
695
696 class SomeImpl implements Some, Another
697 this.member = 'abc'
698 static count: number
699 def Method(nr: number)
700 echo nr
701 enddef
702 endclass
703
Bram Moolenaar94674f22023-01-06 18:42:20 +0000704 END
705 v9.CheckScriptSuccess(lines)
706
707 lines =<< trim END
708 vim9script
709
710 interface Some
711 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000712 endinterface
713
714 class SomeImpl implements Some implements Some
715 static count: number
716 endclass
717 END
718 v9.CheckScriptFailure(lines, 'E1350:')
719
720 lines =<< trim END
721 vim9script
722
723 interface Some
724 static counter: number
725 endinterface
726
727 class SomeImpl implements Some, Some
728 static count: number
729 endclass
730 END
731 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
732
733 lines =<< trim END
734 vim9script
735
736 interface Some
737 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +0000738 def Method(nr: number)
739 endinterface
740
741 class SomeImpl implements Some
742 static count: number
743 def Method(nr: number)
744 echo nr
745 enddef
746 endclass
747 END
748 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
749
750 lines =<< trim END
751 vim9script
752
753 interface Some
754 static count: number
755 def Methods(nr: number)
756 endinterface
757
758 class SomeImpl implements Some
759 static count: number
760 def Method(nr: number)
761 echo nr
762 enddef
763 endclass
764 END
765 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
766enddef
767
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000768def Test_class_used_as_type()
769 var lines =<< trim END
770 vim9script
771
772 class Point
773 this.x = 0
774 this.y = 0
775 endclass
776
777 var p: Point
778 p = Point.new(2, 33)
779 assert_equal(2, p.x)
780 assert_equal(33, p.y)
781 END
782 v9.CheckScriptSuccess(lines)
783
784 lines =<< trim END
785 vim9script
786
787 interface HasX
788 this.x: number
789 endinterface
790
791 class Point implements HasX
792 this.x = 0
793 this.y = 0
794 endclass
795
796 var p: Point
797 p = Point.new(2, 33)
798 var hx = p
799 assert_equal(2, hx.x)
800 END
801 v9.CheckScriptSuccess(lines)
802
803 lines =<< trim END
804 vim9script
805
806 class Point
807 this.x = 0
808 this.y = 0
809 endclass
810
811 var p: Point
812 p = 'text'
813 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000814 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000815enddef
816
Bram Moolenaar83677162023-01-08 19:54:10 +0000817def Test_class_extends()
818 var lines =<< trim END
819 vim9script
820 class Base
821 this.one = 1
822 def GetOne(): number
823 return this.one
824 enddef
825 endclass
826 class Child extends Base
827 this.two = 2
828 def GetTotal(): number
829 return this.one + this.two
830 enddef
831 endclass
832 var o = Child.new()
833 assert_equal(1, o.one)
834 assert_equal(2, o.two)
835 assert_equal(1, o.GetOne())
836 assert_equal(3, o.GetTotal())
837 END
838 v9.CheckScriptSuccess(lines)
839
840 lines =<< trim END
841 vim9script
842 class Base
843 this.one = 1
844 endclass
845 class Child extends Base
846 this.two = 2
847 endclass
848 var o = Child.new(3, 44)
849 assert_equal(3, o.one)
850 assert_equal(44, o.two)
851 END
852 v9.CheckScriptSuccess(lines)
853
854 lines =<< trim END
855 vim9script
856 class Base
857 this.one = 1
858 endclass
859 class Child extends Base extends Base
860 this.two = 2
861 endclass
862 END
863 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
864
865 lines =<< trim END
866 vim9script
867 class Child extends BaseClass
868 this.two = 2
869 endclass
870 END
871 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
872
873 lines =<< trim END
874 vim9script
875 var SomeVar = 99
876 class Child extends SomeVar
877 this.two = 2
878 endclass
879 END
880 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +0000881
882 lines =<< trim END
883 vim9script
884 class Base
885 this.name: string
886 def ToString(): string
887 return this.name
888 enddef
889 endclass
890
891 class Child extends Base
892 this.age: number
893 def ToString(): string
894 return super.ToString() .. ': ' .. this.age
895 enddef
896 endclass
897
898 var o = Child.new('John', 42)
899 assert_equal('John: 42', o.ToString())
900 END
901 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000902
903 lines =<< trim END
904 vim9script
905 class Child
906 this.age: number
907 def ToString(): number
908 return this.age
909 enddef
910 def ToString(): string
911 return this.age
912 enddef
913 endclass
914 END
915 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
916
917 lines =<< trim END
918 vim9script
919 class Child
920 this.age: number
921 def ToString(): string
922 return super .ToString() .. ': ' .. this.age
923 enddef
924 endclass
925 var o = Child.new(42)
926 echo o.ToString()
927 END
928 v9.CheckScriptFailure(lines, 'E1356:')
929
930 lines =<< trim END
931 vim9script
932 class Base
933 this.name: string
934 def ToString(): string
935 return this.name
936 enddef
937 endclass
938
939 var age = 42
940 def ToString(): string
941 return super.ToString() .. ': ' .. age
942 enddef
943 echo ToString()
944 END
945 v9.CheckScriptFailure(lines, 'E1357:')
946
947 lines =<< trim END
948 vim9script
949 class Child
950 this.age: number
951 def ToString(): string
952 return super.ToString() .. ': ' .. this.age
953 enddef
954 endclass
955 var o = Child.new(42)
956 echo o.ToString()
957 END
958 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000959
960 lines =<< trim END
961 vim9script
962 class Base
963 this.name: string
964 static def ToString(): string
965 return 'Base class'
966 enddef
967 endclass
968
969 class Child extends Base
970 this.age: number
971 def ToString(): string
972 return Base.ToString() .. ': ' .. this.age
973 enddef
974 endclass
975
976 var o = Child.new('John', 42)
977 assert_equal('Base class: 42', o.ToString())
978 END
979 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +0000980enddef
981
Bram Moolenaara86655a2023-01-12 17:06:27 +0000982def Test_class_import()
983 var lines =<< trim END
984 vim9script
985 export class Animal
986 this.kind: string
987 this.name: string
988 endclass
989 END
990 writefile(lines, 'Xanimal.vim', 'D')
991
992 lines =<< trim END
993 vim9script
994 import './Xanimal.vim' as animal
995
996 var a: animal.Animal
997 a = animal.Animal.new('fish', 'Eric')
998 assert_equal('fish', a.kind)
999 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001000
1001 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1002 assert_equal('cat', b.kind)
1003 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001004 END
1005 v9.CheckScriptSuccess(lines)
1006enddef
1007
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001008
1009" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker