Skip to content

Commit

Permalink
Merged r26237 from trunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Smith committed Jul 8, 2004
1 parent 0a3fa5d commit 4946261
Showing 1 changed file with 88 additions and 72 deletions.
160 changes: 88 additions & 72 deletions browser/tests/test_checkboxwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,82 +28,98 @@

class CheckBoxWidgetTest(SimpleInputWidgetTest):
"""Documents and tests thec checkbox widget.
>>> verifyClass(IInputWidget, CheckBoxWidget)
True
The rest of the this doctest was moved from widget.py to this test module
to keep widget.py free of detailed tests. XXX the tests below should be
more narrative to highlight the 'story' being told.
>>> field = Bool(__name__='foo', title=u'on')
>>> request = TestRequest(form={'field.foo.used': u'on',
... 'field.foo': u'on'})
>>> widget = CheckBoxWidget(field, request)
>>> widget.hasInput()
True
>>> widget.getInputValue()
True
>>> def normalize(s):
... return '\\n '.join(s.split())
>>> print normalize( widget() )
<input
class="hiddenType"
id="field.foo.used"
name="field.foo.used"
type="hidden"
value=""
/>
<input
class="checkboxType"
checked="checked"
id="field.foo"
name="field.foo"
type="checkbox"
/>
>>> print normalize( widget.hidden() )
<input
class="hiddenType"
id="field.foo"
name="field.foo"
type="hidden"
value="on"
/>
The checkbox widget works with Bool fields:
>>> field = Bool(__name__='foo', title=u'on')
>>> request = TestRequest()
>>> widget = CheckBoxWidget(field, request)
hasInput returns True when the request contains the field.<name>.used
value:
>>> 'field.foo.used' in request.form
False
>>> widget.hasInput()
False
>>> request.form['field.foo.used'] = ''
>>> widget.hasInput()
True
getInputValue returns True when field.<name> equals (and only equals) 'on':
>>> 'field.foo' in request.form
False
>>> widget.getInputValue()
False
>>> request.form['field.foo'] = 'true'
>>> widget.getInputValue()
False
>>> request.form['field.foo'] = 'on'
>>> widget.getInputValue()
True
Below is HTML output of rendered checkbox widgets. We will first define
a helper method condense the HTML output for display in this test:
>>> def normalize(s):
... return '\\n '.join(s.split())
Default widget rendering:
>>> print normalize( widget() )
<input
class="hiddenType"
id="field.foo.used"
name="field.foo.used"
type="hidden"
value=""
/>
<input
class="checkboxType"
checked="checked"
id="field.foo"
name="field.foo"
type="checkbox"
/>
Hidden rendering:
>>> print normalize( widget.hidden() )
<input
class="hiddenType"
id="field.foo"
name="field.foo"
type="hidden"
value="on"
/>
Calling setRenderedValue will change what gets output:
>>> widget.setRenderedValue(False)
>>> print normalize( widget() )
<input
class="hiddenType"
id="field.foo.used"
name="field.foo.used"
type="hidden"
value=""
/>
<input
class="checkboxType"
id="field.foo"
name="field.foo"
type="checkbox"
/>
When a checkbox is not 'checked', it's value is not
sent in the request, so we consider it 'False', which
means that 'required' for a boolean field doesn't make
much sense in the end.
>>> field = Bool(__name__='foo', title=u'on', required=True)
>>> request = TestRequest(form={'field.foo.used': u''})
>>> widget = CheckBoxWidget(field, request)
>>> widget.hasInput()
True
>>> widget.validate()
>>> widget.getInputValue()
False
>>> widget.setRenderedValue(False)
>>> print normalize( widget() )
<input
class="hiddenType"
id="field.foo.used"
name="field.foo.used"
type="hidden"
value=""
/>
<input
class="checkboxType"
id="field.foo"
name="field.foo"
type="checkbox"
/>
The checkbox widget does not support None values, so a Bool required
constraint will always be met with checkbox input:
>>> field.required = True
>>> widget.validate()
"""

_FieldFactory = Bool
Expand Down Expand Up @@ -140,7 +156,7 @@ def test_getInputValue(self):
del self._widget.request.form['field.foo']
self._widget.request.form['field.foo.used'] = ''
self.assertEquals(self._widget.getInputValue(), False)
del self._widget.request.form['field.foo.used']
del self._widget.request.form['field.foo.used']
self.assertRaises(MissingInputError, self._widget.getInputValue)


Expand Down

0 comments on commit 4946261

Please sign in to comment.