1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/mock-1.0.0/tests/testcallable.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,158 @@ 1.4 +# Copyright (C) 2007-2012 Michael Foord & the mock team 1.5 +# E-mail: fuzzyman AT voidspace DOT org DOT uk 1.6 +# http://www.voidspace.org.uk/python/mock/ 1.7 + 1.8 +from tests.support import is_instance, unittest2, X, SomeClass 1.9 + 1.10 +from mock import ( 1.11 + Mock, MagicMock, NonCallableMagicMock, 1.12 + NonCallableMock, patch, create_autospec, 1.13 + CallableMixin 1.14 +) 1.15 + 1.16 + 1.17 + 1.18 +class TestCallable(unittest2.TestCase): 1.19 + 1.20 + def assertNotCallable(self, mock): 1.21 + self.assertTrue(is_instance(mock, NonCallableMagicMock)) 1.22 + self.assertFalse(is_instance(mock, CallableMixin)) 1.23 + 1.24 + 1.25 + def test_non_callable(self): 1.26 + for mock in NonCallableMagicMock(), NonCallableMock(): 1.27 + self.assertRaises(TypeError, mock) 1.28 + self.assertFalse(hasattr(mock, '__call__')) 1.29 + self.assertIn(mock.__class__.__name__, repr(mock)) 1.30 + 1.31 + 1.32 + def test_heirarchy(self): 1.33 + self.assertTrue(issubclass(MagicMock, Mock)) 1.34 + self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock)) 1.35 + 1.36 + 1.37 + def test_attributes(self): 1.38 + one = NonCallableMock() 1.39 + self.assertTrue(issubclass(type(one.one), Mock)) 1.40 + 1.41 + two = NonCallableMagicMock() 1.42 + self.assertTrue(issubclass(type(two.two), MagicMock)) 1.43 + 1.44 + 1.45 + def test_subclasses(self): 1.46 + class MockSub(Mock): 1.47 + pass 1.48 + 1.49 + one = MockSub() 1.50 + self.assertTrue(issubclass(type(one.one), MockSub)) 1.51 + 1.52 + class MagicSub(MagicMock): 1.53 + pass 1.54 + 1.55 + two = MagicSub() 1.56 + self.assertTrue(issubclass(type(two.two), MagicSub)) 1.57 + 1.58 + 1.59 + def test_patch_spec(self): 1.60 + patcher = patch('%s.X' % __name__, spec=True) 1.61 + mock = patcher.start() 1.62 + self.addCleanup(patcher.stop) 1.63 + 1.64 + instance = mock() 1.65 + mock.assert_called_once_with() 1.66 + 1.67 + self.assertNotCallable(instance) 1.68 + self.assertRaises(TypeError, instance) 1.69 + 1.70 + 1.71 + def test_patch_spec_set(self): 1.72 + patcher = patch('%s.X' % __name__, spec_set=True) 1.73 + mock = patcher.start() 1.74 + self.addCleanup(patcher.stop) 1.75 + 1.76 + instance = mock() 1.77 + mock.assert_called_once_with() 1.78 + 1.79 + self.assertNotCallable(instance) 1.80 + self.assertRaises(TypeError, instance) 1.81 + 1.82 + 1.83 + def test_patch_spec_instance(self): 1.84 + patcher = patch('%s.X' % __name__, spec=X()) 1.85 + mock = patcher.start() 1.86 + self.addCleanup(patcher.stop) 1.87 + 1.88 + self.assertNotCallable(mock) 1.89 + self.assertRaises(TypeError, mock) 1.90 + 1.91 + 1.92 + def test_patch_spec_set_instance(self): 1.93 + patcher = patch('%s.X' % __name__, spec_set=X()) 1.94 + mock = patcher.start() 1.95 + self.addCleanup(patcher.stop) 1.96 + 1.97 + self.assertNotCallable(mock) 1.98 + self.assertRaises(TypeError, mock) 1.99 + 1.100 + 1.101 + def test_patch_spec_callable_class(self): 1.102 + class CallableX(X): 1.103 + def __call__(self): 1.104 + pass 1.105 + 1.106 + class Sub(CallableX): 1.107 + pass 1.108 + 1.109 + class Multi(SomeClass, Sub): 1.110 + pass 1.111 + 1.112 + class OldStyle: 1.113 + def __call__(self): 1.114 + pass 1.115 + 1.116 + class OldStyleSub(OldStyle): 1.117 + pass 1.118 + 1.119 + for arg in 'spec', 'spec_set': 1.120 + for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub: 1.121 + patcher = patch('%s.X' % __name__, **{arg: Klass}) 1.122 + mock = patcher.start() 1.123 + 1.124 + try: 1.125 + instance = mock() 1.126 + mock.assert_called_once_with() 1.127 + 1.128 + self.assertTrue(is_instance(instance, MagicMock)) 1.129 + # inherited spec 1.130 + self.assertRaises(AttributeError, getattr, instance, 1.131 + 'foobarbaz') 1.132 + 1.133 + result = instance() 1.134 + # instance is callable, result has no spec 1.135 + instance.assert_called_once_with() 1.136 + 1.137 + result(3, 2, 1) 1.138 + result.assert_called_once_with(3, 2, 1) 1.139 + result.foo(3, 2, 1) 1.140 + result.foo.assert_called_once_with(3, 2, 1) 1.141 + finally: 1.142 + patcher.stop() 1.143 + 1.144 + 1.145 + def test_create_autopsec(self): 1.146 + mock = create_autospec(X) 1.147 + instance = mock() 1.148 + self.assertRaises(TypeError, instance) 1.149 + 1.150 + mock = create_autospec(X()) 1.151 + self.assertRaises(TypeError, mock) 1.152 + 1.153 + 1.154 + def test_create_autospec_instance(self): 1.155 + mock = create_autospec(SomeClass, instance=True) 1.156 + 1.157 + self.assertRaises(TypeError, mock) 1.158 + mock.wibble() 1.159 + mock.wibble.assert_called_once_with() 1.160 + 1.161 + self.assertRaises(TypeError, mock.wibble, 'some', 'args')