From 68d17cd0a2b7b7c110a76e6ffc115cb93ffbad6b Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 17 May 2021 17:22:09 +0200 Subject: [PATCH] Adding tests --- lib/wal/CMakeLists.txt | 2 +- .../Component/Movable/MovableComponent.cpp | 2 +- .../sources/System/Movable/MovableSystem.cpp | 4 +- lib/wal/sources/Wal.cpp | 14 +++---- lib/wal/sources/Wal.hpp | 41 +++++++++++++------ lib/wal/tests/EngineTests.cpp | 36 ++++++++++++++++ lib/wal/tests/EntityTests.cpp | 5 +-- lib/wal/tests/tests.hpp | 9 ---- sources/main.cpp | 2 +- 9 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 lib/wal/tests/EngineTests.cpp delete mode 100644 lib/wal/tests/tests.hpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index 4552fd54..36267b7b 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -30,7 +30,7 @@ target_include_directories(wal PUBLIC sources) add_executable(wal_tests tests/EntityTests.cpp - tests/MainTest.cpp tests/tests.hpp) + tests/MainTest.cpp tests/tests.hpp tests/EngineTests.cpp) target_link_libraries(wal_tests PRIVATE wal) find_package(Catch2 REQUIRED) diff --git a/lib/wal/sources/Component/Movable/MovableComponent.cpp b/lib/wal/sources/Component/Movable/MovableComponent.cpp index b989de28..a86a69ab 100644 --- a/lib/wal/sources/Component/Movable/MovableComponent.cpp +++ b/lib/wal/sources/Component/Movable/MovableComponent.cpp @@ -4,7 +4,7 @@ #include "MovableComponent.hpp" -namespace WAL::Movable +namespace WAL { MovableComponent::MovableComponent(Entity &entity) : Component(entity) diff --git a/lib/wal/sources/System/Movable/MovableSystem.cpp b/lib/wal/sources/System/Movable/MovableSystem.cpp index 108b4c93..600f7c8e 100644 --- a/lib/wal/sources/System/Movable/MovableSystem.cpp +++ b/lib/wal/sources/System/Movable/MovableSystem.cpp @@ -19,8 +19,8 @@ namespace WAL auto &movable = entity.getComponent(); auto &position = entity.getComponent(); - position.position += movable._velocity * WAL::timestep.count(); - movable._velocity = movable._acceleration * WAL::timestep.count(); + position.position += movable._velocity * Wal::timestep.count(); + movable._velocity = movable._acceleration * Wal::timestep.count(); movable._acceleration = Vector3f(); } } \ No newline at end of file diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index de82cddc..67a1891e 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -9,14 +9,14 @@ using namespace std::chrono_literals; namespace WAL { - std::chrono::nanoseconds WAL::timestep = 8ms; + std::chrono::nanoseconds Wal::timestep = 8ms; - SceneManager &WAL::getSceneManger() + SceneManager &Wal::getSceneManger() { return this->_scenes; } - void WAL::run() + void Wal::run() { auto lastTick = std::chrono::steady_clock::now(); std::chrono::nanoseconds dtime(0); @@ -27,15 +27,15 @@ namespace WAL lastTick = now; this->_update(dtime); - while (dtime > WAL::timestep) { - dtime -= WAL::timestep; + while (dtime > Wal::timestep) { + dtime -= Wal::timestep; this->_fixedUpdate(); } this->_renderer->render(); } } - void WAL::_update(std::chrono::nanoseconds dtime) + void Wal::_update(std::chrono::nanoseconds dtime) { auto &entities = this->_scenes.getCurrent().getEntities(); @@ -50,7 +50,7 @@ namespace WAL } } - void WAL::_fixedUpdate() + void Wal::_fixedUpdate() { auto &entities = this->_scenes.getCurrent().getEntities(); diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index d5e9d797..c097436f 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -18,7 +18,7 @@ namespace WAL { //! @brief The main WAL class, it is used to setup and run the ECS. - class WAL + class Wal { private: //! @brief The scene manager that allow multiple scene to work together. @@ -44,39 +44,54 @@ namespace WAL //! @brief Create a new system in place. //! @return The wal instance used to call this function is returned. This allow method chaining. template - WAL &addSystem(Types ...params) + Wal &addSystem(Types ...params) { const std::type_info &type = typeid(T); - auto &existing =std::find(this->_systems.begin(), this->_systems.end(), [&type] (auto &sys) { + auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (auto &sys) { return typeid(*sys) == type; }); if (existing != this->_systems.end()) throw DuplicateError("A system of the type \"" + std::string(type.name()) + "\" already exists."); - this->_systems.push_back(std::make_unique(params...)); + this->_systems.push_back(std::make_unique(params...)); return *this; } //! @brief Add a system by copy. //! @return The wal instance used to call this function is returned. This allow method chaining. template - WAL &addSystem(const T &system) + Wal &addSystem(const T &system) { const std::type_info &type = typeid(T); - auto &existing =std::find(this->_systems.begin(), this->_systems.end(), [&type] (auto &sys) { + auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (auto &sys) { return typeid(*sys) == type; }); if (existing != this->_systems.end()) throw DuplicateError("A system of the type \"" + std::string(type.name()) + "\" already exists."); - this->_systems.push_back(std::make_unique(system)); + this->_systems.push_back(std::make_unique(system)); return *this; } + //! @brief Get a system of a specific type + //! @tparam T the type of the system. + template + T &getSystem() + { + const std::type_info &type = typeid(T); + auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (auto &sys) { + return typeid(*sys) == type; + }); + if (existing == this->_systems.end()) + throw NotFoundError("A system of the type \"" + std::string(type.name()) + "\" could not be found."); + return *static_cast(existing->get()); + + } + //! @brief Remove a system using it's type. template - WAL &removeSystem() + Wal &removeSystem() { const std::type_info &type = typeid(T); - auto &existing =std::find(this->_systems.begin(), this->_systems.end(), [&type] (auto &sys) { + auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (auto &sys) { return typeid(*sys) == type; }); if (existing == this->_systems.end()) @@ -92,12 +107,12 @@ namespace WAL void run(); //! @brief A default constructor - WAL() = default; + Wal() = default; //! @brief A WAL can't be copy constructed - WAL(const WAL &) = delete; + Wal(const Wal &) = delete; //! @brief A default destructor - ~WAL() = default; + ~Wal() = default; //! @brief A WAL can't be assigned. - WAL &operator=(const WAL &) = delete; + Wal &operator=(const Wal &) = delete; }; } diff --git a/lib/wal/tests/EngineTests.cpp b/lib/wal/tests/EngineTests.cpp new file mode 100644 index 00000000..c6074138 --- /dev/null +++ b/lib/wal/tests/EngineTests.cpp @@ -0,0 +1,36 @@ +// +// Created by Zoe Roux on 5/17/21. +// + + +#include "Wal.hpp" +#include "System/Movable/MovableSystem.hpp" +#include + +using namespace WAL; + +TEST_CASE("Create system", "[Engine][System]") +{ + Wal wal; + wal.addSystem(); + + SECTION("Check existence") { + REQUIRE_NOTHROW(wal.getSystem()); + } + SECTION("Duplicate check") { + REQUIRE_THROWS_AS(wal.addSystem(), DuplicateError); + } + SECTION("Remove system") { + wal.removeSystem(); + REQUIRE_THROWS_AS(wal.getSystem(), NotFoundError); + REQUIRE_THROWS_AS(wal.removeSystem(), NotFoundError); + } +} + +TEST_CASE("Create system by reference", "[Engine][System]") +{ + Wal wal; + MovableSystem system; + wal.addSystem(system); + REQUIRE_THROWS_AS(wal.addSystem(), DuplicateError); +} \ No newline at end of file diff --git a/lib/wal/tests/EntityTests.cpp b/lib/wal/tests/EntityTests.cpp index 0ae20e85..6c6c4edf 100644 --- a/lib/wal/tests/EntityTests.cpp +++ b/lib/wal/tests/EntityTests.cpp @@ -2,7 +2,6 @@ // Created by Zoe Roux on 5/17/21. // -#include "tests.hpp" #include "Entity/Entity.hpp" #include "Component/Position/PositionComponent.hpp" #include @@ -17,7 +16,7 @@ TEST_CASE("Component", "[Entity]") SECTION("Check value") { auto &pos = entity.getComponent(); REQUIRE(entity.hasComponent()); - REQUIRE(pos.getPosition() == Vector3f(2, 3, 4)); + REQUIRE(pos.position == Vector3f(2, 3, 4)); } SECTION("Prevent duplicates") { REQUIRE_THROWS_AS(entity.addComponent(), DuplicateError); @@ -41,5 +40,5 @@ TEST_CASE("Add component by reference", "[Entity]") PositionComponent component(entity, 4, 5, 6); REQUIRE(&entity.addComponent(component) == &entity); - REQUIRE(entity.getComponent().getPosition() == Vector3f(4, 5, 6)); + REQUIRE(entity.getComponent().position == Vector3f(4, 5, 6)); } \ No newline at end of file diff --git a/lib/wal/tests/tests.hpp b/lib/wal/tests/tests.hpp deleted file mode 100644 index b447fd9a..00000000 --- a/lib/wal/tests/tests.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// -// Created by Zoe Roux on 5/17/21. -// - - -#pragma once - -#define private public -#define protected public \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index 53c8bb20..70491566 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -3,7 +3,7 @@ int main() { - WAL::WAL wal; + WAL::Wal wal; try { wal.run();