From c609d5149c9295207cd7b2e8154e7b80a18d834a Mon Sep 17 00:00:00 2001 From: abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> Date: Fri, 28 May 2021 06:15:40 +0530 Subject: [PATCH] Refs #24121 -- Added __repr__() to Engine --- django/template/engine.py | 20 ++++++++++++++++ tests/template_tests/test_engine.py | 37 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/django/template/engine.py b/django/template/engine.py index 23a66e7a97..0adc20cfb3 100644 --- a/django/template/engine.py +++ b/django/template/engine.py @@ -52,6 +52,26 @@ class Engine: self.builtins = self.default_builtins + 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 @functools.lru_cache() def get_default(): diff --git a/tests/template_tests/test_engine.py b/tests/template_tests/test_engine.py index b975ea87b4..2b32211761 100644 --- a/tests/template_tests/test_engine.py +++ b/tests/template_tests/test_engine.py @@ -10,6 +10,43 @@ from .utils import ROOT, TEMPLATE_DIR OTHER_DIR = os.path.join(ROOT, 'other_templates') +class EngineTest(SimpleTestCase): + def test_repr_empty(self): + engine = Engine() + self.assertEqual( + repr(engine), + "" + ) + + 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"" + ) + + class RenderToStringTest(SimpleTestCase): def setUp(self):