Adding more tests

This commit is contained in:
Zoe Roux
2021-05-17 15:24:44 +02:00
parent 6ba5ceddd6
commit f9363c7aef
7 changed files with 37 additions and 7 deletions
+1 -2
View File
@@ -23,7 +23,6 @@ add_library(wal
sources/Component/Component.cpp
sources/Component/Basics/PositionComponent.cpp
sources/Component/Basics/PositionComponent.hpp
sources/Models/Vector2.hpp
sources/Models/Vector3.hpp
)
@@ -31,7 +30,7 @@ target_include_directories(wal PUBLIC sources)
add_executable(wal_tests
tests/EntityTests.cpp
tests/MainTest.cpp)
tests/MainTest.cpp tests/tests.hpp)
target_link_libraries(wal_tests PRIVATE wal)
find_package(Catch2 REQUIRED)
@@ -11,6 +11,11 @@ namespace WAL::Components
_position()
{}
PositionComponent::PositionComponent(Entity &entity, Vector3f pos)
: Component(entity),
_position(pos)
{}
PositionComponent::PositionComponent(Entity &entity, float x, float y, float z)
: Component(entity),
_position(x, y, z)
@@ -18,7 +23,7 @@ namespace WAL::Components
Component *PositionComponent::clone(WAL::Entity &entity) const
{
return new PositionComponent(entity);
return new PositionComponent(entity, this->_position);
}
Vector3f &PositionComponent::getPosition()
@@ -34,6 +34,8 @@ namespace WAL::Components
//! @brief Create a new PositionComponent linked to a specific entity
explicit PositionComponent(Entity &entity);
//! @brief Create a new PositionComponent at a certain position
PositionComponent(Entity &entity, Vector3f pos);
//! @brief Create a new PositionComponent at a certain position
PositionComponent(Entity &entity, float x, float y, float z);
//! @brief A position component is copy constructable
PositionComponent(const PositionComponent &) = default;
+2 -2
View File
@@ -65,7 +65,7 @@ namespace WAL
//! @brief Add a component to this entity. The component is constructed in place.
//! @throw DuplicateError is thrown if a component with the same type already exist.
//! @return This entity is returned
template<typename T, class ...Types>
template<typename T, typename ...Types>
Entity &addComponent(Types ...params)
{
if (this->hasComponent<T>())
@@ -85,7 +85,7 @@ namespace WAL
Entity &removeComponent()
{
const std::type_info &type = typeid(T);
auto &existing =std::find(this->_components.begin(), this->_components.end(), [&type] (auto &cmp) {
auto existing =std::find_if(this->_components.begin(), this->_components.end(), [&type] (auto &cmp) {
return typeid(*cmp) == type;
});
if (existing == this->_components.end())
+1 -1
View File
@@ -162,6 +162,6 @@ namespace WAL
template<typename T>
std::ostream &operator<<(std::ostream &s, const WAL::Vector3<T> &v)
{
s << v.x << " " << v.y << " " << v.z;
s << "Vector3<" << typeid(T).name() << ">("<< v.x << ", " << v.y << ", " << v.z << ")";
return s;
}
+16 -1
View File
@@ -2,9 +2,10 @@
// Created by Zoe Roux on 5/17/21.
//
#include <catch2/catch.hpp>
#include "tests.hpp"
#include "Entity/Entity.hpp"
#include "Component/Basics/PositionComponent.hpp"
#include <catch2/catch.hpp>
using namespace WAL;
using namespace WAL::Components;
@@ -22,10 +23,24 @@ TEST_CASE("Component", "[Entity]")
SECTION("Prevent duplicates") {
REQUIRE_THROWS_AS(entity.addComponent<PositionComponent>(), DuplicateError);
}
SECTION("Remove component") {
entity.removeComponent<PositionComponent>();
REQUIRE_THROWS_AS(entity.getComponent<PositionComponent>(), NotFoundError);
REQUIRE_THROWS_AS(entity.removeComponent<PositionComponent>(), NotFoundError);
}
}
TEST_CASE("ComponentNotFound", "[Entity]")
{
Entity entity("Bob");
REQUIRE_THROWS_AS(entity.getComponent<PositionComponent>(), NotFoundError);
}
TEST_CASE("Add component by reference", "[Entity]")
{
Entity entity("Bob");
PositionComponent component(entity, 4, 5, 6);
REQUIRE(&entity.addComponent(component) == &entity);
REQUIRE(entity.getComponent<PositionComponent>().getPosition() == Vector3f(4, 5, 6));
}
+9
View File
@@ -0,0 +1,9 @@
//
// Created by Zoe Roux on 5/17/21.
//
#pragma once
#define private public
#define protected public