🚧 add tests

parent 09cd4ed1
...@@ -57,7 +57,7 @@ class binary_writer ...@@ -57,7 +57,7 @@ class binary_writer
default: default:
{ {
JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); JSON_THROW(type_error::create(317, j.diagnostics() + "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name())));
} }
} }
} }
...@@ -901,12 +901,12 @@ class binary_writer ...@@ -901,12 +901,12 @@ class binary_writer
@return The size of a BSON document entry header, including the id marker @return The size of a BSON document entry header, including the id marker
and the entry name size (and its null-terminator). and the entry name size (and its null-terminator).
*/ */
static std::size_t calc_bson_entry_header_size(const string_t& name) static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
{ {
const auto it = name.find(static_cast<typename string_t::value_type>(0)); const auto it = name.find(static_cast<typename string_t::value_type>(0));
if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
{ {
JSON_THROW(out_of_range::create(409, JSON_THROW(out_of_range::create(409, j.diagnostics() +
"BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")"));
} }
...@@ -1017,21 +1017,21 @@ class binary_writer ...@@ -1017,21 +1017,21 @@ class binary_writer
@brief Writes a BSON element with key @a name and unsigned @a value @brief Writes a BSON element with key @a name and unsigned @a value
*/ */
void write_bson_unsigned(const string_t& name, void write_bson_unsigned(const string_t& name,
const std::uint64_t value) const BasicJsonType& j)
{ {
if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)())) if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
{ {
write_bson_entry_header(name, 0x10 /* int32 */); write_bson_entry_header(name, 0x10 /* int32 */);
write_number<std::int32_t, true>(static_cast<std::int32_t>(value)); write_number<std::int32_t, true>(static_cast<std::int32_t>(j.m_value.number_unsigned));
} }
else if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)())) else if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
{ {
write_bson_entry_header(name, 0x12 /* int64 */); write_bson_entry_header(name, 0x12 /* int64 */);
write_number<std::int64_t, true>(static_cast<std::int64_t>(value)); write_number<std::int64_t, true>(static_cast<std::int64_t>(j.m_value.number_unsigned));
} }
else else
{ {
JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64")); JSON_THROW(out_of_range::create(407, j.diagnostics() + "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64"));
} }
} }
...@@ -1108,7 +1108,7 @@ class binary_writer ...@@ -1108,7 +1108,7 @@ class binary_writer
static std::size_t calc_bson_element_size(const string_t& name, static std::size_t calc_bson_element_size(const string_t& name,
const BasicJsonType& j) const BasicJsonType& j)
{ {
const auto header_size = calc_bson_entry_header_size(name); const auto header_size = calc_bson_entry_header_size(name, j);
switch (j.type()) switch (j.type())
{ {
case value_t::object: case value_t::object:
...@@ -1177,7 +1177,7 @@ class binary_writer ...@@ -1177,7 +1177,7 @@ class binary_writer
return write_bson_integer(name, j.m_value.number_integer); return write_bson_integer(name, j.m_value.number_integer);
case value_t::number_unsigned: case value_t::number_unsigned:
return write_bson_unsigned(name, j.m_value.number_unsigned); return write_bson_unsigned(name, j);
case value_t::string: case value_t::string:
return write_bson_string(name, *j.m_value.string); return write_bson_string(name, *j.m_value.string);
......
...@@ -101,7 +101,11 @@ TEST_CASE("BSON") ...@@ -101,7 +101,11 @@ TEST_CASE("BSON")
{ std::string("en\0try", 6), true } { std::string("en\0try", 6), true }
}; };
CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&); CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&);
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.out_of_range.409] (/en) BSON key cannot contain code point U+0000 (at byte 2)");
#else
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.out_of_range.409] BSON key cannot contain code point U+0000 (at byte 2)"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.out_of_range.409] BSON key cannot contain code point U+0000 (at byte 2)");
#endif
} }
SECTION("string length must be at least 1") SECTION("string length must be at least 1")
...@@ -1235,7 +1239,11 @@ TEST_CASE("BSON numerical data") ...@@ -1235,7 +1239,11 @@ TEST_CASE("BSON numerical data")
}; };
CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&); CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&);
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] (/entry) integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
#else
CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64"); CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
#endif
} }
} }
......
...@@ -394,7 +394,11 @@ TEST_CASE("regression tests 1") ...@@ -394,7 +394,11 @@ TEST_CASE("regression tests 1")
// improve coverage // improve coverage
o["int"] = 1; o["int"] = 1;
CHECK_THROWS_AS(s2 = o["int"], json::type_error); CHECK_THROWS_AS(s2 = o["int"], json::type_error);
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH(s2 = o["int"], "[json.exception.type_error.302] (/int) type must be string, but is number");
#else
CHECK_THROWS_WITH(s2 = o["int"], "[json.exception.type_error.302] type must be string, but is number"); CHECK_THROWS_WITH(s2 = o["int"], "[json.exception.type_error.302] type must be string, but is number");
#endif
} }
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment