diff options
Diffstat (limited to 'nihil.ucl/tests')
| -rw-r--r-- | nihil.ucl/tests/array.cc | 53 | ||||
| -rw-r--r-- | nihil.ucl/tests/boolean.cc | 18 | ||||
| -rw-r--r-- | nihil.ucl/tests/emit.cc | 24 | ||||
| -rw-r--r-- | nihil.ucl/tests/integer.cc | 16 | ||||
| -rw-r--r-- | nihil.ucl/tests/object.cc | 15 | ||||
| -rw-r--r-- | nihil.ucl/tests/parse.cc | 14 | ||||
| -rw-r--r-- | nihil.ucl/tests/real.cc | 17 | ||||
| -rw-r--r-- | nihil.ucl/tests/string.cc | 15 |
8 files changed, 126 insertions, 46 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); } diff --git a/nihil.ucl/tests/boolean.cc b/nihil.ucl/tests/boolean.cc index 49dc408..495071d 100644 --- a/nihil.ucl/tests/boolean.cc +++ b/nihil.ucl/tests/boolean.cc @@ -62,8 +62,11 @@ TEST_CASE("ucl: boolean: key()", "[ucl]") { using namespace nihil::ucl; - auto obj = parse("a_bool = true"); - REQUIRE(object_cast<boolean>(obj["a_bool"]).key() == "a_bool"); + auto err = parse("a_bool = true"); + REQUIRE(err); + + auto obj = *err; + REQUIRE(object_cast<boolean>(obj["a_bool"])->key() == "a_bool"); auto b = nihil::ucl::boolean(true); REQUIRE(b.key() == ""); @@ -95,10 +98,14 @@ TEST_CASE("ucl: boolean: parse", "[ucl]") { using namespace std::literals; - auto obj = nihil::ucl::parse("value = true"sv); + auto err = nihil::ucl::parse("value = true"sv); + REQUIRE(err); + + auto obj = *err; + auto v = obj["value"]; REQUIRE(v.key() == "value"); - REQUIRE(object_cast<nihil::ucl::boolean>(v) == true); + REQUIRE(*object_cast<nihil::ucl::boolean>(v) == true); } TEST_CASE("ucl: boolean: emit", "[ucl]") @@ -111,9 +118,10 @@ TEST_CASE("ucl: boolean: emit", "[ucl]") TEST_CASE("ucl: boolean: parse and emit", "[ucl]") { auto ucl = nihil::ucl::parse("bool = true;"); + REQUIRE(ucl); auto output = std::string(); - emit(ucl, nihil::ucl::emitter::configuration, + emit(*ucl, nihil::ucl::emitter::configuration, std::back_inserter(output)); REQUIRE(output == "bool = true;\n"); diff --git a/nihil.ucl/tests/emit.cc b/nihil.ucl/tests/emit.cc index a8487c6..d75255b 100644 --- a/nihil.ucl/tests/emit.cc +++ b/nihil.ucl/tests/emit.cc @@ -15,11 +15,13 @@ TEST_CASE("ucl: emit to std::ostream", "[ucl]") using namespace std::literals; auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv); + REQUIRE(obj); + auto strm = std::ostringstream(); - strm << obj; + strm << *obj; // The ostream emitter produces JSON. - REQUIRE(strm.str() == std::format("{:j}", obj)); + REQUIRE(strm.str() == std::format("{:j}", *obj)); } TEST_CASE("ucl: emit JSON with std::format", "[ucl]") @@ -27,7 +29,9 @@ TEST_CASE("ucl: emit JSON with std::format", "[ucl]") using namespace std::literals; auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv); - auto str = std::format("{:j}", obj); + REQUIRE(obj); + + auto str = std::format("{:j}", *obj); REQUIRE(str == "{\n" @@ -39,7 +43,7 @@ TEST_CASE("ucl: emit JSON with std::format", "[ucl]") "}"); // Make sure JSON is the default format. - auto str2 = std::format("{}", obj); + auto str2 = std::format("{}", *obj); REQUIRE(str == str2); } @@ -48,7 +52,9 @@ TEST_CASE("ucl: emit compact JSON with std::format", "[ucl]") using namespace std::literals; auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv); - auto str = std::format("{:J}", obj); + REQUIRE(obj); + + auto str = std::format("{:J}", *obj); REQUIRE(str == "{\"int\":[1,42,666]}"); } @@ -58,7 +64,9 @@ TEST_CASE("ucl: emit configuration with std::format", "[ucl]") using namespace std::literals; auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv); - auto str = std::format("{:c}", obj); + REQUIRE(obj); + + auto str = std::format("{:c}", *obj); REQUIRE(str == "int [\n" @@ -73,7 +81,9 @@ TEST_CASE("ucl: emit YAML with std::format", "[ucl]") using namespace std::literals; auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv); - auto str = std::format("{:y}", obj); + REQUIRE(obj); + + auto str = std::format("{:y}", *obj); REQUIRE(str == "int: [\n" diff --git a/nihil.ucl/tests/integer.cc b/nihil.ucl/tests/integer.cc index 811a864..05647fe 100644 --- a/nihil.ucl/tests/integer.cc +++ b/nihil.ucl/tests/integer.cc @@ -63,8 +63,11 @@ TEST_CASE("ucl: integer: key()", "[ucl]") { using namespace nihil::ucl; - auto obj = parse("an_int = 42"); - REQUIRE(object_cast<integer>(obj["an_int"]).key() == "an_int"); + auto err = parse("an_int = 42"); + REQUIRE(err); + + auto obj = *err; + REQUIRE(object_cast<integer>(obj["an_int"])->key() == "an_int"); auto i = nihil::ucl::integer(42); REQUIRE(i.key() == ""); @@ -96,7 +99,11 @@ TEST_CASE("ucl: integer: parse", "[ucl]") { using namespace std::literals; - auto obj = nihil::ucl::parse("value = 42"sv); + auto err = nihil::ucl::parse("value = 42"sv); + REQUIRE(err); + + auto obj = *err; + auto v = obj["value"]; REQUIRE(v.key() == "value"); REQUIRE(object_cast<nihil::ucl::integer>(v) == 42); @@ -112,9 +119,10 @@ TEST_CASE("ucl: integer: emit", "[ucl]") TEST_CASE("ucl: integer: parse and emit", "[ucl]") { auto ucl = nihil::ucl::parse("int = 42;"); + REQUIRE(ucl); auto output = std::string(); - emit(ucl, nihil::ucl::emitter::configuration, + emit(*ucl, nihil::ucl::emitter::configuration, std::back_inserter(output)); REQUIRE(output == "int = 42;\n"); diff --git a/nihil.ucl/tests/object.cc b/nihil.ucl/tests/object.cc index 1bbcf4f..3ad180e 100644 --- a/nihil.ucl/tests/object.cc +++ b/nihil.ucl/tests/object.cc @@ -26,12 +26,19 @@ TEST_CASE("ucl object: compare", "[ucl]") using namespace std::literals; auto obj_41 = nihil::ucl::parse("int = 41;"sv); + REQUIRE(obj_41); + auto obj_42 = nihil::ucl::parse("int = 42;"sv); + REQUIRE(obj_42); + auto obj_42_2 = nihil::ucl::parse("int = 42;"sv); + REQUIRE(obj_42_2); + auto obj_43 = nihil::ucl::parse("int = 43;"sv); + REQUIRE(obj_43); - REQUIRE(obj_42 == obj_42_2); - REQUIRE(obj_42 != obj_43); - REQUIRE(obj_42 < obj_43); - REQUIRE(obj_42 > obj_41); + REQUIRE(*obj_42 == *obj_42_2); + REQUIRE(*obj_42 != *obj_43); + REQUIRE(*obj_42 < *obj_43); + REQUIRE(*obj_42 > *obj_41); } diff --git a/nihil.ucl/tests/parse.cc b/nihil.ucl/tests/parse.cc index 3cf5742..c56974e 100644 --- a/nihil.ucl/tests/parse.cc +++ b/nihil.ucl/tests/parse.cc @@ -15,13 +15,18 @@ TEST_CASE("ucl parse: iterate array", "[ucl]") using namespace std::literals; using namespace nihil::ucl; - auto obj = parse("value = [1, 42, 666];"sv); + auto err = parse("value = [1, 42, 666];"sv); + REQUIRE(err); + + auto obj = *err; auto arr = obj["value"]; REQUIRE(arr.key() == "value"); - auto vec = std::vector(std::from_range, - object_cast<array<integer>>(arr)); + auto ints = object_cast<array<integer>>(arr); + REQUIRE(ints); + + auto vec = std::vector(std::from_range, *ints); REQUIRE(vec.size() == 3); REQUIRE(vec[0] == 1); @@ -36,8 +41,9 @@ TEST_CASE("ucl parse: iterate hash", "[ucl]") auto input = "int = 42; bool = true; str = \"test\";"sv; auto obj = parse(input); + REQUIRE(obj); - for (auto &&[key, value] : obj) { + for (auto &&[key, value] : *obj) { REQUIRE(key == value.key()); if (key == "int") diff --git a/nihil.ucl/tests/real.cc b/nihil.ucl/tests/real.cc index b11c113..be4e213 100644 --- a/nihil.ucl/tests/real.cc +++ b/nihil.ucl/tests/real.cc @@ -31,7 +31,7 @@ TEST_CASE("ucl: real: invariants", "[ucl]") TEST_CASE("ucl: real: construct", "[ucl]") { auto obj = nihil::ucl::real(42.1); - REQUIRE_THAT(object_cast<nihil::ucl::real>(obj).value(), + REQUIRE_THAT(object_cast<nihil::ucl::real>(obj)->value(), Catch::Matchers::WithinRel(42.1)); } @@ -80,19 +80,26 @@ TEST_CASE("ucl: real: parse", "[ucl]") { using namespace std::literals; - auto obj = nihil::ucl::parse("value = 42.1"sv); + auto err = nihil::ucl::parse("value = 42.1"sv); + REQUIRE(err); + + auto obj = *err; + auto v = obj["value"]; REQUIRE(v.key() == "value"); - REQUIRE_THAT(object_cast<nihil::ucl::real>(v).value(), + REQUIRE_THAT(object_cast<nihil::ucl::real>(v)->value(), Catch::Matchers::WithinRel(42.1)); } TEST_CASE("ucl: real: emit", "[ucl]") { - auto ucl = nihil::ucl::parse("real = 42.2"); + auto err = nihil::ucl::parse("real = 42.2"); + REQUIRE(err); + + auto obj = *err; auto output = std::string(); - emit(ucl, nihil::ucl::emitter::configuration, + emit(obj, nihil::ucl::emitter::configuration, std::back_inserter(output)); REQUIRE(output == "real = 42.2;\n"); diff --git a/nihil.ucl/tests/string.cc b/nihil.ucl/tests/string.cc index e7eb0ad..995e95a 100644 --- a/nihil.ucl/tests/string.cc +++ b/nihil.ucl/tests/string.cc @@ -109,8 +109,11 @@ TEST_CASE("ucl: string: key()", "[ucl]") { using namespace nihil::ucl; - auto obj = parse("a_string = \"test\""); - REQUIRE(object_cast<string>(obj["a_string"]).key() == "a_string"); + auto err = parse("a_string = \"test\""); + REQUIRE(err); + + auto obj = *err; + REQUIRE(object_cast<string>(obj["a_string"])->key() == "a_string"); auto s = nihil::ucl::string("test"); REQUIRE(s.key() == ""); @@ -188,7 +191,10 @@ TEST_CASE("ucl: string: parse", "[ucl]") { using namespace std::literals; - auto obj = nihil::ucl::parse("value = \"te\\\"st\""sv); + auto err = nihil::ucl::parse("value = \"te\\\"st\""sv); + REQUIRE(err); + + auto obj = *err; auto v = obj["value"]; REQUIRE(v.key() == "value"); REQUIRE(object_cast<nihil::ucl::string>(v) == "te\"st"); @@ -204,9 +210,10 @@ TEST_CASE("ucl: string: emit", "[ucl]") TEST_CASE("ucl: string: parse and emit", "[ucl]") { auto ucl = nihil::ucl::parse("str = \"te\\\"st\";"); + REQUIRE(ucl); auto output = std::string(); - emit(ucl, nihil::ucl::emitter::configuration, + emit(*ucl, nihil::ucl::emitter::configuration, std::back_inserter(output)); REQUIRE(output == "str = \"te\\\"st\";\n"); |
