diff options
| author | abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> | 2021-05-28 06:15:40 +0530 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-06-01 07:44:36 +0200 |
| commit | c609d5149c9295207cd7b2e8154e7b80a18d834a (patch) | |
| tree | 95f6622ee891044e332dea0c2012c1c4fa0eedc4 | |
| parent | 55775891fbfd8679b75336aa2f15ff9190e9f7a8 (diff) | |
Refs #24121 -- Added __repr__() to Engine
| -rw-r--r-- | django/template/engine.py | 20 | ||||
| -rw-r--r-- | tests/template_tests/test_engine.py | 37 |
2 files changed, 57 insertions, 0 deletions
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), + "<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): def setUp(self): |
