2013-09-15 21:14:00 +00:00
|
|
|
local class = require 'middleclass'
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('A Mixin', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
|
2011-08-14 16:07:44 +00:00
|
|
|
local Mixin1, Mixin2, Class1, Class2
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
before_each(function()
|
2011-08-14 16:07:44 +00:00
|
|
|
Mixin1, Mixin2 = {},{}
|
2011-08-10 19:45:15 +00:00
|
|
|
|
2011-08-14 16:07:44 +00:00
|
|
|
function Mixin1:included(theClass) theClass.includesMixin1 = true end
|
|
|
|
function Mixin1:foo() return 'foo' end
|
|
|
|
function Mixin1:bar() return 'bar' end
|
2011-09-18 15:40:50 +00:00
|
|
|
Mixin1.static = {}
|
|
|
|
Mixin1.static.bazzz = function() return 'bazzz' end
|
|
|
|
|
2011-08-14 16:07:44 +00:00
|
|
|
|
|
|
|
function Mixin2:baz() return 'baz' end
|
|
|
|
|
|
|
|
Class1 = class('Class1'):include(Mixin1, Mixin2)
|
2011-08-13 21:58:55 +00:00
|
|
|
function Class1:foo() return 'foo1' end
|
|
|
|
|
|
|
|
Class2 = class('Class2', Class1)
|
|
|
|
function Class2:bar2() return 'bar2' end
|
|
|
|
end)
|
2011-08-10 19:45:15 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('invokes the "included" method when included', function()
|
|
|
|
assert.is_true(Class1.includesMixin1)
|
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('has all its functions (except "included") copied to its target class', function()
|
|
|
|
assert.equal(Class1:bar(), 'bar')
|
|
|
|
assert.is_nil(Class1.included)
|
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('makes its functions available to subclasses', function()
|
|
|
|
assert.equal(Class2:baz(), 'baz')
|
2011-08-12 08:33:58 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('allows overriding of methods in the same class', function()
|
|
|
|
assert.equal(Class2:foo(), 'foo1')
|
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('allows overriding of methods on subclasses', function()
|
|
|
|
assert.equal(Class2:bar2(), 'bar2')
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
it('makes new static methods available in classes', function()
|
|
|
|
assert.equal(Class1:bazzz(), 'bazzz')
|
|
|
|
assert.equal(Class2:bazzz(), 'bazzz')
|
2011-09-18 15:40:50 +00:00
|
|
|
end)
|
|
|
|
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2011-08-13 21:58:55 +00:00
|
|
|
|