feat: add event bus tests
This commit is contained in:
+2
-1
@@ -4,8 +4,9 @@
|
|||||||
!/docs
|
!/docs
|
||||||
!/include
|
!/include
|
||||||
!/src
|
!/src
|
||||||
|
!/tests
|
||||||
|
|
||||||
# Include files
|
# Include files
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
!/CMakeLists.txt
|
!/CMakeLists.txt
|
||||||
|
!/Makefile
|
||||||
|
|||||||
@@ -10,3 +10,17 @@ add_executable(magic_scrolls ${sources})
|
|||||||
|
|
||||||
target_include_directories(magic_scrolls PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
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
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <engine/core/engine_bus.hpp>
|
||||||
|
|
||||||
|
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<TestEvent*>(&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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user