mirror of
https://github.com/kikito/middleclass.git
synced 2024-11-25 02:44:20 +00:00
ported tests to busted
This commit is contained in:
parent
f5aac38418
commit
157ebac7e7
@ -9,4 +9,4 @@ install:
|
||||
- sudo apt-get install luarocks
|
||||
- sudo luarocks install telescope
|
||||
|
||||
script: "tsc -f spec/*"
|
||||
script: "busted"
|
||||
|
@ -1,89 +1,89 @@
|
||||
local class = require 'middleclass'
|
||||
local Object = class.Object
|
||||
|
||||
context('Object', function()
|
||||
describe('Object', function()
|
||||
|
||||
|
||||
context('name', function()
|
||||
test('is correctly set', function()
|
||||
assert_equal(Object.name, 'Object')
|
||||
describe('name', function()
|
||||
it('is correctly set', function()
|
||||
assert.equal(Object.name, 'Object')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('tostring', function()
|
||||
test('returns "class Object"', function()
|
||||
assert_equal(tostring(Object), 'class Object')
|
||||
describe('tostring', function()
|
||||
it('returns "class Object"', function()
|
||||
assert.equal(tostring(Object), 'class Object')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('()', function()
|
||||
test('returns an object, like Object:new()', function()
|
||||
describe('()', function()
|
||||
it('returns an object, like Object:new()', function()
|
||||
local obj = Object()
|
||||
assert_true(obj:isInstanceOf(Object))
|
||||
assert.is_true(obj:isInstanceOf(Object))
|
||||
end)
|
||||
end)
|
||||
|
||||
context('subclass', function()
|
||||
describe('subclass', function()
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(function() Object.subclass() end)
|
||||
it('throws an error when used without the :', function()
|
||||
assert.error(function() Object.subclass() end)
|
||||
end)
|
||||
|
||||
test('throws an error when no name is given', function()
|
||||
assert_error( function() Object:subclass() end)
|
||||
it('throws an error when no name is given', function()
|
||||
assert.error( function() Object:subclass() end)
|
||||
end)
|
||||
|
||||
context('when given a class name', function()
|
||||
describe('when given a class name', function()
|
||||
|
||||
local SubClass
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
SubClass = Object:subclass('SubClass')
|
||||
end)
|
||||
|
||||
test('it returns a class with the correct name', function()
|
||||
assert_equal(SubClass.name, 'SubClass')
|
||||
it('it returns a class with the correct name', function()
|
||||
assert.equal(SubClass.name, 'SubClass')
|
||||
end)
|
||||
|
||||
test('it returns a class with the correct superclass', function()
|
||||
assert_equal(SubClass.super, Object)
|
||||
it('it returns a class with the correct superclass', function()
|
||||
assert.equal(SubClass.super, Object)
|
||||
end)
|
||||
|
||||
test('it includes the subclass in the list of subclasses', function()
|
||||
assert_true(Object.subclasses[SubClass])
|
||||
it('it includes the subclass in the list of subclasses', function()
|
||||
assert.is_true(Object.subclasses[SubClass])
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('instance creation', function()
|
||||
describe('instance creation', function()
|
||||
|
||||
local SubClass
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
SubClass = class('SubClass')
|
||||
function SubClass:initialize() self.mark=true end
|
||||
end)
|
||||
|
||||
context('allocate', function()
|
||||
describe('allocate', function()
|
||||
|
||||
test('allocates instances properly', function()
|
||||
it('allocates instances properly', function()
|
||||
local instance = SubClass:allocate()
|
||||
assert_equal(instance.class, SubClass)
|
||||
assert_equal(tostring(instance), "instance of " .. tostring(SubClass))
|
||||
assert.equal(instance.class, SubClass)
|
||||
assert.equal(tostring(instance), "instance of " .. tostring(SubClass))
|
||||
end)
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(Object.allocate)
|
||||
it('throws an error when used without the :', function()
|
||||
assert.error(Object.allocate)
|
||||
end)
|
||||
|
||||
test('does not call the initializer', function()
|
||||
it('does not call the initializer', function()
|
||||
local allocated = SubClass:allocate()
|
||||
assert_nil(allocated.mark)
|
||||
assert.is_nil(allocated.mark)
|
||||
end)
|
||||
|
||||
test('can be overriden', function()
|
||||
it('can be overriden', function()
|
||||
|
||||
local previousAllocate = SubClass.static.allocate
|
||||
|
||||
@ -94,26 +94,26 @@ context('Object', function()
|
||||
end
|
||||
|
||||
local allocated = SubClass:allocate()
|
||||
assert_true(allocated.mark)
|
||||
assert.is_true(allocated.mark)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('new', function()
|
||||
describe('new', function()
|
||||
|
||||
test('initializes instances properly', function()
|
||||
it('initializes instances properly', function()
|
||||
local instance = SubClass:new()
|
||||
assert_equal(instance.class, SubClass)
|
||||
assert.equal(instance.class, SubClass)
|
||||
end)
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(SubClass.new)
|
||||
it('throws an error when used without the :', function()
|
||||
assert.error(SubClass.new)
|
||||
end)
|
||||
|
||||
test('calls the initializer', function()
|
||||
it('calls the initializer', function()
|
||||
local initialized = SubClass:new()
|
||||
assert_true(initialized.mark)
|
||||
assert.is_true(initialized.mark)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -1,28 +1,28 @@
|
||||
local class = require 'middleclass'
|
||||
local Object = class.Object
|
||||
|
||||
context('class()', function()
|
||||
describe('class()', function()
|
||||
|
||||
context('when given no params', function()
|
||||
test('it throws an error', function()
|
||||
assert_error(class)
|
||||
describe('when given no params', function()
|
||||
it('it throws an error', function()
|
||||
assert.error(class)
|
||||
end)
|
||||
end)
|
||||
|
||||
context('when given a name', function()
|
||||
test('the resulting class has the correct name and Object as its superclass', function()
|
||||
describe('when given a name', function()
|
||||
it('the resulting class has the correct name and Object as its superclass', function()
|
||||
local TheClass = class('TheClass')
|
||||
assert_equal(TheClass.name, 'TheClass')
|
||||
assert_equal(TheClass.super, Object)
|
||||
assert.equal(TheClass.name, 'TheClass')
|
||||
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()
|
||||
describe('when given a name and a superclass', function()
|
||||
it('the resulting class has the correct name and superclass', function()
|
||||
local TheSuperClass = class('TheSuperClass')
|
||||
local TheSubClass = class('TheSubClass', TheSuperClass)
|
||||
assert_equal(TheSubClass.name, 'TheSubClass')
|
||||
assert_equal(TheSubClass.super, TheSuperClass)
|
||||
assert.equal(TheSubClass.name, 'TheSubClass')
|
||||
assert.equal(TheSubClass.super, TheSuperClass)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
@ -1,76 +1,76 @@
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('A Class', function()
|
||||
describe('A Class', function()
|
||||
|
||||
context('Default stuff', function()
|
||||
describe('Default stuff', function()
|
||||
|
||||
local AClass
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
AClass = class('AClass')
|
||||
end)
|
||||
|
||||
context('name', function()
|
||||
test('is correctly set', function()
|
||||
assert_equal(AClass.name, 'AClass')
|
||||
describe('name', function()
|
||||
it('is correctly set', function()
|
||||
assert.equal(AClass.name, 'AClass')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('tostring', function()
|
||||
test('returns "class *name*"', function()
|
||||
assert_equal(tostring(AClass), 'class AClass')
|
||||
describe('tostring', function()
|
||||
it('returns "class *name*"', function()
|
||||
assert.equal(tostring(AClass), 'class AClass')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('()', function()
|
||||
test('returns an object, like Class:new()', function()
|
||||
describe('()', function()
|
||||
it('returns an object, like Class:new()', function()
|
||||
local obj = AClass()
|
||||
assert_equal(obj.class, AClass)
|
||||
assert.equal(obj.class, AClass)
|
||||
end)
|
||||
end)
|
||||
|
||||
context('include', function()
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(function() AClass.include() end)
|
||||
describe('include', function()
|
||||
it('throws an error when used without the :', function()
|
||||
assert.error(function() AClass.include() end)
|
||||
end)
|
||||
test('throws an error when passed a non-table:', function()
|
||||
assert_error(function() AClass:include(1) end)
|
||||
it('throws an error when passed a non-table:', function()
|
||||
assert.error(function() AClass:include(1) end)
|
||||
end)
|
||||
end)
|
||||
|
||||
context('subclass', function()
|
||||
describe('subclass', function()
|
||||
|
||||
test('throws an error when used without the :', function()
|
||||
assert_error(function() AClass.subclass() end)
|
||||
it('throws an error when used without the :', function()
|
||||
assert.error(function() AClass.subclass() end)
|
||||
end)
|
||||
|
||||
test('throws an error when no name is given', function()
|
||||
assert_error( function() AClass:subclass() end)
|
||||
it('throws an error when no name is given', function()
|
||||
assert.error( function() AClass:subclass() end)
|
||||
end)
|
||||
|
||||
context('when given a subclass name', function()
|
||||
describe('when given a subclass name', function()
|
||||
|
||||
local SubClass
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
function AClass.static:subclassed(other) self.static.child = other end
|
||||
SubClass = AClass:subclass('SubClass')
|
||||
end)
|
||||
|
||||
test('it returns a class with the correct name', function()
|
||||
assert_equal(SubClass.name, 'SubClass')
|
||||
it('it returns a class with the correct name', function()
|
||||
assert.equal(SubClass.name, 'SubClass')
|
||||
end)
|
||||
|
||||
test('it returns a class with the correct superclass', function()
|
||||
assert_equal(SubClass.super, AClass)
|
||||
it('it returns a class with the correct superclass', function()
|
||||
assert.equal(SubClass.super, AClass)
|
||||
end)
|
||||
|
||||
test('it invokes the subclassed hook method', function()
|
||||
assert_equal(SubClass, AClass.child)
|
||||
it('it invokes the subclassed hook method', function()
|
||||
assert.equal(SubClass, AClass.child)
|
||||
end)
|
||||
|
||||
test('it includes the subclass in the list of subclasses', function()
|
||||
assert_true(AClass.subclasses[SubClass])
|
||||
it('it includes the subclass in the list of subclasses', function()
|
||||
assert.is_true(AClass.subclasses[SubClass])
|
||||
end)
|
||||
|
||||
end)
|
||||
@ -81,56 +81,56 @@ context('A Class', function()
|
||||
|
||||
|
||||
|
||||
context('attributes', function()
|
||||
describe('attributes', function()
|
||||
|
||||
local A, B
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
A = class('A')
|
||||
A.static.foo = 'foo'
|
||||
|
||||
B = class('B', A)
|
||||
end)
|
||||
|
||||
test('are available after being initialized', function()
|
||||
assert_equal(A.foo, 'foo')
|
||||
it('are available after being initialized', function()
|
||||
assert.equal(A.foo, 'foo')
|
||||
end)
|
||||
|
||||
test('are available for subclasses', function()
|
||||
assert_equal(B.foo, 'foo')
|
||||
it('are available for subclasses', function()
|
||||
assert.equal(B.foo, 'foo')
|
||||
end)
|
||||
|
||||
test('are overridable by subclasses, without affecting the superclasses', function()
|
||||
it('are overridable by subclasses, without affecting the superclasses', function()
|
||||
B.static.foo = 'chunky bacon'
|
||||
assert_equal(B.foo, 'chunky bacon')
|
||||
assert_equal(A.foo, 'foo')
|
||||
assert.equal(B.foo, 'chunky bacon')
|
||||
assert.equal(A.foo, 'foo')
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('methods', function()
|
||||
describe('methods', function()
|
||||
|
||||
local A, B
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
A = class('A')
|
||||
function A.static:foo() return 'foo' end
|
||||
|
||||
B = class('B', A)
|
||||
end)
|
||||
|
||||
test('are available after being initialized', function()
|
||||
assert_equal(A:foo(), 'foo')
|
||||
it('are available after being initialized', function()
|
||||
assert.equal(A:foo(), 'foo')
|
||||
end)
|
||||
|
||||
test('are available for subclasses', function()
|
||||
assert_equal(B:foo(), 'foo')
|
||||
it('are available for subclasses', function()
|
||||
assert.equal(B:foo(), 'foo')
|
||||
end)
|
||||
|
||||
test('are overridable by subclasses, without affecting the superclasses', function()
|
||||
it('are overridable by subclasses, without affecting the superclasses', function()
|
||||
function B.static:foo() return 'chunky bacon' end
|
||||
assert_equal(B:foo(), 'chunky bacon')
|
||||
assert_equal(A:foo(), 'foo')
|
||||
assert.equal(B:foo(), 'chunky bacon')
|
||||
assert.equal(A:foo(), 'foo')
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -1,37 +1,37 @@
|
||||
local class = require 'middleclass'
|
||||
local Object = class.Object
|
||||
|
||||
context('includes', function()
|
||||
describe('includes', function()
|
||||
|
||||
context('nils, numbers, etc', function()
|
||||
describe('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()
|
||||
describe('A ' .. theType, function()
|
||||
|
||||
local f1 = function() return Object.includes(Object, primitive) end
|
||||
local f2 = function() return Object.includes(primitive, o) end
|
||||
local f3 = function() return Object.includes(primitive, primitive) end
|
||||
|
||||
|
||||
context('don\'t throw errors', function()
|
||||
test('includes(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
describe('don\'t throw errors', function()
|
||||
it('includes(Object, '.. theType ..')', function()
|
||||
assert.not_error(f1)
|
||||
end)
|
||||
test('includes(' .. theType .. ', Object:new())', function()
|
||||
assert_not_error(f2)
|
||||
it('includes(' .. theType .. ', Object:new())', function()
|
||||
assert.not_error(f2)
|
||||
end)
|
||||
test('includes(' .. theType .. ',' .. theType ..')', function()
|
||||
assert_not_error(f3)
|
||||
it('includes(' .. theType .. ',' .. theType ..')', function()
|
||||
assert.not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
test('make includes return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
it('make includes return false', function()
|
||||
assert.is_false(f1())
|
||||
assert.is_false(f2())
|
||||
assert.is_false(f3())
|
||||
end)
|
||||
|
||||
end)
|
||||
@ -39,7 +39,7 @@ context('includes', function()
|
||||
|
||||
end)
|
||||
|
||||
context('A class', function()
|
||||
describe('A class', function()
|
||||
|
||||
local Class1 = class('Class1')
|
||||
local Class2 = class('Class2', Class1)
|
||||
@ -49,17 +49,17 @@ context('includes', function()
|
||||
local hasFoo = { foo=function() return 'foo' end }
|
||||
Class1:include(hasFoo)
|
||||
|
||||
test('returns true if it includes a mixin', function()
|
||||
assert_true(Class1:includes(hasFoo))
|
||||
it('returns true if it includes a mixin', function()
|
||||
assert.is_true(Class1:includes(hasFoo))
|
||||
end)
|
||||
|
||||
test('returns true if its superclass includes a mixin', function()
|
||||
assert_true(Class2:includes(hasFoo))
|
||||
assert_true(Class3:includes(hasFoo))
|
||||
it('returns true if its superclass includes a mixin', function()
|
||||
assert.is_true(Class2:includes(hasFoo))
|
||||
assert.is_true(Class3:includes(hasFoo))
|
||||
end)
|
||||
|
||||
test('returns false otherwise', function()
|
||||
assert_false(UnrelatedClass:includes(hasFoo))
|
||||
it('returns false otherwise', function()
|
||||
assert.is_false(UnrelatedClass:includes(hasFoo))
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -1,36 +1,36 @@
|
||||
local class = require 'middleclass'
|
||||
local Object = class.Object
|
||||
|
||||
context('Object.isInstanceOf', function()
|
||||
describe('Object.isInstanceOf', function()
|
||||
|
||||
context('nils, integers, strings, tables, and functions', function()
|
||||
describe('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()
|
||||
describe('A ' .. theType, function()
|
||||
|
||||
local f1 = function() return Object.isInstanceOf(primitive, Object) end
|
||||
local f2 = function() return Object.isInstanceOf(primitive, o) end
|
||||
local f3 = function() return Object.isInstanceOf(primitive, primitive) end
|
||||
|
||||
context('does not throw errors', function()
|
||||
test('instanceOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
describe('does not throw errors', function()
|
||||
it('instanceOf(Object, '.. theType ..')', function()
|
||||
assert.not_error(f1)
|
||||
end)
|
||||
test('instanceOf(' .. theType .. ', Object:new())', function()
|
||||
assert_not_error(f2)
|
||||
it('instanceOf(' .. theType .. ', Object:new())', function()
|
||||
assert.not_error(f2)
|
||||
end)
|
||||
test('instanceOf(' .. theType .. ',' .. theType ..')', function()
|
||||
assert_not_error(f3)
|
||||
it('instanceOf(' .. theType .. ',' .. theType ..')', function()
|
||||
assert.not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
test('makes instanceOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
it('makes instanceOf return false', function()
|
||||
assert.is_false(f1())
|
||||
assert.is_false(f2())
|
||||
assert.is_false(f3())
|
||||
end)
|
||||
|
||||
end)
|
||||
@ -38,7 +38,7 @@ context('Object.isInstanceOf', function()
|
||||
|
||||
end)
|
||||
|
||||
context('An instance', function()
|
||||
describe('An instance', function()
|
||||
local Class1 = class('Class1')
|
||||
local Class2 = class('Class2', Class1)
|
||||
local Class3 = class('Class3', Class2)
|
||||
@ -46,34 +46,34 @@ context('Object.isInstanceOf', function()
|
||||
|
||||
local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
|
||||
|
||||
test('isInstanceOf(Object)', function()
|
||||
assert_true(o1:isInstanceOf(Object))
|
||||
assert_true(o2:isInstanceOf(Object))
|
||||
assert_true(o3:isInstanceOf(Object))
|
||||
it('isInstanceOf(Object)', function()
|
||||
assert.is_true(o1:isInstanceOf(Object))
|
||||
assert.is_true(o2:isInstanceOf(Object))
|
||||
assert.is_true(o3:isInstanceOf(Object))
|
||||
end)
|
||||
|
||||
test('isInstanceOf its class', function()
|
||||
assert_true(o1:isInstanceOf(Class1))
|
||||
assert_true(o2:isInstanceOf(Class2))
|
||||
assert_true(o3:isInstanceOf(Class3))
|
||||
it('isInstanceOf its class', function()
|
||||
assert.is_true(o1:isInstanceOf(Class1))
|
||||
assert.is_true(o2:isInstanceOf(Class2))
|
||||
assert.is_true(o3:isInstanceOf(Class3))
|
||||
end)
|
||||
|
||||
test('is instanceOf its class\' superclasses', function()
|
||||
assert_true(o2:isInstanceOf(Class1))
|
||||
assert_true(o3:isInstanceOf(Class1))
|
||||
assert_true(o3:isInstanceOf(Class2))
|
||||
it('is instanceOf its class\' superclasses', function()
|
||||
assert.is_true(o2:isInstanceOf(Class1))
|
||||
assert.is_true(o3:isInstanceOf(Class1))
|
||||
assert.is_true(o3:isInstanceOf(Class2))
|
||||
end)
|
||||
|
||||
test('is not instanceOf its class\' subclasses', function()
|
||||
assert_false(o1:isInstanceOf(Class2))
|
||||
assert_false(o1:isInstanceOf(Class3))
|
||||
assert_false(o2:isInstanceOf(Class3))
|
||||
it('is not instanceOf its class\' subclasses', function()
|
||||
assert.is_false(o1:isInstanceOf(Class2))
|
||||
assert.is_false(o1:isInstanceOf(Class3))
|
||||
assert.is_false(o2:isInstanceOf(Class3))
|
||||
end)
|
||||
|
||||
test('is not instanceOf an unrelated class', function()
|
||||
assert_false(o1:isInstanceOf(UnrelatedClass))
|
||||
assert_false(o2:isInstanceOf(UnrelatedClass))
|
||||
assert_false(o3:isInstanceOf(UnrelatedClass))
|
||||
it('is not instanceOf an unrelated class', function()
|
||||
assert.is_false(o1:isInstanceOf(UnrelatedClass))
|
||||
assert.is_false(o2:isInstanceOf(UnrelatedClass))
|
||||
assert.is_false(o3:isInstanceOf(UnrelatedClass))
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -1,24 +1,24 @@
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('An instance', function()
|
||||
describe('An instance', function()
|
||||
|
||||
context('attributes', function()
|
||||
describe('attributes', function()
|
||||
|
||||
local Person
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
Person = class('Person')
|
||||
function Person:initialize(name)
|
||||
self.name = name
|
||||
end
|
||||
end)
|
||||
|
||||
test('are available in the instance after being initialized', function()
|
||||
it('are available in the instance after being initialized', function()
|
||||
local bob = Person:new('bob')
|
||||
assert_equal(bob.name, 'bob')
|
||||
assert.equal(bob.name, 'bob')
|
||||
end)
|
||||
|
||||
test('are available in the instance after being initialized by a superclass', function()
|
||||
it('are available in the instance after being initialized by a superclass', function()
|
||||
local AgedPerson = class('AgedPerson', Person)
|
||||
function AgedPerson:initialize(name, age)
|
||||
Person.initialize(self, name)
|
||||
@ -26,17 +26,17 @@ context('An instance', function()
|
||||
end
|
||||
|
||||
local pete = AgedPerson:new('pete', 31)
|
||||
assert_equal(pete.name, 'pete')
|
||||
assert_equal(pete.age, 31)
|
||||
assert.equal(pete.name, 'pete')
|
||||
assert.equal(pete.age, 31)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('methods', function()
|
||||
describe('methods', function()
|
||||
|
||||
local A, B, a, b
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
A = class('A')
|
||||
function A:overridden() return 'foo' end
|
||||
function A:regular() return 'regular' end
|
||||
@ -48,16 +48,16 @@ context('An instance', function()
|
||||
b = B:new()
|
||||
end)
|
||||
|
||||
test('are available for any instance', function()
|
||||
assert_equal(a:overridden(), 'foo')
|
||||
it('are available for any instance', function()
|
||||
assert.equal(a:overridden(), 'foo')
|
||||
end)
|
||||
|
||||
test('are inheritable', function()
|
||||
assert_equal(b:regular(), 'regular')
|
||||
it('are inheritable', function()
|
||||
assert.equal(b:regular(), 'regular')
|
||||
end)
|
||||
|
||||
test('are overridable', function()
|
||||
assert_equal(b:overridden(), 'bar')
|
||||
it('are overridable', function()
|
||||
assert.equal(b:overridden(), 'bar')
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -1,9 +1,9 @@
|
||||
local class = require 'middleclass'
|
||||
local Object = class.Object
|
||||
|
||||
context('Metamethods', function()
|
||||
describe('Metamethods', function()
|
||||
|
||||
context('Custom Metamethods', function()
|
||||
describe('Custom Metamethods', function()
|
||||
-- Tests all metamethods. Note that __len is missing (lua makes table length unoverridable)
|
||||
-- I'll use a() to note the length of vector "a" (I would have preferred to use #a, but it's not possible)
|
||||
-- I'll be using 'a' instead of 'self' on this example since it is shorter
|
||||
@ -45,12 +45,12 @@ context('Metamethods', function()
|
||||
__mul = { 4*a, Vector(4,8,12) }--,
|
||||
--__index = { b[1], 3 }
|
||||
}) do
|
||||
test(metamethod .. ' works as expected', function()
|
||||
assert_equal(values[1], values[2])
|
||||
it(metamethod .. ' works as expected', function()
|
||||
assert.equal(values[1], values[2])
|
||||
end)
|
||||
end
|
||||
|
||||
context('Inherited Metamethods', function()
|
||||
describe('Inherited Metamethods', function()
|
||||
local Vector2= class('Vector2', Vector)
|
||||
function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
|
||||
|
||||
@ -70,36 +70,36 @@ context('Metamethods', function()
|
||||
__pow = { c^d, Vector(0,0,0) },
|
||||
__mul = { 4*c, Vector(4,8,12) }
|
||||
}) do
|
||||
test(metamethod .. ' works as expected', function()
|
||||
assert_equal(values[1], values[2])
|
||||
it(metamethod .. ' works as expected', function()
|
||||
assert.equal(values[1], values[2])
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
context('Default Metamethods', function()
|
||||
describe('Default Metamethods', function()
|
||||
|
||||
local Peter, peter
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
Peter = class('Peter')
|
||||
peter = Peter()
|
||||
end)
|
||||
|
||||
context('A Class', function()
|
||||
test('has a call metamethod properly set', function()
|
||||
assert_true(peter:isInstanceOf(Peter))
|
||||
describe('A Class', function()
|
||||
it('has a call metamethod properly set', function()
|
||||
assert.is_true(peter:isInstanceOf(Peter))
|
||||
end)
|
||||
test('has a tostring metamethod properly set', function()
|
||||
assert_equal(tostring(Peter), 'class Peter')
|
||||
it('has a tostring metamethod properly set', function()
|
||||
assert.equal(tostring(Peter), 'class Peter')
|
||||
end)
|
||||
end)
|
||||
|
||||
context('An instance', function()
|
||||
test('has a tostring metamethod, returning a different result from Object.__tostring', function()
|
||||
assert_not_equal(Peter.__tostring, Object.__tostring)
|
||||
assert_equal(tostring(peter), 'instance of class Peter')
|
||||
describe('An instance', function()
|
||||
it('has a tostring metamethod, returning a different result from Object.__tostring', function()
|
||||
assert.not_equal(Peter.__tostring, Object.__tostring)
|
||||
assert.equal(tostring(peter), 'instance of class Peter')
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
@ -1,10 +1,10 @@
|
||||
local class = require 'middleclass'
|
||||
|
||||
context('A Mixin', function()
|
||||
describe('A Mixin', function()
|
||||
|
||||
local Mixin1, Mixin2, Class1, Class2
|
||||
|
||||
before(function()
|
||||
before_each(function()
|
||||
Mixin1, Mixin2 = {},{}
|
||||
|
||||
function Mixin1:included(theClass) theClass.includesMixin1 = true end
|
||||
@ -23,30 +23,30 @@ context('A Mixin', function()
|
||||
function Class2:bar2() return 'bar2' end
|
||||
end)
|
||||
|
||||
test('invokes the "included" method when included', function()
|
||||
assert_true(Class1.includesMixin1)
|
||||
it('invokes the "included" method when included', function()
|
||||
assert.is_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)
|
||||
it('has all its functions (except "included") copied to its target class', function()
|
||||
assert.equal(Class1:bar(), 'bar')
|
||||
assert.is_nil(Class1.included)
|
||||
end)
|
||||
|
||||
test('makes its functions available to subclasses', function()
|
||||
assert_equal(Class2:baz(), 'baz')
|
||||
it('makes its functions available to subclasses', function()
|
||||
assert.equal(Class2:baz(), 'baz')
|
||||
end)
|
||||
|
||||
test('allows overriding of methods in the same class', function()
|
||||
assert_equal(Class2:foo(), 'foo1')
|
||||
it('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')
|
||||
it('allows overriding of methods on subclasses', function()
|
||||
assert.equal(Class2:bar2(), 'bar2')
|
||||
end)
|
||||
|
||||
test('makes new static methods available in classes', function()
|
||||
assert_equal(Class1:bazzz(), 'bazzz')
|
||||
assert_equal(Class2:bazzz(), 'bazzz')
|
||||
it('makes new static methods available in classes', function()
|
||||
assert.equal(Class1:bazzz(), 'bazzz')
|
||||
assert.equal(Class2:bazzz(), 'bazzz')
|
||||
end)
|
||||
|
||||
end)
|
||||
|
@ -1,35 +1,35 @@
|
||||
local class = require 'middleclass'
|
||||
local Object = class.Object
|
||||
|
||||
context('isSubclassOf', function()
|
||||
describe('isSubclassOf', function()
|
||||
|
||||
context('nils, integers, etc', function()
|
||||
describe('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()
|
||||
describe('A ' .. theType, function()
|
||||
|
||||
local f1 = function() return Object.isSubclassOf(Object, primitive) end
|
||||
local f2 = function() return Object.isSubclassOf(primitive, o) end
|
||||
local f3 = function() return Object.isSubclassOf(primitive, primitive) end
|
||||
|
||||
context('does not throw errors', function()
|
||||
test('isSubclassOf(Object, '.. theType ..')', function()
|
||||
assert_not_error(f1)
|
||||
describe('does not throw errors', function()
|
||||
it('isSubclassOf(Object, '.. theType ..')', function()
|
||||
assert.not_error(f1)
|
||||
end)
|
||||
test('isSubclassOf(' .. theType .. ', Object:new())', function()
|
||||
assert_not_error(f2)
|
||||
it('isSubclassOf(' .. theType .. ', Object:new())', function()
|
||||
assert.not_error(f2)
|
||||
end)
|
||||
test('isSubclassOf(' .. theType .. ',' .. theType ..')', function()
|
||||
assert_not_error(f3)
|
||||
it('isSubclassOf(' .. theType .. ',' .. theType ..')', function()
|
||||
assert.not_error(f3)
|
||||
end)
|
||||
end)
|
||||
|
||||
test('makes isSubclassOf return false', function()
|
||||
assert_false(f1())
|
||||
assert_false(f2())
|
||||
assert_false(f3())
|
||||
it('makes isSubclassOf return false', function()
|
||||
assert.is_false(f1())
|
||||
assert.is_false(f2())
|
||||
assert.is_false(f3())
|
||||
end)
|
||||
|
||||
end)
|
||||
@ -37,37 +37,37 @@ context('isSubclassOf', function()
|
||||
|
||||
end)
|
||||
|
||||
context('Any class (except Object)', function()
|
||||
describe('Any class (except Object)', function()
|
||||
local Class1 = class('Class1')
|
||||
local Class2 = class('Class2', Class1)
|
||||
local Class3 = class('Class3', Class2)
|
||||
local UnrelatedClass = class('Unrelated')
|
||||
|
||||
test('isSubclassOf(Object)', function()
|
||||
assert_true(Class1:isSubclassOf(Object))
|
||||
assert_true(Class2:isSubclassOf(Object))
|
||||
assert_true(Class3:isSubclassOf(Object))
|
||||
it('isSubclassOf(Object)', function()
|
||||
assert.is_true(Class1:isSubclassOf(Object))
|
||||
assert.is_true(Class2:isSubclassOf(Object))
|
||||
assert.is_true(Class3:isSubclassOf(Object))
|
||||
end)
|
||||
|
||||
test('is subclassOf its direct superclass', function()
|
||||
assert_true(Class2:isSubclassOf(Class1))
|
||||
assert_true(Class3:isSubclassOf(Class2))
|
||||
it('is subclassOf its direct superclass', function()
|
||||
assert.is_true(Class2:isSubclassOf(Class1))
|
||||
assert.is_true(Class3:isSubclassOf(Class2))
|
||||
end)
|
||||
|
||||
test('is subclassOf its ancestors', function()
|
||||
assert_true(Class3:isSubclassOf(Class1))
|
||||
it('is subclassOf its ancestors', function()
|
||||
assert.is_true(Class3:isSubclassOf(Class1))
|
||||
end)
|
||||
|
||||
test('is a subclassOf its class\' subclasses', function()
|
||||
assert_true(Class2:isSubclassOf(Class1))
|
||||
assert_true(Class3:isSubclassOf(Class1))
|
||||
assert_true(Class3:isSubclassOf(Class2))
|
||||
it('is a subclassOf its class\' subclasses', function()
|
||||
assert.is_true(Class2:isSubclassOf(Class1))
|
||||
assert.is_true(Class3:isSubclassOf(Class1))
|
||||
assert.is_true(Class3:isSubclassOf(Class2))
|
||||
end)
|
||||
|
||||
test('is not a subclassOf an unrelated class', function()
|
||||
assert_false(Class1:isSubclassOf(UnrelatedClass))
|
||||
assert_false(Class2:isSubclassOf(UnrelatedClass))
|
||||
assert_false(Class3:isSubclassOf(UnrelatedClass))
|
||||
it('is not a subclassOf an unrelated class', function()
|
||||
assert.is_false(Class1:isSubclassOf(UnrelatedClass))
|
||||
assert.is_false(Class2:isSubclassOf(UnrelatedClass))
|
||||
assert.is_false(Class3:isSubclassOf(UnrelatedClass))
|
||||
end)
|
||||
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user