Compare commits
3
Commits
8ea4bf45ba
...
045fe533d2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
045fe533d2 | ||
|
|
df7a8855d4 | ||
|
|
1978a24d1e |
+2
-1
@@ -4,8 +4,9 @@
|
||||
!/docs
|
||||
!/include
|
||||
!/src
|
||||
!/tests
|
||||
|
||||
# Include files
|
||||
!/.gitignore
|
||||
!/CMakeLists.txt
|
||||
|
||||
!/Makefile
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
.PHONY: build test
|
||||
|
||||
build:
|
||||
cmake -B build
|
||||
cmake --build build
|
||||
|
||||
test:
|
||||
ctest --test-dir build --output-on-failure --progress
|
||||
@@ -18,10 +18,19 @@ public:
|
||||
virtual std::string getName() const = 0;
|
||||
|
||||
/// @brief Is current event is canceled
|
||||
virtual bool isCanceled() const = 0;
|
||||
bool isCanceled() const
|
||||
{
|
||||
return canceled;
|
||||
}
|
||||
|
||||
/// @brief Cancel current event
|
||||
virtual void cancel() = 0;
|
||||
void cancel()
|
||||
{
|
||||
canceled = true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool canceled = false;
|
||||
};
|
||||
|
||||
/// @brief Engine event bus
|
||||
@@ -47,7 +56,7 @@ public:
|
||||
/// @brief Subscribe to specified event and execute once on publish
|
||||
/// @param name event name
|
||||
/// @param listener event handler
|
||||
void subscribeOnce(const std::string &name, Listener listener);
|
||||
SubscriptionId subscribeOnce(const std::string &name, Listener listener);
|
||||
|
||||
/// @brief Unsubscribe listener by its own id
|
||||
/// @param id subscription id
|
||||
|
||||
@@ -48,9 +48,9 @@ MscEventBus::SubscriptionId MscEventBus::subscribe(const std::string &name, List
|
||||
return _subscribe(name, std::move(listener), false);
|
||||
}
|
||||
|
||||
void MscEventBus::subscribeOnce(const std::string &name, Listener listener)
|
||||
MscEventBus::SubscriptionId MscEventBus::subscribeOnce(const std::string &name, Listener listener)
|
||||
{
|
||||
_subscribe(name, std::move(listener), true);
|
||||
return _subscribe(name, std::move(listener), true);
|
||||
}
|
||||
|
||||
bool MscEventBus::unsubscribe(SubscriptionId id)
|
||||
|
||||
@@ -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