2013-09-15 21:14:00 +00:00
|
|
|
local class = require 'middleclass'
|
2013-09-15 21:18:08 +00:00
|
|
|
local Object = class.Object
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('Object', function()
|
2011-08-07 22:07:02 +00:00
|
|
|
|
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-17 18:55:50 +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)
|
|
|
|
|
2011-08-07 22:07:02 +00:00
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|