123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- ##############################################################################
- #
- # Copyright (c) 2010 Zope Foundation and Contributors.
- # All Rights Reserved.
- #
- # This software is subject to the provisions of the Zope Public License,
- # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
- # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
- # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
- # FOR A PARTICULAR PURPOSE.
- #
- ##############################################################################
- """ zope.interface.exceptions unit tests
- """
- import unittest
- def _makeIface():
- from zope.interface import Interface
- class IDummy(Interface):
- pass
- return IDummy
- class DoesNotImplementTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.exceptions import DoesNotImplement
- return DoesNotImplement
- def _makeOne(self, *args):
- iface = _makeIface()
- return self._getTargetClass()(iface, *args)
- def test___str__(self):
- dni = self._makeOne()
- self.assertEqual(
- str(dni),
- "An object has failed to implement interface "
- "__tests__.tests.test_exceptions.IDummy: "
- "Does not declaratively implement the interface."
- )
- def test___str__w_candidate(self):
- dni = self._makeOne('candidate')
- self.assertEqual(
- str(dni),
- "The object 'candidate' has failed to implement interface "
- "__tests__.tests.test_exceptions.IDummy: "
- "Does not declaratively implement the interface."
- )
- class BrokenImplementationTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.exceptions import BrokenImplementation
- return BrokenImplementation
- def _makeOne(self, *args):
- iface = _makeIface()
- return self._getTargetClass()(iface, 'missing', *args)
- def test___str__(self):
- dni = self._makeOne()
- self.assertEqual(
- str(dni),
- 'An object has failed to implement interface '
- '__tests__.tests.test_exceptions.IDummy: '
- "The 'missing' attribute was not provided.")
- def test___str__w_candidate(self):
- dni = self._makeOne('candidate')
- self.assertEqual(
- str(dni),
- 'The object \'candidate\' has failed to implement interface '
- '__tests__.tests.test_exceptions.IDummy: '
- "The 'missing' attribute was not provided.")
- def broken_function():
- """
- This is a global function with a simple argument list.
- It exists to be able to report the same information when
- formatting signatures.
- """
- class BrokenMethodImplementationTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.exceptions import BrokenMethodImplementation
- return BrokenMethodImplementation
- message = 'I said so'
- def _makeOne(self, *args):
- return self._getTargetClass()('aMethod', self.message, *args)
- def test___str__(self):
- dni = self._makeOne()
- self.assertEqual(
- str(dni),
- "An object has failed to implement interface <Unknown>: "
- "The contract of 'aMethod' is violated because I said so."
- )
- def test___str__w_candidate_no_implementation(self):
- dni = self._makeOne('some_function', '<IFoo>', 'candidate')
- self.assertEqual(
- str(dni),
- "The object 'candidate' has failed to implement interface <IFoo>: "
- "The contract of 'aMethod' is violated because I said so."
- )
- def test___str__w_candidate_w_implementation(self):
- self.message = 'implementation is wonky'
- dni = self._makeOne(broken_function, '<IFoo>', 'candidate')
- self.assertEqual(
- str(dni),
- "The object 'candidate' has failed to implement interface <IFoo>: "
- "The contract of 'aMethod' is violated because "
- "'broken_function()' is wonky."
- )
- def test___str__w_candidate_w_implementation_not_callable(self):
- self.message = 'implementation is not callable'
- dni = self._makeOne(42, '<IFoo>', 'candidate')
- self.assertEqual(
- str(dni),
- "The object 'candidate' has failed to implement interface <IFoo>: "
- "The contract of 'aMethod' is violated because "
- "'42' is not callable."
- )
- def test___repr__w_candidate(self):
- dni = self._makeOne(None, 'candidate')
- self.assertEqual(
- repr(dni),
- "BrokenMethodImplementation("
- "'aMethod', 'I said so', None, 'candidate')"
- )
- class MultipleInvalidTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.exceptions import MultipleInvalid
- return MultipleInvalid
- def _makeOne(self, excs):
- iface = _makeIface()
- return self._getTargetClass()(iface, 'target', excs)
- def test__str__(self):
- from zope.interface.exceptions import BrokenMethodImplementation
- excs = [
- BrokenMethodImplementation('aMethod', 'I said so'),
- Exception("Regular exception")
- ]
- dni = self._makeOne(excs)
- self.assertEqual(
- str(dni),
- "The object 'target' has failed to implement interface "
- "__tests__.tests.test_exceptions.IDummy:\n"
- " The contract of 'aMethod' is violated because I said so\n"
- " Regular exception"
- )
- def test__repr__(self):
- from zope.interface.exceptions import BrokenMethodImplementation
- excs = [
- BrokenMethodImplementation('aMethod', 'I said so'),
- # Use multiple arguments to normalize repr; versions of Python
- # prior to 3.7 add a trailing comma if there's just one.
- Exception("Regular", "exception")
- ]
- dni = self._makeOne(excs)
- self.assertEqual(
- repr(dni),
- "MultipleInvalid("
- "<InterfaceClass __tests__.tests.test_exceptions.IDummy>,"
- " 'target',"
- " (BrokenMethodImplementation('aMethod', 'I said so'),"
- " Exception('Regular', 'exception')))"
- )
|