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

Method assertCountEqual

Lib/unittest/case.py:1226–1256  ·  view source on GitHub ↗

Asserts that two iterables have the same elements, the same number of times, without regard to order. self.assertEqual(Counter(list(first)), Counter(list(second))) Example: - [0, 1, 1] and [1, 0, 1] compare equal. -

(self, first, second, msg=None)

Source from the content-addressed store, hash-verified

1224 self.fail(self._formatMessage(msg, standardMsg))
1225
1226 def assertCountEqual(self, first, second, msg=None):
1227 """Asserts that two iterables have the same elements, the same number of
1228 times, without regard to order.
1229
1230 self.assertEqual(Counter(list(first)),
1231 Counter(list(second)))
1232
1233 Example:
1234 - [0, 1, 1] and [1, 0, 1] compare equal.
1235 - [0, 0, 1] and [0, 1] compare unequal.
1236
1237 """
1238 first_seq, second_seq = list(first), list(second)
1239 try:
1240 first = collections.Counter(first_seq)
1241 second = collections.Counter(second_seq)
1242 except TypeError:
1243 # Handle case with unhashable elements
1244 differences = _count_diff_all_purpose(first_seq, second_seq)
1245 else:
1246 if first == second:
1247 return
1248 differences = _count_diff_hashable(first_seq, second_seq)
1249
1250 if differences:
1251 standardMsg = 'Element counts were not equal:\n'
1252 lines = ['First has %d, Second has %d: %r' % diff for diff in differences]
1253 diffMsg = '\n'.join(lines)
1254 standardMsg = self._truncateMessage(standardMsg, diffMsg)
1255 msg = self._formatMessage(msg, standardMsg)
1256 self.fail(msg)
1257
1258 def assertMultiLineEqual(self, first, second, msg=None):
1259 """Assert that two multi-line strings are equal."""

Callers 15

test_args_kwargsMethod · 0.95
test_allMethod · 0.80
test___all__Method · 0.80
test_allMethod · 0.80
globMethod · 0.80
test_make_tarfileMethod · 0.80
test_make_zipfileMethod · 0.80

Calls 7

_truncateMessageMethod · 0.95
_formatMessageMethod · 0.95
failMethod · 0.95
listClass · 0.85
_count_diff_all_purposeFunction · 0.85
_count_diff_hashableFunction · 0.85
joinMethod · 0.45

Tested by 15

test_args_kwargsMethod · 0.76
test_allMethod · 0.64
test___all__Method · 0.64
test_allMethod · 0.64
globMethod · 0.64
test_make_tarfileMethod · 0.64
test_make_zipfileMethod · 0.64