From ccab332079305972c91932ae3a70e69ea670a72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Wed, 7 Mar 2018 09:27:56 -0800 Subject: [PATCH] isInstanceOf works on primitives. Fixes #55 --- middleclass.lua | 7 ++++++- spec/default_methods_spec.lua | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/middleclass.lua b/middleclass.lua index b118f17..de72d49 100644 --- a/middleclass.lua +++ b/middleclass.lua @@ -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 = { diff --git a/spec/default_methods_spec.lua b/spec/default_methods_spec.lua index d9a27c6..91fd9b8 100644 --- a/spec/default_methods_spec.lua +++ b/spec/default_methods_spec.lua @@ -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)