Skip to content

Commit

Permalink
[Tests/Entity] Test entities use custom components
Browse files Browse the repository at this point in the history
- This replaces the current usage of Mesh, Transform & Light

- The render system's test's cubemap doesn't load twice the images anymore
  • Loading branch information
Razakhel committed Feb 24, 2024
1 parent 4128f74 commit 4007ca0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
72 changes: 39 additions & 33 deletions tests/src/RaZ/Entity.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#include "RaZ/Entity.hpp"
#include "RaZ/Data/Mesh.hpp"
#include "RaZ/Math/Transform.hpp"
#include "RaZ/Render/Light.hpp"

#include <catch2/catch_test_macros.hpp>

namespace {

// Declaring entities to be tested
Raz::Entity entity0(0);
Raz::Entity entity1(1);
Raz::Entity entity2(2);
struct FirstTestComponent : public Raz::Component {};
struct SecondTestComponent : public Raz::Component {};

} // namespace

TEST_CASE("Entity basic", "[core]") {
Raz::Entity entity0(0);
const Raz::Entity entity1(1);
const Raz::Entity entity2(2, false);

CHECK(entity0.getId() == 0);
CHECK(entity1.getId() == 1);
CHECK(entity2.getId() == 2);

CHECK(entity0.isEnabled());
CHECK(entity1.isEnabled());
CHECK_FALSE(entity2.isEnabled());

entity0.disable();
CHECK_FALSE(entity0.isEnabled());
Expand All @@ -28,46 +29,51 @@ TEST_CASE("Entity basic", "[core]") {
CHECK(entity0.isEnabled());
}

TEST_CASE("Entity-component manipulations", "[core]") {
CHECK(entity0.getComponents().empty());
TEST_CASE("Entity-component manipulation", "[core]") {
Raz::Entity entity(0);

CHECK_FALSE(entity0.hasComponent<Raz::Mesh>());
CHECK_FALSE(entity0.hasComponent<Raz::Transform>());
CHECK(entity.getComponents().empty());
CHECK_FALSE(entity.hasComponent<FirstTestComponent>());
CHECK_FALSE(entity.hasComponent<SecondTestComponent>());

entity0.addComponent<Raz::Transform>();
CHECK(entity0.hasComponent<Raz::Transform>());
CHECK_FALSE(entity0.hasComponent<Raz::Mesh>());
entity.addComponent<FirstTestComponent>();
CHECK(entity.hasComponent<FirstTestComponent>());
CHECK_NOTHROW(entity.getComponent<FirstTestComponent>());
CHECK_FALSE(entity.hasComponent<SecondTestComponent>());
CHECK_THROWS(entity.getComponent<SecondTestComponent>());

CHECK(std::is_same_v<decltype(entity0.getComponent<Raz::Transform>()), Raz::Transform&>);
CHECK_THROWS(entity0.getComponent<Raz::Mesh>());
CHECK(std::is_same_v<decltype(entity.getComponent<FirstTestComponent>()), FirstTestComponent&>);

entity0.removeComponent<Raz::Transform>();
CHECK_FALSE(entity0.hasComponent<Raz::Transform>());
CHECK_THROWS(entity0.getComponent<Raz::Transform>());
entity.removeComponent<FirstTestComponent>();
CHECK_FALSE(entity.hasComponent<FirstTestComponent>());
CHECK_THROWS(entity.getComponent<FirstTestComponent>());
}

TEST_CASE("Entity bitset", "[core]") {
Raz::Entity entity0(0);
Raz::Entity entity1(1);

CHECK(entity0.getEnabledComponents().isEmpty());
entity0.addComponent<FirstTestComponent>();
CHECK_FALSE(entity0.getEnabledComponents().isEmpty());

CHECK(entity0.getEnabledComponents().getEnabledBitCount() == 1);
CHECK(entity0.getEnabledComponents()[Raz::Component::getId<FirstTestComponent>()]);
CHECK(entity0.getEnabledComponents().getSize() == Raz::Component::getId<FirstTestComponent>() + 1);

CHECK(entity1.getEnabledComponents().isEmpty());
entity1.addComponent<Raz::Transform>();
entity1.addComponent<SecondTestComponent>();
CHECK_FALSE(entity1.getEnabledComponents().isEmpty());

CHECK(entity1.getEnabledComponents().getEnabledBitCount() == 1);
CHECK(entity1.getEnabledComponents()[Raz::Component::getId<Raz::Transform>()]);
CHECK(entity1.getEnabledComponents().getSize() == Raz::Component::getId<Raz::Transform>() + 1);

CHECK(entity2.getEnabledComponents().isEmpty());
entity2.addComponent<Raz::Light>(Raz::LightType::POINT, 10.f);
CHECK_FALSE(entity2.getEnabledComponents().isEmpty());

CHECK(entity2.getEnabledComponents().getEnabledBitCount() == 1);
CHECK(entity2.getEnabledComponents()[Raz::Component::getId<Raz::Light>()]);
CHECK(entity2.getEnabledComponents().getSize() == Raz::Component::getId<Raz::Light>() + 1);
CHECK(entity1.getEnabledComponents()[Raz::Component::getId<SecondTestComponent>()]);
CHECK(entity1.getEnabledComponents().getSize() == Raz::Component::getId<SecondTestComponent>() + 1);

// Match test
// ---
// 0 0 1 -> Transform
// & 0 1 0 -> Light
// 0 0 1 -> FirstTestComponent
// & 0 1 0 -> SecondTestComponent
// _______
// = 0 0 0
CHECK((entity1.getEnabledComponents() & entity2.getEnabledComponents()) == Raz::Bitset(entity1.getEnabledComponents().getSize()));
CHECK((entity0.getEnabledComponents() & entity1.getEnabledComponents()) == Raz::Bitset(entity1.getEnabledComponents().getSize(), false));
}
10 changes: 4 additions & 6 deletions tests/src/RaZ/Render/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ TEST_CASE("RenderSystem Cook-Torrance ball", "[render]") {

// Setting a cubemap & moving the camera to look the ball from below, in order to see the left, top & back faces of the cubemap

renderSystem.setCubemap(Raz::Cubemap(Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/ŔŖȒȐ.png"),
Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/ŔŖȒȐ.png"),
Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/ĜƓGǦ.png"),
Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/ĜƓGǦ.png"),
Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/BƁḂɃ.png"),
Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/BƁḂɃ.png")));
const Raz::Image redImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/ŔŖȒȐ.png");
const Raz::Image greenImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/ĜƓGǦ.png");
const Raz::Image blueImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/textures/BƁḂɃ.png");
renderSystem.setCubemap(Raz::Cubemap(redImg, redImg, greenImg, greenImg, blueImg, blueImg));

cameraTrans.translate(Raz::Vec3f(3.f, -3.f, 0.f));
cameraComp.setCameraType(Raz::CameraType::LOOK_AT);
Expand Down
2 changes: 1 addition & 1 deletion tests/src/RaZ/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TEST_CASE("World refresh", "[core]") {

Raz::Entity& entity0 = world.addEntity(true);
Raz::Entity& entity1 = world.addEntity(true);
Raz::Entity& entity2 = world.addEntity(true);
const Raz::Entity& entity2 = world.addEntity(true);

CHECK(entity0.getId() == 0);
CHECK(entity1.getId() == 1);
Expand Down

0 comments on commit 4007ca0

Please sign in to comment.