add more tests for binary type

This commit is contained in:
Niels Lohmann 2020-05-18 12:33:26 +02:00
parent 5dec7166ea
commit 9eb19bcc27
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 129 additions and 4 deletions

View file

@ -189,7 +189,6 @@ TEST_CASE("value conversion")
} }
} }
SECTION("get an object (implicit)") SECTION("get an object (implicit)")
{ {
json::object_t o_reference = {{"object", json::object()}, 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<json::binary_t>();
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::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is null",
json::type_error&);
CHECK_THROWS_WITH_AS(j_object.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is object",
json::type_error&);
CHECK_THROWS_WITH_AS(j_array.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is array",
json::type_error&);
CHECK_THROWS_WITH_AS(j_string.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is string",
json::type_error&);
CHECK_THROWS_WITH_AS(j_boolean.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is boolean",
json::type_error&);
CHECK_THROWS_WITH_AS(j_null_const.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is null",
json::type_error&);
CHECK_THROWS_WITH_AS(j_object_const.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is object",
json::type_error&);
CHECK_THROWS_WITH_AS(j_array_const.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is array",
json::type_error&);
CHECK_THROWS_WITH_AS(j_string_const.get<json::binary_t>(),
"[json.exception.type_error.302] type must be binary, but is string",
json::type_error&);
CHECK_THROWS_WITH_AS(j_boolean_const.get<json::binary_t>(),
"[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") SECTION("get an enum")
{ {
enum c_enum { value_1, value_2 }; enum c_enum { value_1, value_2 };

View file

@ -996,10 +996,11 @@ TEST_CASE("modifiers")
SECTION("non-binary_t type") SECTION("non-binary_t type")
{ {
json j = 17; json j = 17;
json::binary_t s = {{1, 2, 3, 4}}; json::binary_t s1 = {{1, 2, 3, 4}};
std::vector<std::uint8_t> s2 = {{5, 6, 7, 8}};
CHECK_THROWS_AS(j.swap(s), json::type_error&); CHECK_THROWS_WITH_AS(j.swap(s1), "[json.exception.type_error.310] cannot use swap() with number", 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(s2), "[json.exception.type_error.310] cannot use swap() with number", json::type_error);
} }
} }
} }

View file

@ -763,6 +763,13 @@ TEST_CASE("different basic_json types conversions")
CHECK(cj == "forty-two"); 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") SECTION("object")
{ {
json j = {{"forty", "two"}}; json j = {{"forty", "two"}};