middleclass/spec/Object_spec.lua

126 lines
2.8 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)
2011-08-13 22:08:33 +00:00
test('throws an error when no name is given', 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
before(function()
SubClass = Object:subclass('SubClass')
end)
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()
2011-08-13 01:38:31 +00:00
assert_equal(SubClass.super, Object)
2011-08-07 22:07:02 +00:00
end)
2011-08-14 16:39:57 +00:00
test('it includes the subclass in the list of subclasses', function()
assert_true(Object.subclasses[SubClass])
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
2011-08-07 22:07:02 +00:00
before(function()
SubClass = class('SubClass')
function SubClass:initialize() self.mark=true end
end)
context('allocate', function()
test('allocates instances properly', function()
local instance = SubClass:allocate()
assert_equal(instance.class, SubClass)
assert_equal(tostring(instance), "instance of " .. tostring(SubClass))
end)
2011-08-07 22:07:02 +00:00
test('throws an error when used without the :', function()
assert_error(Object.allocate)
end)
2011-08-07 22:07:02 +00:00
test('does not call the initializer', function()
local allocated = SubClass: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 = 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()
assert_true(allocated.mark)
2011-08-07 22:07:02 +00:00
end)
2011-08-07 22:07:02 +00:00
end)
context('new', function()
test('initializes instances properly', function()
local instance = SubClass:new()
assert_equal(instance.class, SubClass)
end)
test('throws an error when used without the :', function()
assert_error(SubClass.new)
end)
2011-08-07 22:07:02 +00:00
test('calls the initializer', function()
local initialized = SubClass:new()
assert_true(initialized.mark)
2011-08-07 22:07:02 +00:00
end)
end)
2011-08-07 22:07:02 +00:00
end)
end)