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>
144 lines
4.2 KiB
C++
144 lines
4.2 KiB
C++
#include <Void/Async/Observable.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// ── Observable<T> ─────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test observable_initial_value()
|
|
{
|
|
Void::Observable::Observable<int> obs{42};
|
|
Void::Testing::expect_eq(obs.get(), 42);
|
|
}
|
|
|
|
Void::Testing::Test observable_set_changes_value()
|
|
{
|
|
Void::Observable::Observable<int> obs{0};
|
|
obs.set(7);
|
|
Void::Testing::expect_eq(obs.get(), 7);
|
|
}
|
|
|
|
Void::Testing::Test observable_listener_called_on_change()
|
|
{
|
|
Void::Observable::Observable<int> obs{0};
|
|
int seen_new = -1;
|
|
obs.on_change([&](int, int n) { seen_new = n; });
|
|
obs.set(5);
|
|
Void::Testing::expect_eq(seen_new, 5);
|
|
}
|
|
|
|
Void::Testing::Test observable_listener_receives_old_and_new()
|
|
{
|
|
Void::Observable::Observable<int> obs{10};
|
|
int old_val = -1, new_val = -1;
|
|
obs.on_change([&](int o, int n) { old_val = o; new_val = n; });
|
|
obs.set(20);
|
|
Void::Testing::expect_eq(old_val, 10);
|
|
Void::Testing::expect_eq(new_val, 20);
|
|
}
|
|
|
|
Void::Testing::Test observable_no_notification_on_same_value()
|
|
{
|
|
Void::Observable::Observable<int> obs{5};
|
|
int call_count = 0;
|
|
obs.on_change([&](int, int) { ++call_count; });
|
|
obs.set(5);
|
|
Void::Testing::expect_eq(call_count, 0);
|
|
}
|
|
|
|
Void::Testing::Test observable_multiple_listeners()
|
|
{
|
|
Void::Observable::Observable<int> obs{0};
|
|
int a = 0, b = 0;
|
|
obs.on_change([&](int, int n) { a = n; });
|
|
obs.on_change([&](int, int n) { b = n * 2; });
|
|
obs.set(3);
|
|
Void::Testing::expect_eq(a, 3);
|
|
Void::Testing::expect_eq(b, 6);
|
|
}
|
|
|
|
Void::Testing::Test observable_unsubscribe_stops_notifications()
|
|
{
|
|
Void::Observable::Observable<int> obs{0};
|
|
int count = 0;
|
|
auto h = obs.on_change([&](int, int) { ++count; });
|
|
obs.set(1);
|
|
obs.unsubscribe(h);
|
|
obs.set(2);
|
|
Void::Testing::expect_eq(count, 1);
|
|
}
|
|
|
|
Void::Testing::Test observable_unsubscribe_returns_false_for_unknown_handle()
|
|
{
|
|
Void::Observable::Observable<int> obs{0};
|
|
Void::Testing::expect(!obs.unsubscribe(Void::Observable::Handle{999}));
|
|
}
|
|
|
|
Void::Testing::Test observable_assign_operator()
|
|
{
|
|
Void::Observable::Observable<std::string> obs{"hello"};
|
|
int count = 0;
|
|
obs.on_change([&](const std::string&, const std::string&) { ++count; });
|
|
obs = std::string{"world"};
|
|
Void::Testing::expect_eq(obs.get(), std::string{"world"});
|
|
Void::Testing::expect_eq(count, 1);
|
|
}
|
|
|
|
// ── Signal<Args...> ───────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test signal_emit_calls_slot()
|
|
{
|
|
Void::Observable::Signal<int> sig;
|
|
int received = -1;
|
|
sig.connect([&](const int& v) { received = v; });
|
|
sig.emit(42);
|
|
Void::Testing::expect_eq(received, 42);
|
|
}
|
|
|
|
Void::Testing::Test signal_multiple_slots()
|
|
{
|
|
Void::Observable::Signal<int> sig;
|
|
std::vector<int> vals;
|
|
sig.connect([&](const int& v) { vals.push_back(v); });
|
|
sig.connect([&](const int& v) { vals.push_back(v * 10); });
|
|
sig.emit(3);
|
|
Void::Testing::expect_eq(vals.size(), std::size_t{2});
|
|
Void::Testing::expect_eq(vals[0], 3);
|
|
Void::Testing::expect_eq(vals[1], 30);
|
|
}
|
|
|
|
Void::Testing::Test signal_disconnect_stops_slot()
|
|
{
|
|
Void::Observable::Signal<int> sig;
|
|
int count = 0;
|
|
auto h = sig.connect([&](const int&) { ++count; });
|
|
sig.emit(1);
|
|
sig.disconnect(h);
|
|
sig.emit(2);
|
|
Void::Testing::expect_eq(count, 1);
|
|
}
|
|
|
|
Void::Testing::Test signal_clear_removes_all_slots()
|
|
{
|
|
Void::Observable::Signal<> sig;
|
|
int count = 0;
|
|
sig.connect([&] { ++count; });
|
|
sig.connect([&] { ++count; });
|
|
sig.clear();
|
|
sig.emit();
|
|
Void::Testing::expect_eq(count, 0);
|
|
}
|
|
|
|
Void::Testing::Test signal_multi_arg_emit()
|
|
{
|
|
Void::Observable::Signal<int, std::string> sig;
|
|
int n = 0;
|
|
std::string s;
|
|
sig.connect([&](const int& a, const std::string& b) { n = a; s = b; });
|
|
sig.emit(7, std::string{"hello"});
|
|
Void::Testing::expect_eq(n, 7);
|
|
Void::Testing::expect_eq(s, std::string{"hello"});
|
|
}
|