Skip to content

Commit

Permalink
Add assertNotRegex. (#289)
Browse files Browse the repository at this point in the history
Fixes #288.

Co-authored-by: Benjamin Peterson <benjamin@python.org>
  • Loading branch information
jvanasco and benjaminp committed Jan 9, 2020
1 parent 5cd83db commit 10a2b75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,11 @@ Note these functions are only available on Python 2.7 or later.
Alias for :meth:`~py3:unittest.TestCase.assertRegex` on Python 3 and
:meth:`~py2:unittest.TestCase.assertRegexpMatches` on Python 2.

.. function:: assertNotRegex()

Alias for :meth:`~py3:unittest.TestCase.assertNotRegex` on Python 3 and
:meth:`~py2:unittest.TestCase.assertNotRegexpMatches` on Python 2.


Renamed modules and attributes compatibility
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Expand Down
7 changes: 7 additions & 0 deletions six.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,11 @@ def u(s):
if sys.version_info[1] <= 1:
_assertRaisesRegex = "assertRaisesRegexp"
_assertRegex = "assertRegexpMatches"
_assertNotRegex = "assertNotRegexpMatches"
else:
_assertRaisesRegex = "assertRaisesRegex"
_assertRegex = "assertRegex"
_assertNotRegex = "assertNotRegex"
else:
def b(s):
return s
Expand All @@ -668,6 +670,7 @@ def indexbytes(buf, i):
_assertCountEqual = "assertItemsEqual"
_assertRaisesRegex = "assertRaisesRegexp"
_assertRegex = "assertRegexpMatches"
_assertNotRegex = "assertNotRegexpMatches"
_add_doc(b, """Byte literal""")
_add_doc(u, """Text literal""")

Expand All @@ -684,6 +687,10 @@ def assertRegex(self, *args, **kwargs):
return getattr(self, _assertRegex)(*args, **kwargs)


def assertNotRegex(self, *args, **kwargs):
return getattr(self, _assertNotRegex)(*args, **kwargs)


if PY3:
exec_ = getattr(moves.builtins, "exec")

Expand Down
11 changes: 11 additions & 0 deletions test_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,17 @@ def test(self):
TestAssertRegex('test').test()


def test_assertNotRegex():
class TestAssertNotRegex(unittest.TestCase):
def test(self):
with self.assertRaises(AssertionError):
six.assertNotRegex(self, 'test', r'^t')

six.assertNotRegex(self, 'test', r'^a')

TestAssertNotRegex('test').test()


def test_assertRaisesRegex():
class TestAssertRaisesRegex(unittest.TestCase):
def test(self):
Expand Down

0 comments on commit 10a2b75

Please sign in to comment.