From eacf0f60c0752616407fbca6cd9d8ca9757ce624 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 5 Jul 2017 16:52:50 +0200 Subject: Initial commit --- test.lua | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 test.lua (limited to 'test.lua') diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..e66bd55 --- /dev/null +++ b/test.lua @@ -0,0 +1,95 @@ +local Object = require "Object" + +assert(Object) +assert(Object.extends) +assert(Object.new) + +local object = Object() + +assert(object) +assert(object:is(Object)) +assert(not object:is(Child)) +assert(not object:is(GrandChild)) +assert(not object:is(Sibling)) +assert(not object:is(Meta)) +assert(not object:is(MetaChild)) + +local Child = Object:extends() +local GrandChild = Child:extends() + +function Child:new (a) + self.a = a +end +function GrandChild:new (b, a) + self.__super.new(self, a) + self.b = b +end + +Child.NUMBER = 3 +GrandChild.NUMBER = 11 + +local a = 10 +local b = 15 +local child = Child(a) +local grandChild = GrandChild(b, a) + +assert(child:is(Object)) +assert(child:is(Child)) +assert(not child:is(GrandChild)) + +assert(grandChild:is(Object)) +assert(grandChild:is(Child)) +assert(grandChild:is(GrandChild)) + +assert(child.NUMBER == Child.NUMBER) +assert(not (child.NUMBER == GrandChild.NUMBER)) +assert(not (grandChild.NUMBER == Child.NUMBER)) +assert(grandChild.NUMBER == GrandChild.NUMBER) + +assert(child.a == a) +assert(grandChild.a == a and grandChild.b == b) + +assert(GrandChild.__class == GrandChild) +assert(GrandChild.__super == Child) +assert(grandChild.__class == GrandChild) +assert(grandChild.__super == Child) +assert(grandChild.__class.__super == Child) +assert(grandChild.__super.__super == Object) + +local Sibling = Object:extends() + +function Sibling:new (p) + self.p = p ^ 2 +end + +local p = 6 +local sibling = Sibling(p) + +assert(not sibling.NUMBER) +assert(sibling.p == p ^ 2) + +local Meta = Object:extends() + +function Meta:new (x, y) + self.x = x + self.y = y +end +function Meta.__add (self, op) + return self.__class(self.x + op.x, self.y + op.y) +end + +local x = 5 +local y = -2 +local meta = Meta(x, y) +local meta2 = Meta(x, y) + +assert(rawget(getmetatable(meta), "__add")) + +local sum = meta + meta2 + +assert(sum:is(Meta)) +assert((sum.x == meta.x + meta2.x) and (sum.y == meta.y + meta2.y)) + +io.write("\27[1;32;40mSuccess\27[0m\n") +io.flush() +return true -- cgit v1.1