def compare(self): X, Y = self.operands c = self.get_context() if self.is_simple(X) and self.is_simple(Y): # both simple if X == Y: return True c = self.get_context() m = u"X%s is %%r whereas Y%s is %%r" msg = m % (red(c.current_X_keys), green(c.current_Y_keys)) % (X, Y) return DeepExplanation(msg) elif type(X) is not type(Y): # different types xname, yname = map(lambda _: type(_).__name__, (X, Y)) msg = u'X%s is a %%s and Y%s is a %%s instead' % ( red(c.current_X_keys), green(c.current_Y_keys), ) % (xname, yname) exp = DeepExplanation(msg) else: exp = self.compare_complex_stuff(X, Y) if isinstance(exp, DeepExplanation): original_X, original_Y = c.parent.operands raise exp.as_assertion(original_X, original_Y) return exp
def compare(self): X, Y = self.operands if isinstance(X, _CallList): X = list(X) if isinstance(Y, _CallList): X = list(Y) c = self.get_context() if self.is_simple(X) and self.is_simple(Y): # both simple if X == Y: return True c = self.get_context() m = "X%s is %%r whereas Y%s is %%r" msg = m % (red(c.current_X_keys), green(c.current_Y_keys)) % (X, Y) return DeepExplanation(msg) elif type(X) is not type(Y): # different types xname, yname = map(lambda _: type(_).__name__, (X, Y)) msg = 'X%s is a %%s and Y%s is a %%s instead' % ( red(c.current_X_keys), green(c.current_Y_keys), ) % (xname, yname) exp = DeepExplanation(msg) else: exp = self.compare_complex_stuff(X, Y) if isinstance(exp, DeepExplanation): original_X, original_Y = c.parent.operands raise exp.as_assertion(original_X, original_Y) return exp
def compare_generic(self, X, Y): c = self.get_context() if X == Y: return True else: m = 'X%s != Y%s' % (red(c.current_X_keys), green(c.current_Y_keys)) return DeepExplanation(m)
def compare_generic(self, X, Y): c = self.get_context() if X == Y: return True else: m = u'X%s != Y%s' % (red(c.current_X_keys), green(c.current_Y_keys)) return DeepExplanation(m)
def safe_repr(x): try: ret = green(repr(x).decode('utf-8')) except UnicodeEncodeError: ret = red('a %r that cannot be represented' % type(x)) return utf8_bytes(ret)
def compare_generic(self, X, Y, msg_format='X{0} != Y{1}'): c = self.get_context() if X == Y: return True else: m = msg_format.format(red(c.current_X_keys), green(c.current_Y_keys)) return DeepExplanation(m)
def compare_floats(self, X, Y): c = self.get_context() if self.epsilon is None: return self.compare_generic(X, Y) if abs(X - Y) <= self.epsilon: return True else: m = 'X%s±%s != Y%s±%s' % (red(c.current_X_keys), self.epsilon, green(c.current_Y_keys), self.epsilon) return DeepExplanation(m)
def compare_floats(self, X, Y): c = self.get_context() if self.epsilon is None: return self.compare_generic(X, Y) if abs(X - Y) <= self.epsilon: return True else: m = 'X{0}±{1} != Y{2}±{3}'.format( (red(c.current_X_keys), self.epsilon, green(c.current_Y_keys), self.epsilon)) return DeepExplanation(m)
def compare_dicts(self, X, Y): c = self.get_context() x_keys = list(sorted(X.keys())) y_keys = list(sorted(Y.keys())) diff_x = list(set(x_keys).difference(set(y_keys))) diff_y = list(set(y_keys).difference(set(x_keys))) if diff_x: msg = "X{0} has the key {1!r} whereas Y{2} does not".format( red(c.current_X_keys), safe_repr(diff_x[0]), green(c.current_Y_keys)) return DeepExplanation(msg) elif diff_y: msg = "X{0} does not have the key {1!r} whereas Y{2} has it".format( red(c.current_X_keys), safe_repr(diff_y[0]), green(c.current_Y_keys)) return DeepExplanation(msg) elif X == Y: return True else: for key_X, key_Y in zip(x_keys, y_keys): self.key_X = key_X self.key_Y = key_Y value_X = X[key_X] value_Y = Y[key_Y] child = DeepComparison( value_X, value_Y, epsilon=self.epsilon, parent=self, ).compare() if isinstance(child, DeepExplanation): return child
def compare_dicts(self, X, Y): c = self.get_context() x_keys = list(sorted(X.keys())) y_keys = list(sorted(Y.keys())) diff_x = list(set(x_keys).difference(set(y_keys))) diff_y = list(set(y_keys).difference(set(x_keys))) if diff_x: msg = u"X%s has the key '%%s' whereas Y%s doesn't" % ( red(c.current_X_keys), green(c.current_Y_keys), ) % diff_x[0] return DeepExplanation(msg) elif diff_y: msg = u"X%s doesn't have the key '%%s' whereas Y%s has it" % ( red(c.current_X_keys), green(c.current_Y_keys), ) % diff_y[0] return DeepExplanation(msg) elif X == Y: return True else: for key_X, key_Y in zip(x_keys, y_keys): self.key_X = key_X self.key_Y = key_Y value_X = X[key_X] value_Y = Y[key_Y] child = DeepComparison( value_X, value_Y, parent=self, ).compare() if isinstance(child, DeepExplanation): return child
def safe_repr(val): try: if isinstance(val, dict): # We special case dicts to have a sorted repr. This makes testing # significantly easier val = _obj_with_safe_repr(val) ret = repr(val) if not PY3: ret = ret.decode('utf-8') except UnicodeEncodeError: ret = red('a %r that cannot be represented' % type(val)) else: ret = green(ret) return ret
def safe_repr(val): try: if isinstance(val, dict): # We special case dicts to have a sorted repr. This makes testing # significantly easier val = _obj_with_safe_repr(val) ret = repr(val) if six.PY2: ret = ret.decode('utf-8') except UnicodeEncodeError: ret = red('a %r that cannot be represented' % type(val)) else: ret = green(ret) return ret
def compare_ordereddict(self, X, Y): """Compares two instances of an OrderedDict.""" # check if OrderedDict instances have the same keys and values child = self.compare_dicts(X, Y) if isinstance(child, DeepExplanation): return child # check if the order of the keys is the same for i, j in zip(X.items(), Y.items()): if i[0] != j[0]: c = self.get_context() msg = "X{0} and Y{1} are in a different order".format( red(c.current_X_keys), green(c.current_Y_keys)) return DeepExplanation(msg) return True
def compare_ordereddict(self, X, Y): """Compares two instances of an OrderedDict.""" # check if OrderedDict instances have the same keys and values child = self.compare_dicts(X, Y) if isinstance(child, DeepExplanation): return child # check if the order of the keys is the same for i, j in zip(X.items(), Y.items()): if i[0] != j[0]: c = self.get_context() msg = "X{0} and Y{1} are in a different order".format( red(c.current_X_keys), green(c.current_Y_keys) ) return DeepExplanation(msg) return True
def compare_dicts(self, X, Y): """ Difference from parent class is that it could do partial matching :param X: first dict :param Y: second dict :return: True if comparing is succesfull, otherwise it will throw AssertionError """ c = self.get_context() x_keys = list(sorted(X.keys())) y_keys = list(sorted(Y.keys())) diff_y = list(set(y_keys).difference(set(x_keys))) if diff_y: msg = "Filter Param%s has the key %%r whereas Source Item%s does not" % ( red(c.current_X_keys), green(c.current_Y_keys), ) % safe_repr(diff_y[0]) return DeepExplanation(msg).as_assertion(X, Y) elif X == Y: return True else: for key in y_keys: self.key_X = key self.key_Y = key value_X = X[key] value_Y = Y[key] child = ComparisonWrapper( value_X, value_Y, epsilon=self.epsilon, parent=self, ).compare() if isinstance(child, DeepExplanation): return child.as_assertion(value_X, value_Y) return True