[Checkins] SVN: zope.component/tseaver-test_cleanup/ Explicit doctest sections.
Tres Seaver
cvs-admin at zope.org
Sun Jun 17 18:32:09 UTC 2012
Log message for revision 126894:
Explicit doctest sections.
Changed:
_U zope.component/tseaver-test_cleanup/
U zope.component/tseaver-test_cleanup/docs/event.rst
-=-
Modified: zope.component/tseaver-test_cleanup/docs/event.rst
===================================================================
--- zope.component/tseaver-test_cleanup/docs/event.rst 2012-06-17 18:32:01 UTC (rev 126893)
+++ zope.component/tseaver-test_cleanup/docs/event.rst 2012-06-17 18:32:05 UTC (rev 126894)
@@ -8,50 +8,60 @@
Before we can start we need to import ``zope.component.event`` to make
the dispatching effective:
- >>> import zope.component.event
+.. doctest::
+ >>> import zope.component.event
+
Consider two event classes:
- >>> class Event1(object):
- ... pass
+.. doctest::
- >>> class Event2(Event1):
- ... pass
+ >>> class Event1(object):
+ ... pass
+ >>> class Event2(Event1):
+ ... pass
+
Now consider two handlers for these event classes:
- >>> called = []
+.. doctest::
- >>> import zope.component
- >>> @zope.component.adapter(Event1)
- ... def handler1(event):
- ... called.append(1)
+ >>> called = []
- >>> @zope.component.adapter(Event2)
- ... def handler2(event):
- ... called.append(2)
+ >>> import zope.component
+ >>> @zope.component.adapter(Event1)
+ ... def handler1(event):
+ ... called.append(1)
+ >>> @zope.component.adapter(Event2)
+ ... def handler2(event):
+ ... called.append(2)
+
We can register them with the Component Architecture:
- >>> zope.component.provideHandler(handler1)
- >>> zope.component.provideHandler(handler2)
+.. doctest::
+ >>> zope.component.provideHandler(handler1)
+ >>> zope.component.provideHandler(handler2)
+
Now let's go through the events. We'll see that the handlers have been
called accordingly:
- >>> from zope.event import notify
- >>> notify(Event1())
- >>> called
- [1]
+.. doctest::
- >>> del called[:]
- >>> notify(Event2())
- >>> called.sort()
- >>> called
- [1, 2]
+ >>> from zope.event import notify
+ >>> notify(Event1())
+ >>> called
+ [1]
+ >>> del called[:]
+ >>> notify(Event2())
+ >>> called.sort()
+ >>> called
+ [1, 2]
+
Object events
-------------
@@ -61,82 +71,106 @@
First create an object class:
- >>> class IUseless(zope.interface.Interface):
- ... """Useless object"""
+.. doctest::
- >>> class UselessObject(object):
- ... """Useless object"""
- ... zope.interface.implements(IUseless)
+ >>> class IUseless(zope.interface.Interface):
+ ... """Useless object"""
+ >>> class UselessObject(object):
+ ... """Useless object"""
+ ... zope.interface.implements(IUseless)
+
Then create an event class:
- >>> class IObjectThrownEvent(zope.component.interfaces.IObjectEvent):
- ... """An object has been thrown away"""
+.. doctest::
- >>> class ObjectThrownEvent(zope.component.interfaces.ObjectEvent):
- ... """An object has been thrown away"""
- ... zope.interface.implements(IObjectThrownEvent)
+ >>> class IObjectThrownEvent(zope.component.interfaces.IObjectEvent):
+ ... """An object has been thrown away"""
+ >>> class ObjectThrownEvent(zope.component.interfaces.ObjectEvent):
+ ... """An object has been thrown away"""
+ ... zope.interface.implements(IObjectThrownEvent)
+
Create an object and an event:
- >>> hammer = UselessObject()
- >>> event = ObjectThrownEvent(hammer)
+.. doctest::
+ >>> hammer = UselessObject()
+ >>> event = ObjectThrownEvent(hammer)
+
Then notify the event to the subscribers.
Since the subscribers list is empty, nothing happens.
- >>> zope.component.event.objectEventNotify(event)
+.. doctest::
+ >>> zope.component.event.objectEventNotify(event)
+
Now create an handler for the event:
- >>> events = []
- >>> def record(*args):
- ... events.append(args)
+.. doctest::
- >>> zope.component.provideHandler(record, [IUseless, IObjectThrownEvent])
+ >>> events = []
+ >>> def record(*args): #*
+ ... events.append(args)
+ >>> zope.component.provideHandler(record, [IUseless, IObjectThrownEvent])
+
The event is notified to the subscriber:
- >>> zope.component.event.objectEventNotify(event)
- >>> events == [(hammer, event)]
- True
+.. doctest::
+ >>> zope.component.event.objectEventNotify(event)
+ >>> events == [(hammer, event)]
+ True
+
Following test demonstrates how a subscriber can raise an exception
to prevent an action.
- >>> zope.component.provideHandler(zope.component.event.objectEventNotify)
+.. doctest::
+ >>> zope.component.provideHandler(zope.component.event.objectEventNotify)
+
Let's create a container:
- >>> class ToolBox(dict):
- ... def __delitem__(self, key):
- ... notify(ObjectThrownEvent(self[key]))
- ... return super(ToolBox,self).__delitem__(key)
+.. doctest::
- >>> container = ToolBox()
+ >>> class ToolBox(dict):
+ ... def __delitem__(self, key):
+ ... notify(ObjectThrownEvent(self[key]))
+ ... return super(ToolBox,self).__delitem__(key)
+ >>> container = ToolBox()
+
And put the object into the container:
- >>> container['Red Hammer'] = hammer
+.. doctest::
+ >>> container['Red Hammer'] = hammer
+
Create an handler function that will raise an error when called:
- >>> class Veto(Exception):
- ... pass
+.. doctest::
- >>> def callback(item, event):
- ... assert(item == event.object)
- ... raise Veto
+ >>> class Veto(Exception):
+ ... pass
+ >>> def callback(item, event):
+ ... assert(item == event.object)
+ ... raise Veto
+
Register the handler:
- >>> zope.component.provideHandler(callback, [IUseless, IObjectThrownEvent])
+.. doctest::
+ >>> zope.component.provideHandler(callback, [IUseless, IObjectThrownEvent])
+
Then if we try to remove the object, an ObjectThrownEvent is fired:
- >>> del container['Red Hammer']
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- raise Veto
- Veto
+.. doctest::
+
+ >>> del container['Red Hammer']
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ raise Veto
+ Veto
More information about the checkins
mailing list