From 9eb19bcc279623c56547ee1e51b39b13d013c4fc Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 18 May 2020 12:33:26 +0200 Subject: [PATCH] :white_check_mark: add more tests for binary type --- test/src/unit-conversions.cpp | 119 +++++++++++++++++++++++++++++++++- test/src/unit-modifiers.cpp | 7 +- test/src/unit-udt.cpp | 7 ++ 3 files changed, 129 insertions(+), 4 deletions(-) diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 8477cf639..d37e8e9b2 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -189,7 +189,6 @@ TEST_CASE("value conversion") } } - SECTION("get an object (implicit)") { json::object_t o_reference = {{"object", json::object()}, @@ -1259,6 +1258,124 @@ TEST_CASE("value conversion") } } + SECTION("get a binary value (explicit)") + { + json::binary_t n_reference{{1, 2, 3}}; + json j(n_reference); + + SECTION("binary_t") + { + json::binary_t b = j.get(); + CHECK(*json(b).m_value.binary == *j.m_value.binary); + } + + SECTION("get_binary()") + { + SECTION("non-const") + { + auto& b = j.get_binary(); + CHECK(*json(b).m_value.binary == *j.m_value.binary); + } + + SECTION("non-const") + { + const json j_const = j; + const auto& b = j_const.get_binary(); + CHECK(*json(b).m_value.binary == *j.m_value.binary); + } + } + + SECTION("exception in case of a non-string type") + { + json j_null(json::value_t::null); + json j_object(json::value_t::object); + json j_array(json::value_t::array); + json j_string(json::value_t::string); + json j_boolean(json::value_t::boolean); + const json j_null_const(json::value_t::null); + const json j_object_const(json::value_t::object); + const json j_array_const(json::value_t::array); + const json j_string_const(json::value_t::string); + const json j_boolean_const(json::value_t::boolean); + + CHECK_THROWS_WITH_AS(j_null.get(), + "[json.exception.type_error.302] type must be binary, but is null", + json::type_error&); + CHECK_THROWS_WITH_AS(j_object.get(), + "[json.exception.type_error.302] type must be binary, but is object", + json::type_error&); + CHECK_THROWS_WITH_AS(j_array.get(), + "[json.exception.type_error.302] type must be binary, but is array", + json::type_error&); + CHECK_THROWS_WITH_AS(j_string.get(), + "[json.exception.type_error.302] type must be binary, but is string", + json::type_error&); + CHECK_THROWS_WITH_AS(j_boolean.get(), + "[json.exception.type_error.302] type must be binary, but is boolean", + json::type_error&); + + CHECK_THROWS_WITH_AS(j_null_const.get(), + "[json.exception.type_error.302] type must be binary, but is null", + json::type_error&); + CHECK_THROWS_WITH_AS(j_object_const.get(), + "[json.exception.type_error.302] type must be binary, but is object", + json::type_error&); + CHECK_THROWS_WITH_AS(j_array_const.get(), + "[json.exception.type_error.302] type must be binary, but is array", + json::type_error&); + CHECK_THROWS_WITH_AS(j_string_const.get(), + "[json.exception.type_error.302] type must be binary, but is string", + json::type_error&); + CHECK_THROWS_WITH_AS(j_boolean_const.get(), + "[json.exception.type_error.302] type must be binary, but is boolean", + json::type_error&); + + CHECK_THROWS_WITH_AS(j_null.get_binary(), + "[json.exception.type_error.302] type must be binary, but is null", + json::type_error&); + CHECK_THROWS_WITH_AS(j_object.get_binary(), + "[json.exception.type_error.302] type must be binary, but is object", + json::type_error&); + CHECK_THROWS_WITH_AS(j_array.get_binary(), + "[json.exception.type_error.302] type must be binary, but is array", + json::type_error&); + CHECK_THROWS_WITH_AS(j_string.get_binary(), + "[json.exception.type_error.302] type must be binary, but is string", + json::type_error&); + CHECK_THROWS_WITH_AS(j_boolean.get_binary(), + "[json.exception.type_error.302] type must be binary, but is boolean", + json::type_error&); + + CHECK_THROWS_WITH_AS(j_null_const.get_binary(), + "[json.exception.type_error.302] type must be binary, but is null", + json::type_error&); + CHECK_THROWS_WITH_AS(j_object_const.get_binary(), + "[json.exception.type_error.302] type must be binary, but is object", + json::type_error&); + CHECK_THROWS_WITH_AS(j_array_const.get_binary(), + "[json.exception.type_error.302] type must be binary, but is array", + json::type_error&); + CHECK_THROWS_WITH_AS(j_string_const.get_binary(), + "[json.exception.type_error.302] type must be binary, but is string", + json::type_error&); + CHECK_THROWS_WITH_AS(j_boolean_const.get_binary(), + "[json.exception.type_error.302] type must be binary, but is boolean", + json::type_error&); + } + } + + SECTION("get a binary value (implicit)") + { + json::binary_t n_reference{{1, 2, 3}}; + json j(n_reference); + + SECTION("binary_t") + { + json::binary_t b = j; + CHECK(*json(b).m_value.binary == *j.m_value.binary); + } + } + SECTION("get an enum") { enum c_enum { value_1, value_2 }; diff --git a/test/src/unit-modifiers.cpp b/test/src/unit-modifiers.cpp index fc2901628..955051a6b 100644 --- a/test/src/unit-modifiers.cpp +++ b/test/src/unit-modifiers.cpp @@ -996,10 +996,11 @@ TEST_CASE("modifiers") SECTION("non-binary_t type") { json j = 17; - json::binary_t s = {{1, 2, 3, 4}}; + json::binary_t s1 = {{1, 2, 3, 4}}; + std::vector s2 = {{5, 6, 7, 8}}; - CHECK_THROWS_AS(j.swap(s), json::type_error&); - CHECK_THROWS_WITH(j.swap(s), "[json.exception.type_error.310] cannot use swap() with number"); + CHECK_THROWS_WITH_AS(j.swap(s1), "[json.exception.type_error.310] cannot use swap() with number", json::type_error); + CHECK_THROWS_WITH_AS(j.swap(s2), "[json.exception.type_error.310] cannot use swap() with number", json::type_error); } } } diff --git a/test/src/unit-udt.cpp b/test/src/unit-udt.cpp index cd9ce69fb..42b1375ef 100644 --- a/test/src/unit-udt.cpp +++ b/test/src/unit-udt.cpp @@ -763,6 +763,13 @@ TEST_CASE("different basic_json types conversions") CHECK(cj == "forty-two"); } + SECTION("binary") + { + json j = json::binary_array({1, 2, 3}); + custom_json cj = j; + CHECK(cj.get_binary() == j.get_binary()); + } + SECTION("object") { json j = {{"forty", "two"}};