Skip to content

Commit fe429d4

Browse files
authored
assert: fix _compare_eq_iterable: re-format both sides (#6137)
2 parents 5738d18 + ce3d431 commit fe429d4

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/_pytest/assertion/util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,13 @@ def _compare_eq_iterable(
273273
if lines_left != lines_right:
274274
if lines_left > lines_right:
275275
max_width = min(len(x) for x in left_formatting)
276-
right_formatting = pprint.pformat(right, width=max_width).splitlines()
277-
lines_right = len(right_formatting)
278276
else:
279277
max_width = min(len(x) for x in right_formatting)
280-
left_formatting = pprint.pformat(left, width=max_width).splitlines()
281-
lines_left = len(left_formatting)
278+
279+
right_formatting = pprint.pformat(right, width=max_width).splitlines()
280+
lines_right = len(right_formatting)
281+
left_formatting = pprint.pformat(left, width=max_width).splitlines()
282+
lines_left = len(left_formatting)
282283

283284
if lines_left > 1 or lines_right > 1:
284285
_surrounding_parens_on_own_lines(left_formatting)

testing/test_assertion.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,43 @@ def test_list_wrap_for_width_rewrap_same_length(self):
459459
" ]",
460460
]
461461

462+
def test_dict_wrap(self):
463+
d1 = {"common": 1, "env": {"env1": 1}}
464+
d2 = {"common": 1, "env": {"env1": 1, "env2": 2}}
465+
466+
diff = callequal(d1, d2, verbose=True)
467+
assert diff == [
468+
"{'common': 1,...: {'env1': 1}} == {'common': 1,...1, 'env2': 2}}",
469+
"Omitting 1 identical items, use -vv to show",
470+
"Differing items:",
471+
"{'env': {'env1': 1}} != {'env': {'env1': 1, 'env2': 2}}",
472+
"Full diff:",
473+
"- {'common': 1, 'env': {'env1': 1}}",
474+
"+ {'common': 1, 'env': {'env1': 1, 'env2': 2}}",
475+
"? +++++++++++",
476+
]
477+
478+
long_a = "a" * 80
479+
sub = {"long_a": long_a, "sub1": {"long_a": "substring that gets wrapped"}}
480+
d1 = {"env": {"sub": sub}}
481+
d2 = {"env": {"sub": sub}, "new": 1}
482+
diff = callequal(d1, d2, verbose=True)
483+
assert diff == [
484+
"{'env': {'sub...s wrapped'}}}} == {'env': {'sub...}}}, 'new': 1}",
485+
"Omitting 1 identical items, use -vv to show",
486+
"Right contains 1 more item:",
487+
"{'new': 1}",
488+
"Full diff:",
489+
" {",
490+
" 'env': {'sub': {'long_a': '" + long_a + "',",
491+
" 'sub1': {'long_a': 'substring '",
492+
" 'that '",
493+
" 'gets '",
494+
" 'wrapped'}}},",
495+
"+ 'new': 1,",
496+
" }",
497+
]
498+
462499
def test_dict(self):
463500
expl = callequal({"a": 0}, {"a": 1})
464501
assert len(expl) > 1

0 commit comments

Comments
 (0)