middleclass/spec/Object_spec.lua

124 lines
3.0 KiB
Lua
Raw Normal View History

2011-08-07 22:07:02 +00:00
require 'middleclass'
context('Object', function()
2011-08-07 22:07:02 +00:00
context('name', function()
2011-08-07 22:07:02 +00:00
test('is correctly set', function()
assert_equal(Object.name, 'Object')
end)
end)
context('tostring', function()
test('returns "class Object"', function()
assert_equal(tostring(Object), 'class Object')
end)
end)
2011-08-11 09:36:23 +00:00
context('()', function()
test('returns an object, like Object:new()', function()
local obj = Object()
2011-08-11 09:36:23 +00:00
assert_true(instanceOf(Object, obj))
end)
end)
2011-08-07 22:07:02 +00:00
context('subclass', function()
test('throws an error when used without the :', function()
assert_error(function() Object.subclass() end)
end)
context('when given a class name', function()
2011-08-07 22:07:02 +00:00
local SubClass = Object:subclass('SubClass')
2011-08-07 22:07:02 +00:00
test('it returns a class with the correct name', function()
assert_equal(SubClass.name, 'SubClass')
2011-08-07 22:07:02 +00:00
end)
test('it returns a class with the correct superclass', function()
assert_equal(SubClass.superclass, Object)
2011-08-07 22:07:02 +00:00
end)
end)
context('when no name is given', function()
test('it throws an error', function()
assert_error( function() Object:subclass() end)
end)
end)
2011-08-07 22:07:02 +00:00
end)
2011-08-07 22:07:02 +00:00
context('instance creation', function()
2011-08-07 22:07:02 +00:00
local SubClass
local classes = { Object, SubClass }
2011-08-07 22:07:02 +00:00
before(function()
SubClass = Object:subclass('SubClass')
function SubClass:initialize() self.mark=true end
end)
for _,theClass in ipairs(classes) do
context(theClass.name, function()
context('allocate', function()
test('allocates instances properly', function()
local instance = theClass:allocate()
assert_equal(instance.class, theClass)
assert_equal(tostring(instance), "instance of " .. tostring(theClass))
end)
2011-08-07 22:07:02 +00:00
test('throws an error when used without the :', function()
assert_error(Object.allocate)
2011-08-07 22:07:02 +00:00
end)
test('does not call the initializer', function()
local allocated = theClass:allocate()
assert_nil(allocated.mark)
end)
2011-08-07 22:07:02 +00:00
test('can be overriden', function()
2011-08-07 22:07:02 +00:00
local previousAllocate = theClass.allocate
2011-08-07 22:07:02 +00:00
function theClass.static:allocate()
local instance = previousAllocate(theClass)
instance.mark = true
return instance
end
2011-08-07 22:07:02 +00:00
local allocated = theClass:allocate()
assert_true(allocated.mark)
end)
2011-08-07 22:07:02 +00:00
end)
2011-08-07 22:07:02 +00:00
context('new', function()
2011-08-07 22:07:02 +00:00
test('initializes instances properly', function()
local instance = theClass:new()
assert_equal(instance.class, theClass)
2011-08-07 22:07:02 +00:00
end)
test('throws an error when used without the :', function()
assert_error(theClass.new)
2011-08-07 22:07:02 +00:00
end)
test('calls the initializer', function()
local allocated = theClass:new()
assert_true(allocated.mark)
2011-08-07 22:07:02 +00:00
end)
2011-08-07 22:07:02 +00:00
end)
end)
end
2011-08-07 22:07:02 +00:00
end)
end)