Refs #24121 -- Added __repr__() to Engine

This commit is contained in:
abhiabhi94 2021-05-28 06:15:40 +05:30 committed by Mariusz Felisiak
parent 55775891fb
commit c609d5149c
2 changed files with 57 additions and 0 deletions

View File

@ -52,6 +52,26 @@ class Engine:
self.builtins = self.default_builtins + builtins self.builtins = self.default_builtins + builtins
self.template_builtins = self.get_template_builtins(self.builtins) self.template_builtins = self.get_template_builtins(self.builtins)
def __repr__(self):
return (
'<%s:%s app_dirs=%s%s debug=%s loaders=%s string_if_invalid=%s '
'file_charset=%s%s%s autoescape=%s>'
) % (
self.__class__.__qualname__,
'' if not self.dirs else ' dirs=%s' % repr(self.dirs),
self.app_dirs,
''
if not self.context_processors
else ' context_processors=%s' % repr(self.context_processors),
self.debug,
repr(self.loaders),
repr(self.string_if_invalid),
repr(self.file_charset),
'' if not self.libraries else ' libraries=%s' % repr(self.libraries),
'' if not self.builtins else ' builtins=%s' % repr(self.builtins),
repr(self.autoescape),
)
@staticmethod @staticmethod
@functools.lru_cache() @functools.lru_cache()
def get_default(): def get_default():

View File

@ -10,6 +10,43 @@ from .utils import ROOT, TEMPLATE_DIR
OTHER_DIR = os.path.join(ROOT, 'other_templates') OTHER_DIR = os.path.join(ROOT, 'other_templates')
class EngineTest(SimpleTestCase):
def test_repr_empty(self):
engine = Engine()
self.assertEqual(
repr(engine),
"<Engine: app_dirs=False debug=False loaders=[("
"'django.template.loaders.cached.Loader', "
"['django.template.loaders.filesystem.Loader'])] "
"string_if_invalid='' file_charset='utf-8' builtins=["
"'django.template.defaulttags', 'django.template.defaultfilters', "
"'django.template.loader_tags'] autoescape=True>"
)
def test_repr(self):
engine = Engine(
dirs=[TEMPLATE_DIR],
context_processors=['django.template.context_processors.debug'],
debug=True,
loaders=['django.template.loaders.filesystem.Loader'],
string_if_invalid='x',
file_charset='utf-16',
libraries={'custom': 'template_tests.templatetags.custom'},
autoescape=False,
)
self.assertEqual(
repr(engine),
f"<Engine: dirs=[{TEMPLATE_DIR!r}] app_dirs=False "
"context_processors=['django.template.context_processors.debug'] "
"debug=True loaders=['django.template.loaders.filesystem.Loader'] "
"string_if_invalid='x' file_charset='utf-16' "
"libraries={'custom': 'template_tests.templatetags.custom'} "
"builtins=['django.template.defaulttags', "
"'django.template.defaultfilters', 'django.template.loader_tags'] "
"autoescape=False>"
)
class RenderToStringTest(SimpleTestCase): class RenderToStringTest(SimpleTestCase):
def setUp(self): def setUp(self):