Skip to content

Commit

Permalink
Add tests to check 'unicode decode error in weird cases in checkbox.C…
Browse files Browse the repository at this point in the history
…heckboxWidget.update and radio.RadioWidget.update' fix
  • Loading branch information
jribouxquadra committed Jul 5, 2013
1 parent 2def7fc commit 5c0acb9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/z3c/form/browser/checkbox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,55 @@ Check HIDDEN_MODE:
class="single-checkbox-widget" value="selected" />
</span>


Term with non ascii __str__
---------------------------

Check if a term which __str__ returns non ascii string does not crash the update method

>>> from zope.interface.verify import verifyClass
>>> from z3c.form import interfaces
>>> from z3c.form.browser import checkbox
>>> from z3c.form.testing import TestRequest

>>> request = TestRequest()

>>> widget = checkbox.CheckBoxWidget(request)
>>> widget.id = 'widget-id'
>>> widget.name = 'widget.name'

>>> import zope.schema.interfaces
>>> from zope.schema.vocabulary import SimpleVocabulary,SimpleTerm
>>> import z3c.form.term
>>> class ObjWithNonAscii__str__:
... def __str__(self):
... return 'héhé!'
>>> class MyTerms(z3c.form.term.ChoiceTermsVocabulary):
... def __init__(self, context, request, form, field, widget):
... self.terms = SimpleVocabulary([
... SimpleTerm(ObjWithNonAscii__str__(), 'one', 'One'),
... SimpleTerm(ObjWithNonAscii__str__(), 'two', 'Two'),
... ])
>>> zope.component.provideAdapter(MyTerms,
... adapts=(zope.interface.Interface,
... interfaces.IFormLayer, zope.interface.Interface,
... zope.interface.Interface, interfaces.ICheckBoxWidget))
>>> widget.update()
>>> print(widget.render())
<html>
<body>
<span class="option">
<input class="checkbox-widget" id="widget-id-0" name="widget.name:list" type="checkbox" value="one">
<label for="widget-id-0">
<span class="label">One</span>
</label>
</span>
<span class="option">
<input class="checkbox-widget" id="widget-id-1" name="widget.name:list" type="checkbox" value="two">
<label for="widget-id-1">
<span class="label">Two</span>
</label>
</span>
<input name="widget.name-empty-marker" type="hidden" value="1">
</body>
</html>
52 changes: 52 additions & 0 deletions src/z3c/form/browser/radio.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,55 @@ the value (which is used as a backup label) contains non-ASCII characters:

Note: The "\234" character is interpreted differently in Pytohn 2 and 3
here. (This is mostly due to changes int he SimpleVocabulary code.)

Term with non ascii __str__
---------------------------

Check if a term which __str__ returns non ascii string does not crash the update method

>>> request = TestRequest()
>>> widget = radio.RadioWidget(request)
>>> widget.id = 'widget-id'
>>> widget.name = 'widget.name'
>>> template = os.path.join(os.path.dirname(z3c.form.browser.__file__),
... 'radio_input.pt')
>>> factory = z3c.form.widget.WidgetTemplateFactory(template)
>>> zope.component.provideAdapter(factory,
... (zope.interface.Interface, IDefaultBrowserLayer, None, None, None),
... IPageTemplate, name='input')

>>> import zope.schema.interfaces
>>> from zope.schema.vocabulary import SimpleVocabulary,SimpleTerm
>>> import z3c.form.term
>>> class ObjWithNonAscii__str__:
... def __str__(self):
... return 'héhé!'
>>> class MyTerms(z3c.form.term.ChoiceTermsVocabulary):
... def __init__(self, context, request, form, field, widget):
... self.terms = SimpleVocabulary([
... SimpleTerm(ObjWithNonAscii__str__(), 'one', 'One'),
... SimpleTerm(ObjWithNonAscii__str__(), 'two', 'Two'),
... ])
>>> zope.component.provideAdapter(MyTerms,
... adapts=(zope.interface.Interface,
... interfaces.IFormLayer, zope.interface.Interface,
... zope.interface.Interface, interfaces.IRadioWidget))
>>> widget.update()
>>> print(widget.render())
<html>
<body>
<span class="option">
<label for="widget-id-0">
<input class="radio-widget" id="widget-id-0" name="widget.name" type="radio" value="one">
<span class="label">One</span>
</label>
</span>
<span class="option">
<label for="widget-id-1">
<input class="radio-widget" id="widget-id-1" name="widget.name" type="radio" value="two">
<span class="label">Two</span>
</label>
</span>
<input name="widget.name-empty-marker" type="hidden" value="1">
</body>
</html>

0 comments on commit 5c0acb9

Please sign in to comment.