summaryrefslogtreecommitdiff
path: root/honey/ecs/ecs.test.lua
diff options
context:
space:
mode:
Diffstat (limited to 'honey/ecs/ecs.test.lua')
-rw-r--r--honey/ecs/ecs.test.lua206
1 files changed, 124 insertions, 82 deletions
diff --git a/honey/ecs/ecs.test.lua b/honey/ecs/ecs.test.lua
index 74e27dc..5d0b89e 100644
--- a/honey/ecs/ecs.test.lua
+++ b/honey/ecs/ecs.test.lua
@@ -17,59 +17,6 @@ end
local ecs = require 'ecs'
---===== Component tests =====--
-
-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)
-
-
--===== EntityDb tests =====--
local EntityDb = ecs.EntityDb
@@ -233,47 +180,142 @@ end)
local SystemDb = ecs.SystemDb
-test("addSystem() correctly sorts systems", function()
+test("addSystems() correctly sorts systems", function()
local sdb = SystemDb(nil)
local str = ""
- sdb:addSystem(function () return {
- update=function(self, dt) str = str .. "c" end,
- priority = 3,
- } end)
- sdb:addSystem{
- update=function(self, dt) str = "a" end,
- priority = 1,
- }
- sdb:addSystem{
- update=function(self, dt) str = str .. "b" end,
- priority = 2,
- }
+
+ local c = ecs.System("c", function(db, dt, params)
+ str = str .. "c"
+ end)
+
+ local a = ecs.System("a", function(db, dt, params)
+ str = str .. "a"
+ end)
+
+ local b = ecs.System("b", function(db, dt, params)
+ str = str .. "b"
+ end)
+
+ b:addDependency(a)
+ c:addDependency(b)
+
+ sdb:addSystems({c, a, b}, nil)
+
sdb:update(0)
assert(str == "abc")
end)
-test("removeSystem() correctly handles things", function()
+test("addSystems() fails on dependency loop", function()
+ local a = ecs.System("a", nil)
+ local b = ecs.System("b", nil)
+ local c = ecs.System("c", nil)
+
+ a:addDependency(c)
+ b:addDependency(a)
+ c:addDependency(b)
+
+ sdb = SystemDb(nil)
+ local success = pcall(sdb,addSystems, sdb, {c, a, b}, nil)
+ assert(success == false)
+end)
+
+
+test("addSystems() correctly inserts additional systems", function()
+ local str = ""
local sdb = SystemDb(nil)
+
+ local c = ecs.System("c", function(db, dt, params)
+ str = str .. "c"
+ end)
+ local a = ecs.System("a", function(db, dt, params)
+ str = str .. "a"
+ end)
+ local b = ecs.System("b", function(db, dt, params)
+ str = str .. "b"
+ end)
+
+ b:addDependency(a)
+ c:addDependency(b)
+
+ sdb:addSystems({c, a, b}, nil)
+
+ local d = ecs.System("d", function(db, dt, params)
+ str = str .. "d"
+ end)
+ local e = ecs.System("e", function(db, dt, params)
+ str = str .. "e"
+ end)
+
+ d:addDependency(c)
+ e:addDependency(d)
+
+ sdb:addSystems({e, d}, nil)
+ sdb:update(0)
+ assert(str == "abcde")
+end)
+
+
+test("addSystems() correctly handles dangling dependencies", function()
local str = ""
- sdb:addSystem(function () return {
- update=function(self, dt) str = str .. "c" end,
- priority = 3,
- } end)
- sdb:addSystem{
- update=function(self, dt) str = "a" end,
- priority = 1,
- }
- local id = sdb:addSystem{
- update=function(self, dt) str = str .. "b" end,
- priority = 2,
- }
+ local sdb = SystemDb(nil)
+
+ local c = ecs.System("c", function(db, dt, params)
+ str = str .. "c"
+ end)
+ local a = ecs.System("a", function(db, dt, params)
+ str = str .. "a"
+ end)
+ local b = ecs.System("b", function(db, dt, params)
+ str = str .. "b"
+ end)
+
+ b:addDependency(a)
+ c:addDependency(b)
+
+ local d = ecs.System("d", function(db, dt, params)
+ str = str .. "d"
+ end)
+ local e = ecs.System("e", function(db, dt, params)
+ str = str .. "e"
+ end)
+
+ d:addDependency(c)
+ e:addDependency(d)
+
+ sdb:addSystems({e, d}, nil)
+
+ local success = pcall(sdb.update, sdb, nil)
+ assert(success == false)
+
+ sdb:addSystems({c, a, b}, nil)
+
sdb:update(0)
- assert(str == "abc")
+ assert(str == "abcde")
+end)
+
+
+
+
+test("removeSystem() correctly handles things", function()
+ local sdb = SystemDb(nil)
+ local num = 1
+
+
+ local two = ecs.System("x2", function(db, dt, p) num = num*2 end)
+ local three = ecs.System("x3", function(db, dt, p) num = num*3 end)
+ local five = ecs.System("x5", function(db, dt, p) num = num*5 end)
+
+ sdb:addSystems({two, three, five}, nil)
+
+ sdb:update(0)
+ assert(num == 30)
+
+ num = 1
- sdb:removeSystem(id)
+ sdb:removeSystems({three})
sdb:update(1)
- assert(str == "ac")
+ assert(num == 10)
end)