Alexander Rojas created MESOS-2510:
--------------------------------------
Summary: Add a function which test if a JSON object is contained
in another JSON object
Key: MESOS-2510
URL: https://issues.apache.org/jira/browse/MESOS-2510
Project: Mesos
Issue Type: Wish
Components: stout
Reporter: Alexander Rojas
Assignee: Alexander Rojas
It would be nice to check wether one json blob is contained by other blob. i.e.
given the json blob {{a}} and the blob {{b}}, {{a}} contains {{b}} if every key
{{x}} in {{b}} is also in {{a}}, and {{b\[x\] == a\[x\]}} if {{b\[x\]}} is not
a json object itself or, if it is a json object, {{a\[x\]}} contains {{b\[x\]}}.
h3. Rationale
One of the most useful patterns while testing functions which return json, is
to write the expected result and then compare if the expected blob is equal to
the returned one:
{code}
JSON::Value expected = JSON::parse(
"{"
" \"key\" : true"
"}").get();
JSON::Value actual = foo();
CHECK_EQ(expected, actual);
{code}
As can be seen in the example above, it is easy to read what the expected value
is, and checking for failures if fairly easy.
It is no easy, however, to compare returned blobs which contain at least one
random values (for example time stamps), or a value which is uninteresting for
the test. In such cases it is necessary to extract each value separately and
compare them:
{code}
// Returned json:
// {
// "uptime" : 45234.123,
// "key" : true
// }
JSON::Value actual = bar();
// I'm only interested on the "key" entry.
EXPECT_SOME_EQ(true, actual.find<JSON::String>("key"));
{code}
As seen above, is one is only interested in a subset of the keys/values pairs
returned by {{bar}} the readability of the code decreases severely. It is worse
if it weren't for the comments.
The aim is to achieve the same level of readability on the first example while
covering the case of the second:
{code}
JSON::Value expected = JSON::parse(
"{"
" \"key\" : true"
"}").get();
// Returned json:
// {
// "uptime" : 45234.123,
// "key" : true
// }
JSON::Value actual = bar();
// I'm only interested on the "key" entry and ignore the rest.
EXPECT_TRUE(contains(actual, expected));
{code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)