Skip to content

Commit

Permalink
isInstanceOf works on primitives.
Browse files Browse the repository at this point in the history
Fixes #55
  • Loading branch information
kikito committed Mar 7, 2018
1 parent f9955f9 commit ccab332
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 6 additions & 1 deletion middleclass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ local DefaultMixin = {
initialize = function(self, ...) end,

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

static = {
Expand Down
22 changes: 16 additions & 6 deletions spec/default_methods_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,24 @@ describe('Default methods', function()
local o = Object:new()
local primitives = {nil, 1, 'hello', {}, function() end, Object:new()}

for _,primitive in pairs(primitives) do
local theType = type(primitive)
describe('A ' .. theType, function()
describe('used as classes', function()
for _,primitive in pairs(primitives) do
local theType = type(primitive)
it('object:isInstanceOf(, '.. theType ..') returns false', function()
assert.is_false(o:isInstanceOf(primitive))
assert.is_falsy(o:isInstanceOf(primitive))
end)
end)
end
end
end)

describe('used as instances', function()
for _,primitive in pairs(primitives) do
local theType = type(primitive)
it('Object.isInstanceOf('.. theType ..', Object) returns false without error', function()
assert.is_falsy(Object.isInstanceOf(primitive, Object))
end)
end
end)


end)

Expand Down

0 comments on commit ccab332

Please sign in to comment.