Adding tests

This commit is contained in:
Zoe Roux
2021-05-17 17:22:09 +02:00
parent 76ccd80df7
commit 68d17cd0a2
9 changed files with 78 additions and 37 deletions

View File

@@ -30,7 +30,7 @@ target_include_directories(wal PUBLIC sources)
add_executable(wal_tests add_executable(wal_tests
tests/EntityTests.cpp 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) target_link_libraries(wal_tests PRIVATE wal)
find_package(Catch2 REQUIRED) find_package(Catch2 REQUIRED)

View File

@@ -4,7 +4,7 @@
#include "MovableComponent.hpp" #include "MovableComponent.hpp"
namespace WAL::Movable namespace WAL
{ {
MovableComponent::MovableComponent(Entity &entity) MovableComponent::MovableComponent(Entity &entity)
: Component(entity) : Component(entity)

View File

@@ -19,8 +19,8 @@ namespace WAL
auto &movable = entity.getComponent<MovableComponent>(); auto &movable = entity.getComponent<MovableComponent>();
auto &position = entity.getComponent<PositionComponent>(); auto &position = entity.getComponent<PositionComponent>();
position.position += movable._velocity * WAL::timestep.count(); position.position += movable._velocity * Wal::timestep.count();
movable._velocity = movable._acceleration * WAL::timestep.count(); movable._velocity = movable._acceleration * Wal::timestep.count();
movable._acceleration = Vector3f(); movable._acceleration = Vector3f();
} }
} }

View File

@@ -9,14 +9,14 @@ using namespace std::chrono_literals;
namespace WAL namespace WAL
{ {
std::chrono::nanoseconds WAL::timestep = 8ms; std::chrono::nanoseconds Wal::timestep = 8ms;
SceneManager &WAL::getSceneManger() SceneManager &Wal::getSceneManger()
{ {
return this->_scenes; return this->_scenes;
} }
void WAL::run() void Wal::run()
{ {
auto lastTick = std::chrono::steady_clock::now(); auto lastTick = std::chrono::steady_clock::now();
std::chrono::nanoseconds dtime(0); std::chrono::nanoseconds dtime(0);
@@ -27,15 +27,15 @@ namespace WAL
lastTick = now; lastTick = now;
this->_update(dtime); this->_update(dtime);
while (dtime > WAL::timestep) { while (dtime > Wal::timestep) {
dtime -= WAL::timestep; dtime -= Wal::timestep;
this->_fixedUpdate(); this->_fixedUpdate();
} }
this->_renderer->render(); this->_renderer->render();
} }
} }
void WAL::_update(std::chrono::nanoseconds dtime) void Wal::_update(std::chrono::nanoseconds dtime)
{ {
auto &entities = this->_scenes.getCurrent().getEntities(); auto &entities = this->_scenes.getCurrent().getEntities();
@@ -50,7 +50,7 @@ namespace WAL
} }
} }
void WAL::_fixedUpdate() void Wal::_fixedUpdate()
{ {
auto &entities = this->_scenes.getCurrent().getEntities(); auto &entities = this->_scenes.getCurrent().getEntities();

View File

@@ -18,7 +18,7 @@
namespace WAL namespace WAL
{ {
//! @brief The main WAL class, it is used to setup and run the ECS. //! @brief The main WAL class, it is used to setup and run the ECS.
class WAL class Wal
{ {
private: private:
//! @brief The scene manager that allow multiple scene to work together. //! @brief The scene manager that allow multiple scene to work together.
@@ -44,39 +44,54 @@ namespace WAL
//! @brief Create a new system in place. //! @brief Create a new system in place.
//! @return The wal instance used to call this function is returned. This allow method chaining. //! @return The wal instance used to call this function is returned. This allow method chaining.
template<typename T, class ...Types> template<typename T, class ...Types>
WAL &addSystem(Types ...params) Wal &addSystem(Types ...params)
{ {
const std::type_info &type = typeid(T); 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; return typeid(*sys) == type;
}); });
if (existing != this->_systems.end()) if (existing != this->_systems.end())
throw DuplicateError("A system of the type \"" + std::string(type.name()) + "\" already exists."); 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<T>(params...));
return *this; return *this;
} }
//! @brief Add a system by copy. //! @brief Add a system by copy.
//! @return The wal instance used to call this function is returned. This allow method chaining. //! @return The wal instance used to call this function is returned. This allow method chaining.
template<typename T> template<typename T>
WAL &addSystem(const T &system) Wal &addSystem(const T &system)
{ {
const std::type_info &type = typeid(T); 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; return typeid(*sys) == type;
}); });
if (existing != this->_systems.end()) if (existing != this->_systems.end())
throw DuplicateError("A system of the type \"" + std::string(type.name()) + "\" already exists."); 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<T>(system));
return *this; return *this;
} }
//! @brief Get a system of a specific type
//! @tparam T the type of the system.
template<typename T>
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<T *>(existing->get());
}
//! @brief Remove a system using it's type. //! @brief Remove a system using it's type.
template<typename T> template<typename T>
WAL &removeSystem() Wal &removeSystem()
{ {
const std::type_info &type = typeid(T); 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; return typeid(*sys) == type;
}); });
if (existing == this->_systems.end()) if (existing == this->_systems.end())
@@ -92,12 +107,12 @@ namespace WAL
void run(); void run();
//! @brief A default constructor //! @brief A default constructor
WAL() = default; Wal() = default;
//! @brief A WAL can't be copy constructed //! @brief A WAL can't be copy constructed
WAL(const WAL &) = delete; Wal(const Wal &) = delete;
//! @brief A default destructor //! @brief A default destructor
~WAL() = default; ~Wal() = default;
//! @brief A WAL can't be assigned. //! @brief A WAL can't be assigned.
WAL &operator=(const WAL &) = delete; Wal &operator=(const Wal &) = delete;
}; };
} }

