1
0
mirror of https://github.com/django/django.git synced 2024-12-23 17:46:27 +00:00
django/tests/forms_tests/widget_tests/base.py
Preston Timmons b52c73008a Fixed #15667 -- Added template-based widget rendering.
Thanks Carl Meyer and Tim Graham for contributing to the patch.
2016-12-27 17:50:10 -05:00

28 lines
1.1 KiB
Python

from django.forms.renderers import DjangoTemplates, Jinja2
from django.test import SimpleTestCase
try:
import jinja2
except ImportError:
jinja2 = None
class WidgetTest(SimpleTestCase):
beatles = (('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))
@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 [])
super(WidgetTest, cls).setUpClass()
def check_html(self, widget, name, value, html='', attrs=None, **kwargs):
if self.jinja2_renderer:
output = widget.render(name, value, attrs=attrs, renderer=self.jinja2_renderer, **kwargs)
# Django escapes quotes with '"' while Jinja2 uses '"'.
self.assertHTMLEqual(output.replace('"', '"'), html)
output = widget.render(name, value, attrs=attrs, renderer=self.django_renderer, **kwargs)
self.assertHTMLEqual(output, html)