aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/boolean.test.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-07-01 17:07:04 +0100
committerLexi Winter <lexi@le-fay.org>2025-07-01 17:07:04 +0100
commit2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 (patch)
tree54d37ffadf8e677938d9b7a28e4e9b71be1e75c1 /nihil.ucl/boolean.test.cc
parent36427c0966faa7aecd586b397ed9b845f18172f5 (diff)
downloadnihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.gz
nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.bz2
add nihil.std
Diffstat (limited to 'nihil.ucl/boolean.test.cc')
-rw-r--r--nihil.ucl/boolean.test.cc241
1 files changed, 241 insertions, 0 deletions
diff --git a/nihil.ucl/boolean.test.cc b/nihil.ucl/boolean.test.cc
new file mode 100644
index 0000000..9fb0148
--- /dev/null
+++ b/nihil.ucl/boolean.test.cc
@@ -0,0 +1,241 @@
+// This source code is released into the public domain.
+
+#include <catch2/catch_test_macros.hpp>
+#include <ucl.h>
+
+import nihil.std;
+import nihil.ucl;
+
+namespace {
+inline auto constexpr *test_tags = "[nihil][nihil.ucl][nihil.ucl.boolean]";
+
+TEST_CASE("ucl: boolean: invariants", test_tags)
+{
+ using namespace nihil::ucl;
+
+ static_assert(std::same_as<bool, boolean::contained_type>);
+ REQUIRE(boolean::ucl_type == object_type::boolean);
+ REQUIRE(static_cast<::ucl_type>(boolean::ucl_type) == UCL_BOOLEAN);
+
+ static_assert(std::destructible<boolean>);
+ static_assert(std::default_initializable<boolean>);
+ static_assert(std::move_constructible<boolean>);
+ static_assert(std::copy_constructible<boolean>);
+ static_assert(std::equality_comparable<boolean>);
+ static_assert(std::totally_ordered<boolean>);
+ static_assert(std::swappable<boolean>);
+}
+
+SCENARIO("Constructing a ucl::boolean", test_tags)
+{
+ using namespace nihil::ucl;
+
+ GIVEN ("A default-constructed boolean") {
+ auto b = boolean();
+
+ THEN ("The value is false") {
+ REQUIRE(b == false);
+ }
+ }
+
+ GIVEN ("A boolean constructed from a true value") {
+ auto b = boolean(true);
+
+ THEN ("The value is true") {
+ REQUIRE(b == true);
+ }
+ }
+}
+
+TEST_CASE("ucl: boolean: construct from UCL object", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ SECTION("ref, correct type")
+ {
+ auto *uobj = ::ucl_object_frombool(true);
+
+ auto i = boolean(ref, uobj);
+ REQUIRE(i == true);
+
+ ::ucl_object_unref(uobj);
+ }
+
+ SECTION("noref, correct type")
+ {
+ auto *uobj = ::ucl_object_frombool(true);
+
+ auto i = boolean(noref, uobj);
+ REQUIRE(i == true);
+ }
+
+ SECTION("ref, wrong type")
+ {
+ auto *uobj = ::ucl_object_fromint(1);
+
+ REQUIRE_THROWS_AS(boolean(ref, uobj), type_mismatch);
+
+ ::ucl_object_unref(uobj);
+ }
+
+ SECTION("noref, wrong type")
+ {
+ auto *uobj = ::ucl_object_fromint(1);
+
+ REQUIRE_THROWS_AS(boolean(noref, uobj), type_mismatch);
+
+ ::ucl_object_unref(uobj);
+ }
+}
+
+TEST_CASE("ucl: boolean: make_boolean", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ SECTION("default value")
+ {
+ auto b = make_boolean().value();
+ REQUIRE(b == false);
+ }
+
+ SECTION("explicit value")
+ {
+ auto b = make_boolean(true).value();
+ REQUIRE(b == true);
+ }
+}
+
+TEST_CASE("ucl: boolean: swap", "[ucl]")
+{
+ // do not add using namespace nihil::ucl
+
+ auto b1 = nihil::ucl::boolean(true);
+ auto b2 = nihil::ucl::boolean(false);
+
+ swap(b1, b2);
+
+ REQUIRE(b1 == false);
+ REQUIRE(b2 == true);
+}
+
+TEST_CASE("ucl: boolean: value()", "[ucl]")
+{
+ auto b = nihil::ucl::boolean(true);
+ REQUIRE(b.value() == true);
+}
+
+TEST_CASE("ucl: boolean: key()", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ auto obj = parse("a_bool = true").value();
+ REQUIRE(object_cast<boolean>(obj["a_bool"]).value().key() == "a_bool");
+
+ auto b = boolean(true);
+ REQUIRE(b.key().empty() == true);
+}
+
+TEST_CASE("ucl: boolean: comparison", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ auto b = boolean(true);
+
+ SECTION("operator==")
+ {
+ REQUIRE(b == true);
+ REQUIRE(b == boolean(true));
+ }
+
+ SECTION("operator!=")
+ {
+ REQUIRE(b != false);
+ REQUIRE(b != boolean(false));
+ }
+
+ SECTION("operator<")
+ {
+ REQUIRE(b <= true);
+ REQUIRE(b <= nihil::ucl::boolean(true));
+ }
+
+ SECTION("operator>")
+ {
+ REQUIRE(b > false);
+ REQUIRE(b > nihil::ucl::boolean(false));
+ }
+}
+
+TEST_CASE("ucl: boolean: parse", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ auto obj = parse("value = true").value();
+
+ auto v = obj["value"];
+ REQUIRE(v.key() == "value");
+ REQUIRE(object_cast<boolean>(v).value() == true);
+}
+
+TEST_CASE("ucl: boolean: parse and emit", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ auto ucl = parse("bool = true;").value();
+
+ auto output = std::string();
+ emit(ucl, nihil::ucl::emitter::configuration, std::back_inserter(output));
+
+ REQUIRE(output == "bool = true;\n");
+}
+
+TEST_CASE("ucl: boolean: format", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ SECTION("bare boolean")
+ {
+ auto str = std::format("{}", boolean(true));
+ REQUIRE(str == "true");
+ }
+
+ SECTION("parsed boolean")
+ {
+ auto obj = parse("bool = true;").value();
+ auto b = object_cast<boolean>(obj["bool"]).value();
+
+ auto str = std::format("{}", b);
+ REQUIRE(str == "true");
+ }
+
+ SECTION("with format string")
+ {
+ auto str = std::format("{: >5}", boolean(true));
+ REQUIRE(str == " true");
+ }
+}
+
+TEST_CASE("ucl: boolean: print to ostream", "[ucl]")
+{
+ using namespace nihil::ucl;
+
+ SECTION("bare boolean")
+ {
+ auto strm = std::ostringstream();
+ strm << boolean(true);
+
+ REQUIRE(strm.str() == "true");
+ }
+
+ SECTION("parsed boolean")
+ {
+ auto obj = parse("bool = true;").value();
+ auto i = object_cast<boolean>(obj["bool"]).value();
+
+ auto strm = std::ostringstream();
+ strm << i;
+
+ REQUIRE(strm.str() == "true");
+ }
+}
+} // anonymous namespace