Skip to content

Commit 93d70e4

Browse files
committed
tests(textframe): Add pytest_assertrepr_compare hook
why: Provide rich assertion output for TextFrame comparisons without requiring syrupy for basic equality checks. what: - Add pytest_assertrepr_compare hook for TextFrame == TextFrame - Show dimension mismatches (width, height) - Show content diff using difflib.ndiff
1 parent 535606b commit 93d70e4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/textframe/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,67 @@
22

33
from __future__ import annotations
44

5+
import typing as t
6+
from difflib import ndiff
7+
58
import pytest
69
from syrupy.assertion import SnapshotAssertion
710

11+
from .core import TextFrame
812
from .plugin import TextFrameExtension
913

1014

15+
def pytest_assertrepr_compare(
16+
config: pytest.Config,
17+
op: str,
18+
left: t.Any,
19+
right: t.Any,
20+
) -> list[str] | None:
21+
"""Provide rich assertion output for TextFrame comparisons.
22+
23+
This hook provides detailed diff output when two TextFrame objects
24+
are compared with ==, showing dimension mismatches and content diffs.
25+
26+
Parameters
27+
----------
28+
config : pytest.Config
29+
The pytest configuration object.
30+
op : str
31+
The comparison operator (e.g., "==", "!=").
32+
left : Any
33+
The left operand of the comparison.
34+
right : Any
35+
The right operand of the comparison.
36+
37+
Returns
38+
-------
39+
list[str] | None
40+
List of explanation lines, or None to use default behavior.
41+
"""
42+
if not isinstance(left, TextFrame) or not isinstance(right, TextFrame):
43+
return None
44+
if op != "==":
45+
return None
46+
47+
lines = ["TextFrame comparison failed:"]
48+
49+
# Dimension mismatch
50+
if left.content_width != right.content_width:
51+
lines.append(f" width: {left.content_width} != {right.content_width}")
52+
if left.content_height != right.content_height:
53+
lines.append(f" height: {left.content_height} != {right.content_height}")
54+
55+
# Content diff
56+
left_render = left.render().splitlines()
57+
right_render = right.render().splitlines()
58+
if left_render != right_render:
59+
lines.append("")
60+
lines.append("Content diff:")
61+
lines.extend(ndiff(right_render, left_render))
62+
63+
return lines
64+
65+
1166
@pytest.fixture
1267
def snapshot(snapshot: SnapshotAssertion) -> SnapshotAssertion:
1368
"""Override default snapshot fixture to use TextFrameExtension.

0 commit comments

Comments
 (0)