Ignore:
Timestamp:
22 Jul 2010, 15:45:39 (14 years ago)
Author:
uli
Message:

Add missing tests for WAeUPObjectWidget and add test case for
WAeUPDisplayWidget. objectwidget is now at 100% test coverage.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/widgets/tests/test_objectwidget.py

    r5309 r5311  
    11"""Tests for objectwidget.
    22"""
     3import doctest
    34import unittest
    45import sys
    56from zope.component import testing
    67from zope.interface import Interface, implements
     8from zope.interface.verify import verifyClass
    79from zope.publisher.browser import TestRequest
    810from zope.schema import Object, TextLine
    911from zope.schema.interfaces import ITextLine
    10 import doctest
     12from zope.traversing.adapters import DefaultTraversable
     13from zope.traversing.interfaces import ITraversable
    1114from zope.publisher.interfaces.browser import IDefaultBrowserLayer
    1215from zope.component import provideAdapter
     
    1417from zope.formlib.interfaces import (
    1518    IInputWidget, IDisplayWidget,MissingInputError)
    16 from zope.formlib.widgets import TextWidget
     19from zope.formlib.widgets import TextWidget, DisplayWidget
    1720from zope.formlib.tests.test_browserwidget import BrowserWidgetTest
    1821from zope.formlib.interfaces import IWidgetInputErrorView
     
    128131        self.assertEqual(self.content.foo.email, u'foo@foo.test')
    129132
     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       
    130148class ObjectDisplayWidgetTest(BrowserWidgetTest):
    131149    """Documents and tests the display variant of object widget.
     
    134152        >>> verifyClass(IDisplayWidget, ObjectDisplayWidget)
    135153        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)   
    138210
    139211def test_suite():
     
    141213        unittest.makeSuite(ObjectWidgetTest),
    142214        unittest.makeSuite(ObjectDisplayWidgetTest),
    143         #doctest.DocFileSuite('../objectwidget.txt',
    144         #                     setUp=testing.setUp,
    145         #                     tearDown=testing.tearDown),
    146215        doctest.DocTestSuite(),
    147216        ))
Note: See TracChangeset for help on using the changeset viewer.