Merge "Add assertSetEqual to base test class"

This commit is contained in:
Jenkins 2013-12-13 11:45:28 +00:00 committed by Gerrit Code Review
commit f6aa72334c

View File

@ -523,3 +523,56 @@ class TestCase(testtools.TestCase):
raise
else:
s.close()
def assertSetEqual(self, set1, set2, msg=None):
# TODO(morganfainberg): Remove this and self._assertSetEqual once
# support for python 2.6 is no longer needed.
if (sys.version_info < (2, 7)):
return self._assertSetEqual(set1, set2, msg=None)
else:
# use the native assertSetEqual
return super(TestCase, self).assertSetEqual(set1, set2, msg=msg)
def _assertSetEqual(self, set1, set2, msg=None):
"""A set-specific equality assertion.
Args:
set1: The first set to compare.
set2: The second set to compare.
msg: Optional message to use on failure instead of a list of
differences.
assertSetEqual uses ducktyping to support different types of sets, and
is optimized for sets specifically (parameters must support a
difference method).
"""
try:
difference1 = set1.difference(set2)
except TypeError as e:
self.fail('invalid type when attempting set difference: %s' % e)
except AttributeError as e:
self.fail('first argument does not support set difference: %s' % e)
try:
difference2 = set2.difference(set1)
except TypeError as e:
self.fail('invalid type when attempting set difference: %s' % e)
except AttributeError as e:
self.fail('second argument does not support set difference: %s' %
e)
if not (difference1 or difference2):
return
lines = []
if difference1:
lines.append('Items in the first set but not the second:')
for item in difference1:
lines.append(repr(item))
if difference2:
lines.append('Items in the second set but not the first:')
for item in difference2:
lines.append(repr(item))
standardMsg = '\n'.join(lines)
self.fail(self._formatMessage(msg, standardMsg))