|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import typing as t |
| 6 | +from difflib import ndiff |
| 7 | + |
5 | 8 | import pytest |
6 | 9 | from syrupy.assertion import SnapshotAssertion |
7 | 10 |
|
| 11 | +from .core import TextFrame |
8 | 12 | from .plugin import TextFrameExtension |
9 | 13 |
|
10 | 14 |
|
| 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 | + |
11 | 66 | @pytest.fixture |
12 | 67 | def snapshot(snapshot: SnapshotAssertion) -> SnapshotAssertion: |
13 | 68 | """Override default snapshot fixture to use TextFrameExtension. |
|
0 commit comments