Commit 728785c3 authored by FichteFoll's avatar FichteFoll

Replace entire TestCase class in testcase_compat

It's just cleaner, imo.
parent b1edb1c4
...@@ -6,79 +6,75 @@ from functools import wraps ...@@ -6,79 +6,75 @@ from functools import wraps
def inject_into_unittest(): def inject_into_unittest():
assert sys.version_info < (3,)
if sys.version_info < (2, 7): if sys.version_info < (2, 7):
def assertIn(self, member, container, msg=None): class PatchedTestCase(unittest.TestCase):
"""Just like self.assertTrue(a in b), but with a nicer default message.""" def assertIn(self, member, container, msg=None):
if member not in container: """Just like self.assertTrue(a in b), but with a nicer default message."""
if not msg: if member not in container:
msg = '%r not found in %r' % (member, container) if not msg:
self.fail(msg) msg = '%r not found in %r' % (member, container)
unittest.TestCase.assertIn = assertIn self.fail(msg)
def assertNotIn(self, member, container, msg=None):
"""Just like self.assertTrue(a not in b), but with a nicer default message."""
if member in container:
if not msg:
msg = '%s unexpectedly found in %s' % (member,
container)
self.fail(msg)
def assertNotIn(self, member, container, msg=None): def assertGreater(self, a, b, msg=None):
"""Just like self.assertTrue(a not in b), but with a nicer default message.""" """Just like self.assertTrue(a > b), but with a nicer default message."""
if member in container: if not a > b:
if not msg: if not msg:
msg = '%s unexpectedly found in %s' % (member, msg = '%s not greater than %s' % (a, b)
container) self.fail(msg)
self.fail(msg)
unittest.TestCase.assertNotIn = assertNotIn
def assertGreater(self, a, b, msg=None): def assertRegex(self, text, expected_regexp, msg=None):
"""Just like self.assertTrue(a > b), but with a nicer default message.""" """Fail the test unless the text matches the regular expression."""
if not a > b: if isinstance(expected_regexp, basestring):
if not msg: expected_regexp = re.compile(expected_regexp)
msg = '%s not greater than %s' % (a, b) if not expected_regexp.search(text):
self.fail(msg) msg = msg or "Regexp didn't match"
unittest.TestCase.assertGreater = assertGreater msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)
raise self.failureException(msg)
def assertRegexpMatches(self, text, expected_regexp, msg=None): def assertNotRegex(self, text, unexpected_regexp, msg=None):
"""Fail the test unless the text matches the regular expression.""" """Fail the test if the text matches the regular expression."""
if isinstance(expected_regexp, basestring): if isinstance(unexpected_regexp, basestring):
expected_regexp = re.compile(expected_regexp) unexpected_regexp = re.compile(unexpected_regexp)
if not expected_regexp.search(text): match = unexpected_regexp.search(text)
msg = msg or "Regexp didn't match" if match:
msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text) msg = msg or "Regexp matched"
raise self.failureException(msg) msg = '%s: %r matches %r in %r' % (msg,
unittest.TestCase.assertRegex = assertRegexpMatches text[match.start():match.end()],
unexpected_regexp.pattern,
text)
raise self.failureException(msg)
def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None): def assertIsInstance(self, obj, cls, msg=None):
"""Fail the test if the text matches the regular expression.""" """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
if isinstance(unexpected_regexp, basestring): default message."""
unexpected_regexp = re.compile(unexpected_regexp) if not isinstance(obj, cls):
match = unexpected_regexp.search(text) if not msg:
if match: msg = '%s is not an instance of %r' % (obj, cls)
msg = msg or "Regexp matched" self.fail(msg)
msg = '%s: %r matches %r in %r' % (msg,
text[match.start():match.end()],
unexpected_regexp.pattern,
text)
raise self.failureException(msg)
unittest.TestCase.assertNotRegex = assertNotRegexpMatches
def assertIsInstance(self, obj, cls, msg=None): # Patch in setUpClass
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer @wraps(unittest.TestCase.setUp) # no need to call, it's just `pass`
default message.""" def setUp(self):
if not isinstance(obj, cls): cls = self.__class__
if not msg: if not hasattr(self, '_class_set_up'):
msg = '%s is not an instance of %r' % (obj, cls) self._class_set_up = True
self.fail(msg) if hasattr(cls, "setUpClass"):
unittest.TestCase.assertIsInstance = assertIsInstance cls.setUpClass()
# Patch in setUpClass # TODO tearDownClass
original_setUp = unittest.TestCase.setUp
@wraps(original_setUp) unittest.TestCase = PatchedTestCase
def setUp(self):
original_setUp(self)
cls = self.__class__
if not hasattr(self, '_class_set_up'):
self._class_set_up = True
if hasattr(cls, "setUpClass"):
cls.setUpClass()
unittest.TestCase.setUp = setUp
# TODO tearDownClass elif sys.version_info < (3, 2):
else:
unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches
unittest.TestCase.assertNotRegex = unittest.TestCase.assertNotRegexpMatches unittest.TestCase.assertNotRegex = unittest.TestCase.assertNotRegexpMatches
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment