6 class JsonDiffMatcher(object):
7 """Raise AssertionError with a readable JSON diff when not __eq__().
9 Used with assert_called_with() so it's possible for a human to see
10 the differences between expected and actual call arguments that
11 include non-trivial data structures.
13 def __init__(self, expected):
14 self.expected = expected
16 def __eq__(self, actual):
17 expected_json = json.dumps(self.expected, sort_keys=True, indent=2)
18 actual_json = json.dumps(actual, sort_keys=True, indent=2)
19 if expected_json != actual_json:
20 raise AssertionError("".join(difflib.context_diff(
21 expected_json.splitlines(1),
22 actual_json.splitlines(1),
23 fromfile="Expected", tofile="Actual")))
27 def StripYAMLComments(yml):
28 return re.sub(r'(?ms)^(#.*?\n)*\n*', '', yml)