#include #include #include #include #include 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); }