made class non-global

This commit is contained in:
kikito 2013-09-15 23:14:00 +02:00
parent 491eb44628
commit 88e84d13c2
10 changed files with 65 additions and 75 deletions

View File

@ -135,11 +135,6 @@ function Object:initialize() end
function Object:__tostring() return "instance of " .. tostring(self.class) end function Object:__tostring() return "instance of " .. tostring(self.class) end
function class(name, super, ...)
super = super or Object
return super:subclass(name, ...)
end
function instanceOf(aClass, obj) function instanceOf(aClass, obj)
if type(aClass) ~= 'table' or type(obj) ~= 'table' or not obj.class then return false end if type(aClass) ~= 'table' or type(obj) ~= 'table' or not obj.class then return false end
if obj.class == aClass then return true end if obj.class == aClass then return true end
@ -157,4 +152,11 @@ function includes(mixin, aClass)
return includes(mixin, aClass.super) return includes(mixin, aClass.super)
end end
function middleclass.class(name, super, ...)
super = super or Object
return super:subclass(name, ...)
end
setmetatable(middleclass, { __call = function(_, ...) return middleclass.class(...) end })
return middleclass return middleclass

View File

@ -1,4 +1,4 @@
require 'middleclass' local class = require 'middleclass'
context('Object', function() context('Object', function()

View File

@ -1,4 +1,4 @@
require 'middleclass' local class = require 'middleclass'
context('class()', function() context('class()', function()
@ -9,31 +9,19 @@ context('class()', function()
end) end)
context('when given a name', function() context('when given a name', function()
local TheClass test('the resulting class has the correct name and Object as its superclass', function()
local TheClass = class('TheClass')
before(function()
TheClass = class('TheClass')
end)
test('the resulting class has the correct name', function()
assert_equal(TheClass.name, 'TheClass') assert_equal(TheClass.name, 'TheClass')
end)
test('the resulting class has Object as its superclass', function()
assert_equal(TheClass.super, Object) assert_equal(TheClass.super, Object)
end) end)
end) end)
context('when given a name and a superclass', function() context('when given a name and a superclass', function()
local TheSuperClass = class('TheSuperClass') test('the resulting class has the correct name and superclass', function()
local TheSubClass = class('TheSubClass', TheSuperClass) local TheSuperClass = class('TheSuperClass')
local TheSubClass = class('TheSubClass', TheSuperClass)
test('the resulting class has the correct name', function()
assert_equal(TheSubClass.name, 'TheSubClass') assert_equal(TheSubClass.name, 'TheSubClass')
end) assert_equal(TheSubClass.super, TheSuperClass)
test('the resulting class has the correct superclass', function()
assert_equal(TheSubClass.super, TheSuperClass)
end) end)
end) end)

View File

@ -1,4 +1,4 @@
require 'middleclass' local class = require 'middleclass'
context('A Class', function() context('A Class', function()
@ -23,7 +23,7 @@ context('A Class', function()
end) end)
context('()', function() context('()', function()
test('returns an object, like Class:new()', function() test('returns an object, like Class:new()', function()
local obj = AClass() local obj = AClass()
assert_equal(obj.class, AClass) assert_equal(obj.class, AClass)
end) end)
@ -99,7 +99,7 @@ context('A Class', function()
test('are available for subclasses', function() test('are available for subclasses', function()
assert_equal(B.foo, 'foo') assert_equal(B.foo, 'foo')
end) end)
test('are overridable by subclasses, without affecting the superclasses', function() test('are overridable by subclasses, without affecting the superclasses', function()
B.static.foo = 'chunky bacon' B.static.foo = 'chunky bacon'
assert_equal(B.foo, 'chunky bacon') assert_equal(B.foo, 'chunky bacon')
@ -126,7 +126,7 @@ context('A Class', function()
test('are available for subclasses', function() test('are available for subclasses', function()
assert_equal(B:foo(), 'foo') assert_equal(B:foo(), 'foo')
end) end)
test('are overridable by subclasses, without affecting the superclasses', function() test('are overridable by subclasses, without affecting the superclasses', function()
function B.static:foo() return 'chunky bacon' end function B.static:foo() return 'chunky bacon' end
assert_equal(B:foo(), 'chunky bacon') assert_equal(B:foo(), 'chunky bacon')

View File

@ -1,19 +1,19 @@
require 'middleclass' local class = require 'middleclass'
context('includes', function() context('includes', function()
context('nils, numbers, etc', function() context('nils, numbers, etc', function()
local o = Object:new() local o = Object:new()
local primitives = {nil, 1, 'hello', {}, function() end} local primitives = {nil, 1, 'hello', {}, function() end}
for _,primitive in pairs(primitives) do for _,primitive in pairs(primitives) do
local theType = type(primitive) local theType = type(primitive)
context('A ' .. theType, function() context('A ' .. theType, function()
local f1 = function() return includes(Object, primitive) end local f1 = function() return includes(Object, primitive) end
local f2 = function() return includes(primitive, o) end local f2 = function() return includes(primitive, o) end
local f3 = function() return includes(primitive, primitive) end local f3 = function() return includes(primitive, primitive) end
context('don\'t throw errors', function() context('don\'t throw errors', function()
test('includes(Object, '.. theType ..')', function() test('includes(Object, '.. theType ..')', function()
assert_not_error(f1) assert_not_error(f1)
@ -25,7 +25,7 @@ context('includes', function()
assert_not_error(f3) assert_not_error(f3)
end) end)
end) end)
test('make includes return false', function() test('make includes return false', function()
assert_false(f1()) assert_false(f1())
assert_false(f2()) assert_false(f2())
@ -43,19 +43,19 @@ context('includes', function()
local Class2 = class('Class2', Class1) local Class2 = class('Class2', Class1)
local Class3 = class('Class3', Class2) local Class3 = class('Class3', Class2)
local UnrelatedClass = class('Unrelated') local UnrelatedClass = class('Unrelated')
local hasFoo = { foo=function() return 'foo' end } local hasFoo = { foo=function() return 'foo' end }
Class1:include(hasFoo) Class1:include(hasFoo)
test('returns true if it includes a mixin', function() test('returns true if it includes a mixin', function()
assert_true(includes(hasFoo, Class1)) assert_true(includes(hasFoo, Class1))
end) end)
test('returns true if its superclass includes a mixin', function() test('returns true if its superclass includes a mixin', function()
assert_true(includes(hasFoo, Class2)) assert_true(includes(hasFoo, Class2))
assert_true(includes(hasFoo, Class3)) assert_true(includes(hasFoo, Class3))
end) end)
test('returns false otherwise', function() test('returns false otherwise', function()
assert_false(includes(hasFoo, UnrelatedClass)) assert_false(includes(hasFoo, UnrelatedClass))
end) end)

View File

@ -1,19 +1,19 @@
require 'middleclass' local class = require 'middleclass'
context('instanceOf', function() context('instanceOf', function()
context('nils, integers, strings, tables, and functions', function() context('nils, integers, strings, tables, and functions', function()
local o = Object:new() local o = Object:new()
local primitives = {nil, 1, 'hello', {}, function() end} local primitives = {nil, 1, 'hello', {}, function() end}
for _,primitive in pairs(primitives) do for _,primitive in pairs(primitives) do
local theType = type(primitive) local theType = type(primitive)
context('A ' .. theType, function() context('A ' .. theType, function()
local f1 = function() return instanceOf(Object, primitive) end local f1 = function() return instanceOf(Object, primitive) end
local f2 = function() return instanceOf(primitive, o) end local f2 = function() return instanceOf(primitive, o) end
local f3 = function() return instanceOf(primitive, primitive) end local f3 = function() return instanceOf(primitive, primitive) end
context('does not throw errors', function() context('does not throw errors', function()
test('instanceOf(Object, '.. theType ..')', function() test('instanceOf(Object, '.. theType ..')', function()
assert_not_error(f1) assert_not_error(f1)
@ -25,7 +25,7 @@ context('instanceOf', function()
assert_not_error(f3) assert_not_error(f3)
end) end)
end) end)
test('makes instanceOf return false', function() test('makes instanceOf return false', function()
assert_false(f1()) assert_false(f1())
assert_false(f2()) assert_false(f2())
@ -34,7 +34,7 @@ context('instanceOf', function()
end) end)
end end
end) end)
context('An instance', function() context('An instance', function()
@ -42,33 +42,33 @@ context('instanceOf', function()
local Class2 = class('Class2', Class1) local Class2 = class('Class2', Class1)
local Class3 = class('Class3', Class2) local Class3 = class('Class3', Class2)
local UnrelatedClass = class('Unrelated') local UnrelatedClass = class('Unrelated')
local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new() local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
test('is instanceOf(Object)', function() test('is instanceOf(Object)', function()
assert_true(instanceOf(Object, o1)) assert_true(instanceOf(Object, o1))
assert_true(instanceOf(Object, o2)) assert_true(instanceOf(Object, o2))
assert_true(instanceOf(Object, o3)) assert_true(instanceOf(Object, o3))
end) end)
test('is instanceOf its class', function() test('is instanceOf its class', function()
assert_true(instanceOf(Class1, o1)) assert_true(instanceOf(Class1, o1))
assert_true(instanceOf(Class2, o2)) assert_true(instanceOf(Class2, o2))
assert_true(instanceOf(Class3, o3)) assert_true(instanceOf(Class3, o3))
end) end)
test('is instanceOf its class\' superclasses', function() test('is instanceOf its class\' superclasses', function()
assert_true(instanceOf(Class1, o2)) assert_true(instanceOf(Class1, o2))
assert_true(instanceOf(Class1, o3)) assert_true(instanceOf(Class1, o3))
assert_true(instanceOf(Class2, o3)) assert_true(instanceOf(Class2, o3))
end) end)
test('is not instanceOf its class\' subclasses', function() test('is not instanceOf its class\' subclasses', function()
assert_false(instanceOf(Class2, o1)) assert_false(instanceOf(Class2, o1))
assert_false(instanceOf(Class3, o1)) assert_false(instanceOf(Class3, o1))
assert_false(instanceOf(Class3, o2)) assert_false(instanceOf(Class3, o2))
end) end)
test('is not instanceOf an unrelated class', function() test('is not instanceOf an unrelated class', function()
assert_false(instanceOf(UnrelatedClass, o1)) assert_false(instanceOf(UnrelatedClass, o1))
assert_false(instanceOf(UnrelatedClass, o2)) assert_false(instanceOf(UnrelatedClass, o2))

View File

@ -1,4 +1,4 @@
require 'middleclass' local class = require 'middleclass'
context('An instance', function() context('An instance', function()
@ -17,7 +17,7 @@ context('An instance', function()
local bob = Person:new('bob') local bob = Person:new('bob')
assert_equal(bob.name, 'bob') assert_equal(bob.name, 'bob')
end) end)
test('are available in the instance after being initialized by a superclass', function() test('are available in the instance after being initialized by a superclass', function()
local AgedPerson = class('AgedPerson', Person) local AgedPerson = class('AgedPerson', Person)
function AgedPerson:initialize(name, age) function AgedPerson:initialize(name, age)
@ -40,10 +40,10 @@ context('An instance', function()
A = class('A') A = class('A')
function A:overridden() return 'foo' end function A:overridden() return 'foo' end
function A:regular() return 'regular' end function A:regular() return 'regular' end
B = class('B', A) B = class('B', A)
function B:overridden() return 'bar' end function B:overridden() return 'bar' end
a = A:new() a = A:new()
b = B:new() b = B:new()
end) end)
@ -51,11 +51,11 @@ context('An instance', function()
test('are available for any instance', function() test('are available for any instance', function()
assert_equal(a:overridden(), 'foo') assert_equal(a:overridden(), 'foo')
end) end)
test('are inheritable', function() test('are inheritable', function()
assert_equal(b:regular(), 'regular') assert_equal(b:regular(), 'regular')
end) end)
test('are overridable', function() test('are overridable', function()
assert_equal(b:overridden(), 'bar') assert_equal(b:overridden(), 'bar')
end) end)

View File

@ -1,4 +1,4 @@
require 'middleclass' local class = require 'middleclass'
context('Metamethods', function() context('Metamethods', function()
@ -48,11 +48,11 @@ context('Metamethods', function()
assert_equal(values[1], values[2]) assert_equal(values[1], values[2])
end) end)
end end
context('Inherited Metamethods', function() context('Inherited Metamethods', function()
local Vector2= class('Vector2', Vector) local Vector2= class('Vector2', Vector)
function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
local c = Vector2:new(1,2,3) local c = Vector2:new(1,2,3)
local d = Vector2:new(2,4,6) local d = Vector2:new(2,4,6)
for metamethod,values in pairs({ for metamethod,values in pairs({
@ -74,7 +74,7 @@ context('Metamethods', function()
end) end)
end end
end) end)
end) end)
context('Default Metamethods', function() context('Default Metamethods', function()

View File

@ -1,4 +1,4 @@
require 'middleclass' local class = require 'middleclass'
context('A Mixin', function() context('A Mixin', function()
@ -26,12 +26,12 @@ context('A Mixin', function()
test('invokes the "included" method when included', function() test('invokes the "included" method when included', function()
assert_true(Class1.includesMixin1) assert_true(Class1.includesMixin1)
end) end)
test('has all its functions (except "included") copied to its target class', function() test('has all its functions (except "included") copied to its target class', function()
assert_equal(Class1:bar(), 'bar') assert_equal(Class1:bar(), 'bar')
assert_nil(Class1.included) assert_nil(Class1.included)
end) end)
test('makes its functions available to subclasses', function() test('makes its functions available to subclasses', function()
assert_equal(Class2:baz(), 'baz') assert_equal(Class2:baz(), 'baz')
end) end)
@ -39,7 +39,7 @@ context('A Mixin', function()
test('allows overriding of methods in the same class', function() test('allows overriding of methods in the same class', function()
assert_equal(Class2:foo(), 'foo1') assert_equal(Class2:foo(), 'foo1')
end) end)
test('allows overriding of methods on subclasses', function() test('allows overriding of methods on subclasses', function()
assert_equal(Class2:bar2(), 'bar2') assert_equal(Class2:bar2(), 'bar2')
end) end)

View File

@ -1,18 +1,18 @@
require 'middleclass' local class = require 'middleclass'
context('subclassOf', function() context('subclassOf', function()
context('nils, integers, etc', function() context('nils, integers, etc', function()
local primitives = {nil, 1, 'hello', {}, function() end} local primitives = {nil, 1, 'hello', {}, function() end}
for _,primitive in pairs(primitives) do for _,primitive in pairs(primitives) do
local theType = type(primitive) local theType = type(primitive)
context('A ' .. theType, function() context('A ' .. theType, function()
local f1 = function() return subclassOf(Object, primitive) end local f1 = function() return subclassOf(Object, primitive) end
local f2 = function() return subclassOf(primitive, o) end local f2 = function() return subclassOf(primitive, o) end
local f3 = function() return subclassOf(primitive, primitive) end local f3 = function() return subclassOf(primitive, primitive) end
context('does not throw errors', function() context('does not throw errors', function()
test('subclassOf(Object, '.. theType ..')', function() test('subclassOf(Object, '.. theType ..')', function()
assert_not_error(f1) assert_not_error(f1)
@ -24,7 +24,7 @@ context('subclassOf', function()
assert_not_error(f3) assert_not_error(f3)
end) end)
end) end)
test('makes subclassOf return false', function() test('makes subclassOf return false', function()
assert_false(f1()) assert_false(f1())
assert_false(f2()) assert_false(f2())
@ -35,34 +35,34 @@ context('subclassOf', function()
end end
end) end)
context('Any class (except Object)', function() context('Any class (except Object)', function()
local Class1 = class('Class1') local Class1 = class('Class1')
local Class2 = class('Class2', Class1) local Class2 = class('Class2', Class1)
local Class3 = class('Class3', Class2) local Class3 = class('Class3', Class2)
local UnrelatedClass = class('Unrelated') local UnrelatedClass = class('Unrelated')
test('is subclassOf(Object)', function() test('is subclassOf(Object)', function()
assert_true(subclassOf(Object, Class1)) assert_true(subclassOf(Object, Class1))
assert_true(subclassOf(Object, Class2)) assert_true(subclassOf(Object, Class2))
assert_true(subclassOf(Object, Class3)) assert_true(subclassOf(Object, Class3))
end) end)
test('is subclassOf its direct superclass', function() test('is subclassOf its direct superclass', function()
assert_true(subclassOf(Class1, Class2)) assert_true(subclassOf(Class1, Class2))
assert_true(subclassOf(Class2, Class3)) assert_true(subclassOf(Class2, Class3))
end) end)
test('is subclassOf its ancestors', function() test('is subclassOf its ancestors', function()
assert_true(subclassOf(Class1, Class3)) assert_true(subclassOf(Class1, Class3))
end) end)
test('is a subclassOf its class\' subclasses', function() test('is a subclassOf its class\' subclasses', function()
assert_false(subclassOf(Class2, Class1)) assert_false(subclassOf(Class2, Class1))
assert_false(subclassOf(Class3, Class1)) assert_false(subclassOf(Class3, Class1))
assert_false(subclassOf(Class3, Class2)) assert_false(subclassOf(Class3, Class2))
end) end)
test('is not a subclassOf an unrelated class', function() test('is not a subclassOf an unrelated class', function()
assert_false(subclassOf(UnrelatedClass, Class1)) assert_false(subclassOf(UnrelatedClass, Class1))
assert_false(subclassOf(UnrelatedClass, Class2)) assert_false(subclassOf(UnrelatedClass, Class2))