mirror of
https://github.com/kikito/middleclass.git
synced 2024-11-25 02:44:20 +00:00
made class non-global
This commit is contained in:
parent
491eb44628
commit
88e84d13c2
@ -135,11 +135,6 @@ function Object:initialize() 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)
|
||||
if type(aClass) ~= 'table' or type(obj) ~= 'table' or not obj.class then return false end
|
||||
if obj.class == aClass then return true end
|
||||
@ -157,4 +152,11 @@ function includes(mixin, aClass)
|
||||
return includes(mixin, aClass.super)
|
||||
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
|
||||
|
@ -1,4 +1,4 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('Object', function()
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('class()', function()
|
||||
|
||||
@ -9,31 +9,19 @@ context('class()', function()
|
||||
end)
|
||||
|
||||
context('when given a name', function()
|
||||
local TheClass
|
||||
|
||||
before(function()
|
||||
TheClass = class('TheClass')
|
||||
end)
|
||||
|
||||
test('the resulting class has the correct name', function()
|
||||
test('the resulting class has the correct name and Object as its superclass', function()
|
||||
local TheClass = class('TheClass')
|
||||
assert_equal(TheClass.name, 'TheClass')
|
||||
end)
|
||||
|
||||
test('the resulting class has Object as its superclass', function()
|
||||
assert_equal(TheClass.super, Object)
|
||||
end)
|
||||
end)
|
||||
|
||||
context('when given a name and a superclass', function()
|
||||
local TheSuperClass = class('TheSuperClass')
|
||||
local TheSubClass = class('TheSubClass', TheSuperClass)
|
||||
|
||||
test('the resulting class has the correct name', function()
|
||||
test('the resulting class has the correct name and superclass', function()
|
||||
local TheSuperClass = class('TheSuperClass')
|
||||
local TheSubClass = class('TheSubClass', TheSuperClass)
|
||||
assert_equal(TheSubClass.name, 'TheSubClass')
|
||||
end)
|
||||
|
||||
test('the resulting class has the correct superclass', function()
|
||||
assert_equal(TheSubClass.super, TheSuperClass)
|
||||
assert_equal(TheSubClass.super, TheSuperClass)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('A Class', function()
|
||||
|
||||
@ -23,7 +23,7 @@ context('A Class', function()
|
||||
end)
|
||||
|
||||
context('()', function()
|
||||
test('returns an object, like Class:new()', function()
|
||||
test('returns an object, like Class:new()', function()
|
||||
local obj = AClass()
|
||||
assert_equal(obj.class, AClass)
|
||||
end)
|
||||
@ -99,7 +99,7 @@ context('A Class', function()
|
||||
test('are available for subclasses', function()
|
||||
assert_equal(B.foo, 'foo')
|
||||
end)
|
||||
|
||||
|
||||
test('are overridable by subclasses, without affecting the superclasses', function()
|
||||
B.static.foo = 'chunky bacon'
|
||||
assert_equal(B.foo, 'chunky bacon')
|
||||
@ -126,7 +126,7 @@ context('A Class', function()
|
||||
test('are available for subclasses', function()
|
||||
assert_equal(B:foo(), 'foo')
|
||||
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')
|
||||
|
@ -1,19 +1,19 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('includes', function()
|
||||
|
||||
context('nils, numbers, etc', 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('don\'t throw errors', function()
|
||||
test('includes(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
@ -25,7 +25,7 @@ context('includes', function()
|
||||
assert_not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
||||
test('make includes return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
@ -43,19 +43,19 @@ context('includes', function()
|
||||
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)
|
||||
|
@ -1,19 +1,19 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('instanceOf', function()
|
||||
|
||||
context('nils, integers, strings, tables, and functions', 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 instanceOf(Object, primitive) end
|
||||
local f2 = function() return instanceOf(primitive, o) end
|
||||
local f3 = function() return instanceOf(primitive, primitive) end
|
||||
|
||||
|
||||
context('does not throw errors', function()
|
||||
test('instanceOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
@ -25,7 +25,7 @@ context('instanceOf', function()
|
||||
assert_not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
||||
test('makes instanceOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
@ -34,7 +34,7 @@ context('instanceOf', function()
|
||||
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
end)
|
||||
|
||||
context('An instance', function()
|
||||
@ -42,33 +42,33 @@ context('instanceOf', function()
|
||||
local Class2 = class('Class2', Class1)
|
||||
local Class3 = class('Class3', Class2)
|
||||
local UnrelatedClass = class('Unrelated')
|
||||
|
||||
|
||||
local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
|
||||
|
||||
|
||||
test('is instanceOf(Object)', function()
|
||||
assert_true(instanceOf(Object, o1))
|
||||
assert_true(instanceOf(Object, o2))
|
||||
assert_true(instanceOf(Object, o3))
|
||||
end)
|
||||
|
||||
|
||||
test('is instanceOf its class', function()
|
||||
assert_true(instanceOf(Class1, o1))
|
||||
assert_true(instanceOf(Class2, o2))
|
||||
assert_true(instanceOf(Class3, o3))
|
||||
end)
|
||||
|
||||
|
||||
test('is instanceOf its class\' superclasses', function()
|
||||
assert_true(instanceOf(Class1, o2))
|
||||
assert_true(instanceOf(Class1, o3))
|
||||
assert_true(instanceOf(Class2, o3))
|
||||
end)
|
||||
|
||||
|
||||
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('is not instanceOf an unrelated class', function()
|
||||
assert_false(instanceOf(UnrelatedClass, o1))
|
||||
assert_false(instanceOf(UnrelatedClass, o2))
|
||||
|
@ -1,4 +1,4 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('An instance', function()
|
||||
|
||||
@ -17,7 +17,7 @@ context('An instance', function()
|
||||
local bob = Person:new('bob')
|
||||
assert_equal(bob.name, 'bob')
|
||||
end)
|
||||
|
||||
|
||||
test('are available in the instance after being initialized by a superclass', function()
|
||||
local AgedPerson = class('AgedPerson', Person)
|
||||
function AgedPerson:initialize(name, age)
|
||||
@ -40,10 +40,10 @@ context('An instance', function()
|
||||
A = class('A')
|
||||
function A:overridden() return 'foo' end
|
||||
function A:regular() return 'regular' end
|
||||
|
||||
|
||||
B = class('B', A)
|
||||
function B:overridden() return 'bar' end
|
||||
|
||||
|
||||
a = A:new()
|
||||
b = B:new()
|
||||
end)
|
||||
@ -51,11 +51,11 @@ context('An instance', function()
|
||||
test('are available for any instance', function()
|
||||
assert_equal(a:overridden(), 'foo')
|
||||
end)
|
||||
|
||||
|
||||
test('are inheritable', function()
|
||||
assert_equal(b:regular(), 'regular')
|
||||
end)
|
||||
|
||||
|
||||
test('are overridable', function()
|
||||
assert_equal(b:overridden(), 'bar')
|
||||
end)
|
||||
|
@ -1,4 +1,4 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('Metamethods', function()
|
||||
|
||||
@ -48,11 +48,11 @@ context('Metamethods', 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({
|
||||
@ -74,7 +74,7 @@ context('Metamethods', function()
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
end)
|
||||
|
||||
context('Default Metamethods', function()
|
||||
|
@ -1,4 +1,4 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('A Mixin', function()
|
||||
|
||||
@ -26,12 +26,12 @@ context('A Mixin', function()
|
||||
test('invokes the "included" method when included', function()
|
||||
assert_true(Class1.includesMixin1)
|
||||
end)
|
||||
|
||||
|
||||
test('has all its functions (except "included") copied to its target class', function()
|
||||
assert_equal(Class1:bar(), 'bar')
|
||||
assert_nil(Class1.included)
|
||||
end)
|
||||
|
||||
|
||||
test('makes its functions available to subclasses', function()
|
||||
assert_equal(Class2:baz(), 'baz')
|
||||
end)
|
||||
@ -39,7 +39,7 @@ context('A Mixin', function()
|
||||
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)
|
||||
|
@ -1,18 +1,18 @@
|
||||
require 'middleclass'
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('subclassOf', function()
|
||||
|
||||
context('nils, integers, etc', function()
|
||||
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 subclassOf(Object, primitive) end
|
||||
local f2 = function() return subclassOf(primitive, o) end
|
||||
local f3 = function() return subclassOf(primitive, primitive) end
|
||||
|
||||
|
||||
context('does not throw errors', function()
|
||||
test('subclassOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
@ -24,7 +24,7 @@ context('subclassOf', function()
|
||||
assert_not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
||||
test('makes subclassOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
@ -35,34 +35,34 @@ context('subclassOf', function()
|
||||
end
|
||||
|
||||
end)
|
||||
|
||||
|
||||
context('Any class (except Object)', function()
|
||||
local Class1 = class('Class1')
|
||||
local Class2 = class('Class2', Class1)
|
||||
local Class3 = class('Class3', Class2)
|
||||
local UnrelatedClass = class('Unrelated')
|
||||
|
||||
|
||||
test('is subclassOf(Object)', function()
|
||||
assert_true(subclassOf(Object, Class1))
|
||||
assert_true(subclassOf(Object, Class2))
|
||||
assert_true(subclassOf(Object, Class3))
|
||||
end)
|
||||
|
||||
|
||||
test('is subclassOf its direct superclass', function()
|
||||
assert_true(subclassOf(Class1, Class2))
|
||||
assert_true(subclassOf(Class2, Class3))
|
||||
end)
|
||||
|
||||
|
||||
test('is subclassOf its ancestors', function()
|
||||
assert_true(subclassOf(Class1, Class3))
|
||||
end)
|
||||
|
||||
|
||||
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('is not a subclassOf an unrelated class', function()
|
||||
assert_false(subclassOf(UnrelatedClass, Class1))
|
||||
assert_false(subclassOf(UnrelatedClass, Class2))
|
||||
|
Loading…
Reference in New Issue
Block a user