Nicer error message for regex fail.

This commit is contained in:
Jonathan Lange
2011-07-29 17:16:43 +01:00
parent f4a528bb17
commit c6485d5258
3 changed files with 6 additions and 4 deletions

View File

@@ -730,7 +730,8 @@ class MatchesRegex(object):
def match(self, value):
if not re.match(self.pattern, value, self.flags):
return Mismatch("%r does not match %r" % (value, self.pattern))
return Mismatch("%r does not match /%s/" % (
value, self.pattern))
class MatchesSetwise(object):

View File

@@ -258,7 +258,7 @@ class TestMatchesExceptionTypeReInterface(TestCase, TestMatchersInterface):
MatchesException(Exception, 'fo.'))
]
describe_examples = [
("'bar' does not match 'fo.'",
("'bar' does not match /fo./",
error_bar, MatchesException(ValueError, "fo.")),
]
@@ -639,7 +639,8 @@ class TestMatchesRegex(TestCase, TestMatchersInterface):
]
describe_examples = [
("'c' does not match 'a|b'", 'c', MatchesRegex('a|b')),
("'c' does not match /a|b/", 'c', MatchesRegex('a|b')),
("'c' does not match /a\d/", 'c', MatchesRegex(r'a\d')),
]

View File

@@ -32,7 +32,7 @@ class TestExpectedException(TestCase):
raise ValueError('mismatch')
except AssertionError:
e = sys.exc_info()[1]
self.assertEqual("'mismatch' does not match 'tes.'", str(e))
self.assertEqual("'mismatch' does not match /tes./", str(e))
else:
self.fail('AssertionError not raised.')