From f41970666675f873d7c1075efd192f22df8d17fe Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 22 Jun 2025 14:46:53 +0100 Subject: add nihil.ucl (incomplete) --- nihil.ucl/tests/CMakeLists.txt | 23 +++++++++ nihil.ucl/tests/array.cc | 109 +++++++++++++++++++++++++++++++++++++++++ nihil.ucl/tests/boolean.cc | 50 +++++++++++++++++++ nihil.ucl/tests/emit.cc | 8 +++ nihil.ucl/tests/integer.cc | 50 +++++++++++++++++++ nihil.ucl/tests/object.cc | 22 +++++++++ nihil.ucl/tests/parse.cc | 50 +++++++++++++++++++ nihil.ucl/tests/real.cc | 53 ++++++++++++++++++++ nihil.ucl/tests/string.cc | 38 ++++++++++++++ 9 files changed, 403 insertions(+) create mode 100644 nihil.ucl/tests/CMakeLists.txt create mode 100644 nihil.ucl/tests/array.cc create mode 100644 nihil.ucl/tests/boolean.cc create mode 100644 nihil.ucl/tests/emit.cc create mode 100644 nihil.ucl/tests/integer.cc create mode 100644 nihil.ucl/tests/object.cc create mode 100644 nihil.ucl/tests/parse.cc create mode 100644 nihil.ucl/tests/real.cc create mode 100644 nihil.ucl/tests/string.cc (limited to 'nihil.ucl/tests') diff --git a/nihil.ucl/tests/CMakeLists.txt b/nihil.ucl/tests/CMakeLists.txt new file mode 100644 index 0000000..2c4ec5d --- /dev/null +++ b/nihil.ucl/tests/CMakeLists.txt @@ -0,0 +1,23 @@ +# This source code is released into the public domain. + +add_executable(nihil.ucl.test + emit.cc + parse.cc + + object.cc + array.cc + boolean.cc + integer.cc + real.cc + string.cc +) + +target_link_libraries(nihil.ucl.test PRIVATE + nihil nihil.ucl + Catch2::Catch2WithMain) + +find_package(Catch2 REQUIRED) + +include(CTest) +include(Catch) +catch_discover_tests(nihil.ucl.test) diff --git a/nihil.ucl/tests/array.cc b/nihil.ucl/tests/array.cc new file mode 100644 index 0000000..687b02c --- /dev/null +++ b/nihil.ucl/tests/array.cc @@ -0,0 +1,109 @@ +/* + * This source code is released into the public domain. + */ + +#include + +#include + +import nihil.ucl; + +TEST_CASE("ucl: array: construct", "[ucl]") +{ + auto arr = nihil::ucl::array(); + REQUIRE(arr.size() == 0); +} + +TEST_CASE("ucl: array: push_back", "[ucl]") +{ + auto arr = nihil::ucl::array(); + REQUIRE(arr.size() == 0); + + arr.push_back(nihil::ucl::integer(1)); + arr.push_back(nihil::ucl::integer(42)); + arr.push_back(nihil::ucl::integer(666)); + + REQUIRE(arr.size() == 3); + REQUIRE(arr[0].value() == 1); + REQUIRE(arr[1].value() == 42); + REQUIRE(arr[2].value() == 666); + + REQUIRE_THROWS_AS(arr[3], std::out_of_range); + + REQUIRE(arr.front() == 1); + REQUIRE(arr.back() == 666); + +} + +TEST_CASE("ucl: array: compare", "[ucl]") +{ + auto arr = nihil::ucl::array(); + arr.push_back(1); + arr.push_back(42); + arr.push_back(666); + + auto arr2 = nihil::ucl::array(); + REQUIRE(arr != arr2); + + arr2.push_back(1); + arr2.push_back(42); + arr2.push_back(666); + REQUIRE(arr == arr2); + + auto arr3 = nihil::ucl::array(); + arr3.push_back(1); + arr3.push_back(1); + arr3.push_back(1); + REQUIRE(arr != arr3); +} + +TEST_CASE("ucl: array: iterator", "[ucl]") +{ + auto arr = nihil::ucl::array{1, 42, 666}; + + auto it = arr.begin(); + REQUIRE(*it == 1); + + ++it; + REQUIRE(*it == 42); + + ++it; + REQUIRE(*it == 666); + + --it; + REQUIRE(*it == 42); +} + +TEST_CASE("ucl: array: parse", "[ucl]") +{ + using namespace std::literals; + + auto input = "value = [1, 42, 666]"sv; + auto obj = nihil::ucl::parse(input); + auto v = obj.lookup("value"); + + REQUIRE(v); + REQUIRE(v->key() == "value"); + + auto arr = object_cast>(*v); + REQUIRE(arr.size() == 3); + REQUIRE(arr[0] == 1); + REQUIRE(arr[1] == 42); + REQUIRE(arr[2] == 666); +} + +TEST_CASE("ucl: array: emit", "[ucl]") +{ + auto ucl = nihil::ucl::parse("array = [1, 42, 666];"); + + auto output = std::string(); + emit(ucl, nihil::ucl::emitter::configuration, + std::back_inserter(output)); + + REQUIRE(output == +"array [\n" +" 1,\n" +" 42,\n" +" 666,\n" +"]\n"); +} diff --git a/nihil.ucl/tests/boolean.cc b/nihil.ucl/tests/boolean.cc new file mode 100644 index 0000000..c023498 --- /dev/null +++ b/nihil.ucl/tests/boolean.cc @@ -0,0 +1,50 @@ +/* + * This source code is released into the public domain. + */ + +#include + +#include + +import nihil.ucl; + +TEST_CASE("ucl: boolean: construct", "[ucl]") +{ + auto obj = nihil::ucl::boolean(true); + REQUIRE(object_cast(obj).value() == true); +} + +TEST_CASE("ucl: boolean: compare", "[ucl]") +{ + auto b = nihil::ucl::boolean(true); + + REQUIRE(b == nihil::ucl::boolean(true)); + REQUIRE(b == true); + + REQUIRE(b != nihil::ucl::boolean(false)); + REQUIRE(b != false); +} + +TEST_CASE("ucl: boolean: parse", "[ucl]") +{ + using namespace std::literals; + + auto input = "value = true"sv; + auto obj = nihil::ucl::parse(input); + + auto v = obj.lookup("value"); + REQUIRE(v); + REQUIRE(v->key() == "value"); + REQUIRE(object_cast(*v).value() == true); +} + +TEST_CASE("ucl: boolean: emit", "[ucl]") +{ + auto ucl = nihil::ucl::parse("bool = true;"); + + auto output = std::string(); + 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 new file mode 100644 index 0000000..13f5914 --- /dev/null +++ b/nihil.ucl/tests/emit.cc @@ -0,0 +1,8 @@ +/* + * This source code is released into the public domain. + */ + +#include + +import nihil; +import nihil.ucl; diff --git a/nihil.ucl/tests/integer.cc b/nihil.ucl/tests/integer.cc new file mode 100644 index 0000000..da119e6 --- /dev/null +++ b/nihil.ucl/tests/integer.cc @@ -0,0 +1,50 @@ +/* + * This source code is released into the public domain. + */ + +#include + +#include + +import nihil.ucl; + +TEST_CASE("ucl: integer: construct", "[ucl]") +{ + auto obj = nihil::ucl::integer(42); + REQUIRE(object_cast(obj).value() == 42); +} + +TEST_CASE("ucl: integer: compare", "[ucl]") +{ + auto i = nihil::ucl::integer(42); + + REQUIRE(i == nihil::ucl::integer(42)); + REQUIRE(i != nihil::ucl::integer(1)); + REQUIRE((i == 42) == true); + + REQUIRE(i > nihil::ucl::integer(40)); + REQUIRE(i > 40); +} + +TEST_CASE("ucl: parse: integer", "[ucl]") +{ + using namespace std::literals; + + auto input = "value = 42"sv; + auto obj = nihil::ucl::parse(input); + auto v = obj.lookup("value"); + REQUIRE(v); + REQUIRE(v->key() == "value"); + REQUIRE(object_cast(*v).value() == 42); +} + +TEST_CASE("ucl: integer: emit", "[ucl]") +{ + auto ucl = nihil::ucl::parse("int = 42;"); + + auto output = std::string(); + 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 new file mode 100644 index 0000000..f9cef9f --- /dev/null +++ b/nihil.ucl/tests/object.cc @@ -0,0 +1,22 @@ +/* + * This source code is released into the public domain. + */ + +#include + +#include + +import nihil.ucl; + +TEST_CASE("ucl object: get_ucl_object", "[ucl]") +{ + auto obj = nihil::ucl::integer(42); + + REQUIRE(obj.get_ucl_object() != nullptr); + static_assert(std::same_as<::ucl_object_t *, + decltype(obj.get_ucl_object())>); + + auto const cobj = obj; + static_assert(std::same_as<::ucl_object_t const *, + decltype(cobj.get_ucl_object())>); +} diff --git a/nihil.ucl/tests/parse.cc b/nihil.ucl/tests/parse.cc new file mode 100644 index 0000000..3a4f061 --- /dev/null +++ b/nihil.ucl/tests/parse.cc @@ -0,0 +1,50 @@ +/* + * This source code is released into the public domain. + */ + +#include + +#include +#include + +import nihil; +import nihil.ucl; + +TEST_CASE("ucl parse: iterate array", "[ucl]") +{ + using namespace std::literals; + + auto input = "value = [1, 42, 666];"sv; + auto obj = nihil::ucl::parse(input); + + auto array = obj.lookup("value"); + REQUIRE(array); + REQUIRE(array->key() == "value"); + + auto vec = std::vector(); + std::ranges::copy(*array, std::back_inserter(vec)); + REQUIRE(vec.size() == 3); + REQUIRE(object_cast(vec[0]).value() == 1); + REQUIRE(object_cast(vec[1]).value() == 42); + REQUIRE(object_cast(vec[2]).value() == 666); +} + +TEST_CASE("ucl parse: iterate hash", "[ucl]") +{ + using namespace std::literals; + + auto input = "int = 42; bool = true; str = \"test\";"sv; + auto obj = nihil::ucl::parse(input); + + for (auto &&value : obj) { + if (value.key() == "int") + REQUIRE(object_cast(value).value() + == 42); + else if (value.key() == "bool") + REQUIRE(object_cast(value).value() + == true); + else if (value.key() == "str") + REQUIRE(object_cast(value).value() + == "test"); + } +} diff --git a/nihil.ucl/tests/real.cc b/nihil.ucl/tests/real.cc new file mode 100644 index 0000000..275684a --- /dev/null +++ b/nihil.ucl/tests/real.cc @@ -0,0 +1,53 @@ +/* + * This source code is released into the public domain. + */ + +#include + +#include +#include + +import nihil.ucl; + +TEST_CASE("ucl: real: construct", "[ucl]") +{ + auto obj = nihil::ucl::real(42.1); + REQUIRE_THAT(object_cast(obj).value(), + Catch::Matchers::WithinRel(42.1)); +} + +TEST_CASE("ucl: real: compare", "[ucl]") +{ + auto i = nihil::ucl::real(42); + + REQUIRE(i == nihil::ucl::real(42)); + REQUIRE(i != nihil::ucl::real(1)); + REQUIRE((i == 42) == true); + + REQUIRE(i > nihil::ucl::real(40)); + REQUIRE(i > 40); +} + +TEST_CASE("ucl: real: parse", "[ucl]") +{ + using namespace std::literals; + + auto input = "value = 42.1"sv; + auto obj = nihil::ucl::parse(input); + auto v = obj.lookup("value"); + REQUIRE(v); + REQUIRE(v->key() == "value"); + REQUIRE_THAT(object_cast(*v).value(), + Catch::Matchers::WithinRel(42.1)); +} + +TEST_CASE("ucl: real: emit", "[ucl]") +{ + auto ucl = nihil::ucl::parse("real = 42.2"); + + auto output = std::string(); + emit(ucl, 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 new file mode 100644 index 0000000..4385dbb --- /dev/null +++ b/nihil.ucl/tests/string.cc @@ -0,0 +1,38 @@ +/* + * This source code is released into the public domain. + */ + +#include + +#include + +import nihil.ucl; + +TEST_CASE("ucl: string: construct", "[ucl]") +{ + auto obj = nihil::ucl::string("testing"); + REQUIRE(object_cast(obj).value() == "testing"); +} + +TEST_CASE("ucl: string: parse", "[ucl]") +{ + using namespace std::literals; + + auto input = "value = \"str\""sv; + auto obj = nihil::ucl::parse(input); + auto v = obj.lookup("value"); + REQUIRE(v); + REQUIRE(v->key() == "value"); + REQUIRE(object_cast(*v).value() == "str"); +} + +TEST_CASE("ucl: string: emit", "[ucl]") +{ + auto ucl = nihil::ucl::parse("str = \"te\\\"st\";"); + + auto output = std::string(); + emit(ucl, nihil::ucl::emitter::configuration, + std::back_inserter(output)); + + REQUIRE(output == "str = \"te\\\"st\";\n"); +} -- cgit v1.2.3