[Checkins] SVN: Sandbox/darrylcousins/mars.form/s More tests

Darryl Cousins darryl at darrylcousins.net.nz
Sat Jul 21 00:40:54 EDT 2007


Log message for revision 78241:
  More tests

Changed:
  U   Sandbox/darrylcousins/mars.form/setup.py
  A   Sandbox/darrylcousins/mars.form/src/mars/__init__.py
  U   Sandbox/darrylcousins/mars.form/src/mars/form/README.txt
  U   Sandbox/darrylcousins/mars.form/src/mars/form/__init__.py
  U   Sandbox/darrylcousins/mars.form/src/mars/form/components.py
  U   Sandbox/darrylcousins/mars.form/src/mars/form/directive.py
  U   Sandbox/darrylcousins/mars.form/src/mars/form/form.txt
  A   Sandbox/darrylcousins/mars.form/src/mars/form/ftests/__init__.py
  A   Sandbox/darrylcousins/mars.form/src/mars/form/ftests/display.pt
  A   Sandbox/darrylcousins/mars.form/src/mars/form/ftests/form.py
  A   Sandbox/darrylcousins/mars.form/src/mars/form/ftests/ftesting.zcml
  A   Sandbox/darrylcousins/mars.form/src/mars/form/ftests/template.pt
  A   Sandbox/darrylcousins/mars.form/src/mars/form/ftests/test_functional.py
  U   Sandbox/darrylcousins/mars.form/src/mars/form/meta.py
  U   Sandbox/darrylcousins/mars.form/src/mars/form/tests.py
  A   Sandbox/darrylcousins/mars.form/src/mars/form/widget.txt

-=-
Modified: Sandbox/darrylcousins/mars.form/setup.py
===================================================================
--- Sandbox/darrylcousins/mars.form/setup.py	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/setup.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -26,6 +26,7 @@
     extras_require = dict(
                 test=['zope.app.testing',
                       'zope.testbrowser',
+                      'mars.adapter',
         ]
                 ),
     install_requires = [
@@ -34,5 +35,8 @@
         'z3c.form',
         'z3c.formui',
         'z3c.layer',
+        'mars.view',
+        'mars.layer',
+        'mars.template',
         ],
 )

Added: Sandbox/darrylcousins/mars.form/src/mars/__init__.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/__init__.py	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/__init__.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1,7 @@
+try:
+    # Declare this a namespace package if pkg_resources is available.
+    import pkg_resources
+    pkg_resources.declare_namespace('mars')
+except ImportError:
+    pass
+


Property changes on: Sandbox/darrylcousins/mars.form/src/mars/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: Sandbox/darrylcousins/mars.form/src/mars/form/README.txt
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/README.txt	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/README.txt	2007-07-21 04:40:53 UTC (rev 78241)
@@ -2,9 +2,14 @@
 Mars Form
 =========
 
+``mars.form`` includes a FormView object, widget template implementation and
+layers derived from z3c.formui.
+
 Layers
 ------
 
+The following layers are provided by this package.
+
 * mars.form.IFormLayer
   Uses z3c.form.IFormLayer, z3c.layer.IPageletBrowserLayer
 

