tests: Improve partial_compare output.
This commit is contained in:
parent
256352024b
commit
9c5ce8a688
@ -21,31 +21,38 @@
|
|||||||
|
|
||||||
|
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
import pprint
|
||||||
|
|
||||||
|
|
||||||
def _partial_compare_dict(val1, val2):
|
def print_i(text, indent, error=False):
|
||||||
|
if error:
|
||||||
|
text = '| ****** {} ******'.format(text)
|
||||||
|
for line in text.splitlines():
|
||||||
|
print('| ' * indent + line)
|
||||||
|
|
||||||
|
|
||||||
|
def _partial_compare_dict(val1, val2, *, indent=0):
|
||||||
for key in val2:
|
for key in val2:
|
||||||
if key not in val1:
|
if key not in val1:
|
||||||
print("Key {!r} is in second dict but not in first!".format(key))
|
print_i("Key {!r} is in second dict but not in first!".format(key),
|
||||||
|
indent, error=True)
|
||||||
return False
|
return False
|
||||||
if not partial_compare(val1[key], val2[key]):
|
if not partial_compare(val1[key], val2[key], indent=indent+1):
|
||||||
print("Comparison failed for {!r} and {!r}!".format(
|
|
||||||
val1[key], val2[key]))
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _partial_compare_list(val1, val2):
|
def _partial_compare_list(val1, val2, *, indent=0):
|
||||||
if len(val1) < len(val2):
|
if len(val1) < len(val2):
|
||||||
print("Second list is longer than first list -> False!")
|
print_i("Second list is longer than first list", indent, error=True)
|
||||||
return False
|
return False
|
||||||
for item1, item2 in zip(val1, val2):
|
for item1, item2 in zip(val1, val2):
|
||||||
if not partial_compare(item1, item2):
|
if not partial_compare(item1, item2, indent=indent+1):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def partial_compare(val1, val2):
|
def partial_compare(val1, val2, *, indent=0):
|
||||||
"""Do a partial comparison between the given values.
|
"""Do a partial comparison between the given values.
|
||||||
|
|
||||||
For dicts, keys in val2 are checked, others are ignored.
|
For dicts, keys in val2 are checked, others are ignored.
|
||||||
@ -54,31 +61,34 @@ def partial_compare(val1, val2):
|
|||||||
|
|
||||||
This happens recursively.
|
This happens recursively.
|
||||||
"""
|
"""
|
||||||
print()
|
print_i("Comparing", indent)
|
||||||
print("Comparing\n {!r}\nto\n {!r}".format(val1, val2))
|
print_i(pprint.pformat(val1), indent + 1)
|
||||||
|
print_i("|---- to ----", indent)
|
||||||
|
print_i(pprint.pformat(val2), indent + 1)
|
||||||
|
|
||||||
|
|
||||||
if val2 is Ellipsis:
|
if val2 is Ellipsis:
|
||||||
print("Ignoring ellipsis comparison")
|
print_i("Ignoring ellipsis comparison", indent, error=True)
|
||||||
return True
|
return True
|
||||||
elif type(val1) != type(val2): # pylint: disable=unidiomatic-typecheck
|
elif type(val1) != type(val2): # pylint: disable=unidiomatic-typecheck
|
||||||
print("Different types ({}, {}) -> False".format(
|
print_i("Different types ({}, {}) -> False".format(
|
||||||
type(val1), type(val2)))
|
type(val1), type(val2)), indent, error=True)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if isinstance(val2, dict):
|
if isinstance(val2, dict):
|
||||||
print("Comparing as dicts")
|
print_i("|======= Comparing as dicts", indent)
|
||||||
equal = _partial_compare_dict(val1, val2)
|
equal = _partial_compare_dict(val1, val2, indent=indent)
|
||||||
elif isinstance(val2, list):
|
elif isinstance(val2, list):
|
||||||
print("Comparing as lists")
|
print_i("|======= Comparing as lists", indent)
|
||||||
equal = _partial_compare_list(val1, val2)
|
equal = _partial_compare_list(val1, val2, indent=indent)
|
||||||
elif isinstance(val2, float):
|
elif isinstance(val2, float):
|
||||||
print("Doing float comparison")
|
print_i("|======= Doing float comparison", indent)
|
||||||
equal = abs(val1 - val2) < 0.00001
|
equal = abs(val1 - val2) < 0.00001
|
||||||
elif isinstance(val2, str):
|
elif isinstance(val2, str):
|
||||||
print("Doing string comparison")
|
print_i("|======= Doing string comparison", indent)
|
||||||
equal = fnmatch.fnmatchcase(val1, val2)
|
equal = fnmatch.fnmatchcase(val1, val2)
|
||||||
else:
|
else:
|
||||||
print("Comparing via ==")
|
print_i("|======= Comparing via ==", indent)
|
||||||
equal = val1 == val2
|
equal = val1 == val2
|
||||||
print("---> {}".format(equal))
|
print_i("---> {}".format(equal), indent)
|
||||||
return equal
|
return equal
|
||||||
|
Loading…
Reference in New Issue
Block a user