Skip to content

Commit

Permalink
Merge pull request #92 from timofurrer/feature/support-ordereddict
Browse files Browse the repository at this point in the history
Support comparison of OrderedDict. Refs #55
  • Loading branch information
timofurrer committed Jun 5, 2016
2 parents eea5a13 + 76fe3ed commit 5c9a5a7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Python 3.4 and 3.5 support
- pypy support
- Support comparison of OrderedDict. Refs #55


## No previous changelog history.
Expand Down
21 changes: 20 additions & 1 deletion sure/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)

from sure.terminal import red, green, yellow
from sure.compat import safe_repr
from sure.compat import safe_repr, OrderedDict


class DeepExplanation(text_type):
Expand Down Expand Up @@ -67,6 +67,7 @@ def compare_complex_stuff(self, X, Y):
dict: self.compare_dicts,
list: self.compare_iterables,
tuple: self.compare_iterables,
OrderedDict: self.compare_ordereddict
}
return mapping.get(kind, self.compare_generic)(X, Y)

Expand Down Expand Up @@ -129,6 +130,24 @@ def compare_dicts(self, X, Y):
if isinstance(child, DeepExplanation):
return child

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 get_context(self):
if self._context:
return self._context
Expand Down
49 changes: 45 additions & 4 deletions tests/test_assertion_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from __future__ import unicode_literals
import re
import mock
from collections import OrderedDict

from datetime import datetime
from sure import this, these, those, it, expect, AssertionBuilder
from six import PY3
Expand Down Expand Up @@ -649,18 +651,18 @@ def blah(num):

except AssertionError as e:
if PY3:
expect(str(e)).to.equal("When calling b'blah [tests/test_assertion_builder.py line 633]' the exception message does not match. Expected to match regex: 'invalid regex'\n against:\n 'this message'")
expect(str(e)).to.equal("When calling b'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: 'invalid regex'\n against:\n 'this message'")
else:
expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 633]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'")
expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'")

try:
expect(blah).when.called_with(1).should.throw(ValueError, re.compile(r'invalid regex'))
raise RuntimeError('should not have reached here')
except AssertionError as e:
if PY3:
expect(str(e)).to.equal("When calling b'blah [tests/test_assertion_builder.py line 633]' the exception message does not match. Expected to match regex: 'invalid regex'\n against:\n 'this message'")
expect(str(e)).to.equal("When calling b'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: 'invalid regex'\n against:\n 'this message'")
else:
expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 633]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'")
expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'")

def test_should_not_be_different():
("'something'.should_not.be.different('SOMETHING'.lower())")
Expand Down Expand Up @@ -753,3 +755,42 @@ def test_equals_dictionaries_with_tuple_keys():

expect(X).should_not.equal(Y)
expect(Y).should_not.equal(X)


def test_ordereddict_comparison():
".equal(OrderedDict) should check if two ordered dicts are the same"
result = {
"fields": OrderedDict([
("name", "John"),
("age", "22"),
]),
"children": OrderedDict([]),
}

expectation = {
"fields": OrderedDict([
("age", "22"),
("name", "John"),
]),
"children": OrderedDict([]),
}

expect(result).shouldnt.be.equal(expectation)
expect(result).should.be.equal(result)

try:
expect(result).should.equal(expectation)
raise RuntimeError("should not have reached here")
except AssertionError as error:
if PY3:
expect(str(error)).should.be.equal("""given
X = {'children': {}, 'fields': {'age': '22', 'name': 'John'}}
and
Y = {'children': {}, 'fields': {'age': '22', 'name': 'John'}}
X['fields'] and Y['fields'] are in a different order""")
else:
expect(str(error)).should.be.equal("""given
X = {u'children': {}, u'fields': {u'age': u'22', u'name': u'John'}}
and
Y = {u'children': {}, u'fields': {u'age': u'22', u'name': u'John'}}
X[u'fields'] and Y[u'fields'] are in a different order""")

0 comments on commit 5c9a5a7

Please sign in to comment.