[Zope3-checkins] SVN: zope.formlib/branches/faassen-zaf/src/zope/formlib/ Move browserwidget test from zope.app.form.

Martijn Faassen faassen at startifact.com
Wed Dec 30 16:07:07 EST 2009


Log message for revision 107391:
  Move browserwidget test from zope.app.form.
  

Changed:
  A   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py
  A   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_browserwidget.py
  A   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_exception.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py

-=-
Copied: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py (from rev 107362, zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/tests/support.py)
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py	                        (rev 0)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py	2009-12-30 21:07:06 UTC (rev 107391)
@@ -0,0 +1,34 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation 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.
+#
+##############################################################################
+"""General test support.
+
+$Id$
+"""
+
+class VerifyResults(object):
+    """Mix-in for test classes with helpers for checking string data."""
+
+    def verifyResult(self, result, check_list, inorder=False):
+        start = 0
+        for check in check_list:
+            pos = result.find(check, start)
+            self.assert_(pos >= 0,
+                         "%r not found in %r" % (check, result[start:]))
+            if inorder:
+                start = pos + len(check)
+
+    def verifyResultMissing(self, result, check_list):
+        for check in check_list:
+            self.assert_(result.find(check) < 0,
+                         "%r unexpectedly found in %r" % (check, result))

Copied: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_browserwidget.py (from rev 107373, zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/tests/test_browserwidget.py)
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_browserwidget.py	                        (rev 0)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_browserwidget.py	2009-12-30 21:07:06 UTC (rev 107391)
@@ -0,0 +1,214 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation 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.
+#
+##############################################################################
+"""Test Browser Widget
+
+$Id$
+"""
+import unittest
+from zope.component.testing import PlacelessSetup
+from zope.interface import Interface, implements
+from zope.publisher.browser import TestRequest
+from zope.schema import Text, Int
+from zope.testing.doctest import DocTestSuite
+
+from zope.formlib.widget import SimpleInputWidget
+from zope.formlib.interfaces import ConversionError
+from zope.formlib.interfaces import WidgetInputError, MissingInputError
+
+from zope.formlib.tests import support
+
+class BrowserWidgetTest(PlacelessSetup,
+                        support.VerifyResults,
+                        unittest.TestCase):
+
+    _FieldFactory = Text
+    _WidgetFactory = None
+
+    def setUpContent(self, desc=u'', title=u'Foo Title'):
+        field = self._FieldFactory(
+            __name__='foo', title=title, description=desc)
+        class ITestContent(Interface):
+            foo = field
+        class TestObject:
+            implements(ITestContent)
+        self.content = TestObject()
+        field = ITestContent['foo']
+        field = field.bind(self.content)
+        request = TestRequest(HTTP_ACCEPT_LANGUAGE='ru')
+        request.form['field.foo'] = u'Foo Value'
+        self._widget = self._WidgetFactory(field, request)
+
+    def setUp(self):
+        super(BrowserWidgetTest, self).setUp()
+        self.setUpContent()
+
+
+class SimpleInputWidgetTest(BrowserWidgetTest):
+
+    _WidgetFactory = SimpleInputWidget
+
+    def test_required(self):
+        # widget required defaults to its context required
+        self.assert_(self._widget.required)
+        self.assert_(self._widget.context.required)
+        # changing widget context required has no effect on widget required
+        self._widget.context.required = False
+        self.assert_(self._widget.required)
+        self.assert_(not self._widget.context.required)
+
+    def test_hasInput(self):
+        self.failUnless(self._widget.hasInput())
+        del self._widget.request.form['field.foo']
+        self.failIf(self._widget.hasInput())
+
+    def testProperties(self):
+        self.assertEqual(self._widget.tag, 'input')
+        self.assertEqual(self._widget.type, 'text')
+        self.assertEqual(self._widget.cssClass, '')
+        self.assertEqual(self._widget.extra, '')
+
+    def testRender(self, value=None, check_list=None):
+        if value is None:
+            value = 'Foo Value'
+        if check_list is None:
+            check_list = ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                          'value="Foo Value"')
+        self._widget.setRenderedValue(value)
+        self.verifyResult(self._widget(), check_list)
+        check_list = ('type="hidden"',) + check_list[1:]
+        self.verifyResult(self._widget.hidden(), check_list)
+        check_list = ('type="hidden"', 'style="color: red"') + check_list[1:]
+        self._widget.extra = 'style="color: red"'
+        self.verifyResult(self._widget.hidden(), check_list)
+
+    def testLabel(self):
+        self.setUpContent(title=u'Foo:')
+        self.assertEqual(self._widget.label, u'Foo:')
+
+    def testHint(self):
+        self.setUpContent(desc=u'Foo Description')
+        self.assertEqual(self._widget.hint, u'Foo Description')
+
+
+class TestWidget(SimpleInputWidget):
+
+    def _toFieldValue(self, v):
+        if v == u'barf!':
+            raise ConversionError('ralph')
+        return v or None
+
+class Test(BrowserWidgetTest):
+
+    _WidgetFactory = TestWidget
+
+    def test_getFormValue(self):
+
+        class W(SimpleInputWidget):
+            def _toFieldValue(self, v):
+                return u'X' + (v or '')
+
+            def _toFormValue(self, v):
+                return v and v[1:] or ''
+
+        field = Text(__name__ = 'foo', title = u"Foo Title")
+        request = TestRequest()
+
+        w = W(field, request)
+        self.assertEqual(w._getFormValue(), '')
+        request.form['field.foo'] = 'val'
+        self.assertEqual(w._getFormValue(), 'val')
+
+        w.setRenderedValue('Xfoo')
+        self.assertEqual(w._getFormValue(), 'foo')
+
+    def test_hasValidInput(self):
+        self.assertEqual(self._widget.getInputValue(), u'Foo Value')
+
+        self._widget.request.form['field.foo'] = (1, 2)
+        self.failIf(self._widget.hasValidInput())
+
+        self._widget.request.form['field.foo'] = u'barf!'
+        self.failIf(self._widget.hasValidInput())
+
+        del self._widget.request.form['field.foo']
+        self._widget.context.required = True
+        self.failIf(self._widget.hasValidInput())
+
+        self._widget.context.required = False
+        self._widget.request.form['field.foo'] = u''
+        self.failUnless(self._widget.hasValidInput())
+
+    def test_getInputValue(self):
+        self.assertEqual(self._widget.getInputValue(), u'Foo Value')
+
+        self._widget.request.form['field.foo'] = (1, 2)
+        self.assertRaises(WidgetInputError, self._widget.getInputValue)
+
+        self._widget.request.form['field.foo'] = u'barf!'
+        self.assertRaises(ConversionError, self._widget.getInputValue)
+
+        del self._widget.request.form['field.foo']
+        self._widget.context.required = True
+        self.assertRaises(MissingInputError, self._widget.getInputValue)
+
+        self._widget.context.required = False
+        self._widget.request.form['field.foo'] = u''
+        self.assertEqual(self._widget.getInputValue(), None)
+
+    def test_applyChanges(self):
+        self.assertEqual(self._widget.applyChanges(self.content), True)
+
+    def test_hasInput(self):
+        self.failUnless(self._widget.hasInput())
+        del self._widget.request.form['field.foo']
+        self.failIf(self._widget.hasInput())
+        self._widget.request.form['field.foo'] = u'foo'
+        self.failUnless(self._widget.hasInput())
+        # widget has input, even if input is an empty string
+        self._widget.request.form['field.foo'] = u''
+        self.failUnless(self._widget.hasInput())
+
+    def test_getFormValue_w_default(self):
+        field = Text(__name__ = 'foo', title = u"Foo Title", default=u"def")
+        request = TestRequest()
+        widget = self._WidgetFactory(field, request)
+        self.assertEqual(widget._getFormValue(), u'def')
+
+    def test_getFormValue_preserves_errors(self):
+        field = Int(__name__ = 'foo', title = u"Foo Title", default=42)
+        request = TestRequest()
+        widget = self._WidgetFactory(field, request)
+
+        # Sometimes you want to set a custom error on a widget.
+        widget._error = 'my error'
+
+        # _getFormValue shouldn't replace it.
+        request.form['field.foo'] = u'barf!'
+        widget._getFormValue()
+        self.assertEquals(widget._error, 'my error')
+
+        # _getFormValue shouldn't clear it either
+        request.form['field.foo'] = 33
+        widget._getFormValue()
+        self.assertEquals(widget._error, 'my error')
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(Test))
+    suite.addTest(DocTestSuite("zope.formlib.widget"))
+    return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')

