1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
local function test(msg, f)
local success, error = pcall(f)
if success then
print(msg .. "\t\t[OK]")
else
print(msg .. "\t\t[FAIL]")
print(error)
end
end
local ecs = require 'ecs'
local Component = ecs.Component
test("factories work as expected", function()
local factory = Component.newFactory("health", { percent=100 })
local comp1 = factory()
assert(comp1.__type == "health", "bad component type for comp1")
assert(comp1.percent == 100, "bat percent for comp1")
local comp2 = factory{ percent=50 }
assert(comp2.__type == "health", "bad component type for comp2")
assert(comp2.percent == 50, "bad percent for comp2")
local success = pcall(function()
comp2.dne = 5
end)
assert(not success, "incorrectly succeeded in setting comp2.dne")
local success = pcall(function()
local comp3 = factory{ percent = 44, something = 2 }
end)
assert(not success, "incorrectly succeeded in creating comp3")
end)
test("components serialize as expected", function()
local position = Component.newFactory("position", { x=0, y=0, z=0 })
local comp = position{x=10, y=15, z=10}
local str = tostring(comp)
local tbl = (loadstring("return " .. str))()
assert(tbl.__type == "position", "bad type")
assert(tbl.x == 10, "bad x")
assert(tbl.y == 15, "bad y")
assert(tbl.z == 10, "bad z")
end)
test("components serialize successfully with subcomponents", function()
local position = Component.newFactory("position", { x=0, y=0, z=0 })
local player = Component.newFactory("player", { name="", position={} })
local p = player{ name="hannah", position=position{x=10, y=9, z=8} }
local tbl = (loadstring("return " .. tostring(p)))()
assert(tbl.__type == "player")
assert(tbl.name == "hannah")
assert(tbl.position.__type == "position")
assert(tbl.position.x == 10)
assert(tbl.position.y == 9)
assert(tbl.position.z == 8)
end)
|