From 2bcef2b9ff8bdb28b36a1394698a1d691e8205c4 Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 2 Jul 2016 06:00:03 +0200 Subject: [PATCH] simplifies isInstanceOf & isSubclassOf. Fixes #39 --- middleclass.lua | 12 ++----- spec/default_methods_spec.lua | 60 +++++++---------------------------- 2 files changed, 13 insertions(+), 59 deletions(-) diff --git a/middleclass.lua b/middleclass.lua index 90fda19..3c15537 100644 --- a/middleclass.lua +++ b/middleclass.lua @@ -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 = { @@ -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, ...) diff --git a/spec/default_methods_spec.lua b/spec/default_methods_spec.lua index 81602f0..d9a27c6 100644 --- a/spec/default_methods_spec.lua +++ b/spec/default_methods_spec.lua @@ -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) + it('object:isInstanceOf(, '.. theType ..') returns false', function() + assert.is_false(o:isInstanceOf(primitive)) end) - - it('makes instanceOf return false', function() - assert.is_false(f1()) - assert.is_false(f2()) - assert.is_false(f3()) - end) - end) end @@ -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