From f4ace7d773e19c35d84555abb9ec6cbe05140f74 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Sep 2026 22:44:33 +0200 Subject: [PATCH] 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(j.contains(p)) (GCC's warn_unused_result ignores a cast to void); store the result. Signed-off-by: Niels Lohmann --- tests/src/unit-json_view.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/src/unit-json_view.cpp b/tests/src/unit-json_view.cpp index f3ccb1ac7..4d3d0f1ac 100644 --- a/tests/src/unit-json_view.cpp +++ b/tests/src/unit-json_view.cpp @@ -701,6 +701,7 @@ TEST_CASE("json_view element access and iteration") namespace { +#if !defined(JSON_NOEXCEPTION) // an exception message without the context that basic_json adds with // JSON_DIAGNOSTICS ("(/path) ") and JSON_DIAGNOSTIC_POSITIONS ("(bytes 1-2) "); // the view's exceptions have no such context @@ -718,6 +719,7 @@ std::string without_path(std::string msg) } return msg; } +#endif // the bits of a float, to compare values bit for bit 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; } +#if !defined(JSON_NOEXCEPTION) // conversions to the wrong type throw what basic_json throws 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(v.get>()); }) == without_path(exception_of([&] { static_cast(j.get>()); }))); } +#endif if (v.is_array()) { @@ -989,10 +993,12 @@ TEST_CASE("json_view values") // 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("/7"), 3) == j["o"]["x"].value(json::json_pointer("/7"), 3)); +#if !defined(JSON_NOEXCEPTION) CHECK(exception_of([&] { static_cast(v["o"]["x"].value("k", 0)); }) == without_path(exception_of([&] { static_cast(j["o"]["x"].value("k", 0)); }))); CHECK(exception_of([&] { static_cast(v.value("s", 0)); }) == without_path(exception_of([&] { static_cast(j.value("s", 0)); }))); CHECK(exception_of([&] { static_cast(v["n"].value("x", 0)); }) == without_path(exception_of([&] { static_cast(j["n"].value("x", 0)); }))); CHECK(exception_of([&] { static_cast(v["n"].value(json::json_pointer("/x"), 0)); }) == without_path(exception_of([&] { static_cast(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") { 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 // array index token, although it is not meant to throw; the view // answers false) - const std::string contains_error = exception_of([&] { static_cast(j.contains(p)); }); + const std::string contains_error = exception_of([&] + { + const bool found = j.contains(p); + static_cast(found); + }); CHECK(v.contains(p) == (contains_error.empty() && j.contains(p))); CHECK(exception_of([&] { static_cast(v.value(p, 5)); }) == without_path(exception_of([&] { static_cast(j.value(p, 5)); }))); } } +#endif }