/usr/lib/python2.7/dist-packages/zope/formlib/tests/test_widget.py is in python-zope.formlib 4.3.0a2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Generic Widget Tests
"""
from unittest import TestSuite, main
from zope.component.testing import setUp, tearDown
from zope.publisher.browser import TestRequest
from doctest import DocTestSuite
from zope.formlib.widget import Widget
from .support import checker
class TestContext(object):
__name__ = 'Test'
title = 'My Test Context'
description = 'A test context.'
class FooWidget(Widget):
pass
context = TestContext()
request = TestRequest()
class TestWidget(object):
"""Tests basic widget characteristics.
Widget implements IWidget:
>>> from zope.interface.verify import verifyClass
>>> from zope.formlib.interfaces import IWidget
>>> verifyClass(IWidget, Widget)
True
>>> widget = Widget(context, request)
>>> from zope.interface.verify import verifyObject
>>> verifyObject(IWidget, widget)
True
The default values for widget are:
>>> widget.name
'field.Test'
>>> widget.label
'My Test Context'
>>> widget.hint
'A test context.'
>>> widget.visible
True
The `label` and `hint` attributes can be overriden, allowing views to
change them in specific contexts without needing to affect information
stored in the data model (the schema):
>>> widget.label = u'My Alternate Label'
>>> widget.label
u'My Alternate Label'
>>> widget.hint = u'Better help would be good.'
>>> widget.hint
u'Better help would be good.'
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'
Using the empty string as prefix leaves the prefix off entirely:
>>> widget.setPrefix('')
>>> widget.name
'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(object):
"""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.formlib.widget import InputWidget
>>> from zope.formlib.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
>>> field = Field()
>>> widget = TestInputWidget(field, TestRequest())
The default implementation of hasValidInput relies 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() # doctest: +IGNORE_EXCEPTION_DETAIL
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'
and hasValidInput returns True:
>>> widget.hasValidInput()
True
"""
class TestCustomWidgetFactory(object):
"""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.
The custom widget factory can be used for three widget types:
- Regular widgets
- Sequence widgets
- Vocabulary widgets
Test regular widget:
>>> from zope.formlib.widget import CustomWidgetFactory
>>> factory = CustomWidgetFactory(FooWidget, bar='baz')
>>> widget = factory(context, request)
>>> isinstance(widget, FooWidget)
True
>>> widget.bar
'baz'
Test sequence widget:
>>> from zope.schema import TextLine, List
>>> from zope.formlib.widgets import ListSequenceWidget
>>> value_type = TextLine(__name__=u'bar')
>>> field = List( __name__=u'foo', value_type=value_type )
>>> factory = CustomWidgetFactory(ListSequenceWidget,
... subwidget=CustomWidgetFactory(FooWidget, bar='baz'))
>>> widget = factory(field, request)
>>> widget.context.value_type is value_type
True
>>> isinstance(widget, ListSequenceWidget)
True
>>> isinstance(widget.subwidget, CustomWidgetFactory)
True
>>> subwidget = widget.subwidget(context, request)
>>> isinstance(subwidget, FooWidget)
True
>>> subwidget.bar
'baz'
Test vocabulary widget:
>>> from zope.schema import Choice
>>> from zope.formlib.widgets import RadioWidget
>>> field = Choice( __name__=u'foo', values=['1', '2', '3'] )
>>> bound = field.bind(context)
>>> factory = CustomWidgetFactory(RadioWidget,
... orientation = 'vertical')
>>> widget = factory(bound, request)
>>> [term.value for term in widget.context.vocabulary]
['1', '2', '3']
>>> isinstance(widget, RadioWidget)
True
>>> widget.orientation
'vertical'
"""
def test_suite():
return TestSuite((
DocTestSuite(setUp=setUp, tearDown=tearDown, checker=checker),
))
if __name__=='__main__':
main(defaultTest='test_suite')
|