diff --git a/.gitignore b/.gitignore index b582844..0c6ec0e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,9 @@ !/docs !/include !/src +!/tests # Include files !/.gitignore !/CMakeLists.txt - +!/Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a530e6..4f153ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,3 +10,17 @@ add_executable(magic_scrolls ${sources}) target_include_directories(magic_scrolls PUBLIC "${CMAKE_SOURCE_DIR}/include") +# ------- +enable_testing() + +add_executable(event_bus_test + tests/engine/event_bus_test.cpp + src/engine/core/event_bus.cpp +) + +target_include_directories(event_bus_test PUBLIC "${CMAKE_SOURCE_DIR}/include") + +add_test( + NAME "Event Bus" + COMMAND event_bus_test +) diff --git a/tests/engine/event_bus_test.cpp b/tests/engine/event_bus_test.cpp new file mode 100644 index 0000000..ec0fec0 --- /dev/null +++ b/tests/engine/event_bus_test.cpp @@ -0,0 +1,76 @@ +#include +#include + +const std::string testEventName = "TestEvent"; +int calls = 0; + +class TestEvent : public MscEvent +{ +public: + std::string getName() const override + { + return testEventName; + } +}; + +/// @brief Проверка на работу pub-sub +void testEventPubSub() +{ + calls = 0; + MscEventBus bus; + TestEvent event; + + bus.subscribe(testEventName, [&](MscEvent &ev) + { + calls++; + + auto *testEvent = dynamic_cast(&ev); + assert(testEvent != nullptr); }); + + bus.publish(event); + assert(calls == 1); +} + +/// @brief Проверка на работу +void testEventOnce() +{ + calls = 0; + MscEventBus bus; + TestEvent event; + + bus.subscribeOnce(testEventName, [&](MscEvent &) + { calls++; }); + + bus.publish(event); + bus.publish(event); + assert(calls == 1); +} + +/// @brief Проверка работы отписки +void testUnsubscribe() +{ + calls = 0; + MscEventBus bus; + TestEvent event; + + auto id = bus.subscribe(testEventName, [&](MscEvent &) + { calls++; }); + + bus.publish(event); + + assert(bus.unsubscribe(id) == true); + assert(bus.unsubscribe(id) == false); + + bus.publish(event); + + assert(calls == 1); +} + +int main() +{ + testEventPubSub(); + testEventOnce(); + testUnsubscribe(); + + return 0; +}