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 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

View File

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

View File

@ -1,4 +1,4 @@
require 'middleclass'
local class = require 'middleclass'
context('class()', function()
@ -9,30 +9,18 @@ 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()
test('the resulting class has the correct name and superclass', function()
local TheSuperClass = class('TheSuperClass')
local TheSubClass = class('TheSubClass', TheSuperClass)
test('the resulting class has the correct name', function()
assert_equal(TheSubClass.name, 'TheSubClass')
end)
test('the resulting class has the correct superclass', function()
assert_equal(TheSubClass.super, TheSuperClass)
end)
end)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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