Codifying the behavior of json.contains with nested properties, in the unit test. (#4763)

This commit is contained in:
Ahson Khan 2023-07-10 11:48:09 -07:00 committed by GitHub
parent 2f3e5de931
commit cf1c01e089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,3 +16,31 @@ TEST(Json, create)
EXPECT_EQ(expected, j.dump());
}
TEST(Json, duplicateName)
{
json jsonRoot = json::parse(R"({"KeyName": 1, "AnotherObject": {"KeyName": 2}})");
int value = 0;
if (jsonRoot.contains("KeyName"))
{
value = jsonRoot["KeyName"].get<int>();
}
EXPECT_EQ(value, 1);
jsonRoot = json::parse(R"({"AnotherObject": {"KeyName": 2}})");
value = 0;
// The nested KeyName property is considered not found, when at the root.
if (jsonRoot.contains("KeyName"))
{
value = jsonRoot["KeyName"].get<int>();
}
EXPECT_EQ(value, 0);
// The nested KeyName property is considered found, when navigating to the nested object first.
if (jsonRoot["AnotherObject"].contains("KeyName"))
{
value = jsonRoot["AnotherObject"]["KeyName"].get<int>();
}
EXPECT_EQ(value, 2);
}