<s> import copy <EOL> import sys <EOL> import unittest <EOL> from unittest . test . testmock . support import is_instance <EOL> from unittest import mock <EOL> from unittest . mock import ( <EOL> call , DEFAULT , patch , sentinel , <EOL> MagicMock , Mock , NonCallableMock , <EOL> NonCallableMagicMock , _CallList , <EOL> create_autospec <EOL> ) <EOL> class Iter ( object ) : <EOL> def __init__ ( self ) : <EOL> self . thing = iter ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return next ( self . thing ) <EOL> __next__ = next <EOL> class MockTest ( unittest . TestCase ) : <EOL> def test_all ( self ) : <EOL> exec ( "<STR_LIT>" ) <EOL> def test_constructor ( self ) : <EOL> mock = Mock ( ) <EOL> self . assertFalse ( mock . called , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_count , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) <EOL> self . assertTrue ( is_instance ( mock . return_value , Mock ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args , None , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args_list , [ ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . method_calls , [ ] , <EOL> "<STR_LIT>" ) <EOL> self . assertFalse ( '<STR_LIT>' in mock . __dict__ , <EOL> "<STR_LIT>" ) <EOL> self . assertIsNone ( mock . _mock_parent , <EOL> "<STR_LIT>" ) <EOL> self . assertIsNone ( mock . _mock_methods , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . _mock_children , { } , <EOL> "<STR_LIT>" ) <EOL> def test_return_value_in_constructor ( self ) : <EOL> mock = Mock ( return_value = None ) <EOL> self . assertIsNone ( mock . return_value , <EOL> "<STR_LIT>" ) <EOL> def test_repr ( self ) : <EOL> mock = Mock ( name = '<STR_LIT:foo>' ) <EOL> self . assertIn ( '<STR_LIT:foo>' , repr ( mock ) ) <EOL> self . assertIn ( "<STR_LIT>" % id ( mock ) , repr ( mock ) ) <EOL> mocks = [ ( Mock ( ) , '<STR_LIT>' ) , ( Mock ( name = '<STR_LIT:bar>' ) , '<STR_LIT:bar>' ) ] <EOL> for mock , name in mocks : <EOL> self . assertIn ( '<STR_LIT>' % name , repr ( mock . bar ) ) <EOL> self . assertIn ( '<STR_LIT>' % name , repr ( mock . foo ( ) ) ) <EOL> self . assertIn ( '<STR_LIT>' % name , repr ( mock . foo ( ) . bing ) ) <EOL> self . assertIn ( '<STR_LIT>' % name , repr ( mock ( ) ) ) <EOL> self . assertIn ( '<STR_LIT>' % name , repr ( mock ( ) ( ) ) ) <EOL> self . assertIn ( '<STR_LIT>' % name , <EOL> repr ( mock ( ) ( ) . foo . bar . baz ( ) . bing ) ) <EOL> def test_repr_with_spec ( self ) : <EOL> class X ( object ) : <EOL> pass <EOL> mock = Mock ( spec = X ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> mock = Mock ( spec = X ( ) ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> mock = Mock ( spec_set = X ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> mock = Mock ( spec_set = X ( ) ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> mock = Mock ( spec = X , name = '<STR_LIT:foo>' ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> mock = Mock ( name = '<STR_LIT:foo>' ) <EOL> self . assertNotIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> mock = Mock ( ) <EOL> self . assertNotIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> mock = Mock ( spec = [ '<STR_LIT:foo>' ] ) <EOL> self . assertNotIn ( "<STR_LIT>" , repr ( mock ) ) <EOL> def test_side_effect ( self ) : <EOL> mock = Mock ( ) <EOL> def effect ( * args , ** kwargs ) : <EOL> raise SystemError ( '<STR_LIT>' ) <EOL> mock . side_effect = effect <EOL> self . assertRaises ( SystemError , mock , <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> mock . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> results = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> def effect ( ) : <EOL> return results . pop ( ) <EOL> mock . side_effect = effect <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) ] , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> "<STR_LIT>" ) <EOL> mock = Mock ( side_effect = sentinel . SideEffect ) <EOL> self . assertEqual ( mock . side_effect , sentinel . SideEffect , <EOL> "<STR_LIT>" ) <EOL> def side_effect ( ) : <EOL> return DEFAULT <EOL> mock = Mock ( side_effect = side_effect , return_value = sentinel . RETURN ) <EOL> self . assertEqual ( mock ( ) , sentinel . RETURN ) <EOL> @ unittest . skipUnless ( '<STR_LIT>' in sys . platform , <EOL> '<STR_LIT>' ) <EOL> def test_java_exception_side_effect ( self ) : <EOL> import java <EOL> mock = Mock ( side_effect = java . lang . RuntimeException ( "<STR_LIT>" ) ) <EOL> try : <EOL> mock ( <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> except java . lang . RuntimeException : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> mock . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> def test_reset_mock ( self ) : <EOL> parent = Mock ( ) <EOL> spec = [ "<STR_LIT>" ] <EOL> mock = Mock ( name = "<STR_LIT>" , parent = parent , spec = spec ) <EOL> mock ( sentinel . Something , something = sentinel . SomethingElse ) <EOL> something = mock . something <EOL> mock . something ( ) <EOL> mock . side_effect = sentinel . SideEffect <EOL> return_value = mock . return_value <EOL> return_value ( ) <EOL> mock . reset_mock ( ) <EOL> self . assertEqual ( mock . _mock_name , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . _mock_parent , parent , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . _mock_methods , spec , <EOL> "<STR_LIT>" ) <EOL> self . assertFalse ( mock . called , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_count , <NUM_LIT:0> , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args , None , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args_list , [ ] , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . method_calls , [ ] , <EOL> "<STR_LIT>" % <EOL> ( mock . method_calls , [ ] ) ) <EOL> self . assertEqual ( mock . mock_calls , [ ] ) <EOL> self . assertEqual ( mock . side_effect , sentinel . SideEffect , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . return_value , return_value , <EOL> "<STR_LIT>" ) <EOL> self . assertFalse ( return_value . called , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . _mock_children , { '<STR_LIT>' : something } , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . something , something , <EOL> "<STR_LIT>" ) <EOL> self . assertFalse ( mock . something . called , "<STR_LIT>" ) <EOL> def test_reset_mock_recursion ( self ) : <EOL> mock = Mock ( ) <EOL> mock . return_value = mock <EOL> mock . reset_mock ( ) <EOL> def test_call ( self ) : <EOL> mock = Mock ( ) <EOL> self . assertTrue ( is_instance ( mock . return_value , Mock ) , <EOL> "<STR_LIT>" ) <EOL> result = mock ( ) <EOL> self . assertEqual ( mock ( ) , result , <EOL> "<STR_LIT>" ) <EOL> mock . reset_mock ( ) <EOL> ret_val = mock ( sentinel . Arg ) <EOL> self . assertTrue ( mock . called , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_count , <NUM_LIT:1> , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args , ( ( sentinel . Arg , ) , { } ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args_list , [ ( ( sentinel . Arg , ) , { } ) ] , <EOL> "<STR_LIT>" ) <EOL> mock . return_value = sentinel . ReturnValue <EOL> ret_val = mock ( sentinel . Arg , key = sentinel . KeyArg ) <EOL> self . assertEqual ( ret_val , sentinel . ReturnValue , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_count , <NUM_LIT:2> , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args , <EOL> ( ( sentinel . Arg , ) , { '<STR_LIT:key>' : sentinel . KeyArg } ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . call_args_list , [ <EOL> ( ( sentinel . Arg , ) , { } ) , <EOL> ( ( sentinel . Arg , ) , { '<STR_LIT:key>' : sentinel . KeyArg } ) <EOL> ] , <EOL> "<STR_LIT>" ) <EOL> def test_call_args_comparison ( self ) : <EOL> mock = Mock ( ) <EOL> mock ( ) <EOL> mock ( sentinel . Arg ) <EOL> mock ( kw = sentinel . Kwarg ) <EOL> mock ( sentinel . Arg , kw = sentinel . Kwarg ) <EOL> self . assertEqual ( mock . call_args_list , [ <EOL> ( ) , <EOL> ( ( sentinel . Arg , ) , ) , <EOL> ( { "<STR_LIT>" : sentinel . Kwarg } , ) , <EOL> ( ( sentinel . Arg , ) , { "<STR_LIT>" : sentinel . Kwarg } ) <EOL> ] ) <EOL> self . assertEqual ( mock . call_args , <EOL> ( ( sentinel . Arg , ) , { "<STR_LIT>" : sentinel . Kwarg } ) ) <EOL> def test_assert_called_with ( self ) : <EOL> mock = Mock ( ) <EOL> mock ( ) <EOL> mock . assert_called_with ( ) <EOL> self . assertRaises ( AssertionError , mock . assert_called_with , <NUM_LIT:1> ) <EOL> mock . reset_mock ( ) <EOL> self . assertRaises ( AssertionError , mock . assert_called_with ) <EOL> mock ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , a = '<STR_LIT>' , b = '<STR_LIT>' ) <EOL> mock . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , a = '<STR_LIT>' , b = '<STR_LIT>' ) <EOL> def test_assert_called_once_with ( self ) : <EOL> mock = Mock ( ) <EOL> mock ( ) <EOL> mock . assert_called_once_with ( ) <EOL> mock ( ) <EOL> self . assertRaises ( AssertionError , mock . assert_called_once_with ) <EOL> mock . reset_mock ( ) <EOL> self . assertRaises ( AssertionError , mock . assert_called_once_with ) <EOL> mock ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , baz = <NUM_LIT:2> ) <EOL> mock . assert_called_once_with ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , baz = <NUM_LIT:2> ) <EOL> mock . reset_mock ( ) <EOL> mock ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , baz = <NUM_LIT:2> ) <EOL> self . assertRaises ( <EOL> AssertionError , <EOL> lambda : mock . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT:bar>' , baz = <NUM_LIT:2> ) <EOL> ) <EOL> def test_attribute_access_returns_mocks ( self ) : <EOL> mock = Mock ( ) <EOL> something = mock . something <EOL> self . assertTrue ( is_instance ( something , Mock ) , "<STR_LIT>" ) <EOL> self . assertEqual ( mock . something , something , <EOL> "<STR_LIT>" ) <EOL> mock = Mock ( ) <EOL> mock . something . return_value = <NUM_LIT:3> <EOL> self . assertEqual ( mock . something ( ) , <NUM_LIT:3> , "<STR_LIT>" ) <EOL> self . assertTrue ( mock . something . called , <EOL> "<STR_LIT>" ) <EOL> def test_attributes_have_name_and_parent_set ( self ) : <EOL> mock = Mock ( ) <EOL> something = mock . something <EOL> self . assertEqual ( something . _mock_name , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( something . _mock_parent , mock , <EOL> "<STR_LIT>" ) <EOL> def test_method_calls_recorded ( self ) : <EOL> mock = Mock ( ) <EOL> mock . something ( <NUM_LIT:3> , fish = None ) <EOL> mock . something_else . something ( <NUM_LIT:6> , cake = sentinel . Cake ) <EOL> self . assertEqual ( mock . something_else . method_calls , <EOL> [ ( "<STR_LIT>" , ( <NUM_LIT:6> , ) , { '<STR_LIT>' : sentinel . Cake } ) ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( mock . method_calls , [ <EOL> ( "<STR_LIT>" , ( <NUM_LIT:3> , ) , { '<STR_LIT>' : None } ) , <EOL> ( "<STR_LIT>" , ( <NUM_LIT:6> , ) , { '<STR_LIT>' : sentinel . Cake } ) <EOL> ] , <EOL> "<STR_LIT>" ) <EOL> def test_method_calls_compare_easily ( self ) : <EOL> mock = Mock ( ) <EOL> mock . something ( ) <EOL> self . assertEqual ( mock . method_calls , [ ( '<STR_LIT>' , ) ] ) <EOL> self . assertEqual ( mock . method_calls , [ ( '<STR_LIT>' , ( ) , { } ) ] ) <EOL> mock = Mock ( ) <EOL> mock . something ( '<STR_LIT>' ) <EOL> self . assertEqual ( mock . method_calls , [ ( '<STR_LIT>' , ( '<STR_LIT>' , ) ) ] ) <EOL> self . assertEqual ( mock . method_calls , <EOL> [ ( '<STR_LIT>' , ( '<STR_LIT>' , ) , { } ) ] ) <EOL> mock = Mock ( ) <EOL> mock . something ( x = <NUM_LIT:1> ) <EOL> self . assertEqual ( mock . method_calls , [ ( '<STR_LIT>' , { '<STR_LIT:x>' : <NUM_LIT:1> } ) ] ) <EOL> self . assertEqual ( mock . method_calls , [ ( '<STR_LIT>' , ( ) , { '<STR_LIT:x>' : <NUM_LIT:1> } ) ] ) <EOL> mock = Mock ( ) <EOL> mock . something ( '<STR_LIT>' , some = '<STR_LIT>' ) <EOL> self . assertEqual ( mock . method_calls , [ <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> ] ) <EOL> def test_only_allowed_methods_exist ( self ) : <EOL> for spec in [ '<STR_LIT>' ] , ( '<STR_LIT>' , ) : <EOL> for arg in '<STR_LIT>' , '<STR_LIT>' : <EOL> mock = Mock ( ** { arg : spec } ) <EOL> mock . something <EOL> self . assertRaisesRegex ( <EOL> AttributeError , <EOL> "<STR_LIT>" , <EOL> getattr , mock , '<STR_LIT>' <EOL> ) <EOL> def test_from_spec ( self ) : <EOL> class Something ( object ) : <EOL> x = <NUM_LIT:3> <EOL> __something__ = None <EOL> def y ( self ) : <EOL> pass <EOL> def test_attributes ( mock ) : <EOL> mock . x <EOL> mock . y <EOL> mock . __something__ <EOL> self . assertRaisesRegex ( <EOL> AttributeError , <EOL> "<STR_LIT>" , <EOL> getattr , mock , '<STR_LIT:z>' <EOL> ) <EOL> self . assertRaisesRegex ( <EOL> AttributeError , <EOL> "<STR_LIT>" , <EOL> getattr , mock , '<STR_LIT>' <EOL> ) <EOL> test_attributes ( Mock ( spec = Something ) ) <EOL> test_attributes ( Mock ( spec = Something ( ) ) ) <EOL> def test_wraps_calls ( self ) : <EOL> real = Mock ( ) <EOL> mock = Mock ( wraps = real ) <EOL> self . assertEqual ( mock ( ) , real ( ) ) <EOL> real . reset_mock ( ) <EOL> mock ( <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> real . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> def test_wraps_call_with_nondefault_return_value ( self ) : <EOL> real = Mock ( ) <EOL> mock = Mock ( wraps = real ) <EOL> mock . return_value = <NUM_LIT:3> <EOL> self . assertEqual ( mock ( ) , <NUM_LIT:3> ) <EOL> self . assertFalse ( real . called ) <EOL> def test_wraps_attributes ( self ) : <EOL> class Real ( object ) : <EOL> attribute = Mock ( ) <EOL> real = Real ( ) <EOL> mock = Mock ( wraps = real ) <EOL> self . assertEqual ( mock . attribute ( ) , real . attribute ( ) ) <EOL> self . assertRaises ( AttributeError , lambda : mock . fish ) <EOL> self . assertNotEqual ( mock . attribute , real . attribute ) <EOL> result = mock . attribute . frog ( <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> Real . attribute . frog . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , fish = <NUM_LIT:3> ) <EOL> self . assertEqual ( result , Real . attribute . frog ( ) ) <EOL> def test_exceptional_side_effect ( self ) : <EOL> mock = Mock ( side_effect = AttributeError ) <EOL> self . assertRaises ( AttributeError , mock ) <EOL> mock = Mock ( side_effect = AttributeError ( '<STR_LIT:foo>' ) ) <EOL> self . assertRaises ( AttributeError , mock ) <EOL> def test_baseexceptional_side_effect ( self ) : <EOL> mock = Mock ( side_effect = KeyboardInterrupt ) <EOL> self . assertRaises ( KeyboardInterrupt , mock ) <EOL> mock = Mock ( side_effect = KeyboardInterrupt ( '<STR_LIT:foo>' ) ) <EOL> self . assertRaises ( KeyboardInterrupt , mock ) <EOL> def test_assert_called_with_message ( self ) : <EOL> mock = Mock ( ) <EOL> self . assertRaisesRegex ( AssertionError , '<STR_LIT>' , <EOL> mock . assert_called_with ) <EOL> def test__name__ ( self ) : <EOL> mock = Mock ( ) <EOL> self . assertRaises ( AttributeError , lambda : mock . __name__ ) <EOL> mock . __name__ = '<STR_LIT:foo>' <EOL> self . assertEqual ( mock . __name__ , '<STR_LIT:foo>' ) <EOL> def test_spec_list_subclass ( self ) : <EOL> class Sub ( list ) : <EOL> pass <EOL> mock = Mock ( spec = Sub ( [ '<STR_LIT:foo>' ] ) ) <EOL> mock . append ( <NUM_LIT:3> ) <EOL> mock . append . assert_called_with ( <NUM_LIT:3> ) <EOL> self . assertRaises ( AttributeError , getattr , mock , '<STR_LIT:foo>' ) <EOL> def test_spec_class ( self ) : <EOL> class X ( object ) : <EOL> pass <EOL> mock = Mock ( spec = X ) <EOL> self . assertTrue ( isinstance ( mock , X ) ) <EOL> mock = Mock ( spec = X ( ) ) <EOL> self . assertTrue ( isinstance ( mock , X ) ) <EOL> self . assertIs ( mock . __class__ , X ) <EOL> self . assertEqual ( Mock ( ) . __class__ . __name__ , '<STR_LIT>' ) <EOL> mock = Mock ( spec_set = X ) <EOL> self . assertTrue ( isinstance ( mock , X ) ) <EOL> mock = Mock ( spec_set = X ( ) ) <EOL> self . assertTrue ( isinstance ( mock , X ) ) <EOL> def test_setting_attribute_with_spec_set ( self ) : <EOL> class X ( object ) : <EOL> y = <NUM_LIT:3> <EOL> mock = Mock ( spec = X ) <EOL> mock . x = '<STR_LIT:foo>' <EOL> mock = Mock ( spec_set = X ) <EOL> def set_attr ( ) : <EOL> mock . x = '<STR_LIT:foo>' <EOL> mock . y = '<STR_LIT:foo>' <EOL> self . assertRaises ( AttributeError , set_attr ) <EOL> def test_copy ( self ) : <EOL> current = sys . getrecursionlimit ( ) <EOL> self . addCleanup ( sys . setrecursionlimit , current ) <EOL> sys . setrecursionlimit ( int ( <NUM_LIT> ) ) <EOL> copy . copy ( Mock ( ) ) <EOL> def test_subclass_with_properties ( self ) : <EOL> class SubClass ( Mock ) : <EOL> def _get ( self ) : <EOL> return <NUM_LIT:3> <EOL> def _set ( self , value ) : <EOL> raise NameError ( '<STR_LIT>' ) <EOL> some_attribute = property ( _get , _set ) <EOL> s = SubClass ( spec_set = SubClass ) <EOL> self . assertEqual ( s . some_attribute , <NUM_LIT:3> ) <EOL> def test ( ) : <EOL> s . some_attribute = <NUM_LIT:3> <EOL> self . assertRaises ( NameError , test ) <EOL> def test ( ) : <EOL> s . foo = '<STR_LIT:bar>' <EOL> self . assertRaises ( AttributeError , test ) <EOL> def test_setting_call ( self ) : <EOL> mock = Mock ( ) <EOL> def __call__ ( self , a ) : <EOL> return self . _mock_call ( a ) <EOL> type ( mock ) . __call__ = __call__ <EOL> mock ( '<STR_LIT>' ) <EOL> mock . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertRaises ( TypeError , mock , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_dir ( self ) : <EOL> mock = Mock ( ) <EOL> attrs = set ( dir ( mock ) ) <EOL> type_attrs = set ( [ m for m in dir ( Mock ) if not m . startswith ( '<STR_LIT:_>' ) ] ) <EOL> self . assertEqual ( set ( ) , type_attrs - attrs ) <EOL> mock . a , mock . b <EOL> self . assertIn ( '<STR_LIT:a>' , dir ( mock ) ) <EOL> self . assertIn ( '<STR_LIT:b>' , dir ( mock ) ) <EOL> mock . c = mock . d = None <EOL> self . assertIn ( '<STR_LIT:c>' , dir ( mock ) ) <EOL> self . assertIn ( '<STR_LIT:d>' , dir ( mock ) ) <EOL> mock . __iter__ = lambda s : iter ( [ ] ) <EOL> self . assertIn ( '<STR_LIT>' , dir ( mock ) ) <EOL> def test_dir_from_spec ( self ) : <EOL> mock = Mock ( spec = unittest . TestCase ) <EOL> testcase_attrs = set ( dir ( unittest . TestCase ) ) <EOL> attrs = set ( dir ( mock ) ) <EOL> self . assertEqual ( set ( ) , testcase_attrs - attrs ) <EOL> mock . version = <NUM_LIT:3> <EOL> self . assertEqual ( dir ( mock ) . count ( '<STR_LIT:version>' ) , <NUM_LIT:1> ) <EOL> def test_filter_dir ( self ) : <EOL> patcher = patch . object ( mock , '<STR_LIT>' , False ) <EOL> patcher . start ( ) <EOL> try : <EOL> attrs = set ( dir ( Mock ( ) ) ) <EOL> type_attrs = set ( dir ( Mock ) ) <EOL> self . assertEqual ( set ( ) , type_attrs - attrs ) <EOL> finally : <EOL> patcher . stop ( ) <EOL> def test_configure_mock ( self ) : <EOL> mock = Mock ( foo = '<STR_LIT:bar>' ) <EOL> self . assertEqual ( mock . foo , '<STR_LIT:bar>' ) <EOL> mock = MagicMock ( foo = '<STR_LIT:bar>' ) <EOL> self . assertEqual ( mock . foo , '<STR_LIT:bar>' ) <EOL> kwargs = { '<STR_LIT>' : KeyError , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:foo>' : MagicMock ( ) } <EOL> mock = Mock ( ** kwargs ) <EOL> self . assertRaises ( KeyError , mock ) <EOL> self . assertEqual ( mock . foo . bar ( ) , <NUM_LIT> ) <EOL> self . assertIsInstance ( mock . foo , MagicMock ) <EOL> mock = Mock ( ) <EOL> mock . configure_mock ( ** kwargs ) <EOL> self . assertRaises ( KeyError , mock ) <EOL> self . assertEqual ( mock . foo . bar ( ) , <NUM_LIT> ) <EOL> self . assertIsInstance ( mock . foo , MagicMock ) <EOL> def assertRaisesWithMsg ( self , exception , message , func , * args , ** kwargs ) : <EOL> try : <EOL> func ( * args , ** kwargs ) <EOL> except : <EOL> instance = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . assertIsInstance ( instance , exception ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' % ( exception , ) ) <EOL> msg = str ( instance ) <EOL> self . assertEqual ( msg , message ) <EOL> def test_assert_called_with_failure_message ( self ) : <EOL> mock = NonCallableMock ( ) <EOL> expected = "<STR_LIT>" <EOL> message = '<STR_LIT>' <EOL> self . assertRaisesWithMsg ( <EOL> AssertionError , message % ( expected , ) , <EOL> mock . assert_called_with , <NUM_LIT:1> , '<STR_LIT:2>' , <NUM_LIT:3> , bar = '<STR_LIT:foo>' <EOL> ) <EOL> mock . foo ( <NUM_LIT:1> , '<STR_LIT:2>' , <NUM_LIT:3> , foo = '<STR_LIT:foo>' ) <EOL> asserters = [ <EOL> mock . foo . assert_called_with , mock . foo . assert_called_once_with <EOL> ] <EOL> for meth in asserters : <EOL> actual = "<STR_LIT>" <EOL> expected = "<STR_LIT>" <EOL> message = '<STR_LIT>' <EOL> self . assertRaisesWithMsg ( <EOL> AssertionError , message % ( expected , actual ) , <EOL> meth , <NUM_LIT:1> , '<STR_LIT:2>' , <NUM_LIT:3> , bar = '<STR_LIT:foo>' <EOL> ) <EOL> for meth in asserters : <EOL> actual = "<STR_LIT>" <EOL> expected = "<STR_LIT>" <EOL> message = '<STR_LIT>' <EOL> self . assertRaisesWithMsg ( <EOL> AssertionError , message % ( expected , actual ) , <EOL> meth , bar = '<STR_LIT:foo>' <EOL> ) <EOL> for meth in asserters : <EOL> actual = "<STR_LIT>" <EOL> expected = "<STR_LIT>" <EOL> message = '<STR_LIT>' <EOL> self . assertRaisesWithMsg ( <EOL> AssertionError , message % ( expected , actual ) , <EOL> meth , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> <EOL> ) <EOL> for meth in asserters : <EOL> actual = "<STR_LIT>" <EOL> expected = "<STR_LIT>" <EOL> message = '<STR_LIT>' <EOL> self . assertRaisesWithMsg ( <EOL> AssertionError , message % ( expected , actual ) , meth <EOL> ) <EOL> def test_mock_calls ( self ) : <EOL> mock = MagicMock ( ) <EOL> self . assertIs ( mock . mock_calls == [ ] , True ) <EOL> mock = MagicMock ( ) <EOL> mock ( ) <EOL> expected = [ ( '<STR_LIT>' , ( ) , { } ) ] <EOL> self . assertEqual ( mock . mock_calls , expected ) <EOL> mock . foo ( ) <EOL> expected . append ( call . foo ( ) ) <EOL> self . assertEqual ( mock . mock_calls , expected ) <EOL> self . assertEqual ( mock . foo . mock_calls , [ ( '<STR_LIT>' , ( ) , { } ) ] ) <EOL> mock = MagicMock ( ) <EOL> mock ( ) . foo ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , a = <NUM_LIT:4> , b = <NUM_LIT:5> ) <EOL> expected = [ <EOL> ( '<STR_LIT>' , ( ) , { } ) , ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , dict ( a = <NUM_LIT:4> , b = <NUM_LIT:5> ) ) <EOL> ] <EOL> self . assertEqual ( mock . mock_calls , expected ) <EOL> self . assertEqual ( mock . return_value . foo . mock_calls , <EOL> [ ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , dict ( a = <NUM_LIT:4> , b = <NUM_LIT:5> ) ) ] ) <EOL> self . assertEqual ( mock . return_value . mock_calls , <EOL> [ ( '<STR_LIT:foo>' , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , dict ( a = <NUM_LIT:4> , b = <NUM_LIT:5> ) ) ] ) <EOL> mock = MagicMock ( ) <EOL> mock ( ) . foo . bar ( ) . baz ( ) <EOL> expected = [ <EOL> ( '<STR_LIT>' , ( ) , { } ) , ( '<STR_LIT>' , ( ) , { } ) , <EOL> ( '<STR_LIT>' , ( ) , { } ) <EOL> ] <EOL> self . assertEqual ( mock . mock_calls , expected ) <EOL> self . assertEqual ( mock ( ) . mock_calls , <EOL> call . foo . bar ( ) . baz ( ) . call_list ( ) ) <EOL> for kwargs in dict ( ) , dict ( name = '<STR_LIT:bar>' ) : <EOL> mock = MagicMock ( ** kwargs ) <EOL> int ( mock . foo ) <EOL> expected = [ ( '<STR_LIT>' , ( ) , { } ) ] <EOL> self . assertEqual ( mock . mock_calls , expected ) <EOL> mock = MagicMock ( ** kwargs ) <EOL> mock . a ( ) ( ) <EOL> expected = [ ( '<STR_LIT:a>' , ( ) , { } ) , ( '<STR_LIT>' , ( ) , { } ) ] <EOL> self . assertEqual ( mock . mock_calls , expected ) <EOL> self . assertEqual ( mock . a ( ) . mock_calls , [ call ( ) ] ) <EOL> mock = MagicMock ( ** kwargs ) <EOL> mock ( <NUM_LIT:1> ) ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) <EOL> self . assertEqual ( mock . mock_calls , call ( <NUM_LIT:1> ) ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) . call_list ( ) ) <EOL> self . assertEqual ( mock ( ) . mock_calls , call ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) . call_list ( ) ) <EOL> self . assertEqual ( mock ( ) ( ) . mock_calls , call ( <NUM_LIT:3> ) . call_list ( ) ) <EOL> mock = MagicMock ( ** kwargs ) <EOL> mock ( <NUM_LIT:1> ) ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) . a . b . c ( <NUM_LIT:4> ) <EOL> self . assertEqual ( mock . mock_calls , <EOL> call ( <NUM_LIT:1> ) ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) . a . b . c ( <NUM_LIT:4> ) . call_list ( ) ) <EOL> self . assertEqual ( mock ( ) . mock_calls , <EOL> call ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) . a . b . c ( <NUM_LIT:4> ) . call_list ( ) ) <EOL> self . assertEqual ( mock ( ) ( ) . mock_calls , <EOL> call ( <NUM_LIT:3> ) . a . b . c ( <NUM_LIT:4> ) . call_list ( ) ) <EOL> mock = MagicMock ( ** kwargs ) <EOL> int ( mock ( ) . foo . bar ( ) . baz ( ) ) <EOL> last_call = ( '<STR_LIT>' , ( ) , { } ) <EOL> self . assertEqual ( mock . mock_calls [ - <NUM_LIT:1> ] , last_call ) <EOL> self . assertEqual ( mock ( ) . mock_calls , <EOL> call . foo . bar ( ) . baz ( ) . __int__ ( ) . call_list ( ) ) <EOL> self . assertEqual ( mock ( ) . foo . bar ( ) . mock_calls , <EOL> call . baz ( ) . __int__ ( ) . call_list ( ) ) <EOL> self . assertEqual ( mock ( ) . foo . bar ( ) . baz . mock_calls , <EOL> call ( ) . __int__ ( ) . call_list ( ) ) <EOL> def test_subclassing ( self ) : <EOL> class Subclass ( Mock ) : <EOL> pass <EOL> mock = Subclass ( ) <EOL> self . assertIsInstance ( mock . foo , Subclass ) <EOL> self . assertIsInstance ( mock ( ) , Subclass ) <EOL> class Subclass ( Mock ) : <EOL> def _get_child_mock ( self , ** kwargs ) : <EOL> return Mock ( ** kwargs ) <EOL> mock = Subclass ( ) <EOL> self . assertNotIsInstance ( mock . foo , Subclass ) <EOL> self . assertNotIsInstance ( mock ( ) , Subclass ) <EOL> def test_arg_lists ( self ) : <EOL> mocks = [ <EOL> Mock ( ) , <EOL> MagicMock ( ) , <EOL> NonCallableMock ( ) , <EOL> NonCallableMagicMock ( ) <EOL> ] <EOL> def assert_attrs ( mock ) : <EOL> names = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> for name in names : <EOL> attr = getattr ( mock , name ) <EOL> self . assertIsInstance ( attr , _CallList ) <EOL> self . assertIsInstance ( attr , list ) <EOL> self . assertEqual ( attr , [ ] ) <EOL> for mock in mocks : <EOL> assert_attrs ( mock ) <EOL> if callable ( mock ) : <EOL> mock ( ) <EOL> mock ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> mock ( a = <NUM_LIT:3> ) <EOL> mock . reset_mock ( ) <EOL> assert_attrs ( mock ) <EOL> mock . foo ( ) <EOL> mock . foo . bar ( <NUM_LIT:1> , a = <NUM_LIT:3> ) <EOL> mock . foo ( <NUM_LIT:1> ) . bar ( ) . baz ( <NUM_LIT:3> ) <EOL> mock . reset_mock ( ) <EOL> assert_attrs ( mock ) <EOL> def test_call_args_two_tuple ( self ) : <EOL> mock = Mock ( ) <EOL> mock ( <NUM_LIT:1> , a = <NUM_LIT:3> ) <EOL> mock ( <NUM_LIT:2> , b = <NUM_LIT:4> ) <EOL> self . assertEqual ( len ( mock . call_args ) , <NUM_LIT:2> ) <EOL> args , kwargs = mock . call_args <EOL> self . assertEqual ( args , ( <NUM_LIT:2> , ) ) <EOL> self . assertEqual ( kwargs , dict ( b = <NUM_LIT:4> ) ) <EOL> expected_list = [ ( ( <NUM_LIT:1> , ) , dict ( a = <NUM_LIT:3> ) ) , ( ( <NUM_LIT:2> , ) , dict ( b = <NUM_LIT:4> ) ) ] <EOL> for expected , call_args in zip ( expected_list , mock . call_args_list ) : <EOL> self . assertEqual ( len ( call_args ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( expected [ <NUM_LIT:0> ] , call_args [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( expected [ <NUM_LIT:1> ] , call_args [ <NUM_LIT:1> ] ) <EOL> def test_side_effect_iterator ( self ) : <EOL> mock = Mock ( side_effect = iter ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertRaises ( StopIteration , mock ) <EOL> mock = MagicMock ( side_effect = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) ] , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . assertRaises ( StopIteration , mock ) <EOL> mock = Mock ( side_effect = '<STR_LIT>' ) <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) ] , [ '<STR_LIT:g>' , '<STR_LIT:h>' , '<STR_LIT:i>' ] ) <EOL> self . assertRaises ( StopIteration , mock ) <EOL> class Foo ( object ) : <EOL> pass <EOL> mock = MagicMock ( side_effect = Foo ) <EOL> self . assertIsInstance ( mock ( ) , Foo ) <EOL> mock = Mock ( side_effect = Iter ( ) ) <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) , mock ( ) ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertRaises ( StopIteration , mock ) <EOL> def test_side_effect_iterator_exceptions ( self ) : <EOL> for Klass in Mock , MagicMock : <EOL> iterable = ( ValueError , <NUM_LIT:3> , KeyError , <NUM_LIT:6> ) <EOL> m = Klass ( side_effect = iterable ) <EOL> self . assertRaises ( ValueError , m ) <EOL> self . assertEqual ( m ( ) , <NUM_LIT:3> ) <EOL> self . assertRaises ( KeyError , m ) <EOL> self . assertEqual ( m ( ) , <NUM_LIT:6> ) <EOL> def test_side_effect_setting_iterator ( self ) : <EOL> mock = Mock ( ) <EOL> mock . side_effect = iter ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertRaises ( StopIteration , mock ) <EOL> side_effect = mock . side_effect <EOL> self . assertIsInstance ( side_effect , type ( iter ( [ ] ) ) ) <EOL> mock . side_effect = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) ] , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . assertRaises ( StopIteration , mock ) <EOL> side_effect = mock . side_effect <EOL> self . assertIsInstance ( side_effect , type ( iter ( [ ] ) ) ) <EOL> this_iter = Iter ( ) <EOL> mock . side_effect = this_iter <EOL> self . assertEqual ( [ mock ( ) , mock ( ) , mock ( ) , mock ( ) ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertRaises ( StopIteration , mock ) <EOL> self . assertIs ( mock . side_effect , this_iter ) <EOL> def test_assert_has_calls_any_order ( self ) : <EOL> mock = Mock ( ) <EOL> mock ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> mock ( a = <NUM_LIT:3> ) <EOL> mock ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> mock ( b = <NUM_LIT:6> ) <EOL> mock ( b = <NUM_LIT:6> ) <EOL> kalls = [ <EOL> call ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( { '<STR_LIT:a>' : <NUM_LIT:3> } , ) , <EOL> ( ( <NUM_LIT:3> , <NUM_LIT:4> ) , ) , ( ( ) , { '<STR_LIT:a>' : <NUM_LIT:3> } ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) , ( '<STR_LIT>' , { '<STR_LIT:a>' : <NUM_LIT:3> } ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> ) , { } ) , ( '<STR_LIT>' , ( ) , { '<STR_LIT:a>' : <NUM_LIT:3> } ) <EOL> ] <EOL> for kall in kalls : <EOL> mock . assert_has_calls ( [ kall ] , any_order = True ) <EOL> for kall in call ( <NUM_LIT:1> , '<STR_LIT:2>' ) , call ( b = <NUM_LIT:3> ) , call ( ) , <NUM_LIT:3> , None , '<STR_LIT:foo>' : <EOL> self . assertRaises ( <EOL> AssertionError , mock . assert_has_calls , <EOL> [ kall ] , any_order = True <EOL> ) <EOL> kall_lists = [ <EOL> [ call ( <NUM_LIT:1> , <NUM_LIT:2> ) , call ( b = <NUM_LIT:6> ) ] , <EOL> [ call ( <NUM_LIT:3> , <NUM_LIT:4> ) , call ( <NUM_LIT:1> , <NUM_LIT:2> ) ] , <EOL> [ call ( b = <NUM_LIT:6> ) , call ( b = <NUM_LIT:6> ) ] , <EOL> ] <EOL> for kall_list in kall_lists : <EOL> mock . assert_has_calls ( kall_list , any_order = True ) <EOL> kall_lists = [ <EOL> [ call ( b = <NUM_LIT:6> ) , call ( b = <NUM_LIT:6> ) , call ( b = <NUM_LIT:6> ) ] , <EOL> [ call ( <NUM_LIT:1> , <NUM_LIT:2> ) , call ( <NUM_LIT:1> , <NUM_LIT:2> ) ] , <EOL> [ call ( <NUM_LIT:3> , <NUM_LIT:4> ) , call ( <NUM_LIT:1> , <NUM_LIT:2> ) , call ( <NUM_LIT:5> , <NUM_LIT:7> ) ] , <EOL> [ call ( b = <NUM_LIT:6> ) , call ( <NUM_LIT:3> , <NUM_LIT:4> ) , call ( b = <NUM_LIT:6> ) , call ( <NUM_LIT:1> , <NUM_LIT:2> ) , call ( b = <NUM_LIT:6> ) ] , <EOL> ] <EOL> for kall_list in kall_lists : <EOL> self . assertRaises ( <EOL> AssertionError , mock . assert_has_calls , <EOL> kall_list , any_order = True <EOL> ) <EOL> def test_assert_has_calls ( self ) : <EOL> kalls1 = [ <EOL> call ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( { '<STR_LIT:a>' : <NUM_LIT:3> } , ) , <EOL> ( ( <NUM_LIT:3> , <NUM_LIT:4> ) , ) , call ( b = <NUM_LIT:6> ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:1> , ) , { '<STR_LIT:b>' : <NUM_LIT:6> } ) , <EOL> ] <EOL> kalls2 = [ call . foo ( ) , call . bar ( <NUM_LIT:1> ) ] <EOL> kalls2 . extend ( call . spam ( ) . baz ( a = <NUM_LIT:3> ) . call_list ( ) ) <EOL> kalls2 . extend ( call . bam ( set ( ) , foo = { } ) . fish ( [ <NUM_LIT:1> ] ) . call_list ( ) ) <EOL> mocks = [ ] <EOL> for mock in Mock ( ) , MagicMock ( ) : <EOL> mock ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> mock ( a = <NUM_LIT:3> ) <EOL> mock ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> mock ( b = <NUM_LIT:6> ) <EOL> mock ( <NUM_LIT:1> , b = <NUM_LIT:6> ) <EOL> mocks . append ( ( mock , kalls1 ) ) <EOL> mock = Mock ( ) <EOL> mock . foo ( ) <EOL> mock . bar ( <NUM_LIT:1> ) <EOL> mock . spam ( ) . baz ( a = <NUM_LIT:3> ) <EOL> mock . bam ( set ( ) , foo = { } ) . fish ( [ <NUM_LIT:1> ] ) <EOL> mocks . append ( ( mock , kalls2 ) ) <EOL> for mock , kalls in mocks : <EOL> for i in range ( len ( kalls ) ) : <EOL> for step in <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> : <EOL> these = kalls [ i : i + step ] <EOL> mock . assert_has_calls ( these ) <EOL> if len ( these ) > <NUM_LIT:1> : <EOL> self . assertRaises ( <EOL> AssertionError , <EOL> mock . assert_has_calls , <EOL> list ( reversed ( these ) ) <EOL> ) <EOL> def test_assert_any_call ( self ) : <EOL> mock = Mock ( ) <EOL> mock ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> mock ( a = <NUM_LIT:3> ) <EOL> mock ( <NUM_LIT:1> , b = <NUM_LIT:6> ) <EOL> mock . assert_any_call ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> mock . assert_any_call ( a = <NUM_LIT:3> ) <EOL> mock . assert_any_call ( <NUM_LIT:1> , b = <NUM_LIT:6> ) <EOL> self . assertRaises ( <EOL> AssertionError , <EOL> mock . assert_any_call <EOL> ) <EOL> self . assertRaises ( <EOL> AssertionError , <EOL> mock . assert_any_call , <EOL> <NUM_LIT:1> , <NUM_LIT:3> <EOL> ) <EOL> self . assertRaises ( <EOL> AssertionError , <EOL> mock . assert_any_call , <EOL> a = <NUM_LIT:4> <EOL> ) <EOL> def test_mock_calls_create_autospec ( self ) : <EOL> def f ( a , b ) : <EOL> pass <EOL> obj = Iter ( ) <EOL> obj . f = f <EOL> funcs = [ <EOL> create_autospec ( f ) , <EOL> create_autospec ( obj ) . f <EOL> ] <EOL> for func in funcs : <EOL> func ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> func ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertEqual ( <EOL> func . mock_calls , [ call ( <NUM_LIT:1> , <NUM_LIT:2> ) , call ( <NUM_LIT:3> , <NUM_LIT:4> ) ] <EOL> ) <EOL> def test_mock_add_spec ( self ) : <EOL> class _One ( object ) : <EOL> one = <NUM_LIT:1> <EOL> class _Two ( object ) : <EOL> two = <NUM_LIT:2> <EOL> class Anything ( object ) : <EOL> one = two = three = '<STR_LIT>' <EOL> klasses = [ <EOL> Mock , MagicMock , NonCallableMock , NonCallableMagicMock <EOL> ] <EOL> for Klass in list ( klasses ) : <EOL> klasses . append ( lambda K = Klass : K ( spec = Anything ) ) <EOL> klasses . append ( lambda K = Klass : K ( spec_set = Anything ) ) <EOL> for Klass in klasses : <EOL> for kwargs in dict ( ) , dict ( spec_set = True ) : <EOL> mock = Klass ( ) <EOL> mock . one , mock . two , mock . three <EOL> for One , Two in [ ( _One , _Two ) , ( [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) ] : <EOL> for kwargs in dict ( ) , dict ( spec_set = True ) : <EOL> mock . mock_add_spec ( One , ** kwargs ) <EOL> mock . one <EOL> self . assertRaises ( <EOL> AttributeError , getattr , mock , '<STR_LIT>' <EOL> ) <EOL> self . assertRaises ( <EOL> AttributeError , getattr , mock , '<STR_LIT>' <EOL> ) <EOL> if '<STR_LIT>' in kwargs : <EOL> self . assertRaises ( <EOL> AttributeError , setattr , mock , '<STR_LIT>' , None <EOL> ) <EOL> mock . mock_add_spec ( Two , ** kwargs ) <EOL> self . assertRaises ( <EOL> AttributeError , getattr , mock , '<STR_LIT>' <EOL> ) <EOL> mock . two <EOL> self . assertRaises ( <EOL> AttributeError , getattr , mock , '<STR_LIT>' <EOL> ) <EOL> if '<STR_LIT>' in kwargs : <EOL> self . assertRaises ( <EOL> AttributeError , setattr , mock , '<STR_LIT>' , None <EOL> ) <EOL> def test_mock_add_spec_magic_methods ( self ) : <EOL> for Klass in MagicMock , NonCallableMagicMock : <EOL> mock = Klass ( ) <EOL> int ( mock ) <EOL> mock . mock_add_spec ( object ) <EOL> self . assertRaises ( TypeError , int , mock ) <EOL> mock = Klass ( ) <EOL> mock [ '<STR_LIT:foo>' ] <EOL> mock . __int__ . return_value = <NUM_LIT:4> <EOL> mock . mock_add_spec ( int ) <EOL> self . assertEqual ( int ( mock ) , <NUM_LIT:4> ) <EOL> self . assertRaises ( TypeError , lambda : mock [ '<STR_LIT:foo>' ] ) <EOL> def test_adding_child_mock ( self ) : <EOL> for Klass in NonCallableMock , Mock , MagicMock , NonCallableMagicMock : <EOL> mock = Klass ( ) <EOL> mock . foo = Mock ( ) <EOL> mock . foo ( ) <EOL> self . assertEqual ( mock . method_calls , [ call . foo ( ) ] ) <EOL> self . assertEqual ( mock . mock_calls , [ call . foo ( ) ] ) <EOL> mock = Klass ( ) <EOL> mock . bar = Mock ( name = '<STR_LIT:name>' ) <EOL> mock . bar ( ) <EOL> self . assertEqual ( mock . method_calls , [ ] ) <EOL> self . assertEqual ( mock . mock_calls , [ ] ) <EOL> mock = Klass ( ) <EOL> mock . baz = MagicMock ( ) ( ) <EOL> mock . baz ( ) <EOL> self . assertEqual ( mock . method_calls , [ ] ) <EOL> self . assertEqual ( mock . mock_calls , [ ] ) <EOL> def test_adding_return_value_mock ( self ) : <EOL> for Klass in Mock , MagicMock : <EOL> mock = Klass ( ) <EOL> mock . return_value = MagicMock ( ) <EOL> mock ( ) ( ) <EOL> self . assertEqual ( mock . mock_calls , [ call ( ) , call ( ) ( ) ] ) <EOL> def test_manager_mock ( self ) : <EOL> class Foo ( object ) : <EOL> one = '<STR_LIT>' <EOL> two = '<STR_LIT>' <EOL> manager = Mock ( ) <EOL> p1 = patch . object ( Foo , '<STR_LIT>' ) <EOL> p2 = patch . object ( Foo , '<STR_LIT>' ) <EOL> mock_one = p1 . start ( ) <EOL> self . addCleanup ( p1 . stop ) <EOL> mock_two = p2 . start ( ) <EOL> self . addCleanup ( p2 . stop ) <EOL> manager . attach_mock ( mock_one , '<STR_LIT>' ) <EOL> manager . attach_mock ( mock_two , '<STR_LIT>' ) <EOL> Foo . two ( ) <EOL> Foo . one ( ) <EOL> self . assertEqual ( manager . mock_calls , [ call . two ( ) , call . one ( ) ] ) <EOL> def test_magic_methods_mock_calls ( self ) : <EOL> for Klass in Mock , MagicMock : <EOL> m = Klass ( ) <EOL> m . __int__ = Mock ( return_value = <NUM_LIT:3> ) <EOL> m . __float__ = MagicMock ( return_value = <NUM_LIT> ) <EOL> int ( m ) <EOL> float ( m ) <EOL> self . assertEqual ( m . mock_calls , [ call . __int__ ( ) , call . __float__ ( ) ] ) <EOL> self . assertEqual ( m . method_calls , [ ] ) <EOL> def test_attribute_deletion ( self ) : <EOL> for Klass in Mock , MagicMock , NonCallableMagicMock , NonCallableMock : <EOL> m = Klass ( ) <EOL> original = m . foo <EOL> m . foo = <NUM_LIT:3> <EOL> del m . foo <EOL> self . assertEqual ( m . foo , original ) <EOL> new = m . foo = Mock ( ) <EOL> del m . foo <EOL> self . assertEqual ( m . foo , new ) <EOL> def test_mock_parents ( self ) : <EOL> for Klass in Mock , MagicMock : <EOL> m = Klass ( ) <EOL> original_repr = repr ( m ) <EOL> m . return_value = m <EOL> self . assertIs ( m ( ) , m ) <EOL> self . assertEqual ( repr ( m ) , original_repr ) <EOL> m . reset_mock ( ) <EOL> self . assertIs ( m ( ) , m ) <EOL> self . assertEqual ( repr ( m ) , original_repr ) <EOL> m = Klass ( ) <EOL> m . b = m . a <EOL> self . assertIn ( "<STR_LIT>" , repr ( m . b ) ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( m . a ) ) <EOL> m . reset_mock ( ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( m . b ) ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( m . a ) ) <EOL> m = Klass ( ) <EOL> original_repr = repr ( m ) <EOL> m . a = m ( ) <EOL> m . a . return_value = m <EOL> self . assertEqual ( repr ( m ) , original_repr ) <EOL> self . assertEqual ( repr ( m . a ( ) ) , original_repr ) <EOL> def test_attach_mock ( self ) : <EOL> classes = Mock , MagicMock , NonCallableMagicMock , NonCallableMock <EOL> for Klass in classes : <EOL> for Klass2 in classes : <EOL> m = Klass ( ) <EOL> m2 = Klass2 ( name = '<STR_LIT:foo>' ) <EOL> m . attach_mock ( m2 , '<STR_LIT:bar>' ) <EOL> self . assertIs ( m . bar , m2 ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( m2 ) ) <EOL> m . bar . baz ( <NUM_LIT:1> ) <EOL> self . assertEqual ( m . mock_calls , [ call . bar . baz ( <NUM_LIT:1> ) ] ) <EOL> self . assertEqual ( m . method_calls , [ call . bar . baz ( <NUM_LIT:1> ) ] ) <EOL> def test_attach_mock_return_value ( self ) : <EOL> classes = Mock , MagicMock , NonCallableMagicMock , NonCallableMock <EOL> for Klass in Mock , MagicMock : <EOL> for Klass2 in classes : <EOL> m = Klass ( ) <EOL> m2 = Klass2 ( name = '<STR_LIT:foo>' ) <EOL> m . attach_mock ( m2 , '<STR_LIT>' ) <EOL> self . assertIs ( m ( ) , m2 ) <EOL> self . assertIn ( "<STR_LIT>" , repr ( m2 ) ) <EOL> m2 . foo ( ) <EOL> self . assertEqual ( m . mock_calls , call ( ) . foo ( ) . call_list ( ) ) <EOL> def test_attribute_deletion ( self ) : <EOL> for mock in Mock ( ) , MagicMock ( ) : <EOL> self . assertTrue ( hasattr ( mock , '<STR_LIT:m>' ) ) <EOL> del mock . m <EOL> self . assertFalse ( hasattr ( mock , '<STR_LIT:m>' ) ) <EOL> del mock . f <EOL> self . assertFalse ( hasattr ( mock , '<STR_LIT:f>' ) ) <EOL> self . assertRaises ( AttributeError , getattr , mock , '<STR_LIT:f>' ) <EOL> def test_class_assignable ( self ) : <EOL> for mock in Mock ( ) , MagicMock ( ) : <EOL> self . assertNotIsInstance ( mock , int ) <EOL> mock . __class__ = int <EOL> self . assertIsInstance ( mock , int ) <EOL> mock . foo <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
