summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2020-02-15 18:34:29 -0600
committerGitHub <noreply@github.com>2020-02-15 18:34:29 -0600
commit0e32e11a4c58b504f748ae423aa834dbf8538211 (patch)
tree60f0a2bfb97fcde5b2c1d991c78963ccbf95be92
parent447121d0e41e9785251a1e7b1c7f162e4f7f00fd (diff)
parentd49f8e205d36f1a3dceca228b3bc19d9cca57624 (diff)
downloadzope-interface-0e32e11a4c58b504f748ae423aa834dbf8538211.tar.gz
Merge pull request #176 from zopefoundation/issue6
Add test case for #6.
-rw-r--r--src/zope/interface/tests/test_interface.py51
1 files changed, 50 insertions, 1 deletions
diff --git a/src/zope/interface/tests/test_interface.py b/src/zope/interface/tests/test_interface.py
index 1ec5117..433d342 100644
--- a/src/zope/interface/tests/test_interface.py
+++ b/src/zope/interface/tests/test_interface.py
@@ -418,7 +418,7 @@ class InterfaceClassTests(unittest.TestCase):
from zope.interface.interface import InterfaceClass
return InterfaceClass
- def _makeOne(self, name='ITest', bases=(), attrs=None, __doc__=None,
+ def _makeOne(self, name='ITest', bases=(), attrs=None, __doc__=None,
__module__=None):
return self._getTargetClass()(name, bases, attrs, __doc__, __module__)
@@ -885,6 +885,55 @@ class InterfaceClassTests(unittest.TestCase):
self.assertFalse(one > other)
self.assertTrue(other > one)
+ def test_assignment_to__class__(self):
+ # https://github.com/zopefoundation/zope.interface/issues/6
+ class MyException(Exception):
+ pass
+
+ class MyInterfaceClass(self._getTargetClass()):
+ def __call__(self, target):
+ raise MyException(target)
+
+ IFoo = self._makeOne('IName')
+ self.assertIsInstance(IFoo, self._getTargetClass())
+ self.assertIs(type(IFoo), self._getTargetClass())
+
+ with self.assertRaises(TypeError):
+ IFoo(1)
+
+ IFoo.__class__ = MyInterfaceClass
+ self.assertIsInstance(IFoo, MyInterfaceClass)
+ self.assertIs(type(IFoo), MyInterfaceClass)
+
+ with self.assertRaises(MyException):
+ IFoo(1)
+
+ def test_assignment_to__class__2(self):
+ # https://github.com/zopefoundation/zope.interface/issues/6
+ # This is essentially a transcription of the
+ # test presented in the bug report.
+ from zope.interface import Interface
+ class MyInterfaceClass(self._getTargetClass()):
+ def __call__(self, *args):
+ return args
+
+ IFoo = MyInterfaceClass('IFoo', (Interface,))
+ self.assertEqual(IFoo(1), (1,))
+
+ class IBar(IFoo):
+ pass
+
+ self.assertEqual(IBar(1), (1,))
+
+ class ISpam(Interface):
+ pass
+
+ with self.assertRaises(TypeError):
+ ISpam()
+
+ ISpam.__class__ = MyInterfaceClass
+ self.assertEqual(ISpam(1), (1,))
+
class InterfaceTests(unittest.TestCase):