2013-09-15 21:14:00 +00:00
|
|
|
local class = require 'middleclass'
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2015-12-13 00:06:23 +00:00
|
|
|
describe('Default methods', function()
|
|
|
|
local Object
|
|
|
|
before_each(function()
|
|
|
|
Object = class('Object')
|
|
|
|
end)
|
2011-08-13 01:12:21 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('name', function()
|
|
|
|
it('is correctly set', function()
|
|
|
|
assert.equal(Object.name, 'Object')
|
2011-08-07 22:07:02 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('tostring', function()
|
|
|
|
it('returns "class Object"', function()
|
|
|
|
assert.equal(tostring(Object), 'class Object')
|
2011-08-07 22:07:02 +00:00
|
|
|
end)
|
|
|
|
end)
|
2011-08-11 09:36:23 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('()', function()
|
|
|
|
it('returns an object, like Object:new()', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
local obj = Object()
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.is_true(obj:isInstanceOf(Object))
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('subclass', function()
|
2011-08-08 05:38:11 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('throws an error when used without the :', function()
|
|
|
|
assert.error(function() Object.subclass() end)
|
2011-08-08 05:38:11 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('throws an error when no name is given', function()
|
|
|
|
assert.error( function() Object:subclass() end)
|
2011-08-13 22:08:33 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('when given a class name', function()
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
local SubClass
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
before_each(function()
|
2011-08-13 01:12:21 +00:00
|
|
|
SubClass = Object:subclass('SubClass')
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('it returns a class with the correct name', function()
|
|
|
|
assert.equal(SubClass.name, 'SubClass')
|
2011-08-07 22:07:02 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('it returns a class with the correct superclass', function()
|
|
|
|
assert.equal(SubClass.super, Object)
|
2011-08-07 22:07:02 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('it includes the subclass in the list of subclasses', function()
|
|
|
|
assert.is_true(Object.subclasses[SubClass])
|
2011-08-14 16:39:57 +00:00
|
|
|
end)
|
|
|
|
|
2011-08-08 05:38:11 +00:00
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-08 05:38:11 +00:00
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('instance creation', function()
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
local SubClass
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
before_each(function()
|
2011-08-13 01:12:21 +00:00
|
|
|
SubClass = class('SubClass')
|
2011-08-12 08:33:58 +00:00
|
|
|
function SubClass:initialize() self.mark=true end
|
|
|
|
end)
|
2011-08-08 20:29:18 +00:00
|
|
|
|
2013-09-18 21:42:16 +00:00
|
|
|
describe('allocate', function()
|
2011-08-08 22:06:05 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('allocates instances properly', function()
|
2011-08-13 01:12:21 +00:00
|
|
|
local instance = SubClass:allocate()
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.equal(instance.class, SubClass)
|
|
|
|
assert.equal(tostring(instance), "instance of " .. tostring(SubClass))
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('throws an error when used without the :', function()
|
|
|
|
assert.error(Object.allocate)
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('does not call the initializer', function()
|
2011-08-13 01:12:21 +00:00
|
|
|
local allocated = SubClass:allocate()
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.is_nil(allocated.mark)
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('can be overriden', function()
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
local previousAllocate = SubClass.static.allocate
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
function SubClass.static:allocate()
|
|
|
|
local instance = previousAllocate(SubClass)
|
|
|
|
instance.mark = true
|
|
|
|
return instance
|
|
|
|
end
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
local allocated = SubClass:allocate()
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.is_true(allocated.mark)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('new', function()
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('initializes instances properly', function()
|
2011-08-13 01:12:21 +00:00
|
|
|
local instance = SubClass:new()
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.equal(instance.class, SubClass)
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('throws an error when used without the :', function()
|
|
|
|
assert.error(SubClass.new)
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('calls the initializer', function()
|
2011-08-13 01:12:21 +00:00
|
|
|
local initialized = SubClass:new()
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.is_true(initialized.mark)
|
2011-08-07 22:07:02 +00:00
|
|
|
end)
|
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-18 21:42:16 +00:00
|
|
|
describe('isInstanceOf', function()
|
|
|
|
|
|
|
|
describe('nils, integers, strings, tables, and functions', function()
|
|
|
|
local o = Object:new()
|
|
|
|
local primitives = {nil, 1, 'hello', {}, function() end}
|
|
|
|
|
|
|
|
for _,primitive in pairs(primitives) do
|
|
|
|
local theType = type(primitive)
|
|
|
|
describe('A ' .. theType, function()
|
|
|
|
|
|
|
|
local f1 = function() return Object.isInstanceOf(primitive, Object) end
|
|
|
|
local f2 = function() return Object.isInstanceOf(primitive, o) end
|
|
|
|
local f3 = function() return Object.isInstanceOf(primitive, primitive) end
|
|
|
|
|
|
|
|
describe('does not throw errors', function()
|
|
|
|
it('instanceOf(Object, '.. theType ..')', function()
|
|
|
|
assert.not_error(f1)
|
|
|
|
end)
|
|
|
|
it('instanceOf(' .. theType .. ', Object:new())', function()
|
|
|
|
assert.not_error(f2)
|
|
|
|
end)
|
|
|
|
it('instanceOf(' .. theType .. ',' .. theType ..')', function()
|
|
|
|
assert.not_error(f3)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('makes instanceOf return false', function()
|
|
|
|
assert.is_false(f1())
|
|
|
|
assert.is_false(f2())
|
|
|
|
assert.is_false(f3())
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('An instance', function()
|
|
|
|
local Class1 = class('Class1')
|
|
|
|
local Class2 = class('Class2', Class1)
|
|
|
|
local Class3 = class('Class3', Class2)
|
|
|
|
local UnrelatedClass = class('Unrelated')
|
|
|
|
|
|
|
|
local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
|
|
|
|
|
|
|
|
it('isInstanceOf its class', function()
|
|
|
|
assert.is_true(o1:isInstanceOf(Class1))
|
|
|
|
assert.is_true(o2:isInstanceOf(Class2))
|
|
|
|
assert.is_true(o3:isInstanceOf(Class3))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is instanceOf its class\' superclasses', function()
|
|
|
|
assert.is_true(o2:isInstanceOf(Class1))
|
|
|
|
assert.is_true(o3:isInstanceOf(Class1))
|
|
|
|
assert.is_true(o3:isInstanceOf(Class2))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is not instanceOf its class\' subclasses', function()
|
|
|
|
assert.is_false(o1:isInstanceOf(Class2))
|
|
|
|
assert.is_false(o1:isInstanceOf(Class3))
|
|
|
|
assert.is_false(o2:isInstanceOf(Class3))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is not instanceOf an unrelated class', function()
|
|
|
|
assert.is_false(o1:isInstanceOf(UnrelatedClass))
|
|
|
|
assert.is_false(o2:isInstanceOf(UnrelatedClass))
|
|
|
|
assert.is_false(o3:isInstanceOf(UnrelatedClass))
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
|
|
|
end)
|
|
|
|
|
2013-09-18 21:42:16 +00:00
|
|
|
describe('isSubclassOf', function()
|
|
|
|
|
|
|
|
describe('nils, integers, etc', function()
|
|
|
|
local primitives = {nil, 1, 'hello', {}, function() end}
|
|
|
|
|
|
|
|
for _,primitive in pairs(primitives) do
|
|
|
|
local theType = type(primitive)
|
|
|
|
describe('A ' .. theType, function()
|
|
|
|
|
|
|
|
local f1 = function() return Object.isSubclassOf(Object, primitive) end
|
|
|
|
local f2 = function() return Object.isSubclassOf(primitive, o) end
|
|
|
|
local f3 = function() return Object.isSubclassOf(primitive, primitive) end
|
|
|
|
|
|
|
|
describe('does not throw errors', function()
|
|
|
|
it('isSubclassOf(Object, '.. theType ..')', function()
|
|
|
|
assert.not_error(f1)
|
|
|
|
end)
|
|
|
|
it('isSubclassOf(' .. theType .. ', Object:new())', function()
|
|
|
|
assert.not_error(f2)
|
|
|
|
end)
|
|
|
|
it('isSubclassOf(' .. theType .. ',' .. theType ..')', function()
|
|
|
|
assert.not_error(f3)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('makes isSubclassOf return false', function()
|
|
|
|
assert.is_false(f1())
|
|
|
|
assert.is_false(f2())
|
|
|
|
assert.is_false(f3())
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Any class (except Object)', function()
|
|
|
|
local Class1 = class('Class1')
|
|
|
|
local Class2 = class('Class2', Class1)
|
|
|
|
local Class3 = class('Class3', Class2)
|
|
|
|
local UnrelatedClass = class('Unrelated')
|
|
|
|
|
|
|
|
it('is subclassOf its direct superclass', function()
|
|
|
|
assert.is_true(Class2:isSubclassOf(Class1))
|
|
|
|
assert.is_true(Class3:isSubclassOf(Class2))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is subclassOf its ancestors', function()
|
|
|
|
assert.is_true(Class3:isSubclassOf(Class1))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is a subclassOf its class\' subclasses', function()
|
|
|
|
assert.is_true(Class2:isSubclassOf(Class1))
|
|
|
|
assert.is_true(Class3:isSubclassOf(Class1))
|
|
|
|
assert.is_true(Class3:isSubclassOf(Class2))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is not a subclassOf an unrelated class', function()
|
|
|
|
assert.is_false(Class1:isSubclassOf(UnrelatedClass))
|
|
|
|
assert.is_false(Class2:isSubclassOf(UnrelatedClass))
|
|
|
|
assert.is_false(Class3:isSubclassOf(UnrelatedClass))
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
|