<s> import inspect <EOL> from tests . support import unittest2 <EOL> from mock import Mock , mocksignature , patch <EOL> class Something ( object ) : <EOL> def __init__ ( self , foo , bar = <NUM_LIT:10> ) : <EOL> pass <EOL> def __call__ ( self , foo , bar = <NUM_LIT:5> ) : <EOL> pass <EOL> something = Something ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def f ( a , b , c ) : <EOL> pass <EOL> class TestMockSignature ( unittest2 . TestCase ) : <EOL> def test_function ( self ) : <EOL> def f ( a ) : <EOL> pass <EOL> mock = Mock ( ) <EOL> f2 = mocksignature ( f , mock ) <EOL> self . assertIs ( f2 . mock , mock ) <EOL> self . assertRaises ( TypeError , f2 ) <EOL> mock . return_value = <NUM_LIT:3> <EOL> self . assertEqual ( f2 ( '<STR_LIT:foo>' ) , <NUM_LIT:3> ) <EOL> mock . assert_called_with ( '<STR_LIT:foo>' ) <EOL> f2 . mock . assert_called_with ( '<STR_LIT:foo>' ) <EOL> def test_function_without_explicit_mock ( self ) : <EOL> def f ( a ) : <EOL> pass <EOL> f2 = mocksignature ( f ) <EOL> self . assertIsInstance ( f2 . mock , Mock ) <EOL> self . assertRaises ( TypeError , f2 ) <EOL> f2 . mock . return_value = <NUM_LIT:3> <EOL> self . assertEqual ( f2 ( '<STR_LIT:foo>' ) , <NUM_LIT:3> ) <EOL> f2 . mock . assert_called_with ( '<STR_LIT:foo>' ) <EOL> def test_method ( self ) : <EOL> class Foo ( object ) : <EOL> def method ( self , a , b ) : <EOL> pass <EOL> f = Foo ( ) <EOL> mock = Mock ( ) <EOL> mock . return_value = <NUM_LIT:3> <EOL> f . method = mocksignature ( f . method , mock ) <EOL> self . assertEqual ( f . method ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , <NUM_LIT:3> ) <EOL> mock . assert_called_with ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> def test_function_with_defaults ( self ) : <EOL> def f ( a , b = None ) : <EOL> pass <EOL> mock = Mock ( ) <EOL> f2 = mocksignature ( f , mock ) <EOL> f2 ( <NUM_LIT:3> ) <EOL> mock . assert_called_with ( <NUM_LIT:3> , None ) <EOL> mock . reset_mock ( ) <EOL> f2 ( <NUM_LIT:1> , <NUM_LIT:7> ) <EOL> mock . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:7> ) <EOL> mock . reset_mock ( ) <EOL> f2 ( b = <NUM_LIT:1> , a = <NUM_LIT:7> ) <EOL> mock . assert_called_with ( <NUM_LIT:7> , <NUM_LIT:1> ) <EOL> mock . reset_mock ( ) <EOL> a = object ( ) <EOL> def f ( a = a ) : <EOL> pass <EOL> f2 = mocksignature ( f , mock ) <EOL> f2 ( ) <EOL> mock . assert_called_with ( a ) <EOL> def test_introspection ( self ) : <EOL> def f ( a , * args , ** kwargs ) : <EOL> pass <EOL> f2 = mocksignature ( f , f ) <EOL> self . assertEqual ( inspect . getargspec ( f ) , inspect . getargspec ( f2 ) ) <EOL> def f ( a , b = None , c = <NUM_LIT:3> , d = object ( ) ) : <EOL> pass <EOL> f2 = mocksignature ( f , f ) <EOL> self . assertEqual ( inspect . getargspec ( f ) , inspect . getargspec ( f2 ) ) <EOL> def test_function_with_varargs_and_kwargs ( self ) : <EOL> def f ( a , b = None , * args , ** kwargs ) : <EOL> return ( a , b , args , kwargs ) <EOL> f2 = mocksignature ( f , f ) <EOL> self . assertEqual ( f2 ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , x = <NUM_LIT:6> , y = <NUM_LIT:9> ) , ( <NUM_LIT:3> , <NUM_LIT:4> , ( <NUM_LIT:5> , ) , { '<STR_LIT:x>' : <NUM_LIT:6> , '<STR_LIT:y>' : <NUM_LIT:9> } ) ) <EOL> self . assertEqual ( f2 ( <NUM_LIT:3> , x = <NUM_LIT:6> , y = <NUM_LIT:9> , b = '<STR_LIT:a>' ) , ( <NUM_LIT:3> , '<STR_LIT:a>' , ( ) , { '<STR_LIT:x>' : <NUM_LIT:6> , '<STR_LIT:y>' : <NUM_LIT:9> } ) ) <EOL> def f ( * args ) : <EOL> pass <EOL> g = mocksignature ( f ) <EOL> g . mock . return_value = <NUM_LIT:3> <EOL> self . assertEqual ( g ( <NUM_LIT:1> , <NUM_LIT:2> , '<STR_LIT>' ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( g ( ) , <NUM_LIT:3> ) <EOL> self . assertRaises ( TypeError , lambda : g ( a = None ) ) <EOL> def f ( ** kwargs ) : <EOL> pass <EOL> g = mocksignature ( f ) <EOL> g . mock . return_value = <NUM_LIT:3> <EOL> self . assertEqual ( g ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( g ( a = None , b = None ) , <NUM_LIT:3> ) <EOL> self . assertRaises ( TypeError , lambda : g ( None ) ) <EOL> def test_mocksignature_with_patch ( self ) : <EOL> mock = Mock ( ) <EOL> def f ( a , b , c ) : <EOL> pass <EOL> mock . f = f <EOL> @ patch . object ( mock , '<STR_LIT:f>' , mocksignature = True ) <EOL> def test ( mock_f ) : <EOL> self . assertRaises ( TypeError , mock . f , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertRaises ( TypeError , mock . f , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> mock . f ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mock_f . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mock . f . mock . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> test ( ) <EOL> @ patch ( '<STR_LIT>' , mocksignature = True ) <EOL> def test ( mock_wibble ) : <EOL> from tests . support import SomeClass <EOL> instance = SomeClass ( ) <EOL> self . assertRaises ( TypeError , instance . wibble , <NUM_LIT:1> ) <EOL> instance . wibble ( ) <EOL> mock_wibble . assert_called_with ( instance ) <EOL> instance . wibble . mock . assert_called_with ( instance ) <EOL> test ( ) <EOL> @ unittest2 . skipUnless ( __debug__ , '<STR_LIT>' ) <EOL> def test_mocksignature_with_reserved_arg ( self ) : <EOL> def f ( _mock_ ) : <EOL> pass <EOL> self . assertRaises ( AssertionError , lambda : mocksignature ( f ) ) <EOL> def f ( _mock_ = None ) : <EOL> pass <EOL> self . assertRaises ( AssertionError , lambda : mocksignature ( f ) ) <EOL> def f ( * _mock_ ) : <EOL> pass <EOL> self . assertRaises ( AssertionError , lambda : mocksignature ( f ) ) <EOL> def f ( ** _mock_ ) : <EOL> pass <EOL> self . assertRaises ( AssertionError , lambda : mocksignature ( f ) ) <EOL> def test_mocksignature_class ( self ) : <EOL> MockedSomething = mocksignature ( Something ) <EOL> result = MockedSomething ( <NUM_LIT:5> , <NUM_LIT> ) <EOL> self . assertIs ( result , MockedSomething . mock . return_value ) <EOL> MockedSomething ( <NUM_LIT:1> ) <EOL> MockedSomething . mock . assert_caled_with ( <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> self . assertRaises ( TypeError , MockedSomething ) <EOL> def test_mocksignature_callable ( self ) : <EOL> mocked_something = mocksignature ( something ) <EOL> result = mocked_something ( <NUM_LIT:5> , <NUM_LIT> ) <EOL> self . assertIs ( result , mocked_something . mock . return_value ) <EOL> mocked_something ( <NUM_LIT:1> ) <EOL> mocked_something . mock . assert_caled_with ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> self . assertRaises ( TypeError , mocked_something ) <EOL> def test_patch_mocksignature_class ( self ) : <EOL> original_something = Something <EOL> something_name = '<STR_LIT>' % __name__ <EOL> @ patch ( something_name , mocksignature = True ) <EOL> def test ( MockSomething ) : <EOL> Something ( <NUM_LIT:3> , <NUM_LIT:5> ) <EOL> MockSomething . assert_called_with ( <NUM_LIT:3> , <NUM_LIT:5> ) <EOL> Something ( <NUM_LIT:6> ) <EOL> MockSomething . assert_called_with ( <NUM_LIT:6> , <NUM_LIT:10> ) <EOL> self . assertRaises ( TypeError , Something ) <EOL> test ( ) <EOL> self . assertIs ( Something , original_something ) <EOL> def test_patch_mocksignature_callable ( self ) : <EOL> original_something = something <EOL> something_name = '<STR_LIT>' % __name__ <EOL> @ patch ( something_name , mocksignature = True ) <EOL> def test ( MockSomething ) : <EOL> something ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> MockSomething . assert_called_with ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> something ( <NUM_LIT:6> ) <EOL> MockSomething . assert_called_with ( <NUM_LIT:6> , <NUM_LIT:5> ) <EOL> self . assertRaises ( TypeError , something ) <EOL> test ( ) <EOL> self . assertIs ( something , original_something ) <EOL> def test_patchobject_mocksignature ( self ) : <EOL> class something ( object ) : <EOL> def meth ( self , a , b , c ) : <EOL> pass <EOL> original = something . __dict__ [ '<STR_LIT>' ] <EOL> @ patch . object ( something , '<STR_LIT>' , mocksignature = True ) <EOL> def test ( _ ) : <EOL> self . assertIsNot ( something . __dict__ [ '<STR_LIT>' ] , original ) <EOL> thing = something ( ) <EOL> thing . meth ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . assertRaises ( TypeError , thing . meth , <NUM_LIT:1> ) <EOL> test ( ) <EOL> self . assertIs ( something . __dict__ [ '<STR_LIT>' ] , original ) <EOL> thing = something ( ) <EOL> original = thing . meth <EOL> @ patch . object ( thing , '<STR_LIT>' , mocksignature = True ) <EOL> def test ( _ ) : <EOL> thing . meth ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . assertRaises ( TypeError , thing . meth , <NUM_LIT:1> ) <EOL> test ( ) <EOL> self . assertEqual ( thing . meth , original ) <EOL> self . assertNotIn ( '<STR_LIT>' , thing . __dict__ ) <EOL> def test_assert_called_with ( self ) : <EOL> func = mocksignature ( f ) <EOL> self . assertRaises ( AssertionError , func . assert_called_with ) <EOL> self . assertRaises ( AssertionError , func . assert_called_once_with ) <EOL> func ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> func . assert_called_with ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . assertRaises ( AssertionError , func . assert_called_with , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> func . assert_called_once_with ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . assertRaises ( AssertionError , func . assert_called_once_with , <EOL> <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> def test_mock_attributes ( self ) : <EOL> func = mocksignature ( f ) <EOL> return_value = func . return_value <EOL> self . assertIsInstance ( return_value , Mock ) <EOL> self . assertIsNone ( func . side_effect ) <EOL> self . assertFalse ( func . called ) <EOL> self . assertIsNone ( func . call_args ) <EOL> self . assertEqual ( func . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( func . method_calls , [ ] ) <EOL> self . assertEqual ( func . call_args_list , [ ] ) <EOL> self . assertIs ( func . _mock_children , func . mock . _mock_children ) <EOL> self . assertIs ( func ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , return_value ) <EOL> self . assertTrue ( func . called ) <EOL> self . assertEqual ( func . call_args , ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , { } ) ) <EOL> self . assertEqual ( func . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( func . method_calls , [ ] ) <EOL> self . assertEqual ( func . call_args_list , [ ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , { } ) ] ) <EOL> func . method_calls . append ( '<STR_LIT:foo>' ) <EOL> return_value ( ) <EOL> func . reset_mock ( ) <EOL> self . assertEqual ( return_value . call_count , False ) <EOL> self . assertFalse ( func . called ) <EOL> self . assertIsNone ( func . call_args ) <EOL> self . assertEqual ( func . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( func . method_calls , [ ] ) <EOL> self . assertEqual ( func . call_args_list , [ ] ) <EOL> self . assertIs ( func . _mock_children , func . mock . _mock_children ) <EOL> func . side_effect = KeyError <EOL> self . assertRaises ( KeyError , func , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . assertTrue ( func . called ) <EOL> func . side_effect = None <EOL> func . return_value = '<STR_LIT:foo>' <EOL> self . assertEqual ( func ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( func . call_count , <NUM_LIT:2> ) <EOL> def test_return_value_from_existing_mock ( self ) : <EOL> mock = Mock ( return_value = '<STR_LIT:foo>' ) <EOL> func = mocksignature ( f , mock ) <EOL> self . assertEqual ( func ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , '<STR_LIT:foo>' ) <EOL> mock . return_value = '<STR_LIT:bar>' <EOL> self . assertEqual ( func ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , '<STR_LIT:bar>' ) <EOL> def test_side_effect_from_existing_mock ( self ) : <EOL> mock = Mock ( side_effect = KeyError ) <EOL> func = mocksignature ( f , mock ) <EOL> self . assertRaises ( KeyError , func , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mock . side_effect = NameError <EOL> self . assertRaises ( NameError , func , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest2 . main ( ) </s>
