aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/tests
diff options
context:
space:
mode:
Diffstat (limited to 'nihil.ucl/tests')
-rw-r--r--nihil.ucl/tests/array.cc35
-rw-r--r--nihil.ucl/tests/emit.cc10
2 files changed, 32 insertions, 13 deletions
diff --git a/nihil.ucl/tests/array.cc b/nihil.ucl/tests/array.cc
index 687b02c..60cb61d 100644
--- a/nihil.ucl/tests/array.cc
+++ b/nihil.ucl/tests/array.cc
@@ -2,6 +2,8 @@
* This source code is released into the public domain.
*/
+#include <algorithm>
+#include <ranges>
#include <string>
#include <catch2/catch_test_macros.hpp>
@@ -95,11 +97,7 @@ TEST_CASE("ucl: array: parse", "[ucl]")
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));
-
+ auto output = std::format("{:c}", ucl);
REQUIRE(output ==
"array [\n"
" 1,\n"
@@ -107,3 +105,30 @@ TEST_CASE("ucl: array: emit", "[ucl]")
" 666,\n"
"]\n");
}
+
+TEST_CASE("ucl: array is a sized_range", "[ucl]")
+{
+ auto arr = nihil::ucl::array<nihil::ucl::integer>{1, 42, 666};
+ static_assert(std::ranges::sized_range<decltype(arr)>);
+
+ auto size = std::ranges::size(arr);
+ REQUIRE(size == 3);
+
+ auto begin = std::ranges::begin(arr);
+ static_assert(std::random_access_iterator<decltype(begin)>);
+ auto end = std::ranges::end(arr);
+ static_assert(std::sentinel_for<decltype(end), decltype(begin)>);
+
+ REQUIRE(std::distance(begin, end) == 3);
+
+ auto vec = std::vector<nihil::ucl::integer>();
+ std::ranges::copy(arr, std::back_inserter(vec));
+ REQUIRE(std::ranges::equal(arr, vec));
+
+ auto arr_as_ints =
+ arr | std::views::transform(&nihil::ucl::integer::value);
+ auto int_vec = std::vector<nihil::ucl::integer::value_type>();
+ std::ranges::copy(arr_as_ints, std::back_inserter(int_vec));
+ REQUIRE(int_vec == std::vector<std::int64_t>{1, 42, 666});
+
+}
diff --git a/nihil.ucl/tests/emit.cc b/nihil.ucl/tests/emit.cc
index c7f9757..a8487c6 100644
--- a/nihil.ucl/tests/emit.cc
+++ b/nihil.ucl/tests/emit.cc
@@ -18,14 +18,8 @@ TEST_CASE("ucl: emit to std::ostream", "[ucl]")
auto strm = std::ostringstream();
strm << obj;
- REQUIRE(strm.str() ==
-"{\n"
-" \"int\": [\n"
-" 1,\n"
-" 42,\n"
-" 666\n"
-" ]\n"
-"}");
+ // The ostream emitter produces JSON.
+ REQUIRE(strm.str() == std::format("{:j}", obj));
}
TEST_CASE("ucl: emit JSON with std::format", "[ucl]")