Files
Library/tests/standalone/Scope/Scope.cpp
T
faumarayandClaude Fable 5 da2e93c405 Finish module consolidation: fold Memo into Pipeline, dedupe Channel/Stopwatch
Completes the in-progress merges (Optional->Either/Opt, Curry+Memo->Pipeline,
ScopeGuard->Scope, String->Str):
- Pipeline/Memo.hpp now carries the full Memo API (memoize with recursion
  support, TupleHash, SimpleCache) alongside memoize_fn; old Memo module
  and its test dir removed.
- Duplicate Channel module removed; Concurrency::Channel gains empty() and
  the unique test cases (drain-after-close, multi-producer, string payloads).
- StopWatch module merged into Time::Stopwatch: one class with explicit
  start/stop/laps plus a started() factory for the auto-start RAII style;
  ScopedTimer moved along.
- Testing optional dep retargeted String->Str (pad_right lives in Str now).
- Stale empty dirs removed: src/{ScopeGuard,String,Core}, include/Void/{Core,
  Filesystem,Memo,Channel,StopWatch}.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:41:24 +03:00

205 lines
5.0 KiB
C++

#include <Void/Scope/Scope.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <stdexcept>
#include <string>
using namespace Void::Scope;
// ── on_exit ───────────────────────────────────────────────────────────────────
Void::Testing::Test on_exit_runs_on_normal_exit()
{
bool ran = false;
{
auto guard = on_exit([&] { ran = true; });
}
Void::Testing::expect(ran);
}
Void::Testing::Test on_exit_runs_on_exception()
{
bool ran = false;
try {
auto guard = on_exit([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(ran);
}
Void::Testing::Test on_exit_release_prevents_run()
{
bool ran = false;
{
auto guard = on_exit([&] { ran = true; });
guard.release();
}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_exit_dismiss_prevents_run()
{
bool ran = false;
{
auto guard = on_exit([&] { ran = true; });
guard.dismiss();
}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_exit_active_reflects_state()
{
auto guard = on_exit([] {});
Void::Testing::expect(guard.active());
guard.dismiss();
Void::Testing::expect(!guard.active());
}
Void::Testing::Test on_exit_move_transfers_ownership()
{
int called = 0;
{
auto g1 = on_exit([&] { ++called; });
auto g2 = std::move(g1);
Void::Testing::expect(!g1.active());
Void::Testing::expect(g2.active());
}
Void::Testing::expect_eq(called, 1);
}
Void::Testing::Test on_exit_lifo_order()
{
std::string order;
{
auto a = on_exit([&] { order += "A"; });
auto b = on_exit([&] { order += "B"; });
auto c = on_exit([&] { order += "C"; });
}
Void::Testing::expect_eq(order, std::string{"CBA"});
}
Void::Testing::Test on_exit_multiple_guards_lifo()
{
int order = 0, a = 0, b = 0;
{
auto ga = on_exit([&] { a = ++order; });
auto gb = on_exit([&] { b = ++order; });
}
Void::Testing::expect(b < a);
}
Void::Testing::Test on_exit_resource_cleanup()
{
bool released = false;
{
int handle = 42;
auto cleanup = on_exit([&] { if (handle) released = true; });
(void)handle;
}
Void::Testing::expect(released);
}
Void::Testing::Test on_exit_conditional_commit()
{
int value = 10;
{
auto rollback = on_exit([&] { value = 0; });
rollback.dismiss();
}
Void::Testing::expect_eq(value, 10);
}
// ── on_success ────────────────────────────────────────────────────────────────
Void::Testing::Test on_success_runs_on_normal_exit()
{
bool ran = false;
{
auto guard = on_success([&] { ran = true; });
}
Void::Testing::expect(ran);
}
Void::Testing::Test on_success_does_not_run_on_exception()
{
bool ran = false;
try {
auto guard = on_success([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_success_release_prevents_run()
{
bool ran = false;
{
auto guard = on_success([&] { ran = true; });
guard.release();
}
Void::Testing::expect(!ran);
}
// ── on_fail ───────────────────────────────────────────────────────────────────
Void::Testing::Test on_fail_does_not_run_on_normal_exit()
{
bool ran = false;
{
auto guard = on_fail([&] { ran = true; });
}
Void::Testing::expect(!ran);
}
Void::Testing::Test on_fail_runs_on_exception()
{
bool ran = false;
try {
auto guard = on_fail([&] { ran = true; });
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(ran);
}
Void::Testing::Test on_fail_release_prevents_run()
{
bool ran = false;
try {
auto guard = on_fail([&] { ran = true; });
guard.release();
throw std::runtime_error{"test"};
} catch (...) {}
Void::Testing::expect(!ran);
}
// ── scope_exit / scope_success / scope_failure aliases ───────────────────────
Void::Testing::Test scope_exit_alias_fires()
{
int called = 0;
{
auto g = scope_exit([&] { ++called; });
}
Void::Testing::expect_eq(called, 1);
}
Void::Testing::Test scope_success_alias_fires_on_normal()
{
int called = 0;
{
auto g = scope_success([&] { ++called; });
}
Void::Testing::expect_eq(called, 1);
}
Void::Testing::Test scope_failure_alias_fires_on_exception()
{
int called = 0;
try {
auto g = scope_failure([&] { ++called; });
throw std::runtime_error{"x"};
} catch (...) {}
Void::Testing::expect_eq(called, 1);
}