Codifying the behavior of json.contains with nested properties, in the unit test. (#4763)
This commit is contained in:
parent
2f3e5de931
commit
cf1c01e089
@ -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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user