add small testsuite
authorChristian Thaeter <ct@pipapo.org>
Fri, 14 Nov 2014 22:47:58 +0000 (23:47 +0100)
committerChristian Thaeter <ct@pipapo.org>
Fri, 14 Nov 2014 22:47:58 +0000 (23:47 +0100)
proto.lua
prototest.lua [new file with mode: 0644]

index 8dfc300..1dc20d3 100644 (file)
--- a/proto.lua
+++ b/proto.lua
@@ -76,7 +76,7 @@ end
 
 --- Object:proto_prototypes ()
 ---   iterator over all prototypes of an object in search order (depth first, no duplicates, including self)
 
 --- Object:proto_prototypes ()
 ---   iterator over all prototypes of an object in search order (depth first, no duplicates, including self)
----    for parent in object:prototypes()
+---    for prototype in object:prototypes()
 local function next_prototype(state)
     local object, mystate
     repeat
 local function next_prototype(state)
     local object, mystate
     repeat
diff --git a/prototest.lua b/prototest.lua
new file mode 100644 (file)
index 0000000..1e26fa5
--- /dev/null
@@ -0,0 +1,91 @@
+
+local Object = require "proto"
+
+Object.name="Object"
+
+O2 = Object:proto_clone { o2 = "o2"}
+assert( O2.o2 == "o2")
+assert( Object.o2 == nil)
+
+
+O3 = Object:proto_clone {o3 = "o3"}
+assert( O3.o3 == "o3")
+O3:proto_inherit(O2)
+assert( O3.o2 == "o2")
+assert( O3:proto_objectof('o2') == O2)
+
+
+O4 = O3:proto_clone({o4 = "o4", "name"})
+assert( O4.o4 == "o4")
+
+O4.name = "O4"
+Object.name = "Still Object"
+
+assert( O4.name == "O4")
+assert( Object.name == "Still Object")
+
+
+assert(O4:proto_objectof ("name") == O4)
+
+
+do
+   local expected_prototypes = {}
+
+   expected_prototypes[Object] = true
+   expected_prototypes[O2] = true
+   expected_prototypes[O3] = true
+   expected_prototypes[O4] = true
+
+   for prototype in O4:proto_prototypes() do
+      assert(expected_prototypes[prototype])
+      expected_prototypes[prototype] = nil
+   end
+   assert(not next(expected_prototypes))
+end
+
+do
+   local expected_members = {
+      __index=true,
+      __proto_parents=true,
+      __metatable=true,
+      name=true,
+      o2=true,
+      o3=true,
+      o4=true,
+      proto_isa=true,
+      proto_prototypes=true,
+      proto_members=true,
+      proto_findall=true,
+      proto_objectof=true,
+      proto_inherit=true,
+      proto_clone=true,
+   }
+
+   for name,value in O4:proto_members() do
+      assert(expected_members[name])
+      expected_members[name] = nil
+   end
+
+   assert(not next(expected_members))
+end
+
+do
+   local expected_names = {}
+
+   expected_names["O4"] = true
+   expected_names["Still Object"] = true
+
+   for object,value in O4:proto_findall("name") do
+      assert(expected_names[value])
+      expected_names[value] = nil
+   end
+
+   assert(not next(expected_names))
+end
+
+assert(O2:proto_isa(Object))
+assert(O3:proto_isa(Object))
+assert(O3:proto_isa(O2))
+assert(O4:proto_isa(Object))
+assert(O4:proto_isa(O3))
+assert(O4:proto_isa(O2))