Skip to content

Commit

Permalink
simplifies isInstanceOf & isSubclassOf. Fixes kikito#39
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Jul 2, 2016
1 parent bffb00b commit 2bcef2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 59 deletions.
12 changes: 2 additions & 10 deletions middleclass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ local DefaultMixin = {
initialize = function(self, ...) end,

isInstanceOf = function(self, aClass)
return type(self) == 'table' and
type(self.class) == 'table' and
type(aClass) == 'table' and
( aClass == self.class or
type(aClass.isSubclassOf) == 'function' and
self.class:isSubclassOf(aClass) )
return type(aClass) == 'table' and (aClass == self.class or self.class:isSubclassOf(aClass))
end,

static = {
Expand Down Expand Up @@ -153,11 +148,8 @@ local DefaultMixin = {

isSubclassOf = function(self, other)
return type(other) == 'table' and
type(self) == 'table' and
type(self.super) == 'table' and
( self.super == other or
type(self.super.isSubclassOf) == 'function' and
self.super:isSubclassOf(other) )
( self.super == other or self.super:isSubclassOf(other) )
end,

include = function(self, ...)
Expand Down
60 changes: 11 additions & 49 deletions spec/default_methods_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,16 @@ describe('Default methods', function()

describe('isInstanceOf', function()

describe('nils, integers, strings, tables, and functions', function()
describe('primitives', function()
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
local theType = type(primitive)
describe('A ' .. theType, function()

local f1 = function() return Object.isInstanceOf(primitive, Object) end
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)
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())
it('object:isInstanceOf(, '.. theType ..') returns false', function()
assert.is_false(o:isInstanceOf(primitive))
end)

end)
end

Expand Down Expand Up @@ -196,35 +176,17 @@ describe('Default methods', 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}

for _,primitive in pairs(primitives) do
local theType = type(primitive)
describe('A ' .. theType, function()

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)

it('returns false for ' .. theType, function()
assert.is_false(Object:isSubclassOf(primitive))
end)
end

Expand Down

0 comments on commit 2bcef2b

Please sign in to comment.