Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now an alias for Edit<string> so count_op/apply work on line diffs; Memoized uses std::apply instead of type-based get to handle duplicate arg types. 874/877 tests pass (3 are platform-gated plugin tests). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
116 lines
3.8 KiB
C++
116 lines
3.8 KiB
C++
#include <Void/EventBus/EventBus.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
|
|
using namespace Void::EventBus;
|
|
|
|
struct Clicked { int x, y; };
|
|
struct TextChanged { std::string text; };
|
|
|
|
// ── subscribe / publish ───────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_publish_reaches_subscriber()
|
|
{
|
|
EventBus bus;
|
|
int received = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked& e) { received = e.x; });
|
|
bus.publish(Clicked{42, 0});
|
|
Void::Testing::expect_eq(received, 42);
|
|
}
|
|
|
|
Void::Testing::Test bus_multiple_subscribers_same_type()
|
|
{
|
|
EventBus bus;
|
|
int count = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.publish(Clicked{0, 0});
|
|
Void::Testing::expect_eq(count, 2);
|
|
}
|
|
|
|
Void::Testing::Test bus_different_event_types_isolated()
|
|
{
|
|
EventBus bus;
|
|
bool got_click = false;
|
|
bool got_text = false;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { got_click = true; });
|
|
bus.subscribe<TextChanged>([&](const TextChanged&) { got_text = true; });
|
|
bus.publish(Clicked{0, 0});
|
|
Void::Testing::expect(got_click);
|
|
Void::Testing::expect(!got_text);
|
|
}
|
|
|
|
Void::Testing::Test bus_no_subscribers_publish_is_noop()
|
|
{
|
|
EventBus bus;
|
|
bus.publish(Clicked{1, 2});
|
|
}
|
|
|
|
// ── unsubscribe ───────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_unsubscribe_stops_delivery()
|
|
{
|
|
EventBus bus;
|
|
int count = 0;
|
|
const auto id = bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.publish(Clicked{});
|
|
bus.unsubscribe(id);
|
|
bus.publish(Clicked{});
|
|
Void::Testing::expect_eq(count, 1);
|
|
}
|
|
|
|
Void::Testing::Test bus_unsubscribe_nonexistent_is_noop()
|
|
{
|
|
EventBus bus;
|
|
bus.unsubscribe(999);
|
|
}
|
|
|
|
// ── clear ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_clear_event_type()
|
|
{
|
|
EventBus bus;
|
|
int count = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.clear<Clicked>();
|
|
bus.publish(Clicked{});
|
|
Void::Testing::expect_eq(count, 0);
|
|
}
|
|
|
|
Void::Testing::Test bus_clear_all()
|
|
{
|
|
EventBus bus;
|
|
int a = 0, b = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++a; });
|
|
bus.subscribe<TextChanged>([&](const TextChanged&) { ++b; });
|
|
bus.clear_all();
|
|
bus.publish(Clicked{});
|
|
bus.publish(TextChanged{});
|
|
Void::Testing::expect_eq(a, 0);
|
|
Void::Testing::expect_eq(b, 0);
|
|
}
|
|
|
|
// ── subscriber_count ──────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_subscriber_count()
|
|
{
|
|
EventBus bus;
|
|
Void::Testing::expect_eq(bus.subscriber_count<Clicked>(), std::size_t{0});
|
|
bus.subscribe<Clicked>([](const Clicked&) {});
|
|
bus.subscribe<Clicked>([](const Clicked&) {});
|
|
Void::Testing::expect_eq(bus.subscriber_count<Clicked>(), std::size_t{2});
|
|
}
|
|
|
|
// ── event data forwarded correctly ────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_event_data_forwarded()
|
|
{
|
|
EventBus bus;
|
|
std::string received;
|
|
bus.subscribe<TextChanged>([&](const TextChanged& e) { received = e.text; });
|
|
bus.publish(TextChanged{"hello"});
|
|
Void::Testing::expect_eq(received, std::string{"hello"});
|
|
}
|