mirror of
https://github.com/kikito/middleclass.git
synced 2024-11-08 09:34:22 +00:00
added more tests
This commit is contained in:
parent
0c5b65c1a2
commit
d180d7a24f
@ -2,26 +2,128 @@ require('MiddleClass')
|
||||
|
||||
context( 'Object', function()
|
||||
|
||||
context( 'When creating a subclass of Object', function()
|
||||
context( 'When creating a direct subclass of Object', function()
|
||||
|
||||
context( 'using Object:subclass("name")', function()
|
||||
local MyClass = Object:subclass('MyClass')
|
||||
|
||||
test( 'it should have its name correctly set up', function()
|
||||
test( 'should have its name correctly set up', function()
|
||||
assert_equal(MyClass.name, 'MyClass')
|
||||
end)
|
||||
|
||||
test( 'it should have Object as its superclass', function()
|
||||
test( 'should have Object as its superclass', function()
|
||||
assert_equal(MyClass.superclass, Object)
|
||||
end)
|
||||
end)
|
||||
|
||||
context( 'When no name is given', function()
|
||||
test( 'It should throw an error', function()
|
||||
test( 'should throw an error', function()
|
||||
assert_false( pcall(Object.subclass, Object) )
|
||||
end)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context( 'An instance attribute', function()
|
||||
local Person = class('Person')
|
||||
function Person:initialize(name)
|
||||
super.initialize(self)
|
||||
self.name = name
|
||||
end
|
||||
|
||||
local AgedPerson = class('AgedPerson', Person)
|
||||
function AgedPerson:initialize(name, age)
|
||||
super.initialize(self, name)
|
||||
self.age = age
|
||||
end
|
||||
|
||||
test('should be available after being initialized', function()
|
||||
local bob = Person:new('bob')
|
||||
assert_equal(bob.name, 'bob')
|
||||
end)
|
||||
|
||||
test('should be available after being initialized by a superclass', function()
|
||||
local pete = AgedPerson:new('pete', 31)
|
||||
assert_equal(pete.name, 'pete')
|
||||
assert_equal(pete.age, 31)
|
||||
end)
|
||||
end)
|
||||
|
||||
context( 'An instance method', function()
|
||||
local A = class('A')
|
||||
function A:foo() return 'foo' end
|
||||
function A:bar() return 'bar' end
|
||||
|
||||
local B = class('B', A)
|
||||
function B:foo() return 'baz' end
|
||||
|
||||
local a = A:new()
|
||||
local b = B:new()
|
||||
|
||||
test('should be available for any instance', function()
|
||||
assert_equal(a:foo(), 'foo')
|
||||
end)
|
||||
|
||||
test('should be inheritable', function()
|
||||
assert_equal(b:bar(), 'bar')
|
||||
end)
|
||||
|
||||
test('should be overridable', function()
|
||||
assert_equal(b:foo(), 'baz')
|
||||
end)
|
||||
end)
|
||||
|
||||
context( 'A super call', function()
|
||||
local Level0 = Object:subclass('Level0')
|
||||
function Level0:initialize() self.type = self:getType() end
|
||||
function Level0:getType() return 'level0' end
|
||||
function Level0:getNumber() return 10 end
|
||||
|
||||
local Level1 = Level0:subclass('Level1')
|
||||
function Level1:initialize() super.initialize(self) end
|
||||
function Level1:getType() return 'level1' end
|
||||
|
||||
local Level2 = Level1:subclass('Level2')
|
||||
function Level2:initialize() super.initialize(self) end
|
||||
function Level2:getType() return 'level2' end
|
||||
-- Calling super.getNumber(self) on a Level2 object skips Level1
|
||||
-- (since it's not overriden here) and calls Level0:getNumber()
|
||||
function Level2:getNumber() return super.getNumber(self) + 1 end
|
||||
|
||||
local level0 = Level0:new()
|
||||
local level1 = Level1:new()
|
||||
local level2 = Level2:new()
|
||||
|
||||
test('should jump accross classes when not defined on the middle one', function()
|
||||
assert_equal(level2:getNumber(), 11)
|
||||
end)
|
||||
|
||||
test('should use the appropiate versions of each method on every level', function()
|
||||
assert_equal(level0.type, 'level0')
|
||||
assert_equal(level1.type, 'level1')
|
||||
assert_equal(level2.type, 'level2')
|
||||
end)
|
||||
end)
|
||||
|
||||
context( 'A class attribute', function()
|
||||
local A = class('A')
|
||||
A.foo = 'foo'
|
||||
|
||||
local B = class('B', A)
|
||||
|
||||
test('should be available after being initialized', function()
|
||||
assert_equal(A.foo, 'foo')
|
||||
end)
|
||||
|
||||
test('should be available for subclasses', function()
|
||||
assert_equal(B.foo, 'foo')
|
||||
end)
|
||||
|
||||
test('should be overridable by subclasses, without affecting the superclasses', function()
|
||||
B.foo = 'chunky bacon'
|
||||
assert_equal(B.foo, 'chunky bacon')
|
||||
assert_equal(A.foo, 'foo')
|
||||
end)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -7,11 +7,11 @@ context( 'class', function()
|
||||
context( 'using class("name")', function()
|
||||
local TheClass = class('TheClass')
|
||||
|
||||
test( 'it should have the correct name', function()
|
||||
test( 'should have the correct name', function()
|
||||
assert_equal(TheClass.name, 'TheClass')
|
||||
end)
|
||||
|
||||
test( 'it should have Object as their superclass', function()
|
||||
test( 'should have Object as their superclass', function()
|
||||
assert_equal(TheClass.superclass, Object)
|
||||
end)
|
||||
end)
|
||||
@ -20,7 +20,7 @@ context( 'class', function()
|
||||
local TheSuperClass = class('TheSuperClass')
|
||||
local TheSubClass = class('TheSubClass', TheSuperClass)
|
||||
|
||||
test( 'it should have the correct superclass', function()
|
||||
test( 'should have the correct superclass', function()
|
||||
assert_equal(TheSubClass.superclass, TheSuperClass)
|
||||
end)
|
||||
end)
|
||||
|
@ -14,7 +14,7 @@ context( 'instanceOf', function()
|
||||
local f2 = function() return instanceOf(primitive, o) end
|
||||
local f3 = function() return instanceOf(primitive, primitive) end
|
||||
|
||||
context('it should not throw errors', function()
|
||||
context('should not throw errors', function()
|
||||
test('instanceOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
end)
|
||||
@ -26,7 +26,7 @@ context( 'instanceOf', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
test('it should make instanceOf return false', function()
|
||||
test('should make instanceOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
@ -45,31 +45,31 @@ context( 'instanceOf', function()
|
||||
|
||||
local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
|
||||
|
||||
test('it should be instanceOf(Object)', function()
|
||||
test('should be instanceOf(Object)', function()
|
||||
assert_true(instanceOf(Object, o1))
|
||||
assert_true(instanceOf(Object, o2))
|
||||
assert_true(instanceOf(Object, o3))
|
||||
end)
|
||||
|
||||
test('it should be instanceOf its class', function()
|
||||
test('should be instanceOf its class', function()
|
||||
assert_true(instanceOf(Class1, o1))
|
||||
assert_true(instanceOf(Class2, o2))
|
||||
assert_true(instanceOf(Class3, o3))
|
||||
end)
|
||||
|
||||
test('it should be instanceOf its class\' superclasses', function()
|
||||
test('should be instanceOf its class\' superclasses', function()
|
||||
assert_true(instanceOf(Class1, o2))
|
||||
assert_true(instanceOf(Class1, o3))
|
||||
assert_true(instanceOf(Class2, o3))
|
||||
end)
|
||||
|
||||
test('it should not be an instanceOf its class\' subclasses', function()
|
||||
test('should not be an instanceOf its class\' subclasses', function()
|
||||
assert_false(instanceOf(Class2, o1))
|
||||
assert_false(instanceOf(Class3, o1))
|
||||
assert_false(instanceOf(Class3, o2))
|
||||
end)
|
||||
|
||||
test('it should not be an instanceOf an unrelated class', function()
|
||||
test('should not be an instanceOf an unrelated class', function()
|
||||
assert_false(instanceOf(UnrelatedClass, o1))
|
||||
assert_false(instanceOf(UnrelatedClass, o2))
|
||||
assert_false(instanceOf(UnrelatedClass, o3))
|
||||
|
@ -13,7 +13,7 @@ context( 'subclassOf', function()
|
||||
local f2 = function() return subclassOf(primitive, o) end
|
||||
local f3 = function() return subclassOf(primitive, primitive) end
|
||||
|
||||
context('it should not throw errors', function()
|
||||
context('should not throw errors', function()
|
||||
test('subclassOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
end)
|
||||
@ -25,7 +25,7 @@ context( 'subclassOf', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
test('it should make subclassOf return false', function()
|
||||
test('should make subclassOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
@ -35,34 +35,34 @@ context( 'subclassOf', function()
|
||||
end -- for
|
||||
end)
|
||||
|
||||
context( 'An class', function()
|
||||
context( 'Any class (except Object)', function()
|
||||
local Class1 = class('Class1')
|
||||
local Class2 = class('Class2', Class1)
|
||||
local Class3 = class('Class3', Class2)
|
||||
local UnrelatedClass = class('Unrelated')
|
||||
|
||||
test('it should be subclassOf(Object)', function()
|
||||
test('should be subclassOf(Object)', function()
|
||||
assert_true(subclassOf(Object, Class1))
|
||||
assert_true(subclassOf(Object, Class2))
|
||||
assert_true(subclassOf(Object, Class3))
|
||||
end)
|
||||
|
||||
test('it should be subclassOf its direct superclass', function()
|
||||
test('should be subclassOf its direct superclass', function()
|
||||
assert_true(subclassOf(Class1, Class2))
|
||||
assert_true(subclassOf(Class2, Class3))
|
||||
end)
|
||||
|
||||
test('it should be subclassOf its ancestors', function()
|
||||
test('should be subclassOf its ancestors', function()
|
||||
assert_true(subclassOf(Class1, Class3))
|
||||
end)
|
||||
|
||||
test('it should not be an subclassOf its class\' subclasses', function()
|
||||
test('should not be an subclassOf its class\' subclasses', function()
|
||||
assert_false(subclassOf(Class2, Class1))
|
||||
assert_false(subclassOf(Class3, Class1))
|
||||
assert_false(subclassOf(Class3, Class2))
|
||||
end)
|
||||
|
||||
test('it should not be an subclassOf an unrelated class', function()
|
||||
test('should not be an subclassOf an unrelated class', function()
|
||||
assert_false(subclassOf(UnrelatedClass, Class1))
|
||||
assert_false(subclassOf(UnrelatedClass, Class2))
|
||||
assert_false(subclassOf(UnrelatedClass, Class3))
|
||||
|
Loading…
Reference in New Issue
Block a user