blob: c2c4cf30839d6e572464d487b5b6017c3d3b94d1 [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 Moolenaar912bfee2023-01-15 20:18:55 +0000243def Test_expr_after_using_object()
244 var lines =<< trim END
245 vim9script
246
247 class Something
248 this.label: string = ''
249 endclass
250
251 def Foo(): Something
252 var v = Something.new()
253 echo 'in Foo(): ' .. typename(v)
254 return v
255 enddef
256
257 Foo()
258 END
259 v9.CheckScriptSuccess(lines)
260enddef
261
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000262def Test_class_default_new()
263 var lines =<< trim END
264 vim9script
265
266 class TextPosition
267 this.lnum: number = 1
268 this.col: number = 1
269 endclass
270
271 var pos = TextPosition.new()
272 assert_equal(1, pos.lnum)
273 assert_equal(1, pos.col)
274
275 pos = TextPosition.new(v:none, v:none)
276 assert_equal(1, pos.lnum)
277 assert_equal(1, pos.col)
278
279 pos = TextPosition.new(3, 22)
280 assert_equal(3, pos.lnum)
281 assert_equal(22, pos.col)
282
283 pos = TextPosition.new(v:none, 33)
284 assert_equal(1, pos.lnum)
285 assert_equal(33, pos.col)
286 END
287 v9.CheckScriptSuccess(lines)
288
289 lines =<< trim END
290 vim9script
291 class Person
292 this.name: string
293 this.age: number = 42
294 this.education: string = "unknown"
295
296 def new(this.name, this.age = v:none, this.education = v:none)
297 enddef
298 endclass
299
300 var piet = Person.new("Piet")
301 assert_equal("Piet", piet.name)
302 assert_equal(42, piet.age)
303 assert_equal("unknown", piet.education)
304
305 var chris = Person.new("Chris", 4, "none")
306 assert_equal("Chris", chris.name)
307 assert_equal(4, chris.age)
308 assert_equal("none", chris.education)
309 END
310 v9.CheckScriptSuccess(lines)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000311
312 lines =<< trim END
313 vim9script
314 class Person
315 this.name: string
316 this.age: number = 42
317 this.education: string = "unknown"
318
319 def new(this.name, this.age = v:none, this.education = v:none)
320 enddef
321 endclass
322
323 var missing = Person.new()
324 END
325 v9.CheckScriptFailure(lines, 'E119:')
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000326enddef
327
Bram Moolenaar74e12742022-12-13 21:14:28 +0000328def Test_class_object_member_inits()
329 var lines =<< trim END
330 vim9script
331 class TextPosition
332 this.lnum: number
333 this.col = 1
334 this.addcol: number = 2
335 endclass
336
337 var pos = TextPosition.new()
338 assert_equal(0, pos.lnum)
339 assert_equal(1, pos.col)
340 assert_equal(2, pos.addcol)
341 END
342 v9.CheckScriptSuccess(lines)
343
344 lines =<< trim END
345 vim9script
346 class TextPosition
347 this.lnum
348 this.col = 1
349 endclass
350 END
351 v9.CheckScriptFailure(lines, 'E1022:')
352
353 lines =<< trim END
354 vim9script
355 class TextPosition
356 this.lnum = v:none
357 this.col = 1
358 endclass
359 END
360 v9.CheckScriptFailure(lines, 'E1330:')
361enddef
362
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000363def Test_class_object_member_access()
364 var lines =<< trim END
365 vim9script
366 class Triple
367 this._one = 1
368 this.two = 2
369 public this.three = 3
370
371 def GetOne(): number
372 return this._one
373 enddef
374 endclass
375
376 var trip = Triple.new()
377 assert_equal(1, trip.GetOne())
378 assert_equal(2, trip.two)
379 assert_equal(3, trip.three)
380 assert_fails('echo trip._one', 'E1333')
381
382 assert_fails('trip._one = 11', 'E1333')
383 assert_fails('trip.two = 22', 'E1335')
384 trip.three = 33
385 assert_equal(33, trip.three)
Bram Moolenaard505d172022-12-18 21:42:55 +0000386
387 assert_fails('trip.four = 4', 'E1334')
388 END
389 v9.CheckScriptSuccess(lines)
Bram Moolenaar590162c2022-12-24 21:24:06 +0000390
391 lines =<< trim END
392 vim9script
393
394 class MyCar
395 this.make: string
Bram Moolenaar574950d2023-01-03 19:08:50 +0000396 this.age = 5
Bram Moolenaar590162c2022-12-24 21:24:06 +0000397
398 def new(make_arg: string)
399 this.make = make_arg
400 enddef
401
402 def GetMake(): string
403 return $"make = {this.make}"
404 enddef
Bram Moolenaar574950d2023-01-03 19:08:50 +0000405 def GetAge(): number
406 return this.age
407 enddef
Bram Moolenaar590162c2022-12-24 21:24:06 +0000408 endclass
409
410 var c = MyCar.new("abc")
411 assert_equal('make = abc', c.GetMake())
412
413 c = MyCar.new("def")
414 assert_equal('make = def', c.GetMake())
415
416 var c2 = MyCar.new("123")
417 assert_equal('make = 123', c2.GetMake())
Bram Moolenaar574950d2023-01-03 19:08:50 +0000418
419 def CheckCar()
420 assert_equal("make = def", c.GetMake())
421 assert_equal(5, c.GetAge())
422 enddef
423 CheckCar()
Bram Moolenaar590162c2022-12-24 21:24:06 +0000424 END
425 v9.CheckScriptSuccess(lines)
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000426
427 lines =<< trim END
428 vim9script
429
430 class MyCar
431 this.make: string
432
433 def new(make_arg: string)
434 this.make = make_arg
435 enddef
436 endclass
437
438 var c = MyCar.new("abc")
439 var c = MyCar.new("def")
440 END
441 v9.CheckScriptFailure(lines, 'E1041:')
Bram Moolenaard505d172022-12-18 21:42:55 +0000442enddef
443
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000444def Test_class_object_compare()
445 var class_lines =<< trim END
446 vim9script
447 class Item
448 this.nr = 0
449 this.name = 'xx'
450 endclass
451 END
452
453 # used at the script level and in a compiled function
454 var test_lines =<< trim END
455 var i1 = Item.new()
456 assert_equal(i1, i1)
457 assert_true(i1 is i1)
458 var i2 = Item.new()
459 assert_equal(i1, i2)
460 assert_false(i1 is i2)
461 var i3 = Item.new(0, 'xx')
462 assert_equal(i1, i3)
463
464 var io1 = Item.new(1, 'xx')
465 assert_notequal(i1, io1)
466 var io2 = Item.new(0, 'yy')
467 assert_notequal(i1, io2)
468 END
469
470 v9.CheckScriptSuccess(class_lines + test_lines)
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000471 v9.CheckScriptSuccess(
472 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000473
474 for op in ['>', '>=', '<', '<=', '=~', '!~']
475 var op_lines = [
476 'var i1 = Item.new()',
477 'var i2 = Item.new()',
478 'echo i1 ' .. op .. ' i2',
479 ]
480 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000481 v9.CheckScriptFailure(class_lines
482 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000483 endfor
484enddef
485
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000486def Test_object_type()
487 var lines =<< trim END
488 vim9script
489
490 class One
491 this.one = 1
492 endclass
493 class Two
494 this.two = 2
495 endclass
496 class TwoMore extends Two
497 this.more = 9
498 endclass
499
500 var o: One = One.new()
501 var t: Two = Two.new()
502 var m: TwoMore = TwoMore.new()
503 var tm: Two = TwoMore.new()
504
505 t = m
506 END
507 v9.CheckScriptSuccess(lines)
508
509 lines =<< trim END
510 vim9script
511
512 class One
513 this.one = 1
514 endclass
515 class Two
516 this.two = 2
517 endclass
518
519 var o: One = Two.new()
520 END
521 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>')
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000522
523 lines =<< trim END
524 vim9script
525
526 interface One
527 def GetMember(): number
528 endinterface
529 class Two implements One
530 this.one = 1
531 def GetMember(): number
532 return this.one
533 enddef
534 endclass
535
536 var o: One = Two.new(5)
537 assert_equal(5, o.GetMember())
538 END
539 v9.CheckScriptSuccess(lines)
Bram Moolenaar450c7a92023-01-16 16:39:37 +0000540
541 lines =<< trim END
542 vim9script
543
544 class Num
545 this.n: number = 0
546 endclass
547
548 def Ref(name: string): func(Num): Num
549 return (arg: Num): Num => {
550 return eval(name)(arg)
551 }
552 enddef
553
554 const Fn = Ref('Double')
555 var Double = (m: Num): Num => Num.new(m.n * 2)
556
557 echo Fn(Num.new(4))
558 END
559 v9.CheckScriptSuccess(lines)
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000560enddef
561
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000562def Test_class_member()
563 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000564 var lines =<< trim END
565 vim9script
566 class TextPos
567 this.lnum = 1
568 this.col = 1
569 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000570 static _secret = 7
571 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000572
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000573 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000574 counter += nr
575 enddef
576 endclass
577
578 assert_equal(0, TextPos.counter)
579 TextPos.AddToCounter(3)
580 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000581 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000582
583 def GetCounter(): number
584 return TextPos.counter
585 enddef
586 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000587
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000588 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000589 assert_fails('TextPos.counter = 5', 'E1335:')
590 assert_fails('TextPos.counter += 5', 'E1335:')
591
592 assert_fails('echo TextPos._secret', 'E1333:')
593 assert_fails('TextPos._secret = 8', 'E1333:')
594
595 assert_equal(42, TextPos.anybody)
596 TextPos.anybody = 12
597 assert_equal(12, TextPos.anybody)
598 TextPos.anybody += 5
599 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000600 END
601 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000602
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000603 # example in the help
604 lines =<< trim END
605 vim9script
606 class OtherThing
607 this.size: number
608 static totalSize: number
609
610 def new(this.size)
611 totalSize += this.size
612 enddef
613 endclass
614 assert_equal(0, OtherThing.totalSize)
615 var to3 = OtherThing.new(3)
616 assert_equal(3, OtherThing.totalSize)
617 var to7 = OtherThing.new(7)
618 assert_equal(10, OtherThing.totalSize)
619 END
620 v9.CheckScriptSuccess(lines)
621
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000622 # check shadowing
623 lines =<< trim END
624 vim9script
625
626 class Some
627 static count = 0
628 def Method(count: number)
629 echo count
630 enddef
631 endclass
632
633 var s = Some.new()
634 s.Method(7)
635 END
636 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
637
638 lines =<< trim END
639 vim9script
640
641 class Some
642 static count = 0
643 def Method(arg: number)
644 var count = 3
645 echo arg count
646 enddef
647 endclass
648
649 var s = Some.new()
650 s.Method(7)
651 END
652 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000653enddef
654
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000655func Test_class_garbagecollect()
656 let lines =<< trim END
657 vim9script
658
659 class Point
660 this.p = [2, 3]
661 static pl = ['a', 'b']
662 static pd = {a: 'a', b: 'b'}
663 endclass
664
665 echo Point.pl Point.pd
666 call test_garbagecollect_now()
667 echo Point.pl Point.pd
668 END
669 call v9.CheckScriptSuccess(lines)
670endfunc
671
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000672def Test_class_function()
673 var lines =<< trim END
674 vim9script
675 class Value
676 this.value = 0
677 static objects = 0
678
679 def new(v: number)
680 this.value = v
681 ++objects
682 enddef
683
684 static def GetCount(): number
685 return objects
686 enddef
687 endclass
688
689 assert_equal(0, Value.GetCount())
690 var v1 = Value.new(2)
691 assert_equal(1, Value.GetCount())
692 var v2 = Value.new(7)
693 assert_equal(2, Value.GetCount())
694 END
695 v9.CheckScriptSuccess(lines)
696enddef
697
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000698def Test_class_object_to_string()
699 var lines =<< trim END
700 vim9script
701 class TextPosition
702 this.lnum = 1
703 this.col = 22
704 endclass
705
706 assert_equal("class TextPosition", string(TextPosition))
707
708 var pos = TextPosition.new()
709 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
710 END
711 v9.CheckScriptSuccess(lines)
712enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000713
Bram Moolenaar554d0312023-01-05 19:59:18 +0000714def Test_interface_basics()
715 var lines =<< trim END
716 vim9script
717 interface Something
718 this.value: string
719 static count: number
720 def GetCount(): number
721 endinterface
722 END
723 v9.CheckScriptSuccess(lines)
724
725 lines =<< trim END
726 interface SomethingWrong
727 static count = 7
728 endinterface
729 END
730 v9.CheckScriptFailure(lines, 'E1342:')
731
732 lines =<< trim END
733 vim9script
734
735 interface Some
736 static count: number
737 def Method(count: number)
738 endinterface
739 END
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000740 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
741
742 lines =<< trim END
743 vim9script
744
745 interface Some
746 this.value: number
747 def Method(value: number)
748 endinterface
749 END
750 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: value')
Bram Moolenaar554d0312023-01-05 19:59:18 +0000751
752 lines =<< trim END
753 vim9script
754 interface somethingWrong
755 static count = 7
756 endinterface
757 END
758 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
759
760 lines =<< trim END
761 vim9script
762 interface SomethingWrong
763 this.value: string
764 static count = 7
765 def GetCount(): number
766 endinterface
767 END
768 v9.CheckScriptFailure(lines, 'E1344:')
769
770 lines =<< trim END
771 vim9script
772 interface SomethingWrong
773 this.value: string
774 static count: number
775 def GetCount(): number
776 return 5
777 enddef
778 endinterface
779 END
780 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
781enddef
782
Bram Moolenaar94674f22023-01-06 18:42:20 +0000783def Test_class_implements_interface()
784 var lines =<< trim END
785 vim9script
786
787 interface Some
788 static count: number
789 def Method(nr: number)
790 endinterface
791
792 class SomeImpl implements Some
793 static count: number
794 def Method(nr: number)
795 echo nr
796 enddef
797 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000798
799 interface Another
800 this.member: string
801 endinterface
802
803 class SomeImpl implements Some, Another
804 this.member = 'abc'
805 static count: number
806 def Method(nr: number)
807 echo nr
808 enddef
809 endclass
810
Bram Moolenaar94674f22023-01-06 18:42:20 +0000811 END
812 v9.CheckScriptSuccess(lines)
813
814 lines =<< trim END
815 vim9script
816
817 interface Some
818 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000819 endinterface
820
821 class SomeImpl implements Some implements Some
822 static count: number
823 endclass
824 END
825 v9.CheckScriptFailure(lines, 'E1350:')
826
827 lines =<< trim END
828 vim9script
829
830 interface Some
831 static counter: number
832 endinterface
833
834 class SomeImpl implements Some, Some
835 static count: number
836 endclass
837 END
838 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
839
840 lines =<< trim END
841 vim9script
842
843 interface Some
844 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +0000845 def Method(nr: number)
846 endinterface
847
848 class SomeImpl implements Some
849 static count: number
850 def Method(nr: number)
851 echo nr
852 enddef
853 endclass
854 END
855 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
856
857 lines =<< trim END
858 vim9script
859
860 interface Some
861 static count: number
862 def Methods(nr: number)
863 endinterface
864
865 class SomeImpl implements Some
866 static count: number
867 def Method(nr: number)
868 echo nr
869 enddef
870 endclass
871 END
872 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
873enddef
874
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000875def Test_class_used_as_type()
876 var lines =<< trim END
877 vim9script
878
879 class Point
880 this.x = 0
881 this.y = 0
882 endclass
883
884 var p: Point
885 p = Point.new(2, 33)
886 assert_equal(2, p.x)
887 assert_equal(33, p.y)
888 END
889 v9.CheckScriptSuccess(lines)
890
891 lines =<< trim END
892 vim9script
893
894 interface HasX
895 this.x: number
896 endinterface
897
898 class Point implements HasX
899 this.x = 0
900 this.y = 0
901 endclass
902
903 var p: Point
904 p = Point.new(2, 33)
905 var hx = p
906 assert_equal(2, hx.x)
907 END
908 v9.CheckScriptSuccess(lines)
909
910 lines =<< trim END
911 vim9script
912
913 class Point
914 this.x = 0
915 this.y = 0
916 endclass
917
918 var p: Point
919 p = 'text'
920 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000921 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000922enddef
923
Bram Moolenaar83677162023-01-08 19:54:10 +0000924def Test_class_extends()
925 var lines =<< trim END
926 vim9script
927 class Base
928 this.one = 1
929 def GetOne(): number
930 return this.one
931 enddef
932 endclass
933 class Child extends Base
934 this.two = 2
935 def GetTotal(): number
936 return this.one + this.two
937 enddef
938 endclass
939 var o = Child.new()
940 assert_equal(1, o.one)
941 assert_equal(2, o.two)
942 assert_equal(1, o.GetOne())
943 assert_equal(3, o.GetTotal())
944 END
945 v9.CheckScriptSuccess(lines)
946
947 lines =<< trim END
948 vim9script
949 class Base
950 this.one = 1
951 endclass
952 class Child extends Base
953 this.two = 2
954 endclass
955 var o = Child.new(3, 44)
956 assert_equal(3, o.one)
957 assert_equal(44, o.two)
958 END
959 v9.CheckScriptSuccess(lines)
960
961 lines =<< trim END
962 vim9script
963 class Base
964 this.one = 1
965 endclass
966 class Child extends Base extends Base
967 this.two = 2
968 endclass
969 END
970 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
971
972 lines =<< trim END
973 vim9script
974 class Child extends BaseClass
975 this.two = 2
976 endclass
977 END
978 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
979
980 lines =<< trim END
981 vim9script
982 var SomeVar = 99
983 class Child extends SomeVar
984 this.two = 2
985 endclass
986 END
987 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +0000988
989 lines =<< trim END
990 vim9script
991 class Base
992 this.name: string
993 def ToString(): string
994 return this.name
995 enddef
996 endclass
997
998 class Child extends Base
999 this.age: number
1000 def ToString(): string
1001 return super.ToString() .. ': ' .. this.age
1002 enddef
1003 endclass
1004
1005 var o = Child.new('John', 42)
1006 assert_equal('John: 42', o.ToString())
1007 END
1008 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +00001009
1010 lines =<< trim END
1011 vim9script
1012 class Child
1013 this.age: number
1014 def ToString(): number
1015 return this.age
1016 enddef
1017 def ToString(): string
1018 return this.age
1019 enddef
1020 endclass
1021 END
1022 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
1023
1024 lines =<< trim END
1025 vim9script
1026 class Child
1027 this.age: number
1028 def ToString(): string
1029 return super .ToString() .. ': ' .. this.age
1030 enddef
1031 endclass
1032 var o = Child.new(42)
1033 echo o.ToString()
1034 END
1035 v9.CheckScriptFailure(lines, 'E1356:')
1036
1037 lines =<< trim END
1038 vim9script
1039 class Base
1040 this.name: string
1041 def ToString(): string
1042 return this.name
1043 enddef
1044 endclass
1045
1046 var age = 42
1047 def ToString(): string
1048 return super.ToString() .. ': ' .. age
1049 enddef
1050 echo ToString()
1051 END
1052 v9.CheckScriptFailure(lines, 'E1357:')
1053
1054 lines =<< trim END
1055 vim9script
1056 class Child
1057 this.age: number
1058 def ToString(): string
1059 return super.ToString() .. ': ' .. this.age
1060 enddef
1061 endclass
1062 var o = Child.new(42)
1063 echo o.ToString()
1064 END
1065 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001066
1067 lines =<< trim END
1068 vim9script
1069 class Base
1070 this.name: string
1071 static def ToString(): string
1072 return 'Base class'
1073 enddef
1074 endclass
1075
1076 class Child extends Base
1077 this.age: number
1078 def ToString(): string
1079 return Base.ToString() .. ': ' .. this.age
1080 enddef
1081 endclass
1082
1083 var o = Child.new('John', 42)
1084 assert_equal('Base class: 42', o.ToString())
1085 END
1086 v9.CheckScriptSuccess(lines)
Bram Moolenaar4cae8452023-01-15 15:51:48 +00001087
1088 lines =<< trim END
1089 vim9script
1090 class Base
1091 this.value = 1
1092 def new(init: number)
1093 this.value = number + 1
1094 enddef
1095 endclass
1096 class Child extends Base
1097 def new()
1098 this.new(3)
1099 enddef
1100 endclass
1101 var c = Child.new()
1102 END
1103 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Child": new(')
Bram Moolenaarae3205a2023-01-15 20:49:00 +00001104
1105 # base class with more than one object member
1106 lines =<< trim END
1107 vim9script
1108
1109 class Result
1110 this.success: bool
1111 this.value: any = null
1112 endclass
1113
1114 class Success extends Result
1115 def new(this.value = v:none)
1116 this.success = true
1117 enddef
1118 endclass
1119
1120 var v = Success.new('asdf')
1121 assert_equal("object of Success {success: true, value: 'asdf'}", string(v))
1122 END
1123 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +00001124enddef
1125
Bram Moolenaara86655a2023-01-12 17:06:27 +00001126def Test_class_import()
1127 var lines =<< trim END
1128 vim9script
1129 export class Animal
1130 this.kind: string
1131 this.name: string
1132 endclass
1133 END
1134 writefile(lines, 'Xanimal.vim', 'D')
1135
1136 lines =<< trim END
1137 vim9script
1138 import './Xanimal.vim' as animal
1139
1140 var a: animal.Animal
1141 a = animal.Animal.new('fish', 'Eric')
1142 assert_equal('fish', a.kind)
1143 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001144
1145 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1146 assert_equal('cat', b.kind)
1147 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001148 END
1149 v9.CheckScriptSuccess(lines)
1150enddef
1151
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001152def Test_abstract_class()
1153 var lines =<< trim END
1154 vim9script
1155 abstract class Base
1156 this.name: string
1157 endclass
1158 class Person extends Base
1159 this.age: number
1160 endclass
1161 var p: Base = Person.new('Peter', 42)
1162 assert_equal('Peter', p.name)
1163 assert_equal(42, p.age)
1164 END
1165 v9.CheckScriptSuccess(lines)
1166
1167 lines =<< trim END
1168 vim9script
1169 abstract class Base
1170 this.name: string
1171 endclass
1172 class Person extends Base
1173 this.age: number
1174 endclass
1175 var p = Base.new('Peter')
1176 END
1177 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Base": new(')
1178
1179 lines =<< trim END
1180 abstract class Base
1181 this.name: string
1182 endclass
1183 END
1184 v9.CheckScriptFailure(lines, 'E1316:')
1185enddef
1186
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001187
1188" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker