2011-08-07 22:07:02 +00:00
|
|
|
require 'middleclass'
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
context('Object', function()
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
|
2011-08-08 21:18:15 +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()
|
2011-08-10 19:45:15 +00:00
|
|
|
test('returns an object, like Object:new()', function()
|
|
|
|
local obj = Object()
|
2011-08-11 09:36:23 +00:00
|
|
|
assert_true(instanceOf(Object, obj))
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
context('subclass', function()
|
2011-08-08 05:38:11 +00:00
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
test('throws an error when used without the :', function()
|
2011-08-08 05:38:11 +00:00
|
|
|
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)
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
context('when given a class name', function()
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
local SubClass
|
|
|
|
|
|
|
|
before(function()
|
|
|
|
SubClass = Object:subclass('SubClass')
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-08 05:38:11 +00:00
|
|
|
test('it returns a class with the correct name', function()
|
2011-08-12 08:33:58 +00:00
|
|
|
assert_equal(SubClass.name, 'SubClass')
|
2011-08-07 22:07:02 +00:00
|
|
|
end)
|
|
|
|
|
2011-08-08 05:38:11 +00:00
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
context('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
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
before(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
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
context('allocate', function()
|
2011-08-08 22:06:05 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
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
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
test('throws an error when used without the :', function()
|
|
|
|
assert_error(Object.allocate)
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +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
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
test('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()
|
|
|
|
assert_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
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
context('new', function()
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
test('initializes instances properly', function()
|
|
|
|
local instance = SubClass:new()
|
|
|
|
assert_equal(instance.class, SubClass)
|
|
|
|
end)
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
test('throws an error when used without the :', function()
|
|
|
|
assert_error(SubClass.new)
|
|
|
|
end)
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-13 01:12:21 +00:00
|
|
|
test('calls the initializer', function()
|
|
|
|
local initialized = SubClass:new()
|
|
|
|
assert_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)
|
|
|
|
|
|
|
|
|