Python 3 - Fix checking of string types in unittest2 module.

This patch actually introduces a dependency from unittest2 to
six.  This should be ok since both packages are in our own
repo, and we assume a sys.path of the top-level script that
can find the third party packages.  So unittest2 should be
able to find six.

llvm-svn: 251983
This commit is contained in:
Zachary Turner 2015-11-03 21:37:42 +00:00
parent bac6e4f75b
commit 46f1784bac
5 changed files with 17 additions and 11 deletions

View File

@ -7,6 +7,8 @@ import re
import unittest
import warnings
import six
from unittest2 import result
from unittest2.util import (
safe_repr, safe_str, strclass,
@ -137,7 +139,7 @@ class _AssertRaisesContext(object):
return True
expected_regexp = self.expected_regexp
if isinstance(expected_regexp, basestring):
if isinstance(expected_regexp, six.string_types):
expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(str(exc_value)):
raise self.failureException('"%s" does not match "%s"' %
@ -156,7 +158,7 @@ class _TypeEqualityDict(object):
def __getitem__(self, key):
value = self._store[key]
if isinstance(value, basestring):
if isinstance(value, six.string_types):
return getattr(self.testcase, value)
return value
@ -940,9 +942,9 @@ class TestCase(unittest.TestCase):
def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal."""
self.assert_(isinstance(first, basestring), (
self.assert_(isinstance(first, six.string_types), (
'First argument is not a string'))
self.assert_(isinstance(second, basestring), (
self.assert_(isinstance(second, six.string_types), (
'Second argument is not a string'))
if first != second:
@ -1018,7 +1020,7 @@ class TestCase(unittest.TestCase):
try:
callable_obj(*args, **kwargs)
except expected_exception as exc_value:
if isinstance(expected_regexp, basestring):
if isinstance(expected_regexp, six.string_types):
expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(str(exc_value)):
raise self.failureException('"%s" does not match "%s"' %
@ -1033,7 +1035,7 @@ class TestCase(unittest.TestCase):
def assertRegexpMatches(self, text, expected_regexp, msg=None):
"""Fail the test unless the text matches the regular expression."""
if isinstance(expected_regexp, basestring):
if isinstance(expected_regexp, six.string_types):
expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(text):
msg = msg or "Regexp didn't match"
@ -1042,7 +1044,7 @@ class TestCase(unittest.TestCase):
def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None):
"""Fail the test if the text matches the regular expression."""
if isinstance(unexpected_regexp, basestring):
if isinstance(unexpected_regexp, six.string_types):
unexpected_regexp = re.compile(unexpected_regexp)
match = unexpected_regexp.search(text)
if match:

View File

@ -3,6 +3,7 @@
import sys
import os
import types
import six
from unittest2 import loader, runner
try:
@ -76,7 +77,7 @@ class TestProgram(object):
argv=None, testRunner=None,
testLoader=loader.defaultTestLoader, exit=True,
verbosity=1, failfast=None, catchbreak=None, buffer=None):
if isinstance(module, basestring):
if isinstance(module, six.string_types):
self.module = __import__(module)
for part in module.split('.')[1:]:
self.module = getattr(self.module, part)

View File

@ -3,6 +3,7 @@
import sys
import unittest
from unittest2 import case, util
import six
__unittest = True
@ -48,7 +49,7 @@ class BaseTestSuite(unittest.TestSuite):
self._tests.append(test)
def addTests(self, tests):
if isinstance(tests, basestring):
if isinstance(tests, six.string_types):
raise TypeError("tests must be an iterable of tests, not a string")
for test in tests:
self.addTest(test)

View File

@ -1,6 +1,7 @@
import difflib
import pprint
import re
import six
from copy import deepcopy
@ -502,7 +503,7 @@ class Test_TestCase(unittest2.TestCase, EqualityMixin, HashingMixin):
def runTest(self):
pass
self.assertIsInstance(Foo().id(), basestring)
self.assertIsInstance(Foo().id(), six.string_types)
# "If result is omitted or None, a temporary result object is created
# and used, but is not made available to the caller. As TestCase owns the

View File

@ -1,4 +1,5 @@
import unittest2
import six
from unittest2.test.support import LoggingResult
@ -124,7 +125,7 @@ class Test_FunctionTestCase(unittest2.TestCase):
def test_id(self):
test = unittest2.FunctionTestCase(lambda: None)
self.assertIsInstance(test.id(), basestring)
self.assertIsInstance(test.id(), six.string_types)
# "Returns a one-line description of the test, or None if no description
# has been provided. The default implementation of this method returns