blob: a6caad2fcec8061bf6bf8e59f37bc770744f97fd [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
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000642 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
643
644 lines =<< trim END
645 vim9script
646
647 interface Some
648 this.value: number
649 def Method(value: number)
650 endinterface
651 END
652 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: value')
Bram Moolenaar554d0312023-01-05 19:59:18 +0000653
654 lines =<< trim END
655 vim9script
656 interface somethingWrong
657 static count = 7
658 endinterface
659 END
660 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
661
662 lines =<< trim END
663 vim9script
664 interface SomethingWrong
665 this.value: string
666 static count = 7
667 def GetCount(): number
668 endinterface
669 END
670 v9.CheckScriptFailure(lines, 'E1344:')
671
672 lines =<< trim END
673 vim9script
674 interface SomethingWrong
675 this.value: string
676 static count: number
677 def GetCount(): number
678 return 5
679 enddef
680 endinterface
681 END
682 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
683enddef
684
Bram Moolenaar94674f22023-01-06 18:42:20 +0000685def Test_class_implements_interface()
686 var lines =<< trim END
687 vim9script
688
689 interface Some
690 static count: number
691 def Method(nr: number)
692 endinterface
693
694 class SomeImpl implements Some
695 static count: number
696 def Method(nr: number)
697 echo nr
698 enddef
699 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000700
701 interface Another
702 this.member: string
703 endinterface
704
705 class SomeImpl implements Some, Another
706 this.member = 'abc'
707 static count: number
708 def Method(nr: number)
709 echo nr
710 enddef
711 endclass
712
Bram Moolenaar94674f22023-01-06 18:42:20 +0000713 END
714 v9.CheckScriptSuccess(lines)
715
716 lines =<< trim END
717 vim9script
718
719 interface Some
720 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000721 endinterface
722
723 class SomeImpl implements Some implements Some
724 static count: number
725 endclass
726 END
727 v9.CheckScriptFailure(lines, 'E1350:')
728
729 lines =<< trim END
730 vim9script
731
732 interface Some
733 static counter: number
734 endinterface
735
736 class SomeImpl implements Some, Some
737 static count: number
738 endclass
739 END
740 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
741
742 lines =<< trim END
743 vim9script
744
745 interface Some
746 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +0000747 def Method(nr: number)
748 endinterface
749
750 class SomeImpl implements Some
751 static count: number
752 def Method(nr: number)
753 echo nr
754 enddef
755 endclass
756 END
757 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
758
759 lines =<< trim END
760 vim9script
761
762 interface Some
763 static count: number
764 def Methods(nr: number)
765 endinterface
766
767 class SomeImpl implements Some
768 static count: number
769 def Method(nr: number)
770 echo nr
771 enddef
772 endclass
773 END
774 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
775enddef
776
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000777def Test_class_used_as_type()
778 var lines =<< trim END
779 vim9script
780
781 class Point
782 this.x = 0
783 this.y = 0
784 endclass
785
786 var p: Point
787 p = Point.new(2, 33)
788 assert_equal(2, p.x)
789 assert_equal(33, p.y)
790 END
791 v9.CheckScriptSuccess(lines)
792
793 lines =<< trim END
794 vim9script
795
796 interface HasX
797 this.x: number
798 endinterface
799
800 class Point implements HasX
801 this.x = 0
802 this.y = 0
803 endclass
804
805 var p: Point
806 p = Point.new(2, 33)
807 var hx = p
808 assert_equal(2, hx.x)
809 END
810 v9.CheckScriptSuccess(lines)
811
812 lines =<< trim END
813 vim9script
814
815 class Point
816 this.x = 0
817 this.y = 0
818 endclass
819
820 var p: Point
821 p = 'text'
822 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000823 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000824enddef
825
Bram Moolenaar83677162023-01-08 19:54:10 +0000826def Test_class_extends()
827 var lines =<< trim END
828 vim9script
829 class Base
830 this.one = 1
831 def GetOne(): number
832 return this.one
833 enddef
834 endclass
835 class Child extends Base
836 this.two = 2
837 def GetTotal(): number
838 return this.one + this.two
839 enddef
840 endclass
841 var o = Child.new()
842 assert_equal(1, o.one)
843 assert_equal(2, o.two)
844 assert_equal(1, o.GetOne())
845 assert_equal(3, o.GetTotal())
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
855 this.two = 2
856 endclass
857 var o = Child.new(3, 44)
858 assert_equal(3, o.one)
859 assert_equal(44, o.two)
860 END
861 v9.CheckScriptSuccess(lines)
862
863 lines =<< trim END
864 vim9script
865 class Base
866 this.one = 1
867 endclass
868 class Child extends Base extends Base
869 this.two = 2
870 endclass
871 END
872 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
873
874 lines =<< trim END
875 vim9script
876 class Child extends BaseClass
877 this.two = 2
878 endclass
879 END
880 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
881
882 lines =<< trim END
883 vim9script
884 var SomeVar = 99
885 class Child extends SomeVar
886 this.two = 2
887 endclass
888 END
889 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +0000890
891 lines =<< trim END
892 vim9script
893 class Base
894 this.name: string
895 def ToString(): string
896 return this.name
897 enddef
898 endclass
899
900 class Child extends Base
901 this.age: number
902 def ToString(): string
903 return super.ToString() .. ': ' .. this.age
904 enddef
905 endclass
906
907 var o = Child.new('John', 42)
908 assert_equal('John: 42', o.ToString())
909 END
910 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000911
912 lines =<< trim END
913 vim9script
914 class Child
915 this.age: number
916 def ToString(): number
917 return this.age
918 enddef
919 def ToString(): string
920 return this.age
921 enddef
922 endclass
923 END
924 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
925
926 lines =<< trim END
927 vim9script
928 class Child
929 this.age: number
930 def ToString(): string
931 return super .ToString() .. ': ' .. this.age
932 enddef
933 endclass
934 var o = Child.new(42)
935 echo o.ToString()
936 END
937 v9.CheckScriptFailure(lines, 'E1356:')
938
939 lines =<< trim END
940 vim9script
941 class Base
942 this.name: string
943 def ToString(): string
944 return this.name
945 enddef
946 endclass
947
948 var age = 42
949 def ToString(): string
950 return super.ToString() .. ': ' .. age
951 enddef
952 echo ToString()
953 END
954 v9.CheckScriptFailure(lines, 'E1357:')
955
956 lines =<< trim END
957 vim9script
958 class Child
959 this.age: number
960 def ToString(): string
961 return super.ToString() .. ': ' .. this.age
962 enddef
963 endclass
964 var o = Child.new(42)
965 echo o.ToString()
966 END
967 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000968
969 lines =<< trim END
970 vim9script
971 class Base
972 this.name: string
973 static def ToString(): string
974 return 'Base class'
975 enddef
976 endclass
977
978 class Child extends Base
979 this.age: number
980 def ToString(): string
981 return Base.ToString() .. ': ' .. this.age
982 enddef
983 endclass
984
985 var o = Child.new('John', 42)
986 assert_equal('Base class: 42', o.ToString())
987 END
988 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +0000989enddef
990
Bram Moolenaara86655a2023-01-12 17:06:27 +0000991def Test_class_import()
992 var lines =<< trim END
993 vim9script
994 export class Animal
995 this.kind: string
996 this.name: string
997 endclass
998 END
999 writefile(lines, 'Xanimal.vim', 'D')
1000
1001 lines =<< trim END
1002 vim9script
1003 import './Xanimal.vim' as animal
1004
1005 var a: animal.Animal
1006 a = animal.Animal.new('fish', 'Eric')
1007 assert_equal('fish', a.kind)
1008 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001009
1010 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1011 assert_equal('cat', b.kind)
1012 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001013 END
1014 v9.CheckScriptSuccess(lines)
1015enddef
1016
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001017
1018" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker