MCPcopy Index your code
hub / github.com/python/cpython / assertSetEqual

Method assertSetEqual

Lib/unittest/case.py:1144–1185  ·  view source on GitHub ↗

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 diff

(self, set1, set2, msg=None)

Source from the content-addressed store, hash-verified

1142 self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
1143
1144 def assertSetEqual(self, set1, set2, msg=None):
1145 """A set-specific equality assertion.
1146
1147 Args:
1148 set1: The first set to compare.
1149 set2: The second set to compare.
1150 msg: Optional message to use on failure instead of a list of
1151 differences.
1152
1153 assertSetEqual uses ducktyping to support different types of sets, and
1154 is optimized for sets specifically (parameters must support a
1155 difference method).
1156 """
1157 try:
1158 difference1 = set1.difference(set2)
1159 except TypeError as e:
1160 self.fail('invalid type when attempting set difference: %s' % e)
1161 except AttributeError as e:
1162 self.fail('first argument does not support set difference: %s' % e)
1163
1164 try:
1165 difference2 = set2.difference(set1)
1166 except TypeError as e:
1167 self.fail('invalid type when attempting set difference: %s' % e)
1168 except AttributeError as e:
1169 self.fail('second argument does not support set difference: %s' % e)
1170
1171 if not (difference1 or difference2):
1172 return
1173
1174 lines = []
1175 if difference1:
1176 lines.append('Items in the first set but not the second:')
1177 for item in difference1:
1178 lines.append(repr(item))
1179 if difference2:
1180 lines.append('Items in the second set but not the first:')
1181 for item in difference2:
1182 lines.append(repr(item))
1183
1184 standardMsg = '\n'.join(lines)
1185 self.fail(self._formatMessage(msg, standardMsg))
1186
1187 def assertIn(self, member, container, msg=None):
1188 """Just like self.assertTrue(a in b), but with a nicer default message."""

Callers 15

_test_set_comparisonsMethod · 0.95
_events_waitany_testMethod · 0.80
assertSameSetMethod · 0.80
test___all__Method · 0.80
test_uuid6_uniquenessMethod · 0.80
test_uuid7_uniquenessMethod · 0.80
test_uuid8_uniquenessMethod · 0.80
test_set_initMethod · 0.80

Calls 5

failMethod · 0.95
_formatMessageMethod · 0.95
differenceMethod · 0.80
appendMethod · 0.45
joinMethod · 0.45

Tested by 15

_test_set_comparisonsMethod · 0.76
_events_waitany_testMethod · 0.64
assertSameSetMethod · 0.64
test___all__Method · 0.64
test_uuid6_uniquenessMethod · 0.64
test_uuid7_uniquenessMethod · 0.64
test_uuid8_uniquenessMethod · 0.64
test_set_initMethod · 0.64