middleclass/spec/classes_spec.lua

139 lines
3.2 KiB
Lua
Raw Normal View History

2013-09-15 21:14:00 +00:00
local class = require 'middleclass'
2013-09-17 18:55:50 +00:00
describe('A Class', function()
2013-09-17 18:55:50 +00:00
describe('Default stuff', function()
local AClass
2013-09-17 18:55:50 +00:00
before_each(function()
AClass = class('AClass')
end)
2013-09-17 18:55:50 +00:00
describe('name', function()
it('is correctly set', function()
assert.equal(AClass.name, 'AClass')
end)
end)
2013-09-17 18:55:50 +00:00
describe('tostring', function()
it('returns "class *name*"', function()
assert.equal(tostring(AClass), 'class AClass')
end)
end)
2013-09-17 18:55:50 +00:00
describe('()', function()
it('returns an object, like Class:new()', function()
local obj = AClass()
2013-09-17 18:55:50 +00:00
assert.equal(obj.class, AClass)
end)
2011-08-11 09:36:23 +00:00
end)
2013-09-17 18:55:50 +00:00
describe('include', function()
it('throws an error when used without the :', function()
assert.error(function() AClass.include() end)
2011-08-13 21:58:55 +00:00
end)
2013-09-17 18:55:50 +00:00
it('throws an error when passed a non-table:', function()
assert.error(function() AClass:include(1) end)
2011-08-13 21:58:55 +00:00
end)
end)
2013-09-17 18:55:50 +00:00
describe('subclass', function()
2011-08-13 22:08:33 +00:00
2013-09-17 18:55:50 +00:00
it('throws an error when used without the :', function()
assert.error(function() AClass.subclass() end)
2011-08-13 22:08:33 +00:00
end)
2013-09-17 18:55:50 +00:00
it('throws an error when no name is given', function()
assert.error( function() AClass:subclass() end)
2011-08-13 22:08:33 +00:00
end)
2013-09-17 18:55:50 +00:00
describe('when given a subclass name', function()
2011-08-13 22:08:33 +00:00
local SubClass
2013-09-17 18:55:50 +00:00
before_each(function()
2011-08-13 22:08:33 +00:00
function AClass.static:subclassed(other) self.static.child = other end
SubClass = AClass:subclass('SubClass')
end)
2013-09-17 18:55:50 +00:00
it('it returns a class with the correct name', function()
assert.equal(SubClass.name, 'SubClass')
2011-08-13 22:08:33 +00:00
end)
2013-09-17 18:55:50 +00:00
it('it returns a class with the correct superclass', function()
assert.equal(SubClass.super, AClass)
2011-08-13 22:08:33 +00:00
end)
2013-09-17 18:55:50 +00:00
it('it invokes the subclassed hook method', function()
assert.equal(SubClass, AClass.child)
2011-08-13 22:08:33 +00:00
end)
2013-09-17 18:55:50 +00:00
it('it includes the subclass in the list of subclasses', function()
assert.is_true(AClass.subclasses[SubClass])
2011-08-14 16:39:57 +00:00
end)
2011-08-13 22:08:33 +00:00
end)
end)
2011-08-11 09:36:23 +00:00
end)
2011-08-13 22:08:33 +00:00
2013-09-17 18:55:50 +00:00
describe('attributes', function()
local A, B
2013-09-17 18:55:50 +00:00
before_each(function()
A = class('A')
A.static.foo = 'foo'
B = class('B', A)
end)
2013-09-17 18:55:50 +00:00
it('are available after being initialized', function()
assert.equal(A.foo, 'foo')
end)
2013-09-17 18:55:50 +00:00
it('are available for subclasses', function()
assert.equal(B.foo, 'foo')
end)
2013-09-15 21:14:00 +00:00
2013-09-17 18:55:50 +00:00
it('are overridable by subclasses, without affecting the superclasses', function()
B.static.foo = 'chunky bacon'
2013-09-17 18:55:50 +00:00
assert.equal(B.foo, 'chunky bacon')
assert.equal(A.foo, 'foo')
end)
end)
2013-09-17 18:55:50 +00:00
describe('methods', function()
local A, B
2013-09-17 18:55:50 +00:00
before_each(function()
A = class('A')
function A.static:foo() return 'foo' end
B = class('B', A)
end)
2013-09-17 18:55:50 +00:00
it('are available after being initialized', function()
assert.equal(A:foo(), 'foo')
end)
2013-09-17 18:55:50 +00:00
it('are available for subclasses', function()
assert.equal(B:foo(), 'foo')
end)
2013-09-15 21:14:00 +00:00
2013-09-17 18:55:50 +00:00
it('are overridable by subclasses, without affecting the superclasses', function()
function B.static:foo() return 'chunky bacon' end
2013-09-17 18:55:50 +00:00
assert.equal(B:foo(), 'chunky bacon')
assert.equal(A:foo(), 'foo')
end)
end)
end)