[Zope3-checkins] CVS: Zope3/src/zope/app/form/tests -
test_widget.py:1.11
Garrett Smith
garrett at mojave-corp.com
Tue May 11 07:17:37 EDT 2004
Update of /cvs-repository/Zope3/src/zope/app/form/tests
In directory cvs.zope.org:/tmp/cvs-serv20716/src/zope/app/form/tests
Modified Files:
test_widget.py
Log Message:
Updates of tests per browser widget framework refactor.
=== Zope3/src/zope/app/form/tests/test_widget.py 1.10 => 1.11 ===
--- Zope3/src/zope/app/form/tests/test_widget.py:1.10 Fri May 7 15:43:26 2004
+++ Zope3/src/zope/app/form/tests/test_widget.py Tue May 11 07:17:36 2004
@@ -34,13 +34,13 @@
class FooWidget(Widget):
pass
-
+
context = TestContext()
request = TestRequest()
class TestWidget:
"""Tests basic widget characteristics.
-
+
Widget implements IWidget:
>>> verifyClass(IWidget, Widget)
@@ -48,9 +48,9 @@
>>> widget = Widget(context, request)
>>> verifyObject(IWidget, widget)
True
-
+
The default values for widget are:
-
+
>>> widget.name
'field.Test'
>>> widget.label
@@ -59,29 +59,98 @@
'A test context.'
>>> widget.visible
True
-
+
In the last example, the widget name consists of a prefix, a dot, and the
field name. You can change the prefix used by the widget as follows:
-
+
>>> widget.setPrefix('newprefix')
>>> widget.name
'newprefix.Test'
-
+
To configure a widget, call setRenderedValue with a value that the
widget should display:
-
+
>>> widget.setRenderedValue('Render Me')
-
+
The way a widget renders a value depends on the type of widget. E.g. a
browser widget will render the specified value in HTML.
"""
+class TestInputWidget:
+ """Tests the input widget mixin.
+
+ InputWidget is a simple mixin that provides default implementations for
+ some of the IInputWidget methods. Because the implementation of widgets
+ across UI frameworks is so different, most of the input widget methods
+ must be handled by UI specific classes.
+
+ To test the default methods, we must create a basic input widget
+ that provides a getInputValue method:
+
+ >>> from zope.app.form import InputWidget
+ >>> from zope.app.form.interfaces import WidgetInputError
+ >>> class TestInputWidget(InputWidget):
+ ... def getInputValue(self):
+ ... if self.context.required:
+ ... raise WidgetInputError('', '', None)
+ ... else:
+ ... return 'Foo Bar'
+
+ All widgets rely on a field and a request:
+
+ >>> from zope.schema import Field
+ >>> from zope.component.tests.request import Request
+ >>> field = Field()
+ >>> from zope.interface import Interface
+ >>> class ITestRequest(Interface):
+ ... pass
+ >>> widget = TestInputWidget(field, Request(ITestRequest))
+
+ The default implementation of hasValidInput and validate both rely on
+ getInputValue to perform the validation of the current widget input.
+ In this simple example, the widget will always raise an error when its
+ field is read only:
+
+ >>> field.readonly = True
+ >>> widget.getInputValue()
+ Traceback (most recent call last):
+ WidgetInputError: ('', '', None)
+
+ A call to validate, however, accomplishes the same thing with improved
+ readability:
+
+ >>> widget.validate()
+ Traceback (most recent call last):
+ WidgetInputError: ('', '', None)
+
+ A call to hasValidInput returns False instead of raising an error:
+
+ >>> widget.hasValidInput()
+ False
+
+ By changing the field's required attribute, getInputValue returns a
+ simple string:
+
+ >>> field.required = False
+ >>> widget.getInputValue()
+ 'Foo Bar'
+
+ Corredpondingly, validate does not raise an error:
+
+ >>> widget.validate()
+
+ and hasValidInput returns True:
+
+ >>> widget.hasValidInput()
+ True
+ """
+
class TestCustomWidgetFactory:
"""Tests the custom widget factory.
-
+
Custom widgets can be created using a custom widget factory. Factories
are used to assign attribute values to widgets they create:
-
+
>>> factory = CustomWidgetFactory(FooWidget, bar='baz')
>>> widget = factory(context, request)
>>> isinstance(widget, FooWidget)
More information about the Zope3-Checkins
mailing list