blob: 85f47a88be8416de775655605e8f8f3f7812c89a [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 Moolenaar6481acc2023-01-11 21:14:17 +0000540enddef
541
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000542def Test_class_member()
543 # check access rules
Bram Moolenaard505d172022-12-18 21:42:55 +0000544 var lines =<< trim END
545 vim9script
546 class TextPos
547 this.lnum = 1
548 this.col = 1
549 static counter = 0
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000550 static _secret = 7
551 public static anybody = 42
Bram Moolenaard505d172022-12-18 21:42:55 +0000552
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000553 static def AddToCounter(nr: number)
Bram Moolenaard505d172022-12-18 21:42:55 +0000554 counter += nr
555 enddef
556 endclass
557
558 assert_equal(0, TextPos.counter)
559 TextPos.AddToCounter(3)
560 assert_equal(3, TextPos.counter)
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000561 assert_fails('echo TextPos.noSuchMember', 'E1338:')
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000562
563 def GetCounter(): number
564 return TextPos.counter
565 enddef
566 assert_equal(3, GetCounter())
Bram Moolenaard505d172022-12-18 21:42:55 +0000567
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000568 assert_fails('TextPos.noSuchMember = 2', 'E1337:')
Bram Moolenaar9f2d97e2022-12-31 19:01:02 +0000569 assert_fails('TextPos.counter = 5', 'E1335:')
570 assert_fails('TextPos.counter += 5', 'E1335:')
571
572 assert_fails('echo TextPos._secret', 'E1333:')
573 assert_fails('TextPos._secret = 8', 'E1333:')
574
575 assert_equal(42, TextPos.anybody)
576 TextPos.anybody = 12
577 assert_equal(12, TextPos.anybody)
578 TextPos.anybody += 5
579 assert_equal(17, TextPos.anybody)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000580 END
581 v9.CheckScriptSuccess(lines)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000582
Bram Moolenaar4cae8452023-01-15 15:51:48 +0000583 # example in the help
584 lines =<< trim END
585 vim9script
586 class OtherThing
587 this.size: number
588 static totalSize: number
589
590 def new(this.size)
591 totalSize += this.size
592 enddef
593 endclass
594 assert_equal(0, OtherThing.totalSize)
595 var to3 = OtherThing.new(3)
596 assert_equal(3, OtherThing.totalSize)
597 var to7 = OtherThing.new(7)
598 assert_equal(10, OtherThing.totalSize)
599 END
600 v9.CheckScriptSuccess(lines)
601
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000602 # check shadowing
603 lines =<< trim END
604 vim9script
605
606 class Some
607 static count = 0
608 def Method(count: number)
609 echo count
610 enddef
611 endclass
612
613 var s = Some.new()
614 s.Method(7)
615 END
616 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
617
618 lines =<< trim END
619 vim9script
620
621 class Some
622 static count = 0
623 def Method(arg: number)
624 var count = 3
625 echo arg count
626 enddef
627 endclass
628
629 var s = Some.new()
630 s.Method(7)
631 END
632 v9.CheckScriptFailure(lines, 'E1341: Variable already declared in the class: count')
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000633enddef
634
Bram Moolenaarcf760d52023-01-05 13:16:04 +0000635func Test_class_garbagecollect()
636 let lines =<< trim END
637 vim9script
638
639 class Point
640 this.p = [2, 3]
641 static pl = ['a', 'b']
642 static pd = {a: 'a', b: 'b'}
643 endclass
644
645 echo Point.pl Point.pd
646 call test_garbagecollect_now()
647 echo Point.pl Point.pd
648 END
649 call v9.CheckScriptSuccess(lines)
650endfunc
651
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000652def Test_class_function()
653 var lines =<< trim END
654 vim9script
655 class Value
656 this.value = 0
657 static objects = 0
658
659 def new(v: number)
660 this.value = v
661 ++objects
662 enddef
663
664 static def GetCount(): number
665 return objects
666 enddef
667 endclass
668
669 assert_equal(0, Value.GetCount())
670 var v1 = Value.new(2)
671 assert_equal(1, Value.GetCount())
672 var v2 = Value.new(7)
673 assert_equal(2, Value.GetCount())
674 END
675 v9.CheckScriptSuccess(lines)
676enddef
677
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +0000678def Test_class_object_to_string()
679 var lines =<< trim END
680 vim9script
681 class TextPosition
682 this.lnum = 1
683 this.col = 22
684 endclass
685
686 assert_equal("class TextPosition", string(TextPosition))
687
688 var pos = TextPosition.new()
689 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
690 END
691 v9.CheckScriptSuccess(lines)
692enddef
Bram Moolenaar74e12742022-12-13 21:14:28 +0000693
Bram Moolenaar554d0312023-01-05 19:59:18 +0000694def Test_interface_basics()
695 var lines =<< trim END
696 vim9script
697 interface Something
698 this.value: string
699 static count: number
700 def GetCount(): number
701 endinterface
702 END
703 v9.CheckScriptSuccess(lines)
704
705 lines =<< trim END
706 interface SomethingWrong
707 static count = 7
708 endinterface
709 END
710 v9.CheckScriptFailure(lines, 'E1342:')
711
712 lines =<< trim END
713 vim9script
714
715 interface Some
716 static count: number
717 def Method(count: number)
718 endinterface
719 END
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000720 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
721
722 lines =<< trim END
723 vim9script
724
725 interface Some
726 this.value: number
727 def Method(value: number)
728 endinterface
729 END
730 v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: value')
Bram Moolenaar554d0312023-01-05 19:59:18 +0000731
732 lines =<< trim END
733 vim9script
734 interface somethingWrong
735 static count = 7
736 endinterface
737 END
738 v9.CheckScriptFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong')
739
740 lines =<< trim END
741 vim9script
742 interface SomethingWrong
743 this.value: string
744 static count = 7
745 def GetCount(): number
746 endinterface
747 END
748 v9.CheckScriptFailure(lines, 'E1344:')
749
750 lines =<< trim END
751 vim9script
752 interface SomethingWrong
753 this.value: string
754 static count: number
755 def GetCount(): number
756 return 5
757 enddef
758 endinterface
759 END
760 v9.CheckScriptFailure(lines, 'E1345: Not a valid command in an interface: return 5')
761enddef
762
Bram Moolenaar94674f22023-01-06 18:42:20 +0000763def Test_class_implements_interface()
764 var lines =<< trim END
765 vim9script
766
767 interface Some
768 static count: number
769 def Method(nr: number)
770 endinterface
771
772 class SomeImpl implements Some
773 static count: number
774 def Method(nr: number)
775 echo nr
776 enddef
777 endclass
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000778
779 interface Another
780 this.member: string
781 endinterface
782
783 class SomeImpl implements Some, Another
784 this.member = 'abc'
785 static count: number
786 def Method(nr: number)
787 echo nr
788 enddef
789 endclass
790
Bram Moolenaar94674f22023-01-06 18:42:20 +0000791 END
792 v9.CheckScriptSuccess(lines)
793
794 lines =<< trim END
795 vim9script
796
797 interface Some
798 static counter: number
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000799 endinterface
800
801 class SomeImpl implements Some implements Some
802 static count: number
803 endclass
804 END
805 v9.CheckScriptFailure(lines, 'E1350:')
806
807 lines =<< trim END
808 vim9script
809
810 interface Some
811 static counter: number
812 endinterface
813
814 class SomeImpl implements Some, Some
815 static count: number
816 endclass
817 END
818 v9.CheckScriptFailure(lines, 'E1351: Duplicate interface after "implements": Some')
819
820 lines =<< trim END
821 vim9script
822
823 interface Some
824 static counter: number
Bram Moolenaar94674f22023-01-06 18:42:20 +0000825 def Method(nr: number)
826 endinterface
827
828 class SomeImpl implements Some
829 static count: number
830 def Method(nr: number)
831 echo nr
832 enddef
833 endclass
834 END
835 v9.CheckScriptFailure(lines, 'E1348: Member "counter" of interface "Some" not implemented')
836
837 lines =<< trim END
838 vim9script
839
840 interface Some
841 static count: number
842 def Methods(nr: number)
843 endinterface
844
845 class SomeImpl implements Some
846 static count: number
847 def Method(nr: number)
848 echo nr
849 enddef
850 endclass
851 END
852 v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
853enddef
854
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000855def Test_class_used_as_type()
856 var lines =<< trim END
857 vim9script
858
859 class Point
860 this.x = 0
861 this.y = 0
862 endclass
863
864 var p: Point
865 p = Point.new(2, 33)
866 assert_equal(2, p.x)
867 assert_equal(33, p.y)
868 END
869 v9.CheckScriptSuccess(lines)
870
871 lines =<< trim END
872 vim9script
873
874 interface HasX
875 this.x: number
876 endinterface
877
878 class Point implements HasX
879 this.x = 0
880 this.y = 0
881 endclass
882
883 var p: Point
884 p = Point.new(2, 33)
885 var hx = p
886 assert_equal(2, hx.x)
887 END
888 v9.CheckScriptSuccess(lines)
889
890 lines =<< trim END
891 vim9script
892
893 class Point
894 this.x = 0
895 this.y = 0
896 endclass
897
898 var p: Point
899 p = 'text'
900 END
Bram Moolenaar6481acc2023-01-11 21:14:17 +0000901 v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string')
Bram Moolenaareca2c5f2023-01-07 12:08:41 +0000902enddef
903
Bram Moolenaar83677162023-01-08 19:54:10 +0000904def Test_class_extends()
905 var lines =<< trim END
906 vim9script
907 class Base
908 this.one = 1
909 def GetOne(): number
910 return this.one
911 enddef
912 endclass
913 class Child extends Base
914 this.two = 2
915 def GetTotal(): number
916 return this.one + this.two
917 enddef
918 endclass
919 var o = Child.new()
920 assert_equal(1, o.one)
921 assert_equal(2, o.two)
922 assert_equal(1, o.GetOne())
923 assert_equal(3, o.GetTotal())
924 END
925 v9.CheckScriptSuccess(lines)
926
927 lines =<< trim END
928 vim9script
929 class Base
930 this.one = 1
931 endclass
932 class Child extends Base
933 this.two = 2
934 endclass
935 var o = Child.new(3, 44)
936 assert_equal(3, o.one)
937 assert_equal(44, o.two)
938 END
939 v9.CheckScriptSuccess(lines)
940
941 lines =<< trim END
942 vim9script
943 class Base
944 this.one = 1
945 endclass
946 class Child extends Base extends Base
947 this.two = 2
948 endclass
949 END
950 v9.CheckScriptFailure(lines, 'E1352: Duplicate "extends"')
951
952 lines =<< trim END
953 vim9script
954 class Child extends BaseClass
955 this.two = 2
956 endclass
957 END
958 v9.CheckScriptFailure(lines, 'E1353: Class name not found: BaseClass')
959
960 lines =<< trim END
961 vim9script
962 var SomeVar = 99
963 class Child extends SomeVar
964 this.two = 2
965 endclass
966 END
967 v9.CheckScriptFailure(lines, 'E1354: Cannot extend SomeVar')
Bram Moolenaar58b40092023-01-11 15:59:05 +0000968
969 lines =<< trim END
970 vim9script
971 class Base
972 this.name: string
973 def ToString(): string
974 return this.name
975 enddef
976 endclass
977
978 class Child extends Base
979 this.age: number
980 def ToString(): string
981 return super.ToString() .. ': ' .. this.age
982 enddef
983 endclass
984
985 var o = Child.new('John', 42)
986 assert_equal('John: 42', o.ToString())
987 END
988 v9.CheckScriptSuccess(lines)
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000989
990 lines =<< trim END
991 vim9script
992 class Child
993 this.age: number
994 def ToString(): number
995 return this.age
996 enddef
997 def ToString(): string
998 return this.age
999 enddef
1000 endclass
1001 END
1002 v9.CheckScriptFailure(lines, 'E1355: Duplicate function: ToString')
1003
1004 lines =<< trim END
1005 vim9script
1006 class Child
1007 this.age: number
1008 def ToString(): string
1009 return super .ToString() .. ': ' .. this.age
1010 enddef
1011 endclass
1012 var o = Child.new(42)
1013 echo o.ToString()
1014 END
1015 v9.CheckScriptFailure(lines, 'E1356:')
1016
1017 lines =<< trim END
1018 vim9script
1019 class Base
1020 this.name: string
1021 def ToString(): string
1022 return this.name
1023 enddef
1024 endclass
1025
1026 var age = 42
1027 def ToString(): string
1028 return super.ToString() .. ': ' .. age
1029 enddef
1030 echo ToString()
1031 END
1032 v9.CheckScriptFailure(lines, 'E1357:')
1033
1034 lines =<< trim END
1035 vim9script
1036 class Child
1037 this.age: number
1038 def ToString(): string
1039 return super.ToString() .. ': ' .. this.age
1040 enddef
1041 endclass
1042 var o = Child.new(42)
1043 echo o.ToString()
1044 END
1045 v9.CheckScriptFailure(lines, 'E1358:')
Bram Moolenaar6481acc2023-01-11 21:14:17 +00001046
1047 lines =<< trim END
1048 vim9script
1049 class Base
1050 this.name: string
1051 static def ToString(): string
1052 return 'Base class'
1053 enddef
1054 endclass
1055
1056 class Child extends Base
1057 this.age: number
1058 def ToString(): string
1059 return Base.ToString() .. ': ' .. this.age
1060 enddef
1061 endclass
1062
1063 var o = Child.new('John', 42)
1064 assert_equal('Base class: 42', o.ToString())
1065 END
1066 v9.CheckScriptSuccess(lines)
Bram Moolenaar4cae8452023-01-15 15:51:48 +00001067
1068 lines =<< trim END
1069 vim9script
1070 class Base
1071 this.value = 1
1072 def new(init: number)
1073 this.value = number + 1
1074 enddef
1075 endclass
1076 class Child extends Base
1077 def new()
1078 this.new(3)
1079 enddef
1080 endclass
1081 var c = Child.new()
1082 END
1083 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Child": new(')
Bram Moolenaarae3205a2023-01-15 20:49:00 +00001084
1085 # base class with more than one object member
1086 lines =<< trim END
1087 vim9script
1088
1089 class Result
1090 this.success: bool
1091 this.value: any = null
1092 endclass
1093
1094 class Success extends Result
1095 def new(this.value = v:none)
1096 this.success = true
1097 enddef
1098 endclass
1099
1100 var v = Success.new('asdf')
1101 assert_equal("object of Success {success: true, value: 'asdf'}", string(v))
1102 END
1103 v9.CheckScriptSuccess(lines)
Bram Moolenaar83677162023-01-08 19:54:10 +00001104enddef
1105
Bram Moolenaara86655a2023-01-12 17:06:27 +00001106def Test_class_import()
1107 var lines =<< trim END
1108 vim9script
1109 export class Animal
1110 this.kind: string
1111 this.name: string
1112 endclass
1113 END
1114 writefile(lines, 'Xanimal.vim', 'D')
1115
1116 lines =<< trim END
1117 vim9script
1118 import './Xanimal.vim' as animal
1119
1120 var a: animal.Animal
1121 a = animal.Animal.new('fish', 'Eric')
1122 assert_equal('fish', a.kind)
1123 assert_equal('Eric', a.name)
Bram Moolenaar40594002023-01-12 20:04:51 +00001124
1125 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
1126 assert_equal('cat', b.kind)
1127 assert_equal('Garfield', b.name)
Bram Moolenaara86655a2023-01-12 17:06:27 +00001128 END
1129 v9.CheckScriptSuccess(lines)
1130enddef
1131
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001132def Test_abstract_class()
1133 var lines =<< trim END
1134 vim9script
1135 abstract class Base
1136 this.name: string
1137 endclass
1138 class Person extends Base
1139 this.age: number
1140 endclass
1141 var p: Base = Person.new('Peter', 42)
1142 assert_equal('Peter', p.name)
1143 assert_equal(42, p.age)
1144 END
1145 v9.CheckScriptSuccess(lines)
1146
1147 lines =<< trim END
1148 vim9script
1149 abstract class Base
1150 this.name: string
1151 endclass
1152 class Person extends Base
1153 this.age: number
1154 endclass
1155 var p = Base.new('Peter')
1156 END
1157 v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Base": new(')
1158
1159 lines =<< trim END
1160 abstract class Base
1161 this.name: string
1162 endclass
1163 END
1164 v9.CheckScriptFailure(lines, 'E1316:')
1165enddef
1166
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001167
1168" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker