<s> """<STR_LIT>""" <EOL> import sys <EOL> import collections <EOL> import contextlib <EOL> import difflib <EOL> import logging <EOL> import pprint <EOL> import re <EOL> import traceback <EOL> import types <EOL> import unittest <EOL> import warnings <EOL> from python_toolbox . third_party import six <EOL> from python_toolbox . third_party . six . moves import range <EOL> from python_toolbox . third_party . unittest2 import result <EOL> from python_toolbox . third_party . unittest2 . util import ( <EOL> safe_repr , safe_str , strclass , <EOL> unorderable_list_difference , _common_shorten_repr <EOL> ) <EOL> from python_toolbox . third_party . unittest2 . compatibility import ( <EOL> wraps , with_context , catch_warnings , raise_from <EOL> ) <EOL> __unittest = True <EOL> DIFF_OMITTED = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class SkipTest ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class _ShouldStop ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class _UnexpectedSuccess ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class _Outcome ( object ) : <EOL> def __init__ ( self , result = None ) : <EOL> self . expecting_failure = False <EOL> self . result = result <EOL> self . result_supports_subtests = hasattr ( result , "<STR_LIT>" ) <EOL> self . success = True <EOL> self . skipped = [ ] <EOL> self . expectedFailure = None <EOL> self . errors = [ ] <EOL> @ contextlib . contextmanager <EOL> def testPartExecutor ( self , test_case , isTest = False ) : <EOL> old_success = self . success <EOL> self . success = True <EOL> try : <EOL> yield <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except SkipTest as e : <EOL> self . success = False <EOL> self . skipped . append ( ( test_case , str ( e ) ) ) <EOL> except _ShouldStop : <EOL> pass <EOL> except : <EOL> exc_info = sys . exc_info ( ) <EOL> if self . expecting_failure : <EOL> self . expectedFailure = exc_info <EOL> else : <EOL> self . success = False <EOL> self . errors . append ( ( test_case , exc_info ) ) <EOL> exc_info = None <EOL> else : <EOL> if self . result_supports_subtests and self . success : <EOL> self . errors . append ( ( test_case , None ) ) <EOL> finally : <EOL> self . success = self . success and old_success <EOL> def _id ( obj ) : <EOL> return obj <EOL> class_types = [ type ] <EOL> if getattr ( types , '<STR_LIT>' , None ) : <EOL> class_types . append ( types . ClassType ) <EOL> class_types = tuple ( class_types ) <EOL> def skip ( reason ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( test_item ) : <EOL> if not isinstance ( test_item , class_types ) : <EOL> @ wraps ( test_item ) <EOL> def skip_wrapper ( * args , ** kwargs ) : <EOL> raise SkipTest ( reason ) <EOL> test_item = skip_wrapper <EOL> test_item . __unittest_skip__ = True <EOL> test_item . __unittest_skip_why__ = reason <EOL> return test_item <EOL> return decorator <EOL> def skipIf ( condition , reason ) : <EOL> """<STR_LIT>""" <EOL> if condition : <EOL> return skip ( reason ) <EOL> return _id <EOL> def skipUnless ( condition , reason ) : <EOL> """<STR_LIT>""" <EOL> if not condition : <EOL> return skip ( reason ) <EOL> return _id <EOL> def expectedFailure ( test_item ) : <EOL> test_item . __unittest_expecting_failure__ = True <EOL> return test_item <EOL> class _BaseTestCaseContext : <EOL> def __init__ ( self , test_case ) : <EOL> self . test_case = test_case <EOL> def _raiseFailure ( self , standardMsg ) : <EOL> msg = self . test_case . _formatMessage ( self . msg , standardMsg ) <EOL> raise self . test_case . failureException ( msg ) <EOL> class _AssertRaisesBaseContext ( _BaseTestCaseContext ) : <EOL> def __init__ ( self , expected , test_case , callable_obj = None , <EOL> expected_regex = None ) : <EOL> _BaseTestCaseContext . __init__ ( self , test_case ) <EOL> self . expected = expected <EOL> self . failureException = test_case . failureException <EOL> if callable_obj is not None : <EOL> try : <EOL> self . obj_name = callable_obj . __name__ <EOL> except AttributeError : <EOL> self . obj_name = str ( callable_obj ) <EOL> else : <EOL> self . obj_name = None <EOL> if expected_regex is not None : <EOL> expected_regex = re . compile ( expected_regex ) <EOL> self . expected_regex = expected_regex <EOL> self . msg = None <EOL> def handle ( self , name , callable_obj , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> if callable_obj is None : <EOL> self . msg = kwargs . pop ( '<STR_LIT>' , None ) <EOL> return self <EOL> with self : <EOL> callable_obj ( * args , ** kwargs ) <EOL> class _AssertRaisesContext ( _AssertRaisesBaseContext ) : <EOL> """<STR_LIT>""" <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , tb ) : <EOL> if exc_type is None : <EOL> try : <EOL> exc_name = self . expected . __name__ <EOL> except AttributeError : <EOL> exc_name = str ( self . expected ) <EOL> raise self . failureException ( <EOL> "<STR_LIT>" % ( exc_name , ) ) <EOL> if not issubclass ( exc_type , self . expected ) : <EOL> return False <EOL> self . exception = exc_value <EOL> if self . expected_regex is None : <EOL> return True <EOL> expected_regex = self . expected_regex <EOL> if not expected_regex . search ( str ( exc_value ) ) : <EOL> raise self . failureException ( '<STR_LIT>' % <EOL> ( expected_regex . pattern , str ( exc_value ) ) ) <EOL> return True <EOL> class _AssertWarnsContext ( _AssertRaisesBaseContext ) : <EOL> """<STR_LIT>""" <EOL> def __enter__ ( self ) : <EOL> for v in sys . modules . values ( ) : <EOL> if getattr ( v , '<STR_LIT>' , None ) : <EOL> v . __warningregistry__ = { } <EOL> self . warnings_manager = catch_warnings ( record = True ) <EOL> self . warnings = self . warnings_manager . __enter__ ( ) <EOL> warnings . simplefilter ( "<STR_LIT>" , self . expected ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , tb ) : <EOL> self . warnings_manager . __exit__ ( exc_type , exc_value , tb ) <EOL> if exc_type is not None : <EOL> return <EOL> try : <EOL> exc_name = self . expected . __name__ <EOL> except AttributeError : <EOL> exc_name = str ( self . expected ) <EOL> first_matching = None <EOL> for m in self . warnings : <EOL> w = m . message <EOL> if not isinstance ( w , self . expected ) : <EOL> continue <EOL> if first_matching is None : <EOL> first_matching = w <EOL> if ( self . expected_regex is not None and <EOL> not self . expected_regex . search ( str ( w ) ) ) : <EOL> continue <EOL> self . warning = w <EOL> self . filename = m . filename <EOL> self . lineno = m . lineno <EOL> return <EOL> if first_matching is not None : <EOL> raise self . failureException ( '<STR_LIT>' % <EOL> ( self . expected_regex . pattern , str ( first_matching ) ) ) <EOL> if self . obj_name : <EOL> raise self . failureException ( "<STR_LIT>" <EOL> % ( exc_name , self . obj_name ) ) <EOL> else : <EOL> raise self . failureException ( "<STR_LIT>" <EOL> % exc_name ) <EOL> class _TypeEqualityDict ( object ) : <EOL> def __init__ ( self , testcase ) : <EOL> self . testcase = testcase <EOL> self . _store = { } <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _store [ key ] = value <EOL> def __getitem__ ( self , key ) : <EOL> value = self . _store [ key ] <EOL> if isinstance ( value , six . string_types ) : <EOL> return getattr ( self . testcase , value ) <EOL> return value <EOL> def get ( self , key , default = None ) : <EOL> if key in self . _store : <EOL> return self [ key ] <EOL> return default <EOL> _LoggingWatcher = collections . namedtuple ( "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> class _CapturingHandler ( logging . Handler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> logging . Handler . __init__ ( self ) <EOL> self . watcher = _LoggingWatcher ( [ ] , [ ] ) <EOL> def flush ( self ) : <EOL> pass <EOL> def emit ( self , record ) : <EOL> self . watcher . records . append ( record ) <EOL> msg = self . format ( record ) <EOL> self . watcher . output . append ( msg ) <EOL> class _AssertLogsContext ( _BaseTestCaseContext ) : <EOL> """<STR_LIT>""" <EOL> LOGGING_FORMAT = "<STR_LIT>" <EOL> def __init__ ( self , test_case , logger_name , level ) : <EOL> _BaseTestCaseContext . __init__ ( self , test_case ) <EOL> self . logger_name = logger_name <EOL> if level : <EOL> self . level = getattr ( logging , str ( level ) , level ) <EOL> else : <EOL> self . level = logging . INFO <EOL> self . msg = None <EOL> def __enter__ ( self ) : <EOL> if isinstance ( self . logger_name , logging . Logger ) : <EOL> logger = self . logger = self . logger_name <EOL> else : <EOL> logger = self . logger = logging . getLogger ( self . logger_name ) <EOL> formatter = logging . Formatter ( self . LOGGING_FORMAT ) <EOL> handler = _CapturingHandler ( ) <EOL> handler . setFormatter ( formatter ) <EOL> self . watcher = handler . watcher <EOL> self . old_handlers = logger . handlers [ : ] <EOL> self . old_level = logger . level <EOL> self . old_propagate = logger . propagate <EOL> logger . handlers = [ handler ] <EOL> logger . setLevel ( self . level ) <EOL> logger . propagate = False <EOL> return handler . watcher <EOL> def __exit__ ( self , exc_type , exc_value , tb ) : <EOL> self . logger . handlers = self . old_handlers <EOL> self . logger . propagate = self . old_propagate <EOL> self . logger . setLevel ( self . old_level ) <EOL> if exc_type is not None : <EOL> return False <EOL> if len ( self . watcher . records ) == <NUM_LIT:0> : <EOL> self . _raiseFailure ( <EOL> "<STR_LIT>" <EOL> . format ( logging . getLevelName ( self . level ) , self . logger . name ) ) <EOL> class TestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> failureException = AssertionError <EOL> longMessage = True <EOL> maxDiff = <NUM_LIT> * <NUM_LIT:8> <EOL> _diffThreshold = <NUM_LIT:2> ** <NUM_LIT:16> <EOL> _classSetupFailed = False <EOL> def __init__ ( self , methodName = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . _testMethodName = methodName <EOL> self . _outcome = None <EOL> try : <EOL> testMethod = getattr ( self , methodName ) <EOL> except AttributeError : <EOL> raise ValueError ( "<STR_LIT>" % ( self . __class__ , methodName ) ) <EOL> self . _testMethodDoc = testMethod . __doc__ <EOL> self . _cleanups = [ ] <EOL> self . _subtest = None <EOL> self . _type_equality_funcs = _TypeEqualityDict ( self ) <EOL> self . addTypeEqualityFunc ( dict , '<STR_LIT>' ) <EOL> self . addTypeEqualityFunc ( list , '<STR_LIT>' ) <EOL> self . addTypeEqualityFunc ( tuple , '<STR_LIT>' ) <EOL> self . addTypeEqualityFunc ( set , '<STR_LIT>' ) <EOL> self . addTypeEqualityFunc ( frozenset , '<STR_LIT>' ) <EOL> if six . PY2 : <EOL> self . addTypeEqualityFunc ( str , '<STR_LIT>' ) <EOL> self . addTypeEqualityFunc ( six . text_type , '<STR_LIT>' ) <EOL> def addTypeEqualityFunc ( self , typeobj , function ) : <EOL> """<STR_LIT>""" <EOL> self . _type_equality_funcs [ typeobj ] = function <EOL> def addCleanup ( self , function , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _cleanups . append ( ( function , args , kwargs ) ) <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> "<STR_LIT>" <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> "<STR_LIT>" <EOL> def defaultTestResult ( self ) : <EOL> return result . TestResult ( ) <EOL> def shortDescription ( self ) : <EOL> """<STR_LIT>""" <EOL> doc = self . _testMethodDoc <EOL> return doc and doc . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:0> ] . strip ( ) or None <EOL> def id ( self ) : <EOL> return "<STR_LIT>" % ( strclass ( self . __class__ ) , self . _testMethodName ) <EOL> def __eq__ ( self , other ) : <EOL> if type ( self ) is not type ( other ) : <EOL> return NotImplemented <EOL> return self . _testMethodName == other . _testMethodName <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __hash__ ( self ) : <EOL> return hash ( ( type ( self ) , self . _testMethodName ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . _testMethodName , strclass ( self . __class__ ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( strclass ( self . __class__ ) , self . _testMethodName ) <EOL> def _addSkip ( self , result , test_case , reason ) : <EOL> addSkip = getattr ( result , '<STR_LIT>' , None ) <EOL> if addSkip is not None : <EOL> addSkip ( test_case , reason ) <EOL> else : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> RuntimeWarning , <NUM_LIT:2> ) <EOL> result . addSuccess ( test_case ) <EOL> @ contextlib . contextmanager <EOL> def subTest ( self , msg = None , ** params ) : <EOL> """<STR_LIT>""" <EOL> if not self . _outcome . result_supports_subtests : <EOL> yield <EOL> return <EOL> parent = self . _subtest <EOL> if parent is None : <EOL> params_map = collections . ChainMap ( params ) <EOL> else : <EOL> params_map = parent . params . new_child ( params ) <EOL> self . _subtest = _SubTest ( self , msg , params_map ) <EOL> try : <EOL> with self . _outcome . testPartExecutor ( self . _subtest , isTest = True ) : <EOL> yield <EOL> if not self . _outcome . success : <EOL> result = self . _outcome . result <EOL> if result is not None and result . failfast : <EOL> raise _ShouldStop <EOL> elif self . _outcome . expectedFailure : <EOL> raise _ShouldStop <EOL> finally : <EOL> self . _subtest = parent <EOL> def _feedErrorsToResult ( self , result , errors ) : <EOL> for test , exc_info in errors : <EOL> if isinstance ( test , _SubTest ) : <EOL> result . addSubTest ( test . test_case , test , exc_info ) <EOL> elif exc_info is not None : <EOL> if issubclass ( exc_info [ <NUM_LIT:0> ] , self . failureException ) : <EOL> result . addFailure ( test , exc_info ) <EOL> else : <EOL> result . addError ( test , exc_info ) <EOL> def _addExpectedFailure ( self , result , exc_info ) : <EOL> try : <EOL> addExpectedFailure = result . addExpectedFailure <EOL> except AttributeError : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> RuntimeWarning ) <EOL> result . addSuccess ( self ) <EOL> else : <EOL> addExpectedFailure ( self , exc_info ) <EOL> def _addUnexpectedSuccess ( self , result ) : <EOL> try : <EOL> addUnexpectedSuccess = result . addUnexpectedSuccess <EOL> except AttributeError : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> RuntimeWarning ) <EOL> try : <EOL> raise_from ( _UnexpectedSuccess , None ) <EOL> except _UnexpectedSuccess : <EOL> result . addFailure ( self , sys . exc_info ( ) ) <EOL> else : <EOL> addUnexpectedSuccess ( self ) <EOL> def run ( self , result = None ) : <EOL> orig_result = result <EOL> if result is None : <EOL> result = self . defaultTestResult ( ) <EOL> startTestRun = getattr ( result , '<STR_LIT>' , None ) <EOL> if startTestRun is not None : <EOL> startTestRun ( ) <EOL> result . startTest ( self ) <EOL> testMethod = getattr ( self , self . _testMethodName ) <EOL> if ( getattr ( self . __class__ , "<STR_LIT>" , False ) or <EOL> getattr ( testMethod , "<STR_LIT>" , False ) ) : <EOL> try : <EOL> skip_why = ( getattr ( self . __class__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> or getattr ( testMethod , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . _addSkip ( result , self , skip_why ) <EOL> finally : <EOL> result . stopTest ( self ) <EOL> return <EOL> expecting_failure = getattr ( testMethod , <EOL> "<STR_LIT>" , False ) <EOL> outcome = _Outcome ( result ) <EOL> try : <EOL> self . _outcome = outcome <EOL> with outcome . testPartExecutor ( self ) : <EOL> self . setUp ( ) <EOL> if outcome . success : <EOL> outcome . expecting_failure = expecting_failure <EOL> with outcome . testPartExecutor ( self , isTest = True ) : <EOL> testMethod ( ) <EOL> outcome . expecting_failure = False <EOL> with outcome . testPartExecutor ( self ) : <EOL> self . tearDown ( ) <EOL> self . doCleanups ( ) <EOL> for test , reason in outcome . skipped : <EOL> self . _addSkip ( result , test , reason ) <EOL> self . _feedErrorsToResult ( result , outcome . errors ) <EOL> if outcome . success : <EOL> if expecting_failure : <EOL> if outcome . expectedFailure : <EOL> self . _addExpectedFailure ( result , outcome . expectedFailure ) <EOL> else : <EOL> self . _addUnexpectedSuccess ( result ) <EOL> else : <EOL> result . addSuccess ( self ) <EOL> return result <EOL> finally : <EOL> result . stopTest ( self ) <EOL> if orig_result is None : <EOL> stopTestRun = getattr ( result , '<STR_LIT>' , None ) <EOL> if stopTestRun is not None : <EOL> stopTestRun ( ) <EOL> del outcome . errors [ : ] <EOL> outcome . expectedFailure = None <EOL> self . _outcome = None <EOL> def doCleanups ( self ) : <EOL> """<STR_LIT>""" <EOL> outcome = self . _outcome or _Outcome ( ) <EOL> while self . _cleanups : <EOL> function , args , kwargs = self . _cleanups . pop ( ) <EOL> with outcome . testPartExecutor ( self ) : <EOL> function ( * args , ** kwargs ) <EOL> return outcome . success <EOL> def __call__ ( self , * args , ** kwds ) : <EOL> return self . run ( * args , ** kwds ) <EOL> def debug ( self ) : <EOL> """<STR_LIT>""" <EOL> self . setUp ( ) <EOL> getattr ( self , self . _testMethodName ) ( ) <EOL> self . tearDown ( ) <EOL> while self . _cleanups : <EOL> function , args , kwargs = self . _cleanups . pop ( - <NUM_LIT:1> ) <EOL> function ( * args , ** kwargs ) <EOL> def skipTest ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> raise SkipTest ( reason ) <EOL> def fail ( self , msg = None ) : <EOL> """<STR_LIT>""" <EOL> raise self . failureException ( msg ) <EOL> def assertFalse ( self , expr , msg = None ) : <EOL> "<STR_LIT>" <EOL> if expr : <EOL> msg = self . _formatMessage ( msg , "<STR_LIT>" % safe_repr ( expr ) ) <EOL> raise self . failureException ( msg ) <EOL> def assertTrue ( self , expr , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not expr : <EOL> msg = self . _formatMessage ( msg , "<STR_LIT>" % safe_repr ( expr ) ) <EOL> raise self . failureException ( msg ) <EOL> def _formatMessage ( self , msg , standardMsg ) : <EOL> """<STR_LIT>""" <EOL> if not self . longMessage : <EOL> return msg or standardMsg <EOL> if msg is None : <EOL> return standardMsg <EOL> try : <EOL> return '<STR_LIT>' % ( standardMsg , msg ) <EOL> except UnicodeDecodeError : <EOL> return '<STR_LIT>' % ( safe_str ( standardMsg ) , safe_str ( msg ) ) <EOL> def assertRaises ( self , excClass , callableObj = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if callableObj is None : <EOL> return _AssertRaisesContext ( excClass , self ) <EOL> try : <EOL> callableObj ( * args , ** kwargs ) <EOL> except excClass : <EOL> return <EOL> if hasattr ( excClass , '<STR_LIT>' ) : <EOL> excName = excClass . __name__ <EOL> else : <EOL> excName = str ( excClass ) <EOL> raise self . failureException ( "<STR_LIT>" % excName ) <EOL> def assertWarns ( self , expected_warning , callable_obj = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = _AssertWarnsContext ( expected_warning , self , callable_obj ) <EOL> if callable_obj is None : <EOL> return context <EOL> context . __enter__ ( ) <EOL> try : <EOL> callable_obj ( * args , ** kwargs ) <EOL> except : <EOL> if not context . __exit__ ( * sys . exc_info ( ) ) : <EOL> raise <EOL> else : <EOL> return <EOL> else : <EOL> context . __exit__ ( None , None , None ) <EOL> def assertLogs ( self , logger = None , level = None ) : <EOL> """<STR_LIT>""" <EOL> return _AssertLogsContext ( self , logger , level ) <EOL> def _getAssertEqualityFunc ( self , first , second ) : <EOL> """<STR_LIT>""" <EOL> if type ( first ) is type ( second ) : <EOL> asserter = self . _type_equality_funcs . get ( type ( first ) ) <EOL> if asserter is not None : <EOL> return asserter <EOL> return self . _baseAssertEqual <EOL> def _baseAssertEqual ( self , first , second , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not first == second : <EOL> standardMsg = '<STR_LIT>' % _common_shorten_repr ( first , second ) <EOL> msg = self . _formatMessage ( msg , standardMsg ) <EOL> raise self . failureException ( msg ) <EOL> def assertEqual ( self , first , second , msg = None ) : <EOL> """<STR_LIT>""" <EOL> assertion_func = self . _getAssertEqualityFunc ( first , second ) <EOL> assertion_func ( first , second , msg = msg ) <EOL> def assertNotEqual ( self , first , second , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not first != second : <EOL> msg = self . _formatMessage ( msg , '<STR_LIT>' % ( safe_repr ( first ) , <EOL> safe_repr ( second ) ) ) <EOL> raise self . failureException ( msg ) <EOL> def assertAlmostEqual ( self , first , second , places = None , msg = None , delta = None ) : <EOL> """<STR_LIT>""" <EOL> if first == second : <EOL> return <EOL> if delta is not None and places is not None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if delta is not None : <EOL> if abs ( first - second ) <= delta : <EOL> return <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( first ) , <EOL> safe_repr ( second ) , <EOL> safe_repr ( delta ) ) <EOL> else : <EOL> if places is None : <EOL> places = <NUM_LIT:7> <EOL> if round ( abs ( second - first ) , places ) == <NUM_LIT:0> : <EOL> return <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( first ) , <EOL> safe_repr ( second ) , <EOL> places ) <EOL> msg = self . _formatMessage ( msg , standardMsg ) <EOL> raise self . failureException ( msg ) <EOL> def assertNotAlmostEqual ( self , first , second , places = None , msg = None , delta = None ) : <EOL> """<STR_LIT>""" <EOL> if delta is not None and places is not None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if delta is not None : <EOL> if not ( first == second ) and abs ( first - second ) > delta : <EOL> return <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( first ) , <EOL> safe_repr ( second ) , <EOL> safe_repr ( delta ) ) <EOL> else : <EOL> if places is None : <EOL> places = <NUM_LIT:7> <EOL> if not ( first == second ) and round ( abs ( second - first ) , places ) != <NUM_LIT:0> : <EOL> return <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( first ) , <EOL> safe_repr ( second ) , <EOL> places ) <EOL> msg = self . _formatMessage ( msg , standardMsg ) <EOL> raise self . failureException ( msg ) <EOL> def assertSequenceEqual ( self , seq1 , seq2 , msg = None , seq_type = None ) : <EOL> """<STR_LIT>""" <EOL> if seq_type is not None : <EOL> seq_type_name = seq_type . __name__ <EOL> if not isinstance ( seq1 , seq_type ) : <EOL> raise self . failureException ( '<STR_LIT>' <EOL> % ( seq_type_name , safe_repr ( seq1 ) ) ) <EOL> if not isinstance ( seq2 , seq_type ) : <EOL> raise self . failureException ( '<STR_LIT>' <EOL> % ( seq_type_name , safe_repr ( seq2 ) ) ) <EOL> else : <EOL> seq_type_name = "<STR_LIT>" <EOL> differing = None <EOL> try : <EOL> len1 = len ( seq1 ) <EOL> except ( TypeError , NotImplementedError ) : <EOL> differing = '<STR_LIT>' % ( <EOL> seq_type_name ) <EOL> if differing is None : <EOL> try : <EOL> len2 = len ( seq2 ) <EOL> except ( TypeError , NotImplementedError ) : <EOL> differing = '<STR_LIT>' % ( <EOL> seq_type_name ) <EOL> if differing is None : <EOL> if seq1 == seq2 : <EOL> return <EOL> differing = '<STR_LIT>' % ( <EOL> ( seq_type_name . capitalize ( ) , ) + <EOL> _common_shorten_repr ( seq1 , seq2 ) ) <EOL> for i in range ( min ( len1 , len2 ) ) : <EOL> try : <EOL> item1 = seq1 [ i ] <EOL> except ( TypeError , IndexError , NotImplementedError ) : <EOL> differing += ( '<STR_LIT>' % <EOL> ( i , seq_type_name ) ) <EOL> break <EOL> try : <EOL> item2 = seq2 [ i ] <EOL> except ( TypeError , IndexError , NotImplementedError ) : <EOL> differing += ( '<STR_LIT>' % <EOL> ( i , seq_type_name ) ) <EOL> break <EOL> if item1 != item2 : <EOL> differing += ( '<STR_LIT>' % <EOL> ( i , item1 , item2 ) ) <EOL> break <EOL> else : <EOL> if ( len1 == len2 and seq_type is None and <EOL> type ( seq1 ) != type ( seq2 ) ) : <EOL> return <EOL> if len1 > len2 : <EOL> differing += ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( seq_type_name , len1 - len2 ) ) <EOL> try : <EOL> differing += ( '<STR_LIT>' % <EOL> ( len2 , seq1 [ len2 ] ) ) <EOL> except ( TypeError , IndexError , NotImplementedError ) : <EOL> differing += ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( len2 , seq_type_name ) ) <EOL> elif len1 < len2 : <EOL> differing += ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( seq_type_name , len2 - len1 ) ) <EOL> try : <EOL> differing += ( '<STR_LIT>' % <EOL> ( len1 , seq2 [ len1 ] ) ) <EOL> except ( TypeError , IndexError , NotImplementedError ) : <EOL> differing += ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( len1 , seq_type_name ) ) <EOL> standardMsg = differing <EOL> diffMsg = '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( <EOL> difflib . ndiff ( pprint . pformat ( seq1 ) . splitlines ( ) , <EOL> pprint . pformat ( seq2 ) . splitlines ( ) ) ) <EOL> standardMsg = self . _truncateMessage ( standardMsg , diffMsg ) <EOL> msg = self . _formatMessage ( msg , standardMsg ) <EOL> self . fail ( msg ) <EOL> def _truncateMessage ( self , message , diff ) : <EOL> max_diff = self . maxDiff <EOL> if max_diff is None or len ( diff ) <= max_diff : <EOL> return message + diff <EOL> return message + ( DIFF_OMITTED % len ( diff ) ) <EOL> def assertListEqual ( self , list1 , list2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> self . assertSequenceEqual ( list1 , list2 , msg , seq_type = list ) <EOL> def assertTupleEqual ( self , tuple1 , tuple2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> self . assertSequenceEqual ( tuple1 , tuple2 , msg , seq_type = tuple ) <EOL> def assertSetEqual ( self , set1 , set2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> difference1 = set1 . difference ( set2 ) <EOL> except TypeError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . fail ( '<STR_LIT>' % e ) <EOL> except AttributeError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . fail ( '<STR_LIT>' % e ) <EOL> try : <EOL> difference2 = set2 . difference ( set1 ) <EOL> except TypeError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . fail ( '<STR_LIT>' % e ) <EOL> except AttributeError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . fail ( '<STR_LIT>' % e ) <EOL> if not ( difference1 or difference2 ) : <EOL> return <EOL> lines = [ ] <EOL> if difference1 : <EOL> lines . append ( '<STR_LIT>' ) <EOL> for item in difference1 : <EOL> lines . append ( repr ( item ) ) <EOL> if difference2 : <EOL> lines . append ( '<STR_LIT>' ) <EOL> for item in difference2 : <EOL> lines . append ( repr ( item ) ) <EOL> standardMsg = '<STR_LIT:\n>' . join ( lines ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertIn ( self , member , container , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if member not in container : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( member ) , <EOL> safe_repr ( container ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertNotIn ( self , member , container , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if member in container : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( member ) , <EOL> safe_repr ( container ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertIs ( self , expr1 , expr2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if expr1 is not expr2 : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( expr1 ) , safe_repr ( expr2 ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertIsNot ( self , expr1 , expr2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if expr1 is expr2 : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( expr1 ) , ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertDictEqual ( self , d1 , d2 , msg = None ) : <EOL> self . assertIsInstance ( d1 , dict , '<STR_LIT>' ) <EOL> self . assertIsInstance ( d2 , dict , '<STR_LIT>' ) <EOL> if d1 != d2 : <EOL> standardMsg = '<STR_LIT>' % _common_shorten_repr ( d1 , d2 ) <EOL> diff = ( '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( difflib . ndiff ( <EOL> pprint . pformat ( d1 ) . splitlines ( ) , <EOL> pprint . pformat ( d2 ) . splitlines ( ) ) ) ) <EOL> standardMsg = self . _truncateMessage ( standardMsg , diff ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertDictContainsSubset ( self , expected , actual , msg = None ) : <EOL> """<STR_LIT>""" <EOL> missing = [ ] <EOL> mismatched = [ ] <EOL> for key , value in expected . items ( ) : <EOL> if key not in actual : <EOL> missing . append ( key ) <EOL> elif value != actual [ key ] : <EOL> mismatched . append ( '<STR_LIT>' % <EOL> ( safe_repr ( key ) , safe_repr ( value ) , <EOL> safe_repr ( actual [ key ] ) ) ) <EOL> if not ( missing or mismatched ) : <EOL> return <EOL> standardMsg = '<STR_LIT>' <EOL> if missing : <EOL> standardMsg = '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( safe_repr ( m ) for m in <EOL> missing ) <EOL> if mismatched : <EOL> if standardMsg : <EOL> standardMsg += '<STR_LIT>' <EOL> standardMsg += '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( mismatched ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertItemsEqual ( self , expected_seq , actual_seq , msg = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> expected = sorted ( expected_seq ) <EOL> actual = sorted ( actual_seq ) <EOL> except TypeError : <EOL> expected = list ( expected_seq ) <EOL> actual = list ( actual_seq ) <EOL> missing , unexpected = unorderable_list_difference ( <EOL> expected , actual , ignore_duplicate = False <EOL> ) <EOL> else : <EOL> return self . assertSequenceEqual ( expected , actual , msg = msg ) <EOL> errors = [ ] <EOL> if missing : <EOL> errors . append ( '<STR_LIT>' % <EOL> safe_repr ( missing ) ) <EOL> if unexpected : <EOL> errors . append ( '<STR_LIT>' % <EOL> safe_repr ( unexpected ) ) <EOL> if errors : <EOL> standardMsg = '<STR_LIT:\n>' . join ( errors ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertMultiLineEqual ( self , first , second , msg = None ) : <EOL> """<STR_LIT>""" <EOL> self . assertIsInstance ( first , six . string_types , ( <EOL> '<STR_LIT>' ) ) <EOL> self . assertIsInstance ( second , six . string_types , ( <EOL> '<STR_LIT>' ) ) <EOL> if first != second : <EOL> if ( len ( first ) > self . _diffThreshold or <EOL> len ( second ) > self . _diffThreshold ) : <EOL> self . _baseAssertEqual ( first , second , msg ) <EOL> firstlines = first . splitlines ( True ) <EOL> secondlines = second . splitlines ( True ) <EOL> if len ( firstlines ) == <NUM_LIT:1> and first . strip ( '<STR_LIT:\r\n>' ) == first : <EOL> firstlines = [ first + '<STR_LIT:\n>' ] <EOL> secondlines = [ second + '<STR_LIT:\n>' ] <EOL> standardMsg = '<STR_LIT>' % _common_shorten_repr ( first , second ) <EOL> diff = '<STR_LIT:\n>' + '<STR_LIT>' . join ( difflib . ndiff ( firstlines , secondlines ) ) <EOL> standardMsg = self . _truncateMessage ( standardMsg , diff ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertLess ( self , a , b , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not a < b : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( a ) , safe_repr ( b ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertLessEqual ( self , a , b , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not a <= b : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( a ) , safe_repr ( b ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertGreater ( self , a , b , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not a > b : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( a ) , safe_repr ( b ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertGreaterEqual ( self , a , b , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not a >= b : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( a ) , safe_repr ( b ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertIsNone ( self , obj , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if obj is not None : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( obj ) , ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertIsNotNone ( self , obj , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if obj is None : <EOL> standardMsg = '<STR_LIT>' <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertIsInstance ( self , obj , cls , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( obj , cls ) : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( obj ) , cls ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertNotIsInstance ( self , obj , cls , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , cls ) : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( obj ) , cls ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) <EOL> def assertRaisesRegex ( self , expected_exception , expected_regex , <EOL> callable_obj = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = _AssertRaisesContext ( expected_exception , self , callable_obj , <EOL> expected_regex ) <EOL> return context . handle ( '<STR_LIT>' , callable_obj , args , kwargs ) <EOL> def assertWarnsRegex ( self , expected_warning , expected_regex , <EOL> callable_obj = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = _AssertWarnsContext ( expected_warning , self , callable_obj , <EOL> expected_regex ) <EOL> return context . handle ( '<STR_LIT>' , callable_obj , args , kwargs ) <EOL> def assertRegex ( self , text , expected_regex , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( expected_regex , six . string_types ) : <EOL> expected_regex = re . compile ( expected_regex ) <EOL> if not expected_regex . search ( text ) : <EOL> msg = msg or "<STR_LIT>" <EOL> msg = '<STR_LIT>' % ( msg , expected_regex . pattern , text ) <EOL> raise self . failureException ( msg ) <EOL> def assertNotRegex ( self , text , unexpected_regex , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( unexpected_regex , six . string_types ) : <EOL> unexpected_regex = re . compile ( unexpected_regex ) <EOL> match = unexpected_regex . search ( text ) <EOL> if match : <EOL> msg = msg or "<STR_LIT>" <EOL> msg = '<STR_LIT>' % ( msg , <EOL> text [ match . start ( ) : match . end ( ) ] , <EOL> unexpected_regex . pattern , <EOL> text ) <EOL> raise self . failureException ( msg ) <EOL> def _deprecate ( original_func ) : <EOL> def deprecated_func ( * args , ** kwargs ) : <EOL> warnings . warn ( <EOL> ( '<STR_LIT>' % original_func . __name__ ) , <EOL> PendingDeprecationWarning , <NUM_LIT:2> ) <EOL> return original_func ( * args , ** kwargs ) <EOL> return deprecated_func <EOL> failUnlessEqual = assertEquals = _deprecate ( assertEqual ) <EOL> failIfEqual = assertNotEquals = _deprecate ( assertNotEqual ) <EOL> failUnlessAlmostEqual = assertAlmostEquals = _deprecate ( assertAlmostEqual ) <EOL> failIfAlmostEqual = assertNotAlmostEquals = _deprecate ( assertNotAlmostEqual ) <EOL> failUnless = assert_ = _deprecate ( assertTrue ) <EOL> failUnlessRaises = _deprecate ( assertRaises ) <EOL> failIf = _deprecate ( assertFalse ) <EOL> assertRaisesRegexp = _deprecate ( assertRaisesRegex ) <EOL> assertRegexpMatches = _deprecate ( assertRegex ) <EOL> assertNotRegexpMatches = _deprecate ( assertNotRegex ) <EOL> class FunctionTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , testFunc , setUp = None , tearDown = None , description = None ) : <EOL> super ( FunctionTestCase , self ) . __init__ ( ) <EOL> self . _setUpFunc = setUp <EOL> self . _tearDownFunc = tearDown <EOL> self . _testFunc = testFunc <EOL> self . _description = description <EOL> def setUp ( self ) : <EOL> if self . _setUpFunc is not None : <EOL> self . _setUpFunc ( ) <EOL> def tearDown ( self ) : <EOL> if self . _tearDownFunc is not None : <EOL> self . _tearDownFunc ( ) <EOL> def runTest ( self ) : <EOL> self . _testFunc ( ) <EOL> def id ( self ) : <EOL> return self . _testFunc . __name__ <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , self . __class__ ) : <EOL> return NotImplemented <EOL> return self . _setUpFunc == other . _setUpFunc and self . _tearDownFunc == other . _tearDownFunc and self . _testFunc == other . _testFunc and self . _description == other . _description <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __hash__ ( self ) : <EOL> return hash ( ( type ( self ) , self . _setUpFunc , self . _tearDownFunc , <EOL> self . _testFunc , self . _description ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( strclass ( self . __class__ ) , <EOL> self . _testFunc . __name__ ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( strclass ( self . __class__ ) , <EOL> self . _testFunc ) <EOL> def shortDescription ( self ) : <EOL> if self . _description is not None : <EOL> return self . _description <EOL> doc = self . _testFunc . __doc__ <EOL> return doc and doc . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:0> ] . strip ( ) or None <EOL> class _SubTest ( TestCase ) : <EOL> def __init__ ( self , test_case , message , params ) : <EOL> super ( _SubTest , self ) . __init__ ( ) <EOL> self . _message = message <EOL> self . test_case = test_case <EOL> self . params = params <EOL> self . failureException = test_case . failureException <EOL> def runTest ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def _subDescription ( self ) : <EOL> parts = [ ] <EOL> if self . _message : <EOL> parts . append ( "<STR_LIT>" . format ( self . _message ) ) <EOL> if self . params : <EOL> params_desc = '<STR_LIT:U+002CU+0020>' . join ( <EOL> "<STR_LIT>" . format ( k , v ) <EOL> for ( k , v ) in sorted ( self . params . items ( ) ) ) <EOL> parts . append ( "<STR_LIT>" . format ( params_desc ) ) <EOL> return "<STR_LIT:U+0020>" . join ( parts ) or '<STR_LIT>' <EOL> def id ( self ) : <EOL> return "<STR_LIT>" . format ( self . test_case . id ( ) , self . _subDescription ( ) ) <EOL> def shortDescription ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . test_case . shortDescription ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . test_case , self . _subDescription ( ) ) </s>