Copied: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_exception.py (from rev 107362, zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/tests/test_exception.py)
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_exception.py	                        (rev 0)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_exception.py	2009-12-30 21:07:06 UTC (rev 107391)
@@ -0,0 +1,27 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation 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.
+#
+##############################################################################
+"""Exception Views tests
+
+$Id$
+"""
+import unittest
+from zope.testing.doctest import DocTestSuite
+
+def test_suite():
+    return unittest.TestSuite((
+        DocTestSuite('zope.formlib.exception'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py	2009-12-30 20:47:46 UTC (rev 107390)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py	2009-12-30 21:07:06 UTC (rev 107391)
@@ -19,10 +19,11 @@
 
 from xml.sax.saxutils import quoteattr, escape
 
-from zope.component import getMultiAdapter
+from zope.component import getMultiAdapter, provideAdapter
 from zope.interface import implements
 from zope.schema.interfaces import ValidationError
 from zope.publisher.browser import BrowserView
+from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 
 from zope.formlib.interfaces import ConversionError
 from zope.formlib.interfaces import WidgetInputError, MissingInputError
@@ -174,10 +175,10 @@
         ...         self.context = context
         ...     def snippet(self):
         ...         return "The error: " + str(self.context.errors)
-        >>> from zope.app.testing import ztapi
-        >>> ztapi.browserViewProviding(IWidgetInputError, SnippetErrorView,
-        ...                            IWidgetInputErrorView)
-
+        >>> provideAdapter(SnippetErrorView,
+        ...                (IWidgetInputError, IDefaultBrowserLayer),
+        ...                IWidgetInputErrorView, '')
+        
     Whever an error occurs, widgets should set _error:
 
         >>> widget._error = WidgetInputError('foo', 'Foo', ('Err1', 'Err2'))



More information about the Zope3-Checkins mailing list