From 0431c82ff0a936def69c509d2a3410fe5b438cdd Mon Sep 17 00:00:00 2001 From: makspll Date: Sun, 23 Mar 2025 19:49:16 +0000 Subject: [PATCH] chore: add dynamic component query tests --- .../tests/query/dynamic_component_query.lua | 20 +++++++++++++++++++ .../tests/query/dynamic_component_query.rhai | 17 ++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 assets/tests/query/dynamic_component_query.lua create mode 100644 assets/tests/query/dynamic_component_query.rhai diff --git a/assets/tests/query/dynamic_component_query.lua b/assets/tests/query/dynamic_component_query.lua new file mode 100644 index 0000000000..65375c79ec --- /dev/null +++ b/assets/tests/query/dynamic_component_query.lua @@ -0,0 +1,20 @@ +local entity_a = world.spawn() +local NewComponent = world.register_new_component("NewComponent") + +world.add_default_component(entity_a, NewComponent) + +local found_entities = {} +for i,result in pairs(world.query():component(NewComponent):build()) do + table.insert(found_entities, result:entity()) +end + +assert(#found_entities == 1, "Expected 1 entities, got " .. #found_entities) + +expected_entities = { + entity_a +} + +for i, entity in ipairs(found_entities) do + assert(entity:index() == expected_entities[i]:index(), "Expected entity " .. expected_entities[i]:index() .. " but got " .. entity:index()) +end + diff --git a/assets/tests/query/dynamic_component_query.rhai b/assets/tests/query/dynamic_component_query.rhai new file mode 100644 index 0000000000..b89c9fafe1 --- /dev/null +++ b/assets/tests/query/dynamic_component_query.rhai @@ -0,0 +1,17 @@ +let entity_a = world.spawn_.call(); +let NewComponent = world.register_new_component.call("NewComponent"); + +world.add_default_component.call(entity_a, NewComponent); + +let found_entities = []; +for (result, i) in world.query.call().component.call(NewComponent).build.call() { + found_entities.push(result.entity.call()); +} + +assert(found_entities.len == 1, "Expected 1 entities, got " + found_entities.len); + +let expected_entities = [entity_a]; + +for (entity, i) in found_entities { + assert(entity.index.call() == expected_entities[i].index.call(), "Expected entity " + expected_entities[i].index.call() + " but got " + entity.index.call()); +} \ No newline at end of file