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