fixed error in Object_spec that made other tests fail in some cases. Added some minor optimizations & fixes

This commit is contained in:
Enrique García Cota 2011-08-13 03:12:21 +02:00
parent da557bb207
commit af03b4f6d8
6 changed files with 91 additions and 74 deletions

View File

@ -86,5 +86,9 @@ If you are looking for @MindState@ (now called @Stateful@), it's over there, too
h1. Specs
You may find the specs for this library in "middleclass-specs":https://github.com/kikito/middleclass-specs
This project uses "telescope":https://github.com/norman/telescope for its specs. If you want to run the specs, you will have to install telescope first. Then just execute the following from the root inspect folder:
<pre>
tsc -f spec/*
</pre>

View File

@ -13,8 +13,9 @@ local function _setClassDictionariesMetatables(klass)
dict.__index = dict
if super then
local superStatic = super.static
setmetatable(dict, super.__instanceDict)
setmetatable(klass.static, { __index = function(_,k) return dict[k] or super[k] end })
setmetatable(klass.static, { __index = function(_,k) return dict[k] or superStatic[k] end })
else
setmetatable(klass.static, { __index = function(_,k) return dict[k] end })
end
@ -25,7 +26,7 @@ local function _setClassMetatable(klass)
__tostring = function() return "class " .. klass.name end,
__index = klass.static,
__newindex = klass.__instanceDict,
__call = function(_, ...) return klass:new(...) end
__call = function(self, ...) return self:new(...) end
})
end
@ -53,9 +54,9 @@ local function _setClassMetamethods(klass)
end
end
local function _setDefaultInitializeMethod(klass)
local function _setDefaultInitializeMethod(klass, super)
klass.initialize = function(instance, ...)
return klass.superclass.initialize(instance, ...)
return super.initialize(instance, ...)
end
end
@ -81,7 +82,7 @@ function Object.static:subclass(name)
local subclass = _createClass(name, self)
_setClassMetamethods(subclass)
_setDefaultInitializeMethod(subclass)
_setDefaultInitializeMethod(subclass, self)
return subclass
end

View File

@ -2,6 +2,7 @@ require 'middleclass'
context('Object', function()
context('name', function()
test('is correctly set', function()
assert_equal(Object.name, 'Object')
@ -29,7 +30,11 @@ context('Object', function()
context('when given a class name', function()
local SubClass = Object:subclass('SubClass')
local SubClass
before(function()
SubClass = Object:subclass('SubClass')
end)
test('it returns a class with the correct name', function()
assert_equal(SubClass.name, 'SubClass')
@ -51,22 +56,18 @@ context('Object', function()
context('instance creation', function()
local SubClass
local classes = { Object, SubClass }
before(function()
SubClass = Object:subclass('SubClass')
SubClass = class('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))
local instance = SubClass:allocate()
assert_equal(instance.class, SubClass)
assert_equal(tostring(instance), "instance of " .. tostring(SubClass))
end)
test('throws an error when used without the :', function()
@ -74,22 +75,23 @@ context('Object', function()
end)
test('does not call the initializer', function()
local allocated = theClass:allocate()
local allocated = SubClass:allocate()
assert_nil(allocated.mark)
end)
test('can be overriden', function()
local previousAllocate = theClass.allocate
local previousAllocate = SubClass.static.allocate
function theClass.static:allocate()
local instance = previousAllocate(theClass)
function SubClass.static:allocate()
local instance = previousAllocate(SubClass)
instance.mark = true
return instance
end
local allocated = theClass:allocate()
local allocated = SubClass:allocate()
assert_true(allocated.mark)
end)
end)
@ -97,24 +99,21 @@ context('Object', function()
context('new', function()
test('initializes instances properly', function()
local instance = theClass:new()
assert_equal(instance.class, theClass)
local instance = SubClass:new()
assert_equal(instance.class, SubClass)
end)
test('throws an error when used without the :', function()
assert_error(theClass.new)
assert_error(SubClass.new)
end)
test('calls the initializer', function()
local allocated = theClass:new()
assert_true(allocated.mark)
local initialized = SubClass:new()
assert_true(initialized.mark)
end)
end)
end)
end
end)

View File

@ -9,7 +9,11 @@ context('class()', function()
end)
context('when given a name', function()
local TheClass = class('TheClass')
local TheClass
before(function()
TheClass = class('TheClass')
end)
test('the resulting class has the correct name', function()
assert_equal(TheClass.name, 'TheClass')

View File

@ -2,28 +2,35 @@ require 'middleclass'
context('A Class', function()
context('Default stuff', function()
local AClass
before(function()
AClass = class('AClass')
end)
context('name', function()
test('is correctly set', function()
local TheClass = class('TheClass')
assert_equal(TheClass.name, 'TheClass')
assert_equal(AClass.name, 'AClass')
end)
end)
context('tostring', function()
test('returns "class *name*"', function()
local TheClass = class('TheClass')
assert_equal(tostring(TheClass), 'class TheClass')
assert_equal(tostring(AClass), 'class AClass')
end)
end)
context('()', function()
test('returns an object, like Class:new()', function()
local TheClass = class('TheClass')
local obj = TheClass()
assert_true(instanceOf(TheClass, obj))
local obj = AClass()
assert_equal(obj.class, AClass)
end)
end)
end)
context('attributes', function()
local A, B

View File

@ -1,3 +1,5 @@
require 'middleclass'
context('subclassOf', function()
context('nils, integers, etc', function()