2013-09-15 21:14:00 +00:00
|
|
|
local class = require 'middleclass'
|
2011-08-10 19:45:15 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('An instance', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('attributes', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
|
|
|
|
local Person
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
before_each(function()
|
2011-08-10 19:45:15 +00:00
|
|
|
Person = class('Person')
|
|
|
|
function Person:initialize(name)
|
|
|
|
self.name = name
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('are available in the instance after being initialized', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
local bob = Person:new('bob')
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.equal(bob.name, 'bob')
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('are available in the instance after being initialized by a superclass', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
local AgedPerson = class('AgedPerson', Person)
|
|
|
|
function AgedPerson:initialize(name, age)
|
|
|
|
Person.initialize(self, name)
|
|
|
|
self.age = age
|
|
|
|
end
|
|
|
|
|
|
|
|
local pete = AgedPerson:new('pete', 31)
|
2013-09-17 18:55:50 +00:00
|
|
|
assert.equal(pete.name, 'pete')
|
|
|
|
assert.equal(pete.age, 31)
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('methods', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
|
|
|
|
local A, B, a, b
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
before_each(function()
|
2011-08-10 19:45:15 +00:00
|
|
|
A = class('A')
|
|
|
|
function A:overridden() return 'foo' end
|
|
|
|
function A:regular() return 'regular' end
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-10 19:45:15 +00:00
|
|
|
B = class('B', A)
|
|
|
|
function B:overridden() return 'bar' end
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-10 19:45:15 +00:00
|
|
|
a = A:new()
|
|
|
|
b = B:new()
|
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('are available for any instance', function()
|
|
|
|
assert.equal(a:overridden(), 'foo')
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('are inheritable', function()
|
|
|
|
assert.equal(b:regular(), 'regular')
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('are overridable', function()
|
|
|
|
assert.equal(b:overridden(), 'bar')
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|