<s> import unittest <EOL> from unittest . test . testmock . support import is_instance , X , SomeClass <EOL> from unittest . mock import ( <EOL> Mock , MagicMock , NonCallableMagicMock , <EOL> NonCallableMock , patch , create_autospec , <EOL> CallableMixin <EOL> ) <EOL> class TestCallable ( unittest . TestCase ) : <EOL> def assertNotCallable ( self , mock ) : <EOL> self . assertTrue ( is_instance ( mock , NonCallableMagicMock ) ) <EOL> self . assertFalse ( is_instance ( mock , CallableMixin ) ) <EOL> def test_non_callable ( self ) : <EOL> for mock in NonCallableMagicMock ( ) , NonCallableMock ( ) : <EOL> self . assertRaises ( TypeError , mock ) <EOL> self . assertFalse ( hasattr ( mock , '<STR_LIT>' ) ) <EOL> self . assertIn ( mock . __class__ . __name__ , repr ( mock ) ) <EOL> def test_heirarchy ( self ) : <EOL> self . assertTrue ( issubclass ( MagicMock , Mock ) ) <EOL> self . assertTrue ( issubclass ( NonCallableMagicMock , NonCallableMock ) ) <EOL> def test_attributes ( self ) : <EOL> one = NonCallableMock ( ) <EOL> self . assertTrue ( issubclass ( type ( one . one ) , Mock ) ) <EOL> two = NonCallableMagicMock ( ) <EOL> self . assertTrue ( issubclass ( type ( two . two ) , MagicMock ) ) <EOL> def test_subclasses ( self ) : <EOL> class MockSub ( Mock ) : <EOL> pass <EOL> one = MockSub ( ) <EOL> self . assertTrue ( issubclass ( type ( one . one ) , MockSub ) ) <EOL> class MagicSub ( MagicMock ) : <EOL> pass <EOL> two = MagicSub ( ) <EOL> self . assertTrue ( issubclass ( type ( two . two ) , MagicSub ) ) <EOL> def test_patch_spec ( self ) : <EOL> patcher = patch ( '<STR_LIT>' % __name__ , spec = True ) <EOL> mock = patcher . start ( ) <EOL> self . addCleanup ( patcher . stop ) <EOL> instance = mock ( ) <EOL> mock . assert_called_once_with ( ) <EOL> self . assertNotCallable ( instance ) <EOL> self . assertRaises ( TypeError , instance ) <EOL> def test_patch_spec_set ( self ) : <EOL> patcher = patch ( '<STR_LIT>' % __name__ , spec_set = True ) <EOL> mock = patcher . start ( ) <EOL> self . addCleanup ( patcher . stop ) <EOL> instance = mock ( ) <EOL> mock . assert_called_once_with ( ) <EOL> self . assertNotCallable ( instance ) <EOL> self . assertRaises ( TypeError , instance ) <EOL> def test_patch_spec_instance ( self ) : <EOL> patcher = patch ( '<STR_LIT>' % __name__ , spec = X ( ) ) <EOL> mock = patcher . start ( ) <EOL> self . addCleanup ( patcher . stop ) <EOL> self . assertNotCallable ( mock ) <EOL> self . assertRaises ( TypeError , mock ) <EOL> def test_patch_spec_set_instance ( self ) : <EOL> patcher = patch ( '<STR_LIT>' % __name__ , spec_set = X ( ) ) <EOL> mock = patcher . start ( ) <EOL> self . addCleanup ( patcher . stop ) <EOL> self . assertNotCallable ( mock ) <EOL> self . assertRaises ( TypeError , mock ) <EOL> def test_patch_spec_callable_class ( self ) : <EOL> class CallableX ( X ) : <EOL> def __call__ ( self ) : <EOL> pass <EOL> class Sub ( CallableX ) : <EOL> pass <EOL> class Multi ( SomeClass , Sub ) : <EOL> pass <EOL> for arg in '<STR_LIT>' , '<STR_LIT>' : <EOL> for Klass in CallableX , Sub , Multi : <EOL> with patch ( '<STR_LIT>' % __name__ , ** { arg : Klass } ) as mock : <EOL> instance = mock ( ) <EOL> mock . assert_called_once_with ( ) <EOL> self . assertTrue ( is_instance ( instance , MagicMock ) ) <EOL> self . assertRaises ( AttributeError , getattr , instance , <EOL> '<STR_LIT>' ) <EOL> result = instance ( ) <EOL> instance . assert_called_once_with ( ) <EOL> result ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> result . assert_called_once_with ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> result . foo ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> result . foo . assert_called_once_with ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> def test_create_autopsec ( self ) : <EOL> mock = create_autospec ( X ) <EOL> instance = mock ( ) <EOL> self . assertRaises ( TypeError , instance ) <EOL> mock = create_autospec ( X ( ) ) <EOL> self . assertRaises ( TypeError , mock ) <EOL> def test_create_autospec_instance ( self ) : <EOL> mock = create_autospec ( SomeClass , instance = True ) <EOL> self . assertRaises ( TypeError , mock ) <EOL> mock . wibble ( ) <EOL> mock . wibble . assert_called_once_with ( ) <EOL> self . assertRaises ( TypeError , mock . wibble , '<STR_LIT>' , '<STR_LIT:args>' ) </s>
