Skip to content

Commit 399ee45

Browse files
committed
update truncation and display
1 parent 39dc8d5 commit 399ee45

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

check50/_api.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,18 @@ class Mismatch(Failure):
455455
"""
456456

457457
def __init__(self, expected, actual, help=None):
458-
super().__init__(rationale=_("expected {}, not {}").format(_raw(expected), _raw(actual)), help=help)
458+
expected, actual = _truncate(expected, actual), _truncate(actual, expected)
459+
460+
# rationale = _("expected {}, not {}").format(
461+
# _raw(expected),
462+
# _raw(actual)
463+
# )
464+
rationale = _("expected: {}\n actual: {}").format(
465+
_raw(expected),
466+
_raw(actual)
467+
)
468+
469+
super().__init__(rationale=rationale, help=help)
459470

460471
if expected == EOF:
461472
expected = "EOF"
@@ -495,6 +506,30 @@ def wrapper(*args, **kwargs):
495506
return wrapper
496507
return decorator
497508

509+
def _truncate(s, other, max_len=10):
510+
"""Truncate string s around its first difference with other"""
511+
512+
# find the index of first difference
513+
limit = min(len(s), len(other))
514+
i = limit
515+
for index in range(limit):
516+
if s[index] != other[index]:
517+
i = index
518+
break
519+
520+
# center around diff
521+
start = max(i - (max_len // 2), 0)
522+
end = min(start + max_len, len(s))
523+
524+
snippet = s[start:end]
525+
526+
if start > 0:
527+
snippet = "..." + snippet
528+
if end < len(s):
529+
snippet = snippet + "..."
530+
531+
return snippet
532+
498533

499534
def _raw(s):
500535
"""Get raw representation of s, truncating if too long."""
@@ -506,8 +541,7 @@ def _raw(s):
506541
return "EOF"
507542

508543
s = f'"{repr(str(s))[1:-1]}"'
509-
# if len(s) > 15:
510-
# s = s[:15] + "...\"" # Truncate if too long
544+
511545
return s
512546

513547

check50/renderer/_renderers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
TEMPLATES = pathlib.Path(files("check50.renderer").joinpath("templates"))
1010

11-
1211
def to_html(slug, results, version):
1312
with open(TEMPLATES / "results.html") as f:
1413
content = f.read()

check50/renderer/templates/results.html

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,37 @@ <h3 style="color:orange">:| {{ check.description }}</h3>
7575
{% if check.cause and "actual" in check.cause and "expected" in check.cause %}
7676
<br/>
7777
<div class="row">
78-
{% set expected = check.cause.expected | join('\n') if check.cause.expected is not string else check.cause.expected %}
7978
<div class="col-md-6">
8079
<b>Expected Output:</b>
81-
<pre style="background-color:black; color:white; padding:10px;">
82-
{{ expected }}
83-
</pre>
80+
<br/>
81+
<div style="background-color:black; color:white;">
82+
<samp>
83+
{% autoescape false %}
84+
{% if check.cause.expected is not string %}
85+
{% set expected = check.cause.expected | join('\n') | e %}
86+
{% else %}
87+
{% set expected = check.cause.expected | e %}
88+
{% endif %}
89+
{{ expected | replace(" ", "&nbsp;") | replace("\n", "<br/>") }}
90+
{% endautoescape %}
91+
</samp>
92+
</div>
8493
</div>
85-
86-
{% set actual = check.cause.actual | join('\n') if check.cause.actual is not string else check.cause.actual %}
8794
<div class="col-md-6">
8895
<b>Actual Output:</b>
89-
<pre style="background-color:black; color:white; padding:10px;">
90-
{{ actual }}
91-
</pre>
96+
<br/>
97+
<div style="background-color:black; color:white;">
98+
<samp>
99+
{% autoescape false %}
100+
{% if check.cause.actual is not string %}
101+
{% set actual = check.cause.actual | join('\n') | e %}
102+
{% else %}
103+
{% set actual = check.cause.actual | e %}
104+
{% endif %}
105+
{{ actual | replace(" ", "&nbsp;") | replace("\n", "<br/>") }}
106+
{% endautoescape %}
107+
</samp>
108+
</div>
92109
</div>
93110
</div>
94111
{% endif %}

0 commit comments

Comments
 (0)