Use MatchesRegex internally in MatchesException.

Change MatchesRegex to have a nicer error message.
This commit is contained in:
Jonathan Lange
2011-07-20 19:38:59 +01:00
parent 402ac942e8
commit 152ebc4ef8
3 changed files with 12 additions and 7 deletions

6
NEWS
View File

@@ -4,6 +4,12 @@ testtools NEWS
NEXT
~~~~
Changes
-------
* ``MatchesRegex`` mismatch now says "<value> does not match <regex>" rather
than "<regex> did not match <value>"
Improvements
------------

View File

@@ -417,6 +417,8 @@ class MatchesException(Matcher):
"""
Matcher.__init__(self)
self.expected = exception
if value_re:
value_re = MatchesRegex(value_re)
self.value_re = value_re
self._is_instance = type(self.expected) not in classtypes()
@@ -434,10 +436,7 @@ class MatchesException(Matcher):
_error_repr(other[1]), _error_repr(self.expected)))
elif self.value_re is not None:
str_exc_value = str(other[1])
if not re.match(self.value_re, str_exc_value):
return Mismatch(
'"%s" does not match "%s".'
% (str_exc_value, self.value_re))
return self.value_re.match(str_exc_value)
def __str__(self):
if self._is_instance:
@@ -729,7 +728,7 @@ class MatchesRegex(object):
def match(self, value):
if not re.match(self.pattern, value, self.flags):
return Mismatch("%r did not match %r" % (self.pattern, value))
return Mismatch("%r does not match %r" % (value, self.pattern))
class MatchesSetwise(object):

View File

@@ -257,7 +257,7 @@ class TestMatchesExceptionTypeReInterface(TestCase, TestMatchersInterface):
MatchesException(Exception))
]
describe_examples = [
('"bar" does not match "fo.".',
("'bar' does not match 'fo.'",
error_bar, MatchesException(ValueError, "fo.")),
]
@@ -618,7 +618,7 @@ class TestMatchesRegex(TestCase, TestMatchersInterface):
]
describe_examples = [
("'a|b' did not match 'c'", 'c', MatchesRegex('a|b')),
("'c' does not match 'a|b'", 'c', MatchesRegex('a|b')),
]