Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert OrderedDicts to dicts to make diff more understandable #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pytest_icdiff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import OrderedDict

from pprintpp import pformat
import icdiff

Expand All @@ -12,6 +14,11 @@ def pytest_assertrepr_compare(config, op, left, right):
except TypeError:
pass

if isinstance(left, OrderedDict) and type(right) is dict:
left = _convert_to_dict(left)
if isinstance(right, OrderedDict) and type(left) is dict:
right = _convert_to_dict(right)

terminal_writer = config.get_terminal_writer()
cols = terminal_writer.fullwidth - 12

Expand All @@ -27,7 +34,6 @@ def pytest_assertrepr_compare(config, op, left, right):
pretty_left = pformat(left, indent=2, width=cols / 2).splitlines()
pretty_right = pformat(right, indent=2, width=cols / 2).splitlines()


differ = icdiff.ConsoleDiff(cols=cols + 12, tabsize=2)

if not terminal_writer.hasmarkup:
Expand All @@ -43,3 +49,20 @@ def pytest_assertrepr_compare(config, op, left, right):
icdiff_lines = list(differ.make_table(pretty_left, pretty_right))

return ['equals failed'] + [color_off + l for l in icdiff_lines]


def _convert_to_dict(layer):
"""Converts nested ordered dicts to normal dicts"""
result = layer
if isinstance(layer, OrderedDict):
result = dict(layer)
if isinstance(layer, list):
return [_convert_to_dict(item) for item in layer]

try:
for key, value in result.items():
result[key] = _convert_to_dict(value)
except AttributeError:
pass

return result