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