🎨 clean up binary formats

parent 4d1eaace
...@@ -48,7 +48,7 @@ TEST_CASE("BSON") ...@@ -48,7 +48,7 @@ TEST_CASE("BSON")
SECTION("null") SECTION("null")
{ {
json j = nullptr; json j = nullptr;
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&); CHECK_THROWS_AS(json::to_bson(j), json::type_error&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null");
} }
...@@ -57,14 +57,14 @@ TEST_CASE("BSON") ...@@ -57,14 +57,14 @@ TEST_CASE("BSON")
SECTION("true") SECTION("true")
{ {
json j = true; json j = true;
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&); CHECK_THROWS_AS(json::to_bson(j), json::type_error&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean");
} }
SECTION("false") SECTION("false")
{ {
json j = false; json j = false;
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&); CHECK_THROWS_AS(json::to_bson(j), json::type_error&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean");
} }
} }
...@@ -72,28 +72,28 @@ TEST_CASE("BSON") ...@@ -72,28 +72,28 @@ TEST_CASE("BSON")
SECTION("number") SECTION("number")
{ {
json j = 42; json j = 42;
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&); CHECK_THROWS_AS(json::to_bson(j), json::type_error&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number");
} }
SECTION("float") SECTION("float")
{ {
json j = 4.2; json j = 4.2;
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&); CHECK_THROWS_AS(json::to_bson(j), json::type_error&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number");
} }
SECTION("string") SECTION("string")
{ {
json j = "not supported"; json j = "not supported";
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&); CHECK_THROWS_AS(json::to_bson(j), json::type_error&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string");
} }
SECTION("array") SECTION("array")
{ {
json j = std::vector<int> {1, 2, 3, 4, 5, 6, 7}; json j = std::vector<int> {1, 2, 3, 4, 5, 6, 7};
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&); CHECK_THROWS_AS(json::to_bson(j), json::type_error&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array"); CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array");
} }
} }
...@@ -104,7 +104,7 @@ TEST_CASE("BSON") ...@@ -104,7 +104,7 @@ TEST_CASE("BSON")
{ {
{ std::string("en\0try", 6), true } { std::string("en\0try", 6), true }
}; };
REQUIRE_THROWS_AS(json::to_bson(j), json::out_of_range&); CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&);
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)");
} }
...@@ -541,6 +541,30 @@ TEST_CASE("BSON") ...@@ -541,6 +541,30 @@ TEST_CASE("BSON")
CHECK(json::from_bson(result, true, false) == j); CHECK(json::from_bson(result, true, false) == j);
} }
} }
SECTION("Examples from http://bsonspec.org/faq.html")
{
SECTION("Example 1")
{
std::vector<std::uint8_t> input = {0x16, 0x00, 0x00, 0x00, 0x02, 'h', 'e', 'l', 'l', 'o', 0x00, 0x06, 0x00, 0x00, 0x00, 'w', 'o', 'r', 'l', 'd', 0x00, 0x00};
json parsed = json::from_bson(input);
json expected = {{"hello", "world"}};
CHECK(parsed == expected);
auto dumped = json::to_bson(parsed);
CHECK(dumped == input);
}
SECTION("Example 2")
{
std::vector<std::uint8_t> input = {0x31, 0x00, 0x00, 0x00, 0x04, 'B', 'S', 'O', 'N', 0x00, 0x26, 0x00, 0x00, 0x00, 0x02, 0x30, 0x00, 0x08, 0x00, 0x00, 0x00, 'a', 'w', 'e', 's', 'o', 'm', 'e', 0x00, 0x01, 0x31, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x14, 0x40, 0x10, 0x32, 0x00, 0xc2, 0x07, 0x00, 0x00, 0x00, 0x00};
json parsed = json::from_bson(input);
json expected = {{"BSON", {"awesome", 5.05, 1986}}};
CHECK(parsed == expected);
auto dumped = json::to_bson(parsed);
//CHECK(dumped == input); // see https://github.com/nlohmann/json/pull/1254#issuecomment-432831216
CHECK(json::from_bson(dumped) == expected);
}
}
} }
TEST_CASE("BSON input/output_adapters") TEST_CASE("BSON input/output_adapters")
...@@ -601,10 +625,6 @@ TEST_CASE("BSON input/output_adapters") ...@@ -601,10 +625,6 @@ TEST_CASE("BSON input/output_adapters")
} }
} }
class SaxCountdown class SaxCountdown
{ {
public: public:
...@@ -675,9 +695,10 @@ class SaxCountdown ...@@ -675,9 +695,10 @@ class SaxCountdown
int events_left = 0; int events_left = 0;
}; };
TEST_CASE("Incomplete BSON Input")
TEST_CASE("Incomplete BSON INPUT")
{ {
SECTION("Incomplete BSON Input 1")
{
std::vector<uint8_t> incomplete_bson = std::vector<uint8_t> incomplete_bson =
{ {
0x0D, 0x00, 0x00, 0x00, // size (little endian) 0x0D, 0x00, 0x00, 0x00, // size (little endian)
...@@ -693,10 +714,10 @@ TEST_CASE("Incomplete BSON INPUT") ...@@ -693,10 +714,10 @@ TEST_CASE("Incomplete BSON INPUT")
SaxCountdown scp(0); SaxCountdown scp(0);
CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson)); CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
} }
TEST_CASE("Incomplete BSON INPUT 2") SECTION("Incomplete BSON Input 2")
{ {
std::vector<uint8_t> incomplete_bson = std::vector<uint8_t> incomplete_bson =
{ {
0x0D, 0x00, 0x00, 0x00, // size (little endian) 0x0D, 0x00, 0x00, 0x00, // size (little endian)
...@@ -710,11 +731,10 @@ TEST_CASE("Incomplete BSON INPUT 2") ...@@ -710,11 +731,10 @@ TEST_CASE("Incomplete BSON INPUT 2")
SaxCountdown scp(0); SaxCountdown scp(0);
CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson)); CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
} }
TEST_CASE("Incomplete BSON INPUT 3") SECTION("Incomplete BSON Input 3")
{ {
std::vector<uint8_t> incomplete_bson = std::vector<uint8_t> incomplete_bson =
{ {
0x41, 0x00, 0x00, 0x00, // size (little endian) 0x41, 0x00, 0x00, 0x00, // size (little endian)
...@@ -734,12 +754,10 @@ TEST_CASE("Incomplete BSON INPUT 3") ...@@ -734,12 +754,10 @@ TEST_CASE("Incomplete BSON INPUT 3")
SaxCountdown scp(1); SaxCountdown scp(1);
CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson)); CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
} }
TEST_CASE("Incomplete BSON INPUT 4") SECTION("Incomplete BSON Input 4")
{ {
std::vector<uint8_t> incomplete_bson = std::vector<uint8_t> incomplete_bson =
{ {
0x0D, 0x00, // size (incomplete), unexpected EOF 0x0D, 0x00, // size (incomplete), unexpected EOF
...@@ -752,9 +770,9 @@ TEST_CASE("Incomplete BSON INPUT 4") ...@@ -752,9 +770,9 @@ TEST_CASE("Incomplete BSON INPUT 4")
SaxCountdown scp(0); SaxCountdown scp(0);
CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson)); CHECK(not json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
} }
TEST_CASE("Unsupported BSON input") TEST_CASE("Unsupported BSON input")
{ {
std::vector<uint8_t> bson = std::vector<uint8_t> bson =
...@@ -774,8 +792,6 @@ TEST_CASE("Unsupported BSON input") ...@@ -774,8 +792,6 @@ TEST_CASE("Unsupported BSON input")
CHECK(not json::sax_parse(bson, &scp, json::input_format_t::bson)); CHECK(not json::sax_parse(bson, &scp, json::input_format_t::bson));
} }
TEST_CASE("BSON numerical data") TEST_CASE("BSON numerical data")
{ {
SECTION("number") SECTION("number")
......
...@@ -139,10 +139,10 @@ bool operator==(Data const& lhs, Data const& rhs) ...@@ -139,10 +139,10 @@ bool operator==(Data const& lhs, Data const& rhs)
return lhs.a == rhs.a && lhs.b == rhs.b; return lhs.a == rhs.a && lhs.b == rhs.b;
} }
bool operator!=(Data const& lhs, Data const& rhs) //bool operator!=(Data const& lhs, Data const& rhs)
{ //{
return !(lhs == rhs); // return !(lhs == rhs);
} //}
} }
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
......
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