isInstanceOf works on primitives.

Fixes #55
This commit is contained in:
Enrique García Cota 2018-03-07 09:27:56 -08:00
parent f9955f9285
commit ccab332079
2 changed files with 22 additions and 7 deletions

View File

@ -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 = {

View File

@ -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)