made includes a private method

This commit is contained in:
kikito 2013-09-17 01:14:57 +02:00
parent 3ac8aa167f
commit f5aac38418
2 changed files with 20 additions and 12 deletions

View File

@ -141,6 +141,17 @@ function Object.static:include( ... )
return self
end
function Object.static:includes(mixin)
return type(mixin) == 'table' and
type(self) == 'table' and
type(self.__mixins) == 'table' and
( self.__mixins[mixin] or
type(self.super) == 'table' and
type(self.super.includes) == 'function' and
self.super:includes(mixin)
)
end
function Object:initialize() end
function Object:__tostring() return "instance of " .. tostring(self.class) end
@ -155,11 +166,7 @@ function Object:isInstanceOf(aClass)
)
end
function includes(mixin, aClass)
if type(mixin) ~= 'table' or type(aClass) ~= 'table' or not aClass.__mixins then return false end
if aClass.__mixins[mixin] then return true end
return includes(mixin, aClass.super)
end
function middleclass.class(name, super, ...)
super = super or Object

View File

@ -11,9 +11,10 @@ context('includes', function()
local theType = type(primitive)
context('A ' .. theType, function()
local f1 = function() return includes(Object, primitive) end
local f2 = function() return includes(primitive, o) end
local f3 = function() return includes(primitive, primitive) end
local f1 = function() return Object.includes(Object, primitive) end
local f2 = function() return Object.includes(primitive, o) end
local f3 = function() return Object.includes(primitive, primitive) end
context('don\'t throw errors', function()
test('includes(Object, '.. theType ..')', function()
@ -49,16 +50,16 @@ context('includes', function()
Class1:include(hasFoo)
test('returns true if it includes a mixin', function()
assert_true(includes(hasFoo, Class1))
assert_true(Class1:includes(hasFoo))
end)
test('returns true if its superclass includes a mixin', function()
assert_true(includes(hasFoo, Class2))
assert_true(includes(hasFoo, Class3))
assert_true(Class2:includes(hasFoo))
assert_true(Class3:includes(hasFoo))
end)
test('returns false otherwise', function()
assert_false(includes(hasFoo, UnrelatedClass))
assert_false(UnrelatedClass:includes(hasFoo))
end)
end)