mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 09:30:03 +00:00
Add tests
This commit is contained in:
parent
3bb4872199
commit
0f74a033ed
1 changed files with 88 additions and 38 deletions
126
tests/world.luau
126
tests/world.luau
|
@ -22,10 +22,11 @@ local function CHECK_NO_ERR<T...>(s: string, fn: (T...) -> (), ...: T...)
|
||||||
end
|
end
|
||||||
local N = 10
|
local N = 10
|
||||||
|
|
||||||
|
type World = jecs.WorldShim
|
||||||
|
|
||||||
TEST("world", function()
|
TEST("world", function()
|
||||||
do
|
do CASE("should be iterable")
|
||||||
CASE("should be iterable")
|
local world = jecs.World.new() :: World
|
||||||
local world = jecs.World.new()
|
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
local eA = world:entity()
|
local eA = world:entity()
|
||||||
|
@ -36,9 +37,7 @@ TEST("world", function()
|
||||||
world:set(eAB, A, true)
|
world:set(eAB, A, true)
|
||||||
world:set(eAB, B, true)
|
world:set(eAB, B, true)
|
||||||
|
|
||||||
local count = 0
|
|
||||||
for id, data in world do
|
for id, data in world do
|
||||||
count += 1
|
|
||||||
if id == eA then
|
if id == eA then
|
||||||
CHECK(data[A] == true)
|
CHECK(data[A] == true)
|
||||||
CHECK(data[B] == nil)
|
CHECK(data[B] == nil)
|
||||||
|
@ -50,14 +49,79 @@ TEST("world", function()
|
||||||
CHECK(data[B] == true)
|
CHECK(data[B] == true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- components are registered in the entity index as well
|
|
||||||
-- so this test has to add 2 to account for them
|
|
||||||
CHECK(count == 3 + 2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should remove its components")
|
||||||
CASE("should query all matching entities")
|
local world = jecs.World.new() :: World
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
|
||||||
|
world:set(e, A, true)
|
||||||
|
world:set(e, B, true)
|
||||||
|
|
||||||
|
CHECK(world:get(e, A))
|
||||||
|
CHECK(world:get(e, B))
|
||||||
|
|
||||||
|
world:clear(e)
|
||||||
|
CHECK(world:get(e, A) == nil)
|
||||||
|
CHECK(world:get(e, B) == nil)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE("iterator should not drain the query")
|
||||||
|
local world = jecs.World.new() :: World
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local eA = world:entity()
|
||||||
|
world:set(eA, A, true)
|
||||||
|
local eB = world:entity()
|
||||||
|
world:set(eB, B, true)
|
||||||
|
local eAB = world:entity()
|
||||||
|
world:set(eAB, A, true)
|
||||||
|
world:set(eAB, B, true)
|
||||||
|
|
||||||
|
local q = world:query(A)
|
||||||
|
|
||||||
|
local i = 0
|
||||||
|
local j = 0
|
||||||
|
for _ in q do
|
||||||
|
i+=1
|
||||||
|
end
|
||||||
|
for _ in q do
|
||||||
|
j+=1
|
||||||
|
end
|
||||||
|
CHECK(i == j)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE("should be able to get next results")
|
||||||
|
local world = jecs.World.new() :: World
|
||||||
|
world:component()
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local eA = world:entity()
|
||||||
|
world:set(eA, A, true)
|
||||||
|
local eB = world:entity()
|
||||||
|
world:set(eB, B, true)
|
||||||
|
local eAB = world:entity()
|
||||||
|
world:set(eAB, A, true)
|
||||||
|
world:set(eAB, B, true)
|
||||||
|
|
||||||
|
local q = world:query(A)
|
||||||
|
|
||||||
|
local e, data = q:next()
|
||||||
|
while e do
|
||||||
|
CHECK(
|
||||||
|
if e == eA then data == true
|
||||||
|
elseif e == eAB then data == true
|
||||||
|
else false
|
||||||
|
)
|
||||||
|
e, data = q:next()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE("should query all matching entities")
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
@ -80,8 +144,7 @@ TEST("world", function()
|
||||||
CHECK(#entities == 0)
|
CHECK(#entities == 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should query all matching entities when irrelevant component is removed")
|
||||||
CASE("should query all matching entities when irrelevant component is removed")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
@ -110,8 +173,7 @@ TEST("world", function()
|
||||||
CHECK(added == N)
|
CHECK(added == N)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should query all entities without B")
|
||||||
CASE("should query all entities without B")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
@ -135,8 +197,7 @@ TEST("world", function()
|
||||||
CHECK(#entities == 0)
|
CHECK(#entities == 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should allow setting components in arbitrary order")
|
||||||
CASE("should allow setting components in arbitrary order")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
|
|
||||||
local Health = world:entity()
|
local Health = world:entity()
|
||||||
|
@ -149,8 +210,7 @@ TEST("world", function()
|
||||||
CHECK(world:get(id, Poison) == 5)
|
CHECK(world:get(id, Poison) == 5)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should allow deleting components")
|
||||||
CASE("should allow deleting components")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
|
|
||||||
local Health = world:entity()
|
local Health = world:entity()
|
||||||
|
@ -171,8 +231,7 @@ TEST("world", function()
|
||||||
CHECK(world:get(id1, Health) == 50)
|
CHECK(world:get(id1, Health) == 50)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should allow remove that doesn't exist on entity")
|
||||||
CASE("should allow remove that doesn't exist on entity")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
|
|
||||||
local Health = world:entity()
|
local Health = world:entity()
|
||||||
|
@ -186,8 +245,7 @@ TEST("world", function()
|
||||||
CHECK(world:get(id, Health) == 50)
|
CHECK(world:get(id, Health) == 50)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should increment generation")
|
||||||
CASE("should increment generation")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local e = world:entity()
|
local e = world:entity()
|
||||||
CHECK(ECS_ID(e) == 1 + jecs.Rest)
|
CHECK(ECS_ID(e) == 1 + jecs.Rest)
|
||||||
|
@ -197,8 +255,7 @@ TEST("world", function()
|
||||||
CHECK(ECS_GENERATION(e) == 1) -- 1
|
CHECK(ECS_GENERATION(e) == 1) -- 1
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should get alive from index in the dense array")
|
||||||
CASE("should get alive from index in the dense array")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local _e = world:entity()
|
local _e = world:entity()
|
||||||
local e2 = world:entity()
|
local e2 = world:entity()
|
||||||
|
@ -213,8 +270,7 @@ TEST("world", function()
|
||||||
CHECK(ECS_PAIR_OBJECT(world.entityIndex, pair) == e3)
|
CHECK(ECS_PAIR_OBJECT(world.entityIndex, pair) == e3)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should allow querying for relations")
|
||||||
CASE("should allow querying for relations")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local Eats = world:entity()
|
local Eats = world:entity()
|
||||||
local Apples = world:entity()
|
local Apples = world:entity()
|
||||||
|
@ -227,8 +283,7 @@ TEST("world", function()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should allow wildcards in queries")
|
||||||
CASE("should allow wildcards in queries")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local Eats = world:entity()
|
local Eats = world:entity()
|
||||||
local Apples = world:entity()
|
local Apples = world:entity()
|
||||||
|
@ -247,8 +302,7 @@ TEST("world", function()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should match against multiple pairs")
|
||||||
CASE("should match against multiple pairs")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local Eats = world:entity()
|
local Eats = world:entity()
|
||||||
local Apples = world:entity()
|
local Apples = world:entity()
|
||||||
|
@ -280,8 +334,7 @@ TEST("world", function()
|
||||||
CHECK(count == 1)
|
CHECK(count == 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should only relate alive entities")
|
||||||
CASE("should only relate alive entities")
|
|
||||||
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local Eats = world:entity()
|
local Eats = world:entity()
|
||||||
|
@ -308,8 +361,7 @@ TEST("world", function()
|
||||||
CHECK(world:get(bob, ECS_PAIR(Eats, Apples)) == nil)
|
CHECK(world:get(bob, ECS_PAIR(Eats, Apples)) == nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should error when setting invalid pair")
|
||||||
CASE("should error when setting invalid pair")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
local Eats = world:entity()
|
local Eats = world:entity()
|
||||||
local Apples = world:entity()
|
local Apples = world:entity()
|
||||||
|
@ -320,8 +372,7 @@ TEST("world", function()
|
||||||
world:set(bob, ECS_PAIR(Eats, Apples), "bob eats apples")
|
world:set(bob, ECS_PAIR(Eats, Apples), "bob eats apples")
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do CASE("should find target for ChildOf")
|
||||||
CASE("should find target for ChildOf")
|
|
||||||
local world = jecs.World.new()
|
local world = jecs.World.new()
|
||||||
|
|
||||||
local ChildOf = world:component()
|
local ChildOf = world:component()
|
||||||
|
@ -343,7 +394,6 @@ TEST("world", function()
|
||||||
|
|
||||||
local count = 0
|
local count = 0
|
||||||
for _, name in world:query(Name, ECS_PAIR(ChildOf, alice)) do
|
for _, name in world:query(Name, ECS_PAIR(ChildOf, alice)) do
|
||||||
print(name)
|
|
||||||
count += 1
|
count += 1
|
||||||
end
|
end
|
||||||
CHECK(count == 2)
|
CHECK(count == 2)
|
||||||
|
|
Loading…
Reference in a new issue