Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import absolute_import
- try:
- from types import SimpleNamespace # type: ignore
- except ImportError: # on Python 2
- from argparse import Namespace as SimpleNamespace
- def _list_comparison(context, ns, l, r):
- """For recursive tuple/list comparison."""
- if len(l) != len(r):
- context.errs.append("[%s]: length does not match: %s != %s" % (ns, len(l), len(r)))
- for i, item in enumerate(l):
- if isinstance(item, context.supported_types) and len(r) > i and type(item) == type(r[i]):
- context.comparators[type(item)](context, ns + [str(i)], item, r[i])
- def _dict_comparison(context, ns, l, r):
- """For recursive dict comparison."""
- for k, v in l.items():
- if isinstance(l[k], context.supported_types) and type(l[k]) == type(r.get(k)):
- context.comparators[type(l[k])](context, ns + [k], l[k], r[k])
- else:
- namespaced = "][".join(map(str, ns + [str(k)]))
- try:
- if r[k] != v:
- context.errs.extend(
- [
- "[{}]:".format(namespaced),
- "LS: {0} [{1}]".format(v, type(v)),
- "RS: {0} [{1}]".format(r[k], type(r[k])),
- ]
- )
- except KeyError:
- context.errs.append("[{}]: missing from the right-side dict".format(namespaced))
- extra_right = set(r.keys()) - set(l.keys())
- if extra_right:
- context.errs.append("extra keys in the right-side dict: {}".format(list(extra_right)))
- def pytest_assertrepr_compare(config, op, left, right):
- """Hook for Pytest producing a better output for non-matching nested structures."""
- if config.getoption("verbose") == 2:
- return None
- context = SimpleNamespace()
- context.comparators = {list: _list_comparison, tuple: _list_comparison, dict: _dict_comparison}
- context.supported_types = tuple(context.comparators.keys())
- context.errs = []
- if isinstance(left, context.supported_types) and type(left) == type(right) and op == "==":
- context.comparators[type(left)](context, [], left, right)
- if context.errs:
- return context.errs
- return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement