Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f01b16c

Browse files
committed
Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
Patch from Ilia Kurenkov.
2 parents e4d35dc + be6caca commit f01b16c

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

Lib/unittest/case.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,10 @@ def assertRegex(self, text, expected_regex, msg=None):
12791279
assert expected_regex, "expected_regex must not be empty."
12801280
expected_regex = re.compile(expected_regex)
12811281
if not expected_regex.search(text):
1282-
msg = msg or "Regex didn't match"
1283-
msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text)
1282+
standardMsg = "Regex didn't match: %r not found in %r" % (
1283+
expected_regex.pattern, text)
1284+
# _formatMessage ensures the longMessage option is respected
1285+
msg = self._formatMessage(msg, standardMsg)
12841286
raise self.failureException(msg)
12851287

12861288
def assertNotRegex(self, text, unexpected_regex, msg=None):
@@ -1289,11 +1291,12 @@ def assertNotRegex(self, text, unexpected_regex, msg=None):
12891291
unexpected_regex = re.compile(unexpected_regex)
12901292
match = unexpected_regex.search(text)
12911293
if match:
1292-
msg = msg or "Regex matched"
1293-
msg = '%s: %r matches %r in %r' % (msg,
1294-
text[match.start():match.end()],
1295-
unexpected_regex.pattern,
1296-
text)
1294+
standardMsg = 'Regex matched: %r matches %r in %r' % (
1295+
text[match.start() : match.end()],
1296+
unexpected_regex.pattern,
1297+
text)
1298+
# _formatMessage ensures the longMessage option is respected
1299+
msg = self._formatMessage(msg, standardMsg)
12971300
raise self.failureException(msg)
12981301

12991302

@@ -1315,6 +1318,7 @@ def deprecated_func(*args, **kwargs):
13151318
failIf = _deprecate(assertFalse)
13161319
assertRaisesRegexp = _deprecate(assertRaisesRegex)
13171320
assertRegexpMatches = _deprecate(assertRegex)
1321+
assertNotRegexpMatches = _deprecate(assertNotRegex)
13181322

13191323

13201324

Lib/unittest/test/test_assertions.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ def testAssertNotRegex(self):
133133
try:
134134
self.assertNotRegex('Ala ma kota', r'k.t', 'Message')
135135
except self.failureException as e:
136-
self.assertIn("'kot'", e.args[0])
137136
self.assertIn('Message', e.args[0])
138137
else:
139138
self.fail('assertNotRegex should have failed.')
@@ -329,6 +328,20 @@ def testAssertIsNot(self):
329328
"^unexpectedly identical: None$",
330329
"^unexpectedly identical: None : oops$"])
331330

331+
def testAssertRegex(self):
332+
self.assertMessages('assertRegex', ('foo', 'bar'),
333+
["^Regex didn't match:",
334+
"^oops$",
335+
"^Regex didn't match:",
336+
"^Regex didn't match: (.*) : oops$"])
337+
338+
def testAssertNotRegex(self):
339+
self.assertMessages('assertNotRegex', ('foo', 'foo'),
340+
["^Regex matched:",
341+
"^oops$",
342+
"^Regex matched:",
343+
"^Regex matched: (.*) : oops$"])
344+
332345

333346
def assertMessagesCM(self, methodName, args, func, errors):
334347
"""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ Andrew Kuchling
786786
Dave Kuhlman
787787
Jon Kuhn
788788
Toshio Kuratomi
789+
Ilia Kurenkov
789790
Vladimir Kushnir
790791
Erno Kuusela
791792
Ross Lagerwall

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
26+
Patch from Ilia Kurenkov.
27+
2528
- Issue #24764: cgi.FieldStorage.read_multi() now ignores the Content-Length
2629
header in part headers. Patch written by Peter Landry and reviewed by Pierre
2730
Quentel.

0 commit comments

Comments
 (0)