Modified: Sandbox/darrylcousins/mars.form/src/mars/form/__init__.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/__init__.py	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/__init__.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -1,4 +1,4 @@
-from directive import mode, view, field, widget
+from directive import view, field, widget
 from components import (IFormLayer,
                         IDivFormLayer,
                         ITableFormLayer,

Modified: Sandbox/darrylcousins/mars.form/src/mars/form/components.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/components.py	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/components.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -1,10 +1,14 @@
+import zope.interface
+
 from z3c.pagelet.interfaces import IPagelet
+from z3c.layer.pagelet import IPageletBrowserLayer
 from z3c.form.interfaces import IFormLayer as IZ3CFormLayer
 from z3c.formui.interfaces import IDivFormLayer as IZ3CDivFormLayer
 from z3c.formui.interfaces import ITableFormLayer as IZ3CTableFormLayer
 
 from mars.layer import ILayer
 
+## layers
 class IFormLayer(ILayer, IZ3CFormLayer, IPageletBrowserLayer):
     pass
 
@@ -14,6 +18,9 @@
 class ITableFormLayer(ILayer, IZ3CTableFormLayer, IZ3CFormLayer, IPageletBrowserLayer):
     pass
 
+## a widget template factory
+class WidgetTemplateFactory(object):
+    pass
 
 class FormView(object):
     """Vanilla view to mixin with z3c.form views"""

Modified: Sandbox/darrylcousins/mars.form/src/mars/form/directive.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/directive.py	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/directive.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -1,9 +1,6 @@
 from martian.directive import (InterfaceOrClassDirective,
-                               SingleTextDirective,
                                ClassDirectiveContext)
 
-mode = SingleTextDirective('mars.form.mode',
-                           ClassDirectiveContext())
 view = InterfaceOrClassDirective('mars.form.view',
                            ClassDirectiveContext())
 field = InterfaceOrClassDirective('mars.form.field',

Modified: Sandbox/darrylcousins/mars.form/src/mars/form/form.txt
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/form.txt	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/form.txt	2007-07-21 04:40:53 UTC (rev 78241)
@@ -2,8 +2,7 @@
 Mars Form
 =========
 
-``mars.form`` includes a FormView object, widget template implementation and
-layers derived from z3c.formui.
+The ``FormView`` class is a mixin for z3c.form views.
 
 Set up
 ------
@@ -21,12 +20,12 @@
   >>> from z3c.formui import layout
   >>> from zope.traversing.browser import absoluteURL
 
-And we'll access and set up the z3c.form package defaults.
+We need a request object to use in the tests, we use z3c.form testing request
+because it implements IFormLayer.
 
-  >>> from z3c.form import testing
-  >>> testing.setupFormDefaults()
+  >>> from z3c.form.testing import TestRequest
+  >>> request = TestRequest()
 
-
 Form View
 ---------
 
@@ -50,24 +49,57 @@
 
 Now create and grok an add form.
 
-  >>> class Add(mars.view.FormView, layout.AddFormLayoutSupport, form.AddForm):
+  >>> class Add(mars.form.FormView, layout.AddFormLayoutSupport, form.AddForm):
   ...     """ A sample add form."""
   ...     grok.context(zope.interface.Interface)
   ... 
   ...     label = u'Document Add Form'
   ...     fields = field.Fields(IDocument)
+
+In functional testing we can use z3c.formui defaults to render the form. Here we
+need a simple template for the form.
    
-  >>> from mars.view.meta import FormViewGrokker
-  >>> FormViewGrokker().grok('', Add, None, ModuleInfo(), None)
+  >>> template = os.path.join(temp_dir, 'template.pt')
+  >>> open(template, 'w').write('''
+  ...   <tal:block repeat="widget view/widgets/values">
+  ...   <div class="widget" tal:content="structure widget/render">
+  ...   </div>
+  ...   </tal:block>
+  ... ''')
+
+  >>> class Template(mars.template.TemplateFactory):
+  ...     grok.context(Add)
+  ...     grok.template(template) 
+
+Grok our form and template
+    
+  >>> from mars.form.meta import FormViewGrokker
+  >>> FormViewGrokker().grok('', Add, None, module_info, None)
   True
 
-  >>> view = zope.component.getMultiAdapter((content, request),
+  >>> from mars.template.meta import TemplateFactoryGrokker
+  >>> TemplateFactoryGrokker().grok('', Template, None, module_info, None)
+  True
+
+  >>> view = zope.component.getMultiAdapter((document, request),
   ...                                       name='add')
 
-We'll need a layout template to render the form into.
+We can now render the add form.
 
-  >>> template = os.path.join(temp_dir, 'template.pt')
-  >>> open(template, 'w').write('''
+  >>> view.update()
+  >>> print view.render()
+  <div class="widget"><input type="text" id="form-widgets-text"
+       name="form.widgets.text"
+       class="textWidget textline-field" value="" />
+  </div>
+
+Form as a pagelet
+-----------------
+
+Forms also may act as pagelets for which we need a layout template to render the form into.
+
+  >>> layout = os.path.join(temp_dir, 'layout.pt')
+  >>> open(layout, 'w').write('''
   ...   <html>
   ...     <body>
   ...       <div class="layout" tal:content="structure provider:pagelet">
@@ -78,20 +110,16 @@
   ... ''')
 
   >>> from zope.publisher.interfaces.browser import IBrowserPage
-  >>> class Template(mars.template.LayoutFactory):
+  >>> class Layout(mars.template.LayoutFactory):
   ...     """layout template for `home`"""
   ...     grok.context(IBrowserPage)
-  ...     grok.template(template) 
+  ...     grok.template(layout) 
     
-  >>> LayoutFactoryGrokker().grok('', Template, None, ModuleInfo(), None)
+  >>> from mars.template.meta import LayoutFactoryGrokker
+  >>> LayoutFactoryGrokker().grok('', Layout, None, module_info, None)
   True
 
-  >>> print view.render()
-  Traceback (most recent call last):
-  ...
-  ContentProviderLookupError: pagelet
-
-That's right, we need to register the content provider ``pagelet`` before we
+We need to register the content provider ``pagelet`` before we
 can use them. This is usually registered by inclusion of z3c.pagelet in
 site.zcml
 
@@ -100,12 +128,20 @@
   >>> zope.component.provideAdapter(provider.PageletRenderer,
   ...     provides=IContentProvider, name='pagelet')
 
-We also need to register the widget manager
 
-  >>> zope.component.provideAdapter(field.FieldWidgets)
-
   >>> print view()
+  <html>
+    <body>
+      <div class="layout">
+  <div class="widget"><input type="text" id="form-widgets-text"
+       name="form.widgets.text"
+       class="textWidget textline-field" value="" />
+  </div>
+  </div>
+    </body>
+  </html>
 
+
 Cleanup
 -------
 

Added: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/__init__.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/ftests/__init__.py	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/ftests/__init__.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1 @@
+#


Property changes on: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/display.pt
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/ftests/display.pt	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/ftests/display.pt	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1,5 @@
+<div>
+<div tal:replace="structure view/widgets/when/render" />
+<div tal:replace="structure view/widgets/who/render" />
+</div>
+

Added: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/form.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/ftests/form.py	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/ftests/form.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1,184 @@
+"""
+
+  >>> import grok
+  >>> grok.grok('mars.form.ftests.form')
+  >>> root = getRootFolder()
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+
+These tests make use of minimal layer
+
+  >>> skinURL = 'http://localhost/++skin++formskin'
+  >>> browser.open(skinURL + '/add')
+
+If we submit the form by clicking on add, ...
+
+  >>> browser.getControl('Add').click()
+
+... the same page returns telling us we have some errors:
+
+This is because we forgot to enter the "Who" field, which is required:
+
+  >>> print browser.contents
+  <html>
+  ...
+  <div class="summary">There were some errors.</div>
+  ...
+  <li>
+     Who: <div class="error">Required input is missing.</div>
+  </li>
+  ...
+  </html>
+
+Let's now fill out all the required fields and try to add the message again:
+
+  >>> browser.getControl('Who').value = u'Manfred'
+  >>> browser.getControl('Add').click()
+  >>> print browser.url
+  http://localhost/++skin++formskin/manfred
+
+  >>> print browser.contents
+  <div>
+  <span id="form-widgets-when" class="textWidget date-field">
+  ...
+  </span>
+  <span id="form-widgets-who"
+        class="textWidget textline-field">
+    Manfred
+  </span>
+  </div>
+
+The default value for when should have rendered today.
+
+  >>> import datetime
+  >>> root['manfred'].when == datetime.date.today()
+  True
+
+We can also edit manfred with the edit form.
+
+  >>> browser.open(skinURL + '/manfred/edit')
+
+Let us now change the date and the name to test the form.
+
+  >>> browser.getControl('Who').value = u'Manfred the man'
+  >>> browser.getControl('When').value = u'08/08/20'
+  >>> browser.getControl('Apply and View').click()
+  >>> print browser.url
+  http://localhost/++skin++formskin/manfred
+
+  >>> print browser.contents
+  <div>
+  <span id="form-widgets-when" class="textWidget date-field">
+  08/08/20
+  </span>
+  <span id="form-widgets-who"
+        class="textWidget textline-field">
+    Manfred the man
+  </span>
+  </div>
+
+"""
+import datetime
+
+import zope.interface
+import zope.schema
+from zope.schema import fieldproperty
+from zope.traversing.browser import absoluteURL
+from zope.app.folder.interfaces import IFolder
+from zope.publisher.interfaces.browser import IBrowserPage
+
+from z3c.form import form, field, widget, button
+from z3c.form.interfaces import IAddForm
+from z3c.formui import layout
+
+import grok
+
+import mars.view
+import mars.template
+import mars.layer
+import mars.adapter
+
+## set up the layer, skin and template
+class IMyFormLayer(mars.form.IDivFormLayer):
+    pass
+
+mars.layer.layer(IMyFormLayer)
+
+class FormSkin(mars.layer.Skin):
+    pass
+
+class Template(mars.template.LayoutFactory):
+    """main template for pages (note the context!)"""
+    grok.context(IBrowserPage)
+    grok.template('template.pt')
+
+class IMessage(zope.interface.Interface):
+
+    who = zope.schema.TextLine(
+        title=u'Who',
+        description=u'Name of the person sending the message',
+        required=True)
+
+    when = zope.schema.Date(
+        title=u'When',
+        description=u'Date of the message sent.',
+        required=True)
+
+class Message(grok.Model):
+    """Content object"""
+    zope.interface.implements(IMessage)
+
+    who = fieldproperty.FieldProperty(IMessage['who'])
+    when = fieldproperty.FieldProperty(IMessage['when'])
+
+    def __init__(self, who, when):
+        self.who = who
+        self.when = when
+
+class Edit(mars.form.FormView, layout.FormLayoutSupport, form.EditForm):
+    form.extends(form.EditForm)
+    label = u'Message Edit Form'
+    fields = field.Fields(IMessage)
+
+    @button.buttonAndHandler(u'Apply and View', name='applyView')
+    def handleApplyView(self, action):
+        self.handleApply(self, action)
+        if not self.widgets.errors:
+            url = absoluteURL(self.context, self.request)
+            self.request.response.redirect(url)
+
+class DefaultDate(mars.adapter.AdapterFactory):
+    """Create a default date adapter for the `when` field"""
+    grok.name('default')
+    mars.adapter.factory(widget.ComputedWidgetAttribute(
+                        lambda adapter: datetime.date.today(),
+                        field=IMessage['when'], view=IAddForm))
+
+class Add(mars.form.FormView, layout.AddFormLayoutSupport, form.AddForm):
+    grok.context(IFolder) #
+
+    label = u'Message Add Form'
+    fields = field.Fields(IMessage)
+
+    def create(self, data):
+        return Message(**data)
+
+    def add(self, object):
+        name = object.who.lower()
+        self._name = name
+        self.context[name] = object
+        return object
+
+    def nextURL(self):
+        return absoluteURL(self.context[self._name], self.request)
+
+class Display(mars.form.FormView, layout.FormLayoutSupport, form.DisplayForm):
+    grok.name('index')
+    fields = field.Fields(IMessage)
+
+class DisplayTemplate(mars.template.LayoutFactory):
+    grok.context(Display)
+    grok.template('display.pt')
+


Property changes on: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/form.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/ftesting.zcml
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/ftests/ftesting.zcml	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/ftests/ftesting.zcml	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1,28 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+           xmlns:meta="http://namespaces.zope.org/meta"
+           xmlns:browser="http://namespaces.zope.org/browser"
+           i18n_domain="zope"
+           package="mars.form.ftests">
+
+  <include package="grok" file="meta.zcml" />
+  <include package="mars.template" file="meta.zcml" />
+  <include package="mars.layer" file="meta.zcml" />
+  <include package="mars.view" file="meta.zcml" />
+  <include package="mars.adapter" file="meta.zcml" />
+  <include package="mars.form" file="meta.zcml" />
+
+  <include package="grok" />
+
+  <include package="zope.contentprovider" />
+  <include package="zope.viewlet" file="meta.zcml" />
+
+  <include package="z3c.form" file="meta.zcml" />
+  <include package="z3c.macro" file="meta.zcml" />
+  <include package="z3c.template" file="meta.zcml" />
+
+  <include package="z3c.form" />
+  <include package="z3c.formui" />
+  <include package="z3c.pagelet" />
+
+</configure>
+

Added: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/template.pt
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/ftests/template.pt	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/ftests/template.pt	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1,5 @@
+<html>
+<body>
+<tal:block replace="structure provider:pagelet" />
+</body>
+</html>

Added: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/test_functional.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/ftests/test_functional.py	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/ftests/test_functional.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1,32 @@
+import unittest
+from zope.testing import doctest
+
+from zope.app.testing.functional import FunctionalTestSetup, getRootFolder
+from zope.app.testing import functional
+functional.defineLayer('TestLayer', 'ftesting.zcml')
+
+optionflags = doctest.NORMALIZE_WHITESPACE + doctest.ELLIPSIS
+globs = dict(getRootFolder=getRootFolder)
+
+def setUp(test):
+    FunctionalTestSetup().setUp()
+
+def tearDown(test):
+    FunctionalTestSetup().tearDown()
+
+def test_suite():
+    suite = unittest.TestSuite()
+    dottedname = 'mars.form.ftests.%s'
+    for name in ['form']:
+        test = doctest.DocTestSuite(
+                    dottedname % name, setUp=setUp, globs=globs,
+                    tearDown=tearDown, optionflags=optionflags)
+        test.layer = TestLayer
+        suite.addTest(test)
+    return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+
+
+


Property changes on: Sandbox/darrylcousins/mars.form/src/mars/form/ftests/test_functional.py
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: Sandbox/darrylcousins/mars.form/src/mars/form/meta.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/meta.py	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/meta.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -18,13 +18,13 @@
 import grok
 from grok.util import check_adapts
 
-import mars.view
+import mars.form
 import mars.template
 from mars.view.meta import ViewGrokkerBase
 
 
 class FormViewGrokker(ViewGrokkerBase):
-    component_class = mars.view.FormView
+    component_class = mars.form.FormView
 
     def register(self, factory, module_info):
 
@@ -38,7 +38,7 @@
                                  name=self.view_name)
 
 class WidgetTemplateFactoryGrokker(martian.ClassGrokker):
-    component_class = mars.template.WidgetTemplateFactory
+    component_class = mars.form.WidgetTemplateFactory
     provides = IPageTemplate
 
     def grok(self, name, factory, context, module_info, templates):
@@ -67,7 +67,7 @@
         view_layer = util.class_annotation(factory, 'mars.layer.layer',
                                        None) or module_info.getAnnotation('mars.layer.layer',
                                        None) or IDefaultBrowserLayer
-        mode = util.class_annotation(factory, 'mars.form.mode', INPUT_MODE)
+        mode = util.class_annotation(factory, 'grok.name', INPUT_MODE)
         view = util.class_annotation(factory, 'mars.form.view', None)
         field = util.class_annotation(factory, 'mars.form.field', None)
         widget = util.class_annotation(factory, 'mars.form.widget', None)

Modified: Sandbox/darrylcousins/mars.form/src/mars/form/tests.py
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/tests.py	2007-07-21 03:36:17 UTC (rev 78240)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/tests.py	2007-07-21 04:40:53 UTC (rev 78241)
@@ -1,16 +1,45 @@
 import unittest
 from zope.testing import doctest
+from zope.app.testing import setup
+import zope.interface
 
+from martian.interfaces import IModuleInfo
+
+class ModuleInfo(object):
+    zope.interface.implements(IModuleInfo)
+    path = ''
+    package_dotted_name = ''
+
+    def getAnnotation(self, name, default):
+        return default
+
+from zope.app.testing import setup
+globs = dict(module_info=ModuleInfo(), root=setup.placefulSetUp(True))
+
 optionflags = doctest.NORMALIZE_WHITESPACE + doctest.ELLIPSIS
 
 def setUp(test):
-    pass
+    from z3c.form import testing
+    testing.setupFormDefaults()
+    # register provider TALES
+    from zope.app.pagetemplate import metaconfigure
+    from zope.contentprovider import tales
+    metaconfigure.registerType('provider', tales.TALESProviderExpression)
 
+def widgetSetUp(test):
+    setup.placefulSetUp(True)
+
+def tearDown(test):
+    setup.placefulTearDown()
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTests([doctest.DocFileSuite('./form.txt',
-                             setUp=setUp,
+                             setUp=setUp, globs=globs, tearDown=tearDown,
                              optionflags=optionflags),
+                    doctest.DocFileSuite('./widget.txt',
+                             setUp=widgetSetUp, globs=globs, tearDown=tearDown,
+                             optionflags=optionflags),
                    ])
 
     return suite

Added: Sandbox/darrylcousins/mars.form/src/mars/form/widget.txt
===================================================================
--- Sandbox/darrylcousins/mars.form/src/mars/form/widget.txt	                        (rev 0)
+++ Sandbox/darrylcousins/mars.form/src/mars/form/widget.txt	2007-07-21 04:40:53 UTC (rev 78241)
@@ -0,0 +1,71 @@
+Widget Template Factory
+=======================
+
+The widget template factory seeks to provide a way to define and register using
+``martian`` widget templates.
+
+First lets create a simple widget to use in the tests.
+
+  >>> from z3c.form.testing import TestRequest
+  >>> from z3c.form import widget
+  >>> request = TestRequest()
+  >>> age = widget.Widget(request)
+
+We'll assign some values to the widget.
+
+  >>> age.name = 'age'
+  >>> age.label = u'Age'
+  >>> age.value = '39'
+
+  >>> print age.render()
+  Traceback (most recent call last):
+  ...
+  ComponentLookupError: ... 'input')
+
+Because we have no template registered for the widget using INPUT_MODE we get
+this lookup error.
+
+Lets create and register a template for the widget. As with other mars packages
+the grok.name directive defines the ``name`` by which the adapter should be
+registered.
+
+  >>> from z3c.form.interfaces import INPUT_MODE
+
+  >>> import tempfile
+  >>> template = tempfile.mktemp('text.pt')
+  >>> open(template, 'w').write('''\
+  ... <input type="text" name="" value=""
+  ...        tal:attributes="name view/name; value view/value;" />\
+  ... ''')
+
+  >>> import zope.interface
+  >>> import grok
+  >>> import mars.form
+  >>> class AgeWidgetTemplateFactory(mars.form.WidgetTemplateFactory):
+  ...     grok.name(INPUT_MODE)
+  ...     grok.context(zope.interface.Interface)
+  ...     grok.template(template)
+
+We need to ``grok`` the template.
+
+  >>> from mars.form.meta import WidgetTemplateFactoryGrokker
+  >>> WidgetTemplateFactoryGrokker().grok('', AgeWidgetTemplateFactory,
+  ...                           zope.interface.Interface, module_info, None)
+  True
+
+Render the widget
+-----------------
+
+With a template now registered for the widget we can render the widget.
+
+  >>> age.update()
+  >>> print age.render()
+  <input type="text" name="age" value="39" />
+
+We also expect to be able to render values from the request.
+
+  >>> age.request = TestRequest(form={'age': '25'})
+  >>> age.update()
+  >>> print age.render()
+  <input type="text" name="age" value="25" />
+


Property changes on: Sandbox/darrylcousins/mars.form/src/mars/form/widget.txt
___________________________________________________________________
Name: svn:keywords
   + Date Author



More information about the Checkins mailing list