diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 918c679..3e955f3 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -10,6 +10,7 @@ include_directories( set(SOURCE_FILES main.cpp FileWatcherTest.cpp + EventTest.cpp ComponentCopyTest.cpp ) 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