10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Test case implementation"""
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport collections
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport functools
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport difflib
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport pprint
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport re
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport types
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport warnings
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom . import result
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom .util import (
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    strclass, safe_repr, unorderable_list_difference,
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _count_diff_all_purpose, _count_diff_hashable
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao)
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__unittest = True
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
220a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDIFF_OMITTED = ('\nDiff is %s characters long. '
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 'Set self.maxDiff to None to see it.')
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SkipTest(Exception):
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Raise this exception in a test to skip it.
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Usually you can use TestCase.skipTest() or one of the skipping decorators
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    instead of raising this directly.
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _ExpectedFailure(Exception):
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Raise this when a test is expected to fail.
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    This is an implementation detail.
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, exc_info):
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        super(_ExpectedFailure, self).__init__()
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exc_info = exc_info
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _UnexpectedSuccess(Exception):
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    The test was supposed to fail, but it didn't!
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _id(obj):
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return obj
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef skip(reason):
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Unconditionally skip a test.
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def decorator(test_item):
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not isinstance(test_item, (type, types.ClassType)):
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            @functools.wraps(test_item)
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def skip_wrapper(*args, **kwargs):
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise SkipTest(reason)
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            test_item = skip_wrapper
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test_item.__unittest_skip__ = True
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test_item.__unittest_skip_why__ = reason
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return test_item
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return decorator
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef skipIf(condition, reason):
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Skip a test if the condition is true.
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if condition:
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return skip(reason)
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _id
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef skipUnless(condition, reason):
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Skip a test unless the condition is true.
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if not condition:
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return skip(reason)
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _id
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef expectedFailure(func):
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @functools.wraps(func)
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def wrapper(*args, **kwargs):
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            func(*args, **kwargs)
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except Exception:
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise _ExpectedFailure(sys.exc_info())
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise _UnexpectedSuccess
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return wrapper
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _AssertRaisesContext(object):
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """A context manager used to implement TestCase.assertRaises* methods."""
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, expected, test_case, expected_regexp=None):
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.expected = expected
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.failureException = test_case.failureException
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.expected_regexp = expected_regexp
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __enter__(self):
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __exit__(self, exc_type, exc_value, tb):
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if exc_type is None:
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                exc_name = self.expected.__name__
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except AttributeError:
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                exc_name = str(self.expected)
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException(
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "{0} not raised".format(exc_name))
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not issubclass(exc_type, self.expected):
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # let unexpected exceptions pass through
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return False
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exception = exc_value # store for later retrieval
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.expected_regexp is None:
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return True
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        expected_regexp = self.expected_regexp
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(expected_regexp, basestring):
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected_regexp = re.compile(expected_regexp)
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not expected_regexp.search(str(exc_value)):
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException('"%s" does not match "%s"' %
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     (expected_regexp.pattern, str(exc_value)))
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return True
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCase(object):
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """A class whose instances are single test cases.
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    By default, the test code itself should be placed in a method named
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    'runTest'.
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    If the fixture may be used for many test cases, create as
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    many test methods as are needed. When instantiating such a TestCase
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    subclass, specify in the constructor arguments the name of the test method
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    that the instance is to execute.
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Test authors should subclass TestCase for their own tests. Construction
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    and deconstruction of the test's environment ('fixture') can be
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    implemented by overriding the 'setUp' and 'tearDown' methods respectively.
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    If it is necessary to override the __init__ method, the base class
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    __init__ method must always be called. It is important that subclasses
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    should not change the signature of their __init__ method, since instances
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    of the classes are instantiated automatically by parts of the framework
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    in order to be run.
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    When subclassing TestCase, you can set these attributes:
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    * failureException: determines which exception will be raised when
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the instance's assertion methods fail; test methods raising this
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        exception will be deemed to have 'failed' rather than 'errored'.
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    * longMessage: determines whether long messages (including repr of
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        objects used in assert methods) will be printed on failure in *addition*
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        to any explicit message passed.
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    * maxDiff: sets the maximum length of a diff in failure messages
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        by assert methods using difflib. It is looked up as an instance
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        attribute so can be configured by individual tests if required.
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failureException = AssertionError
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    longMessage = False
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    maxDiff = 80*8
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # If a string is longer than _diffThreshold, use normal comparison instead
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # of difflib.  See #11763.
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _diffThreshold = 2**16
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Attribute used by TestSuite for classSetUp
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _classSetupFailed = False
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, methodName='runTest'):
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Create an instance of the class that will use the named test
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           method when executed. Raises a ValueError if the instance does
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           not have a method with the specified name.
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._testMethodName = methodName
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._resultForDoCleanups = None
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            testMethod = getattr(self, methodName)
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError:
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise ValueError("no such test method in %s: %s" %
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  (self.__class__, methodName))
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._testMethodDoc = testMethod.__doc__
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._cleanups = []
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Map types to custom assertEqual functions that will compare
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # instances of said type in more detail to generate a more useful
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # error message.
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._type_equality_funcs = {}
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.addTypeEqualityFunc(dict, 'assertDictEqual')
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.addTypeEqualityFunc(list, 'assertListEqual')
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.addTypeEqualityFunc(tuple, 'assertTupleEqual')
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.addTypeEqualityFunc(set, 'assertSetEqual')
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.addTypeEqualityFunc(frozenset, 'assertSetEqual')
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.addTypeEqualityFunc(unicode, 'assertMultiLineEqual')
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except NameError:
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # No unicode support in this build
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def addTypeEqualityFunc(self, typeobj, function):
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Add a type specific assertEqual style function to compare a type.
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        This method is for use by TestCase subclasses that need to register
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        their own type equality functions to provide nicer error messages.
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Args:
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            typeobj: The data type to call this function on when both values
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    are of the same type in assertEqual().
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            function: The callable taking two arguments and an optional
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    msg= argument that raises self.failureException with a
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    useful error message when the two arguments are not equal.
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._type_equality_funcs[typeobj] = function
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def addCleanup(self, function, *args, **kwargs):
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Add a function, with arguments, to be called when the test is
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        completed. Functions added are called on a LIFO basis and are
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        called after tearDown on test failure or success.
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Cleanup items are called even if setUp fails (unlike tearDown)."""
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._cleanups.append((function, args, kwargs))
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUp(self):
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "Hook method for setting up the test fixture before exercising it."
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pass
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "Hook method for deconstructing the test fixture after testing it."
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pass
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @classmethod
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUpClass(cls):
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "Hook method for setting up class fixture before running tests in the class."
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @classmethod
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDownClass(cls):
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "Hook method for deconstructing the class fixture after running all tests in the class."
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def countTestCases(self):
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return 1
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def defaultTestResult(self):
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return result.TestResult()
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def shortDescription(self):
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Returns a one-line description of the test, or None if no
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        description has been provided.
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        The default implementation of this method returns the first line of
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the specified test method's docstring.
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        doc = self._testMethodDoc
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return doc and doc.split("\n")[0].strip() or None
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def id(self):
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "%s.%s" % (strclass(self.__class__), self._testMethodName)
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __eq__(self, other):
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if type(self) is not type(other):
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return NotImplemented
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._testMethodName == other._testMethodName
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __ne__(self, other):
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return not self == other
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __hash__(self):
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return hash((type(self), self._testMethodName))
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __str__(self):
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "%s (%s)" % (self._testMethodName, strclass(self.__class__))
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __repr__(self):
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "<%s testMethod=%s>" % \
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               (strclass(self.__class__), self._testMethodName)
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _addSkip(self, result, reason):
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        addSkip = getattr(result, 'addSkip', None)
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if addSkip is not None:
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            addSkip(self, reason)
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            warnings.warn("TestResult has no addSkip method, skips not reported",
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          RuntimeWarning, 2)
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            result.addSuccess(self)
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def run(self, result=None):
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        orig_result = result
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if result is None:
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            result = self.defaultTestResult()
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            startTestRun = getattr(result, 'startTestRun', None)
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if startTestRun is not None:
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                startTestRun()
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._resultForDoCleanups = result
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result.startTest(self)
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        testMethod = getattr(self, self._testMethodName)
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if (getattr(self.__class__, "__unittest_skip__", False) or
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            getattr(testMethod, "__unittest_skip__", False)):
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # If the class or method was skipped.
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            or getattr(testMethod, '__unittest_skip_why__', ''))
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._addSkip(result, skip_why)
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                result.stopTest(self)
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            success = False
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.setUp()
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except SkipTest as e:
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._addSkip(result, str(e))
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except KeyboardInterrupt:
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except:
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                result.addError(self, sys.exc_info())
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    testMethod()
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except KeyboardInterrupt:
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    raise
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except self.failureException:
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    result.addFailure(self, sys.exc_info())
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except _ExpectedFailure as e:
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    addExpectedFailure = getattr(result, 'addExpectedFailure', None)
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if addExpectedFailure is not None:
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        addExpectedFailure(self, e.exc_info)
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    else:
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        warnings.warn("TestResult has no addExpectedFailure method, reporting as passes",
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                      RuntimeWarning)
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        result.addSuccess(self)
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except _UnexpectedSuccess:
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    addUnexpectedSuccess = getattr(result, 'addUnexpectedSuccess', None)
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if addUnexpectedSuccess is not None:
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        addUnexpectedSuccess(self)
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    else:
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        warnings.warn("TestResult has no addUnexpectedSuccess method, reporting as failures",
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                      RuntimeWarning)
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        result.addFailure(self, sys.exc_info())
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except SkipTest as e:
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self._addSkip(result, str(e))
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except:
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    result.addError(self, sys.exc_info())
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    success = True
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.tearDown()
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except KeyboardInterrupt:
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    raise
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except:
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    result.addError(self, sys.exc_info())
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    success = False
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cleanUpSuccess = self.doCleanups()
3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            success = success and cleanUpSuccess
3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if success:
3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                result.addSuccess(self)
3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            result.stopTest(self)
3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if orig_result is None:
3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                stopTestRun = getattr(result, 'stopTestRun', None)
3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if stopTestRun is not None:
3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    stopTestRun()
3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def doCleanups(self):
3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Execute all cleanup functions. Normally called for you after
3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tearDown."""
3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = self._resultForDoCleanups
3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ok = True
3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while self._cleanups:
3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            function, args, kwargs = self._cleanups.pop(-1)
3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                function(*args, **kwargs)
3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except KeyboardInterrupt:
3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise
3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except:
3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                ok = False
3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                result.addError(self, sys.exc_info())
3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return ok
3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __call__(self, *args, **kwds):
3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.run(*args, **kwds)
3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def debug(self):
3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Run the test without collecting errors in a TestResult"""
3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.setUp()
4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        getattr(self, self._testMethodName)()
4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.tearDown()
4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while self._cleanups:
4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            function, args, kwargs = self._cleanups.pop(-1)
4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            function(*args, **kwargs)
4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def skipTest(self, reason):
4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Skip this test."""
4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise SkipTest(reason)
4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def fail(self, msg=None):
4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail immediately, with the given message."""
4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise self.failureException(msg)
4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertFalse(self, expr, msg=None):
4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Check that the expression is false."""
4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if expr:
4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = self._formatMessage(msg, "%s is not false" % safe_repr(expr))
4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException(msg)
4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertTrue(self, expr, msg=None):
4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Check that the expression is true."""
4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not expr:
4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException(msg)
4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _formatMessage(self, msg, standardMsg):
4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Honour the longMessage attribute when generating failure messages.
4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If longMessage is False this means:
4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        * Use only an explicit message if it is provided
4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        * Otherwise use the standard message for the assert
4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If longMessage is True:
4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        * Use the standard message
4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        * If an explicit message is provided, plus ' : ' and the explicit message
4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not self.longMessage:
4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return msg or standardMsg
4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if msg is None:
4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return standardMsg
4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # don't switch to '{}' formatting in Python 2.X
4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # it changes the way unicode input is handled
4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return '%s : %s' % (standardMsg, msg)
4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except UnicodeDecodeError:
4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return  '%s : %s' % (safe_repr(standardMsg), safe_repr(msg))
4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail unless an exception of class excClass is raised
4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           by callableObj when invoked with arguments args and keyword
4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           arguments kwargs. If a different type of exception is
4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           raised, it will not be caught, and the test case will be
4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           deemed to have suffered an error, exactly as for an
4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           unexpected exception.
4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           If called with callableObj omitted or None, will return a
4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           context object used like this::
4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with self.assertRaises(SomeException):
4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    do_something()
4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           The context manager keeps a reference to the exception as
4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           the 'exception' attribute. This allows you to inspect the
4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           exception after the assertion::
4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               with self.assertRaises(SomeException) as cm:
4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   do_something()
4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               the_exception = cm.exception
4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               self.assertEqual(the_exception.error_code, 3)
4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        context = _AssertRaisesContext(excClass, self)
4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if callableObj is None:
4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return context
4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with context:
4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            callableObj(*args, **kwargs)
4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _getAssertEqualityFunc(self, first, second):
4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Get a detailed comparison function for the types of the two args.
4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Returns: A callable accepting (first, second, msg=None) that will
4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise a failure exception if first != second with a useful human
4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        readable error message for those types.
4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #
4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # NOTE(gregory.p.smith): I considered isinstance(first, type(second))
4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # and vice versa.  I opted for the conservative approach in case
4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # subclasses are not intended to be compared in detail to their super
4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # class instances using a type equality func.  This means testing
4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # subtypes won't automagically use the detailed comparison.  Callers
4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # should use their type specific assertSpamEqual method to compare
4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # subclasses if the detailed comparison is desired and appropriate.
4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # See the discussion in http://bugs.python.org/issue2578.
4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #
4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if type(first) is type(second):
4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            asserter = self._type_equality_funcs.get(type(first))
4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if asserter is not None:
4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if isinstance(asserter, basestring):
4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    asserter = getattr(self, asserter)
4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return asserter
5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._baseAssertEqual
5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _baseAssertEqual(self, first, second, msg=None):
5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """The default assertEqual implementation, not type specific."""
5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not first == second:
5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s != %s' % (safe_repr(first), safe_repr(second))
5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = self._formatMessage(msg, standardMsg)
5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException(msg)
5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertEqual(self, first, second, msg=None):
5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail if the two objects are unequal as determined by the '=='
5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           operator.
5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        assertion_func = self._getAssertEqualityFunc(first, second)
5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        assertion_func(first, second, msg=msg)
5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertNotEqual(self, first, second, msg=None):
5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail if the two objects are equal as determined by the '!='
5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           operator.
5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not first != second:
5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = self._formatMessage(msg, '%s == %s' % (safe_repr(first),
5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                          safe_repr(second)))
5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException(msg)
5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertAlmostEqual(self, first, second, places=None, msg=None, delta=None):
5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail if the two objects are unequal as determined by their
5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           difference rounded to the given number of decimal places
5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           (default 7) and comparing to zero, or by comparing that the
5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           between the two objects is more than the given delta.
5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           Note that decimal places (from zero) are usually not the same
5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           as significant digits (measured from the most signficant digit).
5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           If the two objects compare equal then they will automatically
5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           compare almost equal.
5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if first == second:
5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # shortcut
5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if delta is not None and places is not None:
5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TypeError("specify delta or places not both")
5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if delta is not None:
5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if abs(first - second) <= delta:
5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s != %s within %s delta' % (safe_repr(first),
5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                        safe_repr(second),
5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                        safe_repr(delta))
5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if places is None:
5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                places = 7
5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if round(abs(second-first), places) == 0:
5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s != %s within %r places' % (safe_repr(first),
5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                          safe_repr(second),
5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                          places)
5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        msg = self._formatMessage(msg, standardMsg)
5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise self.failureException(msg)
5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertNotAlmostEqual(self, first, second, places=None, msg=None, delta=None):
5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail if the two objects are equal as determined by their
5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           difference rounded to the given number of decimal places
5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           (default 7) and comparing to zero, or by comparing that the
5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           between the two objects is less than the given delta.
5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           Note that decimal places (from zero) are usually not the same
5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           as significant digits (measured from the most signficant digit).
5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           Objects that are equal automatically fail.
5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if delta is not None and places is not None:
5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TypeError("specify delta or places not both")
5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if delta is not None:
5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not (first == second) and abs(first - second) > delta:
5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s == %s within %s delta' % (safe_repr(first),
5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                        safe_repr(second),
5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                        safe_repr(delta))
5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if places is None:
5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                places = 7
5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not (first == second) and round(abs(second-first), places) != 0:
5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s == %s within %r places' % (safe_repr(first),
5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                         safe_repr(second),
5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                         places)
5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        msg = self._formatMessage(msg, standardMsg)
5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise self.failureException(msg)
5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Synonyms for assertion methods
5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # The plurals are undocumented.  Keep them that way to discourage use.
5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Do not add more.  Do not remove.
6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Going through a deprecation cycle on these would annoy many people.
6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    assertEquals = assertEqual
6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    assertNotEquals = assertNotEqual
6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    assertAlmostEquals = assertAlmostEqual
6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    assertNotAlmostEquals = assertNotAlmostEqual
6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    assert_ = assertTrue
6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # These fail* assertion method names are pending deprecation and will
6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # be a DeprecationWarning in 3.2; http://bugs.python.org/issue2578
6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _deprecate(original_func):
6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def deprecated_func(*args, **kwargs):
6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            warnings.warn(
6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                'Please use {0} instead.'.format(original_func.__name__),
6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                PendingDeprecationWarning, 2)
6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return original_func(*args, **kwargs)
6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return deprecated_func
6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failUnlessEqual = _deprecate(assertEqual)
6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failIfEqual = _deprecate(assertNotEqual)
6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failUnlessAlmostEqual = _deprecate(assertAlmostEqual)
6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failIfAlmostEqual = _deprecate(assertNotAlmostEqual)
6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failUnless = _deprecate(assertTrue)
6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failUnlessRaises = _deprecate(assertRaises)
6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    failIf = _deprecate(assertFalse)
6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """An equality assertion for ordered sequences (like lists and tuples).
6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        For the purposes of this function, a valid ordered sequence type is one
6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        which can be indexed, has a length, and has an equality operator.
6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Args:
6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            seq1: The first sequence to compare.
6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            seq2: The second sequence to compare.
6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            seq_type: The expected datatype of the sequences, or None if no
6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    datatype should be enforced.
6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg: Optional message to use on failure instead of a list of
6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differences.
6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if seq_type is not None:
6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            seq_type_name = seq_type.__name__
6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not isinstance(seq1, seq_type):
6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise self.failureException('First sequence is not a %s: %s'
6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                        % (seq_type_name, safe_repr(seq1)))
6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not isinstance(seq2, seq_type):
6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise self.failureException('Second sequence is not a %s: %s'
6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                        % (seq_type_name, safe_repr(seq2)))
6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            seq_type_name = "sequence"
6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        differing = None
6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            len1 = len(seq1)
6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except (TypeError, NotImplementedError):
6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            differing = 'First %s has no length.    Non-sequence?' % (
6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    seq_type_name)
6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if differing is None:
6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                len2 = len(seq2)
6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except (TypeError, NotImplementedError):
6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                differing = 'Second %s has no length.    Non-sequence?' % (
6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        seq_type_name)
6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if differing is None:
6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if seq1 == seq2:
6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            seq1_repr = safe_repr(seq1)
6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            seq2_repr = safe_repr(seq2)
6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if len(seq1_repr) > 30:
6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                seq1_repr = seq1_repr[:30] + '...'
6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if len(seq2_repr) > 30:
6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                seq2_repr = seq2_repr[:30] + '...'
6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elements = (seq_type_name.capitalize(), seq1_repr, seq2_repr)
6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            differing = '%ss differ: %s != %s\n' % elements
6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for i in xrange(min(len1, len2)):
6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    item1 = seq1[i]
6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except (TypeError, IndexError, NotImplementedError):
6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differing += ('\nUnable to index element %d of first %s\n' %
6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 (i, seq_type_name))
6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    break
6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    item2 = seq2[i]
6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except (TypeError, IndexError, NotImplementedError):
6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differing += ('\nUnable to index element %d of second %s\n' %
6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 (i, seq_type_name))
6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    break
6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if item1 != item2:
6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differing += ('\nFirst differing element %d:\n%s\n%s\n' %
6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 (i, item1, item2))
6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    break
6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if (len1 == len2 and seq_type is None and
6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    type(seq1) != type(seq2)):
6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # The sequences are the same, but have differing types.
7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    return
7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if len1 > len2:
7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                differing += ('\nFirst %s contains %d additional '
7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             'elements.\n' % (seq_type_name, len1 - len2))
7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differing += ('First extra element %d:\n%s\n' %
7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                  (len2, seq1[len2]))
7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except (TypeError, IndexError, NotImplementedError):
7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differing += ('Unable to index element %d '
7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                  'of first %s\n' % (len2, seq_type_name))
7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif len1 < len2:
7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                differing += ('\nSecond %s contains %d additional '
7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             'elements.\n' % (seq_type_name, len2 - len1))
7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differing += ('First extra element %d:\n%s\n' %
7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                  (len1, seq2[len1]))
7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except (TypeError, IndexError, NotImplementedError):
7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differing += ('Unable to index element %d '
7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                  'of second %s\n' % (len1, seq_type_name))
7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        standardMsg = differing
7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        diffMsg = '\n' + '\n'.join(
7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            difflib.ndiff(pprint.pformat(seq1).splitlines(),
7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          pprint.pformat(seq2).splitlines()))
7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        standardMsg = self._truncateMessage(standardMsg, diffMsg)
7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        msg = self._formatMessage(msg, standardMsg)
7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fail(msg)
7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _truncateMessage(self, message, diff):
7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        max_diff = self.maxDiff
7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if max_diff is None or len(diff) <= max_diff:
7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return message + diff
7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return message + (DIFF_OMITTED % len(diff))
7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertListEqual(self, list1, list2, msg=None):
7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """A list-specific equality assertion.
7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Args:
7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            list1: The first list to compare.
7390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            list2: The second list to compare.
7400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg: Optional message to use on failure instead of a list of
7410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differences.
7420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertSequenceEqual(list1, list2, msg, seq_type=list)
7450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertTupleEqual(self, tuple1, tuple2, msg=None):
7470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """A tuple-specific equality assertion.
7480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Args:
7500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            tuple1: The first tuple to compare.
7510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            tuple2: The second tuple to compare.
7520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg: Optional message to use on failure instead of a list of
7530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differences.
7540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
7560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertSetEqual(self, set1, set2, msg=None):
7580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """A set-specific equality assertion.
7590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Args:
7610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            set1: The first set to compare.
7620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            set2: The second set to compare.
7630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg: Optional message to use on failure instead of a list of
7640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    differences.
7650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        assertSetEqual uses ducktyping to support different types of sets, and
7670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        is optimized for sets specifically (parameters must support a
7680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        difference method).
7690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
7710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            difference1 = set1.difference(set2)
7720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except TypeError, e:
7730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail('invalid type when attempting set difference: %s' % e)
7740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError, e:
7750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail('first argument does not support set difference: %s' % e)
7760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
7780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            difference2 = set2.difference(set1)
7790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except TypeError, e:
7800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail('invalid type when attempting set difference: %s' % e)
7810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError, e:
7820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail('second argument does not support set difference: %s' % e)
7830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not (difference1 or difference2):
7850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
7860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lines = []
7880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if difference1:
7890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            lines.append('Items in the first set but not the second:')
7900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for item in difference1:
7910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                lines.append(repr(item))
7920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if difference2:
7930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            lines.append('Items in the second set but not the first:')
7940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for item in difference2:
7950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                lines.append(repr(item))
7960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        standardMsg = '\n'.join(lines)
7980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fail(self._formatMessage(msg, standardMsg))
7990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertIn(self, member, container, msg=None):
8010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a in b), but with a nicer default message."""
8020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if member not in container:
8030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s not found in %s' % (safe_repr(member),
8040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                  safe_repr(container))
8050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
8060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertNotIn(self, member, container, msg=None):
8080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a not in b), but with a nicer default message."""
8090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if member in container:
8100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s unexpectedly found in %s' % (safe_repr(member),
8110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                        safe_repr(container))
8120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
8130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertIs(self, expr1, expr2, msg=None):
8150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a is b), but with a nicer default message."""
8160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if expr1 is not expr2:
8170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s is not %s' % (safe_repr(expr1),
8180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                             safe_repr(expr2))
8190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
8200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertIsNot(self, expr1, expr2, msg=None):
8220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a is not b), but with a nicer default message."""
8230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if expr1 is expr2:
8240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = 'unexpectedly identical: %s' % (safe_repr(expr1),)
8250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
8260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertDictEqual(self, d1, d2, msg=None):
8280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(d1, dict, 'First argument is not a dictionary')
8290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')
8300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if d1 != d2:
8320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s != %s' % (safe_repr(d1, True), safe_repr(d2, True))
8330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            diff = ('\n' + '\n'.join(difflib.ndiff(
8340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           pprint.pformat(d1).splitlines(),
8350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           pprint.pformat(d2).splitlines())))
8360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = self._truncateMessage(standardMsg, diff)
8370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
8380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertDictContainsSubset(self, expected, actual, msg=None):
8400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Checks whether actual is a superset of expected."""
8410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        missing = []
8420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mismatched = []
8430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for key, value in expected.iteritems():
8440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if key not in actual:
8450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                missing.append(key)
8460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif value != actual[key]:
8470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                mismatched.append('%s, expected: %s, actual: %s' %
8480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                  (safe_repr(key), safe_repr(value),
8490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   safe_repr(actual[key])))
8500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not (missing or mismatched):
8520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
8530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        standardMsg = ''
8550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if missing:
8560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = 'Missing: %s' % ','.join(safe_repr(m) for m in
8570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                    missing)
8580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if mismatched:
8590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if standardMsg:
8600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                standardMsg += '; '
8610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg += 'Mismatched values: %s' % ','.join(mismatched)
8620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fail(self._formatMessage(msg, standardMsg))
8640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertItemsEqual(self, expected_seq, actual_seq, msg=None):
8660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """An unordered sequence specific comparison. It asserts that
8670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        actual_seq and expected_seq have the same element counts.
8680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Equivalent to::
8690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(Counter(iter(actual_seq)),
8710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             Counter(iter(expected_seq)))
8720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Asserts that each element has the same count in both sequences.
8740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example:
8750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            - [0, 1, 1] and [1, 0, 1] compare equal.
8760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            - [0, 0, 1] and [0, 1] compare unequal.
8770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
8780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        first_seq, second_seq = list(expected_seq), list(actual_seq)
8790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with warnings.catch_warnings():
8800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if sys.py3kwarning:
8810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Silence Py3k warning raised during the sorting
8820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for _msg in ["(code|dict|type) inequality comparisons",
8830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "builtin_function_or_method order comparisons",
8840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "comparing unequal types"]:
8850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    warnings.filterwarnings("ignore", _msg, DeprecationWarning)
8860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
8870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                first = collections.Counter(first_seq)
8880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                second = collections.Counter(second_seq)
8890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except TypeError:
8900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Handle case with unhashable elements
8910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                differences = _count_diff_all_purpose(first_seq, second_seq)
8920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
8930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if first == second:
8940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    return
8950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                differences = _count_diff_hashable(first_seq, second_seq)
8960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if differences:
8980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = 'Element counts were not equal:\n'
8990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            lines = ['First has %d, Second has %d:  %r' % diff for diff in differences]
9000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            diffMsg = '\n'.join(lines)
9010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = self._truncateMessage(standardMsg, diffMsg)
9020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = self._formatMessage(msg, standardMsg)
9030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(msg)
9040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertMultiLineEqual(self, first, second, msg=None):
9060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Assert that two multi-line strings are equal."""
9070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(first, basestring,
9080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                'First argument is not a string')
9090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(second, basestring,
9100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                'Second argument is not a string')
9110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if first != second:
9130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # don't use difflib if the strings are too long
9140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if (len(first) > self._diffThreshold or
9150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                len(second) > self._diffThreshold):
9160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._baseAssertEqual(first, second, msg)
9170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            firstlines = first.splitlines(True)
9180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            secondlines = second.splitlines(True)
9190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if len(firstlines) == 1 and first.strip('\r\n') == first:
9200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                firstlines = [first + '\n']
9210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                secondlines = [second + '\n']
9220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s != %s' % (safe_repr(first, True),
9230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                        safe_repr(second, True))
9240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
9250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = self._truncateMessage(standardMsg, diff)
9260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertLess(self, a, b, msg=None):
9290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a < b), but with a nicer default message."""
9300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not a < b:
9310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s not less than %s' % (safe_repr(a), safe_repr(b))
9320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertLessEqual(self, a, b, msg=None):
9350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a <= b), but with a nicer default message."""
9360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not a <= b:
9370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s not less than or equal to %s' % (safe_repr(a), safe_repr(b))
9380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertGreater(self, a, b, msg=None):
9410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a > b), but with a nicer default message."""
9420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not a > b:
9430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s not greater than %s' % (safe_repr(a), safe_repr(b))
9440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertGreaterEqual(self, a, b, msg=None):
9470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Just like self.assertTrue(a >= b), but with a nicer default message."""
9480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not a >= b:
9490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s not greater than or equal to %s' % (safe_repr(a), safe_repr(b))
9500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertIsNone(self, obj, msg=None):
9530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Same as self.assertTrue(obj is None), with a nicer default message."""
9540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if obj is not None:
9550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s is not None' % (safe_repr(obj),)
9560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertIsNotNone(self, obj, msg=None):
9590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Included for symmetry with assertIsNone."""
9600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if obj is None:
9610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = 'unexpectedly None'
9620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertIsInstance(self, obj, cls, msg=None):
9650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
9660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        default message."""
9670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not isinstance(obj, cls):
9680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls)
9690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertNotIsInstance(self, obj, cls, msg=None):
9720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Included for symmetry with assertIsInstance."""
9730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(obj, cls):
9740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls)
9750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail(self._formatMessage(msg, standardMsg))
9760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertRaisesRegexp(self, expected_exception, expected_regexp,
9780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           callable_obj=None, *args, **kwargs):
9790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Asserts that the message in a raised exception matches a regexp.
9800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Args:
9820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected_exception: Exception class expected to be raised.
9830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected_regexp: Regexp (re pattern object or string) expected
9840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    to be found in error message.
9850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            callable_obj: Function to be called.
9860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            args: Extra args.
9870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            kwargs: Extra kwargs.
9880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
9890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        context = _AssertRaisesContext(expected_exception, self, expected_regexp)
9900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if callable_obj is None:
9910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return context
9920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with context:
9930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            callable_obj(*args, **kwargs)
9940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertRegexpMatches(self, text, expected_regexp, msg=None):
9960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail the test unless the text matches the regular expression."""
9970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(expected_regexp, basestring):
9980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected_regexp = re.compile(expected_regexp)
9990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not expected_regexp.search(text):
10000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = msg or "Regexp didn't match"
10010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)
10020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException(msg)
10030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None):
10050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fail the test if the text matches the regular expression."""
10060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(unexpected_regexp, basestring):
10070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unexpected_regexp = re.compile(unexpected_regexp)
10080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        match = unexpected_regexp.search(text)
10090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if match:
10100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = msg or "Regexp matched"
10110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = '%s: %r matches %r in %r' % (msg,
10120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                               text[match.start():match.end()],
10130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                               unexpected_regexp.pattern,
10140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                               text)
10150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise self.failureException(msg)
10160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass FunctionTestCase(TestCase):
10190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """A test case that wraps a test function.
10200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    This is useful for slipping pre-existing test functions into the
10220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    unittest framework. Optionally, set-up and tidy-up functions can be
10230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    supplied. As with TestCase, the tidy-up ('tearDown') function will
10240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    always be called if the set-up ('setUp') function ran successfully.
10250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
10260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
10280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        super(FunctionTestCase, self).__init__()
10290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._setUpFunc = setUp
10300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._tearDownFunc = tearDown
10310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._testFunc = testFunc
10320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._description = description
10330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUp(self):
10350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._setUpFunc is not None:
10360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._setUpFunc()
10370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
10390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._tearDownFunc is not None:
10400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._tearDownFunc()
10410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def runTest(self):
10430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._testFunc()
10440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def id(self):
10460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._testFunc.__name__
10470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __eq__(self, other):
10490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not isinstance(other, self.__class__):
10500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return NotImplemented
10510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._setUpFunc == other._setUpFunc and \
10530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               self._tearDownFunc == other._tearDownFunc and \
10540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               self._testFunc == other._testFunc and \
10550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               self._description == other._description
10560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __ne__(self, other):
10580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return not self == other
10590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __hash__(self):
10610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return hash((type(self), self._setUpFunc, self._tearDownFunc,
10620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     self._testFunc, self._description))
10630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __str__(self):
10650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "%s (%s)" % (strclass(self.__class__),
10660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            self._testFunc.__name__)
10670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __repr__(self):
10690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "<%s tec=%s>" % (strclass(self.__class__),
10700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     self._testFunc)
10710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def shortDescription(self):
10730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._description is not None:
10740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._description
10750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        doc = self._testFunc.__doc__
10760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return doc and doc.split("\n")[0].strip() or None
1077