Skip to content

Commit

Permalink
Refactor test for nlohmann#3007 so it doesn't use private members. Al…
Browse files Browse the repository at this point in the history
…so extend to test both update() functions.
  • Loading branch information
AnthonyVH committed Sep 10, 2021
1 parent 6f2dce5 commit 40b07f2
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions test/src/unit-diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ SOFTWARE.
#endif

#define JSON_DIAGNOSTICS 1
#define JSON_TESTS_PRIVATE

#include <nlohmann/json.hpp>
using nlohmann::json;
Expand Down Expand Up @@ -188,18 +187,36 @@ TEST_CASE("Better diagnostics")

SECTION("Regression test for issue #3007 - Parent pointers properly set when using update()")
{
json j = json::object();

{
json j2 = json::object();
j2["one"] = 1;
// void update(const_reference j)
json j = json::object();

{
json j2 = json::object();
j2["one"] = 1;

j.update(j2);
}

j.update(j2);
// Must call operator[] on const element, otherwise m_parent gets updated.
auto const& constJ = j;
CHECK_THROWS_WITH_AS(constJ["one"].at(0), "[json.exception.type_error.304] (/one) cannot use at() with number", json::type_error);
}

for (auto const& kv : j)
{
CHECK(kv.m_parent == &j);
// void update(const_iterator first, const_iterator last)
json j = json::object();

{
json j2 = json::object();
j2["one"] = 1;

j.update(j2.begin(), j2.end());
}

// Must call operator[] on const element, otherwise m_parent gets updated.
auto const& constJ = j;
CHECK_THROWS_WITH_AS(constJ["one"].at(0), "[json.exception.type_error.304] (/one) cannot use at() with number", json::type_error);
}
}
}

0 comments on commit 40b07f2

Please sign in to comment.