Skip to content

Commit 2ef4dd9

Browse files
committed
better handling of small things
1 parent f0a2885 commit 2ef4dd9

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

pytest_icdiff.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
import icdiff
55

66
COLS = py.io.TerminalWriter().fullwidth # pylint: disable=no-member
7-
MARGIN_L = 9
7+
MARGIN_L = 10
88
GUTTER = 2
99
MARGINS = MARGIN_L + GUTTER + 1
1010

11+
# def _debug(*things):
12+
# with open('/tmp/icdiff-debug.txt', 'a') as f:
13+
# f.write(' '.join(str(thing) for thing in things))
14+
# f.write('\n')
15+
1116

1217
def pytest_assertrepr_compare(config, op, left, right):
1318
if op != '==':
1419
return
1520

1621
try:
17-
if abs(left + right) < 100:
22+
if abs(left + right) < 19999:
1823
return
1924
except TypeError:
2025
pass
@@ -27,11 +32,13 @@ def pytest_assertrepr_compare(config, op, left, right):
2732

2833
if len(pretty_left) < 3 or len(pretty_right) < 3:
2934
# avoid small diffs far apart by smooshing them up to the left
30-
pretty_left = pformat(left, indent=2, width=1).splitlines()
31-
pretty_right = pformat(right, indent=2, width=1).splitlines()
32-
diff_cols = max(len(l) + 1 for l in pretty_left + pretty_right) * 2
33-
if (diff_cols + MARGINS) > COLS:
34-
diff_cols = COLS - MARGINS
35+
smallest_left = pformat(left, indent=2, width=1).splitlines()
36+
smallest_right = pformat(right, indent=2, width=1).splitlines()
37+
max_side = max(len(l) + 1 for l in smallest_left + smallest_right)
38+
if (max_side * 2 + MARGINS) < COLS:
39+
diff_cols = max_side * 2 + GUTTER
40+
pretty_left = pformat(left, indent=2, width=max_side).splitlines()
41+
pretty_right = pformat(right, indent=2, width=max_side).splitlines()
3542

3643
differ = icdiff.ConsoleDiff(cols=diff_cols, tabsize=2)
3744

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def read(fname):
1313

1414
setup(
1515
name='pytest-icdiff',
16-
version='0.4',
16+
version='0.5',
1717
author='Harry Percival',
1818
author_email='[email protected]',
1919
maintainer='Harry Percival',

tests/test_pytest_icdiff.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,27 @@ def test_one():
249249
)
250250
assert comparison_line.count('hell') > 5
251251

252+
253+
def test_small_numbers_are_specialcased(testdir):
254+
testdir.makepyfile(
255+
f"""
256+
def test_one():
257+
assert 404 == 400
258+
"""
259+
)
260+
output = testdir.runpytest('-vv', '--color=yes').stdout.str()
261+
assert "assert 404 == 400" in output
262+
assert "+404" in output
263+
assert "-400" in output
264+
265+
266+
def test_larger_numbers_are_sane(testdir):
267+
testdir.makepyfile(
268+
f"""
269+
def test_one():
270+
assert 123456 == 1234567
271+
"""
272+
)
273+
output = testdir.runpytest('-vv', '--color=yes').stdout.str()
274+
assert f"123456 123456{GREEN_ON}7" in output
275+

0 commit comments

Comments
 (0)