add small testsuite
[luaproto] / prototest.lua
1
2 local Object = require "proto"
3
4 Object.name="Object"
5
6 O2 = Object:proto_clone { o2 = "o2"}
7 assert( O2.o2 == "o2")
8 assert( Object.o2 == nil)
9
10
11 O3 = Object:proto_clone {o3 = "o3"}
12 assert( O3.o3 == "o3")
13 O3:proto_inherit(O2)
14 assert( O3.o2 == "o2")
15 assert( O3:proto_objectof('o2') == O2)
16
17
18 O4 = O3:proto_clone({o4 = "o4", "name"})
19 assert( O4.o4 == "o4")
20
21 O4.name = "O4"
22 Object.name = "Still Object"
23
24 assert( O4.name == "O4")
25 assert( Object.name == "Still Object")
26
27
28 assert(O4:proto_objectof ("name") == O4)
29
30
31 do
32    local expected_prototypes = {}
33
34    expected_prototypes[Object] = true
35    expected_prototypes[O2] = true
36    expected_prototypes[O3] = true
37    expected_prototypes[O4] = true
38
39    for prototype in O4:proto_prototypes() do
40       assert(expected_prototypes[prototype])
41       expected_prototypes[prototype] = nil
42    end
43    assert(not next(expected_prototypes))
44 end
45
46 do
47    local expected_members = {
48       __index=true,
49       __proto_parents=true,
50       __metatable=true,
51       name=true,
52       o2=true,
53       o3=true,
54       o4=true,
55       proto_isa=true,
56       proto_prototypes=true,
57       proto_members=true,
58       proto_findall=true,
59       proto_objectof=true,
60       proto_inherit=true,
61       proto_clone=true,
62    }
63
64    for name,value in O4:proto_members() do
65       assert(expected_members[name])
66       expected_members[name] = nil
67    end
68
69    assert(not next(expected_members))
70 end
71
72 do
73    local expected_names = {}
74
75    expected_names["O4"] = true
76    expected_names["Still Object"] = true
77
78    for object,value in O4:proto_findall("name") do
79       assert(expected_names[value])
80       expected_names[value] = nil
81    end
82
83    assert(not next(expected_names))
84 end
85
86 assert(O2:proto_isa(Object))
87 assert(O3:proto_isa(Object))
88 assert(O3:proto_isa(O2))
89 assert(O4:proto_isa(Object))
90 assert(O4:proto_isa(O3))
91 assert(O4:proto_isa(O2))