2007-09-14 10:01:11 +08:00
|
|
|
# coding: utf-8
|
|
|
|
"""
|
2008-07-19 09:22:26 +08:00
|
|
|
Tests for forms/util.py module.
|
2007-09-14 10:01:11 +08:00
|
|
|
"""
|
|
|
|
|
2007-09-20 07:40:47 +08:00
|
|
|
tests = r"""
|
2008-07-19 09:22:26 +08:00
|
|
|
>>> from django.forms.util import *
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> from django.core.exceptions import ValidationError
|
2007-09-14 10:01:11 +08:00
|
|
|
>>> from django.utils.translation import ugettext_lazy
|
|
|
|
|
2007-09-14 10:05:45 +08:00
|
|
|
###########
|
|
|
|
# flatatt #
|
|
|
|
###########
|
|
|
|
|
2008-07-19 09:22:26 +08:00
|
|
|
>>> from django.forms.util import flatatt
|
2007-09-14 10:05:45 +08:00
|
|
|
>>> flatatt({'id': "header"})
|
|
|
|
u' id="header"'
|
|
|
|
>>> flatatt({'class': "news", 'title': "Read this"})
|
|
|
|
u' class="news" title="Read this"'
|
|
|
|
>>> flatatt({})
|
|
|
|
u''
|
|
|
|
|
2007-09-14 10:01:11 +08:00
|
|
|
###################
|
|
|
|
# ValidationError #
|
|
|
|
###################
|
|
|
|
|
|
|
|
# Can take a string.
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList(ValidationError("There was an error.").messages)
|
2007-09-14 10:01:11 +08:00
|
|
|
<ul class="errorlist"><li>There was an error.</li></ul>
|
|
|
|
|
|
|
|
# Can take a unicode string.
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList(ValidationError(u"Not \u03C0.").messages)
|
2007-09-14 10:01:11 +08:00
|
|
|
<ul class="errorlist"><li>Not π.</li></ul>
|
|
|
|
|
|
|
|
# Can take a lazy string.
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList(ValidationError(ugettext_lazy("Error.")).messages)
|
2007-09-14 10:01:11 +08:00
|
|
|
<ul class="errorlist"><li>Error.</li></ul>
|
|
|
|
|
|
|
|
# Can take a list.
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList(ValidationError(["Error one.", "Error two."]).messages)
|
2007-09-14 10:01:11 +08:00
|
|
|
<ul class="errorlist"><li>Error one.</li><li>Error two.</li></ul>
|
|
|
|
|
|
|
|
# Can take a mixture in a list.
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList(ValidationError(["First error.", u"Not \u03C0.", ugettext_lazy("Error.")]).messages)
|
2007-09-14 10:01:11 +08:00
|
|
|
<ul class="errorlist"><li>First error.</li><li>Not π.</li><li>Error.</li></ul>
|
2007-10-28 13:40:26 +08:00
|
|
|
|
|
|
|
>>> class VeryBadError:
|
|
|
|
... def __unicode__(self): return u"A very bad error."
|
|
|
|
|
|
|
|
# Can take a non-string.
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList(ValidationError(VeryBadError()).messages)
|
2007-10-28 13:40:26 +08:00
|
|
|
<ul class="errorlist"><li>A very bad error.</li></ul>
|
2008-11-07 03:49:24 +08:00
|
|
|
|
|
|
|
# Escapes non-safe input but not input marked safe.
|
|
|
|
>>> example = 'Example of link: <a href="http://www.example.com/">example</a>'
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList([example])
|
2008-11-07 03:49:24 +08:00
|
|
|
<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>
|
2010-01-05 11:56:19 +08:00
|
|
|
>>> print ErrorList([mark_safe(example)])
|
2008-11-07 03:49:24 +08:00
|
|
|
<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>
|
2007-09-14 10:01:11 +08:00
|
|
|
"""
|