MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / assertProtoEqual

Function assertProtoEqual

tensorflow/python/util/protobuf/compare.py:77–118  ·  view source on GitHub ↗

Fails with a useful error if a and b aren't equal. Comparison of repeated fields matches the semantics of unittest.TestCase.assertEqual(), ie order and extra duplicates fields matter. Args: self: googletest.TestCase a: proto2 PB instance, or text string representing one. b: proto

(self, a, b, check_initialized=True,  # pylint: disable=invalid-name
                     normalize_numbers=False, msg=None)

Source from the content-addressed store, hash-verified

75
76
77def assertProtoEqual(self, a, b, check_initialized=True, # pylint: disable=invalid-name
78 normalize_numbers=False, msg=None):
79 """Fails with a useful error if a and b aren't equal.
80
81 Comparison of repeated fields matches the semantics of
82 unittest.TestCase.assertEqual(), ie order and extra duplicates fields matter.
83
84 Args:
85 self: googletest.TestCase
86 a: proto2 PB instance, or text string representing one.
87 b: proto2 PB instance -- message.Message or subclass thereof.
88 check_initialized: boolean, whether to fail if either a or b isn't
89 initialized.
90 normalize_numbers: boolean, whether to normalize types and precision of
91 numbers before comparison.
92 msg: if specified, is used as the error message on failure.
93 """
94 pool = descriptor_pool.Default()
95 if isinstance(a, six.string_types):
96 a = text_format.Merge(a, b.__class__(), descriptor_pool=pool)
97
98 for pb in a, b:
99 if check_initialized:
100 errors = pb.FindInitializationErrors()
101 if errors:
102 self.fail('Initialization errors: %s\n%s' % (errors, pb))
103 if normalize_numbers:
104 NormalizeNumberFields(pb)
105
106 a_str = text_format.MessageToString(a, descriptor_pool=pool)
107 b_str = text_format.MessageToString(b, descriptor_pool=pool)
108
109 # Some Python versions would perform regular diff instead of multi-line
110 # diff if string is longer than 2**16. We substitute this behavior
111 # with a call to unified_diff instead to have easier-to-read diffs.
112 # For context, see: https://bugs.python.org/issue11763.
113 if len(a_str) < 2**16 and len(b_str) < 2**16:
114 self.assertMultiLineEqual(a_str, b_str, msg=msg)
115 else:
116 diff = '\n' + ''.join(difflib.unified_diff(a_str.splitlines(True),
117 b_str.splitlines(True)))
118 self.fail('%s : %s' % (msg, diff))
119
120
121def NormalizeNumberFields(pb):

Callers 1

assertProtoEqualMethod · 0.85

Calls 5

NormalizeNumberFieldsFunction · 0.85
failMethod · 0.80
DefaultMethod · 0.45
MergeMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected