- Timestamp:
- 22 Jul 2010, 15:45:39 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/widgets/tests/test_objectwidget.py
r5309 r5311 1 1 """Tests for objectwidget. 2 2 """ 3 import doctest 3 4 import unittest 4 5 import sys 5 6 from zope.component import testing 6 7 from zope.interface import Interface, implements 8 from zope.interface.verify import verifyClass 7 9 from zope.publisher.browser import TestRequest 8 10 from zope.schema import Object, TextLine 9 11 from zope.schema.interfaces import ITextLine 10 import doctest 12 from zope.traversing.adapters import DefaultTraversable 13 from zope.traversing.interfaces import ITraversable 11 14 from zope.publisher.interfaces.browser import IDefaultBrowserLayer 12 15 from zope.component import provideAdapter … … 14 17 from zope.formlib.interfaces import ( 15 18 IInputWidget, IDisplayWidget,MissingInputError) 16 from zope.formlib.widgets import TextWidget 19 from zope.formlib.widgets import TextWidget, DisplayWidget 17 20 from zope.formlib.tests.test_browserwidget import BrowserWidgetTest 18 21 from zope.formlib.interfaces import IWidgetInputErrorView … … 128 131 self.assertEqual(self.content.foo.email, u'foo@foo.test') 129 132 133 def test_respect_custom_widgets(self): 134 # We can use our own subwidgets when creating ObjectWidgets 135 class CustomTextWidget(TextWidget): 136 pass 137 138 # We create a custom widget for the `name` field of ITestContact 139 name_widget = CustomTextWidget(ITestContact['name'], self.request) 140 141 # Custom widgets are passed by <fieldname>_widget keyword: 142 widget = self._WidgetFactory(self.field, self.request, 143 name_widget=name_widget) 144 widget1, widget2 = widget.subwidgets() 145 self.assertTrue(widget1 is name_widget) 146 147 130 148 class ObjectDisplayWidgetTest(BrowserWidgetTest): 131 149 """Documents and tests the display variant of object widget. … … 134 152 >>> verifyClass(IDisplayWidget, ObjectDisplayWidget) 135 153 True 136 """ 137 154 155 """ 156 _FieldFactory = Object 157 def _WidgetFactory(self, context, request, **kw): 158 kw.update({'factory': TestContact}) 159 return ObjectDisplayWidget(context, request, **kw) 160 161 def setUpContent(self, desc=u'', title=u'Foo Title'): 162 provideAdapter(TextWidget, (ITextLine, IDefaultBrowserLayer), 163 IInputWidget) 164 provideAdapter(DisplayWidget, (ITextLine, IDefaultBrowserLayer), 165 IDisplayWidget) 166 # The widget must be traversable. We register the default 167 # adapter that can turn nearly any object into an ITraversable. 168 provideAdapter(DefaultTraversable, (None,), ITraversable) 169 170 class TestObject(object): 171 implements(ITestContent) 172 173 self.content = TestObject() 174 self.content.name = u'Foo Name' 175 self.content.email = u'foo@foo.test' 176 self.field = ITestContent['foo'] 177 self.request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl') 178 self.request.form['field.foo'] = u'Foo Value' 179 self._widget = self._WidgetFactory(self.field, self.request) 180 181 def setUp(self): 182 super(ObjectDisplayWidgetTest, self).setUp() 183 self.field = Object(ITestContact, __name__=u'foo') 184 provideAdapter(TextWidget, 185 (ITextLine, IDefaultBrowserLayer), 186 IInputWidget) 187 provideAdapter(DisplayWidget, 188 (ITextLine, IDefaultBrowserLayer), 189 IDisplayWidget) 190 191 192 def test_interfaces(self): 193 from zope.interface.verify import DoesNotImplement 194 self.assertTrue(IDisplayWidget.providedBy(self._widget)) 195 self.assertFalse(IInputWidget.providedBy(self._widget)) 196 self.assertTrue(verifyClass(IDisplayWidget, ObjectDisplayWidget)) 197 198 def test_render(self): 199 widget = ObjectDisplayWidget( 200 self.field, self.request, TestContact) 201 widget.setRenderedValue(self.content) 202 203 check_list = [ 204 '<td', 'class=', 'row', 205 'Foo Name', 206 '</td>', '<td', 'class=', 'row', 207 'foo@foo.test', 208 '</td>'] 209 self.verifyResult(widget(), check_list, inorder=True) 138 210 139 211 def test_suite(): … … 141 213 unittest.makeSuite(ObjectWidgetTest), 142 214 unittest.makeSuite(ObjectDisplayWidgetTest), 143 #doctest.DocFileSuite('../objectwidget.txt',144 # setUp=testing.setUp,145 # tearDown=testing.tearDown),146 215 doctest.DocTestSuite(), 147 216 ))
Note: See TracChangeset for help on using the changeset viewer.