Part of canonical.launchpad.webapp.tests
Class | LaunchpadFormTest | No class docstring; 1/2 methods documented |
Function | doctest_custom_widget_with_setUpFields_override | As a regression test, it is important to note that the custom_widget |
Function | test_suite | Undocumented |
As a regression test, it is important to note that the custom_widget class advisor should still work when setUpFields is overridden. For instance, consider this custom widget and view:
>>> from zope.app.form.interfaces import IDisplayWidget, IInputWidget >>> from zope.interface import directlyProvides, implements >>> from canonical.launchpad.webapp import ( ... LaunchpadFormView, custom_widget) >>> from zope.schema import Bool >>> from zope.publisher.browser import TestRequest >>> from zope.formlib import form>>> class CustomStubWidget: ... implements(IInputWidget) ... # The methods below are the minimal necessary for widget ... # initialization. ... def __init__(self, field, request): ... self.field, self.request = field, request ... def setPrefix(self, prefix): ... self.name = '.'.join((prefix, self.field.__name__)) ... def hasInput(self): ... return False ... def setRenderedValue(self, value): ... self.value = value ... >>> class CustomView(LaunchpadFormView): ... custom_widget('my_bool', CustomStubWidget) ... def setUpFields(self): ... self.form_fields = form.Fields(Bool(__name__='my_bool')) ...
The custom setUpFields adds a field dynamically. Then setUpWidgets will use the custom widget for the field. We simply call setUpFields and setUpWidgets explicitly here for ease of testing, though normally they are called by LaunchpadFormView.initialize.
>>> view = CustomView(None, TestRequest()) >>> view.setUpFields() >>> view.setUpWidgets() >>> isinstance(view.widgets['my_bool'], CustomStubWidget) True