-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathclass_inheritance.chai
More file actions
137 lines (110 loc) · 2.63 KB
/
class_inheritance.chai
File metadata and controls
137 lines (110 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class Base
{
attr x
def Base()
{
this.x = 10
}
def do_something()
{
return this.x * 2
}
}
class Derived : Base
{
attr y
def Derived()
{
this.x = 10
this.y = 20
}
def do_other()
{
return this.y * 3
}
}
// Test basic inheritance - derived can call base methods
auto d = Derived()
assert_equal(10, d.x)
assert_equal(20, d.y)
assert_equal(20, d.do_something())
assert_equal(60, d.do_other())
// Test base class still works independently
auto b = Base()
assert_equal(10, b.x)
assert_equal(20, b.do_something())
// Test method override
class Derived2 : Base
{
def Derived2()
{
this.x = 5
}
def do_something()
{
return this.x * 100
}
}
auto d2 = Derived2()
assert_equal(500, d2.do_something())
// Test passing a derived object to an untyped free function
def call_do_something_untyped(obj) {
return obj.do_something()
}
auto d3 = Derived()
assert_equal(20, call_do_something_untyped(d3))
assert_equal(20, call_do_something_untyped(Base()))
// Test typed functions: parameter declared as Base, accepts derived objects
def call_do_something(Base obj) {
return obj.do_something()
}
assert_equal(20, call_do_something(Base()))
assert_equal(20, call_do_something(d3))
// Test typed function accessing base attributes on a derived object
def get_x(Base obj) {
return obj.x
}
assert_equal(10, get_x(d3))
assert_equal(10, get_x(Base()))
// Test polymorphic dispatch through typed function: derived override is called
auto d4 = Derived2()
assert_equal(500, call_do_something(d4))
// Test mixing base and derived in a container, calling base methods
var objects = [Base(), Derived(), Derived2()]
assert_equal(20, objects[0].do_something())
assert_equal(20, objects[1].do_something())
assert_equal(500, objects[2].do_something())
// Test that derived objects still report correct type
auto d5 = Derived()
assert_true(d5.is_type("Derived"))
// Test multi-level inheritance
class GrandChild : Derived
{
attr z
def GrandChild()
{
this.x = 1
this.y = 2
this.z = 3
}
def do_grandchild()
{
return this.z * 4
}
}
auto gc = GrandChild()
assert_equal(1, gc.x)
assert_equal(2, gc.y)
assert_equal(3, gc.z)
assert_equal(2, gc.do_something()) // Base method
assert_equal(6, gc.do_other()) // Derived method
assert_equal(12, gc.do_grandchild()) // Own method
// Test passing grandchild to typed Base function (multi-level inheritance)
assert_equal(2, call_do_something(gc))
assert_equal(1, get_x(gc))
// Test typed function expecting mid-level type
def call_do_other(Derived obj) {
return obj.do_other()
}
assert_equal(6, call_do_other(gc))
assert_equal(60, call_do_other(Derived()))