|
| 1 | +#include "TestSupport.h" |
| 2 | + |
| 3 | +#include "engine/WorldQuery.h" |
| 4 | + |
| 5 | +namespace { |
| 6 | + |
| 7 | +struct Position { |
| 8 | + float x{0.0f}; |
| 9 | + float y{0.0f}; |
| 10 | +}; |
| 11 | + |
| 12 | +struct Velocity { |
| 13 | + float vx{0.0f}; |
| 14 | + float vy{0.0f}; |
| 15 | +}; |
| 16 | + |
| 17 | +} // namespace |
| 18 | + |
| 19 | +SC_TEST(WorldQuery_ViewFiltersEntitiesBySignature) { |
| 20 | + safecrowd::engine::EcsCore core; |
| 21 | + safecrowd::engine::WorldQuery query{core}; |
| 22 | + |
| 23 | + const auto e1 = core.createEntity(); |
| 24 | + const auto e2 = core.createEntity(); |
| 25 | + const auto e3 = core.createEntity(); |
| 26 | + |
| 27 | + core.addComponent(e1, Position{}); |
| 28 | + core.addComponent(e1, Velocity{}); |
| 29 | + core.addComponent(e2, Position{}); |
| 30 | + core.addComponent(e3, Velocity{}); |
| 31 | + |
| 32 | + const auto result = query.view<Position, Velocity>(); |
| 33 | + SC_EXPECT_EQ(result.size(), std::size_t{1}); |
| 34 | + SC_EXPECT_TRUE(result[0] == e1); |
| 35 | +} |
| 36 | + |
| 37 | +SC_TEST(WorldQuery_ViewReturnsEmptyIfTypeNotRegistered) { |
| 38 | + safecrowd::engine::EcsCore core; |
| 39 | + safecrowd::engine::WorldQuery query{core}; |
| 40 | + |
| 41 | + static_cast<void>(core.createEntity()); |
| 42 | + |
| 43 | + const auto result = query.view<Position>(); |
| 44 | + SC_EXPECT_TRUE(result.empty()); |
| 45 | +} |
| 46 | + |
| 47 | +SC_TEST(WorldQuery_ViewExcludesDestroyedEntities) { |
| 48 | + safecrowd::engine::EcsCore core; |
| 49 | + safecrowd::engine::WorldQuery query{core}; |
| 50 | + |
| 51 | + const auto e1 = core.createEntity(); |
| 52 | + core.addComponent(e1, Position{}); |
| 53 | + core.destroyEntity(e1); |
| 54 | + |
| 55 | + const auto e2 = core.createEntity(); |
| 56 | + core.addComponent(e2, Position{}); |
| 57 | + |
| 58 | + const auto result = query.view<Position>(); |
| 59 | + SC_EXPECT_EQ(result.size(), std::size_t{1}); |
| 60 | + SC_EXPECT_TRUE(result[0] == e2); |
| 61 | +} |
| 62 | + |
| 63 | +SC_TEST(WorldQuery_ContainsReflectsComponentPresence) { |
| 64 | + safecrowd::engine::EcsCore core; |
| 65 | + safecrowd::engine::WorldQuery query{core}; |
| 66 | + |
| 67 | + const auto e = core.createEntity(); |
| 68 | + core.addComponent(e, Position{}); |
| 69 | + |
| 70 | + SC_EXPECT_TRUE(query.contains<Position>(e)); |
| 71 | + SC_EXPECT_TRUE(!query.contains<Velocity>(e)); |
| 72 | +} |
| 73 | + |
| 74 | +SC_TEST(WorldQuery_GetReturnsComponentRef) { |
| 75 | + safecrowd::engine::EcsCore core; |
| 76 | + safecrowd::engine::WorldQuery query{core}; |
| 77 | + |
| 78 | + const auto e = core.createEntity(); |
| 79 | + core.addComponent(e, Position{3.0f, 4.0f}); |
| 80 | + |
| 81 | + const auto& pos = query.get<Position>(e); |
| 82 | + SC_EXPECT_NEAR(pos.x, 3.0f, 1e-6); |
| 83 | + SC_EXPECT_NEAR(pos.y, 4.0f, 1e-6); |
| 84 | +} |
0 commit comments