#include #include #include #include #include #include using namespace Void::Optional; // ── free functions ──────────────────────────────────────────────────────────── Void::Testing::Test opt_transform_present() { const std::optional o{5}; const auto r = transform(o, [](int x) { return x * 2; }); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, 10); } Void::Testing::Test opt_transform_empty() { const std::optional o; const auto r = transform(o, [](int x) { return x * 2; }); Void::Testing::expect(!r.has_value()); } Void::Testing::Test opt_and_then_chain() { const std::optional o{4}; const auto r = and_then(o, [](int x) -> std::optional { if (x > 0) return std::to_string(x); return std::nullopt; }); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, std::string{"4"}); } Void::Testing::Test opt_and_then_short_circuits() { const std::optional o; bool called = false; const auto r = and_then(o, [&](int) -> std::optional { called = true; return 1; }); Void::Testing::expect(!called); Void::Testing::expect(!r.has_value()); } Void::Testing::Test opt_filter_passes() { const std::optional o{10}; const auto r = filter(o, [](int x) { return x > 5; }); Void::Testing::expect(r.has_value()); } Void::Testing::Test opt_filter_blocks() { const std::optional o{3}; const auto r = filter(o, [](int x) { return x > 5; }); Void::Testing::expect(!r.has_value()); } Void::Testing::Test opt_expect_present() { const std::optional o{42}; Void::Testing::expect_eq(expect(o), 42); } Void::Testing::Test opt_expect_throws_on_empty() { const std::optional o; bool threw = false; try { [[maybe_unused]] auto v = expect(o, "must have value"); } catch (...) { threw = true; } Void::Testing::expect(threw); } Void::Testing::Test opt_zip_both_present() { const std::optional a{1}; const std::optional b{"hello"}; const auto r = zip(a, b); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(r->first, 1); Void::Testing::expect_eq(r->second, std::string{"hello"}); } Void::Testing::Test opt_zip_one_empty() { const std::optional a{1}; const std::optional b; const auto r = zip(a, b); Void::Testing::expect(!r.has_value()); } Void::Testing::Test opt_flatten() { const std::optional> o{std::optional{7}}; const auto r = flatten(o); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, 7); } Void::Testing::Test opt_flatten_inner_empty() { const std::optional> o{std::optional{}}; Void::Testing::expect(!flatten(o).has_value()); } Void::Testing::Test opt_or_else_calls_on_empty() { const std::optional o; const auto r = or_else(o, [] { return std::optional{99}; }); Void::Testing::expect_eq(*r, 99); } Void::Testing::Test opt_or_else_skips_on_present() { const std::optional o{5}; bool called = false; const auto r = or_else(o, [&] { called = true; return std::optional{99}; }); Void::Testing::expect(!called); Void::Testing::expect_eq(*r, 5); } // ── Opt fluent wrapper ───────────────────────────────────────────────────── Void::Testing::Test opt_some_has_value() { const auto o = some(42); Void::Testing::expect(o.has_value()); Void::Testing::expect_eq(*o, 42); } Void::Testing::Test opt_none_empty() { const auto o = none(); Void::Testing::expect(!o.has_value()); } Void::Testing::Test opt_map_chain() { const auto r = some(3) .map([](int x) { return x + 1; }) .map([](int x) { return x * 2; }); Void::Testing::expect_eq(*r, 8); } Void::Testing::Test opt_filter_fluent() { const auto r = some(10).filter([](int x) { return x > 5; }); Void::Testing::expect(r.has_value()); const auto r2 = some(2).filter([](int x) { return x > 5; }); Void::Testing::expect(!r2.has_value()); } Void::Testing::Test opt_flat_map_fluent() { const auto r = some(5).flat_map([](int x) -> Opt { return some(std::to_string(x)); }); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, std::string{"5"}); } Void::Testing::Test opt_value_or() { Void::Testing::expect_eq(some(7).value_or(0), 7); Void::Testing::expect_eq(none().value_or(99), 99); }