Reflection and filesystem

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-05-03 18:09:46 +03:00
1 parent f0825fbd1a
commit 42268c6c35
45 files changed
+2732 -262

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
#include <Void/Result/Error.hpp>
#include <Void/Result/Result.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
namespace
{
Void::Result::Result<int> divide(int n, int d)
{
if (d == 0) {
return Void::Result::fail("division by zero");
}
return Void::Result::ok(n / d);
}
} // namespace
Void::Testing::Test ok_constructs_success()
{
auto r = divide(10, 2);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 5);
}
Void::Testing::Test fail_constructs_failure()
{
auto r = divide(10, 0);
Void::Testing::expect(!r.has_value());
Void::Testing::expect_eq(r.error().message(),
std::string{"division by zero"});
}
Void::Testing::Test value_or_returns_fallback_on_error()
{
auto r = divide(10, 0);
Void::Testing::expect_eq(r.value_or(-1), -1);
}
Void::Testing::Test bool_conversion()
{
Void::Testing::expect(static_cast<bool>(divide(10, 2)));
Void::Testing::expect(!static_cast<bool>(divide(10, 0)));
}
+56
View File
@@ -0,0 +1,56 @@
#include <Void/Result/Error.hpp>
#include <Void/Result/Result.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
namespace
{
Void::Result::Result<int> read_file()
{
return Void::Result::fail("disk read error");
}
Void::Result::Result<int> parse_config()
{
return Void::Result::with_context(read_file(), "parsing config.toml");
}
Void::Result::Result<int> load()
{
return Void::Result::with_context(parse_config(), "starting up");
}
} // namespace
Void::Testing::Test context_chain_preserves_root_message()
{
auto r = load();
Void::Testing::expect(!r.has_value());
const auto& e = r.error();
Void::Testing::expect_eq(e.message(), std::string{"starting up"});
Void::Testing::expect(e.cause() != nullptr);
Void::Testing::expect_eq(e.cause()->message(),
std::string{"parsing config.toml"});
Void::Testing::expect_eq(e.cause()->cause()->message(),
std::string{"disk read error"});
}
Void::Testing::Test format_renders_full_chain()
{
auto r = load();
const auto rendered = r.error().format();
Void::Testing::expect(rendered.find("starting up") != std::string::npos);
Void::Testing::expect(rendered.find("parsing config.toml") !=
std::string::npos);
Void::Testing::expect(rendered.find("disk read error") !=
std::string::npos);
Void::Testing::expect(rendered.find("caused by:") != std::string::npos);
}
Void::Testing::Test with_context_no_op_on_success()
{
auto r = Void::Result::with_context(Void::Result::ok(42), "unused context");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 42);
}
+49
View File
@@ -0,0 +1,49 @@
#include <Void/Result/Error.hpp>
#include <Void/Result/Result.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
namespace
{
Void::Result::Result<int> parse_int(const std::string& s)
{
try {
return Void::Result::ok(std::stoi(s));
} catch (...) {
return Void::Result::fail("not a number");
}
}
Void::Result::Result<int> doubled(int n) { return Void::Result::ok(n * 2); }
} // namespace
Void::Testing::Test and_then_chains_on_success()
{
auto r = parse_int("21").and_then(doubled);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 42);
}
Void::Testing::Test and_then_short_circuits_on_failure()
{
auto r = parse_int("xyz").and_then(doubled);
Void::Testing::expect(!r.has_value());
Void::Testing::expect_eq(r.error().message(), std::string{"not a number"});
}
Void::Testing::Test transform_maps_value()
{
auto r = parse_int("3").transform([](int n) { return n + 100; });
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 103);
}
Void::Testing::Test or_else_recovers_on_failure()
{
auto r = parse_int("xyz").or_else(
[](const Void::Result::Error&) { return Void::Result::ok(0); });
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 0);
}
@@ -0,0 +1,54 @@
#include <Void/Result/Detail/Repr.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
namespace
{
struct PlainStruct {
int code;
std::string label;
};
struct DiagInfo {
std::string file;
int line;
std::string detail;
};
} // namespace
Void::Testing::Test repr_uses_std_format_when_available()
{
const auto out = Void::Result::Detail::repr(42);
Void::Testing::expect_eq(out, std::string{"42"});
}
Void::Testing::Test repr_uses_std_format_for_string()
{
const auto out = Void::Result::Detail::repr(std::string{"hello"});
Void::Testing::expect_eq(out, std::string{"hello"});
}
#ifdef VOID_RESULT_HAS_REFLECTION
// @tag reflection
Void::Testing::Test repr_walks_struct_fields_via_reflection()
{
PlainStruct s{42, "ok"};
const auto out = Void::Result::Detail::repr(s);
Void::Testing::expect(out.find("PlainStruct") != std::string::npos);
Void::Testing::expect(out.find("code: 42") != std::string::npos);
Void::Testing::expect(out.find("label: ok") != std::string::npos);
}
// @tag reflection
Void::Testing::Test repr_handles_multiple_fields()
{
DiagInfo d{"foo.cpp", 17, "bad token"};
const auto out = Void::Result::Detail::repr(d);
Void::Testing::expect(out.find("DiagInfo") != std::string::npos);
Void::Testing::expect(out.find("file: foo.cpp") != std::string::npos);
Void::Testing::expect(out.find("line: 17") != std::string::npos);
Void::Testing::expect(out.find("detail: bad token") != std::string::npos);
}
#endif