Skip to content

Commit

Permalink
Refactor unit tests to use more convenient doctest assertion macros (#…
Browse files Browse the repository at this point in the history
…3393)

* Add missing check

* Refactor assertions in unit-algorithms.cpp

* Refactor assertions in unit-bson.cpp

* Refactor assertions in unit-cbor.cpp

* Refactor assertions in unit-class_const_iterator.cpp

* Refactor assertions in unit-class_iterator.cpp

* Refactor assertions in unit-class_parser.cpp

* Refactor assertions in unit-constructor1.cpp

* Refactor assertions in unit-convenience.cpp

* Refactor assertions in unit-conversions.cpp

* Refactor assertions in unit-deserialization.cpp

* Refactor assertions in unit-element_access1.cpp

* Refactor assertions in unit-element_access2.cpp

* Refactor assertions in unit-iterators1.cpp

* Refactor assertions in unit-iterators2.cpp

* Refactor assertions in unit-json_patch.cpp

* Refactor assertions in unit-json_pointer.cpp

* Refactor assertions in unit-modifiers.cpp

* Refactor assertions in unit-msgpack.cpp

* Refactor assertions in unit-reference_access.cpp

* Refactor assertions in unit-regression1.cpp

* Refactor assertions in unit-serialization.cpp

* Refactor assertions in unit-ubjson.cpp

* Refactor assertions in unit-unicode1.cpp

* Apply formatting
  • Loading branch information
kkarbowiak authored Mar 24, 2022
1 parent ad103e5 commit ce35256
Show file tree
Hide file tree
Showing 23 changed files with 1,245 additions and 2,380 deletions.
4 changes: 1 addition & 3 deletions test/src/unit-algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ TEST_CASE("algorithms")
SECTION("sorting an object")
{
json j({{"one", 1}, {"two", 2}});
CHECK_THROWS_AS(std::sort(j.begin(), j.end()), json::invalid_iterator&);
CHECK_THROWS_WITH(std::sort(j.begin(), j.end()),
"[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
CHECK_THROWS_WITH_AS(std::sort(j.begin(), j.end()), "[json.exception.invalid_iterator.209] cannot use offsets with object iterators", json::invalid_iterator&);
}
}

Expand Down
52 changes: 16 additions & 36 deletions test/src/unit-bson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,46 @@ TEST_CASE("BSON")
SECTION("null")
{
json j = nullptr;
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_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null", json::type_error&);
}

SECTION("boolean")
{
SECTION("true")
{
json j = true;
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_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
}

SECTION("false")
{
json j = false;
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_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
}
}

SECTION("number")
{
json j = 42;
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_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
}

SECTION("float")
{
json j = 4.2;
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_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
}

SECTION("string")
{
json j = "not supported";
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_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string", json::type_error&);
}

SECTION("array")
{
json j = std::vector<int> {1, 2, 3, 4, 5, 6, 7};
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_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array", json::type_error&);
}
}

Expand All @@ -100,11 +93,10 @@ TEST_CASE("BSON")
{
{ std::string("en\0try", 6), true }
};
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)");
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.out_of_range.409] (/en) BSON key cannot contain code point U+0000 (at byte 2)", json::out_of_range&);
#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_AS(json::to_bson(j), "[json.exception.out_of_range.409] BSON key cannot contain code point U+0000 (at byte 2)", json::out_of_range&);
#endif
}

Expand All @@ -119,8 +111,7 @@ TEST_CASE("BSON")
0x00, 0x00, 0x00, 0x80
};
json _;
CHECK_THROWS_AS(_ = json::from_bson(v), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bson(v), "[json.exception.parse_error.112] parse error at byte 10: syntax error while parsing BSON string: string length must be at least 1, is -2147483648");
CHECK_THROWS_WITH_AS(_ = json::from_bson(v), "[json.exception.parse_error.112] parse error at byte 10: syntax error while parsing BSON string: string length must be at least 1, is -2147483648", json::parse_error&);
}

SECTION("objects")
Expand Down Expand Up @@ -764,9 +755,7 @@ TEST_CASE("Incomplete BSON Input")
};

json _;
CHECK_THROWS_AS(_ = json::from_bson(incomplete_bson), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bson(incomplete_bson),
"[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing BSON cstring: unexpected end of input");
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing BSON cstring: unexpected end of input", json::parse_error&);

CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());

Expand All @@ -783,9 +772,7 @@ TEST_CASE("Incomplete BSON Input")
};

json _;
CHECK_THROWS_AS(_ = json::from_bson(incomplete_bson), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bson(incomplete_bson),
"[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing BSON cstring: unexpected end of input");
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing BSON cstring: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());

SaxCountdown scp(0);
Expand All @@ -807,9 +794,7 @@ TEST_CASE("Incomplete BSON Input")
};

json _;
CHECK_THROWS_AS(_ = json::from_bson(incomplete_bson), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bson(incomplete_bson),
"[json.exception.parse_error.110] parse error at byte 28: syntax error while parsing BSON element list: unexpected end of input");
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 28: syntax error while parsing BSON element list: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());

SaxCountdown scp(1);
Expand All @@ -824,9 +809,7 @@ TEST_CASE("Incomplete BSON Input")
};

json _;
CHECK_THROWS_AS(_ = json::from_bson(incomplete_bson), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bson(incomplete_bson),
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing BSON number: unexpected end of input");
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());

SaxCountdown scp(0);
Expand Down Expand Up @@ -872,8 +855,7 @@ TEST_CASE("Negative size of binary value")
0x00 // end marker
};
json _;
CHECK_THROWS_AS(_ = json::from_bson(input), json::parse_error);
CHECK_THROWS_WITH(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1");
CHECK_THROWS_WITH_AS(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1", json::parse_error);
}

TEST_CASE("Unsupported BSON input")
Expand All @@ -887,9 +869,7 @@ TEST_CASE("Unsupported BSON input")
};

json _;
CHECK_THROWS_AS(_ = json::from_bson(bson), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bson(bson),
"[json.exception.parse_error.114] parse error at byte 5: Unsupported BSON record type 0xFF");
CHECK_THROWS_WITH_AS(_ = json::from_bson(bson), "[json.exception.parse_error.114] parse error at byte 5: Unsupported BSON record type 0xFF", json::parse_error&);
CHECK(json::from_bson(bson, true, false).is_discarded());

SaxCountdown scp(0);
Expand Down
Loading

0 comments on commit ce35256

Please sign in to comment.