moving plugin inside pytest first pass

This commit is contained in:
Aly Sivji 2018-08-02 02:54:15 -05:00
parent b1312147e0
commit 9769bc05c6
2 changed files with 48 additions and 0 deletions

2
.gitignore vendored
View File

@ -44,3 +44,5 @@ coverage.xml
.pydevproject .pydevproject
.project .project
.settings .settings
.vscode
.envrc

View File

@ -122,6 +122,12 @@ def assertrepr_compare(config, op, left, right):
def isset(x): def isset(x):
return isinstance(x, (set, frozenset)) return isinstance(x, (set, frozenset))
def isdatacls(obj):
return hasattr(obj, "__dataclass_fields__")
def isattrs(obj):
return hasattr(obj, "__attrs_attrs__")
def isiterable(obj): def isiterable(obj):
try: try:
iter(obj) iter(obj)
@ -142,6 +148,10 @@ def assertrepr_compare(config, op, left, right):
explanation = _compare_eq_set(left, right, verbose) explanation = _compare_eq_set(left, right, verbose)
elif isdict(left) and isdict(right): elif isdict(left) and isdict(right):
explanation = _compare_eq_dict(left, right, verbose) explanation = _compare_eq_dict(left, right, verbose)
elif type(left) == type(right) and isdatacls(left) and isdatacls(right):
explanation = _compare_eq_class(left, right, verbose, type="data")
elif type(left) == type(right) and isattrs(left) and isattrs(right):
explanation = _compare_eq_class(left, right, verbose, type="attrs")
if isiterable(left) and isiterable(right): if isiterable(left) and isiterable(right):
expl = _compare_eq_iterable(left, right, verbose) expl = _compare_eq_iterable(left, right, verbose)
if explanation is not None: if explanation is not None:
@ -315,6 +325,42 @@ def _compare_eq_dict(left, right, verbose=False):
return explanation return explanation
def _compare_eq_class(left, right, verbose, type=None):
# TODO account for verbose
# TODO write tests
if type == "data":
all_fields = left.__dataclass_fields__
fields_to_check = [field for field, info in all_fields.items() if info.compare]
elif type == "attrs":
all_fields = left.__attrs_attrs__
fields_to_check = [field.name for field in all_fields if field.cmp]
else:
raise RuntimeError # TODO figure out what to raise
same = []
diff = []
for field in fields_to_check:
if getattr(left, field) == getattr(right, field):
same.append(field)
else:
diff.append(field)
explanation = []
if same:
explanation += [("Common attributes:")]
explanation += pprint.pformat(same).splitlines()
if diff:
explanation += [("Differing attributes:")]
for k in diff:
class_name = left.__class__.__name__
explanation += [
u("%s(%s=%r) != %s(%s=%r)")
% (class_name, k, getattr(left, k), class_name, k, getattr(right, k))
]
return explanation
def _notin_text(term, text, verbose=False): def _notin_text(term, text, verbose=False):
index = text.find(term) index = text.find(term)
head = text[:index] head = text[:index]