// This source code is released into the public domain. #include import nihil.std; import nihil.core; namespace { TEST_CASE("monad: co_await std::optional<> with value", "[nihil]") { auto get_value = [] -> std::optional { return 42; }; auto try_get_value = [&get_value] -> std::optional { co_return co_await get_value(); }; auto o = try_get_value(); REQUIRE(o == 42); } TEST_CASE("monad: co_await std::optional<> without value", "[nihil]") { auto get_value = [] -> std::optional { return {}; }; auto try_get_value = [&get_value] -> std::optional { co_return co_await get_value(); }; auto o = try_get_value(); REQUIRE(!o.has_value()); } TEST_CASE("monad: co_await std::expected<> with value", "[nihil]") { auto get_value = [] -> std::expected { return 42; }; auto try_get_value = [&get_value] -> std::expected { co_return co_await get_value(); }; auto o = try_get_value(); REQUIRE(o == 42); } TEST_CASE("monad: co_await std::expected<> with error", "[nihil]") { auto get_value = [] -> std::expected { return std::unexpected("error"); }; auto try_get_value = [&get_value] -> std::expected { co_return co_await get_value(); }; auto o = try_get_value(); REQUIRE(!o); REQUIRE(o.error() == "error"); } } // anonymous namespace