From 3379ed5e33a8e0b12c1e8a7cfa4dcc4578c594b1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 24 Sep 2026 09:35:29 +0200 Subject: [PATCH] Build the update()/merge_patch() diagnostics test values instead of parsing them Parsed values carry byte positions under JSON_DIAGNOSTIC_POSITIONS, which the expected messages do not include. Signed-off-by: Niels Lohmann --- tests/src/unit-diagnostics.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/src/unit-diagnostics.cpp b/tests/src/unit-diagnostics.cpp index da4b0170e..71c2958d5 100644 --- a/tests/src/unit-diagnostics.cpp +++ b/tests/src/unit-diagnostics.cpp @@ -311,25 +311,25 @@ TEST_CASE("Better diagnostics past the descent bound of update() and merge_patch // Both merge objects nested more than detail::recursion_depth_limit() // (128) levels deep without recursing; the values they add or replace // there must still know their parents. + // The values are built rather than parsed, so that the expected messages + // carry no byte positions under JSON_DIAGNOSTIC_POSITIONS. const std::size_t depth = 200; - std::string target_text; - std::string patch_text; + json target = {{"x", 1}}; + json patch = {{"y", 2}}; std::string path; for (std::size_t i = 0; i < depth; ++i) { - target_text += "{\"a\":"; - patch_text += "{\"a\":"; + target = json{{"a", std::move(target)}}; + patch = json{{"a", std::move(patch)}}; path += "/a"; } - target_text += "{\"x\":1}" + std::string(depth, '}'); - patch_text += "{\"y\":2}" + std::string(depth, '}'); const std::string expected_x = "[json.exception.type_error.304] (" + path + "/x) cannot use at() with number"; const std::string expected_y = "[json.exception.type_error.304] (" + path + "/y) cannot use at() with number"; SECTION("update()") { - json j = json::parse(target_text); - j.update(json::parse(patch_text), true); + json j = target; + j.update(patch, true); // walk down through const references, which leave m_parent alone const json* p = &j; @@ -343,8 +343,8 @@ TEST_CASE("Better diagnostics past the descent bound of update() and merge_patch SECTION("merge_patch()") { - json j = json::parse(target_text); - j.merge_patch(json::parse(patch_text)); + json j = target; + j.merge_patch(patch); const json* p = &j; for (std::size_t i = 0; i < depth; ++i)