| 1 |
--[[ Copyright (c) 2014 Edvin "Lego3" Linge |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
require("class_test_base") |
| 22 |
|
| 23 |
class "ParentClass" |
| 24 |
|
| 25 |
function ParentClass:ParentClass(name) |
| 26 |
self.class_name = name |
| 27 |
end |
| 28 |
|
| 29 |
class "ChildClass" (ParentClass) |
| 30 |
|
| 31 |
function ChildClass:ChildClass(name) |
| 32 |
self:ParentClass(name) |
| 33 |
end |
| 34 |
|
| 35 |
describe("A class in CorsixTH ", function() |
| 36 |
local child_class, parent_class |
| 37 |
|
| 38 |
setup(function() |
| 39 |
child_class = ChildClass("TheChild") |
| 40 |
parent_class = ParentClass("TheParent") |
| 41 |
end) |
| 42 |
|
| 43 |
it("should be instansiatable", function() |
| 44 |
assert.are.equal("TheChild", child_class.class_name) |
| 45 |
assert.are.equal("TheParent", parent_class.class_name) |
| 46 |
end) |
| 47 |
|
| 48 |
it("should be the same for many instances of the same type, including inheritance", function() |
| 49 |
assert.truthy(class.is(child_class, ChildClass)) |
| 50 |
assert.falsy(class.is(parent_class, ChildClass)) |
| 51 |
|
| 52 |
assert.truthy(class.is(child_class, ParentClass)) |
| 53 |
assert.truthy(class.is(parent_class, ParentClass)) |
| 54 |
end) |
| 55 |
|
| 56 |
it("should have a name", function() |
| 57 |
assert.are.equal("ChildClass", class.name(ChildClass)) |
| 58 |
assert.are.equal("ParentClass", class.name(ParentClass)) |
| 59 |
assert.are.equal(nil, class.name(NoRealClass)) |
| 60 |
end) |
| 61 |
|
| 62 |
it("should know if it has a superclass", function() |
| 63 |
assert.are.equal(ParentClass, class.superclass(ChildClass)) |
| 64 |
assert.are.equal(nil, class.superclass(ParentClass)) |
| 65 |
assert.has_error(class.superclass, NoRealClass) |
| 66 |
end) |
| 67 |
|
| 68 |
it("should have a type", function() |
| 69 |
assert.are.equal("ChildClass", class.type(child_class)) |
| 70 |
assert.are.equal("ParentClass", class.type(parent_class)) |
| 71 |
end) |
| 72 |
|
| 73 |
it("should make the metatable available", function() |
| 74 |
assert.are.equal(getmetatable(parent_class), parent_class._metatable) |
| 75 |
end) |
| 76 |
end) |