Skip to content

Commit

Permalink
Adds diff for differing strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Wesseling committed Jan 30, 2013
1 parent 37d4805 commit 46b987b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions expecter.py
@@ -1,4 +1,5 @@
__all__ = ['expect']
import difflib


class expect(object):
Expand Down Expand Up @@ -35,8 +36,13 @@ def __getattr__(self, name):
return getattr(super(expect, self), name)

def __eq__(self, other):
assert self._actual == other, (
'Expected %s but got %s' % (repr(other), repr(self._actual)))
msg = 'Expected %s but got %s' % (repr(other), repr(self._actual))
if isinstance(other, str) and isinstance(self._actual, str):
diff = difflib.unified_diff(other.split('\n'),
self._actual.split('\n'),
lineterm='')
msg = '\n'.join([msg, 'Diff:'] + list(diff)[2:])
assert self._actual == other, msg
return self

def __ne__(self, other):
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_expecter.py
Expand Up @@ -12,6 +12,17 @@ def _fails(): expect(1) == 2
assert_raises(AssertionError, _fails)
assert fail_msg(_fails) == 'Expected 2 but got 1'

def it_shows_diff_when_strings_differ(self):
def _fails(): expect('foo\nbar') == 'foo\nbaz'
assert_raises(AssertionError, _fails)
assert fail_msg(_fails) == ("Expected 'foo\\nbaz' but got 'foo\\nbar'\n"
"Diff:\n"
"@@ -1,2 +1,2 @@\n"
" foo\n"
"-baz\n"
"+bar"
), repr(fail_msg(_fails))

def it_expects_not_equals(self):
expect(1) != 2
def _fails(): expect(1) != 1
Expand Down

0 comments on commit 46b987b

Please sign in to comment.