middleclass/spec/Object_spec.lua

127 lines
2.9 KiB
Lua
Raw Normal View History

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
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()
local obj = Object()
2013-09-17 18:55:50 +00:00
assert.is_true(obj:isInstanceOf(Object))
end)
end)
2011-08-07 22:07:02 +00:00
2013-09-17 18:55:50 +00:00
describe('subclass', function()
2013-09-17 18:55:50 +00:00
it('throws an error when used without the :', function()
assert.error(function() Object.subclass() end)
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
local SubClass
2013-09-17 18:55:50 +00:00
before_each(function()
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)
end)
2011-08-07 22:07:02 +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
local SubClass
2011-08-07 22:07:02 +00:00
2013-09-17 18:55:50 +00:00
before_each(function()
SubClass = class('SubClass')
function SubClass:initialize() self.mark=true end
end)
2013-09-17 18:55:50 +00:00
describe('allocate', function()
2013-09-17 18:55:50 +00:00
it('allocates instances properly', function()
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))
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)
end)
2011-08-07 22:07:02 +00:00
2013-09-17 18:55:50 +00:00
it('does not call the initializer', function()
local allocated = SubClass:allocate()
2013-09-17 18:55:50 +00:00
assert.is_nil(allocated.mark)
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
local previousAllocate = SubClass.static.allocate
2011-08-07 22:07:02 +00:00
function SubClass.static:allocate()
local instance = previousAllocate(SubClass)
instance.mark = true
return instance
end
2011-08-07 22:07:02 +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
end)
2011-08-07 22:07:02 +00:00
end)
2013-09-17 18:55:50 +00:00
describe('new', function()
2013-09-17 18:55:50 +00:00
it('initializes instances properly', function()
local instance = SubClass:new()
2013-09-17 18:55:50 +00:00
assert.equal(instance.class, SubClass)
end)
2013-09-17 18:55:50 +00:00
it('throws an error when used without the :', function()
assert.error(SubClass.new)
end)
2011-08-07 22:07:02 +00:00
2013-09-17 18:55:50 +00:00
it('calls the initializer', function()
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)
end)
2011-08-07 22:07:02 +00:00
end)
end)