-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.lua
208 lines (173 loc) · 5.32 KB
/
test.lua
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
--- unit test for Bo-LuaOOP
-- @author BHOU
-- require 'bhou.oo' -- if you put '?.init.lua' in your lua path, you can use this to init the oo lib
require 'bhou.oo.init' -- this will register 'oo' as a global variable
local class = oo.class -- shortcut for class declaration
local new = oo.new -- shortcut for new an object
local interface = oo.interface -- shortcut for interface declaration
local allPass = true
--[[
util function for test
--]]
local function assertEquals(expected, value)
if expected ~= value then
print( "[Fail] expected '"..expected.."', but got '"..value.."'" )
allPass = false
return
end
print('[Pass]', expected, value)
end
local function assertNil(value)
if value ~= nil then
print( "[Fail] expected 'nil', but got '"..value.."'")
allPass = false
return
end
print('[Pass]', nil, value)
end
local function assertNotNil(value)
if value ~= nil then
print('[Pass]', nil, value)
return
end
print( "[Fail] expected Not 'nil', but got 'nil'")
allPass = false
end
--[[
define classes and interfaces used in the test
--]]
-- define parent class, all defined in the definition
local Parent = class('Parent', nil, nil, {
a = 1, -- properties, can be inherited in the children
add = function(self, b)
self.a = self.a + b
return self.a
end,
minus = function(self, b)
self.a = self.a - b
return self.a
end,
multiple = function(self, b)
self.a = self.a * b
return self.a
end,
getA = function(self)
return self.a
end
})
-- define child class, put method outside of class definition
local Child = class('Child', Parent, nil, {
b = 10;
})
function Child:add(b) -- override parent's method
self.a = self.a + self.b + b
return self.a
end
function Child:add2(b) -- use self.super to access super method
self.a = Child.super.add(self, b) + self.b
return self.a
end
-- define interface
local Interface = interface('Interf')
function Interface:divide(b) -- interface function without implementation
end
function Interface:divide2() -- interface function with implementation
self.a = self.a/2
return self.a
end
function Interface:divide5() -- interface function with implementation
self.a = self.a/5
return self.a
end
-- define child2 with interface
local Child2 = class('Child2', Parent, {Interface})
function Child2:divide(b)
self.a = self.a/b
return self.a
end
function Child2:divide2()
self.a = self.a / 4
return self.a
end
-- define GrandChild
local GrandChild = class('GrandChild', Child, {Interface})
function GrandChild:testSuperInterface()
return GrandChild.super.divide5(self) -- this will fail, because the super Child does not have divide5 implemented
end
local GrandChild2 = class('GrandChild', Child2, {Interface})
function GrandChild2:testSuperInterface()
return GrandChild2.super.divide5(self)
end
--[[--------------------------------------------------------------------
test begins here
--]]--------------------------------------------------------------------
print 'Global registry test'
assertNil(_G['NotExistedClass'])
assertNotNil(_G['Parent'])
assertNotNil(_G['Child'])
assertNotNil(_G['GrandChild'])
assertNotNil(_G['Interf'])
-- create parent object
print('Parent test')
local parent = new(Parent)
assertEquals(1, parent.a)
assertEquals(1, parent:getA())
assertEquals(10, parent:add(9))
assertEquals(5, parent:minus(5))
assertEquals(25, parent:multiple(5))
assertNil(parent.b)
print('Finshed testing parent\n')
-- create child object
print('Child test')
local child = new(Child)
assertEquals(1, child.a)
assertEquals(10, child.b)
assertEquals(20, child:add(9))
assertEquals(39, child:add2(9))
assertEquals(34, child:minus(5))
assertEquals(170, child:multiple(5))
print('Finshed testing child\n')
print('Interface test')
local child2 = new(Child2)
assertEquals(1, child2.a)
assertEquals(1, child2:getA())
assertEquals(10, child2:add(9))
assertEquals(5, child2:minus(5))
assertEquals(50, child2:multiple(10))
assertEquals(2, child2:divide(25))
assertEquals(20, child2:multiple(10))
assertEquals(5, child2:divide2()) -- use child2's method, so actually divide by 4
assertEquals(1, child2:divide5()) -- use interface method implement
print('Finished testing interface\n')
print('GrandChild test')
local grandChild = new(GrandChild) -- since grand child overrides nothing, the test is the same as the child
assertEquals(1, grandChild.a)
assertEquals(10, grandChild.b)
assertEquals(20, grandChild:add(9))
assertEquals(39, grandChild:add2(9))
assertEquals(34, grandChild:minus(5))
assertEquals(170, grandChild:multiple(5))
local grandChild2 = new(GrandChild2) -- since grand child overrides nothing, the test is the same as the child
assertEquals(1, grandChild2.a)
assertEquals(1, grandChild2:getA())
assertEquals(10, grandChild2:add(9))
assertEquals(5, grandChild2:minus(5))
assertEquals(50, grandChild2:multiple(10))
assertEquals(2, grandChild2:divide(25))
assertEquals(20, grandChild2:multiple(10))
assertEquals(5, grandChild2:divide2()) -- use child2's method, so actually divide by 4
assertEquals(1, grandChild2:divide5()) -- use interface method implement
assertEquals(100, grandChild2:multiple(100))
assertEquals(20, grandChild2:testSuperInterface())
local reflection = require 'bhou.oo.Reflection'
local funcs = reflection.functions(grandChild)
for k, v in pairs(funcs) do
print(k, v)
end
print()
if allPass then
print('LuaOOP works well!')
else
print('At least one test fails!')
os.exit(allPass)
end