1
===================================
2
Verifying interface implementations
3
===================================
5
The ``zope.interface.verify`` module provides functions that test whether a
6
given interface is implemented by a class or provided by an object, resp.
12
This is covered by unit tests defined in ``zope.interface.tests.test_verify``.
18
An object provides an interface if
20
- either its class declares that it implements the interfaces, or the object
21
declares that it directly provides the interface
23
- the object defines all the methods required by the interface
25
- all the methods have the correct signature
27
- the object defines all non-method attributes required by the interface
29
This doctest currently covers only the latter item.
31
Testing for attributes
32
----------------------
34
Attributes of the object, be they defined by its class or added by its
35
``__init__`` method, will be recognized:
39
>>> from zope.interface import Interface, Attribute, implements
40
>>> from zope.interface.exceptions import BrokenImplementation
41
>>> class IFoo(Interface):
42
... x = Attribute("The X attribute")
43
... y = Attribute("The Y attribute")
45
>>> class Foo(object):
48
... def __init__(self):
51
>>> from zope.interface.verify import verifyObject
52
>>> verifyObject(IFoo, Foo())
55
If either attribute is missing, verification will fail:
59
>>> class Foo(object):
62
>>> try: #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
63
... verifyObject(IFoo, Foo())
64
... except BrokenImplementation, e:
66
An object has failed to implement interface <InterfaceClass ...IFoo>
68
The y attribute was not provided.
70
>>> class Foo(object):
72
... def __init__(self):
74
>>> try: #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
75
... verifyObject(IFoo, Foo())
76
... except BrokenImplementation, e:
78
An object has failed to implement interface <InterfaceClass ...IFoo>
80
The x attribute was not provided.
83
If an attribute is implemented as a property that raises an AttributeError
84
when trying to get its value, the attribute is considered missing:
88
>>> class IFoo(Interface):
89
... x = Attribute('The X attribute')
90
>>> class Foo(object):
94
... raise AttributeError
95
>>> try: #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
96
... verifyObject(IFoo, Foo())
97
... except BrokenImplementation, e:
99
An object has failed to implement interface <InterfaceClass ...IFoo>
101
The x attribute was not provided.
104
Any other exception raised by a property will propagate to the caller of
109
>>> class Foo(object):
114
>>> verifyObject(IFoo, Foo())
115
Traceback (most recent call last):
118
Of course, broken properties that are not required by the interface don't do
123
>>> class Foo(object):
129
>>> verifyObject(IFoo, Foo())