D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
usr
/
share
/
doc
/
python-zope-interface-4.0.5
/
docs
/
Filename :
verify.rst
back
Copy
=================================== Verifying interface implementations =================================== The ``zope.interface.verify`` module provides functions that test whether a given interface is implemented by a class or provided by an object, resp. Verifying classes ================= This is covered by unit tests defined in ``zope.interface.tests.test_verify``. Verifying objects ================= An object provides an interface if - either its class declares that it implements the interfaces, or the object declares that it directly provides the interface - the object defines all the methods required by the interface - all the methods have the correct signature - the object defines all non-method attributes required by the interface This doctest currently covers only the latter item. Testing for attributes ---------------------- Attributes of the object, be they defined by its class or added by its ``__init__`` method, will be recognized: .. doctest:: >>> from zope.interface import Interface, Attribute, implements >>> from zope.interface.exceptions import BrokenImplementation >>> class IFoo(Interface): ... x = Attribute("The X attribute") ... y = Attribute("The Y attribute") >>> class Foo(object): ... implements(IFoo) ... x = 1 ... def __init__(self): ... self.y = 2 >>> from zope.interface.verify import verifyObject >>> verifyObject(IFoo, Foo()) True If either attribute is missing, verification will fail: .. doctest:: >>> class Foo(object): ... implements(IFoo) ... x = 1 >>> try: #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS ... verifyObject(IFoo, Foo()) ... except BrokenImplementation, e: ... print str(e) An object has failed to implement interface <InterfaceClass ...IFoo> <BLANKLINE> The y attribute was not provided. <BLANKLINE> >>> class Foo(object): ... implements(IFoo) ... def __init__(self): ... self.y = 2 >>> try: #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS ... verifyObject(IFoo, Foo()) ... except BrokenImplementation, e: ... print str(e) An object has failed to implement interface <InterfaceClass ...IFoo> <BLANKLINE> The x attribute was not provided. <BLANKLINE> If an attribute is implemented as a property that raises an AttributeError when trying to get its value, the attribute is considered missing: .. doctest:: >>> class IFoo(Interface): ... x = Attribute('The X attribute') >>> class Foo(object): ... implements(IFoo) ... @property ... def x(self): ... raise AttributeError >>> try: #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS ... verifyObject(IFoo, Foo()) ... except BrokenImplementation, e: ... print str(e) An object has failed to implement interface <InterfaceClass ...IFoo> <BLANKLINE> The x attribute was not provided. <BLANKLINE> Any other exception raised by a property will propagate to the caller of ``verifyObject``: .. doctest:: >>> class Foo(object): ... implements(IFoo) ... @property ... def x(self): ... raise Exception >>> verifyObject(IFoo, Foo()) Traceback (most recent call last): Exception Of course, broken properties that are not required by the interface don't do any harm: .. doctest:: >>> class Foo(object): ... implements(IFoo) ... x = 1 ... @property ... def y(self): ... raise Exception >>> verifyObject(IFoo, Foo()) True
Name
Size
Last Modified
Owner
Permissions
Actions
Makefile
5.461
KB
February 28 2013 6:51:06
root
0644
README.rst
24.32
KB
February 28 2013 6:51:06
root
0644
README.ru.rst
33.185
KB
February 28 2013 6:51:06
root
0644
adapter.rst
14.796
KB
February 28 2013 6:51:06
root
0644
adapter.ru.rst
20.673
KB
February 28 2013 6:51:06
root
0644
api.rst
21.021
KB
February 28 2013 6:51:06
root
0644
conf.py
7.985
KB
February 28 2013 6:51:06
root
0644
foodforthought.rst
1.701
KB
February 28 2013 6:51:06
root
0644
hacking.rst
10.583
KB
February 28 2013 6:51:06
root
0644
human.rst
6.275
KB
February 28 2013 6:51:06
root
0644
human.ru.rst
10.431
KB
February 28 2013 6:51:06
root
0644
index.rst
0.608
KB
February 28 2013 6:51:06
root
0644
make.bat
4.99
KB
February 28 2013 6:51:06
root
0644
verify.rst
3.52
KB
February 28 2013 6:51:06
root
0644
2017 © D7net | D704T team