| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Iterator |
| 4 |
from collections.abc import Mapping |
| 5 |
import pprint |
| 6 |
|
| 7 |
from _pytest._io.saferepr import saferepr |
| 8 |
from _pytest.assertion._typing import _HighlightFunc |
| 9 |
|
| 10 |
|
| 11 |
def _compare_eq_mapping( |
| 12 |
left: Mapping[object, object], |
| 13 |
right: Mapping[object, object], |
| 14 |
highlighter: _HighlightFunc, |
| 15 |
verbose: int = 0, |
| 16 |
) -> Iterator[str]: |
| 17 |
set_left = set(left) |
| 18 |
set_right = set(right) |
| 19 |
common = set_left.intersection(set_right) |
| 20 |
same = {k: left[k] for k in common if left[k] == right[k]} |
| 21 |
if same and verbose < 2: |
| 22 |
yield f"Omitting {len(same)} identical items, use -vv to show" |
| 23 |
elif same: |
| 24 |
yield "Common items:" |
| 25 |
yield from highlighter(pprint.pformat(same)).splitlines() |
| 26 |
diff = {k for k in common if left[k] != right[k]} |
| 27 |
if diff: |
| 28 |
yield "Differing items:" |
| 29 |
for k in diff: |
| 30 |
yield ( |
| 31 |
highlighter(saferepr({k: left[k]})) |
| 32 |
+ " != " |
| 33 |
+ highlighter(saferepr({k: right[k]})) |
| 34 |
) |
| 35 |
extra_left = set_left - set_right |
| 36 |
len_extra_left = len(extra_left) |
| 37 |
if len_extra_left: |
| 38 |
yield f"Left contains {len_extra_left} more item{'' if len_extra_left == 1 else 's'}:" |
| 39 |
yield from highlighter( |
| 40 |
pprint.pformat({k: left[k] for k in extra_left}) |
| 41 |
).splitlines() |
| 42 |
extra_right = set_right - set_left |
| 43 |
len_extra_right = len(extra_right) |
| 44 |
if len_extra_right: |
| 45 |
yield f"Right contains {len_extra_right} more item{'' if len_extra_right == 1 else 's'}:" |
| 46 |
yield from highlighter( |
| 47 |
pprint.pformat({k: right[k] for k in extra_right}) |
| 48 |
).splitlines() |