diff options
Diffstat (limited to 'honey/ecs.test.lua')
-rw-r--r-- | honey/ecs.test.lua | 164 |
1 files changed, 162 insertions, 2 deletions
diff --git a/honey/ecs.test.lua b/honey/ecs.test.lua index 9f7519b..fbdba30 100644 --- a/honey/ecs.test.lua +++ b/honey/ecs.test.lua @@ -1,5 +1,5 @@ local function test(msg, f) - local success, error = pcall(f) + local success, error = xpcall(f, debug.traceback) if success then print(msg .. "\t\t[OK]") else @@ -12,8 +12,9 @@ end local ecs = require 'ecs' -local Component = ecs.Component +--===== Component tests =====-- +local Component = ecs.Component test("factories work as expected", function() local factory = Component.newFactory("health", { percent=100 }) @@ -62,3 +63,162 @@ test("components serialize successfully with subcomponents", function() assert(tbl.position.y == 9) assert(tbl.position.z == 8) end) + + +--===== EntityDb tests =====-- + +local EntityDb = ecs.EntityDb + + +test("EntityDb.createEntity() always returns a new id", function() + local db = EntityDb() + + local ids = {} + for i=1,100 do + local id = db:createEntity() + assert(ids[id] == nil, "id was already returned!") + ids[id] = true + end +end) + + +test("EntityDb.queryComponent() gets all entities with a given component", function() + local db = EntityDb() + + local ids = {} + for i=1,100 do + local id = db:createEntity() + if i%2==0 then + ids[id] = 5*i + db:addComponent(id, "number", 5*i) + end + end + + local query = db:queryComponent("number") + local count = 0 + for id, number in pairs(query) do + count = count + 1 + assert(number == ids[id]) + end + assert(count == 50) +end) + + +test("EntityDb.queryEntity() gets all components associated with an entity", function() + local db = EntityDb() + + local entity + for i=1,100 do + local id = db:createEntity() + if i%2 == 0 then db:addComponent(id, "number", 2) end + if i%3 == 0 then db:addComponent(id, "string", "hello") end + if i%5 == 0 then db:addComponent(id, "number2", 4) end + if i%7 == 0 then db:addComponent(id, "string2", "world") end + if i == 30 then entity=id end + end + + local query = db:queryEntity(entity) + assert(query.number == 2) + assert(query.string == "hello") + assert(query.number2 == 4) + assert(query.string2 == nil) +end) + + +test("EntityDb.removeComponent() removes components correctly", function() + local db = EntityDb() + + local id = db:createEntity() + db:addComponent(id, "number", 2) + db:addComponent(id, "string", "hello") + db:addComponent(id, "number2", 4) + db:addComponent(id, "string2", "world") + + local query = db:queryEntity(id) + assert(query.number == 2) + assert(query.string == "hello") + assert(query.number2 == 4) + assert(query.string2 == "world") + + db:removeComponent(id, "string2") + query = db:queryEntity(id) + assert(query.number == 2) + assert(query.string == "hello") + assert(query.number2 == 4) + assert(query.string2 == nil) + + db:removeComponent(id, "number2") + query = db:queryEntity(id) + assert(query.number == 2) + assert(query.string == "hello") + assert(query.number2 == nil) + assert(query.string2 == nil) + + db:removeComponent(id, "string") + query = db:queryEntity(id) + assert(query.number == 2) + assert(query.string == nil) + assert(query.number2 == nil) + assert(query.string2 == nil) + + db:removeComponent(id, "number") + query = db:queryEntity(id) + assert(query.number == nil) + assert(query.string == nil) + assert(query.number2 == nil) + assert(query.string2 == nil) +end) + + +test("EntityDb.removeComponent() deletes component table when empty", function() + local db = EntityDb() + local id1 = db:createEntity() + local id2 = db:createEntity() + db:addComponent(id1, "number", 2) + db:addComponent(id2, "number", 3) + + assert(db.components.number ~= nil) + db:removeComponent(id1, "number") + assert(db.components.number ~= nil) + db:removeComponent(id2, "number") + assert(db.components.number == nil) +end) + + +test("EntityDb.removeComponent() does nothing if the component is not present", function() + local db = EntityDb() + local id1 = db:createEntity() + local id2 = db:createEntity() + db:addComponent(id1, "number", 2) + db:addComponent(id2, "number", 3) + + assert(db.components.number ~= nil) + db:removeComponent(id1, "number") + assert(db.components.number ~= nil) + db:removeComponent(id1, "number") + assert(db.components.number ~= nil) + + db:removeComponent(id2, "number") + assert(db.components.number == nil) + +end) + + +test("EntityDb.deleteEntity() correctly removes an entity", function() + local db = EntityDb() + local id1 = db:createEntity() + local id2 = db:createEntity() + db:addComponent(id1, "number", 2) + db:addComponent(id2, "number", 3) + + local query = db:queryComponent("number") + assert(query[id1] and query[id2]) + db:deleteEntity(id1) + query = db:queryComponent("number") + assert(query[id1] == nil) + assert(query[id2] ~= nil) + + assert(false == pcall(function() + local query = db:queryEntity(id1) + end)) +end) |