This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestObject.lua
More file actions
498 lines (413 loc) · 11.5 KB
/
TestObject.lua
File metadata and controls
498 lines (413 loc) · 11.5 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
--
-- Tested with Lua 5.1.3 & Lua 5.1.4
--
local Object = require 'objectlua.Object'
local Class = require 'objectlua.Class'
TestObject = {}
function TestObject:tearDown()
Class:reset()
package.loaded['SomeClass'] = nil
end
----
-- Inheritance & Initialization
function TestObject:testInheritanceAndInitialization()
local Toto = Object:subclass()
function Toto:initialize(name)
self.name = name
end
local toto = Toto:new('John')
local otherToto = Toto:new('Bob')
assertEquals(Toto, toto.class)
assertEquals(Object, Toto.superclass)
assertEquals(Object.class, Toto.class.superclass)
assertEquals(nil, toto.new)
assertEquals(toto.name, 'John')
assertEquals(otherToto.name, 'Bob')
end
----
-- Testing subclassing
function TestObject:testSubclassing()
local Toto = Object:subclass()
function Toto:initialize(name)
self.name = name
end
local john = Toto:new('John')
local bob = Toto:new('Bob')
assertEquals(john.name, 'John')
assertEquals(bob.name, 'Bob')
end
----
-- Calling super
function TestObject:testSuper()
local Toto = Object:subclass()
function Toto:initialize(name)
self.name = name
end
local Tata = Toto:subclass()
function Tata:initialize(name)
super(self, name)
end
local tata = Tata:new('Bob')
assertEquals(tata.name, 'Bob')
end
----
-- Calling super through two levels of inheritance.
-- This one can cause a stackoverflow with a too simple super() implementation.
function TestObject:testSuperThroughTwoLevels()
local Toto = Object:subclass()
function Toto:initialize(name)
self.name = name
end
local Tata = Toto:subclass()
function Tata:initialize(name)
super(self, name)
end
local Titi = Tata:subclass()
function Titi:initialize(name)
super(self, name)
end
local titi = Titi:new('Paul')
assertEquals(titi.name, 'Paul')
end
----
-- Virtual calls
function TestObject:testVirtualCall()
local Base = Object:subclass()
function Base:initialize()
self.type = self:getType()
end
function Base:getType()
return 'base'
end
--
local Derived = Base:subclass()
function Derived:getType()
return 'derived'
end
local derived = Derived:new()
assertEquals(derived.type, 'derived')
end
----
-- Super jump
function TestObject:testSuperJump()
local Level0 = Object:subclass()
function Level0:getNumber()
return 10
end
local Level1 = Level0:subclass()
local Level2 = Level1:subclass()
-- Calling super(self) in getNumber() on a Level2 object skips Level1
-- (since it's not overriden here) and calls Level0:getNumber()
function Level2:getNumber()
return super(self) + 1
end
local level2 = Level2:new()
assertEquals(level2:getNumber(), 11)
end
----
-- Super virtual calls through two levels...
function TestObject:testVirtualSuperThroughTwoLevels()
local Level0 = Object:subclass()
function Level0:initialize()
self.type = self:getType()
end
function Level0:getType()
return 'level0'
end
assertEquals(Level0:new().type, 'level0')
local Level1 = Level0:subclass()
function Level1:initialize()
super(self)
end
function Level1:getType()
return 'level1'
end
assertEquals(Level1:new().type, 'level1')
local Level2 = Level1:subclass()
function Level2:initialize()
super(self)
end
function Level2:getType()
return 'level2'
end
assertEquals(Level2:new().type, 'level2')
end
----
-- Calling initialize with two arguments through super
-- Passes since version 0.0.3
function TestObject:testInitializeWithTwoArguments()
local Toto = Object:subclass()
function Toto:initialize(name, color)
self.name = name
self.color = color
end
local toto = Toto:new('Bob', 'red')
assertEquals(toto.name, 'Bob')
assertEquals(toto.color, 'red')
local Tata = Toto:subclass()
function Tata:initialize(name, color)
super(self, name, color)
end
local tata = Tata:new('John', 'blue')
assertEquals(tata.name, 'John')
assertEquals(tata.color, 'blue')
end
----
-- Testing class methods
function TestObject:testClassMethods()
local Foo = Object:subclass()
function Foo.class:sayHello()
return 'Hello'
end
assertEquals(Foo:sayHello(), 'Hello')
end
----
-- Testing super on class methods
function TestObject:testSuperInClassMethods()
local Level1 = Object:subclass()
function Level1.class:getName()
return 'Level1'
end
assert('Level1' == Level1:getName())
local Level2 = Level1:subclass()
function Level2.class:getName()
return super(self)..'-Level2'
end
assertEquals(Level2:getName(), 'Level1-Level2')
end
----
-- Testing isKindOf
function TestObject:testIsKindOf()
local Level1 = Object:subclass()
local Level2 = Level1:subclass()
local object = Object:new()
local level2 = Level2:new()
assert(not object:isKindOf())
assert(object:isKindOf(Object))
assert(not object:isKindOf(Level1))
assert(level2:isKindOf(Object))
assert(level2:isKindOf(Level1))
assert(level2:isKindOf(Level2))
assert(not object:isKindOf(Class))
assert(not Level2:isKindOf(Level2))
end
----
-- Testing inheritsFrom
function TestObject:testInheritsFrom()
local Level1 = Object:subclass()
local Level2 = Level1:subclass()
assert(Level1:inheritsFrom(Object))
assert(Level2:inheritsFrom(Object))
assert(Level2:inheritsFrom(Level1))
assert(not Level2:inheritsFrom(Level2))
end
----
-- Validating object model
function TestObject:testObjectModel()
assertEquals(Object.class.class, Class)
assertEquals(Class.class.class, Class)
assertEquals(Object:subclass().class.class, Class)
assert(Object:isKindOf(Object))
assert(Object:isKindOf(Class))
assert(Class:isKindOf(Object))
assert(Class:isKindOf(Class))
assert(not Object:inheritsFrom(Class))
assert(Class:inheritsFrom(Object))
assert(not Object:inheritsFrom(Object))
end
----
-- Testing clone
function TestObject:testClone()
local Level1 = Object:subclass()
local level1 = Level1:new()
level1.value = 5
level1.values = {6, 7}
local clone = level1:clone()
assert(clone:isKindOf(level1.class))
assert(clone.value == level1.value)
assert(clone.values == level1.values)
end
----
-- Private metatables (get)
function TestObject:testPrivateMetatable()
local Level1 = Object:subclass()
local level1 = Level1:new()
assertEquals(getmetatable(Level1), 'private')
assertEquals(getmetatable(level1), 'private')
end
----
-- Private metatables (set)
function TestObject:testSetMetatableFails()
local Titi = Object:subclass()
local titi = Titi:new()
assertError(setmetatable, titi, {})
end
----
-- Adding methods to an instance
function TestObject:testAddingMethodToInstance()
local john = Object:new()
function john:itWorks()
return true
end
assert(john:itWorks())
end
----
-- Testing redefining new()
function TestObject:testRedefiningNew()
local OtherNew = Object:subclass()
function OtherNew.class:new(...)
local instance = super(self, ...)
instance.fromOtherNew = true
return instance
end
local Foo = OtherNew:subclass()
assert(Foo:isKindOf(Class))
local foo = Foo:new()
assert(foo.fromOtherNew)
end
----
-- Testing exception in super(self)
-- Passes since 0.3.2
function TestObject:testExceptionInSuper()
local Base = Object:subclass()
function Base:getString(name)
assert('string' == type(name))
return 'Base:'..name
end
local Derived = Base:subclass()
function Derived:getString()
pcall(super, self) -- throws & recovers immediately
return super(self, 'John')
end
local derived = Derived:new()
assertEquals(derived:getString(), 'Base:John')
end
----
-- Testing super(self) in tail call
function TestObject:testSuperTailCall()
local Base = Object:subclass()
function Base:getAnyString()
return 'Coco'
end
local Derived = Base:subclass()
function Derived:getAnyString()
return super(self)
end
local derived = Derived:new()
assertEquals(derived:getAnyString(), 'Coco')
end
----
-- Testing isMeta()
function TestObject:testIsMeta()
assert(not Object:isMeta())
assert(not Class:isMeta())
assert(Object.class:isMeta())
assert(Class.class:isMeta())
local Toto = Object:subclass()
assert(not Toto:isMeta())
assert(Toto.class:isMeta())
end
----
-- Multiple return values
function TestObject:testMultipleReturnValues()
local Toto = Object:subclass()
function Toto:returnTwoValues()
return 1, 2
end
local toto = Toto:new()
a, b = toto:returnTwoValues()
assertEquals(a, 1)
assertEquals(b, 2)
end
----
--
function TestObject:testCallingNonExistingMethodFails()
local Titi = Object:subclass()
local titi = Titi:new()
assertError(titi.aNonExistingMethod, titi)
end
----
-- Reading outside data
function TestObject:testReadOutsideData()
local toto = 1
local Foo = Object:subclass()
function Foo:readToto()
assertEquals(toto, 1)
end
local foo = Foo:new()
foo:readToto()
end
----
-- Creating global data
function TestObject:testCreateGlobalData()
local Foo = Object:subclass()
function Foo:createToto()
toto = 2
end
local foo = Foo:new()
foo:createToto()
assertEquals(toto, 2)
end
----
-- Testing named subclass in tail call
function TestObject:testNamedSubclassInTailCall()
return Object:subclass 'NamedClass'
end
----
-- Testing named classes
function TestObject:testClassName()
local NamedClass = Object:subclass 'NamedClass'
assertEquals(NamedClass:name(), 'NamedClass')
end
----
-- Testing scope
function TestObject:testNamedAndAnonymClassScope()
local assert = assert
local assertEquals = assertEquals
setfenv(1, {})
do
local NamedClass = Object:subclass 'NamedClass'
local AnonymClass = Object:subclass()
end
assertEquals(NamedClass, nil)
assertEquals(AnonymClass, nil)
assert(nil ~= Class:find 'NamedClass')
assertEquals(Class:find 'AnonymClass', nil)
end
----
-- Testing all Classes and class scope
function TestObject:testAllClasses()
(function()
Object:subclass 'NamedClass'
end)()
local classes = Class:all()
assertEquals(classes['objectlua.Object']:name(), 'objectlua.Object')
assertEquals(classes['objectlua.Object Metaclass']:name(), 'objectlua.Object Metaclass')
assertEquals(classes['objectlua.Class']:name(), 'objectlua.Class')
assertEquals(classes['objectlua.Class Metaclass']:name(), 'objectlua.Class Metaclass')
assertEquals(classes['NamedClass']:name(), 'NamedClass')
assertEquals(classes['NamedClass Metaclass']:name(), 'NamedClass Metaclass')
end
----
-- Testing requiring a class file
function TestObject:testLoadAClassFromFile()
local SomeClass = require 'SomeClass'
local someObject = SomeClass:new()
assertEquals(someObject:className(), 'SomeClass')
assert(someObject:itWorks())
end
function TestObject:testClassShadowedFails1()
Object:subclass('SomeClass')
assertError(require, 'SomeClass')
end
function TestObject:testClassShadowedFails2()
require 'SomeClass'
assertError(function()
Object:subclass('SomeClass')
end)
end
function TestObject:testGlobalName()
local Toto = Object:subclass('tata.Toto')
assert(_G.tata)
assert(_G.tata.Toto)
end