]> granicus.if.org Git - python/commitdiff
Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
authorRobert Collins <rbtcollins@hp.com>
Wed, 19 Aug 2015 23:13:09 +0000 (11:13 +1200)
committerRobert Collins <rbtcollins@hp.com>
Wed, 19 Aug 2015 23:13:09 +0000 (11:13 +1200)
Patch from Ilia Kurenkov.

Lib/unittest/case.py
Lib/unittest/test/test_assertions.py
Misc/ACKS
Misc/NEWS

index 7701ad3adcc22612f0c74e86b5550d83f584262f..a4a0a1f31e6df9219a7c2d1c10ebc3ec978bb3f8 100644 (file)
@@ -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)
 
 
 
index c349a95794f76a95b508db3967c07b1973214799..e6e2bc2ca72aebb885763ffe62d004d47c902a56 100644 (file)
@@ -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):
         """
index 59828fb0ff224a57943a0bb1c7e976b6d434ac48..7418d02b1d3b35762b4133b519cf05ebe23cbceb 100644 (file)
--- 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
index 825bd156d7d07c0f92636cfeee9b1e9413eb6249..5035e4738ef4083d88dafc9df59e0703143cfdb7 100644 (file)
--- 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