aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/tests
diff options
context:
space:
mode:
Diffstat (limited to 'nihil.ucl/tests')
-rw-r--r--nihil.ucl/tests/CMakeLists.txt23
-rw-r--r--nihil.ucl/tests/array.cc109
-rw-r--r--nihil.ucl/tests/boolean.cc50
-rw-r--r--nihil.ucl/tests/emit.cc8
-rw-r--r--nihil.ucl/tests/integer.cc50
-rw-r--r--nihil.ucl/tests/object.cc22
-rw-r--r--nihil.ucl/tests/parse.cc50
-rw-r--r--nihil.ucl/tests/real.cc53
-rw-r--r--nihil.ucl/tests/string.cc38
9 files changed, 403 insertions, 0 deletions
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 <string>
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil.ucl;
+
+TEST_CASE("ucl: array: construct", "[ucl]")
+{
+ auto arr = nihil::ucl::array<nihil::ucl::integer>();
+ REQUIRE(arr.size() == 0);
+}
+
+TEST_CASE("ucl: array: push_back", "[ucl]")
+{
+ auto arr = nihil::ucl::array<nihil::ucl::integer>();
+ 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<nihil::ucl::integer>();
+ arr.push_back(1);
+ arr.push_back(42);
+ arr.push_back(666);
+
+ auto arr2 = nihil::ucl::array<nihil::ucl::integer>();
+ REQUIRE(arr != arr2);
+
+ arr2.push_back(1);
+ arr2.push_back(42);
+ arr2.push_back(666);
+ REQUIRE(arr == arr2);
+
+ auto arr3 = nihil::ucl::array<nihil::ucl::integer>();
+ 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<nihil::ucl::integer>{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<nihil::ucl::array<nihil::ucl::integer>>(*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 <string>
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil.ucl;
+
+TEST_CASE("ucl: boolean: construct", "[ucl]")
+{
+ auto obj = nihil::ucl::boolean(true);
+ REQUIRE(object_cast<nihil::ucl::boolean>(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<nihil::ucl::boolean>(*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 <catch2/catch_test_macros.hpp>
+
+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 <string>
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil.ucl;
+
+TEST_CASE("ucl: integer: construct", "[ucl]")
+{
+ auto obj = nihil::ucl::integer(42);
+ REQUIRE(object_cast<nihil::ucl::integer>(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<nihil::ucl::integer>(*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 <catch2/catch_test_macros.hpp>
+
+#include <ucl.h>
+
+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 <string>
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/matchers/catch_matchers_floating_point.hpp>
+
+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<nihil::ucl::object>();
+ std::ranges::copy(*array, std::back_inserter(vec));
+ REQUIRE(vec.size() == 3);
+ REQUIRE(object_cast<nihil::ucl::integer>(vec[0]).value() == 1);
+ REQUIRE(object_cast<nihil::ucl::integer>(vec[1]).value() == 42);
+ REQUIRE(object_cast<nihil::ucl::integer>(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<nihil::ucl::integer>(value).value()
+ == 42);
+ else if (value.key() == "bool")
+ REQUIRE(object_cast<nihil::ucl::boolean>(value).value()
+ == true);
+ else if (value.key() == "str")
+ REQUIRE(object_cast<nihil::ucl::string>(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 <string>
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/matchers/catch_matchers_floating_point.hpp>
+
+import nihil.ucl;
+
+TEST_CASE("ucl: real: construct", "[ucl]")
+{
+ auto obj = nihil::ucl::real(42.1);
+ REQUIRE_THAT(object_cast<nihil::ucl::real>(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<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 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 <string>
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil.ucl;
+
+TEST_CASE("ucl: string: construct", "[ucl]")
+{
+ auto obj = nihil::ucl::string("testing");
+ REQUIRE(object_cast<nihil::ucl::string>(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<nihil::ucl::string>(*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");
+}