Add tests and improve messages
This commit is contained in:
parent
7f1bf44aa8
commit
47d92a0d96
|
@ -292,16 +292,23 @@ def _compare_eq_sequence(left, right, verbose=0):
|
||||||
explanation += [u"At index %s diff: %r != %r" % (i, left[i], right[i])]
|
explanation += [u"At index %s diff: %r != %r" % (i, left[i], right[i])]
|
||||||
break
|
break
|
||||||
len_diff = len_left - len_right
|
len_diff = len_left - len_right
|
||||||
if len_diff > 0:
|
|
||||||
explanation += [
|
if len_diff:
|
||||||
u"Left contains %d more items, first extra item: %s"
|
if len_diff > 0:
|
||||||
% (len_diff, saferepr(left[len_right]))
|
dir_with_more = "Left"
|
||||||
]
|
extra = saferepr(left[len_right])
|
||||||
elif len_diff < 0:
|
elif len_diff < 0:
|
||||||
explanation += [
|
len_diff = 0 - len_diff
|
||||||
u"Right contains %d more items, first extra item: %s"
|
dir_with_more = "Right"
|
||||||
% (0 - len_diff, saferepr(right[len_left]))
|
extra = saferepr(right[len_left])
|
||||||
]
|
|
||||||
|
if len_diff == 1:
|
||||||
|
explanation += [u"%s contains one more item: %s" % (dir_with_more, extra)]
|
||||||
|
else:
|
||||||
|
explanation += [
|
||||||
|
u"%s contains %d more items, first extra item: %s"
|
||||||
|
% (dir_with_more, len_diff, extra)
|
||||||
|
]
|
||||||
return explanation
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
@ -337,14 +344,22 @@ def _compare_eq_dict(left, right, verbose=0):
|
||||||
for k in diff:
|
for k in diff:
|
||||||
explanation += [saferepr({k: left[k]}) + " != " + saferepr({k: right[k]})]
|
explanation += [saferepr({k: left[k]}) + " != " + saferepr({k: right[k]})]
|
||||||
extra_left = set_left - set_right
|
extra_left = set_left - set_right
|
||||||
if extra_left:
|
len_extra_left = len(extra_left)
|
||||||
explanation.append(u"Left contains %d more items:" % len(extra_left))
|
if len_extra_left:
|
||||||
|
explanation.append(
|
||||||
|
u"Left contains %d more item%s:"
|
||||||
|
% (len_extra_left, "" if len_extra_left == 1 else "s")
|
||||||
|
)
|
||||||
explanation.extend(
|
explanation.extend(
|
||||||
pprint.pformat({k: left[k] for k in extra_left}).splitlines()
|
pprint.pformat({k: left[k] for k in extra_left}).splitlines()
|
||||||
)
|
)
|
||||||
extra_right = set_right - set_left
|
extra_right = set_right - set_left
|
||||||
if extra_right:
|
len_extra_right = len(extra_right)
|
||||||
explanation.append(u"Right contains %d more items:" % len(extra_right))
|
if len_extra_right:
|
||||||
|
explanation.append(
|
||||||
|
u"Right contains %d more item%s:"
|
||||||
|
% (len_extra_right, "" if len_extra_right == 1 else "s")
|
||||||
|
)
|
||||||
explanation.extend(
|
explanation.extend(
|
||||||
pprint.pformat({k: right[k] for k in extra_right}).splitlines()
|
pprint.pformat({k: right[k] for k in extra_right}).splitlines()
|
||||||
)
|
)
|
||||||
|
|
|
@ -446,6 +446,50 @@ class TestAssert_reprcompare(object):
|
||||||
assert "Omitting" not in lines[1]
|
assert "Omitting" not in lines[1]
|
||||||
assert lines[2] == "{'b': 1}"
|
assert lines[2] == "{'b': 1}"
|
||||||
|
|
||||||
|
def test_dict_different_items(self):
|
||||||
|
lines = callequal({"a": 0}, {"b": 1, "c": 2}, verbose=2)
|
||||||
|
assert lines == [
|
||||||
|
"{'a': 0} == {'b': 1, 'c': 2}",
|
||||||
|
"Left contains 1 more item:",
|
||||||
|
"{'a': 0}",
|
||||||
|
"Right contains 2 more items:",
|
||||||
|
"{'b': 1, 'c': 2}",
|
||||||
|
"Full diff:",
|
||||||
|
"- {'a': 0}",
|
||||||
|
"+ {'b': 1, 'c': 2}",
|
||||||
|
]
|
||||||
|
lines = callequal({"b": 1, "c": 2}, {"a": 0}, verbose=2)
|
||||||
|
assert lines == [
|
||||||
|
"{'b': 1, 'c': 2} == {'a': 0}",
|
||||||
|
"Left contains 2 more items:",
|
||||||
|
"{'b': 1, 'c': 2}",
|
||||||
|
"Right contains 1 more item:",
|
||||||
|
"{'a': 0}",
|
||||||
|
"Full diff:",
|
||||||
|
"- {'b': 1, 'c': 2}",
|
||||||
|
"+ {'a': 0}",
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_sequence_different_items(self):
|
||||||
|
lines = callequal((1, 2), (3, 4, 5), verbose=2)
|
||||||
|
assert lines == [
|
||||||
|
"(1, 2) == (3, 4, 5)",
|
||||||
|
"At index 0 diff: 1 != 3",
|
||||||
|
"Right contains one more item: 5",
|
||||||
|
"Full diff:",
|
||||||
|
"- (1, 2)",
|
||||||
|
"+ (3, 4, 5)",
|
||||||
|
]
|
||||||
|
lines = callequal((1, 2, 3), (4,), verbose=2)
|
||||||
|
assert lines == [
|
||||||
|
"(1, 2, 3) == (4,)",
|
||||||
|
"At index 0 diff: 1 != 4",
|
||||||
|
"Left contains 2 more items, first extra item: 2",
|
||||||
|
"Full diff:",
|
||||||
|
"- (1, 2, 3)",
|
||||||
|
"+ (4,)",
|
||||||
|
]
|
||||||
|
|
||||||
def test_set(self):
|
def test_set(self):
|
||||||
expl = callequal({0, 1}, {0, 2})
|
expl = callequal({0, 1}, {0, 2})
|
||||||
assert len(expl) > 1
|
assert len(expl) > 1
|
||||||
|
|
Loading…
Reference in New Issue