mirror of
https://github.com/kikito/middleclass.git
synced 2024-12-11 19:44:24 +00:00
Merge pull request #41 from kikito/simplify
simplifies isInstanceOf & isSubclassOf. Fixes #39
This commit is contained in:
commit
0250f97bea
@ -111,12 +111,7 @@ local DefaultMixin = {
|
|||||||
initialize = function(self, ...) end,
|
initialize = function(self, ...) end,
|
||||||
|
|
||||||
isInstanceOf = function(self, aClass)
|
isInstanceOf = function(self, aClass)
|
||||||
return type(self) == 'table' and
|
return type(aClass) == 'table' and (aClass == self.class or self.class:isSubclassOf(aClass))
|
||||||
type(self.class) == 'table' and
|
|
||||||
type(aClass) == 'table' and
|
|
||||||
( aClass == self.class or
|
|
||||||
type(aClass.isSubclassOf) == 'function' and
|
|
||||||
self.class:isSubclassOf(aClass) )
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
static = {
|
static = {
|
||||||
@ -153,11 +148,8 @@ local DefaultMixin = {
|
|||||||
|
|
||||||
isSubclassOf = function(self, other)
|
isSubclassOf = function(self, other)
|
||||||
return type(other) == 'table' and
|
return type(other) == 'table' and
|
||||||
type(self) == 'table' and
|
|
||||||
type(self.super) == 'table' and
|
type(self.super) == 'table' and
|
||||||
( self.super == other or
|
( self.super == other or self.super:isSubclassOf(other) )
|
||||||
type(self.super.isSubclassOf) == 'function' and
|
|
||||||
self.super:isSubclassOf(other) )
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
include = function(self, ...)
|
include = function(self, ...)
|
||||||
|
@ -121,36 +121,16 @@ describe('Default methods', function()
|
|||||||
|
|
||||||
describe('isInstanceOf', function()
|
describe('isInstanceOf', function()
|
||||||
|
|
||||||
describe('nils, integers, strings, tables, and functions', function()
|
describe('primitives', function()
|
||||||
local o = Object:new()
|
local o = Object:new()
|
||||||
local primitives = {nil, 1, 'hello', {}, function() end}
|
local primitives = {nil, 1, 'hello', {}, function() end, Object:new()}
|
||||||
|
|
||||||
for _,primitive in pairs(primitives) do
|
for _,primitive in pairs(primitives) do
|
||||||
local theType = type(primitive)
|
local theType = type(primitive)
|
||||||
describe('A ' .. theType, function()
|
describe('A ' .. theType, function()
|
||||||
|
it('object:isInstanceOf(, '.. theType ..') returns false', function()
|
||||||
local f1 = function() return Object.isInstanceOf(primitive, Object) end
|
assert.is_false(o:isInstanceOf(primitive))
|
||||||
local f2 = function() return Object.isInstanceOf(primitive, o) end
|
|
||||||
local f3 = function() return Object.isInstanceOf(primitive, primitive) end
|
|
||||||
|
|
||||||
describe('does not throw errors', function()
|
|
||||||
it('instanceOf(Object, '.. theType ..')', function()
|
|
||||||
assert.not_error(f1)
|
|
||||||
end)
|
end)
|
||||||
it('instanceOf(' .. theType .. ', Object:new())', function()
|
|
||||||
assert.not_error(f2)
|
|
||||||
end)
|
|
||||||
it('instanceOf(' .. theType .. ',' .. theType ..')', function()
|
|
||||||
assert.not_error(f3)
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('makes instanceOf return false', function()
|
|
||||||
assert.is_false(f1())
|
|
||||||
assert.is_false(f2())
|
|
||||||
assert.is_false(f3())
|
|
||||||
end)
|
|
||||||
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -196,35 +176,17 @@ describe('Default methods', function()
|
|||||||
|
|
||||||
describe('isSubclassOf', function()
|
describe('isSubclassOf', function()
|
||||||
|
|
||||||
describe('nils, integers, etc', function()
|
it('returns false for instances', function()
|
||||||
|
assert.is_false(Object:isSubclassOf(Object:new()))
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('on primitives', function()
|
||||||
local primitives = {nil, 1, 'hello', {}, function() end}
|
local primitives = {nil, 1, 'hello', {}, function() end}
|
||||||
|
|
||||||
for _,primitive in pairs(primitives) do
|
for _,primitive in pairs(primitives) do
|
||||||
local theType = type(primitive)
|
local theType = type(primitive)
|
||||||
describe('A ' .. theType, function()
|
it('returns false for ' .. theType, function()
|
||||||
|
assert.is_false(Object:isSubclassOf(primitive))
|
||||||
local f1 = function() return Object.isSubclassOf(Object, primitive) end
|
|
||||||
local f2 = function() return Object.isSubclassOf(primitive, Object:new()) end
|
|
||||||
local f3 = function() return Object.isSubclassOf(primitive, primitive) end
|
|
||||||
|
|
||||||
describe('does not throw errors', function()
|
|
||||||
it('isSubclassOf(Object, '.. theType ..')', function()
|
|
||||||
assert.not_error(f1)
|
|
||||||
end)
|
|
||||||
it('isSubclassOf(' .. theType .. ', Object:new())', function()
|
|
||||||
assert.not_error(f2)
|
|
||||||
end)
|
|
||||||
it('isSubclassOf(' .. theType .. ',' .. theType ..')', function()
|
|
||||||
assert.not_error(f3)
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('makes isSubclassOf return false', function()
|
|
||||||
assert.is_false(f1())
|
|
||||||
assert.is_false(f2())
|
|
||||||
assert.is_false(f3())
|
|
||||||
end)
|
|
||||||
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user