blob: 56b2dad7e07bffb54e1eb0f300b87fda6230f7a3 [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 Moolenaar4cae8452023-01-15 15:51:48 +0000203def Test_assignment_with_operator()
204 var lines =<< trim END
205 vim9script
206
207 class Foo
208 this.x: number
209
210 def Add(n: number)
211 this.x += n
212 enddef
213 endclass
214
215 var f = Foo.new(3)
216 f.Add(17)
217 assert_equal(20, f.x)
218 END
219 v9.CheckScriptSuccess(lines)
220enddef
221
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000222def Test_class_default_new()
223 var lines =<< trim END
224 vim9script
225
226 class TextPosition
227 this.lnum: number = 1
228 this.col: number = 1
229 endclass
230
231 var pos = TextPosition.new()
232 assert_equal(1, pos.lnum)
233 assert_equal(1, pos.col)
234
235 pos = TextPosition.new(v:none, v:none)
236 assert_equal(1, pos.lnum)
237 assert_equal(1, pos.col)
238
239 pos = TextPosition.new(3, 22)
240 assert_equal(3, pos.lnum)
241 assert_equal(22, pos.col)
242
243 pos = TextPosition.new(v:none, 33)
244 assert_equal(1, pos.lnum)
245 assert_equal(33, pos.col)
246 END
247 v9.CheckScriptSuccess(lines)
248
249 lines =<< trim END
250 vim9script
251 class Person
252 this.name: string
253 this.age: number = 42
254 this.education: string = "unknown"
255
256 def new(this.name, this.age = v:none, this.education = v:none)
257 enddef
258 endclass
259
260 var piet = Person.new("Piet")
261 assert_equal("Piet", piet.name)
262 assert_equal(42, piet.age)
263 assert_equal("unknown", piet.education)
264
265 var chris = Person.new("Chris", 4, "none")
266 assert_equal("Chris", chris.name)
267 assert_equal(4, chris.age)
268 assert_equal("none", chris.education)
269 END
270 v9.CheckScriptSuccess(lines)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000271
272 lines =<< trim END
273 vim9script
274 class Person
275 this.name: string
276 this.age: number = 42
277 this.education: string = "unknown"
278
279 def new(this.name, this.age = v:none, this.education = v:none)
280 enddef
281 endclass
282
283 var missing = Person.new()
284 END
285 v9.CheckScriptFailure(lines, 'E119:')
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000286enddef
287
Bram Moolenaar74e12742022-12-13 21:14:28 +0000288def Test_class_object_member_inits()
289 var lines =<< trim END
290 vim9script
291 class TextPosition
292 this.lnum: number
293 this.col = 1
294 this.addcol: number = 2
295 endclass
296
297 var pos = TextPosition.new()
298 assert_equal(0, pos.lnum)
299 assert_equal(1, pos.col)
300 assert_equal(2, pos.addcol)
301 END
302 v9.CheckScriptSuccess(lines)
303
304 lines =<< trim END
305 vim9script
306 class TextPosition
307 this.lnum
308 this.col = 1
309 endclass
310 END
311 v9.CheckScriptFailure(lines, 'E1022:')
312
313 lines =<< trim END
314 vim9script
315 class TextPosition
316 this.lnum = v:none
317 this.col = 1
318 endclass
319 END
320 v9.CheckScriptFailure(lines, 'E1330:')
321enddef
322
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000323def Test_class_object_member_access()
324 var lines =<< trim END
325 vim9script
326 class Triple
327 this._one = 1
328 this.two = 2
329 public this.three = 3
330
331 def GetOne(): number
332 return this._one
333 enddef
334 endclass
335
336 var trip = Triple.new()
337 assert_equal(1, trip.GetOne())
338 assert_equal(2, trip.two)
339 assert_equal(3, trip.three)
340 assert_fails('echo trip._one', 'E1333')
341
342 assert_fails('trip._one = 11', 'E1333')
343 assert_fails('trip.two = 22', 'E1335')
344 trip.three = 33
345 assert_equal(33, trip.three)
Bram Moolenaard505d172022-12-18 21:42:55 +0000346
347 assert_fails('trip.four = 4', 'E1334')
348 END
349 v9.CheckScriptSuccess(lines)
Bram Moolenaar590162c2022-12-24 21:24:06 +0000350
351 lines =<< trim END
352 vim9script
353
354 class MyCar
355 this.make: string
Bram Moolenaar574950d2023-01-03 19:08:50 +0000356 this.age = 5
Bram Moolenaar590162c2022-12-24 21:24:06 +0000357
358 def new(make_arg: string)
359 this.make = make_arg
360 enddef
361
362 def GetMake(): string
363 return $"make = {this.make}"
364 enddef
Bram Moolenaar574950d2023-01-03 19:08:50 +0000365 def GetAge(): number
366 return this.age
367 enddef
Bram Moolenaar590162c2022-12-24 21:24:06 +0000368 endclass
369
370 var c = MyCar.new("abc")
371 assert_equal('make = abc', c.GetMake())
372
373 c = MyCar.new("def")
374 assert_equal('make = def', c.GetMake())
375
376 var c2 = MyCar.new("123")
377 assert_equal('make = 123', c2.GetMake())
Bram Moolenaar574950d2023-01-03 19:08:50 +0000378
379 def CheckCar()
380 assert_equal("make = def", c.GetMake())
381 assert_equal(5, c.GetAge())
382 enddef
383 CheckCar()
Bram Moolenaar590162c2022-12-24 21:24:06 +0000384 END
385 v9.CheckScriptSuccess(lines)
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000386
387 lines =<< trim END
388 vim9script
389
390 class MyCar
391 this.make: string
392
393 def new(make_arg: string)
394 this.make = make_arg
395 enddef
396 endclass
397
398 var c = MyCar.new("abc")
399 var c = MyCar.new("def")
400 END
401 v9.CheckScriptFailure(lines, 'E1041:')
Bram Moolenaard505d172022-12-18 21:42:55 +0000402enddef
403
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000404def Test_class_object_compare()
405 var class_lines =<< trim END
406 vim9script
407 class Item
408 this.nr = 0
409 this.name = 'xx'
410 endclass
411 END
412
413 # used at the script level and in a compiled function
414 var test_lines =<< trim END
415 var i1 = Item.new()
416 assert_equal(i1, i1)
417 assert_true(i1 is i1)
418 var i2 = Item.new()
419 assert_equal(i1, i2)
420 assert_false(i1 is i2)
421 var i3 = Item.new(0, 'xx')
422 assert_equal(i1, i3)
423
424 var io1 = Item.new(1, 'xx')
425 assert_notequal(i1, io1)
426 var io2 = Item.new(0, 'yy')
427 assert_notequal(i1, io2)
428 END
429
430 v9.CheckScriptSuccess(class_lines + test_lines)
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000431 v9.CheckScriptSuccess(
432 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000433
434 for op in ['>', '>=', '<', '<=', '=~', '!~']
435 var op_lines = [
436 'var i1 = Item.new()',
437 'var i2 = Item.new()',
438 'echo i1 ' .. op .. ' i2',
439 ]
440 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000441 v9.CheckScriptFailure(class_lines
442 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000443 endfor
444enddef
445
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000446def Test_object_type()
447 var lines =<< trim END
448 vim9script
449
450 class One
451 this.one = 1
452 endclass
453 class Two
454 this.two = 2
455 endclass
456 class TwoMore extends Two
457 this.more = 9
458 endclass
459
460 var o: One = One.new()
461 var t: Two = Two.new()
462 var m: TwoMore = TwoMore.new()
463 var tm: Two = TwoMore.new()
464
465 t = m
466 END
467 v9.CheckScriptSuccess(lines)
468
469 lines =<< trim END
470 vim9script
471
472 class One
473 this.one = 1
474 endclass
475 class Two
476 this.two = 2
477 endclass
478
479 var o: One = Two.new()
480 END
481 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>')
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000482
483 lines =<< trim END
484 vim9script
485
486 interface One
487 def GetMember(): number
488 endinterface
489 class Two implements One
490 this.one = 1
491 def GetMember(): number
492 return this.one
493 enddef
494 endclass
495
496 var o: One = Two.new(5)
497 assert_equal(5, o.GetMember())
498 END
499 v9.CheckScriptSuccess(lines)
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000500enddef
501
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000502def Test_class_member()
503 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000504 var lines =<< trim END
505 vim9script
506 class TextPos
507 this.lnum = 1
508 this.col = 1
509 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000510 static _secret = 7
511 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000512
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000513 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000514 counter += nr
515 enddef
516 endclass
517
518 assert_equal(0, TextPos.counter)
519 TextPos.AddToCounter(3)
520 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000521 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000522
523 def GetCounter(): number
524 return TextPos.counter
525 enddef
526 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000527
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000528 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000529 assert_fails('TextPos.counter = 5', 'E1335:')
530 assert_fails('TextPos.counter += 5', 'E1335:')
531
532 assert_fails('echo TextPos._secret', 'E1333:')
533 assert_fails('TextPos._secret = 8', 'E1333:')
534
535 assert_equal(42, TextPos.anybody)
536 TextPos.anybody = 12
537 assert_equal(12, TextPos.anybody)
538 TextPos.anybody += 5
539 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000540 END
541 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000542
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000543 # example in the help
544 lines =<< trim END
545 vim9script
546 class OtherThing
547 this.size: number
548 static totalSize: number
549
550 def new(this.size)
551 totalSize += this.size
552 enddef
553 endclass
554 assert_equal(0, OtherThing.totalSize)
555 var to3 = OtherThing.new(3)
556 assert_equal(3, OtherThing.totalSize)
557 var to7 = OtherThing.new(7)
558 assert_equal(10, OtherThing.totalSize)
559 END
560 v9.CheckScriptSuccess(lines)
561
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000562 # check shadowing
563 lines =<< trim END
564 vim9script
565
566 class Some
567 static count = 0
568 def Method(count: number)
569 echo count
570 enddef
571 endclass
572
573 var s = Some.new()
574 s.Method(7)
575 END
576 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
577
578 lines =<< trim END
579 vim9script
580
581 class Some
582 static count = 0
583 def Method(arg: number)
584 var count = 3
585 echo arg count
586 enddef
587 endclass
588
589 var s = Some.new()
590 s.Method(7)
591 END
592 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000593enddef
594
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000595func Test_class_garbagecollect()
596 let lines =<< trim END
597 vim9script
598
599 class Point
600 this.p = [2, 3]
601 static pl = ['a', 'b']
602 static pd = {a: 'a', b: 'b'}
603 endclass
604
605 echo Point.pl Point.pd
606 call test_garbagecollect_now()
607 echo Point.pl Point.pd
608 END
609 call v9.CheckScriptSuccess(lines)
610endfunc
611
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000612def Test_class_function()
613 var lines =<< trim END
614 vim9script
615 class Value
616 this.value = 0
617 static objects = 0
618
619 def new(v: number)
620 this.value = v
621 ++objects
622 enddef
623
624 static def GetCount(): number
625 return objects
626 enddef
627 endclass
628
629 assert_equal(0, Value.GetCount())
630 var v1 = Value.new(2)
631 assert_equal(1, Value.GetCount())
632 var v2 = Value.new(7)
633 assert_equal(2, Value.GetCount())
634 END
635 v9.CheckScriptSuccess(lines)
636enddef
637
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000638def Test_class_object_to_string()
639 var lines =<< trim END
640 vim9script
641 class TextPosition
642 this.lnum = 1
643 this.col = 22
644 endclass
645
646 assert_equal("class TextPosition", string(TextPosition))
647
648 var pos = TextPosition.new()
649 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
650 END
651 v9.CheckScriptSuccess(lines)
652enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000653
Bram Moolenaar554d0312023-01-05 19:59:18 +0000654def Test_interface_basics()
655 var lines =<< trim END
656 vim9script
657 interface Something
658 this.value: string
659 static count: number
660 def GetCount(): number
661 endinterface
662 END
663 v9.CheckScriptSuccess(lines)
664
665 lines =<< trim END
666 interface SomethingWrong
667 static count = 7
668 endinterface
669 END
670 v9.CheckScriptFailure(lines, 'E1342:')
671
672 lines =<< trim END
673 vim9script
674
675 interface Some
676 static count: number
677 def Method(count: number)
678 endinterface
679 END
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000680 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
681
682 lines =<< trim END
683 vim9script
684
685 interface Some
686 this.value: number
687 def Method(value: number)
688 endinterface
689 END
690 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: value')
Bram Moolenaar554d0312023-01-05 19:59:18 +0000691
692 lines =<< trim END
693 vim9script
694 interface somethingWrong
695 static count = 7
696 endinterface
697 END
698 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
699
700 lines =<< trim END
701 vim9script
702 interface SomethingWrong
703 this.value: string
704 static count = 7
705 def GetCount(): number
706 endinterface
707 END
708 v9.CheckScriptFailure(lines, 'E1344:')
709
710 lines =<< trim END
711 vim9script
712 interface SomethingWrong
713 this.value: string
714 static count: number
715 def GetCount(): number
716 return 5
717 enddef
718 endinterface
719 END
720 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
721enddef
722
Bram Moolenaar94674f22023-01-06 18:42:20 +0000723def Test_class_implements_interface()
724 var lines =<< trim END
725 vim9script
726
727 interface Some
728 static count: number
729 def Method(nr: number)
730 endinterface
731
732 class SomeImpl implements Some
733 static count: number
734 def Method(nr: number)
735 echo nr
736 enddef
737 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000738
739 interface Another
740 this.member: string
741 endinterface
742
743 class SomeImpl implements Some, Another
744 this.member = 'abc'
745 static count: number
746 def Method(nr: number)
747 echo nr
748 enddef
749 endclass
750
Bram Moolenaar94674f22023-01-06 18:42:20 +0000751 END
752 v9.CheckScriptSuccess(lines)
753
754 lines =<< trim END
755 vim9script
756
757 interface Some
758 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000759 endinterface
760
761 class SomeImpl implements Some implements Some
762 static count: number
763 endclass
764 END
765 v9.CheckScriptFailure(lines, 'E1350:')
766
767 lines =<< trim END
768 vim9script
769
770 interface Some
771 static counter: number
772 endinterface
773
774 class SomeImpl implements Some, Some
775 static count: number
776 endclass
777 END
778 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
779
780 lines =<< trim END
781 vim9script
782
783 interface Some
784 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +0000785 def Method(nr: number)
786 endinterface
787
788 class SomeImpl implements Some
789 static count: number
790 def Method(nr: number)
791 echo nr
792 enddef
793 endclass
794 END
795 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
796
797 lines =<< trim END
798 vim9script
799
800 interface Some
801 static count: number
802 def Methods(nr: number)
803 endinterface
804
805 class SomeImpl implements Some
806 static count: number
807 def Method(nr: number)
808 echo nr
809 enddef
810 endclass
811 END
812 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
813enddef
814
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000815def Test_class_used_as_type()
816 var lines =<< trim END
817 vim9script
818
819 class Point
820 this.x = 0
821 this.y = 0
822 endclass
823
824 var p: Point
825 p = Point.new(2, 33)
826 assert_equal(2, p.x)
827 assert_equal(33, p.y)
828 END
829 v9.CheckScriptSuccess(lines)
830
831 lines =<< trim END
832 vim9script
833
834 interface HasX
835 this.x: number
836 endinterface
837
838 class Point implements HasX
839 this.x = 0
840 this.y = 0
841 endclass
842
843 var p: Point
844 p = Point.new(2, 33)
845 var hx = p
846 assert_equal(2, hx.x)
847 END
848 v9.CheckScriptSuccess(lines)
849
850 lines =<< trim END
851 vim9script
852
853 class Point
854 this.x = 0
855 this.y = 0
856 endclass
857
858 var p: Point
859 p = 'text'
860 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000861 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000862enddef
863
Bram Moolenaar83677162023-01-08 19:54:10 +0000864def Test_class_extends()
865 var lines =<< trim END
866 vim9script
867 class Base
868 this.one = 1
869 def GetOne(): number
870 return this.one
871 enddef
872 endclass
873 class Child extends Base
874 this.two = 2
875 def GetTotal(): number
876 return this.one + this.two
877 enddef
878 endclass
879 var o = Child.new()
880 assert_equal(1, o.one)
881 assert_equal(2, o.two)
882 assert_equal(1, o.GetOne())
883 assert_equal(3, o.GetTotal())
884 END
885 v9.CheckScriptSuccess(lines)
886
887 lines =<< trim END
888 vim9script
889 class Base
890 this.one = 1
891 endclass
892 class Child extends Base
893 this.two = 2
894 endclass
895 var o = Child.new(3, 44)
896 assert_equal(3, o.one)
897 assert_equal(44, o.two)
898 END
899 v9.CheckScriptSuccess(lines)
900
901 lines =<< trim END
902 vim9script
903 class Base
904 this.one = 1
905 endclass
906 class Child extends Base extends Base
907 this.two = 2
908 endclass
909 END
910 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
911
912 lines =<< trim END
913 vim9script
914 class Child extends BaseClass
915 this.two = 2
916 endclass
917 END
918 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
919
920 lines =<< trim END
921 vim9script
922 var SomeVar = 99
923 class Child extends SomeVar
924 this.two = 2
925 endclass
926 END
927 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +0000928
929 lines =<< trim END
930 vim9script
931 class Base
932 this.name: string
933 def ToString(): string
934 return this.name
935 enddef
936 endclass
937
938 class Child extends Base
939 this.age: number
940 def ToString(): string
941 return super.ToString() .. ': ' .. this.age
942 enddef
943 endclass
944
945 var o = Child.new('John', 42)
946 assert_equal('John: 42', o.ToString())
947 END
948 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000949
950 lines =<< trim END
951 vim9script
952 class Child
953 this.age: number
954 def ToString(): number
955 return this.age
956 enddef
957 def ToString(): string
958 return this.age
959 enddef
960 endclass
961 END
962 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
963
964 lines =<< trim END
965 vim9script
966 class Child
967 this.age: number
968 def ToString(): string
969 return super .ToString() .. ': ' .. this.age
970 enddef
971 endclass
972 var o = Child.new(42)
973 echo o.ToString()
974 END
975 v9.CheckScriptFailure(lines, 'E1356:')
976
977 lines =<< trim END
978 vim9script
979 class Base
980 this.name: string
981 def ToString(): string
982 return this.name
983 enddef
984 endclass
985
986 var age = 42
987 def ToString(): string
988 return super.ToString() .. ': ' .. age
989 enddef
990 echo ToString()
991 END
992 v9.CheckScriptFailure(lines, 'E1357:')
993
994 lines =<< trim END
995 vim9script
996 class Child
997 this.age: number
998 def ToString(): string
999 return super.ToString() .. ': ' .. this.age
1000 enddef
1001 endclass
1002 var o = Child.new(42)
1003 echo o.ToString()
1004 END
1005 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001006
1007 lines =<< trim END
1008 vim9script
1009 class Base
1010 this.name: string
1011 static def ToString(): string
1012 return 'Base class'
1013 enddef
1014 endclass
1015
1016 class Child extends Base
1017 this.age: number
1018 def ToString(): string
1019 return Base.ToString() .. ': ' .. this.age
1020 enddef
1021 endclass
1022
1023 var o = Child.new('John', 42)
1024 assert_equal('Base class: 42', o.ToString())
1025 END
1026 v9.CheckScriptSuccess(lines)
Bram Moolenaar4cae8452023-01-15 15:51:48 +00001027
1028 lines =<< trim END
1029 vim9script
1030 class Base
1031 this.value = 1
1032 def new(init: number)
1033 this.value = number + 1
1034 enddef
1035 endclass
1036 class Child extends Base
1037 def new()
1038 this.new(3)
1039 enddef
1040 endclass
1041 var c = Child.new()
1042 END
1043 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Child": new(')
Bram Moolenaar83677162023-01-08 19:54:10 +00001044enddef
1045
Bram Moolenaara86655a2023-01-12 17:06:27 +00001046def Test_class_import()
1047 var lines =<< trim END
1048 vim9script
1049 export class Animal
1050 this.kind: string
1051 this.name: string
1052 endclass
1053 END
1054 writefile(lines, 'Xanimal.vim', 'D')
1055
1056 lines =<< trim END
1057 vim9script
1058 import './Xanimal.vim' as animal
1059
1060 var a: animal.Animal
1061 a = animal.Animal.new('fish', 'Eric')
1062 assert_equal('fish', a.kind)
1063 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001064
1065 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1066 assert_equal('cat', b.kind)
1067 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001068 END
1069 v9.CheckScriptSuccess(lines)
1070enddef
1071
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001072def Test_abstract_class()
1073 var lines =<< trim END
1074 vim9script
1075 abstract class Base
1076 this.name: string
1077 endclass
1078 class Person extends Base
1079 this.age: number
1080 endclass
1081 var p: Base = Person.new('Peter', 42)
1082 assert_equal('Peter', p.name)
1083 assert_equal(42, p.age)
1084 END
1085 v9.CheckScriptSuccess(lines)
1086
1087 lines =<< trim END
1088 vim9script
1089 abstract class Base
1090 this.name: string
1091 endclass
1092 class Person extends Base
1093 this.age: number
1094 endclass
1095 var p = Base.new('Peter')
1096 END
1097 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Base": new(')
1098
1099 lines =<< trim END
1100 abstract class Base
1101 this.name: string
1102 endclass
1103 END
1104 v9.CheckScriptFailure(lines, 'E1316:')
1105enddef
1106
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001107
1108" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker