From 316a4432767efbea70c1409edf5f546e61077c54 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 3 Dec 2022 18:18:19 +0200 Subject: [PATCH] Add unit tests for the event bus --- source/game/eventsource.h | 2 +- tests/eventbus.cpp | 100 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/eventbus.cpp diff --git a/source/game/eventsource.h b/source/game/eventsource.h index ccb830f..1748f15 100644 --- a/source/game/eventsource.h +++ b/source/game/eventsource.h @@ -29,7 +29,7 @@ public: template void add_observer(EventObserver &obs, std::function cb) - { static_cast &>(*this).add_observer(obs, std::move(cb)); } + { static_cast &>(*this).add_observer(&obs, std::move(cb)); } void remove_observer(EventObserver &obs) override { (static_cast &>(*this).remove_observer(&obs), ...); } diff --git a/tests/eventbus.cpp b/tests/eventbus.cpp new file mode 100644 index 0000000..6e4e0b5 --- /dev/null +++ b/tests/eventbus.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class EventBusTests: public Test::RegisteredTest +{ +public: + EventBusTests(); + + static const char *get_name() { return "Event bus"; } + +private: + void broadcast_events(); + void targeted_events(); + void observe_single(); + void observer_lifetime(); +}; + +struct E1 { int value; }; +struct E2 { int value; }; + +EventBusTests::EventBusTests() +{ + add(&EventBusTests::broadcast_events, "Broadcast events"); + add(&EventBusTests::targeted_events, "Targeted events"); + add(&EventBusTests::observe_single, "Observe single source"); + add(&EventBusTests::observer_lifetime, "Observer lifetime"); +} + +void EventBusTests::broadcast_events() +{ + Game::EventBus bus; + Game::EventSource src1(bus); + Game::EventSource src2(bus); + Game::EventObserver obs1(bus); + Game::EventObserver obs2(bus); + int value11 = 0; + int value12 = 0; + int value21 = 0; + int value22 = 0; + obs1.observe([&value11](const E1 &e){ value11 = e.value; }); + obs1.observe([&value12](const E2 &e){ value12 = e.value; }); + obs2.observe([&value21](const E1 &e){ value21 = e.value; }); + obs2.observe([&value22](const E2 &e){ value22 = e.value; }); + src1.emit(42); + src2.emit(7777); + EXPECT_EQUAL(value11, 42); + EXPECT_EQUAL(value21, 42); + EXPECT_EQUAL(value12, 7777); + EXPECT_EQUAL(value22, 7777); +} + +void EventBusTests::targeted_events() +{ + Game::EventBus bus; + Game::EventSource src(bus); + Game::EventObserver obs1(bus); + Game::EventObserver obs2(bus); + int value1 = 0; + int value2 = 0; + obs1.observe([&value1](const E1 &e){ value1 = e.value; }); + obs2.observe([&value2](const E1 &e){ value2 = e.value; }); + src.emit_to(obs1, 42); + EXPECT_EQUAL(value1, 42); + EXPECT_EQUAL(value2, 0); +} + +void EventBusTests::observe_single() +{ + Game::EventBus bus; + Game::EventSource src1(bus); + Game::EventSource src2(bus); + Game::EventObserver obs(bus); + int value1 = 0; + int value2 = 0; + obs.observe(src1, [&value1](const E1 &e){ value1 = e.value; }); + src1.emit(42); + src2.emit(7777); + EXPECT_EQUAL(value1, 42); + EXPECT_EQUAL(value2, 0); +} + +void EventBusTests::observer_lifetime() +{ + Game::EventBus bus; + Game::EventSource src(bus); + int value = 0; + { + Game::EventObserver obs(bus); + obs.observe([&value](const E1 &e){ value = e.value; }); + src.emit(42); + EXPECT_EQUAL(value, 42); + } + src.emit(7777); + EXPECT_EQUAL(value, 42); +} -- 2.45.2