summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2015-08-19 23:13:09 (GMT)
committerRobert Collins <rbtcollins@hp.com>2015-08-19 23:13:09 (GMT)
commitbe6caca534a6d35ccae6fe13c15eead76b721e1d (patch)
tree2ef2a5e86e2ae980d886192f2cd6eac20e911d62
parent807404921a17ae9232f028377dc186025b0fed1d (diff)
downloadcpython-be6caca534a6d35ccae6fe13c15eead76b721e1d.zip
cpython-be6caca534a6d35ccae6fe13c15eead76b721e1d.tar.gz
cpython-be6caca534a6d35ccae6fe13c15eead76b721e1d.tar.bz2
Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
Patch from Ilia Kurenkov.
-rw-r--r--Lib/unittest/case.py18
-rw-r--r--Lib/unittest/test/test_assertions.py15
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
4 files changed, 29 insertions, 8 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 7701ad3..a4a0a1f 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -1279,8 +1279,10 @@ class TestCase(object):
assert expected_regex, "expected_regex must not be empty."
expected_regex = re.compile(expected_regex)
if not expected_regex.search(text):
- msg = msg or "Regex didn't match"
- msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text)
+ standardMsg = "Regex didn't match: %r not found in %r" % (
+ expected_regex.pattern, text)
+ # _formatMessage ensures the longMessage option is respected
+ msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg)
def assertNotRegex(self, text, unexpected_regex, msg=None):
@@ -1289,11 +1291,12 @@ class TestCase(object):
unexpected_regex = re.compile(unexpected_regex)
match = unexpected_regex.search(text)
if match:
- msg = msg or "Regex matched"
- msg = '%s: %r matches %r in %r' % (msg,
- text[match.start():match.end()],
- unexpected_regex.pattern,
- text)
+ standardMsg = 'Regex matched: %r matches %r in %r' % (
+ text[match.start() : match.end()],
+ unexpected_regex.pattern,
+ text)
+ # _formatMessage ensures the longMessage option is respected
+ msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg)
@@ -1315,6 +1318,7 @@ class TestCase(object):
failIf = _deprecate(assertFalse)
assertRaisesRegexp = _deprecate(assertRaisesRegex)
assertRegexpMatches = _deprecate(assertRegex)
+ assertNotRegexpMatches = _deprecate(assertNotRegex)
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py
index c349a95..e6e2bc2 100644
--- a/Lib/unittest/test/test_assertions.py
+++ b/Lib/unittest/test/test_assertions.py
@@ -133,7 +133,6 @@ class Test_Assertions(unittest.TestCase):
try:
self.assertNotRegex('Ala ma kota', r'k.t', 'Message')
except self.failureException as e:
- self.assertIn("'kot'", e.args[0])
self.assertIn('Message', e.args[0])
else:
self.fail('assertNotRegex should have failed.')
@@ -329,6 +328,20 @@ class TestLongMessage(unittest.TestCase):
"^unexpectedly identical: None$",
"^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):
"""
diff --git a/Misc/ACKS b/Misc/ACKS
index 59828fb..7418d02 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -786,6 +786,7 @@ Andrew Kuchling
Dave Kuhlman
Jon Kuhn
Toshio Kuratomi
+Ilia Kurenkov
Vladimir Kushnir
Erno Kuusela
Ross Lagerwall
diff --git a/Misc/NEWS b/Misc/NEWS
index 825bd15..5035e47 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Core and Builtins
Library
-------
+- Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
+ Patch from Ilia Kurenkov.
+
- Issue #24847: Removes vcruntime140.dll dependency from Tcl/Tk.
- Issue #23572: Fixed functools.singledispatch on classes with falsy