Void::Scope — RAII scope guards: ScopeExit/ScopeSuccess/ScopeFail with on_exit/on_success/on_fail factory functions. No macros. Void::StateMachine — compile-time FSM driven by Transition<From,Ev,To> type lists. Supports typed entry/exit callbacks and returns bool from send() indicating whether the transition was valid. Void::TypeList — compile-time type list with: size_of, at, front, back, contains, index_of, push_front/back, pop_front, concat, filter, transform, reverse, unique, for_each, all_of/any_of/none_of. Void::Encoding — Base64 (standard + URL-safe encode/decode) and Hex encode/decode. Included in previous commit but missed in message. 36/36 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
122 lines
3.3 KiB
C++
122 lines
3.3 KiB
C++
#include <Void/Scope/Scope.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
// ── on_exit ───────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test on_exit_runs_on_normal_exit()
|
|
{
|
|
bool ran = false;
|
|
{
|
|
auto guard = Void::Scope::on_exit([&] { ran = true; });
|
|
}
|
|
Void::Testing::expect(ran);
|
|
}
|
|
|
|
Void::Testing::Test on_exit_runs_on_exception()
|
|
{
|
|
bool ran = false;
|
|
try {
|
|
auto guard = Void::Scope::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 = Void::Scope::on_exit([&] { ran = true; });
|
|
guard.release();
|
|
}
|
|
Void::Testing::expect(!ran);
|
|
}
|
|
|
|
Void::Testing::Test on_exit_lifo_order()
|
|
{
|
|
std::string order;
|
|
{
|
|
auto a = Void::Scope::on_exit([&] { order += "A"; });
|
|
auto b = Void::Scope::on_exit([&] { order += "B"; });
|
|
auto c = Void::Scope::on_exit([&] { order += "C"; });
|
|
}
|
|
Void::Testing::expect_eq(order, std::string{"CBA"});
|
|
}
|
|
|
|
Void::Testing::Test on_exit_captures_by_ref()
|
|
{
|
|
int count = 0;
|
|
{
|
|
auto guard = Void::Scope::on_exit([&] { count += 10; });
|
|
count = 5;
|
|
}
|
|
Void::Testing::expect_eq(count, 15);
|
|
}
|
|
|
|
// ── on_success ────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test on_success_runs_on_normal_exit()
|
|
{
|
|
bool ran = false;
|
|
{
|
|
auto guard = Void::Scope::on_success([&] { ran = true; });
|
|
}
|
|
Void::Testing::expect(ran);
|
|
}
|
|
|
|
Void::Testing::Test on_success_does_not_run_on_exception()
|
|
{
|
|
bool ran = false;
|
|
try {
|
|
auto guard = Void::Scope::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 = Void::Scope::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 = Void::Scope::on_fail([&] { ran = true; });
|
|
}
|
|
Void::Testing::expect(!ran);
|
|
}
|
|
|
|
Void::Testing::Test on_fail_runs_on_exception()
|
|
{
|
|
bool ran = false;
|
|
try {
|
|
auto guard = Void::Scope::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 = Void::Scope::on_fail([&] { ran = true; });
|
|
guard.release();
|
|
throw std::runtime_error{"test"};
|
|
} catch (...) {}
|
|
Void::Testing::expect(!ran);
|
|
}
|