<s> """<STR_LIT>""" <EOL> from collections import deque <EOL> import difflib <EOL> import inspect <EOL> import re <EOL> import types <EOL> import unittest <EOL> import stubout <EOL> class Error ( AssertionError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ExpectedMethodCallsError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , expected_methods ) : <EOL> """<STR_LIT>""" <EOL> if not expected_methods : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> Error . __init__ ( self ) <EOL> self . _expected_methods = expected_methods <EOL> def __str__ ( self ) : <EOL> calls = "<STR_LIT:\n>" . join ( [ "<STR_LIT>" % ( i , m ) <EOL> for i , m in enumerate ( self . _expected_methods ) ] ) <EOL> return "<STR_LIT>" % ( calls , ) <EOL> class UnexpectedMethodCallError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , unexpected_method , expected ) : <EOL> """<STR_LIT>""" <EOL> Error . __init__ ( self ) <EOL> if expected is None : <EOL> self . _str = "<STR_LIT>" % ( unexpected_method , ) <EOL> else : <EOL> differ = difflib . Differ ( ) <EOL> diff = differ . compare ( str ( unexpected_method ) . splitlines ( True ) , <EOL> str ( expected ) . splitlines ( True ) ) <EOL> self . _str = ( "<STR_LIT>" <EOL> % ( "<STR_LIT:\n>" . join ( diff ) , ) ) <EOL> def __str__ ( self ) : <EOL> return self . _str <EOL> class UnknownMethodCallError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , unknown_method_name ) : <EOL> """<STR_LIT>""" <EOL> Error . __init__ ( self ) <EOL> self . _unknown_method_name = unknown_method_name <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . _unknown_method_name <EOL> class PrivateAttributeError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attr ) : <EOL> Error . __init__ ( self ) <EOL> self . _attr = attr <EOL> def __str__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" % attr ) <EOL> class ExpectedMockCreationError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , expected_mocks ) : <EOL> """<STR_LIT>""" <EOL> if not expected_mocks : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> Error . __init__ ( self ) <EOL> self . _expected_mocks = expected_mocks <EOL> def __str__ ( self ) : <EOL> mocks = "<STR_LIT:\n>" . join ( [ "<STR_LIT>" % ( i , m ) <EOL> for i , m in enumerate ( self . _expected_mocks ) ] ) <EOL> return "<STR_LIT>" % ( mocks , ) <EOL> class UnexpectedMockCreationError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , instance , * params , ** named_params ) : <EOL> """<STR_LIT>""" <EOL> Error . __init__ ( self ) <EOL> self . _instance = instance <EOL> self . _params = params <EOL> self . _named_params = named_params <EOL> def __str__ ( self ) : <EOL> args = "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT:%s>" % v for i , v in enumerate ( self . _params ) ] ) <EOL> error = "<STR_LIT>" % ( self . _instance , args ) <EOL> if self . _named_params : <EOL> error += "<STR_LIT:U+002CU+0020>" + "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % ( k , v ) for k , v in <EOL> self . _named_params . iteritems ( ) ] ) <EOL> error += "<STR_LIT:)>" <EOL> return error <EOL> class Mox ( object ) : <EOL> """<STR_LIT>""" <EOL> _USE_MOCK_OBJECT = [ types . ClassType , types . FunctionType , types . InstanceType , <EOL> types . ModuleType , types . ObjectType , types . TypeType ] <EOL> _USE_MOCK_FACTORY = [ types . ClassType , types . ObjectType , types . TypeType ] <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _mock_objects = [ ] <EOL> self . stubs = stubout . StubOutForTesting ( ) <EOL> def CreateMock ( self , class_to_mock , attrs = { } ) : <EOL> """<STR_LIT>""" <EOL> new_mock = MockObject ( class_to_mock , attrs = attrs ) <EOL> self . _mock_objects . append ( new_mock ) <EOL> return new_mock <EOL> def CreateMockAnything ( self , description = None ) : <EOL> """<STR_LIT>""" <EOL> new_mock = MockAnything ( description = description ) <EOL> self . _mock_objects . append ( new_mock ) <EOL> return new_mock <EOL> def ReplayAll ( self ) : <EOL> """<STR_LIT>""" <EOL> for mock_obj in self . _mock_objects : <EOL> mock_obj . _Replay ( ) <EOL> def VerifyAll ( self ) : <EOL> """<STR_LIT>""" <EOL> for mock_obj in self . _mock_objects : <EOL> mock_obj . _Verify ( ) <EOL> def ResetAll ( self ) : <EOL> """<STR_LIT>""" <EOL> for mock_obj in self . _mock_objects : <EOL> mock_obj . _Reset ( ) <EOL> def StubOutWithMock ( self , obj , attr_name , use_mock_anything = False ) : <EOL> """<STR_LIT>""" <EOL> attr_to_replace = getattr ( obj , attr_name ) <EOL> attr_type = type ( attr_to_replace ) <EOL> if attr_type == MockAnything or attr_type == MockObject : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if attr_type in self . _USE_MOCK_OBJECT and not use_mock_anything : <EOL> stub = self . CreateMock ( attr_to_replace ) <EOL> else : <EOL> stub = self . CreateMockAnything ( description = '<STR_LIT>' % attr_to_replace ) <EOL> self . stubs . Set ( obj , attr_name , stub ) <EOL> def StubOutClassWithMocks ( self , obj , attr_name ) : <EOL> """<STR_LIT>""" <EOL> attr_to_replace = getattr ( obj , attr_name ) <EOL> attr_type = type ( attr_to_replace ) <EOL> if attr_type == MockAnything or attr_type == MockObject : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if attr_type not in self . _USE_MOCK_FACTORY : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> factory = _MockObjectFactory ( attr_to_replace , self ) <EOL> self . _mock_objects . append ( factory ) <EOL> self . stubs . Set ( obj , attr_name , factory ) <EOL> def UnsetStubs ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stubs . UnsetAll ( ) <EOL> def Replay ( * args ) : <EOL> """<STR_LIT>""" <EOL> for mock in args : <EOL> mock . _Replay ( ) <EOL> def Verify ( * args ) : <EOL> """<STR_LIT>""" <EOL> for mock in args : <EOL> mock . _Verify ( ) <EOL> def Reset ( * args ) : <EOL> """<STR_LIT>""" <EOL> for mock in args : <EOL> mock . _Reset ( ) <EOL> class MockAnything : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , description = None ) : <EOL> """<STR_LIT>""" <EOL> self . _description = description <EOL> self . _Reset ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % id ( self ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __getattr__ ( self , method_name ) : <EOL> """<STR_LIT>""" <EOL> return self . _CreateMockMethod ( method_name ) <EOL> def _CreateMockMethod ( self , method_name , method_to_mock = None ) : <EOL> """<STR_LIT>""" <EOL> return MockMethod ( method_name , self . _expected_calls_queue , <EOL> self . _replay_mode , method_to_mock = method_to_mock , <EOL> description = self . _description ) <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:1> <EOL> def __eq__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( rhs , MockAnything ) and <EOL> self . _replay_mode == rhs . _replay_mode and <EOL> self . _expected_calls_queue == rhs . _expected_calls_queue ) <EOL> def __ne__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return not self == rhs <EOL> def _Replay ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _replay_mode = True <EOL> def _Verify ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _expected_calls_queue : <EOL> if ( len ( self . _expected_calls_queue ) == <NUM_LIT:1> and <EOL> isinstance ( self . _expected_calls_queue [ <NUM_LIT:0> ] , MultipleTimesGroup ) and <EOL> self . _expected_calls_queue [ <NUM_LIT:0> ] . IsSatisfied ( ) ) : <EOL> pass <EOL> else : <EOL> raise ExpectedMethodCallsError ( self . _expected_calls_queue ) <EOL> def _Reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _expected_calls_queue = deque ( ) <EOL> self . _replay_mode = False <EOL> class MockObject ( MockAnything , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , class_to_mock , attrs = { } ) : <EOL> """<STR_LIT>""" <EOL> MockAnything . __dict__ [ '<STR_LIT>' ] ( self ) <EOL> self . _known_methods = set ( ) <EOL> self . _known_vars = set ( ) <EOL> self . _class_to_mock = class_to_mock <EOL> try : <EOL> self . _description = class_to_mock . __name__ <EOL> except ( UnknownMethodCallError , AttributeError ) : <EOL> try : <EOL> self . _description = type ( class_to_mock ) . __name__ <EOL> except AttributeError : <EOL> pass <EOL> for method in dir ( class_to_mock ) : <EOL> attr = getattr ( class_to_mock , method ) <EOL> if callable ( attr ) : <EOL> self . _known_methods . add ( method ) <EOL> elif not ( type ( attr ) is property ) : <EOL> self . _known_vars . add ( method ) <EOL> for attr , value in attrs . items ( ) : <EOL> if attr . startswith ( "<STR_LIT:_>" ) : <EOL> raise PrivateAttributeError ( attr ) <EOL> elif attr in self . _known_methods : <EOL> raise ValueError ( "<STR_LIT>" % ( attr , <EOL> class_to_mock ) ) <EOL> else : <EOL> setattr ( self , attr , value ) <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . _known_vars : <EOL> return getattr ( self . _class_to_mock , name ) <EOL> if name in self . _known_methods : <EOL> return self . _CreateMockMethod ( <EOL> name , <EOL> method_to_mock = getattr ( self . _class_to_mock , name ) ) <EOL> raise UnknownMethodCallError ( name ) <EOL> def __eq__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( rhs , MockObject ) and <EOL> self . _class_to_mock == rhs . _class_to_mock and <EOL> self . _replay_mode == rhs . _replay_mode and <EOL> self . _expected_calls_queue == rhs . _expected_calls_queue ) <EOL> def __setitem__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in dir ( self . _class_to_mock ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if self . _replay_mode : <EOL> return MockMethod ( '<STR_LIT>' , self . _expected_calls_queue , <EOL> self . _replay_mode ) ( key , value ) <EOL> return self . _CreateMockMethod ( '<STR_LIT>' ) ( key , value ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in dir ( self . _class_to_mock ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if self . _replay_mode : <EOL> return MockMethod ( '<STR_LIT>' , self . _expected_calls_queue , <EOL> self . _replay_mode ) ( key ) <EOL> return self . _CreateMockMethod ( '<STR_LIT>' ) ( key ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> methods = dir ( self . _class_to_mock ) <EOL> if '<STR_LIT>' not in methods : <EOL> if '<STR_LIT>' not in methods or not self . _replay_mode : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> else : <EOL> results = [ ] <EOL> index = <NUM_LIT:0> <EOL> try : <EOL> while True : <EOL> results . append ( self [ index ] ) <EOL> index += <NUM_LIT:1> <EOL> except IndexError : <EOL> return iter ( results ) <EOL> if self . _replay_mode : <EOL> return MockMethod ( '<STR_LIT>' , self . _expected_calls_queue , <EOL> self . _replay_mode ) ( ) <EOL> return self . _CreateMockMethod ( '<STR_LIT>' ) ( ) <EOL> def __contains__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> contains = self . _class_to_mock . __dict__ . get ( '<STR_LIT>' , None ) <EOL> if contains is None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if self . _replay_mode : <EOL> return MockMethod ( '<STR_LIT>' , self . _expected_calls_queue , <EOL> self . _replay_mode ) ( key ) <EOL> return self . _CreateMockMethod ( '<STR_LIT>' ) ( key ) <EOL> def __call__ ( self , * params , ** named_params ) : <EOL> """<STR_LIT>""" <EOL> callable = hasattr ( self . _class_to_mock , '<STR_LIT>' ) <EOL> if not callable : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> method = None <EOL> if type ( self . _class_to_mock ) == types . FunctionType : <EOL> method = self . _class_to_mock ; <EOL> else : <EOL> method = getattr ( self . _class_to_mock , '<STR_LIT>' ) <EOL> mock_method = self . _CreateMockMethod ( '<STR_LIT>' , method_to_mock = method ) <EOL> return mock_method ( * params , ** named_params ) <EOL> @ property <EOL> def __class__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _class_to_mock <EOL> class _MockObjectFactory ( MockObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , class_to_mock , mox_instance ) : <EOL> MockObject . __init__ ( self , class_to_mock ) <EOL> self . _mox = mox_instance <EOL> self . _instance_queue = deque ( ) <EOL> def __call__ ( self , * params , ** named_params ) : <EOL> """<STR_LIT>""" <EOL> method = getattr ( self . _class_to_mock , '<STR_LIT>' ) <EOL> mock_method = self . _CreateMockMethod ( '<STR_LIT>' , method_to_mock = method ) <EOL> if self . _replay_mode : <EOL> if not self . _instance_queue : <EOL> raise UnexpectedMockCreationError ( self . _class_to_mock , * params , <EOL> ** named_params ) <EOL> mock_method ( * params , ** named_params ) <EOL> return self . _instance_queue . pop ( ) <EOL> else : <EOL> mock_method ( * params , ** named_params ) <EOL> instance = self . _mox . CreateMock ( self . _class_to_mock ) <EOL> self . _instance_queue . appendleft ( instance ) <EOL> return instance <EOL> def _Verify ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _instance_queue : <EOL> raise ExpectedMockCreationError ( self . _instance_queue ) <EOL> super ( _MockObjectFactory , self ) . _Verify ( ) <EOL> class MethodCallChecker ( object ) : <EOL> """<STR_LIT>""" <EOL> _NEEDED , _DEFAULT , _GIVEN = range ( <NUM_LIT:3> ) <EOL> def __init__ ( self , method ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _args , varargs , varkw , defaults = inspect . getargspec ( method ) <EOL> except TypeError : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % ( method , ) ) <EOL> if inspect . ismethod ( method ) : <EOL> self . _args = self . _args [ <NUM_LIT:1> : ] <EOL> self . _method = method <EOL> self . _has_varargs = varargs is not None <EOL> self . _has_varkw = varkw is not None <EOL> if defaults is None : <EOL> self . _required_args = self . _args <EOL> self . _default_args = [ ] <EOL> else : <EOL> self . _required_args = self . _args [ : - len ( defaults ) ] <EOL> self . _default_args = self . _args [ - len ( defaults ) : ] <EOL> def _RecordArgumentGiven ( self , arg_name , arg_status ) : <EOL> """<STR_LIT>""" <EOL> if arg_status . get ( arg_name , None ) == MethodCallChecker . _GIVEN : <EOL> raise AttributeError ( '<STR_LIT>' % ( arg_name , ) ) <EOL> arg_status [ arg_name ] = MethodCallChecker . _GIVEN <EOL> def Check ( self , params , named_params ) : <EOL> """<STR_LIT>""" <EOL> arg_status = dict ( ( a , MethodCallChecker . _NEEDED ) <EOL> for a in self . _required_args ) <EOL> for arg in self . _default_args : <EOL> arg_status [ arg ] = MethodCallChecker . _DEFAULT <EOL> for i in range ( len ( params ) ) : <EOL> try : <EOL> arg_name = self . _args [ i ] <EOL> except IndexError : <EOL> if not self . _has_varargs : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . _method . __name__ , i ) ) <EOL> else : <EOL> self . _RecordArgumentGiven ( arg_name , arg_status ) <EOL> for arg_name in named_params : <EOL> if arg_name not in arg_status and not self . _has_varkw : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> % ( self . _method . __name__ , arg_name ) ) <EOL> self . _RecordArgumentGiven ( arg_name , arg_status ) <EOL> still_needed = [ k for k , v in arg_status . iteritems ( ) <EOL> if v == MethodCallChecker . _NEEDED ] <EOL> if still_needed : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> % ( '<STR_LIT:U+0020>' . join ( sorted ( still_needed ) ) ) ) <EOL> class MockMethod ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method_name , call_queue , replay_mode , <EOL> method_to_mock = None , description = None ) : <EOL> """<STR_LIT>""" <EOL> self . _name = method_name <EOL> self . __name__ = method_name <EOL> self . _call_queue = call_queue <EOL> if not isinstance ( call_queue , deque ) : <EOL> self . _call_queue = deque ( self . _call_queue ) <EOL> self . _replay_mode = replay_mode <EOL> self . _description = description <EOL> self . _params = None <EOL> self . _named_params = None <EOL> self . _return_value = None <EOL> self . _exception = None <EOL> self . _side_effects = None <EOL> try : <EOL> self . _checker = MethodCallChecker ( method_to_mock ) <EOL> except ValueError : <EOL> self . _checker = None <EOL> def __call__ ( self , * params , ** named_params ) : <EOL> """<STR_LIT>""" <EOL> self . _params = params <EOL> self . _named_params = named_params <EOL> if not self . _replay_mode : <EOL> if self . _checker is not None : <EOL> self . _checker . Check ( params , named_params ) <EOL> self . _call_queue . append ( self ) <EOL> return self <EOL> expected_method = self . _VerifyMethodCall ( ) <EOL> if expected_method . _side_effects : <EOL> result = expected_method . _side_effects ( * params , ** named_params ) <EOL> if expected_method . _return_value is None : <EOL> expected_method . _return_value = result <EOL> if expected_method . _exception : <EOL> raise expected_method . _exception <EOL> return expected_method . _return_value <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _PopNextMethod ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _call_queue . popleft ( ) <EOL> except IndexError : <EOL> raise UnexpectedMethodCallError ( self , None ) <EOL> def _VerifyMethodCall ( self ) : <EOL> """<STR_LIT>""" <EOL> expected = self . _PopNextMethod ( ) <EOL> while isinstance ( expected , MethodGroup ) : <EOL> expected , method = expected . MethodCalled ( self ) <EOL> if method is not None : <EOL> return method <EOL> if expected != self : <EOL> raise UnexpectedMethodCallError ( self , expected ) <EOL> return expected <EOL> def __str__ ( self ) : <EOL> params = '<STR_LIT:U+002CU+0020>' . join ( <EOL> [ repr ( p ) for p in self . _params or [ ] ] + <EOL> [ '<STR_LIT>' % x for x in sorted ( ( self . _named_params or { } ) . items ( ) ) ] ) <EOL> full_desc = "<STR_LIT>" % ( self . _name , params , self . _return_value ) <EOL> if self . _description : <EOL> full_desc = "<STR_LIT>" % ( self . _description , full_desc ) <EOL> return full_desc <EOL> def __eq__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( rhs , MockMethod ) and <EOL> self . _name == rhs . _name and <EOL> self . _params == rhs . _params and <EOL> self . _named_params == rhs . _named_params ) <EOL> def __ne__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return not self == rhs <EOL> def GetPossibleGroup ( self ) : <EOL> """<STR_LIT>""" <EOL> this_method = self . _call_queue . pop ( ) <EOL> assert this_method == self <EOL> group = None <EOL> try : <EOL> group = self . _call_queue [ - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> pass <EOL> return group <EOL> def _CheckAndCreateNewGroup ( self , group_name , group_class ) : <EOL> """<STR_LIT>""" <EOL> group = self . GetPossibleGroup ( ) <EOL> if isinstance ( group , group_class ) and group . group_name ( ) == group_name : <EOL> group . AddMethod ( self ) <EOL> return self <EOL> new_group = group_class ( group_name ) <EOL> new_group . AddMethod ( self ) <EOL> self . _call_queue . append ( new_group ) <EOL> return self <EOL> def InAnyOrder ( self , group_name = "<STR_LIT:default>" ) : <EOL> """<STR_LIT>""" <EOL> return self . _CheckAndCreateNewGroup ( group_name , UnorderedGroup ) <EOL> def MultipleTimes ( self , group_name = "<STR_LIT:default>" ) : <EOL> """<STR_LIT>""" <EOL> return self . _CheckAndCreateNewGroup ( group_name , MultipleTimesGroup ) <EOL> def AndReturn ( self , return_value ) : <EOL> """<STR_LIT>""" <EOL> self . _return_value = return_value <EOL> return return_value <EOL> def AndRaise ( self , exception ) : <EOL> """<STR_LIT>""" <EOL> self . _exception = exception <EOL> def WithSideEffects ( self , side_effects ) : <EOL> """<STR_LIT>""" <EOL> self . _side_effects = side_effects <EOL> return self <EOL> class Comparator : <EOL> """<STR_LIT>""" <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def __eq__ ( self , rhs ) : <EOL> return self . equals ( rhs ) <EOL> def __ne__ ( self , rhs ) : <EOL> return not self . equals ( rhs ) <EOL> class IsA ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , class_name ) : <EOL> """<STR_LIT>""" <EOL> self . _class_name = class_name <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return isinstance ( rhs , self . _class_name ) <EOL> except TypeError : <EOL> return type ( rhs ) == type ( self . _class_name ) <EOL> def __repr__ ( self ) : <EOL> return str ( self . _class_name ) <EOL> class IsAlmost ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , float_value , places = <NUM_LIT:7> ) : <EOL> """<STR_LIT>""" <EOL> self . _float_value = float_value <EOL> self . _places = places <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return round ( rhs - self . _float_value , self . _places ) == <NUM_LIT:0> <EOL> except TypeError : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return str ( self . _float_value ) <EOL> class StrContains ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , search_string ) : <EOL> """<STR_LIT>""" <EOL> self . _search_string = search_string <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return rhs . find ( self . _search_string ) > - <NUM_LIT:1> <EOL> except Exception : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . _search_string <EOL> class Regex ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . regex = re . compile ( pattern , flags = flags ) <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return self . regex . search ( rhs ) is not None <EOL> def __repr__ ( self ) : <EOL> s = '<STR_LIT>' % self . regex . pattern <EOL> if self . regex . flags : <EOL> s += '<STR_LIT>' % self . regex . flags <EOL> s += '<STR_LIT:>>' <EOL> return s <EOL> class In ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> self . _key = key <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return self . _key in rhs <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . _key <EOL> class Not ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , predicate ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( predicate , Comparator ) , ( "<STR_LIT>" <EOL> "<STR_LIT>" % predicate ) <EOL> self . _predicate = predicate <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return not self . _predicate . equals ( rhs ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . _predicate <EOL> class ContainsKeyValue ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . _key = key <EOL> self . _value = value <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return rhs [ self . _key ] == self . _value <EOL> except Exception : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _key , self . _value ) <EOL> class ContainsAttributeValue ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . _key = key <EOL> self . _value = value <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return getattr ( rhs , self . _key ) == self . _value <EOL> except Exception : <EOL> return False <EOL> class SameElementsAs ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , expected_seq ) : <EOL> """<STR_LIT>""" <EOL> self . _expected_seq = expected_seq <EOL> def equals ( self , actual_seq ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> expected = dict ( [ ( element , None ) for element in self . _expected_seq ] ) <EOL> actual = dict ( [ ( element , None ) for element in actual_seq ] ) <EOL> except TypeError : <EOL> expected = list ( self . _expected_seq ) <EOL> actual = list ( actual_seq ) <EOL> expected . sort ( ) <EOL> actual . sort ( ) <EOL> return expected == actual <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . _expected_seq <EOL> class And ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . _comparators = args <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> for comparator in self . _comparators : <EOL> if not comparator . equals ( rhs ) : <EOL> return False <EOL> return True <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % str ( self . _comparators ) <EOL> class Or ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . _comparators = args <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> for comparator in self . _comparators : <EOL> if comparator . equals ( rhs ) : <EOL> return True <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % str ( self . _comparators ) <EOL> class Func ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . _func = func <EOL> def equals ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> return self . _func ( rhs ) <EOL> def __repr__ ( self ) : <EOL> return str ( self . _func ) <EOL> class IgnoreArg ( Comparator ) : <EOL> """<STR_LIT>""" <EOL> def equals ( self , unused_rhs ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class MethodGroup ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , group_name ) : <EOL> self . _group_name = group_name <EOL> def group_name ( self ) : <EOL> return self . _group_name <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . _group_name ) <EOL> def AddMethod ( self , mock_method ) : <EOL> raise NotImplementedError <EOL> def MethodCalled ( self , mock_method ) : <EOL> raise NotImplementedError <EOL> def IsSatisfied ( self ) : <EOL> raise NotImplementedError <EOL> class UnorderedGroup ( MethodGroup ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , group_name ) : <EOL> super ( UnorderedGroup , self ) . __init__ ( group_name ) <EOL> self . _methods = [ ] <EOL> def AddMethod ( self , mock_method ) : <EOL> """<STR_LIT>""" <EOL> self . _methods . append ( mock_method ) <EOL> def MethodCalled ( self , mock_method ) : <EOL> """<STR_LIT>""" <EOL> for method in self . _methods : <EOL> if method == mock_method : <EOL> self . _methods . remove ( mock_method ) <EOL> if not self . IsSatisfied ( ) : <EOL> mock_method . _call_queue . appendleft ( self ) <EOL> return self , method <EOL> raise UnexpectedMethodCallError ( mock_method , self ) <EOL> def IsSatisfied ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _methods ) == <NUM_LIT:0> <EOL> class MultipleTimesGroup ( MethodGroup ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , group_name ) : <EOL> super ( MultipleTimesGroup , self ) . __init__ ( group_name ) <EOL> self . _methods = set ( ) <EOL> self . _methods_left = set ( ) <EOL> def AddMethod ( self , mock_method ) : <EOL> """<STR_LIT>""" <EOL> self . _methods . add ( mock_method ) <EOL> self . _methods_left . add ( mock_method ) <EOL> def MethodCalled ( self , mock_method ) : <EOL> """<STR_LIT>""" <EOL> for method in self . _methods : <EOL> if method == mock_method : <EOL> self . _methods_left . discard ( method ) <EOL> mock_method . _call_queue . appendleft ( self ) <EOL> return self , method <EOL> if self . IsSatisfied ( ) : <EOL> next_method = mock_method . _PopNextMethod ( ) ; <EOL> return next_method , None <EOL> else : <EOL> raise UnexpectedMethodCallError ( mock_method , self ) <EOL> def IsSatisfied ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _methods_left ) == <NUM_LIT:0> <EOL> class MoxMetaTestBase ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , d ) : <EOL> type . __init__ ( cls , name , bases , d ) <EOL> for base in bases : <EOL> for attr_name in dir ( base ) : <EOL> if attr_name not in d : <EOL> d [ attr_name ] = getattr ( base , attr_name ) <EOL> for func_name , func in d . items ( ) : <EOL> if func_name . startswith ( '<STR_LIT:test>' ) and callable ( func ) : <EOL> setattr ( cls , func_name , MoxMetaTestBase . CleanUpTest ( cls , func ) ) <EOL> @ staticmethod <EOL> def CleanUpTest ( cls , func ) : <EOL> """<STR_LIT>""" <EOL> def new_method ( self , * args , ** kwargs ) : <EOL> mox_obj = getattr ( self , '<STR_LIT>' , None ) <EOL> stubout_obj = getattr ( self , '<STR_LIT>' , None ) <EOL> cleanup_mox = False <EOL> cleanup_stubout = False <EOL> if mox_obj and isinstance ( mox_obj , Mox ) : <EOL> cleanup_mox = True <EOL> if stubout_obj and isinstance ( stubout_obj , stubout . StubOutForTesting ) : <EOL> cleanup_stubout = True <EOL> try : <EOL> func ( self , * args , ** kwargs ) <EOL> finally : <EOL> if cleanup_mox : <EOL> mox_obj . UnsetStubs ( ) <EOL> if cleanup_stubout : <EOL> stubout_obj . UnsetAll ( ) <EOL> stubout_obj . SmartUnsetAll ( ) <EOL> if cleanup_mox : <EOL> mox_obj . VerifyAll ( ) <EOL> new_method . __name__ = func . __name__ <EOL> new_method . __doc__ = func . __doc__ <EOL> new_method . __module__ = func . __module__ <EOL> return new_method <EOL> class MoxTestBase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = MoxMetaTestBase <EOL> def setUp ( self ) : <EOL> super ( MoxTestBase , self ) . setUp ( ) <EOL> self . mox = Mox ( ) <EOL> self . stubs = stubout . StubOutForTesting ( ) </s>
