test_exceptions.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2010 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """ zope.interface.exceptions unit tests
  15. """
  16. import unittest
  17. def _makeIface():
  18. from zope.interface import Interface
  19. class IDummy(Interface):
  20. pass
  21. return IDummy
  22. class DoesNotImplementTests(unittest.TestCase):
  23. def _getTargetClass(self):
  24. from zope.interface.exceptions import DoesNotImplement
  25. return DoesNotImplement
  26. def _makeOne(self):
  27. iface = _makeIface()
  28. return self._getTargetClass()(iface)
  29. def test___str__(self):
  30. dni = self._makeOne()
  31. # XXX The trailing newlines and blank spaces are a stupid artifact.
  32. self.assertEqual(str(dni),
  33. 'An object does not implement interface <InterfaceClass '
  34. 'zope.interface.tests.test_exceptions.IDummy>\n\n ')
  35. class BrokenImplementationTests(unittest.TestCase):
  36. def _getTargetClass(self):
  37. from zope.interface.exceptions import BrokenImplementation
  38. return BrokenImplementation
  39. def _makeOne(self, name='missing'):
  40. iface = _makeIface()
  41. return self._getTargetClass()(iface, name)
  42. def test___str__(self):
  43. dni = self._makeOne()
  44. # XXX The trailing newlines and blank spaces are a stupid artifact.
  45. self.assertEqual(str(dni),
  46. 'An object has failed to implement interface <InterfaceClass '
  47. 'zope.interface.tests.test_exceptions.IDummy>\n\n'
  48. ' The missing attribute was not provided.\n ')
  49. class BrokenMethodImplementationTests(unittest.TestCase):
  50. def _getTargetClass(self):
  51. from zope.interface.exceptions import BrokenMethodImplementation
  52. return BrokenMethodImplementation
  53. def _makeOne(self, method='aMethod', mess='I said so'):
  54. return self._getTargetClass()(method, mess)
  55. def test___str__(self):
  56. dni = self._makeOne()
  57. self.assertEqual(str(dni),
  58. 'The implementation of aMethod violates its contract\n'
  59. ' because I said so.\n ')