Kaydet (Commit) be6caca5 authored tarafından Robert Collins's avatar Robert Collins

Issue #20362: Honour TestCase.longMessage correctly in assertRegex.

Patch from Ilia Kurenkov.
üst 80740492
...@@ -1279,8 +1279,10 @@ class TestCase(object): ...@@ -1279,8 +1279,10 @@ class TestCase(object):
assert expected_regex, "expected_regex must not be empty." assert expected_regex, "expected_regex must not be empty."
expected_regex = re.compile(expected_regex) expected_regex = re.compile(expected_regex)
if not expected_regex.search(text): if not expected_regex.search(text):
msg = msg or "Regex didn't match" standardMsg = "Regex didn't match: %r not found in %r" % (
msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text) expected_regex.pattern, text)
# _formatMessage ensures the longMessage option is respected
msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg) raise self.failureException(msg)
def assertNotRegex(self, text, unexpected_regex, msg=None): def assertNotRegex(self, text, unexpected_regex, msg=None):
...@@ -1289,11 +1291,12 @@ class TestCase(object): ...@@ -1289,11 +1291,12 @@ class TestCase(object):
unexpected_regex = re.compile(unexpected_regex) unexpected_regex = re.compile(unexpected_regex)
match = unexpected_regex.search(text) match = unexpected_regex.search(text)
if match: if match:
msg = msg or "Regex matched" standardMsg = 'Regex matched: %r matches %r in %r' % (
msg = '%s: %r matches %r in %r' % (msg, text[match.start() : match.end()],
text[match.start():match.end()],
unexpected_regex.pattern, unexpected_regex.pattern,
text) text)
# _formatMessage ensures the longMessage option is respected
msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg) raise self.failureException(msg)
...@@ -1315,6 +1318,7 @@ class TestCase(object): ...@@ -1315,6 +1318,7 @@ class TestCase(object):
failIf = _deprecate(assertFalse) failIf = _deprecate(assertFalse)
assertRaisesRegexp = _deprecate(assertRaisesRegex) assertRaisesRegexp = _deprecate(assertRaisesRegex)
assertRegexpMatches = _deprecate(assertRegex) assertRegexpMatches = _deprecate(assertRegex)
assertNotRegexpMatches = _deprecate(assertNotRegex)
......
...@@ -133,7 +133,6 @@ class Test_Assertions(unittest.TestCase): ...@@ -133,7 +133,6 @@ class Test_Assertions(unittest.TestCase):
try: try:
self.assertNotRegex('Ala ma kota', r'k.t', 'Message') self.assertNotRegex('Ala ma kota', r'k.t', 'Message')
except self.failureException as e: except self.failureException as e:
self.assertIn("'kot'", e.args[0])
self.assertIn('Message', e.args[0]) self.assertIn('Message', e.args[0])
else: else:
self.fail('assertNotRegex should have failed.') self.fail('assertNotRegex should have failed.')
...@@ -329,6 +328,20 @@ class TestLongMessage(unittest.TestCase): ...@@ -329,6 +328,20 @@ class TestLongMessage(unittest.TestCase):
"^unexpectedly identical: None$", "^unexpectedly identical: None$",
"^unexpectedly identical: None : oops$"]) "^unexpectedly identical: None : oops$"])
def testAssertRegex(self):
self.assertMessages('assertRegex', ('foo', 'bar'),
["^Regex didn't match:",
"^oops$",
"^Regex didn't match:",
"^Regex didn't match: (.*) : oops$"])
def testAssertNotRegex(self):
self.assertMessages('assertNotRegex', ('foo', 'foo'),
["^Regex matched:",
"^oops$",
"^Regex matched:",
"^Regex matched: (.*) : oops$"])
def assertMessagesCM(self, methodName, args, func, errors): def assertMessagesCM(self, methodName, args, func, errors):
""" """
......
...@@ -786,6 +786,7 @@ Andrew Kuchling ...@@ -786,6 +786,7 @@ Andrew Kuchling
Dave Kuhlman Dave Kuhlman
Jon Kuhn Jon Kuhn
Toshio Kuratomi Toshio Kuratomi
Ilia Kurenkov
Vladimir Kushnir Vladimir Kushnir
Erno Kuusela Erno Kuusela
Ross Lagerwall Ross Lagerwall
......
...@@ -17,6 +17,9 @@ Core and Builtins ...@@ -17,6 +17,9 @@ Core and Builtins
Library Library
------- -------
- Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
Patch from Ilia Kurenkov.
- Issue #24847: Removes vcruntime140.dll dependency from Tcl/Tk. - Issue #24847: Removes vcruntime140.dll dependency from Tcl/Tk.
- Issue #23572: Fixed functools.singledispatch on classes with falsy - Issue #23572: Fixed functools.singledispatch on classes with falsy
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment