mirror of
https://github.com/kikito/middleclass.git
synced 2024-11-25 02:44:20 +00:00
fixed error in Object_spec that made other tests fail in some cases. Added some minor optimizations & fixes
This commit is contained in:
parent
da557bb207
commit
af03b4f6d8
@ -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>
|
||||
|
||||
|
@ -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
|
||||
|
@ -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,70 +56,64 @@ 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()
|
||||
|
||||
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)
|
||||
|
||||
test('allocates instances properly', function()
|
||||
local instance = theClass:allocate()
|
||||
assert_equal(instance.class, theClass)
|
||||
assert_equal(tostring(instance), "instance of " .. tostring(theClass))
|
||||
end)
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(Object.allocate)
|
||||
end)
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(Object.allocate)
|
||||
end)
|
||||
test('does not call the initializer', function()
|
||||
local allocated = SubClass:allocate()
|
||||
assert_nil(allocated.mark)
|
||||
end)
|
||||
|
||||
test('does not call the initializer', function()
|
||||
local allocated = theClass:allocate()
|
||||
assert_nil(allocated.mark)
|
||||
end)
|
||||
test('can be overriden', function()
|
||||
|
||||
test('can be overriden', function()
|
||||
local previousAllocate = SubClass.static.allocate
|
||||
|
||||
local previousAllocate = theClass.allocate
|
||||
function SubClass.static:allocate()
|
||||
local instance = previousAllocate(SubClass)
|
||||
instance.mark = true
|
||||
return instance
|
||||
end
|
||||
|
||||
function theClass.static:allocate()
|
||||
local instance = previousAllocate(theClass)
|
||||
instance.mark = true
|
||||
return instance
|
||||
end
|
||||
|
||||
local allocated = theClass:allocate()
|
||||
assert_true(allocated.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('new', function()
|
||||
|
||||
test('initializes instances properly', function()
|
||||
local instance = theClass:new()
|
||||
assert_equal(instance.class, theClass)
|
||||
end)
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(theClass.new)
|
||||
end)
|
||||
|
||||
test('calls the initializer', function()
|
||||
local allocated = theClass:new()
|
||||
assert_true(allocated.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
local allocated = SubClass:allocate()
|
||||
assert_true(allocated.mark)
|
||||
|
||||
end)
|
||||
|
||||
end
|
||||
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)
|
||||
|
||||
test('calls the initializer', function()
|
||||
local initialized = SubClass:new()
|
||||
assert_true(initialized.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
|
||||
end)
|
||||
|
||||
|
@ -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')
|
||||
|
@ -2,26 +2,33 @@ require 'middleclass'
|
||||
|
||||
context('A Class', function()
|
||||
|
||||
context('name', function()
|
||||
test('is correctly set', function()
|
||||
local TheClass = class('TheClass')
|
||||
assert_equal(TheClass.name, 'TheClass')
|
||||
end)
|
||||
end)
|
||||
context('Default stuff', function()
|
||||
|
||||
context('tostring', function()
|
||||
test('returns "class *name*"', function()
|
||||
local TheClass = class('TheClass')
|
||||
assert_equal(tostring(TheClass), 'class TheClass')
|
||||
end)
|
||||
end)
|
||||
local AClass
|
||||
|
||||
context('()', function()
|
||||
test('returns an object, like Class:new()', function()
|
||||
local TheClass = class('TheClass')
|
||||
local obj = TheClass()
|
||||
assert_true(instanceOf(TheClass, obj))
|
||||
before(function()
|
||||
AClass = class('AClass')
|
||||
end)
|
||||
|
||||
context('name', function()
|
||||
test('is correctly set', function()
|
||||
assert_equal(AClass.name, 'AClass')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('tostring', function()
|
||||
test('returns "class *name*"', function()
|
||||
assert_equal(tostring(AClass), 'class AClass')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('()', function()
|
||||
test('returns an object, like Class:new()', function()
|
||||
local obj = AClass()
|
||||
assert_equal(obj.class, AClass)
|
||||
end)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('attributes', function()
|
||||
|
@ -1,3 +1,5 @@
|
||||
require 'middleclass'
|
||||
|
||||
context('subclassOf', function()
|
||||
|
||||
context('nils, integers, etc', function()
|
||||
|
Loading…
Reference in New Issue
Block a user