mirror of
https://github.com/kikito/middleclass.git
synced 2024-11-25 02:44:20 +00:00
split tests more. refactorized class creation. included metamethod support
This commit is contained in:
parent
90bef60d86
commit
da557bb207
@ -6,43 +6,67 @@
|
||||
|
||||
local _classes = setmetatable({}, {__mode = "k"})
|
||||
|
||||
local function _initializeClass(klass, super)
|
||||
|
||||
local function _setClassDictionariesMetatables(klass)
|
||||
local dict = klass.__instanceDict
|
||||
local super = klass.superclass
|
||||
|
||||
dict.__index = dict
|
||||
|
||||
if super then
|
||||
setmetatable(dict, { __index = super.__instanceDict })
|
||||
setmetatable(dict, super.__instanceDict)
|
||||
setmetatable(klass.static, { __index = function(_,k) return dict[k] or super[k] end })
|
||||
else
|
||||
setmetatable(klass.static, { __index = function(_,k) return dict[k] end })
|
||||
end
|
||||
end
|
||||
|
||||
local function _setClassMetatable(klass)
|
||||
setmetatable(klass, {
|
||||
__tostring = function() return "class " .. klass.name end,
|
||||
__index = klass.static,
|
||||
__newindex = klass.__instanceDict,
|
||||
__call = function(_, ...) return klass:new(...) end
|
||||
})
|
||||
|
||||
_classes[klass] = true
|
||||
end
|
||||
|
||||
Object = {
|
||||
name = "Object",
|
||||
static = {},
|
||||
__mixins = {},
|
||||
__instanceDict = {},
|
||||
__metamethods = { '__add', '__call', '__concat', '__div', '__le', '__lt',
|
||||
local function _createClass(name, super)
|
||||
local klass = { name = name, superclass = super, static = {}, __mixins = {}, __instanceDict={} }
|
||||
|
||||
_setClassDictionariesMetatables(klass)
|
||||
_setClassMetatable(klass)
|
||||
_classes[klass] = true
|
||||
|
||||
return klass
|
||||
end
|
||||
|
||||
local function _createLookupMetamethod(klass, methodName)
|
||||
return function(...)
|
||||
local method = klass.superclass[methodName]
|
||||
assert( type(method)=='function', tostring(klass) .. " doesn't implement metamethod '" .. methodName .. "'" )
|
||||
return method(...)
|
||||
end
|
||||
end
|
||||
|
||||
local function _setClassMetamethods(klass)
|
||||
for _,m in ipairs(klass.__metamethods) do
|
||||
klass[m]= _createLookupMetamethod(klass, m)
|
||||
end
|
||||
end
|
||||
|
||||
local function _setDefaultInitializeMethod(klass)
|
||||
klass.initialize = function(instance, ...)
|
||||
return klass.superclass.initialize(instance, ...)
|
||||
end
|
||||
end
|
||||
|
||||
Object = _createClass("Object", nil)
|
||||
|
||||
Object.static.__metamethods = { '__add', '__call', '__concat', '__div', '__le', '__lt',
|
||||
'__mod', '__mul', '__pow', '__sub', '__tostring', '__unm' }
|
||||
}
|
||||
|
||||
_initializeClass(Object)
|
||||
|
||||
Object.initialize = function() end
|
||||
|
||||
function Object.static:allocate()
|
||||
assert(_classes[self], "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'")
|
||||
return setmetatable({ class = self }, {__index = self.__instanceDict })
|
||||
return setmetatable({ class = self }, self.__instanceDict)
|
||||
end
|
||||
|
||||
function Object.static:new(...)
|
||||
@ -55,38 +79,22 @@ function Object.static:subclass(name)
|
||||
assert(_classes[self], "Make sure that you are using 'Class:subclass' instead of 'Class.subclass'")
|
||||
assert(type(name) == "string", "You must provide a name(string) for your class")
|
||||
|
||||
local subclass = { name = name, superclass = self, static = {}, __mixins = {}, __instanceDict={} }
|
||||
|
||||
_initializeClass(subclass, self)
|
||||
local subclass = _createClass(name, self)
|
||||
_setClassMetamethods(subclass)
|
||||
_setDefaultInitializeMethod(subclass)
|
||||
|
||||
return subclass
|
||||
end
|
||||
|
||||
function Object:initialize() end
|
||||
|
||||
function Object:__tostring() return "instance of " .. tostring(self.class) end
|
||||
|
||||
--[[
|
||||
|
||||
-- creates a subclass
|
||||
function Object.subclass(klass, name)
|
||||
|
||||
setmetatable(thesubclass, {
|
||||
__index = dict, -- look for stuff on the dict
|
||||
__newindex = function(_, methodName, method) -- ensure that __index isn't modified by mistake
|
||||
assert(methodName ~= '__index', "Can't modify __index. Include middleclass-extras.Indexable and use 'index' instead")
|
||||
rawset(dict, methodName , method)
|
||||
end,
|
||||
__tostring = function() return ("class ".. name) end, -- allows tostring(MyClass)
|
||||
__call = function(_, ...) return thesubclass:new(...) end -- allows MyClass(...) instead of MyClass:new(...)
|
||||
})
|
||||
|
||||
for _,mmName in ipairs(klass.__metamethods) do -- Creates the initial metamethods
|
||||
dict[mmName]= function(...) -- by default, they just 'look up' for an implememtation
|
||||
local method = superDict[mmName] -- and if none found, they throw an error
|
||||
assert( type(method)=='function', tostring(thesubclass) .. " doesn't implement metamethod '" .. mmName .. "'" )
|
||||
return method(...)
|
||||
end
|
||||
end
|
||||
|
||||
thesubclass.initialize = function(instance,...) klass.initialize(instance, ...) end
|
||||
_classes[thesubclass]= true -- registers the new class on the list of _classes
|
||||
klass:subclassed(thesubclass) -- hook method. By default it does nothing
|
||||
|
||||
return thesubclass
|
||||
|
@ -21,64 +21,6 @@ context('Object', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
context('instance creation', function()
|
||||
|
||||
local MyClass
|
||||
|
||||
before(function()
|
||||
MyClass = Object:subclass('MyClass')
|
||||
function MyClass:initialize() self.mark=true end
|
||||
end)
|
||||
|
||||
context('allocate', function()
|
||||
|
||||
test('allocates instances properly', function()
|
||||
local instance = MyClass:allocate()
|
||||
assert_equal(instance.class, MyClass)
|
||||
end)
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(Object.allocate)
|
||||
end)
|
||||
|
||||
test('does not call the initializer', function()
|
||||
local allocated = MyClass:allocate()
|
||||
assert_nil(allocated.mark)
|
||||
end)
|
||||
|
||||
test('can be overriden', function()
|
||||
function MyClass.static:allocate()
|
||||
local instance = Object:allocate()
|
||||
instance.mark = true
|
||||
return instance
|
||||
end
|
||||
|
||||
local allocated = MyClass:allocate()
|
||||
assert_true(allocated.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('new', function()
|
||||
|
||||
test('initializes instances properly', function()
|
||||
local instance = MyClass:new()
|
||||
assert_equal(instance.class, MyClass)
|
||||
end)
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(MyClass.new)
|
||||
end)
|
||||
|
||||
test('calls the initializer', function()
|
||||
local allocated = MyClass:new()
|
||||
assert_true(allocated.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('subclass', function()
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
@ -87,14 +29,14 @@ context('Object', function()
|
||||
|
||||
context('when given a class name', function()
|
||||
|
||||
local MyClass = Object:subclass('MyClass')
|
||||
local SubClass = Object:subclass('SubClass')
|
||||
|
||||
test('it returns a class with the correct name', function()
|
||||
assert_equal(MyClass.name, 'MyClass')
|
||||
assert_equal(SubClass.name, 'SubClass')
|
||||
end)
|
||||
|
||||
test('it returns a class with the correct superclass', function()
|
||||
assert_equal(MyClass.superclass, Object)
|
||||
assert_equal(SubClass.superclass, Object)
|
||||
end)
|
||||
end)
|
||||
|
||||
@ -106,189 +48,76 @@ context('Object', function()
|
||||
|
||||
end)
|
||||
|
||||
context('instance creation', function()
|
||||
|
||||
local SubClass
|
||||
local classes = { Object, SubClass }
|
||||
|
||||
before(function()
|
||||
SubClass = Object:subclass('SubClass')
|
||||
function SubClass:initialize() self.mark=true end
|
||||
end)
|
||||
|
||||
for _,theClass in ipairs(classes) do
|
||||
context(theClass.name, function()
|
||||
|
||||
context('allocate', function()
|
||||
|
||||
|
||||
|
||||
--[[
|
||||
|
||||
context('Metamethods', function()
|
||||
|
||||
test('__index should throw an error', function()
|
||||
local NonIndexable = class('NonIndexable')
|
||||
|
||||
assert_error(function() function NonIndexable:__index(name) end end)
|
||||
test('allocates instances properly', function()
|
||||
local instance = theClass:allocate()
|
||||
assert_equal(instance.class, theClass)
|
||||
assert_equal(tostring(instance), "instance of " .. tostring(theClass))
|
||||
end)
|
||||
|
||||
context('Custom Metamethods', function()
|
||||
-- Tests all metamethods. Note that __len is missing (lua makes table length unoverridable)
|
||||
-- I'll use a() to note the length of vector "a" (I would have preferred to use #a, but it's not possible)
|
||||
-- I'll be using 'a' instead of 'self' on this example since it is shorter
|
||||
local Vector= class('Vector')
|
||||
function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end
|
||||
function Vector.__tostring(a) return a.class.name .. '[' .. a.x .. ',' .. a.y .. ',' .. a.z .. ']' end
|
||||
function Vector.__eq(a,b) return a.x==b.x and a.y==b.y and a.z==b.z end
|
||||
function Vector.__lt(a,b) return a() < b() end
|
||||
function Vector.__le(a,b) return a() <= b() end
|
||||
function Vector.__add(a,b) return Vector:new(a.x+b.x, a.y+b.y ,a.z+b.z) end
|
||||
function Vector.__sub(a,b) return Vector:new(a.x-b.x, a.y-b.y, a.z-b.z) end
|
||||
function Vector.__div(a,s) return Vector:new(a.x/s, a.y/s, a.z/s) end
|
||||
function Vector.__unm(a) return Vector:new(-a.x, -a.y, -a.z) end
|
||||
function Vector.__concat(a,b) return a.x*b.x+a.y*b.y+a.z*b.z end
|
||||
function Vector.__call(a) return math.sqrt(a.x*a.x+a.y*a.y+a.z*a.z) end
|
||||
function Vector.__pow(a,b)
|
||||
return Vector:new(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x)
|
||||
end
|
||||
function Vector.__mul(a,b)
|
||||
if type(b)=="number" then return Vector:new(a.x*b, a.y*b, a.z*b) end
|
||||
if type(a)=="number" then return Vector:new(a*b.x, a*b.y, a*b.z) end
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(Object.allocate)
|
||||
end)
|
||||
|
||||
test('does not call the initializer', function()
|
||||
local allocated = theClass:allocate()
|
||||
assert_nil(allocated.mark)
|
||||
end)
|
||||
|
||||
test('can be overriden', function()
|
||||
|
||||
local previousAllocate = theClass.allocate
|
||||
|
||||
function theClass.static:allocate()
|
||||
local instance = previousAllocate(theClass)
|
||||
instance.mark = true
|
||||
return instance
|
||||
end
|
||||
|
||||
local a = Vector:new(1,2,3)
|
||||
local b = Vector:new(2,4,6)
|
||||
|
||||
for metamethod,values in pairs({
|
||||
__tostring = { tostring(a), "Vector[1,2,3]" },
|
||||
__eq = { a, a},
|
||||
__lt = { a<b, true },
|
||||
__le = { a<=b, true },
|
||||
__add = { a+b, Vector(3,6,9) },
|
||||
__sub = { b-a, Vector(1,2,3) },
|
||||
__div = { b/2, Vector(1,2,3) },
|
||||
__unm = { -a, Vector(-1,-2,-3) },
|
||||
__concat = { a..b, 28 },
|
||||
__call = { a(), math.sqrt(14) },
|
||||
__pow = { a^b, Vector(0,0,0) },
|
||||
__mul = { 4*a, Vector(4,8,12) }--,
|
||||
--__index = { b[1], 3 }
|
||||
}) do
|
||||
test(metamethod .. ' should work', function()
|
||||
assert_equal(values[1], values[2])
|
||||
local allocated = theClass:allocate()
|
||||
assert_true(allocated.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('new', function()
|
||||
|
||||
test('initializes instances properly', function()
|
||||
local instance = theClass:new()
|
||||
assert_equal(instance.class, theClass)
|
||||
end)
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(theClass.new)
|
||||
end)
|
||||
|
||||
test('calls the initializer', function()
|
||||
local allocated = theClass:new()
|
||||
assert_true(allocated.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end
|
||||
|
||||
context('Inherited Metamethods', function()
|
||||
local Vector2= class('Vector2', Vector)
|
||||
function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
|
||||
|
||||
local c = Vector2:new(1,2,3)
|
||||
local d = Vector2:new(2,4,6)
|
||||
for metamethod,values in pairs({
|
||||
__tostring = { tostring(c), "Vector2[1,2,3]" },
|
||||
__eq = { c, c },
|
||||
__lt = { c<d, true },
|
||||
__le = { c<=d, true },
|
||||
__add = { c+d, Vector(3,6,9) },
|
||||
__sub = { d-c, Vector(1,2,3) },
|
||||
__div = { d/2, Vector(1,2,3) },
|
||||
__unm = { -c, Vector(-1,-2,-3) },
|
||||
__concat = { c..d, 28 },
|
||||
__call = { c(), math.sqrt(14) },
|
||||
__pow = { c^d, Vector(0,0,0) },
|
||||
__mul = { 4*c, Vector(4,8,12) }
|
||||
}) do
|
||||
test(metamethod .. ' should work', function()
|
||||
assert_equal(values[1], values[2])
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('Default Metamethods', function()
|
||||
local Peter = class('Peter')
|
||||
local peter = Peter()
|
||||
|
||||
context('A Class', function()
|
||||
test('should have a tostring metamethod', function()
|
||||
assert_equal(tostring(Peter), 'class Peter')
|
||||
end)
|
||||
test('should have a call metamethod', function()
|
||||
assert_true(instanceOf(Peter, peter))
|
||||
end)
|
||||
end)
|
||||
|
||||
context('An instance', function()
|
||||
test('should have a tostring metamethod, different from Object.__tostring', function()
|
||||
assert_not_equal(Peter.__tostring, Object.__tostring)
|
||||
assert_equal(tostring(peter), 'instance of Peter')
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
|
||||
|
||||
|
||||
context('includes', function()
|
||||
|
||||
context('Primitives', function()
|
||||
local o = Object:new()
|
||||
local primitives = {nil, 1, 'hello', {}, function() end}
|
||||
|
||||
for _,primitive in pairs(primitives) do
|
||||
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
|
||||
|
||||
context('should not throw errors', function()
|
||||
test('includes(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
end)
|
||||
test('includes(' .. theType .. ', Object:new())', function()
|
||||
assert_not_error(f2)
|
||||
end)
|
||||
test('includes(' .. theType .. ',' .. theType ..')', function()
|
||||
assert_not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
test('should make includes return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
end)
|
||||
|
||||
end)
|
||||
end -- for
|
||||
|
||||
end)
|
||||
|
||||
context('A class', function()
|
||||
|
||||
local Class1 = class('Class1')
|
||||
local Class2 = class('Class2', Class1)
|
||||
local Class3 = class('Class3', Class2)
|
||||
local UnrelatedClass = class('Unrelated')
|
||||
|
||||
local hasFoo = { foo=function() return 'foo' end }
|
||||
Class1:include(hasFoo)
|
||||
|
||||
test('should return true if it includes a mixin', function()
|
||||
assert_true(includes(hasFoo, Class1))
|
||||
end)
|
||||
|
||||
test('should return true if its superclass includes a mixin', function()
|
||||
assert_true(includes(hasFoo, Class2))
|
||||
assert_true(includes(hasFoo, Class3))
|
||||
end)
|
||||
|
||||
test('should return false otherwise', function()
|
||||
assert_false(includes(hasFoo, UnrelatedClass))
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
require 'middleclass'
|
||||
|
||||
context('Class', function()
|
||||
context('A Class', function()
|
||||
|
||||
context('name', function()
|
||||
test('is correctly set', function()
|
||||
@ -35,16 +35,16 @@ context('Class', function()
|
||||
B = class('B', A)
|
||||
end)
|
||||
|
||||
test('should be available after being initialized', function()
|
||||
test('are available after being initialized', function()
|
||||
assert_equal(A.foo, 'foo')
|
||||
end)
|
||||
|
||||
test('should be available for subclasses', function()
|
||||
test('are available for subclasses', function()
|
||||
assert_equal(B.foo, 'foo')
|
||||
end)
|
||||
|
||||
test('should be overridable by subclasses, without affecting the superclasses', function()
|
||||
B.foo = 'chunky bacon'
|
||||
test('are overridable by subclasses, without affecting the superclasses', function()
|
||||
B.static.foo = 'chunky bacon'
|
||||
assert_equal(B.foo, 'chunky bacon')
|
||||
assert_equal(A.foo, 'foo')
|
||||
end)
|
||||
@ -57,21 +57,21 @@ context('Class', function()
|
||||
|
||||
before(function()
|
||||
A = class('A')
|
||||
function A.foo(theClass) return 'foo' end
|
||||
function A.static:foo() return 'foo' end
|
||||
|
||||
B = class('B', A)
|
||||
end)
|
||||
|
||||
test('should be available after being initialized', function()
|
||||
test('are available after being initialized', function()
|
||||
assert_equal(A:foo(), 'foo')
|
||||
end)
|
||||
|
||||
test('should be available for subclasses', function()
|
||||
test('are available for subclasses', function()
|
||||
assert_equal(B:foo(), 'foo')
|
||||
end)
|
||||
|
||||
test('should be overridable by subclasses, without affecting the superclasses', function()
|
||||
function B.foo(theClass) return 'chunky bacon' end
|
||||
test('are overridable by subclasses, without affecting the superclasses', function()
|
||||
function B.static:foo() return 'chunky bacon' end
|
||||
assert_equal(B:foo(), 'chunky bacon')
|
||||
assert_equal(A:foo(), 'foo')
|
||||
end)
|
||||
|
69
spec/includes_spec.lua
Normal file
69
spec/includes_spec.lua
Normal file
@ -0,0 +1,69 @@
|
||||
--[[
|
||||
|
||||
require 'middleclass'
|
||||
|
||||
context('includes', function()
|
||||
|
||||
context('Primitives', function()
|
||||
local o = Object:new()
|
||||
local primitives = {nil, 1, 'hello', {}, function() end}
|
||||
|
||||
for _,primitive in pairs(primitives) do
|
||||
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
|
||||
|
||||
context('does not throw errors', function()
|
||||
test('includes(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
end)
|
||||
test('includes(' .. theType .. ', Object:new())', function()
|
||||
assert_not_error(f2)
|
||||
end)
|
||||
test('includes(' .. theType .. ',' .. theType ..')', function()
|
||||
assert_not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
test('should make includes return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
end)
|
||||
|
||||
end)
|
||||
end -- for
|
||||
|
||||
end)
|
||||
|
||||
context('A class', function()
|
||||
|
||||
local Class1 = class('Class1')
|
||||
local Class2 = class('Class2', Class1)
|
||||
local Class3 = class('Class3', Class2)
|
||||
local UnrelatedClass = class('Unrelated')
|
||||
|
||||
local hasFoo = { foo=function() return 'foo' end }
|
||||
Class1:include(hasFoo)
|
||||
|
||||
test('returns true if it includes a mixin', function()
|
||||
assert_true(includes(hasFoo, Class1))
|
||||
end)
|
||||
|
||||
test('returns true if its superclass includes a mixin', function()
|
||||
assert_true(includes(hasFoo, Class2))
|
||||
assert_true(includes(hasFoo, Class3))
|
||||
end)
|
||||
|
||||
test('returns false otherwise', function()
|
||||
assert_false(includes(hasFoo, UnrelatedClass))
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
]]
|
@ -1,3 +1,5 @@
|
||||
require 'middleclass'
|
||||
|
||||
context('instanceOf', function()
|
||||
|
||||
context('nils, integers, strings, tables, and functions', function()
|
||||
@ -12,7 +14,7 @@ context('instanceOf', function()
|
||||
local f2 = function() return instanceOf(primitive, o) end
|
||||
local f3 = function() return instanceOf(primitive, primitive) end
|
||||
|
||||
context('should not throw errors', function()
|
||||
context('does not throw errors', function()
|
||||
test('instanceOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
end)
|
||||
@ -24,7 +26,7 @@ context('instanceOf', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
test('should make instanceOf return false', function()
|
||||
test('makes instanceOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
@ -43,31 +45,31 @@ context('instanceOf', function()
|
||||
|
||||
local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
|
||||
|
||||
test('should be instanceOf(Object)', function()
|
||||
test('is instanceOf(Object)', function()
|
||||
assert_true(instanceOf(Object, o1))
|
||||
assert_true(instanceOf(Object, o2))
|
||||
assert_true(instanceOf(Object, o3))
|
||||
end)
|
||||
|
||||
test('should be instanceOf its class', function()
|
||||
test('is instanceOf its class', function()
|
||||
assert_true(instanceOf(Class1, o1))
|
||||
assert_true(instanceOf(Class2, o2))
|
||||
assert_true(instanceOf(Class3, o3))
|
||||
end)
|
||||
|
||||
test('should be instanceOf its class\' superclasses', function()
|
||||
test('is instanceOf its class\' superclasses', function()
|
||||
assert_true(instanceOf(Class1, o2))
|
||||
assert_true(instanceOf(Class1, o3))
|
||||
assert_true(instanceOf(Class2, o3))
|
||||
end)
|
||||
|
||||
test('should not be an instanceOf its class\' subclasses', function()
|
||||
test('is not instanceOf its class\' subclasses', function()
|
||||
assert_false(instanceOf(Class2, o1))
|
||||
assert_false(instanceOf(Class3, o1))
|
||||
assert_false(instanceOf(Class3, o2))
|
||||
end)
|
||||
|
||||
test('should not be an instanceOf an unrelated class', function()
|
||||
test('is not instanceOf an unrelated class', function()
|
||||
assert_false(instanceOf(UnrelatedClass, o1))
|
||||
assert_false(instanceOf(UnrelatedClass, o2))
|
||||
assert_false(instanceOf(UnrelatedClass, o3))
|
||||
|
@ -1,6 +1,6 @@
|
||||
require 'middleclass'
|
||||
|
||||
context('Instance', function()
|
||||
context('An instance', function()
|
||||
|
||||
context('attributes', function()
|
||||
|
||||
@ -48,15 +48,15 @@ context('Instance', function()
|
||||
b = B:new()
|
||||
end)
|
||||
|
||||
test('should be available for any instance', function()
|
||||
test('are available for any instance', function()
|
||||
assert_equal(a:overridden(), 'foo')
|
||||
end)
|
||||
|
||||
test('should be inheritable', function()
|
||||
test('are inheritable', function()
|
||||
assert_equal(b:regular(), 'regular')
|
||||
end)
|
||||
|
||||
test('should be overridable', function()
|
||||
test('are overridable', function()
|
||||
assert_equal(b:overridden(), 'bar')
|
||||
end)
|
||||
|
||||
|
106
spec/metamethods_spec.lua
Normal file
106
spec/metamethods_spec.lua
Normal file
@ -0,0 +1,106 @@
|
||||
require 'middleclass'
|
||||
|
||||
context('Metamethods', function()
|
||||
|
||||
context('Custom Metamethods', function()
|
||||
-- Tests all metamethods. Note that __len is missing (lua makes table length unoverridable)
|
||||
-- I'll use a() to note the length of vector "a" (I would have preferred to use #a, but it's not possible)
|
||||
-- I'll be using 'a' instead of 'self' on this example since it is shorter
|
||||
local Vector= class('Vector')
|
||||
function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end
|
||||
function Vector.__tostring(a) return a.class.name .. '[' .. a.x .. ',' .. a.y .. ',' .. a.z .. ']' end
|
||||
function Vector.__eq(a,b) return a.x==b.x and a.y==b.y and a.z==b.z end
|
||||
function Vector.__lt(a,b) return a() < b() end
|
||||
function Vector.__le(a,b) return a() <= b() end
|
||||
function Vector.__add(a,b) return Vector:new(a.x+b.x, a.y+b.y ,a.z+b.z) end
|
||||
function Vector.__sub(a,b) return Vector:new(a.x-b.x, a.y-b.y, a.z-b.z) end
|
||||
function Vector.__div(a,s) return Vector:new(a.x/s, a.y/s, a.z/s) end
|
||||
function Vector.__unm(a) return Vector:new(-a.x, -a.y, -a.z) end
|
||||
function Vector.__concat(a,b) return a.x*b.x+a.y*b.y+a.z*b.z end
|
||||
function Vector.__call(a) return math.sqrt(a.x*a.x+a.y*a.y+a.z*a.z) end
|
||||
function Vector.__pow(a,b)
|
||||
return Vector:new(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x)
|
||||
end
|
||||
function Vector.__mul(a,b)
|
||||
if type(b)=="number" then return Vector:new(a.x*b, a.y*b, a.z*b) end
|
||||
if type(a)=="number" then return Vector:new(a*b.x, a*b.y, a*b.z) end
|
||||
end
|
||||
|
||||
local a = Vector:new(1,2,3)
|
||||
local b = Vector:new(2,4,6)
|
||||
|
||||
for metamethod,values in pairs({
|
||||
__tostring = { tostring(a), "Vector[1,2,3]" },
|
||||
__eq = { a, a},
|
||||
__lt = { a<b, true },
|
||||
__le = { a<=b, true },
|
||||
__add = { a+b, Vector(3,6,9) },
|
||||
__sub = { b-a, Vector(1,2,3) },
|
||||
__div = { b/2, Vector(1,2,3) },
|
||||
__unm = { -a, Vector(-1,-2,-3) },
|
||||
__concat = { a..b, 28 },
|
||||
__call = { a(), math.sqrt(14) },
|
||||
__pow = { a^b, Vector(0,0,0) },
|
||||
__mul = { 4*a, Vector(4,8,12) }--,
|
||||
--__index = { b[1], 3 }
|
||||
}) do
|
||||
test(metamethod .. ' works as expected', function()
|
||||
assert_equal(values[1], values[2])
|
||||
end)
|
||||
end
|
||||
|
||||
context('Inherited Metamethods', function()
|
||||
local Vector2= class('Vector2', Vector)
|
||||
function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
|
||||
|
||||
local c = Vector2:new(1,2,3)
|
||||
local d = Vector2:new(2,4,6)
|
||||
for metamethod,values in pairs({
|
||||
__tostring = { tostring(c), "Vector2[1,2,3]" },
|
||||
__eq = { c, c },
|
||||
__lt = { c<d, true },
|
||||
__le = { c<=d, true },
|
||||
__add = { c+d, Vector(3,6,9) },
|
||||
__sub = { d-c, Vector(1,2,3) },
|
||||
__div = { d/2, Vector(1,2,3) },
|
||||
__unm = { -c, Vector(-1,-2,-3) },
|
||||
__concat = { c..d, 28 },
|
||||
__call = { c(), math.sqrt(14) },
|
||||
__pow = { c^d, Vector(0,0,0) },
|
||||
__mul = { 4*c, Vector(4,8,12) }
|
||||
}) do
|
||||
test(metamethod .. ' works as expected', function()
|
||||
assert_equal(values[1], values[2])
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('Default Metamethods', function()
|
||||
|
||||
local Peter, peter
|
||||
|
||||
before(function()
|
||||
Peter = class('Peter')
|
||||
peter = Peter()
|
||||
end)
|
||||
|
||||
context('A Class', function()
|
||||
test('has a call metamethod properly set', function()
|
||||
assert_true(instanceOf(Peter, peter))
|
||||
end)
|
||||
test('has a tostring metamethod properly set', function()
|
||||
assert_equal(tostring(Peter), 'class Peter')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('An instance', function()
|
||||
test('has a tostring metamethod, returning a different result from Object.__tostring', function()
|
||||
assert_not_equal(Peter.__tostring, Object.__tostring)
|
||||
assert_equal(tostring(peter), 'instance of class Peter')
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
end)
|
@ -1,31 +1,39 @@
|
||||
--[[
|
||||
require 'middleclass'
|
||||
|
||||
context('A Mixin', function()
|
||||
|
||||
local Class1 = class('Class1')
|
||||
local Mixin = {}
|
||||
function Mixin:included(theClass) theClass.includesMixin = true end
|
||||
function Mixin:foo() return 'foo' end
|
||||
function Mixin:bar() return 'bar' end
|
||||
Class1:include(Mixin)
|
||||
function Mixin:baz() return 'baz' end
|
||||
|
||||
Class2 = class('Class2', Class1)
|
||||
function Class2:foo() return 'baz' end
|
||||
local Class1 = class('Class1'):include(Mixin)
|
||||
function Class1:foo() return 'foo1' end
|
||||
|
||||
test('should invoke the "included" method when included', function()
|
||||
local Class2 = class('Class2', Class1)
|
||||
function Class2:bar() return 'bar2' end
|
||||
|
||||
test('invokes the "included" method when included', function()
|
||||
assert_true(Class1.includesMixin)
|
||||
end)
|
||||
|
||||
test('should have all its functions (except "included") copied to its target class', function()
|
||||
assert_equal(Class1:foo(), 'foo')
|
||||
test('has all its functions (except "included") copied to its target class', function()
|
||||
assert_equal(Class1:baz(), 'baz')
|
||||
assert_equal(Class1.included, nil)
|
||||
end)
|
||||
|
||||
test('should make its functions available to subclasses', function()
|
||||
assert_equal(Class2:bar(), 'bar')
|
||||
test('makes its functions available to subclasses', function()
|
||||
assert_equal(Class2:baz(), 'baz')
|
||||
end)
|
||||
|
||||
test('should allow overriding of methods on subclasses', function()
|
||||
assert_equal(Class2:foo(), 'baz')
|
||||
test('allows overriding of methods in the same class', function()
|
||||
assert_equal(Class2:foo(), 'foo1')
|
||||
end)
|
||||
|
||||
test('allows overriding of methods on subclasses', function()
|
||||
assert_equal(Class2:bar2(), 'bar2')
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -11,7 +11,7 @@ context('subclassOf', function()
|
||||
local f2 = function() return subclassOf(primitive, o) end
|
||||
local f3 = function() return subclassOf(primitive, primitive) end
|
||||
|
||||
context('should not throw errors', function()
|
||||
context('does not throw errors', function()
|
||||
test('subclassOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
end)
|
||||
@ -23,7 +23,7 @@ context('subclassOf', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
test('should make subclassOf return false', function()
|
||||
test('makes subclassOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
@ -40,28 +40,28 @@ context('subclassOf', function()
|
||||
local Class3 = class('Class3', Class2)
|
||||
local UnrelatedClass = class('Unrelated')
|
||||
|
||||
test('should be subclassOf(Object)', function()
|
||||
test('is subclassOf(Object)', function()
|
||||
assert_true(subclassOf(Object, Class1))
|
||||
assert_true(subclassOf(Object, Class2))
|
||||
assert_true(subclassOf(Object, Class3))
|
||||
end)
|
||||
|
||||
test('should be subclassOf its direct superclass', function()
|
||||
test('is subclassOf its direct superclass', function()
|
||||
assert_true(subclassOf(Class1, Class2))
|
||||
assert_true(subclassOf(Class2, Class3))
|
||||
end)
|
||||
|
||||
test('should be subclassOf its ancestors', function()
|
||||
test('is subclassOf its ancestors', function()
|
||||
assert_true(subclassOf(Class1, Class3))
|
||||
end)
|
||||
|
||||
test('should not be an subclassOf its class\' subclasses', function()
|
||||
test('is a subclassOf its class\' subclasses', function()
|
||||
assert_false(subclassOf(Class2, Class1))
|
||||
assert_false(subclassOf(Class3, Class1))
|
||||
assert_false(subclassOf(Class3, Class2))
|
||||
end)
|
||||
|
||||
test('should not be an subclassOf an unrelated class', function()
|
||||
test('is not a subclassOf an unrelated class', function()
|
||||
assert_false(subclassOf(UnrelatedClass, Class1))
|
||||
assert_false(subclassOf(UnrelatedClass, Class2))
|
||||
assert_false(subclassOf(UnrelatedClass, Class3))
|
||||
|
Loading…
Reference in New Issue
Block a user