5 class JsonDiffMatcher(object):
6 """Raise AssertionError with a readable JSON diff when not __eq__().
8 Used with assert_called_with() so it's possible for a human to see
9 the differences between expected and actual call arguments that
10 include non-trivial data structures.
12 def __init__(self, expected):
13 self.expected = expected
15 def __eq__(self, actual):
16 expected_json = json.dumps(self.expected, sort_keys=True, indent=2)
17 actual_json = json.dumps(actual, sort_keys=True, indent=2)
18 if expected_json != actual_json:
19 raise AssertionError("".join(difflib.context_diff(
20 expected_json.splitlines(1),
21 actual_json.splitlines(1),
22 fromfile="Expected", tofile="Actual")))