aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/tests/array.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-26 20:47:45 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-26 20:47:45 +0100
commit90aa957ca9b7c217af7569009d1675e0f3ff8e9b (patch)
treee6a61ca2b6928e6414372b9b1484ce80fa2fb0b3 /nihil.ucl/tests/array.cc
parent1db86c401df11423c945634d8b2a483e97afa878 (diff)
downloadnihil-90aa957ca9b7c217af7569009d1675e0f3ff8e9b.tar.gz
nihil-90aa957ca9b7c217af7569009d1675e0f3ff8e9b.tar.bz2
ucl, config: use monadic error handling more
Diffstat (limited to 'nihil.ucl/tests/array.cc')
-rw-r--r--nihil.ucl/tests/array.cc53
1 files changed, 40 insertions, 13 deletions
diff --git a/nihil.ucl/tests/array.cc b/nihil.ucl/tests/array.cc
index ce0976f..fb23178 100644
--- a/nihil.ucl/tests/array.cc
+++ b/nihil.ucl/tests/array.cc
@@ -4,12 +4,14 @@
#include <algorithm>
#include <concepts>
+#include <expected>
#include <ranges>
#include <string>
#include <catch2/catch_test_macros.hpp>
#include <ucl.h>
+import nihil;
import nihil.ucl;
TEST_CASE("ucl: array: invariants", "[ucl]")
@@ -154,8 +156,14 @@ TEST_CASE("ucl: array: parse", "[ucl]")
using namespace std::literals;
using namespace nihil::ucl;
- auto obj = parse("value = [1, 42, 666]"sv);
- auto arr = object_cast<array<integer>>(obj["value"]);
+ auto obj_err = parse("value = [1, 42, 666]"sv);
+ REQUIRE(obj_err);
+ auto obj = *obj_err;
+
+ auto err = object_cast<array<integer>>(obj["value"]);
+ REQUIRE(err);
+
+ auto arr = *err;
REQUIRE(arr.size() == 3);
REQUIRE(arr[0] == 1);
REQUIRE(arr[1] == 42);
@@ -167,7 +175,9 @@ TEST_CASE("ucl: array: emit", "[ucl]")
using namespace nihil::ucl;
auto ucl = parse("array = [1, 42, 666];");
- auto output = std::format("{:c}", ucl);
+ REQUIRE(ucl);
+
+ auto output = std::format("{:c}", *ucl);
REQUIRE(output ==
"array [\n"
" 1,\n"
@@ -211,7 +221,8 @@ TEST_CASE("ucl: array: bad object_cast", "[ucl]")
auto arr = array<integer>();
- REQUIRE_THROWS_AS(object_cast<integer>(arr), type_mismatch);
+ auto cast_ok = object_cast<integer>(arr);
+ REQUIRE(!cast_ok);
}
TEST_CASE("ucl: array: heterogeneous elements", "[ucl]")
@@ -219,19 +230,27 @@ TEST_CASE("ucl: array: heterogeneous elements", "[ucl]")
using namespace std::literals;
using namespace nihil::ucl;
- auto obj = parse("array [ 42, true, \"test\" ];");
- auto arr = object_cast<array<>>(obj["array"]);
+ auto obj_err = parse("array [ 42, true, \"test\" ];");
+ REQUIRE(obj_err);
+ auto obj = *obj_err;
+ auto err = object_cast<array<>>(obj["array"]);
+ REQUIRE(err);
+
+ auto arr = *err;
REQUIRE(arr.size() == 3);
auto int_obj = object_cast<integer>(arr[0]);
- REQUIRE(int_obj == 42);
+ REQUIRE(int_obj);
+ REQUIRE(*int_obj == 42);
auto bool_obj = object_cast<boolean>(arr[1]);
- REQUIRE(bool_obj == true);
+ REQUIRE(bool_obj);
+ REQUIRE(*bool_obj == true);
auto string_obj = object_cast<string>(arr[2]);
- REQUIRE(string_obj == "test");
+ REQUIRE(string_obj);
+ REQUIRE(*string_obj == "test");
}
TEST_CASE("ucl: array: heterogenous cast", "[ucl]")
@@ -243,10 +262,14 @@ TEST_CASE("ucl: array: heterogenous cast", "[ucl]")
arr.push_back(boolean(true));
// Converting to an array<integer> should fail.
- REQUIRE_THROWS_AS(object_cast<array<integer>>(arr), type_mismatch);
+ auto cast_ok = object_cast<array<integer>>(arr);
+ REQUIRE(!cast_ok);
// Converting to array<object> should succeed.
- auto obj_arr = object_cast<array<object>>(arr);
+ auto err = object_cast<array<object>>(arr);
+ REQUIRE(err);
+
+ auto obj_arr = *err;
REQUIRE(obj_arr[0] == integer(42));
}
@@ -261,10 +284,14 @@ TEST_CASE("ucl: array: homogeneous cast", "[ucl]")
auto obj = object(ref, arr.get_ucl_object());
// Converting to array<string> should fail.
- REQUIRE_THROWS_AS(object_cast<array<string>>(obj), type_mismatch);
+ auto cast_ok = object_cast<array<string>>(obj);
+ REQUIRE(!cast_ok);
// Converting to an array<integer> should succeed.
- auto obj_arr = object_cast<array<integer>>(obj);
+ auto err = object_cast<array<integer>>(obj);
+ REQUIRE(err);
+
+ auto obj_arr = *err;
REQUIRE(obj_arr[0] == 1);
REQUIRE(obj_arr[1] == 42);
}