aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.util/monad.test.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-07-02 03:25:28 +0100
committerLexi Winter <lexi@le-fay.org>2025-07-02 03:25:28 +0100
commita4607e29540a9352c35afff17193ceeab137cc9d (patch)
tree0e9c2ea9c94f17b81f222fd6ebf1ccd75bb1f7f8 /nihil.util/monad.test.cc
parentbde0492644845de63cf95b8997c5e613a9247826 (diff)
downloadnihil-a4607e29540a9352c35afff17193ceeab137cc9d.tar.gz
nihil-a4607e29540a9352c35afff17193ceeab137cc9d.tar.bz2
move monad to util
Diffstat (limited to 'nihil.util/monad.test.cc')
-rw-r--r--nihil.util/monad.test.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/nihil.util/monad.test.cc b/nihil.util/monad.test.cc
new file mode 100644
index 0000000..bc9e406
--- /dev/null
+++ b/nihil.util/monad.test.cc
@@ -0,0 +1,66 @@
+// This source code is released into the public domain.
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil.std;
+import nihil.error;
+import nihil.util;
+
+namespace {
+TEST_CASE("monad: co_await std::optional<> with value", "[nihil]")
+{
+ auto get_value = [] -> std::optional<int> {
+ return 42;
+ };
+
+ auto try_get_value = [&get_value] -> std::optional<int> {
+ 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<int> {
+ return {};
+ };
+
+ auto try_get_value = [&get_value] -> std::optional<int> {
+ 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<int, std::string> {
+ return 42;
+ };
+
+ auto try_get_value = [&get_value] -> std::expected<int, std::string> {
+ 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<int, std::string> {
+ return std::unexpected("error");
+ };
+
+ auto try_get_value = [&get_value] -> std::expected<int, std::string> {
+ co_return co_await get_value();
+ };
+
+ auto o = try_get_value();
+ REQUIRE(!o);
+ REQUIRE(o.error() == "error");
+}
+} // anonymous namespace