Plex plugin to to play various online streams (mostly Latvian).

testcallable.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (C) 2007-2012 Michael Foord & the mock team
  2. # E-mail: fuzzyman AT voidspace DOT org DOT uk
  3. # http://www.voidspace.org.uk/python/mock/
  4. import unittest2 as unittest
  5. from mock.tests.support import is_instance, X, SomeClass
  6. from mock import (
  7. Mock, MagicMock, NonCallableMagicMock,
  8. NonCallableMock, patch, create_autospec,
  9. CallableMixin
  10. )
  11. class TestCallable(unittest.TestCase):
  12. def assertNotCallable(self, mock):
  13. self.assertTrue(is_instance(mock, NonCallableMagicMock))
  14. self.assertFalse(is_instance(mock, CallableMixin))
  15. def test_non_callable(self):
  16. for mock in NonCallableMagicMock(), NonCallableMock():
  17. self.assertRaises(TypeError, mock)
  18. self.assertFalse(hasattr(mock, '__call__'))
  19. self.assertIn(mock.__class__.__name__, repr(mock))
  20. def test_heirarchy(self):
  21. self.assertTrue(issubclass(MagicMock, Mock))
  22. self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock))
  23. def test_attributes(self):
  24. one = NonCallableMock()
  25. self.assertTrue(issubclass(type(one.one), Mock))
  26. two = NonCallableMagicMock()
  27. self.assertTrue(issubclass(type(two.two), MagicMock))
  28. def test_subclasses(self):
  29. class MockSub(Mock):
  30. pass
  31. one = MockSub()
  32. self.assertTrue(issubclass(type(one.one), MockSub))
  33. class MagicSub(MagicMock):
  34. pass
  35. two = MagicSub()
  36. self.assertTrue(issubclass(type(two.two), MagicSub))
  37. def test_patch_spec(self):
  38. patcher = patch('%s.X' % __name__, spec=True)
  39. mock = patcher.start()
  40. self.addCleanup(patcher.stop)
  41. instance = mock()
  42. mock.assert_called_once_with()
  43. self.assertNotCallable(instance)
  44. self.assertRaises(TypeError, instance)
  45. def test_patch_spec_set(self):
  46. patcher = patch('%s.X' % __name__, spec_set=True)
  47. mock = patcher.start()
  48. self.addCleanup(patcher.stop)
  49. instance = mock()
  50. mock.assert_called_once_with()
  51. self.assertNotCallable(instance)
  52. self.assertRaises(TypeError, instance)
  53. def test_patch_spec_instance(self):
  54. patcher = patch('%s.X' % __name__, spec=X())
  55. mock = patcher.start()
  56. self.addCleanup(patcher.stop)
  57. self.assertNotCallable(mock)
  58. self.assertRaises(TypeError, mock)
  59. def test_patch_spec_set_instance(self):
  60. patcher = patch('%s.X' % __name__, spec_set=X())
  61. mock = patcher.start()
  62. self.addCleanup(patcher.stop)
  63. self.assertNotCallable(mock)
  64. self.assertRaises(TypeError, mock)
  65. def test_patch_spec_callable_class(self):
  66. class CallableX(X):
  67. def __call__(self):
  68. pass
  69. class Sub(CallableX):
  70. pass
  71. class Multi(SomeClass, Sub):
  72. pass
  73. class OldStyle:
  74. def __call__(self):
  75. pass
  76. class OldStyleSub(OldStyle):
  77. pass
  78. for arg in 'spec', 'spec_set':
  79. for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub:
  80. with patch('%s.X' % __name__, **{arg: Klass}) as mock:
  81. instance = mock()
  82. mock.assert_called_once_with()
  83. self.assertTrue(is_instance(instance, MagicMock))
  84. # inherited spec
  85. self.assertRaises(AttributeError, getattr, instance,
  86. 'foobarbaz')
  87. result = instance()
  88. # instance is callable, result has no spec
  89. instance.assert_called_once_with()
  90. result(3, 2, 1)
  91. result.assert_called_once_with(3, 2, 1)
  92. result.foo(3, 2, 1)
  93. result.foo.assert_called_once_with(3, 2, 1)
  94. def test_create_autospec(self):
  95. mock = create_autospec(X)
  96. instance = mock()
  97. self.assertRaises(TypeError, instance)
  98. mock = create_autospec(X())
  99. self.assertRaises(TypeError, mock)
  100. def test_create_autospec_instance(self):
  101. mock = create_autospec(SomeClass, instance=True)
  102. self.assertRaises(TypeError, mock)
  103. mock.wibble()
  104. mock.wibble.assert_called_once_with()
  105. self.assertRaises(TypeError, mock.wibble, 'some', 'args')
  106. if __name__ == "__main__":
  107. unittest.main()