summaryrefslogtreecommitdiff
path: root/src/zope/interface/common/tests
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2020-02-11 06:44:46 -0600
committerJason Madden <jamadden@gmail.com>2020-02-17 07:01:03 -0600
commit653e24f53650810bb6a8ff401477e0e03ab84aa0 (patch)
treec471f8cfff4b8dcc84f966d1ec7fad06d2981075 /src/zope/interface/common/tests
parente819c75e609781848d0ba3c7301b352e0cb93d88 (diff)
downloadzope-interface-653e24f53650810bb6a8ff401477e0e03ab84aa0.tar.gz
Add collections.IByteString and refactor to avoid one-to-one assumption about ABCs and builtins.
bytearray turns out to violate that.
Diffstat (limited to 'src/zope/interface/common/tests')
-rw-r--r--src/zope/interface/common/tests/test_collections.py47
1 files changed, 33 insertions, 14 deletions
diff --git a/src/zope/interface/common/tests/test_collections.py b/src/zope/interface/common/tests/test_collections.py
index 2415731..8fd1d0c 100644
--- a/src/zope/interface/common/tests/test_collections.py
+++ b/src/zope/interface/common/tests/test_collections.py
@@ -25,13 +25,33 @@ except ImportError:
from zope.interface.verify import verifyClass
from zope.interface.verify import verifyObject
+from zope.interface.common import ABCInterface
+from zope.interface.common import ABCInterfaceClass
# Note that importing z.i.c.collections does work on import.
from zope.interface.common import collections
-from zope.interface.common import stdlib_class_registry
+
from zope.interface._compat import PYPY
from zope.interface._compat import PYTHON2 as PY2
+def walk_abc_interfaces():
+ # Note that some builtin classes are registered for two distinct
+ # parts of the ABC/interface tree. For example, bytearray is both ByteString
+ # and MutableSequence.
+ seen = set()
+ stack = list(ABCInterface.dependents) # subclasses, but also implementedBy objects
+ while stack:
+ iface = stack.pop(0)
+ if iface in seen or not isinstance(iface, ABCInterfaceClass):
+ continue
+ seen.add(iface)
+ stack.extend(list(iface.dependents))
+
+ registered = list(iface.getRegisteredConformers())
+ if registered:
+ yield iface, registered
+
+
class TestVerifyClass(unittest.TestCase):
verifier = staticmethod(verifyClass)
@@ -50,12 +70,10 @@ class TestVerifyClass(unittest.TestCase):
def test_frozenset(self):
self.assertIsInstance(frozenset(), abc.Set)
self.assertTrue(self.verify(collections.ISet, frozenset))
- self.assertIn(frozenset, stdlib_class_registry)
def test_list(self):
self.assertIsInstance(list(), abc.MutableSequence)
self.assertTrue(self.verify(collections.IMutableSequence, list))
- self.assertIn(list, stdlib_class_registry)
# Now we go through the registry, which should have several things,
# mostly builtins, but if we've imported other libraries already,
@@ -107,17 +125,18 @@ class TestVerifyClass(unittest.TestCase):
@classmethod
def gen_tests(cls):
- for stdlib_class, iface in stdlib_class_registry.items():
- if stdlib_class in cls._UNVERIFIABLE or stdlib_class.__name__ in cls._UNVERIFIABLE:
- continue
-
- def test(self, stdlib_class=stdlib_class, iface=iface):
- self.assertTrue(self.verify(iface, stdlib_class))
-
- name = 'test_auto_' + stdlib_class.__name__
- test.__name__ = name
- assert not hasattr(cls, name)
- setattr(cls, name, test)
+ for iface, registered_classes in walk_abc_interfaces():
+ for stdlib_class in registered_classes:
+ if stdlib_class in cls._UNVERIFIABLE or stdlib_class.__name__ in cls._UNVERIFIABLE:
+ continue
+
+ def test(self, stdlib_class=stdlib_class, iface=iface):
+ self.assertTrue(self.verify(iface, stdlib_class))
+
+ name = 'test_auto_' + stdlib_class.__name__ + '_' + iface.__name__
+ test.__name__ = name
+ assert not hasattr(cls, name)
+ setattr(cls, name, test)
TestVerifyClass.gen_tests()