67
72
self.assertEqual({}, mismatch.get_details())
75
class TestMismatchError(TestCase):
77
def test_is_assertion_error(self):
78
# MismatchError is an AssertionError, so that most of the time, it
79
# looks like a test failure, rather than an error.
80
def raise_mismatch_error():
81
raise MismatchError(2, Equals(3), Equals(3).match(2))
82
self.assertRaises(AssertionError, raise_mismatch_error)
84
def test_default_description_is_mismatch(self):
85
mismatch = Equals(3).match(2)
86
e = MismatchError(2, Equals(3), mismatch)
87
self.assertEqual(mismatch.describe(), str(e))
89
def test_default_description_unicode(self):
91
matcher = Equals(_u('a'))
92
mismatch = matcher.match(matchee)
93
e = MismatchError(matchee, matcher, mismatch)
94
self.assertEqual(mismatch.describe(), str(e))
96
def test_verbose_description(self):
99
mismatch = matcher.match(2)
100
e = MismatchError(matchee, matcher, mismatch, True)
102
'Match failed. Matchee: %r\n'
104
'Difference: %s\n' % (
107
matcher.match(matchee).describe(),
109
self.assertEqual(expected, str(e))
111
def test_verbose_unicode(self):
112
# When assertThat is given matchees or matchers that contain non-ASCII
113
# unicode strings, we can still provide a meaningful error.
115
matcher = Equals(_u('a'))
116
mismatch = matcher.match(matchee)
118
'Match failed. Matchee: %s\n'
120
'Difference: %s\n' % (
125
e = MismatchError(matchee, matcher, mismatch, True)
130
# Using str() should still work, and return ascii only
132
expected.replace(matchee, matchee.encode("unicode-escape")),
133
str(e).decode("ascii"))
134
self.assertEqual(expected, actual)
137
class Test_BinaryMismatch(TestCase):
138
"""Mismatches from binary comparisons need useful describe output"""
140
_long_string = "This is a longish multiline non-ascii string\n\xa7"
141
_long_b = _b(_long_string)
142
_long_u = _u(_long_string)
144
def test_short_objects(self):
145
o1, o2 = object(), object()
146
mismatch = _BinaryMismatch(o1, "!~", o2)
147
self.assertEqual(mismatch.describe(), "%r !~ %r" % (o1, o2))
149
def test_short_mixed_strings(self):
150
b, u = _b("\xa7"), _u("\xa7")
151
mismatch = _BinaryMismatch(b, "!~", u)
152
self.assertEqual(mismatch.describe(), "%r !~ %r" % (b, u))
154
def test_long_bytes(self):
155
one_line_b = self._long_b.replace(_b("\n"), _b(" "))
156
mismatch = _BinaryMismatch(one_line_b, "!~", self._long_b)
157
self.assertEqual(mismatch.describe(),
158
"%s:\nreference = %s\nactual = %s\n" % ("!~",
159
text_repr(one_line_b),
160
text_repr(self._long_b, multiline=True)))
162
def test_long_unicode(self):
163
one_line_u = self._long_u.replace("\n", " ")
164
mismatch = _BinaryMismatch(one_line_u, "!~", self._long_u)
165
self.assertEqual(mismatch.describe(),
166
"%s:\nreference = %s\nactual = %s\n" % ("!~",
167
text_repr(one_line_u),
168
text_repr(self._long_u, multiline=True)))
170
def test_long_mixed_strings(self):
171
mismatch = _BinaryMismatch(self._long_b, "!~", self._long_u)
172
self.assertEqual(mismatch.describe(),
173
"%s:\nreference = %s\nactual = %s\n" % ("!~",
174
text_repr(self._long_b, multiline=True),
175
text_repr(self._long_u, multiline=True)))
177
def test_long_bytes_and_object(self):
179
mismatch = _BinaryMismatch(self._long_b, "!~", obj)
180
self.assertEqual(mismatch.describe(),
181
"%s:\nreference = %s\nactual = %s\n" % ("!~",
182
text_repr(self._long_b, multiline=True),
185
def test_long_unicode_and_object(self):
187
mismatch = _BinaryMismatch(self._long_u, "!~", obj)
188
self.assertEqual(mismatch.describe(),
189
"%s:\nreference = %s\nactual = %s\n" % ("!~",
190
text_repr(self._long_u, multiline=True),
70
194
class TestMatchersInterface(object):
72
196
run_tests_with = FullStackRunTest
150
274
self.assertEqual("bar\n", matcher.want)
151
275
self.assertEqual(doctest.ELLIPSIS, matcher.flags)
277
def test_describe_non_ascii_bytes(self):
278
"""Even with bytestrings, the mismatch should be coercible to unicode
280
DocTestMatches is intended for text, but the Python 2 str type also
281
permits arbitrary binary inputs. This is a slightly bogus thing to do,
282
and under Python 3 using bytes objects will reasonably raise an error.
284
header = _b("\x89PNG\r\n\x1a\n...")
286
self.assertRaises(TypeError,
287
DocTestMatches, header, doctest.ELLIPSIS)
289
matcher = DocTestMatches(header, doctest.ELLIPSIS)
290
mismatch = matcher.match(_b("GIF89a\1\0\1\0\0\0\0;"))
291
# Must be treatable as unicode text, the exact output matters less
292
self.assertTrue(unicode(mismatch.describe()))
154
295
class TestEqualsInterface(TestCase, TestMatchersInterface):
712
903
("MatchesRegex('a|b')", MatchesRegex('a|b')),
713
904
("MatchesRegex('a|b', re.M)", MatchesRegex('a|b', re.M)),
714
905
("MatchesRegex('a|b', re.I|re.M)", MatchesRegex('a|b', re.I|re.M)),
906
("MatchesRegex(%r)" % (_b("\xA7"),), MatchesRegex(_b("\xA7"))),
907
("MatchesRegex(%r)" % (_u("\xA7"),), MatchesRegex(_u("\xA7"))),
717
910
describe_examples = [
718
911
("'c' does not match /a|b/", 'c', MatchesRegex('a|b')),
719
912
("'c' does not match /a\d/", 'c', MatchesRegex(r'a\d')),
913
("%r does not match /\\s+\\xa7/" % (_b('c'),),
914
_b('c'), MatchesRegex(_b("\\s+\xA7"))),
915
("%r does not match /\\s+\\xa7/" % (_u('c'),),
916
_u('c'), MatchesRegex(_u("\\s+\xA7"))),