Commit 0786c8e7 authored by wbond's avatar wbond

Re-add Python2 compatibility

parent 08ce1ca9
......@@ -27,6 +27,7 @@ if sys.version_info >= (3,):
generator_method_type = 'method'
str_cls = str
else:
from . import unittest_compat
from urlparse import urljoin
from urllib2 import HTTPError, urlopen
generator_method_type = 'instancemethod'
......
import sys
import re
import unittest
def assertIn(self, member, container, msg=None):
if sys.version_info < (2, 7):
def assertIn(self, member, container, msg=None):
"""Just like self.assertTrue(a in b), but with a nicer default message."""
if member not in container:
if not msg:
msg = '%r not found in %r' % (member, container)
self.fail(msg)
unittest.TestCase.assertIn = assertIn
unittest.TestCase.assertIn = assertIn
def assertNotIn(self, member, container, msg=None):
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)
unittest.TestCase.assertNotIn = assertNotIn
unittest.TestCase.assertNotIn = assertNotIn
def assertGreater(self, a, b, msg=None):
def assertGreater(self, a, b, msg=None):
"""Just like self.assertTrue(a > b), but with a nicer default message."""
if not a > b:
if not msg:
msg = '%s not greater than %s' % (a, b)
self.fail(msg)
unittest.TestCase.assertGreater = assertGreater
unittest.TestCase.assertGreater = assertGreater
def assertRegexpMatches(self, text, expected_regexp, msg=None):
def assertRegexpMatches(self, text, expected_regexp, msg=None):
"""Fail the test unless the text matches the regular expression."""
if isinstance(expected_regexp, basestring):
expected_regexp = re.compile(expected_regexp)
......@@ -35,9 +37,9 @@ def assertRegexpMatches(self, text, expected_regexp, msg=None):
msg = msg or "Regexp didn't match"
msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)
raise self.failureException(msg)
unittest.TestCase.assertRegex = assertRegexpMatches
unittest.TestCase.assertRegex = assertRegexpMatches
def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None):
def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None):
"""Fail the test if the text matches the regular expression."""
if isinstance(unexpected_regexp, basestring):
unexpected_regexp = re.compile(unexpected_regexp)
......@@ -49,13 +51,17 @@ def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None):
unexpected_regexp.pattern,
text)
raise self.failureException(msg)
unittest.TestCase.assertNotRegex = assertNotRegexpMatches
unittest.TestCase.assertNotRegex = assertNotRegexpMatches
def assertIsInstance(self, obj, cls, msg=None):
def assertIsInstance(self, obj, cls, msg=None):
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer
default message."""
if not isinstance(obj, cls):
if not msg:
msg = '%s is not an instance of %r' % (obj, cls)
self.fail(msg)
unittest.TestCase.assertIsInstance = assertIsInstance
unittest.TestCase.assertIsInstance = assertIsInstance
else:
unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches
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