Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
116 lines
3.8 KiB
C++
116 lines
3.8 KiB
C++
#include <Void/Async/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"});
|
|
}
|