#include #include #include #define CATCH_CONFIG_MAIN #include #include "../zecsy.hpp" using namespace zecsy; TEST_CASE("Create a single entity and verify its existence") { world w; auto e = w.make_entity(); REQUIRE(w.is_alive(e)); } TEST_CASE("Destroy an entity and ensure it no longer exists in the world") { world w; auto e = w.make_entity(); w.destroy_entity(e); REQUIRE_FALSE(w.is_alive(e)); } TEST_CASE("Entity #0 should be reserved and never used") { world w; auto e = w.make_entity(); REQUIRE(e != 0); REQUIRE_FALSE(w.is_alive(0)); } struct ChoosenOne { }; TEST_CASE("Entity shouldn't have a component that wasn't attached to it") { world w; auto e = w.make_entity(); REQUIRE_FALSE(w.has(e)); } TEST_CASE("Attempt of getting non-owned component should throw") { world w; auto e = w.make_entity(); REQUIRE_THROWS(w.get(e)); } TEST_CASE("Attach a simple component to an entity and verify it is correctly associated") { world w; auto e1 = w.make_entity(); w.set(e1, ChoosenOne{}); REQUIRE(w.has(e1)); auto e2 = w.make_entity(); w.set(e2, ChoosenOne{}); REQUIRE(w.has(e2)); } struct Name { std::string value; }; TEST_CASE("Retrieve a component from an entity and verify its data matches what was set") { world w; auto e = w.make_entity(); w.set(e, Name{"zecsy!"}); REQUIRE(w.get(e).value == "zecsy!"); w.get(e).value = "super-zecsy!"; REQUIRE(w.get(e).value == "super-zecsy!"); } TEST_CASE("Remove a component from an entity and verify it is no longer attached") { world w; auto e = w.make_entity(); w.set(e, ChoosenOne{}); REQUIRE_NOTHROW(w.remove(e)); REQUIRE_FALSE(w.has(e)); w.set(e, ChoosenOne{}); REQUIRE_NOTHROW(w.remove(e)); REQUIRE_FALSE(w.has(e)); } TEST_CASE("Addresses of removed components should be reused") { world w; std::vector entities; std::vector addr; const int N = 4; for(int i = 0; i < 2; ++i) { for(int j = 0; j < N; ++j) { entities.emplace_back(w.make_entity()); w.set(entities.back()); } if(addr.empty()) { for(int j = 0; j < N; ++j) { addr.emplace_back(&w.get(entities[j])); } } else { for(int j = 0; j < N; ++j) { REQUIRE(&w.get(entities[j]) == addr[j]); } } for(auto e: entities) { w.remove(e); } entities.clear(); } } TEST_CASE("Attach multiple components to an entity and verify all are correctly stored and retrievable") { world w; auto e = w.make_entity(); w.set(e, ChoosenOne{}, Name{"zecsy"}); REQUIRE(w.has(e)); w.remove(e); REQUIRE_FALSE(w.has(e)); REQUIRE_FALSE(w.has(e)); REQUIRE_FALSE(w.has(e)); } TEST_CASE("Create a simple system that processes entities with a specific component and verify it executes correctly") { struct Component { int value = 0; }; world w; auto e0 = w.make_entity(), e1 = w.make_entity(); w.set(e0); //or e0.set(Component{}) w.set(e1, Component{20}); REQUIRE(w.get(e0).value == 0); REQUIRE(w.get(e1).value == 20); /* * Really wanna deduce it to w.query([](Component&){}), * but I have some troubles with it */ w.query([](Component& c) { c.value++; }); REQUIRE(w.get(e0).value == 1); REQUIRE(w.get(e1).value == 21); } TEST_CASE("Test a system’s ability to query and process only entities with a specific combination of components") { struct C0 { int value = 0; }; struct C1 { int value = 10; }; world w; auto e0 = w.make_entity(); w.set(e0, C0{}, C1{}); auto e1 = w.make_entity(); w.set(e1, C0{}); auto e2 = w.make_entity(); w.set(e2, C1{}); int count = 0; REQUIRE(w.get(e0).value == 0); REQUIRE(w.get(e0).value == 10); w.query([&count](C0& c0, C1& c1) { c0.value++; c1.value++; }); REQUIRE(w.get(e0).value == 1); REQUIRE(w.get(e0).value == 11); REQUIRE(w.get(e1).value == 0); REQUIRE(w.get(e2).value == 10); REQUIRE_FALSE(w.has(e1)); REQUIRE_FALSE(w.has(e2)); }