From 4d99fe92c67f97838c2d9570a57b4dccb619aa64 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Sep 2026 22:41:59 +0200 Subject: [PATCH] Fix CI: json_view access tests without exceptions and on clang 3.6 - ci_test_noexceptions: the element access tests compare the exceptions of json_view and basic_json through exception_of(), which catches them outside a CHECK_THROWS; with JSON_NOEXCEPTION the first one aborted the test. Compile those comparisons only with exceptions. - clang 3.6: value-initialize a const json_view, as in the tests of json-view/10-view-document. - Format three new documentation examples with the pinned astyle, which the "check" job runs once it gets past the amalgamation step. Signed-off-by: Niels Lohmann --- docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp | 2 +- docs/mkdocs/docs/examples/basic_json_view__cend.cpp | 2 +- docs/mkdocs/docs/examples/basic_json_view__type_name.cpp | 5 ++++- tests/src/unit-json_view.cpp | 6 +++++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp b/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp index 811db6267..4e67a8041 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__cbegin.cpp @@ -15,7 +15,7 @@ int main() const auto values = measurements.root(); const int sum = std::accumulate(values.cbegin(), values.cend(), 0, - [](int total, const json_view & v) + [](int total, const json_view & v) { return total + v.materialize().get(); }); diff --git a/docs/mkdocs/docs/examples/basic_json_view__cend.cpp b/docs/mkdocs/docs/examples/basic_json_view__cend.cpp index 2999bd684..c72990673 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__cend.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__cend.cpp @@ -15,7 +15,7 @@ int main() const auto records = batch.root(); const bool all_objects = std::all_of(records.cbegin(), records.cend(), - [](const json_view & v) + [](const json_view & v) { return v.is_object(); }); diff --git a/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp b/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp index 9d59792ca..30661c0fd 100644 --- a/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp +++ b/docs/mkdocs/docs/examples/basic_json_view__type_name.cpp @@ -13,7 +13,10 @@ int main() json_document bad = json_document::parse("[1, 2, 3]"); json_document failed = json_document::parse("not json", /* allow_exceptions */ false); - for (const json_view v : { good.root(), bad.root(), failed.root() }) + for (const json_view v : + { + good.root(), bad.root(), failed.root() + }) { if (v.is_object()) { diff --git a/tests/src/unit-json_view.cpp b/tests/src/unit-json_view.cpp index a8fea7d96..30cfb372d 100644 --- a/tests/src/unit-json_view.cpp +++ b/tests/src/unit-json_view.cpp @@ -418,6 +418,7 @@ TEST_CASE("json_view") namespace { +#if !defined(JSON_NOEXCEPTION) // the exception a call throws, or "" if it throws none template std::string exception_of(F f) @@ -432,6 +433,7 @@ std::string exception_of(F f) } return ""; } +#endif // compares a view with the ordered_json value materialize() gives for it: // types, sizes, elements and members (by index, key, and iteration), in @@ -600,6 +602,7 @@ TEST_CASE("json_view element access and iteration") const json_document d = json_document::parse(text); const json_view v = d.root(); const json j = v.materialize(); +#if !defined(JSON_NOEXCEPTION) if (!j.is_object()) { CHECK(exception_of([&] { static_cast(v["a"]); }) == exception_of([&] { static_cast(j["a"]); })); @@ -621,6 +624,7 @@ TEST_CASE("json_view element access and iteration") CHECK(exception_of([&] { static_cast(v.front()); }) == exception_of([&] { static_cast(j.front()); })); CHECK(exception_of([&] { static_cast(v.back()); }) == exception_of([&] { static_cast(j.back()); })); } +#endif CHECK(v.contains("a") == j.contains("a")); CHECK(v.count("a") == j.count("a")); CHECK((v.find("a") == v.end()) == (j.find("a") == j.end())); // NOLINT(readability-container-contains): find() is what is tested @@ -632,7 +636,7 @@ TEST_CASE("json_view element access and iteration") CHECK(!d.root()["a"][0]); CHECK_THROWS_WITH_AS(d.root()["a"].front(), "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&); CHECK_THROWS_WITH_AS(d.root()["a"].back(), "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&); - const json_view invalid; + const json_view invalid{}; CHECK(invalid.begin() == invalid.end()); CHECK(std::string(invalid.type_name()) == "discarded"); CHECK_THROWS_WITH_AS(invalid["a"], "[json.exception.type_error.305] cannot use operator[] with a string argument with discarded", json::type_error&);