diff --git a/include/Core/EventBroker.h b/include/Core/EventBroker.h index 410a93a..f19309c 100644 --- a/include/Core/EventBroker.h +++ b/include/Core/EventBroker.h @@ -121,8 +121,6 @@ public: int Process(std::string contextTypeName); void Clear(); void Unsubscribe(BaseEventRelay &relay); - template - void UnsubscribeAll(); private: typedef std::string ContextTypeName_t; // typeid(ContextType).name() @@ -156,13 +154,6 @@ int EventBroker::Process() return Process(contextTypeName); } -template -void EventBroker::UnsubscribeAll() -{ - const std::string contextTypeName = typeid(ContextType).name(); - m_ContextRelays.erase(contextTypeName); -} - } #endif // MessageRelay_h__ diff --git a/src/game/Core/EventBroker.cpp b/src/game/Core/EventBroker.cpp index 748983a..f24b073 100644 --- a/src/game/Core/EventBroker.cpp +++ b/src/game/Core/EventBroker.cpp @@ -40,6 +40,7 @@ void dd::EventBroker::Unsubscribe(BaseEventRelay &relay) // ? { if (it->second == &relay) { + relay.m_Broker = nullptr; eventRelays.erase(it); break; } diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 918c679..ae7d47f 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -10,7 +10,9 @@ include_directories( set(SOURCE_FILES main.cpp FileWatcherTest.cpp + EventTest.cpp ComponentCopyTest.cpp + EntityCloneTest.cpp ) if(CMAKE_COMPILER_IS_GNUCXX) diff --git a/src/tests/EntityCloneTest.cpp b/src/tests/EntityCloneTest.cpp new file mode 100644 index 0000000..041f594 --- /dev/null +++ b/src/tests/EntityCloneTest.cpp @@ -0,0 +1,46 @@ +#include +#include "Core/EventBroker.h" +#include "Core/World.h" +#include "Core/CTemplate.h" +using namespace dd; + +struct TestComponent : public Component +{ + int Int = 5; + float Float = 1.33333f; + double Double = 1.33333; + std::string String = "Hello World"; +}; + +BOOST_AUTO_TEST_CASE(EntityCloneTest) +{ + // Setup world + std::shared_ptr eventBroker = std::make_shared(); + World world(eventBroker); + world.ComponentFactory.Register(); + world.ComponentFactory.Register(); + + // Create our original entity + EntityID entOriginal = world.CreateEntity(); + world.AddComponent(entOriginal); + world.AddComponent(entOriginal); + + // Clone it + EntityID entClone = world.CloneEntity(entOriginal); + + // Check + // EntityID of clone should not be same as original (obviously) + BOOST_CHECK(entOriginal != entClone); + // Template component SHOULD be in clone + auto templateComponentClone = world.GetComponent(entClone); + BOOST_REQUIRE(templateComponentClone != nullptr); + // Values should be the same across the clone + auto testComponentOriginal = world.GetComponent(entOriginal); + BOOST_REQUIRE(testComponentOriginal != nullptr); + auto testComponentClone = world.GetComponent(entClone); + BOOST_REQUIRE(testComponentClone != nullptr); + BOOST_CHECK(testComponentClone->Int == testComponentOriginal->Int); + BOOST_CHECK(testComponentClone->Float == testComponentOriginal->Float); + BOOST_CHECK(testComponentClone->Double == testComponentOriginal->Double); + BOOST_CHECK(testComponentClone->String == testComponentOriginal->String); +} \ No newline at end of file diff --git a/src/tests/EventFixture.h b/src/tests/EventFixture.h new file mode 100644 index 0000000..ef9acbf --- /dev/null +++ b/src/tests/EventFixture.h @@ -0,0 +1,54 @@ +#ifndef EVENTFIXTURE_H +#define EVENTFIXTURE_H + +#include +#include "Core/EventBroker.h" + +template +struct EventFixture +{ + EventFixture() + { + this->EventBroker = new dd::EventBroker(); + m_EEventType = decltype(m_EEventType)(std::bind(&OnEvent, this, std::placeholders::_1)); + this->EventBroker->Subscribe(m_EEventType); + Run(); + Check(); + } + ~EventFixture() + { + this->EventBroker->Unsubscribe(m_EEventType); + delete this->EventBroker; + } + + dd::EventBroker* EventBroker = nullptr; + dd::EventRelay m_EEventType; + bool m_EventRecieved = false; + EventType Before; + EventType After; + + bool OnEvent(const EventType& event) + { + m_EventRecieved = true; + After = event; + + return true; + } + + void Run() + { + // Publish the event + this->EventBroker->Publish(Before); + // Clear to swap buffers + this->EventBroker->Clear(); + // Process the event + this->EventBroker->template Process(); + } + + void Check() + { + BOOST_CHECK(m_EventRecieved); + } +}; + +#endif diff --git a/src/tests/EventTest.cpp b/src/tests/EventTest.cpp new file mode 100644 index 0000000..7d7975a --- /dev/null +++ b/src/tests/EventTest.cpp @@ -0,0 +1,21 @@ +#include +#include "EventFixture.h" + +using namespace dd; + +struct ETestEvent : public Event +{ + int Int = 5; + float Float = 1.33333f; + double Double = 1.33333; + std::string String = "Hello World"; +}; + +BOOST_AUTO_TEST_CASE(EventBrokerTest) +{ + EventFixture f; + BOOST_CHECK(f.Before.Int == f.After.Int); + BOOST_CHECK_CLOSE(f.Before.Float, f.After.Float, 0.00001f); + BOOST_CHECK_CLOSE(f.Before.Double, f.After.Double, 0.00001f); + BOOST_CHECK(f.Before.String == f.After.String); +} \ No newline at end of file