Skip to content

Commit

Permalink
Added test_System and test_Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
timschwartz committed Nov 8, 2023
1 parent d21a2c6 commit 02508fa
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/libecs-cpp/System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ namespace ecs
virtual nlohmann::json Export() = 0;
ecs::TypeEntityComponentList *Components = nullptr;
ecs::Timing Timing;
const size_t MessagesWaiting();
const uint32_t DeltaTimeGet();
protected:
std::queue<nlohmann::json> messages;
std::chrono::steady_clock::time_point LastTime = std::chrono::steady_clock::now();
uint32_t DeltaTimeGet();
};
}
1 change: 1 addition & 0 deletions src/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ namespace ecs
{
components.erase(Handle);
}
delete this->Entities[Handle];
this->Entities.erase(Handle);
}

Expand Down
7 changes: 6 additions & 1 deletion src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ namespace ecs
this->messages.push(message);
}

uint32_t System::DeltaTimeGet()
const size_t System::MessagesWaiting()
{
return this->messages.size();
}

const uint32_t System::DeltaTimeGet()
{
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
uint32_t dt = std::chrono::duration_cast<std::chrono::milliseconds>(now - this->LastTime).count();
Expand Down
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
test_Manager
test_System
test_Entity
9 changes: 8 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
AM_CPPFLAGS = ${CATCH2_CFLAGS} -I../include

check_PROGRAMS = test_Manager
check_PROGRAMS = test_Manager test_System test_Entity

test_Manager_SOURCES = test_Manager.cpp
test_Manager_LDADD = ../src/libecs-cpp.la

test_System_SOURCES = test_System.cpp
test_System_LDADD = ../src/libecs-cpp.la

test_Entity_SOURCES = test_Entity.cpp
test_Entity_LDADD = ../src/libecs-cpp.la
78 changes: 78 additions & 0 deletions tests/test_Entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <libecs-cpp/ecs.hpp>

class TestComponent : public ecs::Component
{
public:
TestComponent()
{
this->Type = "TestComponent";
}

TestComponent(nlohmann::json config)
{
this->Type = "TestComponent";
this->value = config["value"].get<uint64_t>();
}

nlohmann::json Export()
{
nlohmann::json config;
config["value"] = this->value;
return config;
}

uint64_t value = 0;
};

TEST_CASE("Entity is created with a unique handle", "[Entity]") {
auto container = ECS->Container();
auto entity = container->Entity();
REQUIRE_FALSE(entity->Handle.empty());
}

TEST_CASE("Entity can be created with a custom handle", "[Entity]") {
std::string customHandle = "myEntity";
auto container = ECS->Container();
auto entity = container->Entity(customHandle);
REQUIRE(entity->Handle == customHandle);
}

TEST_CASE("Entity exports its data correctly", "[Entity]") {
auto container = ECS->Container();
auto entity = container->Entity();
nlohmann::json exportedData = entity->Export();
REQUIRE(exportedData["Handle"] == entity->Handle);
}

TEST_CASE("Entity can add and retrieve a component", "[Entity]") {
auto container = ECS->Container();
auto entity = container->Entity();
nlohmann::json config;
config["value"] = 5;
//auto component = std::make_shared<TestComponent>(config);
entity->Component(new TestComponent(config));
auto retrievedComponent = container->Components["TestComponent"][entity->Handle];
REQUIRE(retrievedComponent != nullptr);
}

TEST_CASE("Entity can destroy a component", "[Entity]") {
auto container = ECS->Container();
auto entity = container->Entity();
nlohmann::json config;
config["value"] = 5;
entity->Component(new TestComponent(config));
entity->ComponentDestroy("TestComponent");
auto retrievedComponent = container->Components["TestComponent"][entity->Handle];
REQUIRE(retrievedComponent == nullptr);
}

TEST_CASE("Entity can be destroyed", "[Entity]") {
auto container = ECS->Container();
auto entity = container->Entity();
std::string handle = entity->Handle;
entity->Destroy();
// Assuming there is a way to check if an entity has been destroyed, e.g.:
REQUIRE(container->Entities[handle] == nullptr);
}
57 changes: 57 additions & 0 deletions tests/test_System.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <libecs-cpp/ecs.hpp>

class TestSystem : public ecs::System
{
public:
TestSystem()
{
this->Handle = "TestSystem";
}

TestSystem(std::string Handle)
{
this->Handle = Handle;
}

nlohmann::json Export()
{
nlohmann::json config;
return config;
}

};

TEST_CASE("System is initialized with a valid handle", "[System]") {
TestSystem system;
REQUIRE_FALSE(system.Handle.empty());
}

TEST_CASE("System can be initialized with a custom handle", "[System]") {
std::string customHandle = "custom_handle";
TestSystem system(customHandle);
REQUIRE(system.Handle == customHandle);
}

TEST_CASE("System starts with no messages", "[System]") {
TestSystem system;
REQUIRE(system.MessagesWaiting() == 0);
}

TEST_CASE("System can submit messages", "[System]") {
TestSystem system;
nlohmann::json message = {{"key", "value"}};
system.MessageSubmit(message);
REQUIRE(system.MessagesWaiting() == 1);
}

TEST_CASE("System can calculate delta time", "[System]") {
TestSystem system;
// First call to initialize LastTime
system.DeltaTimeGet();
// Small sleep to simulate passage of time
std::this_thread::sleep_for(std::chrono::milliseconds(10));
uint32_t deltaTime = system.DeltaTimeGet();
REQUIRE(deltaTime >= 10); // Check if at least 10ms have passed
}

0 comments on commit 02508fa

Please sign in to comment.