comparison env/lib/python3.7/site-packages/schema_salad/tests/matcher.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import difflib
6 import re
7
8 from schema_salad.utils import json_dumps
9
10
11 class JsonDiffMatcher(object):
12 """Raise AssertionError with a readable JSON diff when not __eq__().
13
14 Used with assert_called_with() so it's possible for a human to see
15 the differences between expected and actual call arguments that
16 include non-trivial data structures.
17 """
18
19 def __init__(self, expected):
20 self.expected = expected
21
22 def __eq__(self, actual):
23 expected_json = json_dumps(self.expected, sort_keys=True, indent=2)
24 actual_json = json_dumps(actual, sort_keys=True, indent=2)
25 if expected_json != actual_json:
26 raise AssertionError(
27 "".join(
28 difflib.context_diff(
29 expected_json.splitlines(1),
30 actual_json.splitlines(1),
31 fromfile="Expected",
32 tofile="Actual",
33 )
34 )
35 )
36 return True
37
38
39 def StripYAMLComments(yml):
40 return re.sub(r"(?ms)^(#.*?\n)*\n*", "", yml)