aboutsummaryrefslogtreecommitdiffstats
path: root/nihil/tests
diff options
context:
space:
mode:
Diffstat (limited to 'nihil/tests')
-rw-r--r--nihil/tests/CMakeLists.txt4
-rw-r--r--nihil/tests/generic_error.cc23
-rw-r--r--nihil/tests/monad.cc68
3 files changed, 90 insertions, 5 deletions
diff --git a/nihil/tests/CMakeLists.txt b/nihil/tests/CMakeLists.txt
index abeab88..25111c2 100644
--- a/nihil/tests/CMakeLists.txt
+++ b/nihil/tests/CMakeLists.txt
@@ -8,10 +8,12 @@ add_executable(nihil.test
generic_error.cc
getenv.cc
guard.cc
+ monad.cc
next_word.cc
skipws.cc
spawn.cc
- tabulate.cc)
+ tabulate.cc
+)
target_link_libraries(nihil.test PRIVATE
nihil
diff --git a/nihil/tests/generic_error.cc b/nihil/tests/generic_error.cc
index b213af9..ee3eccd 100644
--- a/nihil/tests/generic_error.cc
+++ b/nihil/tests/generic_error.cc
@@ -6,12 +6,27 @@
import nihil;
-using namespace std::literals;
-
TEST_CASE("generic_error: basic", "[generic_error]") {
+ using namespace std::literals;
+
+ // C string
+ try {
+ throw nihil::generic_error("test error");
+ } catch (nihil::generic_error const &exc) {
+ REQUIRE(exc.what() == "test error"sv);
+ }
+
+ // std::string
+ try {
+ throw nihil::generic_error("test error"s);
+ } catch (nihil::generic_error const &exc) {
+ REQUIRE(exc.what() == "test error"sv);
+ }
+
+ // std::string_view
try {
- throw nihil::generic_error("{} + {} = {}", 1, 2, 3);
+ throw nihil::generic_error("test error"sv);
} catch (nihil::generic_error const &exc) {
- REQUIRE(exc.what() == "1 + 2 = 3"s);
+ REQUIRE(exc.what() == "test error"sv);
}
}
diff --git a/nihil/tests/monad.cc b/nihil/tests/monad.cc
new file mode 100644
index 0000000..3964494
--- /dev/null
+++ b/nihil/tests/monad.cc
@@ -0,0 +1,68 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+#include <coroutine>
+#include <expected>
+#include <optional>
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil;
+
+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");
+}