DarkflameServer/tests/dECSTests/TestECS.cpp
2024-12-17 01:02:30 -06:00

68 lines
1.8 KiB
C++

#include <cstdio>
#include <gtest/gtest.h>
#include "Core.h"
#include <dComponents/Component.h>
#include <eReplicaComponentType.h>
using namespace dECS;
struct TestComponent {
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::CHOICE_BUILD;
int value;
};
// Test that entity IDs increment correctly
TEST(ECSTest, IncrementEntityIdsSingleThread) {
auto w = World{};
auto ea = w.MakeEntity();
ASSERT_EQ(ea.GetObjectID(), 1);
auto eb = w.MakeEntity();
ASSERT_EQ(eb.GetObjectID(), 2);
auto ec = w.MakeEntity();
ASSERT_EQ(ec.GetObjectID(), 3);
}
// Test adding and getting components
TEST(ECSTest, MakeOneEntityAndAddComponents) {
auto w = World{};
auto e = w.MakeEntity();
ASSERT_EQ(e.GetObjectID(), 1);
// add component
auto* const testCompPtr = e.AddComponent<TestComponent>();
ASSERT_NE(testCompPtr, nullptr);
ASSERT_EQ(testCompPtr->ComponentType, eReplicaComponentType::CHOICE_BUILD);
ASSERT_EQ(testCompPtr->value, 0);
testCompPtr->value = 15;
// try getting the same component we just added
auto* const gotTestCompPtr = e.GetComponent<TestComponent>();
ASSERT_NE(gotTestCompPtr, nullptr);
ASSERT_EQ(gotTestCompPtr, testCompPtr);
ASSERT_NE(gotTestCompPtr->value, 0);
ASSERT_EQ(gotTestCompPtr->value, 15);
}
// Test world scoping
TEST(ECSTest, WorldScope) {
auto e = std::optional<dECS::Entity>{};
{
auto w = World{};
e.emplace(w.MakeEntity());
ASSERT_EQ(e->GetObjectID(), 1);
// add component within scope
auto* const cPtr = e->AddComponent<TestComponent>();
ASSERT_NE(cPtr, nullptr);
}
// Attempting to access this component should return nullptr
// now that the world has gone out of scope
ASSERT_EQ(e->GetComponent<TestComponent>(), nullptr);
}