feat: add event bus tests
This commit is contained in:
@@ -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