Skip to content

Commit

Permalink
python#10273: Rename assertRegexpMatches and assertRaisesRegexp to as…
Browse files Browse the repository at this point in the history
…sertRegex and assertRaisesRegex.
  • Loading branch information
ezio-melotti committed Dec 1, 2010
1 parent f10c400 commit ed3a7d2
Show file tree
Hide file tree
Showing 21 changed files with 203 additions and 186 deletions.
72 changes: 40 additions & 32 deletions Doc/library/unittest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ Test cases
+-----------------------------------------+-----------------------------+---------------+

All the assert methods (except :meth:`assertRaises`,
:meth:`assertRaisesRegexp`, :meth:`assertWarns`, :meth:`assertWarnsRegexp`)
:meth:`assertRaisesRegex`, :meth:`assertWarns`, :meth:`assertWarnsRegex`)
accept a *msg* argument that, if specified, is used as the error message on
failure (see also :data:`longMessage`).

Expand Down Expand Up @@ -919,14 +919,14 @@ Test cases
| :meth:`assertRaises(exc, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises `exc` | |
| <TestCase.assertRaises>` | | |
+---------------------------------------------------------+--------------------------------------+------------+
| :meth:`assertRaisesRegexp(exc, re, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises `exc` | 3.1 |
| <TestCase.assertRaisesRegexp>` | and the message matches `re` | |
| :meth:`assertRaisesRegex(exc, re, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises `exc` | 3.1 |
| <TestCase.assertRaisesRegex>` | and the message matches `re` | |
+---------------------------------------------------------+--------------------------------------+------------+
| :meth:`assertWarns(warn, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises `warn` | 3.2 |
| <TestCase.assertWarns>` | | |
+---------------------------------------------------------+--------------------------------------+------------+
| :meth:`assertWarnsRegexp(warn, re, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises `warn` | 3.2 |
| <TestCase.assertWarnsRegexp>` | and the message matches `re` | |
| :meth:`assertWarnsRegex(warn, re, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises `warn` | 3.2 |
| <TestCase.assertWarnsRegex>` | and the message matches `re` | |
+---------------------------------------------------------+--------------------------------------+------------+

.. method:: assertRaises(exception, callable, *args, **kwds)
Expand Down Expand Up @@ -962,23 +962,25 @@ Test cases
Added the :attr:`exception` attribute.


.. method:: assertRaisesRegexp(exception, regexp, callable, *args, **kwds)
assertRaisesRegexp(exception, regexp)
.. method:: assertRaisesRegex(exception, regex, callable, *args, **kwds)
assertRaisesRegex(exception, regex)

Like :meth:`assertRaises` but also tests that *regexp* matches
on the string representation of the raised exception. *regexp* may be
Like :meth:`assertRaises` but also tests that *regex* matches
on the string representation of the raised exception. *regex* may be
a regular expression object or a string containing a regular expression
suitable for use by :func:`re.search`. Examples::

self.assertRaisesRegexp(ValueError, 'invalid literal for.*XYZ$',
int, 'XYZ')
self.assertRaisesRegex(ValueError, 'invalid literal for.*XYZ$',
int, 'XYZ')

or::

with self.assertRaisesRegexp(ValueError, 'literal'):
with self.assertRaisesRegex(ValueError, 'literal'):
int('XYZ')

.. versionadded:: 3.1
.. versionadded:: 3.1 ``assertRaisesRegexp``
.. versionchanged:: 3.2
The method has been renamed to :meth:`assertRaisesRegex`


.. method:: assertWarns(warning, callable, *args, **kwds)
Expand Down Expand Up @@ -1015,21 +1017,21 @@ Test cases
.. versionadded:: 3.2


.. method:: assertWarnsRegexp(warning, regexp, callable, *args, **kwds)
assertWarnsRegexp(warning, regexp)
.. method:: assertWarnsRegex(warning, regex, callable, *args, **kwds)
assertWarnsRegex(warning, regex)

Like :meth:`assertWarns` but also tests that *regexp* matches on the
message of the triggered warning. *regexp* may be a regular expression
Like :meth:`assertWarns` but also tests that *regex* matches on the
message of the triggered warning. *regex* may be a regular expression
object or a string containing a regular expression suitable for use
by :func:`re.search`. Example::

self.assertWarnsRegexp(DeprecationWarning,
r'legacy_function\(\) is deprecated',
legacy_function, 'XYZ')
self.assertWarnsRegex(DeprecationWarning,
r'legacy_function\(\) is deprecated',
legacy_function, 'XYZ')

or::

with self.assertWarnsRegexp(RuntimeWarning, 'unsafe frobnicating'):
with self.assertWarnsRegex(RuntimeWarning, 'unsafe frobnicating'):
frobnicate('/etc/passwd')

