aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/tests
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-22 23:25:26 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-22 23:25:26 +0100
commit8cbb82a1f6eb6605a4615d30922b777e7bf1e4d8 (patch)
treec79559e81aa9570e22a13ec0439ff7eee05f9ffd /nihil.ucl/tests
parent639b270eed81f7c2627d810057d188e2e8ee67f9 (diff)
downloadnihil-8cbb82a1f6eb6605a4615d30922b777e7bf1e4d8.tar.gz
nihil-8cbb82a1f6eb6605a4615d30922b777e7bf1e4d8.tar.bz2
nihil.ucl: add map<>
Diffstat (limited to 'nihil.ucl/tests')
-rw-r--r--nihil.ucl/tests/CMakeLists.txt1
-rw-r--r--nihil.ucl/tests/array.cc14
-rw-r--r--nihil.ucl/tests/boolean.cc11
-rw-r--r--nihil.ucl/tests/integer.cc10
-rw-r--r--nihil.ucl/tests/map.cc132
-rw-r--r--nihil.ucl/tests/parse.cc44
-rw-r--r--nihil.ucl/tests/real.cc10
-rw-r--r--nihil.ucl/tests/string.cc10
8 files changed, 174 insertions, 58 deletions
diff --git a/nihil.ucl/tests/CMakeLists.txt b/nihil.ucl/tests/CMakeLists.txt
index 2c4ec5d..93559a7 100644
--- a/nihil.ucl/tests/CMakeLists.txt
+++ b/nihil.ucl/tests/CMakeLists.txt
@@ -8,6 +8,7 @@ add_executable(nihil.ucl.test
array.cc
boolean.cc
integer.cc
+ map.cc
real.cc
string.cc
)
diff --git a/nihil.ucl/tests/array.cc b/nihil.ucl/tests/array.cc
index ce86058..023b3bf 100644
--- a/nihil.ucl/tests/array.cc
+++ b/nihil.ucl/tests/array.cc
@@ -97,14 +97,8 @@ TEST_CASE("ucl: array: parse", "[ucl]")
using namespace std::literals;
using namespace nihil::ucl;
- auto input = "value = [1, 42, 666]"sv;
- auto obj = parse(input);
- auto v = obj.lookup("value");
-
- REQUIRE(v);
- REQUIRE(v->key() == "value");
-
- auto arr = object_cast<array<integer>>(*v);
+ auto obj = parse("value = [1, 42, 666]"sv);
+ auto arr = object_cast<array<integer>>(obj["value"]);
REQUIRE(arr.size() == 3);
REQUIRE(arr[0] == 1);
REQUIRE(arr[1] == 42);
@@ -170,9 +164,7 @@ TEST_CASE("ucl: array: heterogeneous elements", "[ucl]")
using namespace nihil::ucl;
auto obj = parse("array [ 42, true, \"test\" ];");
- auto v = obj.lookup("array");
- REQUIRE(v);
- auto arr = object_cast<array<>>(*v);
+ auto arr = object_cast<array<>>(obj["array"]);
REQUIRE(arr.size() == 3);
diff --git a/nihil.ucl/tests/boolean.cc b/nihil.ucl/tests/boolean.cc
index b0b3b58..ed5e1d7 100644
--- a/nihil.ucl/tests/boolean.cc
+++ b/nihil.ucl/tests/boolean.cc
@@ -46,13 +46,10 @@ 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);
+ auto obj = nihil::ucl::parse("value = true"sv);
+ auto v = obj["value"];
+ REQUIRE(v.key() == "value");
+ REQUIRE(object_cast<nihil::ucl::boolean>(v) == true);
}
TEST_CASE("ucl: boolean: emit", "[ucl]")
diff --git a/nihil.ucl/tests/integer.cc b/nihil.ucl/tests/integer.cc
index c7db851..ad513ca 100644
--- a/nihil.ucl/tests/integer.cc
+++ b/nihil.ucl/tests/integer.cc
@@ -46,12 +46,10 @@ 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);
+ auto obj = nihil::ucl::parse("value = 42"sv);
+ auto v = obj["value"];
+ REQUIRE(v.key() == "value");
+ REQUIRE(object_cast<nihil::ucl::integer>(v) == 42);
}
TEST_CASE("ucl: integer: emit", "[ucl]")
diff --git a/nihil.ucl/tests/map.cc b/nihil.ucl/tests/map.cc
new file mode 100644
index 0000000..d106c79
--- /dev/null
+++ b/nihil.ucl/tests/map.cc
@@ -0,0 +1,132 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil.ucl;
+
+TEST_CASE("ucl: map: default construct", "[ucl]")
+{
+ auto map = nihil::ucl::map<>();
+ REQUIRE(str(map.type()) == "object");
+}
+
+TEST_CASE("ucl: map: construct from initializer_list", "[ucl]")
+{
+ using namespace nihil::ucl;
+ using namespace std::literals;
+
+ auto map = nihil::ucl::map<integer>{
+ {"1"sv, integer(1)},
+ {"42"sv, integer(42)},
+ };
+
+ REQUIRE(str(map.type()) == "object");
+ REQUIRE(map["1"] == 1);
+ REQUIRE(map["42"] == 42);
+}
+
+TEST_CASE("ucl: map: insert", "[ucl]")
+{
+ using namespace nihil::ucl;
+ using namespace std::literals;
+
+ auto m = map<integer>();
+
+ m.insert({"test1"sv, integer(42)});
+ m.insert({"test2"sv, integer(666)});
+
+ REQUIRE(m["test1"] == 42);
+ REQUIRE(m["test2"] == 666);
+}
+
+TEST_CASE("ucl: map: find", "[ucl]")
+{
+ using namespace nihil::ucl;
+ using namespace std::literals;
+
+ auto map = nihil::ucl::map<integer>{
+ {"1"sv, integer(1)},
+ {"42"sv, integer(42)},
+ };
+
+ auto obj = map.find("42");
+ REQUIRE(obj != std::nullopt);
+ REQUIRE(*obj == 42);
+
+ obj = map.find("43");
+ REQUIRE(obj == std::nullopt);
+}
+
+TEST_CASE("ucl: map: iterate", "[ucl]")
+{
+ using namespace nihil::ucl;
+ using namespace std::literals;
+
+ auto map = nihil::ucl::map<integer>{
+ {"1"sv, integer(1)},
+ {"42"sv, integer(42)},
+ };
+
+ auto i = 0u;
+
+ for (auto [key, value] : map) {
+ if (key == "1")
+ REQUIRE(value == 1);
+ else if (key == "42")
+ REQUIRE(value == 42);
+ else
+ REQUIRE(false);
+ ++i;
+ }
+
+ REQUIRE(i == 2);
+}
+
+TEST_CASE("ucl: map: operator[] throws key_not_found", "[ucl]")
+{
+ auto map = nihil::ucl::map<nihil::ucl::integer>();
+ REQUIRE_THROWS_AS(map["nonesuch"], nihil::ucl::key_not_found);
+}
+
+TEST_CASE("ucl: map: remove", "[uc]")
+{
+ using namespace std::literals;
+ using namespace nihil::ucl;
+
+ auto map = nihil::ucl::map<integer>{
+ {"1"sv, integer(1)},
+ {"42"sv, integer(42)},
+ };
+
+ REQUIRE(map.find("42") != std::nullopt);
+ REQUIRE(map.remove("42") == true);
+ REQUIRE(map.find("42") == std::nullopt);
+ REQUIRE(map["1"] == 1);
+
+ REQUIRE(map.remove("42") == false);
+}
+
+TEST_CASE("ucl: map: pop", "[uc]")
+{
+ using namespace std::literals;
+ using namespace nihil::ucl;
+
+ auto map = nihil::ucl::map<integer>{
+ {"1"sv, integer(1)},
+ {"42"sv, integer(42)},
+ };
+
+ REQUIRE(map.find("42") != std::nullopt);
+
+ auto obj = map.pop("42");
+ REQUIRE(obj != std::nullopt);
+ REQUIRE(*obj == 42);
+
+ REQUIRE(map.find("42") == std::nullopt);
+ REQUIRE(map["1"] == 1);
+
+ obj = map.pop("42");
+ REQUIRE(obj == std::nullopt);
+}
diff --git a/nihil.ucl/tests/parse.cc b/nihil.ucl/tests/parse.cc
index 3a4f061..3cf5742 100644
--- a/nihil.ucl/tests/parse.cc
+++ b/nihil.ucl/tests/parse.cc
@@ -13,38 +13,38 @@ import nihil.ucl;
TEST_CASE("ucl parse: iterate array", "[ucl]")
{
using namespace std::literals;
+ using namespace nihil::ucl;
- auto input = "value = [1, 42, 666];"sv;
- auto obj = nihil::ucl::parse(input);
+ auto obj = parse("value = [1, 42, 666];"sv);
- auto array = obj.lookup("value");
- REQUIRE(array);
- REQUIRE(array->key() == "value");
+ auto arr = obj["value"];
+ REQUIRE(arr.key() == "value");
+
+ auto vec = std::vector(std::from_range,
+ object_cast<array<integer>>(arr));
- 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);
+ REQUIRE(vec[0] == 1);
+ REQUIRE(vec[1] == 42);
+ REQUIRE(vec[2] == 666);
}
TEST_CASE("ucl parse: iterate hash", "[ucl]")
{
using namespace std::literals;
+ using namespace nihil::ucl;
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");
+ auto obj = parse(input);
+
+ for (auto &&[key, value] : obj) {
+ REQUIRE(key == value.key());
+
+ if (key == "int")
+ REQUIRE(object_cast<integer>(value) == 42);
+ else if (key == "bool")
+ REQUIRE(object_cast<boolean>(value) == true);
+ else if (key == "str")
+ REQUIRE(object_cast<string>(value) == "test");
}
}
diff --git a/nihil.ucl/tests/real.cc b/nihil.ucl/tests/real.cc
index d97e767..4bd9b3f 100644
--- a/nihil.ucl/tests/real.cc
+++ b/nihil.ucl/tests/real.cc
@@ -48,12 +48,10 @@ 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(),
+ auto obj = nihil::ucl::parse("value = 42.1"sv);
+ auto v = obj["value"];
+ REQUIRE(v.key() == "value");
+ REQUIRE_THAT(object_cast<nihil::ucl::real>(v).value(),
Catch::Matchers::WithinRel(42.1));
}
diff --git a/nihil.ucl/tests/string.cc b/nihil.ucl/tests/string.cc
index b702b51..19052cd 100644
--- a/nihil.ucl/tests/string.cc
+++ b/nihil.ucl/tests/string.cc
@@ -108,12 +108,10 @@ 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");
+ auto obj = nihil::ucl::parse("value = \"str\""sv);
+ auto v = obj["value"];
+ REQUIRE(v.key() == "value");
+ REQUIRE(object_cast<nihil::ucl::string>(v) == "str");
}
TEST_CASE("ucl: string: emit", "[ucl]")