Refs #24121 -- Added __repr__() to Origin and Template.

This commit is contained in:
Tiago Honorato 2021-03-12 19:37:47 -03:00 committed by Mariusz Felisiak
parent 065832eaec
commit 4f4f770f77
3 changed files with 26 additions and 0 deletions

View File

@ -122,6 +122,9 @@ class Origin:
def __str__(self):
return self.name
def __repr__(self):
return '<%s name=%r>' % (self.__class__.__qualname__, self.name)
def __eq__(self, other):
return (
isinstance(other, Origin) and
@ -158,6 +161,12 @@ class Template:
for node in self.nodelist:
yield from node
def __repr__(self):
return '<%s template_string="%s...">' % (
self.__class__.__qualname__,
self.source[:20].replace('\n', ''),
)
def _render(self, context):
return self.nodelist.render(context)

View File

@ -8,6 +8,17 @@ class TemplateTests(SimpleTestCase):
template_string = gettext_lazy('lazy string')
self.assertEqual(Template(template_string).render(Context()), template_string)
def test_repr(self):
template = Template(
'<html><body>\n'
'{% if test %}<h1>{{ varvalue }}</h1>{% endif %}'
'</body></html>'
)
self.assertEqual(
repr(template),
'<Template template_string="<html><body>{% if t...">',
)
class VariableDoesNotExistTests(SimpleTestCase):
def test_str(self):

View File

@ -1,3 +1,4 @@
import os
from unittest import TestCase
from django.template import Engine
@ -24,3 +25,8 @@ class OriginTestCase(TestCase):
# Use assertIs() to test __eq__/__ne__.
self.assertIs(a.origin == b.origin, False)
self.assertIs(a.origin != b.origin, True)
def test_repr(self):
a = self.engine.get_template('index.html')
name = os.path.join(TEMPLATE_DIR, 'index.html')
self.assertEqual(repr(a.origin), '<Origin name=%r>' % name)