Merge branch 'jszlenk/create_new_subproject' refs #21937
[arvados.git] / sdk / cwl / tests / matcher.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import difflib
6 import json
7 import re
8
9 class JsonDiffMatcher(object):
10     """Raise AssertionError with a readable JSON diff when not __eq__().
11
12     Used with assert_called_with() so it's possible for a human to see
13     the differences between expected and actual call arguments that
14     include non-trivial data structures.
15     """
16     def __init__(self, expected):
17         self.expected = expected
18
19     def __eq__(self, actual):
20         expected_json = json.dumps(self.expected, sort_keys=True, indent=2)
21         actual_json = json.dumps(actual, sort_keys=True, indent=2)
22         if expected_json != actual_json:
23             raise AssertionError("".join(difflib.context_diff(
24                 expected_json.splitlines(1),
25                 actual_json.splitlines(1),
26                 fromfile="Expected", tofile="Actual")))
27         return True
28
29
30 def StripYAMLComments(yml):
31     return re.sub(r'(?ms)^(#.*?\n)*\n*', '', yml)