2016-12-28 06:00:56 +08:00
|
|
|
from django.forms.renderers import DjangoTemplates, Jinja2
|
2015-08-31 10:13:42 +08:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
try:
|
|
|
|
import jinja2
|
|
|
|
except ImportError:
|
|
|
|
jinja2 = None
|
|
|
|
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
class WidgetTest(SimpleTestCase):
|
|
|
|
beatles = (('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.django_renderer = DjangoTemplates()
|
|
|
|
cls.jinja2_renderer = Jinja2() if jinja2 else None
|
|
|
|
cls.renderers = [cls.django_renderer] + ([cls.jinja2_renderer] if cls.jinja2_renderer else [])
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2016-12-28 06:00:56 +08:00
|
|
|
|
2017-01-07 08:11:32 +08:00
|
|
|
def check_html(self, widget, name, value, html='', attrs=None, strict=False, **kwargs):
|
|
|
|
assertEqual = self.assertEqual if strict else self.assertHTMLEqual
|
2016-12-28 06:00:56 +08:00
|
|
|
if self.jinja2_renderer:
|
|
|
|
output = widget.render(name, value, attrs=attrs, renderer=self.jinja2_renderer, **kwargs)
|
|
|
|
# Django escapes quotes with '"' while Jinja2 uses '"'.
|
2019-04-24 19:30:34 +08:00
|
|
|
output = output.replace('"', '"')
|
|
|
|
# Django escapes single quotes with ''' while Jinja2 uses '''.
|
|
|
|
output = output.replace(''', ''')
|
|
|
|
assertEqual(output, html)
|
2016-12-28 06:00:56 +08:00
|
|
|
|
|
|
|
output = widget.render(name, value, attrs=attrs, renderer=self.django_renderer, **kwargs)
|
2017-01-07 08:11:32 +08:00
|
|
|
assertEqual(output, html)
|