mirror of
https://github.com/nlohmann/json.git
synced 2026-10-01 04:00:31 +00:00
Fix CI: json_view value tests without exceptions and with GCC
- ci_test_noexceptions: exception_of() and without_path() exist only with exceptions (they catch outside a CHECK_THROWS, which aborts with JSON_NOEXCEPTION); compile the comparisons of the conversion, value(), and JSON pointer errors only with exceptions as well. - ci_test_gcc: -Werror=unused-result for static_cast<void>(j.contains(p)) (GCC's warn_unused_result ignores a cast to void); store the result. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -701,6 +701,7 @@ TEST_CASE("json_view element access and iteration")
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
// an exception message without the context that basic_json adds with
|
// an exception message without the context that basic_json adds with
|
||||||
// JSON_DIAGNOSTICS ("(/path) ") and JSON_DIAGNOSTIC_POSITIONS ("(bytes 1-2) ");
|
// JSON_DIAGNOSTICS ("(/path) ") and JSON_DIAGNOSTIC_POSITIONS ("(bytes 1-2) ");
|
||||||
// the view's exceptions have no such context
|
// the view's exceptions have no such context
|
||||||
@@ -718,6 +719,7 @@ std::string without_path(std::string msg)
|
|||||||
}
|
}
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// the bits of a float, to compare values bit for bit
|
// the bits of a float, to compare values bit for bit
|
||||||
std::uint64_t bits(double x)
|
std::uint64_t bits(double x)
|
||||||
@@ -797,6 +799,7 @@ void check_values(const ordered_json_view& v, const ordered_json& j, const std::
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
// conversions to the wrong type throw what basic_json throws
|
// conversions to the wrong type throw what basic_json throws
|
||||||
if (!j.is_number())
|
if (!j.is_number())
|
||||||
{
|
{
|
||||||
@@ -813,6 +816,7 @@ void check_values(const ordered_json_view& v, const ordered_json& j, const std::
|
|||||||
{
|
{
|
||||||
CHECK(exception_of([&] { static_cast<void>(v.get<std::map<std::string, int>>()); }) == without_path(exception_of([&] { static_cast<void>(j.get<std::map<std::string, int>>()); })));
|
CHECK(exception_of([&] { static_cast<void>(v.get<std::map<std::string, int>>()); }) == without_path(exception_of([&] { static_cast<void>(j.get<std::map<std::string, int>>()); })));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (v.is_array())
|
if (v.is_array())
|
||||||
{
|
{
|
||||||
@@ -989,10 +993,12 @@ TEST_CASE("json_view values")
|
|||||||
// with a JSON pointer, arrays can be asked as well
|
// with a JSON pointer, arrays can be asked as well
|
||||||
CHECK(v["o"]["x"].value(json::json_pointer("/1"), 0) == j["o"]["x"].value(json::json_pointer("/1"), 0));
|
CHECK(v["o"]["x"].value(json::json_pointer("/1"), 0) == j["o"]["x"].value(json::json_pointer("/1"), 0));
|
||||||
CHECK(v["o"]["x"].value(json::json_pointer("/7"), 3) == j["o"]["x"].value(json::json_pointer("/7"), 3));
|
CHECK(v["o"]["x"].value(json::json_pointer("/7"), 3) == j["o"]["x"].value(json::json_pointer("/7"), 3));
|
||||||
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
CHECK(exception_of([&] { static_cast<void>(v["o"]["x"].value("k", 0)); }) == without_path(exception_of([&] { static_cast<void>(j["o"]["x"].value("k", 0)); })));
|
CHECK(exception_of([&] { static_cast<void>(v["o"]["x"].value("k", 0)); }) == without_path(exception_of([&] { static_cast<void>(j["o"]["x"].value("k", 0)); })));
|
||||||
CHECK(exception_of([&] { static_cast<void>(v.value("s", 0)); }) == without_path(exception_of([&] { static_cast<void>(j.value("s", 0)); })));
|
CHECK(exception_of([&] { static_cast<void>(v.value("s", 0)); }) == without_path(exception_of([&] { static_cast<void>(j.value("s", 0)); })));
|
||||||
CHECK(exception_of([&] { static_cast<void>(v["n"].value("x", 0)); }) == without_path(exception_of([&] { static_cast<void>(j["n"].value("x", 0)); })));
|
CHECK(exception_of([&] { static_cast<void>(v["n"].value("x", 0)); }) == without_path(exception_of([&] { static_cast<void>(j["n"].value("x", 0)); })));
|
||||||
CHECK(exception_of([&] { static_cast<void>(v["n"].value(json::json_pointer("/x"), 0)); }) == without_path(exception_of([&] { static_cast<void>(j["n"].value(json::json_pointer("/x"), 0)); })));
|
CHECK(exception_of([&] { static_cast<void>(v["n"].value(json::json_pointer("/x"), 0)); }) == without_path(exception_of([&] { static_cast<void>(j["n"].value(json::json_pointer("/x"), 0)); })));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1031,6 +1037,7 @@ TEST_CASE("json_view JSON pointers")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
SECTION("errors are those of basic_json")
|
SECTION("errors are those of basic_json")
|
||||||
{
|
{
|
||||||
const std::string text = R"({"a": [1, {"b": null}], "c": "s", "": {"": 0}, "a~b": 1, "c/d": 2})";
|
const std::string text = R"({"a": [1, {"b": null}], "c": "s", "": {"": 0}, "a~b": 1, "c/d": 2})";
|
||||||
@@ -1063,9 +1070,14 @@ TEST_CASE("json_view JSON pointers")
|
|||||||
// (basic_json::contains() throws out_of_range.404 for an empty
|
// (basic_json::contains() throws out_of_range.404 for an empty
|
||||||
// array index token, although it is not meant to throw; the view
|
// array index token, although it is not meant to throw; the view
|
||||||
// answers false)
|
// answers false)
|
||||||
const std::string contains_error = exception_of([&] { static_cast<void>(j.contains(p)); });
|
const std::string contains_error = exception_of([&]
|
||||||
|
{
|
||||||
|
const bool found = j.contains(p);
|
||||||
|
static_cast<void>(found);
|
||||||
|
});
|
||||||
CHECK(v.contains(p) == (contains_error.empty() && j.contains(p)));
|
CHECK(v.contains(p) == (contains_error.empty() && j.contains(p)));
|
||||||
CHECK(exception_of([&] { static_cast<void>(v.value(p, 5)); }) == without_path(exception_of([&] { static_cast<void>(j.value(p, 5)); })));
|
CHECK(exception_of([&] { static_cast<void>(v.value(p, 5)); }) == without_path(exception_of([&] { static_cast<void>(j.value(p, 5)); })));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user