View File

@@ -0,0 +1,36 @@
//
// Created by Zoe Roux on 5/17/21.
//
#include "Wal.hpp"
#include "System/Movable/MovableSystem.hpp"
#include <catch2/catch.hpp>
using namespace WAL;
TEST_CASE("Create system", "[Engine][System]")
{
Wal wal;
wal.addSystem<MovableSystem>();
SECTION("Check existence") {
REQUIRE_NOTHROW(wal.getSystem<MovableSystem>());
}
SECTION("Duplicate check") {
REQUIRE_THROWS_AS(wal.addSystem<MovableSystem>(), DuplicateError);
}
SECTION("Remove system") {
wal.removeSystem<MovableSystem>();
REQUIRE_THROWS_AS(wal.getSystem<MovableSystem>(), NotFoundError);
REQUIRE_THROWS_AS(wal.removeSystem<MovableSystem>(), NotFoundError);
}
}
TEST_CASE("Create system by reference", "[Engine][System]")
{
Wal wal;
MovableSystem system;
wal.addSystem(system);
REQUIRE_THROWS_AS(wal.addSystem<MovableSystem>(), DuplicateError);
}

View File

@@ -2,7 +2,6 @@
// Created by Zoe Roux on 5/17/21. // Created by Zoe Roux on 5/17/21.
// //
#include "tests.hpp"
#include "Entity/Entity.hpp" #include "Entity/Entity.hpp"
#include "Component/Position/PositionComponent.hpp" #include "Component/Position/PositionComponent.hpp"
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
@@ -17,7 +16,7 @@ TEST_CASE("Component", "[Entity]")
SECTION("Check value") { SECTION("Check value") {
auto &pos = entity.getComponent<PositionComponent>(); auto &pos = entity.getComponent<PositionComponent>();
REQUIRE(entity.hasComponent<PositionComponent>()); REQUIRE(entity.hasComponent<PositionComponent>());
REQUIRE(pos.getPosition() == Vector3f(2, 3, 4)); REQUIRE(pos.position == Vector3f(2, 3, 4));
} }
SECTION("Prevent duplicates") { SECTION("Prevent duplicates") {
REQUIRE_THROWS_AS(entity.addComponent<PositionComponent>(), DuplicateError); REQUIRE_THROWS_AS(entity.addComponent<PositionComponent>(), DuplicateError);
@@ -41,5 +40,5 @@ TEST_CASE("Add component by reference", "[Entity]")
PositionComponent component(entity, 4, 5, 6); PositionComponent component(entity, 4, 5, 6);
REQUIRE(&entity.addComponent(component) == &entity); REQUIRE(&entity.addComponent(component) == &entity);
REQUIRE(entity.getComponent<PositionComponent>().getPosition() == Vector3f(4, 5, 6)); REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f(4, 5, 6));
} }

View File

@@ -1,9 +0,0 @@
//
// Created by Zoe Roux on 5/17/21.
//
#pragma once
#define private public
#define protected public

View File

@@ -3,7 +3,7 @@
int main() int main()
{ {
WAL::WAL wal; WAL::Wal wal;
try { try {
wal.run(); wal.run();