# HG changeset patch # User Ryszard Szopa # Date 1268080936 -3600 # Node ID 566e61e661537e06864d7f678c74b2c439663049 # Parent 4e18fc44fdbc601cc19d3f9037e6ef5cfe8f95d9 TestCase.assertNotRegexpMatches. diff -r 4e18fc44fdbc -r 566e61e66153 unittest2/case.py --- a/unittest2/case.py Sun Mar 07 22:02:59 2010 +0000 +++ b/unittest2/case.py Mon Mar 08 21:42:16 2010 +0100 @@ -944,6 +944,18 @@ msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text) raise self.failureException(msg) + def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None): + if isinstance(unexpected_regexp, basestring): + unexpected_regexp = re.compile(unexpected_regexp) + match = unexpected_regexp.search(text) + if match: + msg = msg or "Regexp matched" + msg = '%s: %r matches %r in %r' % (msg, + text[match.start():match.end()], + unexpected_regexp.pattern, + text) + raise self.failureException(msg) + class FunctionTestCase(TestCase): """A test case that wraps a test function. diff -r 4e18fc44fdbc -r 566e61e66153 unittest2/test/test_unittest2.py --- a/unittest2/test/test_unittest2.py Sun Mar 07 22:02:59 2010 +0000 +++ b/unittest2/test/test_unittest2.py Mon Mar 08 21:42:16 2010 +0100 @@ -2822,6 +2822,16 @@ self.assertRaises(self.failureException, self.assertRegexpMatches, 'saaas', r'aaaa') + def testAssertNotRegexpMatches(self): + self.assertNotRegexpMatches('Ala ma kota', r'r+') + try: + self.assertNotRegexpMatches('Ala ma kota', r'k.t', 'Message') + except self.failureException, e: + self.assertIn("'kot'", e.args[0]) + self.assertIn('Message', e.args[0]) + else: + self.fail('assertNotRegexpMatches should have failed.') + def testAssertRaisesRegexp(self): class ExceptionMock(Exception): pass