def equal(self, what, epsilon=None): """compares given object ``X`` with an expected ``Y`` object. It primarily assures that the compared objects are absolute equal ``==``. :param what: the expected value :param epsilon: a delta to leverage upper-bound floating point permissiveness """ try: comparison = DeepComparison(self.obj, what, epsilon).compare() error = False except AssertionError as e: error = e comparison = None if isinstance(comparison, DeepExplanation): error = comparison.get_assertion(self.obj, what) if self.negative: if error: return True msg = '%s should differ from %s, but is the same thing' raise AssertionError(msg % (safe_repr(self.obj), safe_repr(what))) else: if not error: return True raise error
def callable(self): if self.negative: assert not callable(self.obj), ( 'expected `{0}` to not be callable but it is'.format(safe_repr(self.obj))) else: assert callable(self.obj), ( 'expected {0} to be callable'.format(safe_repr(self.obj))) return True
def test_unicode(): "dicts with unicode should work properly" class Y(object): def __init__(self, x): self.x = x def __repr__(self): if PY2: # PY2 should return the regular (unicode) string return self.x.encode('utf-8') else: return self.x def __eq__(self, other): return self.x == other.x y1 = { 'a': 2, 'b': Y('Gabriel Falcão'), 'c': 'Foo', } name = 'Gabriel Falc\xe3o' if PY2 else 'Gabriel Falcão' expect(safe_repr(y1)).should.equal( compat_repr("{'a': 2, 'b': %s, 'c': 'Foo'}" % name))
def test_unicode(): "dicts with unicode should work properly" class Y(object): def __init__(self, x): self.x = x def __repr__(self): if PY3: # PY3K should return the regular (unicode) string return self.x else: return self.x.encode('utf-8') def __eq__(self, other): return self.x == other.x y1 = { 'a': 2, 'b': Y('Gabriel Falcão'), 'c': 'Foo', } name = 'Gabriel Falcão' if PY3 else 'Gabriel Falc\xe3o' expect(safe_repr(y1)).should.equal(compat_repr( "{'a': 2, 'b': %s, 'c': 'Foo'}" % name ))
def test_nested_dict(): "dicts nested inside values should also get sorted" X = {'my::all_users': [{'age': 33, 'name': 'John', 'foo': 'bar'}]} expect(safe_repr(X)).should.equal( compat_repr( '''{'my::all_users': [{'age': 33, 'foo': 'bar', 'name': 'John'}]}''' ))
def __getattr__(self, attr): try: return super(VariablesBag, self).__getattribute__(attr) except AttributeError: if attr not in dir(VariablesBag): raise AssertionError(not_here_error % ( attr, safe_repr(self.__varnames__), ))
def empty(self): representation = safe_repr(self.obj) length = len(self.obj) if self.negative: assert length > 0, "expected `{0}` to not be empty".format(representation) else: assert length == 0, "expected `{0}` to be empty but it has {1} items".format(representation, length) return True
def wrapper(self, *args, **kw): value = func(self, *args, **kw) msg = "{0}({1}) failed".format( func.__name__, ", ".join(map(safe_repr, args)), ", ".join(["{0}={1}".format(k, safe_repr(kw[k])) for k in kw]), ) assert value, unicode(msg) return value
def empty(self): representation = safe_repr(self.obj) length = len(self.obj) if self.negative: assert length > 0, ( "expected `{0}` to not be empty".format(representation)) else: assert length == 0, ( "expected `{0}` to be empty but it has {1} items".format(representation, length)) return True
def wrapper(self, *args, **kw): value = func(self, *args, **kw) msg = "{0}({1}) failed".format( func.__name__, ", ".join(map(safe_repr, args)), ", ".join(["{0}={1}".format(k, safe_repr(kw[k])) for k in kw]), ) if not PY3: msg = text_type(msg) assert value, msg return value
def equal(self, what, epsilon=None): try: comparison = DeepComparison(self.obj, what, epsilon).compare() error = False except AssertionError as e: error = e comparison = None if isinstance(comparison, DeepExplanation): error = comparison.get_assertion(self.obj, what) if self.negative: if error: return True msg = '%s should differ from %s, but is the same thing' raise AssertionError(msg % (safe_repr(self.obj), safe_repr(what))) else: if not error: return True raise error
def wrapper(self, *args, **kw): try: value = func(self, *args, **kw) except AssertionError as e: raise AssertionError(e) msg = "{0}({1}) failed".format( func.__name__, ", ".join(map(safe_repr, args)), ", ".join(["{0}={1}".format(k, safe_repr(kw[k])) for k in kw]), ) if PY2: msg = text_type(msg) assert value, msg return value
def test_basic_list(): "safe_repr should display a simple list" X = ['one', 'yeah'] expect(safe_repr(X)).should.equal(compat_repr( "['one', 'yeah']" ))
def test_basic_dict(): "safe_repr should return a sorted repr" X = {'b': 'd', 'a': 'c'} expect(safe_repr(X)).should.equal(compat_repr( "{'a': 'c', 'b': 'd'}" ))
def test_nested_dict(): "dicts nested inside values should also get sorted" X = {'my::all_users': [{'age': 33, 'name': 'John', 'foo': 'bar'}]} expect(safe_repr(X)).should.equal(compat_repr( '''{'my::all_users': [{'age': 33, 'foo': 'bar', 'name': 'John'}]}''' ))
def test_basic_list(): "safe_repr should display a simple list" X = ['one', 'yeah'] expect(safe_repr(X)).should.equal(compat_repr("['one', 'yeah']"))
def test_basic_dict(): "safe_repr should return a sorted repr" X = {'b': 'd', 'a': 'c'} expect(safe_repr(X)).should.equal(compat_repr("{'a': 'c', 'b': 'd'}"))