.. versionadded:: 3.2
Expand Down Expand Up @@ -1059,11 +1061,11 @@ Test cases
| :meth:`assertLessEqual(a, b) | ``a <= b`` | 3.1 |
| <TestCase.assertLessEqual>` | | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertRegexpMatches(s, re) | ``regex.search(s)`` | 3.1 |
| <TestCase.assertRegexpMatches>` | | |
| :meth:`assertRegex(s, re) | ``regex.search(s)`` | 3.1 |
| <TestCase.assertRegex>` | | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertNotRegexpMatches(s, re) | ``not regex.search(s)`` | 3.2 |
| <TestCase.assertNotRegexpMatches>` | | |
| :meth:`assertNotRegex(s, re) | ``not regex.search(s)`` | 3.2 |
| <TestCase.assertNotRegex>` | | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertDictContainsSubset(a, b) | all the key/value pairs | 3.1 |
| <TestCase.assertDictContainsSubset>` | in `a` exist in `b` | |
Expand Down Expand Up @@ -1108,17 +1110,19 @@ Test cases
.. versionadded:: 3.1


.. method:: assertRegexpMatches(text, regexp, msg=None)
assertNotRegexpMatches(text, regexp, msg=None)
.. method:: assertRegex(text, regex, msg=None)
assertNotRegex(text, regex, msg=None)

Test that a *regexp* search matches (or does not match) *text*. In case
Test that a *regex* search matches (or does not match) *text*. In case
of failure, the error message will include the pattern and the *text* (or
the pattern and the part of *text* that unexpectedly matched). *regexp*
the pattern and the part of *text* that unexpectedly matched). *regex*
may be a regular expression object or a string containing a regular
expression suitable for use by :func:`re.search`.

.. versionadded:: 3.1 :meth:`~TestCase.assertRegexpMatches`
.. versionadded:: 3.2 :meth:`~TestCase.assertNotRegexpMatches`
.. versionadded:: 3.1 ``.assertRegexpMatches``
.. versionchanged:: 3.2
``.assertRegexpMatches`` has been renamed to :meth:`.assertRegex`
.. versionadded:: 3.2 :meth:`.assertNotRegex`


.. method:: assertDictContainsSubset(expected, actual, msg=None)
Expand Down Expand Up @@ -1420,13 +1424,17 @@ along with their deprecated aliases:
:meth:`.assertRaises` failUnlessRaises
:meth:`.assertAlmostEqual` failUnlessAlmostEqual assertAlmostEquals
:meth:`.assertNotAlmostEqual` failIfAlmostEqual assertNotAlmostEquals
:meth:`.assertRegex` assertRegexpMatches
:meth:`.assertRaisesRegex` assertRaisesRegexp
============================== ====================== ======================

.. deprecated-removed:: 3.1 3.3
the fail* aliases listed in the second column.
.. deprecated:: 3.2
the assert* aliases listed in the third column.

.. deprecated:: 3.2
``assertRegexpMatches`` and ``assertRaisesRegexp`` have been renamed to
:meth:`.assertRegex` and :meth:`.assertRaisesRegex`


.. _testsuite-objects:
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ class C(A):
def test_register_non_class(self):
class A(metaclass=abc.ABCMeta):
pass
self.assertRaisesRegexp(TypeError, "Can only register classes",
A.register, 4)
self.assertRaisesRegex(TypeError, "Can only register classes",
A.register, 4)

def test_registration_transitiveness(self):
class A(metaclass=abc.ABCMeta):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_asyncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ def test_issue_8594(self):
d = asyncore.dispatcher(socket.socket())
# make sure the error message no longer refers to the socket
# object but the dispatcher instance instead
self.assertRaisesRegexp(AttributeError, 'dispatcher instance',
getattr, d, 'foo')
self.assertRaisesRegex(AttributeError, 'dispatcher instance',
getattr, d, 'foo')
# cheap inheritance with the underlying socket is supposed
# to still work but a DeprecationWarning is expected
with warnings.catch_warnings(record=True) as w:
Expand Down
20 changes: 10 additions & 10 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,18 +682,18 @@ def fn(callback_future):
self.assertTrue(was_cancelled)

def test_repr(self):
self.assertRegexpMatches(repr(PENDING_FUTURE),
'<Future at 0x[0-9a-f]+ state=pending>')
self.assertRegexpMatches(repr(RUNNING_FUTURE),
'<Future at 0x[0-9a-f]+ state=running>')
self.assertRegexpMatches(repr(CANCELLED_FUTURE),
'<Future at 0x[0-9a-f]+ state=cancelled>')
self.assertRegexpMatches(repr(CANCELLED_AND_NOTIFIED_FUTURE),
'<Future at 0x[0-9a-f]+ state=cancelled>')
self.assertRegexpMatches(
self.assertRegex(repr(PENDING_FUTURE),
'<Future at 0x[0-9a-f]+ state=pending>')
self.assertRegex(repr(RUNNING_FUTURE),
'<Future at 0x[0-9a-f]+ state=running>')
self.assertRegex(repr(CANCELLED_FUTURE),
'<Future at 0x[0-9a-f]+ state=cancelled>')
self.assertRegex(repr(CANCELLED_AND_NOTIFIED_FUTURE),
'<Future at 0x[0-9a-f]+ state=cancelled>')
self.assertRegex(
repr(EXCEPTION_FUTURE),
'<Future at 0x[0-9a-f]+ state=finished raised IOError>')
self.assertRegexpMatches(
self.assertRegex(
repr(SUCCESSFUL_FUTURE),
'<Future at 0x[0-9a-f]+ state=finished returned int>')

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_contextdecorator(self):
def test_contextdecorator_with_exception(self):
context = mycontext()

with self.assertRaisesRegexp(NameError, 'foo'):
with self.assertRaisesRegex(NameError, 'foo'):
with context:
raise NameError('foo')
self.assertIsNotNone(context.exc)
Expand Down Expand Up @@ -265,7 +265,7 @@ def test():
self.assertTrue(context.started)
raise NameError('foo')

with self.assertRaisesRegexp(NameError, 'foo'):
with self.assertRaisesRegex(NameError, 'foo'):
test()
self.assertIsNotNone(context.exc)
self.assertIs(context.exc[0], NameError)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,14 @@ class CodeInfoTests(unittest.TestCase):
def test_code_info(self):
self.maxDiff = 1000
for x, expected in self.test_pairs:
self.assertRegexpMatches(dis.code_info(x), expected)
self.assertRegex(dis.code_info(x), expected)

def test_show_code(self):
self.maxDiff = 1000
for x, expected in self.test_pairs:
with captured_stdout() as output:
dis.show_code(x)
self.assertRegexpMatches(output.getvalue(), expected+"\n")
self.assertRegex(output.getvalue(), expected+"\n")

def test_main():
run_unittest(DisTests, CodeInfoTests)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_memoryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class MyObject:
self.assertTrue(wr() is None, wr())

def _check_released(self, m, tp):
check = self.assertRaisesRegexp(ValueError, "released")
check = self.assertRaisesRegex(ValueError, "released")
with check: bytes(m)
with check: m.tobytes()
with check: m.tolist()
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def _check_script(self, script_name, expected_name, expected_file,

def _check_import_error(self, script_name, msg):
msg = re.escape(msg)
self.assertRaisesRegexp(ImportError, msg, run_path, script_name)
self.assertRaisesRegex(ImportError, msg, run_path, script_name)

def test_basic_script(self):
with temp_dir() as script_dir:
Expand Down Expand Up @@ -403,7 +403,7 @@ def test_main_recursion_error(self):
script_name = self._make_test_script(script_dir, mod_name, source)
zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
msg = "recursion depth exceeded"
self.assertRaisesRegexp(RuntimeError, msg, run_path, zip_name)
self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name)



Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ def testSendMessageWithAddresses(self):
self.assertEqual(self.output.getvalue(), mexpect)
debugout = smtpd.DEBUGSTREAM.getvalue()
sender = re.compile("^sender: [email protected]$", re.MULTILINE)
self.assertRegexpMatches(debugout, sender)
self.assertRegex(debugout, sender)
for addr in ('John', 'Sally', 'Fred', 'root@localhost',
'[email protected]'):
to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
re.MULTILINE)
self.assertRegexpMatches(debugout, to_addr)
self.assertRegex(debugout, to_addr)

def testSendMessageWithSomeAddresses(self):
# Make sure nothing breaks if not all of the three 'to' headers exist
Expand All @@ -347,11 +347,11 @@ def testSendMessageWithSomeAddresses(self):
self.assertEqual(self.output.getvalue(), mexpect)
debugout = smtpd.DEBUGSTREAM.getvalue()
sender = re.compile("^sender: [email protected]$", re.MULTILINE)
self.assertRegexpMatches(debugout, sender)
self.assertRegex(debugout, sender)
for addr in ('John', 'Dinsdale'):
to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
re.MULTILINE)
self.assertRegexpMatches(debugout, to_addr)
self.assertRegex(debugout, to_addr)


class NonConnectingTests(unittest.TestCase):
Expand Down
Loading

0 comments on commit ed3a7d2

Please sign in to comment.