From 9c19aff7c7561e3a82978a272ecdaad40dda5c00 Mon Sep 17 00:00:00 2001 From: django-bot Date: Thu, 3 Feb 2022 20:24:19 +0100 Subject: Refs #33476 -- Reformatted code with Black. --- tests/template_tests/syntax_tests/i18n/base.py | 2 +- .../syntax_tests/i18n/test_blocktranslate.py | 805 +++++++++++++-------- .../syntax_tests/i18n/test_filters.py | 76 +- .../i18n/test_get_available_languages.py | 20 +- .../syntax_tests/i18n/test_get_current_language.py | 6 +- .../i18n/test_get_current_language_bidi.py | 6 +- .../syntax_tests/i18n/test_get_language_info.py | 54 +- .../i18n/test_get_language_info_list.py | 70 +- .../syntax_tests/i18n/test_language.py | 10 +- .../syntax_tests/i18n/test_translate.py | 272 ++++--- .../syntax_tests/i18n/test_underscore_syntax.py | 98 +-- .../template_tests/syntax_tests/test_autoescape.py | 155 ++-- tests/template_tests/syntax_tests/test_basic.py | 253 +++---- tests/template_tests/syntax_tests/test_builtins.py | 19 +- tests/template_tests/syntax_tests/test_cache.py | 211 +++--- tests/template_tests/syntax_tests/test_comment.py | 111 +-- tests/template_tests/syntax_tests/test_cycle.py | 267 ++++--- tests/template_tests/syntax_tests/test_debug.py | 47 +- .../template_tests/syntax_tests/test_exceptions.py | 29 +- tests/template_tests/syntax_tests/test_extends.py | 294 ++++---- .../syntax_tests/test_filter_syntax.py | 139 ++-- .../template_tests/syntax_tests/test_filter_tag.py | 41 +- tests/template_tests/syntax_tests/test_firstof.py | 103 ++- tests/template_tests/syntax_tests/test_for.py | 350 ++++++--- tests/template_tests/syntax_tests/test_if.py | 761 ++++++++++--------- .../template_tests/syntax_tests/test_if_changed.py | 348 ++++++--- tests/template_tests/syntax_tests/test_include.py | 307 ++++---- .../syntax_tests/test_invalid_string.py | 64 +- .../template_tests/syntax_tests/test_list_index.py | 55 +- tests/template_tests/syntax_tests/test_load.py | 76 +- tests/template_tests/syntax_tests/test_lorem.py | 33 +- .../template_tests/syntax_tests/test_multiline.py | 5 +- .../syntax_tests/test_named_endblock.py | 75 +- tests/template_tests/syntax_tests/test_now.py | 97 ++- tests/template_tests/syntax_tests/test_numpy.py | 19 +- tests/template_tests/syntax_tests/test_regroup.py | 197 +++-- .../template_tests/syntax_tests/test_resetcycle.py | 169 +++-- tests/template_tests/syntax_tests/test_setup.py | 23 +- .../template_tests/syntax_tests/test_simple_tag.py | 18 +- .../template_tests/syntax_tests/test_spaceless.py | 49 +- tests/template_tests/syntax_tests/test_static.py | 95 ++- .../syntax_tests/test_template_tag.py | 71 +- tests/template_tests/syntax_tests/test_url.py | 296 ++++---- tests/template_tests/syntax_tests/test_verbatim.py | 55 +- .../syntax_tests/test_width_ratio.py | 160 ++-- tests/template_tests/syntax_tests/test_with.py | 62 +- 46 files changed, 3760 insertions(+), 2713 deletions(-) (limited to 'tests/template_tests/syntax_tests') diff --git a/tests/template_tests/syntax_tests/i18n/base.py b/tests/template_tests/syntax_tests/i18n/base.py index 61d2bbdb1c..6197179ee4 100644 --- a/tests/template_tests/syntax_tests/i18n/base.py +++ b/tests/template_tests/syntax_tests/i18n/base.py @@ -7,7 +7,7 @@ from django.utils.translation import activate, get_language here = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) pdir = os.path.split(os.path.split(os.path.abspath(here))[0])[0] extended_locale_paths = settings.LOCALE_PATHS + [ - os.path.join(pdir, 'i18n', 'other', 'locale'), + os.path.join(pdir, "i18n", "other", "locale"), ] diff --git a/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py index 114d4392c6..8ad004a899 100644 --- a/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py +++ b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py @@ -18,18 +18,18 @@ from .base import MultipleLocaleActivationTestCase, extended_locale_paths, here def setup(templates, *args, **kwargs): blocktranslate_setup = base_setup(templates, *args, **kwargs) - blocktrans_setup = base_setup({ - name: template.replace( - '{% blocktranslate ', '{% blocktrans ' - ).replace( - '{% endblocktranslate %}', '{% endblocktrans %}' - ) - for name, template in templates.items() - }) + blocktrans_setup = base_setup( + { + name: template.replace("{% blocktranslate ", "{% blocktrans ").replace( + "{% endblocktranslate %}", "{% endblocktrans %}" + ) + for name, template in templates.items() + } + ) tags = { - 'blocktrans': blocktrans_setup, - 'blocktranslate': blocktranslate_setup, + "blocktrans": blocktrans_setup, + "blocktranslate": blocktranslate_setup, } def decorator(func): @@ -37,300 +37,459 @@ def setup(templates, *args, **kwargs): def inner(self, *args): signature = inspect.signature(func) for tag_name, setup_func in tags.items(): - if 'tag_name' in signature.parameters: + if "tag_name" in signature.parameters: setup_func(partial(func, tag_name=tag_name))(self) else: setup_func(func)(self) + return inner + return decorator class I18nBlockTransTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} + libraries = {"i18n": "django.templatetags.i18n"} - @setup({'i18n03': '{% load i18n %}{% blocktranslate %}{{ anton }}{% endblocktranslate %}'}) + @setup( + { + "i18n03": "{% load i18n %}{% blocktranslate %}{{ anton }}{% endblocktranslate %}" + } + ) def test_i18n03(self): """simple translation of a variable""" - output = self.engine.render_to_string('i18n03', {'anton': 'Å'}) - self.assertEqual(output, 'Å') - - @setup({'i18n04': '{% load i18n %}{% blocktranslate with berta=anton|lower %}{{ berta }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n03", {"anton": "Å"}) + self.assertEqual(output, "Å") + + @setup( + { + "i18n04": "{% load i18n %}{% blocktranslate with berta=anton|lower %}{{ berta }}{% endblocktranslate %}" + } + ) def test_i18n04(self): """simple translation of a variable and filter""" - output = self.engine.render_to_string('i18n04', {'anton': 'Å'}) - self.assertEqual(output, 'å') - - @setup({'legacyi18n04': '{% load i18n %}' - '{% blocktranslate with anton|lower as berta %}{{ berta }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n04", {"anton": "Å"}) + self.assertEqual(output, "å") + + @setup( + { + "legacyi18n04": "{% load i18n %}" + "{% blocktranslate with anton|lower as berta %}{{ berta }}{% endblocktranslate %}" + } + ) def test_legacyi18n04(self): """simple translation of a variable and filter""" - output = self.engine.render_to_string('legacyi18n04', {'anton': 'Å'}) - self.assertEqual(output, 'å') - - @setup({'i18n05': '{% load i18n %}{% blocktranslate %}xxx{{ anton }}xxx{% endblocktranslate %}'}) + output = self.engine.render_to_string("legacyi18n04", {"anton": "Å"}) + self.assertEqual(output, "å") + + @setup( + { + "i18n05": "{% load i18n %}{% blocktranslate %}xxx{{ anton }}xxx{% endblocktranslate %}" + } + ) def test_i18n05(self): """simple translation of a string with interpolation""" - output = self.engine.render_to_string('i18n05', {'anton': 'yyy'}) - self.assertEqual(output, 'xxxyyyxxx') - - @setup({'i18n07': '{% load i18n %}' - '{% blocktranslate count counter=number %}singular{% plural %}' - '{{ counter }} plural{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n05", {"anton": "yyy"}) + self.assertEqual(output, "xxxyyyxxx") + + @setup( + { + "i18n07": "{% load i18n %}" + "{% blocktranslate count counter=number %}singular{% plural %}" + "{{ counter }} plural{% endblocktranslate %}" + } + ) def test_i18n07(self): """translation of singular form""" - output = self.engine.render_to_string('i18n07', {'number': 1}) - self.assertEqual(output, 'singular') - - @setup({'legacyi18n07': '{% load i18n %}' - '{% blocktranslate count number as counter %}singular{% plural %}' - '{{ counter }} plural{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n07", {"number": 1}) + self.assertEqual(output, "singular") + + @setup( + { + "legacyi18n07": "{% load i18n %}" + "{% blocktranslate count number as counter %}singular{% plural %}" + "{{ counter }} plural{% endblocktranslate %}" + } + ) def test_legacyi18n07(self): """translation of singular form""" - output = self.engine.render_to_string('legacyi18n07', {'number': 1}) - self.assertEqual(output, 'singular') - - @setup({'i18n08': '{% load i18n %}' - '{% blocktranslate count number as counter %}singular{% plural %}' - '{{ counter }} plural{% endblocktranslate %}'}) + output = self.engine.render_to_string("legacyi18n07", {"number": 1}) + self.assertEqual(output, "singular") + + @setup( + { + "i18n08": "{% load i18n %}" + "{% blocktranslate count number as counter %}singular{% plural %}" + "{{ counter }} plural{% endblocktranslate %}" + } + ) def test_i18n08(self): """translation of plural form""" - output = self.engine.render_to_string('i18n08', {'number': 2}) - self.assertEqual(output, '2 plural') - - @setup({'legacyi18n08': '{% load i18n %}' - '{% blocktranslate count counter=number %}singular{% plural %}' - '{{ counter }} plural{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n08", {"number": 2}) + self.assertEqual(output, "2 plural") + + @setup( + { + "legacyi18n08": "{% load i18n %}" + "{% blocktranslate count counter=number %}singular{% plural %}" + "{{ counter }} plural{% endblocktranslate %}" + } + ) def test_legacyi18n08(self): """translation of plural form""" - output = self.engine.render_to_string('legacyi18n08', {'number': 2}) - self.assertEqual(output, '2 plural') - - @setup({'i18n17': '{% load i18n %}' - '{% blocktranslate with berta=anton|escape %}{{ berta }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("legacyi18n08", {"number": 2}) + self.assertEqual(output, "2 plural") + + @setup( + { + "i18n17": "{% load i18n %}" + "{% blocktranslate with berta=anton|escape %}{{ berta }}{% endblocktranslate %}" + } + ) def test_i18n17(self): """ Escaping inside blocktranslate and translate works as if it was directly in the template. """ - output = self.engine.render_to_string('i18n17', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @setup({'i18n18': '{% load i18n %}' - '{% blocktranslate with berta=anton|force_escape %}{{ berta }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n17", {"anton": "α & β"}) + self.assertEqual(output, "α & β") + + @setup( + { + "i18n18": "{% load i18n %}" + "{% blocktranslate with berta=anton|force_escape %}{{ berta }}{% endblocktranslate %}" + } + ) def test_i18n18(self): - output = self.engine.render_to_string('i18n18', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @setup({'i18n19': '{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n18", {"anton": "α & β"}) + self.assertEqual(output, "α & β") + + @setup( + { + "i18n19": "{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}" + } + ) def test_i18n19(self): - output = self.engine.render_to_string('i18n19', {'andrew': 'a & b'}) - self.assertEqual(output, 'a & b') - - @setup({'i18n21': '{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n19", {"andrew": "a & b"}) + self.assertEqual(output, "a & b") + + @setup( + { + "i18n21": "{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}" + } + ) def test_i18n21(self): - output = self.engine.render_to_string('i18n21', {'andrew': mark_safe('a & b')}) - self.assertEqual(output, 'a & b') - - @setup({'legacyi18n17': '{% load i18n %}' - '{% blocktranslate with anton|escape as berta %}{{ berta }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n21", {"andrew": mark_safe("a & b")}) + self.assertEqual(output, "a & b") + + @setup( + { + "legacyi18n17": "{% load i18n %}" + "{% blocktranslate with anton|escape as berta %}{{ berta }}{% endblocktranslate %}" + } + ) def test_legacyi18n17(self): - output = self.engine.render_to_string('legacyi18n17', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @setup({'legacyi18n18': '{% load i18n %}' - '{% blocktranslate with anton|force_escape as berta %}' - '{{ berta }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("legacyi18n17", {"anton": "α & β"}) + self.assertEqual(output, "α & β") + + @setup( + { + "legacyi18n18": "{% load i18n %}" + "{% blocktranslate with anton|force_escape as berta %}" + "{{ berta }}{% endblocktranslate %}" + } + ) def test_legacyi18n18(self): - output = self.engine.render_to_string('legacyi18n18', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @setup({'i18n26': '{% load i18n %}' - '{% blocktranslate with extra_field=myextra_field count counter=number %}' - 'singular {{ extra_field }}{% plural %}plural{% endblocktranslate %}'}) + output = self.engine.render_to_string("legacyi18n18", {"anton": "α & β"}) + self.assertEqual(output, "α & β") + + @setup( + { + "i18n26": "{% load i18n %}" + "{% blocktranslate with extra_field=myextra_field count counter=number %}" + "singular {{ extra_field }}{% plural %}plural{% endblocktranslate %}" + } + ) def test_i18n26(self): """ translation of plural form with extra field in singular form (#13568) """ - output = self.engine.render_to_string('i18n26', {'myextra_field': 'test', 'number': 1}) - self.assertEqual(output, 'singular test') - - @setup({'legacyi18n26': '{% load i18n %}' - '{% blocktranslate with myextra_field as extra_field count number as counter %}' - 'singular {{ extra_field }}{% plural %}plural{% endblocktranslate %}'}) + output = self.engine.render_to_string( + "i18n26", {"myextra_field": "test", "number": 1} + ) + self.assertEqual(output, "singular test") + + @setup( + { + "legacyi18n26": "{% load i18n %}" + "{% blocktranslate with myextra_field as extra_field count number as counter %}" + "singular {{ extra_field }}{% plural %}plural{% endblocktranslate %}" + } + ) def test_legacyi18n26(self): - output = self.engine.render_to_string('legacyi18n26', {'myextra_field': 'test', 'number': 1}) - self.assertEqual(output, 'singular test') - - @setup({'i18n27': '{% load i18n %}{% blocktranslate count counter=number %}' - '{{ counter }} result{% plural %}{{ counter }} results' - '{% endblocktranslate %}'}) + output = self.engine.render_to_string( + "legacyi18n26", {"myextra_field": "test", "number": 1} + ) + self.assertEqual(output, "singular test") + + @setup( + { + "i18n27": "{% load i18n %}{% blocktranslate count counter=number %}" + "{{ counter }} result{% plural %}{{ counter }} results" + "{% endblocktranslate %}" + } + ) def test_i18n27(self): """translation of singular form in Russian (#14126)""" - with translation.override('ru'): - output = self.engine.render_to_string('i18n27', {'number': 1}) - self.assertEqual(output, '1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442') + with translation.override("ru"): + output = self.engine.render_to_string("i18n27", {"number": 1}) + self.assertEqual( + output, "1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442" + ) - @setup({'legacyi18n27': '{% load i18n %}' - '{% blocktranslate count number as counter %}{{ counter }} result' - '{% plural %}{{ counter }} results{% endblocktranslate %}'}) + @setup( + { + "legacyi18n27": "{% load i18n %}" + "{% blocktranslate count number as counter %}{{ counter }} result" + "{% plural %}{{ counter }} results{% endblocktranslate %}" + } + ) def test_legacyi18n27(self): - with translation.override('ru'): - output = self.engine.render_to_string('legacyi18n27', {'number': 1}) - self.assertEqual(output, '1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442') + with translation.override("ru"): + output = self.engine.render_to_string("legacyi18n27", {"number": 1}) + self.assertEqual( + output, "1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442" + ) - @setup({'i18n28': '{% load i18n %}' - '{% blocktranslate with a=anton b=berta %}{{ a }} + {{ b }}{% endblocktranslate %}'}) + @setup( + { + "i18n28": "{% load i18n %}" + "{% blocktranslate with a=anton b=berta %}{{ a }} + {{ b }}{% endblocktranslate %}" + } + ) def test_i18n28(self): """simple translation of multiple variables""" - output = self.engine.render_to_string('i18n28', {'anton': 'α', 'berta': 'β'}) - self.assertEqual(output, 'α + β') - - @setup({'legacyi18n28': '{% load i18n %}' - '{% blocktranslate with anton as a and berta as b %}' - '{{ a }} + {{ b }}{% endblocktranslate %}'}) + output = self.engine.render_to_string("i18n28", {"anton": "α", "berta": "β"}) + self.assertEqual(output, "α + β") + + @setup( + { + "legacyi18n28": "{% load i18n %}" + "{% blocktranslate with anton as a and berta as b %}" + "{{ a }} + {{ b }}{% endblocktranslate %}" + } + ) def test_legacyi18n28(self): - output = self.engine.render_to_string('legacyi18n28', {'anton': 'α', 'berta': 'β'}) - self.assertEqual(output, 'α + β') + output = self.engine.render_to_string( + "legacyi18n28", {"anton": "α", "berta": "β"} + ) + self.assertEqual(output, "α + β") # blocktranslate handling of variables which are not in the context. # this should work as if blocktranslate was not there (#19915) - @setup({'i18n34': '{% load i18n %}{% blocktranslate %}{{ missing }}{% endblocktranslate %}'}) + @setup( + { + "i18n34": "{% load i18n %}{% blocktranslate %}{{ missing }}{% endblocktranslate %}" + } + ) def test_i18n34(self): - output = self.engine.render_to_string('i18n34') + output = self.engine.render_to_string("i18n34") if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') + self.assertEqual(output, "") - @setup({'i18n34_2': '{% load i18n %}{% blocktranslate with a=\'α\' %}{{ missing }}{% endblocktranslate %}'}) + @setup( + { + "i18n34_2": "{% load i18n %}{% blocktranslate with a='α' %}{{ missing }}{% endblocktranslate %}" + } + ) def test_i18n34_2(self): - output = self.engine.render_to_string('i18n34_2') + output = self.engine.render_to_string("i18n34_2") if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') + self.assertEqual(output, "") - @setup({'i18n34_3': '{% load i18n %}{% blocktranslate with a=anton %}{{ missing }}{% endblocktranslate %}'}) + @setup( + { + "i18n34_3": "{% load i18n %}{% blocktranslate with a=anton %}{{ missing }}{% endblocktranslate %}" + } + ) def test_i18n34_3(self): - output = self.engine.render_to_string( - 'i18n34_3', {'anton': '\xce\xb1'}) + output = self.engine.render_to_string("i18n34_3", {"anton": "\xce\xb1"}) if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') - - @setup({'i18n37': '{% load i18n %}' - '{% translate "Page not found" as page_not_found %}' - '{% blocktranslate %}Error: {{ page_not_found }}{% endblocktranslate %}'}) + self.assertEqual(output, "") + + @setup( + { + "i18n37": "{% load i18n %}" + '{% translate "Page not found" as page_not_found %}' + "{% blocktranslate %}Error: {{ page_not_found }}{% endblocktranslate %}" + } + ) def test_i18n37(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n37') - self.assertEqual(output, 'Error: Seite nicht gefunden') + with translation.override("de"): + output = self.engine.render_to_string("i18n37") + self.assertEqual(output, "Error: Seite nicht gefunden") # blocktranslate tag with asvar - @setup({'i18n39': '{% load i18n %}' - '{% blocktranslate asvar page_not_found %}Page not found{% endblocktranslate %}' - '>{{ page_not_found }}<'}) + @setup( + { + "i18n39": "{% load i18n %}" + "{% blocktranslate asvar page_not_found %}Page not found{% endblocktranslate %}" + ">{{ page_not_found }}<" + } + ) def test_i18n39(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n39') - self.assertEqual(output, '>Seite nicht gefunden<') - - @setup({'i18n40': '{% load i18n %}' - '{% translate "Page not found" as pg_404 %}' - '{% blocktranslate with page_not_found=pg_404 asvar output %}' - 'Error: {{ page_not_found }}' - '{% endblocktranslate %}'}) + with translation.override("de"): + output = self.engine.render_to_string("i18n39") + self.assertEqual(output, ">Seite nicht gefunden<") + + @setup( + { + "i18n40": "{% load i18n %}" + '{% translate "Page not found" as pg_404 %}' + "{% blocktranslate with page_not_found=pg_404 asvar output %}" + "Error: {{ page_not_found }}" + "{% endblocktranslate %}" + } + ) def test_i18n40(self): - output = self.engine.render_to_string('i18n40') - self.assertEqual(output, '') - - @setup({'i18n41': '{% load i18n %}' - '{% translate "Page not found" as pg_404 %}' - '{% blocktranslate with page_not_found=pg_404 asvar output %}' - 'Error: {{ page_not_found }}' - '{% endblocktranslate %}' - '>{{ output }}<'}) + output = self.engine.render_to_string("i18n40") + self.assertEqual(output, "") + + @setup( + { + "i18n41": "{% load i18n %}" + '{% translate "Page not found" as pg_404 %}' + "{% blocktranslate with page_not_found=pg_404 asvar output %}" + "Error: {{ page_not_found }}" + "{% endblocktranslate %}" + ">{{ output }}<" + } + ) def test_i18n41(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n41') - self.assertEqual(output, '>Error: Seite nicht gefunden<') - - @setup({'template': '{% load i18n %}{% blocktranslate asvar %}Yes{% endblocktranslate %}'}) + with translation.override("de"): + output = self.engine.render_to_string("i18n41") + self.assertEqual(output, ">Error: Seite nicht gefunden<") + + @setup( + { + "template": "{% load i18n %}{% blocktranslate asvar %}Yes{% endblocktranslate %}" + } + ) def test_blocktrans_syntax_error_missing_assignment(self, tag_name): - msg = "No argument provided to the '{}' tag for the asvar option.".format(tag_name) + msg = "No argument provided to the '{}' tag for the asvar option.".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% blocktranslate %}%s{% endblocktranslate %}'}) + @setup({"template": "{% load i18n %}{% blocktranslate %}%s{% endblocktranslate %}"}) def test_blocktrans_tag_using_a_string_that_looks_like_str_fmt(self): - output = self.engine.render_to_string('template') - self.assertEqual(output, '%s') - - @setup({'template': '{% load i18n %}{% blocktranslate %}{% block b %} {% endblock %}{% endblocktranslate %}'}) + output = self.engine.render_to_string("template") + self.assertEqual(output, "%s") + + @setup( + { + "template": "{% load i18n %}{% blocktranslate %}{% block b %} {% endblock %}{% endblocktranslate %}" + } + ) def test_with_block(self, tag_name): - msg = "'{}' doesn't allow other block tags (seen 'block b') inside it".format(tag_name) + msg = "'{}' doesn't allow other block tags (seen 'block b') inside it".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') - - @setup({'template': ( - '{% load i18n %}' - '{% blocktranslate %}{% for b in [1, 2, 3] %} {% endfor %}' - '{% endblocktranslate %}' - )}) + self.engine.render_to_string("template") + + @setup( + { + "template": ( + "{% load i18n %}" + "{% blocktranslate %}{% for b in [1, 2, 3] %} {% endfor %}" + "{% endblocktranslate %}" + ) + } + ) def test_with_for(self, tag_name): - msg = "'{}' doesn't allow other block tags (seen 'for b in [1, 2, 3]') inside it".format(tag_name) + msg = "'{}' doesn't allow other block tags (seen 'for b in [1, 2, 3]') inside it".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% blocktranslate with foo=bar with %}{{ foo }}{% endblocktranslate %}'}) + @setup( + { + "template": "{% load i18n %}{% blocktranslate with foo=bar with %}{{ foo }}{% endblocktranslate %}" + } + ) def test_variable_twice(self): - with self.assertRaisesMessage(TemplateSyntaxError, "The 'with' option was specified more than once"): - self.engine.render_to_string('template', {'foo': 'bar'}) - - @setup({'template': '{% load i18n %}{% blocktranslate with %}{% endblocktranslate %}'}) + with self.assertRaisesMessage( + TemplateSyntaxError, "The 'with' option was specified more than once" + ): + self.engine.render_to_string("template", {"foo": "bar"}) + + @setup( + {"template": "{% load i18n %}{% blocktranslate with %}{% endblocktranslate %}"} + ) def test_no_args_with(self, tag_name): - msg = '"with" in \'{}\' tag needs at least one keyword argument.'.format(tag_name) + msg = "\"with\" in '{}' tag needs at least one keyword argument.".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% blocktranslate count a %}{% endblocktranslate %}'}) + @setup( + { + "template": "{% load i18n %}{% blocktranslate count a %}{% endblocktranslate %}" + } + ) def test_count(self, tag_name): - msg = '"count" in \'{}\' tag expected exactly one keyword argument.'.format(tag_name) + msg = "\"count\" in '{}' tag expected exactly one keyword argument.".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template', {'a': [1, 2, 3]}) + self.engine.render_to_string("template", {"a": [1, 2, 3]}) - @setup({'template': ( - '{% load i18n %}{% blocktranslate count counter=num %}{{ counter }}' - '{% plural %}{{ counter }}{% endblocktranslate %}' - )}) + @setup( + { + "template": ( + "{% load i18n %}{% blocktranslate count counter=num %}{{ counter }}" + "{% plural %}{{ counter }}{% endblocktranslate %}" + ) + } + ) def test_count_not_number(self, tag_name): msg = "'counter' argument to '{}' tag must be a number.".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template', {'num': '1'}) - - @setup({'template': ( - '{% load i18n %}{% blocktranslate count count=var|length %}' - 'There is {{ count }} object. {% block a %} {% endblock %}' - '{% endblocktranslate %}' - )}) + self.engine.render_to_string("template", {"num": "1"}) + + @setup( + { + "template": ( + "{% load i18n %}{% blocktranslate count count=var|length %}" + "There is {{ count }} object. {% block a %} {% endblock %}" + "{% endblocktranslate %}" + ) + } + ) def test_plural_bad_syntax(self, tag_name): msg = "'{}' doesn't allow other block tags inside it".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template', {'var': [1, 2, 3]}) + self.engine.render_to_string("template", {"var": [1, 2, 3]}) class TranslationBlockTranslateTagTests(SimpleTestCase): - tag_name = 'blocktranslate' + tag_name = "blocktranslate" def get_template(self, template_string): return Template( template_string.replace( - '{{% blocktranslate ', - '{{% {}'.format(self.tag_name) + "{{% blocktranslate ", "{{% {}".format(self.tag_name) ).replace( - '{{% endblocktranslate %}}', - '{{% end{} %}}'.format(self.tag_name) + "{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name) ) ) @@ -339,108 +498,112 @@ class TranslationBlockTranslateTagTests(SimpleTestCase): """{% blocktranslate %} takes message contexts into account (#14806).""" trans_real._active = Local() trans_real._translations = {} - with translation.override('de'): + with translation.override("de"): # Nonexistent context t = self.get_template( '{% load i18n %}{% blocktranslate context "nonexistent" %}May' - '{% endblocktranslate %}' + "{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, 'May') + self.assertEqual(rendered, "May") # Existing context... using a literal - t = self.get_template('{% load i18n %}{% blocktranslate context "month name" %}May{% endblocktranslate %}') + t = self.get_template( + '{% load i18n %}{% blocktranslate context "month name" %}May{% endblocktranslate %}' + ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Mai') - t = self.get_template('{% load i18n %}{% blocktranslate context "verb" %}May{% endblocktranslate %}') + self.assertEqual(rendered, "Mai") + t = self.get_template( + '{% load i18n %}{% blocktranslate context "verb" %}May{% endblocktranslate %}' + ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Kann') + self.assertEqual(rendered, "Kann") # Using a variable t = self.get_template( - '{% load i18n %}{% blocktranslate context message_context %}' - 'May{% endblocktranslate %}' + "{% load i18n %}{% blocktranslate context message_context %}" + "May{% endblocktranslate %}" ) - rendered = t.render(Context({'message_context': 'month name'})) - self.assertEqual(rendered, 'Mai') + rendered = t.render(Context({"message_context": "month name"})) + self.assertEqual(rendered, "Mai") t = self.get_template( - '{% load i18n %}{% blocktranslate context message_context %}' - 'May{% endblocktranslate %}' + "{% load i18n %}{% blocktranslate context message_context %}" + "May{% endblocktranslate %}" ) - rendered = t.render(Context({'message_context': 'verb'})) - self.assertEqual(rendered, 'Kann') + rendered = t.render(Context({"message_context": "verb"})) + self.assertEqual(rendered, "Kann") # Using a filter t = self.get_template( - '{% load i18n %}{% blocktranslate context message_context|lower %}May{% endblocktranslate %}' + "{% load i18n %}{% blocktranslate context message_context|lower %}May{% endblocktranslate %}" ) - rendered = t.render(Context({'message_context': 'MONTH NAME'})) - self.assertEqual(rendered, 'Mai') + rendered = t.render(Context({"message_context": "MONTH NAME"})) + self.assertEqual(rendered, "Mai") t = self.get_template( - '{% load i18n %}{% blocktranslate context message_context|lower %}May{% endblocktranslate %}' + "{% load i18n %}{% blocktranslate context message_context|lower %}May{% endblocktranslate %}" ) - rendered = t.render(Context({'message_context': 'VERB'})) - self.assertEqual(rendered, 'Kann') + rendered = t.render(Context({"message_context": "VERB"})) + self.assertEqual(rendered, "Kann") # Using 'count' t = self.get_template( '{% load i18n %}{% blocktranslate count number=1 context "super search" %}' - '{{ number }} super result{% plural %}{{ number }} super results{% endblocktranslate %}' + "{{ number }} super result{% plural %}{{ number }} super results{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, '1 Super-Ergebnis') + self.assertEqual(rendered, "1 Super-Ergebnis") t = self.get_template( '{% load i18n %}{% blocktranslate count number=2 context "super search" %}{{ number }}' - ' super result{% plural %}{{ number }} super results{% endblocktranslate %}' + " super result{% plural %}{{ number }} super results{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, '2 Super-Ergebnisse') + self.assertEqual(rendered, "2 Super-Ergebnisse") t = self.get_template( '{% load i18n %}{% blocktranslate context "other super search" count number=1 %}' - '{{ number }} super result{% plural %}{{ number }} super results{% endblocktranslate %}' + "{{ number }} super result{% plural %}{{ number }} super results{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, '1 anderen Super-Ergebnis') + self.assertEqual(rendered, "1 anderen Super-Ergebnis") t = self.get_template( '{% load i18n %}{% blocktranslate context "other super search" count number=2 %}' - '{{ number }} super result{% plural %}{{ number }} super results{% endblocktranslate %}' + "{{ number }} super result{% plural %}{{ number }} super results{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, '2 andere Super-Ergebnisse') + self.assertEqual(rendered, "2 andere Super-Ergebnisse") # Using 'with' t = self.get_template( '{% load i18n %}{% blocktranslate with num_comments=5 context "comment count" %}' - 'There are {{ num_comments }} comments{% endblocktranslate %}' + "There are {{ num_comments }} comments{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Es gibt 5 Kommentare') + self.assertEqual(rendered, "Es gibt 5 Kommentare") t = self.get_template( '{% load i18n %}{% blocktranslate with num_comments=5 context "other comment count" %}' - 'There are {{ num_comments }} comments{% endblocktranslate %}' + "There are {{ num_comments }} comments{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Andere: Es gibt 5 Kommentare') + self.assertEqual(rendered, "Andere: Es gibt 5 Kommentare") # Using trimmed t = self.get_template( - '{% load i18n %}{% blocktranslate trimmed %}\n\nThere\n\t are 5 ' - '\n\n comments\n{% endblocktranslate %}' + "{% load i18n %}{% blocktranslate trimmed %}\n\nThere\n\t are 5 " + "\n\n comments\n{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, 'There are 5 comments') + self.assertEqual(rendered, "There are 5 comments") t = self.get_template( '{% load i18n %}{% blocktranslate with num_comments=5 context "comment count" trimmed %}\n\n' - 'There are \t\n \t {{ num_comments }} comments\n\n{% endblocktranslate %}' + "There are \t\n \t {{ num_comments }} comments\n\n{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Es gibt 5 Kommentare') + self.assertEqual(rendered, "Es gibt 5 Kommentare") t = self.get_template( '{% load i18n %}{% blocktranslate context "other super search" count number=2 trimmed %}\n' - '{{ number }} super \n result{% plural %}{{ number }} super results{% endblocktranslate %}' + "{{ number }} super \n result{% plural %}{{ number }} super results{% endblocktranslate %}" ) rendered = t.render(Context()) - self.assertEqual(rendered, '2 andere Super-Ergebnisse') + self.assertEqual(rendered, "2 andere Super-Ergebnisse") # Misuses msg = "Unknown argument for 'blocktranslate' tag: %r." @@ -448,55 +611,61 @@ class TranslationBlockTranslateTagTests(SimpleTestCase): self.get_template( '{% load i18n %}{% blocktranslate context with month="May" %}{{ month }}{% endblocktranslate %}' ) - msg = '"context" in %r tag expected exactly one argument.' % 'blocktranslate' + msg = ( + '"context" in %r tag expected exactly one argument.' % "blocktranslate" + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.get_template('{% load i18n %}{% blocktranslate context %}{% endblocktranslate %}') + self.get_template( + "{% load i18n %}{% blocktranslate context %}{% endblocktranslate %}" + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.get_template( - '{% load i18n %}{% blocktranslate count number=2 context %}' - '{{ number }} super result{% plural %}{{ number }}' - ' super results{% endblocktranslate %}' + "{% load i18n %}{% blocktranslate count number=2 context %}" + "{{ number }} super result{% plural %}{{ number }}" + " super results{% endblocktranslate %}" ) - @override_settings(LOCALE_PATHS=[os.path.join(here, 'other', 'locale')]) + @override_settings(LOCALE_PATHS=[os.path.join(here, "other", "locale")]) def test_bad_placeholder_1(self): """ Error in translation file should not crash template rendering (#16516). (%(person)s is translated as %(personne)s in fr.po). """ - with translation.override('fr'): - t = Template('{% load i18n %}{% blocktranslate %}My name is {{ person }}.{% endblocktranslate %}') - rendered = t.render(Context({'person': 'James'})) - self.assertEqual(rendered, 'My name is James.') + with translation.override("fr"): + t = Template( + "{% load i18n %}{% blocktranslate %}My name is {{ person }}.{% endblocktranslate %}" + ) + rendered = t.render(Context({"person": "James"})) + self.assertEqual(rendered, "My name is James.") - @override_settings(LOCALE_PATHS=[os.path.join(here, 'other', 'locale')]) + @override_settings(LOCALE_PATHS=[os.path.join(here, "other", "locale")]) def test_bad_placeholder_2(self): """ Error in translation file should not crash template rendering (#18393). (%(person) misses a 's' in fr.po, causing the string formatting to fail) . """ - with translation.override('fr'): - t = Template('{% load i18n %}{% blocktranslate %}My other name is {{ person }}.{% endblocktranslate %}') - rendered = t.render(Context({'person': 'James'})) - self.assertEqual(rendered, 'My other name is James.') + with translation.override("fr"): + t = Template( + "{% load i18n %}{% blocktranslate %}My other name is {{ person }}.{% endblocktranslate %}" + ) + rendered = t.render(Context({"person": "James"})) + self.assertEqual(rendered, "My other name is James.") class TranslationBlockTransnTagTests(TranslationBlockTranslateTagTests): - tag_name = 'blocktrans' + tag_name = "blocktrans" class MultipleLocaleActivationBlockTranslateTests(MultipleLocaleActivationTestCase): - tag_name = 'blocktranslate' + tag_name = "blocktranslate" def get_template(self, template_string): return Template( template_string.replace( - '{{% blocktranslate ', - '{{% {}'.format(self.tag_name) + "{{% blocktranslate ", "{{% {}".format(self.tag_name) ).replace( - '{{% endblocktranslate %}}', - '{{% end{} %}}'.format(self.tag_name) + "{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name) ) ) @@ -505,46 +674,54 @@ class MultipleLocaleActivationBlockTranslateTests(MultipleLocaleActivationTestCa Simple baseline behavior with one locale for all the supported i18n constructs. """ - with translation.override('fr'): + with translation.override("fr"): self.assertEqual( - self.get_template("{% load i18n %}{% blocktranslate %}Yes{% endblocktranslate %}").render(Context({})), - 'Oui' + self.get_template( + "{% load i18n %}{% blocktranslate %}Yes{% endblocktranslate %}" + ).render(Context({})), + "Oui", ) def test_multiple_locale_btrans(self): - with translation.override('de'): - t = self.get_template("{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}") - with translation.override(self._old_language), translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("de"): + t = self.get_template( + "{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}" + ) + with translation.override(self._old_language), translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_deactivate_btrans(self): - with translation.override('de', deactivate=True): - t = self.get_template("{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("de", deactivate=True): + t = self.get_template( + "{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}" + ) + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_direct_switch_btrans(self): - with translation.override('de'): - t = self.get_template("{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("de"): + t = self.get_template( + "{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}" + ) + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") -class MultipleLocaleActivationBlockTransTests(MultipleLocaleActivationBlockTranslateTests): - tag_name = 'blocktrans' +class MultipleLocaleActivationBlockTransTests( + MultipleLocaleActivationBlockTranslateTests +): + tag_name = "blocktrans" class MiscTests(SimpleTestCase): - tag_name = 'blocktranslate' + tag_name = "blocktranslate" def get_template(self, template_string): return Template( template_string.replace( - '{{% blocktranslate ', - '{{% {}'.format(self.tag_name) + "{{% blocktranslate ", "{{% {}".format(self.tag_name) ).replace( - '{{% endblocktranslate %}}', - '{{% end{} %}}'.format(self.tag_name) + "{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name) ) ) @@ -558,10 +735,18 @@ class MiscTests(SimpleTestCase): "{{ percent }}% represents {{ num }} object{% plural %}" "{{ percent }}% represents {{ num }} objects{% endblocktranslate %}" ) - with translation.override('de'): - self.assertEqual(t_sing.render(Context({'percent': 42})), 'Das Ergebnis war 42%') - self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 1})), '42% stellt 1 Objekt dar') - self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 4})), '42% stellt 4 Objekte dar') + with translation.override("de"): + self.assertEqual( + t_sing.render(Context({"percent": 42})), "Das Ergebnis war 42%" + ) + self.assertEqual( + t_plur.render(Context({"percent": 42, "num": 1})), + "42% stellt 1 Objekt dar", + ) + self.assertEqual( + t_plur.render(Context({"percent": 42, "num": 4})), + "42% stellt 4 Objekte dar", + ) @override_settings(LOCALE_PATHS=extended_locale_paths) def test_percent_formatting_in_blocktranslate(self): @@ -577,26 +762,38 @@ class MiscTests(SimpleTestCase): "%(percent)s% represents {{ num }} object{% plural %}" "%(percent)s% represents {{ num }} objects{% endblocktranslate %}" ) - with translation.override('de'): + with translation.override("de"): # Strings won't get translated as they don't match after escaping % - self.assertEqual(t_sing.render(Context({'num_comments': 42})), 'There are %(num_comments)s comments') - self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 1})), '%(percent)s% represents 1 object') - self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 4})), '%(percent)s% represents 4 objects') + self.assertEqual( + t_sing.render(Context({"num_comments": 42})), + "There are %(num_comments)s comments", + ) + self.assertEqual( + t_plur.render(Context({"percent": 42, "num": 1})), + "%(percent)s% represents 1 object", + ) + self.assertEqual( + t_plur.render(Context({"percent": 42, "num": 4})), + "%(percent)s% represents 4 objects", + ) class MiscBlockTranslationTests(MiscTests): - tag_name = 'blocktrans' + tag_name = "blocktrans" class BlockTranslateNodeTests(SimpleTestCase): def test_repr(self): - block_translate_node = BlockTranslateNode(extra_context={}, singular=[ - Token(TokenType.TEXT, 'content'), - Token(TokenType.VAR, 'variable'), - ]) + block_translate_node = BlockTranslateNode( + extra_context={}, + singular=[ + Token(TokenType.TEXT, "content"), + Token(TokenType.VAR, "variable"), + ], + ) self.assertEqual( repr(block_translate_node), - ', ] ' - 'plural=None>', + "plural=None>", ) diff --git a/tests/template_tests/syntax_tests/i18n/test_filters.py b/tests/template_tests/syntax_tests/i18n/test_filters.py index cf8e4573c3..c78b8aca5f 100644 --- a/tests/template_tests/syntax_tests/i18n/test_filters.py +++ b/tests/template_tests/syntax_tests/i18n/test_filters.py @@ -6,42 +6,56 @@ from ...utils import setup class I18nFiltersTests(SimpleTestCase): libraries = { - 'custom': 'template_tests.templatetags.custom', - 'i18n': 'django.templatetags.i18n', + "custom": "template_tests.templatetags.custom", + "i18n": "django.templatetags.i18n", } - @setup({'i18n32': '{% load i18n %}{{ "hu"|language_name }} ' - '{{ "hu"|language_name_local }} {{ "hu"|language_bidi }} ' - '{{ "hu"|language_name_translated }}'}) + @setup( + { + "i18n32": '{% load i18n %}{{ "hu"|language_name }} ' + '{{ "hu"|language_name_local }} {{ "hu"|language_bidi }} ' + '{{ "hu"|language_name_translated }}' + } + ) def test_i18n32(self): - output = self.engine.render_to_string('i18n32') - self.assertEqual(output, 'Hungarian Magyar False Hungarian') - - with translation.override('cs'): - output = self.engine.render_to_string('i18n32') - self.assertEqual(output, 'Hungarian Magyar False maďarsky') - - @setup({'i18n33': '{% load i18n %}' - '{{ langcode|language_name }} {{ langcode|language_name_local }} ' - '{{ langcode|language_bidi }} {{ langcode|language_name_translated }}'}) + output = self.engine.render_to_string("i18n32") + self.assertEqual(output, "Hungarian Magyar False Hungarian") + + with translation.override("cs"): + output = self.engine.render_to_string("i18n32") + self.assertEqual(output, "Hungarian Magyar False maďarsky") + + @setup( + { + "i18n33": "{% load i18n %}" + "{{ langcode|language_name }} {{ langcode|language_name_local }} " + "{{ langcode|language_bidi }} {{ langcode|language_name_translated }}" + } + ) def test_i18n33(self): - output = self.engine.render_to_string('i18n33', {'langcode': 'nl'}) - self.assertEqual(output, 'Dutch Nederlands False Dutch') - - with translation.override('cs'): - output = self.engine.render_to_string('i18n33', {'langcode': 'nl'}) - self.assertEqual(output, 'Dutch Nederlands False nizozemsky') - - @setup({'i18n38_2': '{% load i18n custom %}' - '{% get_language_info_list for langcodes|noop:"x y" as langs %}' - '{% for l in langs %}{{ l.code }}: {{ l.name }}/' - '{{ l.name_local }}/{{ l.name_translated }} ' - 'bidi={{ l.bidi }}; {% endfor %}'}) + output = self.engine.render_to_string("i18n33", {"langcode": "nl"}) + self.assertEqual(output, "Dutch Nederlands False Dutch") + + with translation.override("cs"): + output = self.engine.render_to_string("i18n33", {"langcode": "nl"}) + self.assertEqual(output, "Dutch Nederlands False nizozemsky") + + @setup( + { + "i18n38_2": "{% load i18n custom %}" + '{% get_language_info_list for langcodes|noop:"x y" as langs %}' + "{% for l in langs %}{{ l.code }}: {{ l.name }}/" + "{{ l.name_local }}/{{ l.name_translated }} " + "bidi={{ l.bidi }}; {% endfor %}" + } + ) def test_i18n38_2(self): - with translation.override('cs'): - output = self.engine.render_to_string('i18n38_2', {'langcodes': ['it', 'fr']}) + with translation.override("cs"): + output = self.engine.render_to_string( + "i18n38_2", {"langcodes": ["it", "fr"]} + ) self.assertEqual( output, - 'it: Italian/italiano/italsky bidi=False; ' - 'fr: French/français/francouzsky bidi=False; ' + "it: Italian/italiano/italsky bidi=False; " + "fr: French/français/francouzsky bidi=False; ", ) diff --git a/tests/template_tests/syntax_tests/i18n/test_get_available_languages.py b/tests/template_tests/syntax_tests/i18n/test_get_available_languages.py index 5ac97a4f79..dce5a4de66 100644 --- a/tests/template_tests/syntax_tests/i18n/test_get_available_languages.py +++ b/tests/template_tests/syntax_tests/i18n/test_get_available_languages.py @@ -5,17 +5,21 @@ from ...utils import setup class GetAvailableLanguagesTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} + libraries = {"i18n": "django.templatetags.i18n"} - @setup({'i18n12': '{% load i18n %}' - '{% get_available_languages as langs %}{% for lang in langs %}' - '{% if lang.0 == "de" %}{{ lang.0 }}{% endif %}{% endfor %}'}) + @setup( + { + "i18n12": "{% load i18n %}" + "{% get_available_languages as langs %}{% for lang in langs %}" + '{% if lang.0 == "de" %}{{ lang.0 }}{% endif %}{% endfor %}' + } + ) def test_i18n12(self): - output = self.engine.render_to_string('i18n12') - self.assertEqual(output, 'de') + output = self.engine.render_to_string("i18n12") + self.assertEqual(output, "de") - @setup({'syntax_i18n': '{% load i18n %}{% get_available_languages a langs %}'}) + @setup({"syntax_i18n": "{% load i18n %}{% get_available_languages a langs %}"}) def test_no_as_var(self): msg = "'get_available_languages' requires 'as variable' (got ['get_available_languages', 'a', 'langs'])" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('syntax_i18n') + self.engine.render_to_string("syntax_i18n") diff --git a/tests/template_tests/syntax_tests/i18n/test_get_current_language.py b/tests/template_tests/syntax_tests/i18n/test_get_current_language.py index f168b74f8e..8ed7f742c2 100644 --- a/tests/template_tests/syntax_tests/i18n/test_get_current_language.py +++ b/tests/template_tests/syntax_tests/i18n/test_get_current_language.py @@ -5,10 +5,10 @@ from django.test import SimpleTestCase class I18nGetCurrentLanguageTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} + libraries = {"i18n": "django.templatetags.i18n"} - @setup({'template': '{% load i18n %} {% get_current_language %}'}) + @setup({"template": "{% load i18n %} {% get_current_language %}"}) def test_no_as_var(self): msg = "'get_current_language' requires 'as variable' (got ['get_current_language'])" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") diff --git a/tests/template_tests/syntax_tests/i18n/test_get_current_language_bidi.py b/tests/template_tests/syntax_tests/i18n/test_get_current_language_bidi.py index 0ac408f78f..3f307471ae 100644 --- a/tests/template_tests/syntax_tests/i18n/test_get_current_language_bidi.py +++ b/tests/template_tests/syntax_tests/i18n/test_get_current_language_bidi.py @@ -5,10 +5,10 @@ from django.test import SimpleTestCase class I18nGetCurrentLanguageBidiTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} + libraries = {"i18n": "django.templatetags.i18n"} - @setup({'template': '{% load i18n %} {% get_current_language_bidi %}'}) + @setup({"template": "{% load i18n %} {% get_current_language_bidi %}"}) def test_no_as_var(self): msg = "'get_current_language_bidi' requires 'as variable' (got ['get_current_language_bidi'])" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") diff --git a/tests/template_tests/syntax_tests/i18n/test_get_language_info.py b/tests/template_tests/syntax_tests/i18n/test_get_language_info.py index 4ae8186af5..cf05df13db 100644 --- a/tests/template_tests/syntax_tests/i18n/test_get_language_info.py +++ b/tests/template_tests/syntax_tests/i18n/test_get_language_info.py @@ -7,37 +7,49 @@ from ...utils import setup class I18nGetLanguageInfoTagTests(SimpleTestCase): libraries = { - 'custom': 'template_tests.templatetags.custom', - 'i18n': 'django.templatetags.i18n', + "custom": "template_tests.templatetags.custom", + "i18n": "django.templatetags.i18n", } # retrieving language information - @setup({'i18n28_2': '{% load i18n %}' - '{% get_language_info for "de" as l %}' - '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'}) + @setup( + { + "i18n28_2": "{% load i18n %}" + '{% get_language_info for "de" as l %}' + "{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}" + } + ) def test_i18n28_2(self): - output = self.engine.render_to_string('i18n28_2') - self.assertEqual(output, 'de: German/Deutsch bidi=False') + output = self.engine.render_to_string("i18n28_2") + self.assertEqual(output, "de: German/Deutsch bidi=False") - @setup({'i18n29': '{% load i18n %}' - '{% get_language_info for LANGUAGE_CODE as l %}' - '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'}) + @setup( + { + "i18n29": "{% load i18n %}" + "{% get_language_info for LANGUAGE_CODE as l %}" + "{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}" + } + ) def test_i18n29(self): - output = self.engine.render_to_string('i18n29', {'LANGUAGE_CODE': 'fi'}) - self.assertEqual(output, 'fi: Finnish/suomi bidi=False') + output = self.engine.render_to_string("i18n29", {"LANGUAGE_CODE": "fi"}) + self.assertEqual(output, "fi: Finnish/suomi bidi=False") # Test whitespace in filter arguments - @setup({'i18n38': '{% load i18n custom %}' - '{% get_language_info for "de"|noop:"x y" as l %}' - '{{ l.code }}: {{ l.name }}/{{ l.name_local }}/' - '{{ l.name_translated }} bidi={{ l.bidi }}'}) + @setup( + { + "i18n38": "{% load i18n custom %}" + '{% get_language_info for "de"|noop:"x y" as l %}' + "{{ l.code }}: {{ l.name }}/{{ l.name_local }}/" + "{{ l.name_translated }} bidi={{ l.bidi }}" + } + ) def test_i18n38(self): - with translation.override('cs'): - output = self.engine.render_to_string('i18n38') - self.assertEqual(output, 'de: German/Deutsch/německy bidi=False') + with translation.override("cs"): + output = self.engine.render_to_string("i18n38") + self.assertEqual(output, "de: German/Deutsch/německy bidi=False") - @setup({'template': '{% load i18n %}{% get_language_info %}'}) + @setup({"template": "{% load i18n %}{% get_language_info %}"}) def test_no_for_as(self): msg = "'get_language_info' requires 'for string as variable' (got [])" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") diff --git a/tests/template_tests/syntax_tests/i18n/test_get_language_info_list.py b/tests/template_tests/syntax_tests/i18n/test_get_language_info_list.py index c2c7f8b9cb..93038daa2f 100644 --- a/tests/template_tests/syntax_tests/i18n/test_get_language_info_list.py +++ b/tests/template_tests/syntax_tests/i18n/test_get_language_info_list.py @@ -7,46 +7,64 @@ from ...utils import setup class GetLanguageInfoListTests(SimpleTestCase): libraries = { - 'custom': 'template_tests.templatetags.custom', - 'i18n': 'django.templatetags.i18n', + "custom": "template_tests.templatetags.custom", + "i18n": "django.templatetags.i18n", } - @setup({'i18n30': '{% load i18n %}' - '{% get_language_info_list for langcodes as langs %}' - '{% for l in langs %}{{ l.code }}: {{ l.name }}/' - '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'}) + @setup( + { + "i18n30": "{% load i18n %}" + "{% get_language_info_list for langcodes as langs %}" + "{% for l in langs %}{{ l.code }}: {{ l.name }}/" + "{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}" + } + ) def test_i18n30(self): - output = self.engine.render_to_string('i18n30', {'langcodes': ['it', 'no']}) - self.assertEqual(output, 'it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; ') + output = self.engine.render_to_string("i18n30", {"langcodes": ["it", "no"]}) + self.assertEqual( + output, "it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; " + ) - @setup({'i18n31': '{% load i18n %}' - '{% get_language_info_list for langcodes as langs %}' - '{% for l in langs %}{{ l.code }}: {{ l.name }}/' - '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'}) + @setup( + { + "i18n31": "{% load i18n %}" + "{% get_language_info_list for langcodes as langs %}" + "{% for l in langs %}{{ l.code }}: {{ l.name }}/" + "{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}" + } + ) def test_i18n31(self): - output = self.engine.render_to_string('i18n31', {'langcodes': (('sl', 'Slovenian'), ('fa', 'Persian'))}) + output = self.engine.render_to_string( + "i18n31", {"langcodes": (("sl", "Slovenian"), ("fa", "Persian"))} + ) self.assertEqual( output, - 'sl: Slovenian/Sloven\u0161\u010dina bidi=False; ' - 'fa: Persian/\u0641\u0627\u0631\u0633\u06cc bidi=True; ' + "sl: Slovenian/Sloven\u0161\u010dina bidi=False; " + "fa: Persian/\u0641\u0627\u0631\u0633\u06cc bidi=True; ", ) - @setup({'i18n38_2': '{% load i18n custom %}' - '{% get_language_info_list for langcodes|noop:"x y" as langs %}' - '{% for l in langs %}{{ l.code }}: {{ l.name }}/' - '{{ l.name_local }}/{{ l.name_translated }} ' - 'bidi={{ l.bidi }}; {% endfor %}'}) + @setup( + { + "i18n38_2": "{% load i18n custom %}" + '{% get_language_info_list for langcodes|noop:"x y" as langs %}' + "{% for l in langs %}{{ l.code }}: {{ l.name }}/" + "{{ l.name_local }}/{{ l.name_translated }} " + "bidi={{ l.bidi }}; {% endfor %}" + } + ) def test_i18n38_2(self): - with translation.override('cs'): - output = self.engine.render_to_string('i18n38_2', {'langcodes': ['it', 'fr']}) + with translation.override("cs"): + output = self.engine.render_to_string( + "i18n38_2", {"langcodes": ["it", "fr"]} + ) self.assertEqual( output, - 'it: Italian/italiano/italsky bidi=False; ' - 'fr: French/français/francouzsky bidi=False; ' + "it: Italian/italiano/italsky bidi=False; " + "fr: French/français/francouzsky bidi=False; ", ) - @setup({'i18n_syntax': '{% load i18n %} {% get_language_info_list error %}'}) + @setup({"i18n_syntax": "{% load i18n %} {% get_language_info_list error %}"}) def test_no_for_as(self): msg = "'get_language_info_list' requires 'for sequence as variable' (got ['error'])" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('i18n_syntax') + self.engine.render_to_string("i18n_syntax") diff --git a/tests/template_tests/syntax_tests/i18n/test_language.py b/tests/template_tests/syntax_tests/i18n/test_language.py index 47797befae..57a3a53866 100644 --- a/tests/template_tests/syntax_tests/i18n/test_language.py +++ b/tests/template_tests/syntax_tests/i18n/test_language.py @@ -5,9 +5,11 @@ from django.test import SimpleTestCase class I18nLanguageTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} + libraries = {"i18n": "django.templatetags.i18n"} - @setup({'i18n_language': '{% load i18n %} {% language %} {% endlanguage %}'}) + @setup({"i18n_language": "{% load i18n %} {% language %} {% endlanguage %}"}) def test_no_arg(self): - with self.assertRaisesMessage(TemplateSyntaxError, "'language' takes one argument (language)"): - self.engine.render_to_string('i18n_language') + with self.assertRaisesMessage( + TemplateSyntaxError, "'language' takes one argument (language)" + ): + self.engine.render_to_string("i18n_language") diff --git a/tests/template_tests/syntax_tests/i18n/test_translate.py b/tests/template_tests/syntax_tests/i18n/test_translate.py index 55fdb92f7a..932b47a373 100644 --- a/tests/template_tests/syntax_tests/i18n/test_translate.py +++ b/tests/template_tests/syntax_tests/i18n/test_translate.py @@ -16,14 +16,16 @@ from .base import MultipleLocaleActivationTestCase, extended_locale_paths def setup(templates, *args, **kwargs): translate_setup = base_setup(templates, *args, **kwargs) - trans_setup = base_setup({ - name: template.replace('{% translate ', '{% trans ') - for name, template in templates.items() - }) + trans_setup = base_setup( + { + name: template.replace("{% translate ", "{% trans ") + for name, template in templates.items() + } + ) tags = { - 'trans': trans_setup, - 'translate': translate_setup, + "trans": trans_setup, + "translate": translate_setup, } def decorator(func): @@ -31,143 +33,162 @@ def setup(templates, *args, **kwargs): def inner(self, *args): signature = inspect.signature(func) for tag_name, setup_func in tags.items(): - if 'tag_name' in signature.parameters: + if "tag_name" in signature.parameters: setup_func(partial(func, tag_name=tag_name))(self) else: setup_func(func)(self) + return inner + return decorator class I18nTransTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} + libraries = {"i18n": "django.templatetags.i18n"} - @setup({'i18n01': '{% load i18n %}{% translate \'xxxyyyxxx\' %}'}) + @setup({"i18n01": "{% load i18n %}{% translate 'xxxyyyxxx' %}"}) def test_i18n01(self): """simple translation of a string delimited by '.""" - output = self.engine.render_to_string('i18n01') - self.assertEqual(output, 'xxxyyyxxx') + output = self.engine.render_to_string("i18n01") + self.assertEqual(output, "xxxyyyxxx") - @setup({'i18n02': '{% load i18n %}{% translate "xxxyyyxxx" %}'}) + @setup({"i18n02": '{% load i18n %}{% translate "xxxyyyxxx" %}'}) def test_i18n02(self): """simple translation of a string delimited by ".""" - output = self.engine.render_to_string('i18n02') - self.assertEqual(output, 'xxxyyyxxx') + output = self.engine.render_to_string("i18n02") + self.assertEqual(output, "xxxyyyxxx") - @setup({'i18n06': '{% load i18n %}{% translate "Page not found" %}'}) + @setup({"i18n06": '{% load i18n %}{% translate "Page not found" %}'}) def test_i18n06(self): """simple translation of a string to German""" - with translation.override('de'): - output = self.engine.render_to_string('i18n06') - self.assertEqual(output, 'Seite nicht gefunden') + with translation.override("de"): + output = self.engine.render_to_string("i18n06") + self.assertEqual(output, "Seite nicht gefunden") - @setup({'i18n09': '{% load i18n %}{% translate "Page not found" noop %}'}) + @setup({"i18n09": '{% load i18n %}{% translate "Page not found" noop %}'}) def test_i18n09(self): """simple non-translation (only marking) of a string to German""" - with translation.override('de'): - output = self.engine.render_to_string('i18n09') - self.assertEqual(output, 'Page not found') + with translation.override("de"): + output = self.engine.render_to_string("i18n09") + self.assertEqual(output, "Page not found") - @setup({'i18n20': '{% load i18n %}{% translate andrew %}'}) + @setup({"i18n20": "{% load i18n %}{% translate andrew %}"}) def test_i18n20(self): - output = self.engine.render_to_string('i18n20', {'andrew': 'a & b'}) - self.assertEqual(output, 'a & b') + output = self.engine.render_to_string("i18n20", {"andrew": "a & b"}) + self.assertEqual(output, "a & b") - @setup({'i18n22': '{% load i18n %}{% translate andrew %}'}) + @setup({"i18n22": "{% load i18n %}{% translate andrew %}"}) def test_i18n22(self): - output = self.engine.render_to_string('i18n22', {'andrew': mark_safe('a & b')}) - self.assertEqual(output, 'a & b') - - @setup({'i18n23': '{% load i18n %}{% translate "Page not found"|capfirst|slice:"6:" %}'}) + output = self.engine.render_to_string("i18n22", {"andrew": mark_safe("a & b")}) + self.assertEqual(output, "a & b") + + @setup( + { + "i18n23": '{% load i18n %}{% translate "Page not found"|capfirst|slice:"6:" %}' + } + ) def test_i18n23(self): """Using filters with the {% translate %} tag (#5972).""" - with translation.override('de'): - output = self.engine.render_to_string('i18n23') - self.assertEqual(output, 'nicht gefunden') + with translation.override("de"): + output = self.engine.render_to_string("i18n23") + self.assertEqual(output, "nicht gefunden") - @setup({'i18n24': '{% load i18n %}{% translate \'Page not found\'|upper %}'}) + @setup({"i18n24": "{% load i18n %}{% translate 'Page not found'|upper %}"}) def test_i18n24(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n24') - self.assertEqual(output, 'SEITE NICHT GEFUNDEN') + with translation.override("de"): + output = self.engine.render_to_string("i18n24") + self.assertEqual(output, "SEITE NICHT GEFUNDEN") - @setup({'i18n25': '{% load i18n %}{% translate somevar|upper %}'}) + @setup({"i18n25": "{% load i18n %}{% translate somevar|upper %}"}) def test_i18n25(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n25', {'somevar': 'Page not found'}) - self.assertEqual(output, 'SEITE NICHT GEFUNDEN') + with translation.override("de"): + output = self.engine.render_to_string( + "i18n25", {"somevar": "Page not found"} + ) + self.assertEqual(output, "SEITE NICHT GEFUNDEN") # trans tag with as var - @setup({'i18n35': '{% load i18n %}{% translate "Page not found" as page_not_found %}{{ page_not_found }}'}) + @setup( + { + "i18n35": '{% load i18n %}{% translate "Page not found" as page_not_found %}{{ page_not_found }}' + } + ) def test_i18n35(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n35') - self.assertEqual(output, 'Seite nicht gefunden') - - @setup({'i18n36': '{% load i18n %}' - '{% translate "Page not found" noop as page_not_found %}{{ page_not_found }}'}) + with translation.override("de"): + output = self.engine.render_to_string("i18n35") + self.assertEqual(output, "Seite nicht gefunden") + + @setup( + { + "i18n36": "{% load i18n %}" + '{% translate "Page not found" noop as page_not_found %}{{ page_not_found }}' + } + ) def test_i18n36(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n36') - self.assertEqual(output, 'Page not found') + with translation.override("de"): + output = self.engine.render_to_string("i18n36") + self.assertEqual(output, "Page not found") - @setup({'template': '{% load i18n %}{% translate %}A}'}) + @setup({"template": "{% load i18n %}{% translate %}A}"}) def test_syntax_error_no_arguments(self, tag_name): msg = "'{}' takes at least one argument".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% translate "Yes" badoption %}'}) + @setup({"template": '{% load i18n %}{% translate "Yes" badoption %}'}) def test_syntax_error_bad_option(self, tag_name): msg = "Unknown argument for '{}' tag: 'badoption'".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% translate "Yes" as %}'}) + @setup({"template": '{% load i18n %}{% translate "Yes" as %}'}) def test_syntax_error_missing_assignment(self, tag_name): msg = "No argument provided to the '{}' tag for the as option.".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% translate "Yes" as var context %}'}) + @setup({"template": '{% load i18n %}{% translate "Yes" as var context %}'}) def test_syntax_error_missing_context(self, tag_name): - msg = "No argument provided to the '{}' tag for the context option.".format(tag_name) + msg = "No argument provided to the '{}' tag for the context option.".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% translate "Yes" context as var %}'}) + @setup({"template": '{% load i18n %}{% translate "Yes" context as var %}'}) def test_syntax_error_context_as(self, tag_name): - msg = "Invalid argument 'as' provided to the '{}' tag for the context option".format(tag_name) + msg = "Invalid argument 'as' provided to the '{}' tag for the context option".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% translate "Yes" context noop %}'}) + @setup({"template": '{% load i18n %}{% translate "Yes" context noop %}'}) def test_syntax_error_context_noop(self, tag_name): - msg = "Invalid argument 'noop' provided to the '{}' tag for the context option".format(tag_name) + msg = "Invalid argument 'noop' provided to the '{}' tag for the context option".format( + tag_name + ) with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% translate "Yes" noop noop %}'}) + @setup({"template": '{% load i18n %}{% translate "Yes" noop noop %}'}) def test_syntax_error_duplicate_option(self): msg = "The 'noop' option was specified more than once." with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% load i18n %}{% translate "%s" %}'}) + @setup({"template": '{% load i18n %}{% translate "%s" %}'}) def test_trans_tag_using_a_string_that_looks_like_str_fmt(self): - output = self.engine.render_to_string('template') - self.assertEqual(output, '%s') + output = self.engine.render_to_string("template") + self.assertEqual(output, "%s") class TranslationTransTagTests(SimpleTestCase): - tag_name = 'trans' + tag_name = "trans" def get_template(self, template_string): return Template( - template_string.replace( - '{{% translate ', - '{{% {}'.format(self.tag_name) - ) + template_string.replace("{{% translate ", "{{% {}".format(self.tag_name)) ) @override_settings(LOCALE_PATHS=extended_locale_paths) @@ -175,58 +196,71 @@ class TranslationTransTagTests(SimpleTestCase): """{% translate %} takes message contexts into account (#14806).""" trans_real._active = Local() trans_real._translations = {} - with translation.override('de'): + with translation.override("de"): # Nonexistent context... - t = self.get_template('{% load i18n %}{% translate "May" context "nonexistent" %}') + t = self.get_template( + '{% load i18n %}{% translate "May" context "nonexistent" %}' + ) rendered = t.render(Context()) - self.assertEqual(rendered, 'May') + self.assertEqual(rendered, "May") # Existing context... using a literal - t = self.get_template('{% load i18n %}{% translate "May" context "month name" %}') + t = self.get_template( + '{% load i18n %}{% translate "May" context "month name" %}' + ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Mai') + self.assertEqual(rendered, "Mai") t = self.get_template('{% load i18n %}{% translate "May" context "verb" %}') rendered = t.render(Context()) - self.assertEqual(rendered, 'Kann') + self.assertEqual(rendered, "Kann") # Using a variable - t = self.get_template('{% load i18n %}{% translate "May" context message_context %}') - rendered = t.render(Context({'message_context': 'month name'})) - self.assertEqual(rendered, 'Mai') - t = self.get_template('{% load i18n %}{% translate "May" context message_context %}') - rendered = t.render(Context({'message_context': 'verb'})) - self.assertEqual(rendered, 'Kann') + t = self.get_template( + '{% load i18n %}{% translate "May" context message_context %}' + ) + rendered = t.render(Context({"message_context": "month name"})) + self.assertEqual(rendered, "Mai") + t = self.get_template( + '{% load i18n %}{% translate "May" context message_context %}' + ) + rendered = t.render(Context({"message_context": "verb"})) + self.assertEqual(rendered, "Kann") # Using a filter - t = self.get_template('{% load i18n %}{% translate "May" context message_context|lower %}') - rendered = t.render(Context({'message_context': 'MONTH NAME'})) - self.assertEqual(rendered, 'Mai') - t = self.get_template('{% load i18n %}{% translate "May" context message_context|lower %}') - rendered = t.render(Context({'message_context': 'VERB'})) - self.assertEqual(rendered, 'Kann') + t = self.get_template( + '{% load i18n %}{% translate "May" context message_context|lower %}' + ) + rendered = t.render(Context({"message_context": "MONTH NAME"})) + self.assertEqual(rendered, "Mai") + t = self.get_template( + '{% load i18n %}{% translate "May" context message_context|lower %}' + ) + rendered = t.render(Context({"message_context": "VERB"})) + self.assertEqual(rendered, "Kann") # Using 'as' - t = self.get_template('{% load i18n %}{% translate "May" context "month name" as var %}Value: {{ var }}') + t = self.get_template( + '{% load i18n %}{% translate "May" context "month name" as var %}Value: {{ var }}' + ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Value: Mai') - t = self.get_template('{% load i18n %}{% translate "May" as var context "verb" %}Value: {{ var }}') + self.assertEqual(rendered, "Value: Mai") + t = self.get_template( + '{% load i18n %}{% translate "May" as var context "verb" %}Value: {{ var }}' + ) rendered = t.render(Context()) - self.assertEqual(rendered, 'Value: Kann') + self.assertEqual(rendered, "Value: Kann") class TranslationTranslateTagTests(TranslationTransTagTests): - tag_name = 'translate' + tag_name = "translate" class MultipleLocaleActivationTransTagTests(MultipleLocaleActivationTestCase): - tag_name = 'trans' + tag_name = "trans" def get_template(self, template_string): return Template( - template_string.replace( - '{{% translate ', - '{{% {}'.format(self.tag_name) - ) + template_string.replace("{{% translate ", "{{% {}".format(self.tag_name)) ) def test_single_locale_activation(self): @@ -234,36 +268,38 @@ class MultipleLocaleActivationTransTagTests(MultipleLocaleActivationTestCase): Simple baseline behavior with one locale for all the supported i18n constructs. """ - with translation.override('fr'): + with translation.override("fr"): self.assertEqual( - self.get_template("{% load i18n %}{% translate 'Yes' %}").render(Context({})), - 'Oui' + self.get_template("{% load i18n %}{% translate 'Yes' %}").render( + Context({}) + ), + "Oui", ) def test_multiple_locale_trans(self): - with translation.override('de'): + with translation.override("de"): t = self.get_template("{% load i18n %}{% translate 'No' %}") - with translation.override(self._old_language), translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override(self._old_language), translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_deactivate_trans(self): - with translation.override('de', deactivate=True): + with translation.override("de", deactivate=True): t = self.get_template("{% load i18n %}{% translate 'No' %}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_direct_switch_trans(self): - with translation.override('de'): + with translation.override("de"): t = self.get_template("{% load i18n %}{% translate 'No' %}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") class MultipleLocaleActivationTranslateTagTests(MultipleLocaleActivationTransTagTests): - tag_name = 'translate' + tag_name = "translate" class LocalizeNodeTests(SimpleTestCase): def test_repr(self): node = LocalizeNode(nodelist=[], use_l10n=True) - self.assertEqual(repr(node), '') + self.assertEqual(repr(node), "") diff --git a/tests/template_tests/syntax_tests/i18n/test_underscore_syntax.py b/tests/template_tests/syntax_tests/i18n/test_underscore_syntax.py index 38d929e214..dfb035999c 100644 --- a/tests/template_tests/syntax_tests/i18n/test_underscore_syntax.py +++ b/tests/template_tests/syntax_tests/i18n/test_underscore_syntax.py @@ -7,101 +7,105 @@ from .base import MultipleLocaleActivationTestCase class MultipleLocaleActivationTests(MultipleLocaleActivationTestCase): - def test_single_locale_activation(self): """ Simple baseline behavior with one locale for all the supported i18n constructs. """ - with translation.override('fr'): - self.assertEqual(Template("{{ _('Yes') }}").render(Context({})), 'Oui') + with translation.override("fr"): + self.assertEqual(Template("{{ _('Yes') }}").render(Context({})), "Oui") # Literal marked up with _() in a filter expression def test_multiple_locale_filter(self): - with translation.override('de'): + with translation.override("de"): t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") - with translation.override(self._old_language), translation.override('nl'): - self.assertEqual(t.render(Context({})), 'nee') + with translation.override(self._old_language), translation.override("nl"): + self.assertEqual(t.render(Context({})), "nee") def test_multiple_locale_filter_deactivate(self): - with translation.override('de', deactivate=True): + with translation.override("de", deactivate=True): t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "nee") def test_multiple_locale_filter_direct_switch(self): - with translation.override('de'): + with translation.override("de"): t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "nee") # Literal marked up with _() def test_multiple_locale(self): - with translation.override('de'): + with translation.override("de"): t = Template("{{ _('No') }}") - with translation.override(self._old_language), translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override(self._old_language), translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_deactivate(self): - with translation.override('de', deactivate=True): + with translation.override("de", deactivate=True): t = Template("{{ _('No') }}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_direct_switch(self): - with translation.override('de'): + with translation.override("de"): t = Template("{{ _('No') }}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") # Literal marked up with _(), loading the i18n template tag library def test_multiple_locale_loadi18n(self): - with translation.override('de'): + with translation.override("de"): t = Template("{% load i18n %}{{ _('No') }}") - with translation.override(self._old_language), translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override(self._old_language), translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_loadi18n_deactivate(self): - with translation.override('de', deactivate=True): + with translation.override("de", deactivate=True): t = Template("{% load i18n %}{{ _('No') }}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") def test_multiple_locale_loadi18n_direct_switch(self): - with translation.override('de'): + with translation.override("de"): t = Template("{% load i18n %}{{ _('No') }}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') + with translation.override("nl"): + self.assertEqual(t.render(Context({})), "Nee") class I18nStringLiteralTests(SimpleTestCase): """translation of constant strings""" - libraries = {'i18n': 'django.templatetags.i18n'} - @setup({'i18n13': '{{ _("Password") }}'}) + libraries = {"i18n": "django.templatetags.i18n"} + + @setup({"i18n13": '{{ _("Password") }}'}) def test_i18n13(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n13') - self.assertEqual(output, 'Passwort') + with translation.override("de"): + output = self.engine.render_to_string("i18n13") + self.assertEqual(output, "Passwort") - @setup({'i18n14': '{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}'}) + @setup( + { + "i18n14": '{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}' + } + ) def test_i18n14(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n14') - self.assertEqual(output, 'foo Passwort Passwort') + with translation.override("de"): + output = self.engine.render_to_string("i18n14") + self.assertEqual(output, "foo Passwort Passwort") - @setup({'i18n15': '{{ absent|default:_("Password") }}'}) + @setup({"i18n15": '{{ absent|default:_("Password") }}'}) def test_i18n15(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n15', {'absent': ''}) - self.assertEqual(output, 'Passwort') + with translation.override("de"): + output = self.engine.render_to_string("i18n15", {"absent": ""}) + self.assertEqual(output, "Passwort") - @setup({'i18n16': '{{ _("<") }}'}) + @setup({"i18n16": '{{ _("<") }}'}) def test_i18n16(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n16') - self.assertEqual(output, '<') + with translation.override("de"): + output = self.engine.render_to_string("i18n16") + self.assertEqual(output, "<") diff --git a/tests/template_tests/syntax_tests/test_autoescape.py b/tests/template_tests/syntax_tests/test_autoescape.py index 13968537a6..d3c5cb02da 100644 --- a/tests/template_tests/syntax_tests/test_autoescape.py +++ b/tests/template_tests/syntax_tests/test_autoescape.py @@ -6,72 +6,89 @@ from ..utils import SafeClass, UnsafeClass, setup class AutoescapeTagTests(SimpleTestCase): - - @setup({'autoescape-tag01': '{% autoescape off %}hello{% endautoescape %}'}) + @setup({"autoescape-tag01": "{% autoescape off %}hello{% endautoescape %}"}) def test_autoescape_tag01(self): - output = self.engine.render_to_string('autoescape-tag01') - self.assertEqual(output, 'hello') + output = self.engine.render_to_string("autoescape-tag01") + self.assertEqual(output, "hello") - @setup({'autoescape-tag02': '{% autoescape off %}{{ first }}{% endautoescape %}'}) + @setup({"autoescape-tag02": "{% autoescape off %}{{ first }}{% endautoescape %}"}) def test_autoescape_tag02(self): - output = self.engine.render_to_string('autoescape-tag02', {'first': 'hello'}) - self.assertEqual(output, 'hello') + output = self.engine.render_to_string( + "autoescape-tag02", {"first": "hello"} + ) + self.assertEqual(output, "hello") - @setup({'autoescape-tag03': '{% autoescape on %}{{ first }}{% endautoescape %}'}) + @setup({"autoescape-tag03": "{% autoescape on %}{{ first }}{% endautoescape %}"}) def test_autoescape_tag03(self): - output = self.engine.render_to_string('autoescape-tag03', {'first': 'hello'}) - self.assertEqual(output, '<b>hello</b>') + output = self.engine.render_to_string( + "autoescape-tag03", {"first": "hello"} + ) + self.assertEqual(output, "<b>hello</b>") # Autoescape disabling and enabling nest in a predictable way. - @setup({ - 'autoescape-tag04': - '{% autoescape off %}{{ first }} {% autoescape on %}{{ first }}{% endautoescape %}{% endautoescape %}' - }) + @setup( + { + "autoescape-tag04": "{% autoescape off %}{{ first }} {% autoescape on %}{{ first }}{% endautoescape %}{% endautoescape %}" + } + ) def test_autoescape_tag04(self): - output = self.engine.render_to_string('autoescape-tag04', {'first': ''}) - self.assertEqual(output, ' <a>') + output = self.engine.render_to_string("autoescape-tag04", {"first": ""}) + self.assertEqual(output, " <a>") - @setup({'autoescape-tag05': '{% autoescape on %}{{ first }}{% endautoescape %}'}) + @setup({"autoescape-tag05": "{% autoescape on %}{{ first }}{% endautoescape %}"}) def test_autoescape_tag05(self): - output = self.engine.render_to_string('autoescape-tag05', {'first': 'first'}) - self.assertEqual(output, '<b>first</b>') + output = self.engine.render_to_string( + "autoescape-tag05", {"first": "first"} + ) + self.assertEqual(output, "<b>first</b>") # Strings (ASCII or Unicode) already marked as "safe" are not # auto-escaped - @setup({'autoescape-tag06': '{{ first }}'}) + @setup({"autoescape-tag06": "{{ first }}"}) def test_autoescape_tag06(self): - output = self.engine.render_to_string('autoescape-tag06', {'first': mark_safe('first')}) - self.assertEqual(output, 'first') + output = self.engine.render_to_string( + "autoescape-tag06", {"first": mark_safe("first")} + ) + self.assertEqual(output, "first") - @setup({'autoescape-tag07': '{% autoescape on %}{{ first }}{% endautoescape %}'}) + @setup({"autoescape-tag07": "{% autoescape on %}{{ first }}{% endautoescape %}"}) def test_autoescape_tag07(self): - output = self.engine.render_to_string('autoescape-tag07', {'first': mark_safe('Apple')}) - self.assertEqual(output, 'Apple') - - @setup({ - 'autoescape-tag08': - r'{% autoescape on %}{{ var|default_if_none:" endquote\" hah" }}{% endautoescape %}' - }) + output = self.engine.render_to_string( + "autoescape-tag07", {"first": mark_safe("Apple")} + ) + self.assertEqual(output, "Apple") + + @setup( + { + "autoescape-tag08": r'{% autoescape on %}{{ var|default_if_none:" endquote\" hah" }}{% endautoescape %}' + } + ) def test_autoescape_tag08(self): """ Literal string arguments to filters, if used in the result, are safe. """ - output = self.engine.render_to_string('autoescape-tag08', {"var": None}) + output = self.engine.render_to_string("autoescape-tag08", {"var": None}) self.assertEqual(output, ' endquote" hah') # Objects which return safe strings as their __str__ method # won't get double-escaped. - @setup({'autoescape-tag09': r'{{ unsafe }}'}) + @setup({"autoescape-tag09": r"{{ unsafe }}"}) def test_autoescape_tag09(self): - output = self.engine.render_to_string('autoescape-tag09', {'unsafe': UnsafeClass()}) - self.assertEqual(output, 'you & me') + output = self.engine.render_to_string( + "autoescape-tag09", {"unsafe": UnsafeClass()} + ) + self.assertEqual(output, "you & me") - @setup({'autoescape-tag10': r'{{ safe }}'}) + @setup({"autoescape-tag10": r"{{ safe }}"}) def test_autoescape_tag10(self): - output = self.engine.render_to_string('autoescape-tag10', {'safe': SafeClass()}) - self.assertEqual(output, 'you > me') - - @setup({'autoescape-filtertag01': '{{ first }}{% filter safe %}{{ first }} x'}) + self.engine.render_to_string("autoescape-filtertag01", {"first": ""}) # Arguments to filters are 'safe' and manipulate their input unescaped. - @setup({'autoescape-filters01': '{{ var|cut:"&" }}'}) + @setup({"autoescape-filters01": '{{ var|cut:"&" }}'}) def test_autoescape_filters01(self): - output = self.engine.render_to_string('autoescape-filters01', {'var': 'this & that'}) - self.assertEqual(output, 'this that') + output = self.engine.render_to_string( + "autoescape-filters01", {"var": "this & that"} + ) + self.assertEqual(output, "this that") - @setup({'autoescape-filters02': '{{ var|join:" & " }}'}) + @setup({"autoescape-filters02": '{{ var|join:" & " }}'}) def test_autoescape_filters02(self): - output = self.engine.render_to_string('autoescape-filters02', {'var': ('Tom', 'Dick', 'Harry')}) - self.assertEqual(output, 'Tom & Dick & Harry') + output = self.engine.render_to_string( + "autoescape-filters02", {"var": ("Tom", "Dick", "Harry")} + ) + self.assertEqual(output, "Tom & Dick & Harry") - @setup({'autoescape-literals01': '{{ "this & that" }}'}) + @setup({"autoescape-literals01": '{{ "this & that" }}'}) def test_autoescape_literals01(self): """ Literal strings are safe. """ - output = self.engine.render_to_string('autoescape-literals01') - self.assertEqual(output, 'this & that') + output = self.engine.render_to_string("autoescape-literals01") + self.assertEqual(output, "this & that") - @setup({'autoescape-stringiterations01': '{% for l in var %}{{ l }},{% endfor %}'}) + @setup({"autoescape-stringiterations01": "{% for l in var %}{{ l }},{% endfor %}"}) def test_autoescape_stringiterations01(self): """ Iterating over strings outputs safe characters. """ - output = self.engine.render_to_string('autoescape-stringiterations01', {'var': 'K&R'}) - self.assertEqual(output, 'K,&,R,') + output = self.engine.render_to_string( + "autoescape-stringiterations01", {"var": "K&R"} + ) + self.assertEqual(output, "K,&,R,") - @setup({'autoescape-lookup01': '{{ var.key }}'}) + @setup({"autoescape-lookup01": "{{ var.key }}"}) def test_autoescape_lookup01(self): """ Escape requirement survives lookup. """ - output = self.engine.render_to_string('autoescape-lookup01', {'var': {'key': 'this & that'}}) - self.assertEqual(output, 'this & that') - - @setup({'autoescape-incorrect-arg': '{% autoescape true %}{{ var.key }}{% endautoescape %}'}) + output = self.engine.render_to_string( + "autoescape-lookup01", {"var": {"key": "this & that"}} + ) + self.assertEqual(output, "this & that") + + @setup( + { + "autoescape-incorrect-arg": "{% autoescape true %}{{ var.key }}{% endautoescape %}" + } + ) def test_invalid_arg(self): msg = "'autoescape' argument should be 'on' or 'off'" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('autoescape-incorrect-arg', {'var': {'key': 'this & that'}}) + self.engine.render_to_string( + "autoescape-incorrect-arg", {"var": {"key": "this & that"}} + ) - @setup({'autoescape-incorrect-arg': '{% autoescape %}{{ var.key }}{% endautoescape %}'}) + @setup( + {"autoescape-incorrect-arg": "{% autoescape %}{{ var.key }}{% endautoescape %}"} + ) def test_no_arg(self): msg = "'autoescape' tag requires exactly one argument." with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('autoescape-incorrect-arg', {'var': {'key': 'this & that'}}) + self.engine.render_to_string( + "autoescape-incorrect-arg", {"var": {"key": "this & that"}} + ) diff --git a/tests/template_tests/syntax_tests/test_basic.py b/tests/template_tests/syntax_tests/test_basic.py index 011ce51229..20bf30d55c 100644 --- a/tests/template_tests/syntax_tests/test_basic.py +++ b/tests/template_tests/syntax_tests/test_basic.py @@ -6,20 +6,19 @@ from django.test import SimpleTestCase from ..utils import SilentAttrClass, SilentGetItemClass, SomeClass, setup basic_templates = { - 'basic-syntax01': 'something cool', - 'basic-syntax02': '{{ headline }}', - 'basic-syntax03': '{{ first }} --- {{ second }}', + "basic-syntax01": "something cool", + "basic-syntax02": "{{ headline }}", + "basic-syntax03": "{{ first }} --- {{ second }}", } class BasicSyntaxTests(SimpleTestCase): - @setup(basic_templates) def test_basic_syntax01(self): """ Plain text should go through the template parser untouched. """ - output = self.engine.render_to_string('basic-syntax01') + output = self.engine.render_to_string("basic-syntax01") self.assertEqual(output, "something cool") @setup(basic_templates) @@ -28,318 +27,330 @@ class BasicSyntaxTests(SimpleTestCase): Variables should be replaced with their value in the current context """ - output = self.engine.render_to_string('basic-syntax02', {'headline': 'Success'}) - self.assertEqual(output, 'Success') + output = self.engine.render_to_string("basic-syntax02", {"headline": "Success"}) + self.assertEqual(output, "Success") @setup(basic_templates) def test_basic_syntax03(self): """ More than one replacement variable is allowed in a template """ - output = self.engine.render_to_string('basic-syntax03', {"first": 1, "second": 2}) - self.assertEqual(output, '1 --- 2') + output = self.engine.render_to_string( + "basic-syntax03", {"first": 1, "second": 2} + ) + self.assertEqual(output, "1 --- 2") - @setup({'basic-syntax04': 'as{{ missing }}df'}) + @setup({"basic-syntax04": "as{{ missing }}df"}) def test_basic_syntax04(self): """ Fail silently when a variable is not found in the current context """ - output = self.engine.render_to_string('basic-syntax04') + output = self.engine.render_to_string("basic-syntax04") if self.engine.string_if_invalid: - self.assertEqual(output, 'asINVALIDdf') + self.assertEqual(output, "asINVALIDdf") else: - self.assertEqual(output, 'asdf') + self.assertEqual(output, "asdf") - @setup({'basic-syntax06': '{{ multi word variable }}'}) + @setup({"basic-syntax06": "{{ multi word variable }}"}) def test_basic_syntax06(self): """ A variable may not contain more than one word """ with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax06') + self.engine.get_template("basic-syntax06") - @setup({'basic-syntax07': '{{ }}'}) + @setup({"basic-syntax07": "{{ }}"}) def test_basic_syntax07(self): """ Raise TemplateSyntaxError for empty variable tags. """ - with self.assertRaisesMessage(TemplateSyntaxError, 'Empty variable tag on line 1'): - self.engine.get_template('basic-syntax07') + with self.assertRaisesMessage( + TemplateSyntaxError, "Empty variable tag on line 1" + ): + self.engine.get_template("basic-syntax07") - @setup({'basic-syntax08': '{{ }}'}) + @setup({"basic-syntax08": "{{ }}"}) def test_basic_syntax08(self): """ Raise TemplateSyntaxError for empty variable tags. """ - with self.assertRaisesMessage(TemplateSyntaxError, 'Empty variable tag on line 1'): - self.engine.get_template('basic-syntax08') + with self.assertRaisesMessage( + TemplateSyntaxError, "Empty variable tag on line 1" + ): + self.engine.get_template("basic-syntax08") - @setup({'basic-syntax09': '{{ var.method }}'}) + @setup({"basic-syntax09": "{{ var.method }}"}) def test_basic_syntax09(self): """ Attribute syntax allows a template to call an object's attribute """ - output = self.engine.render_to_string('basic-syntax09', {'var': SomeClass()}) - self.assertEqual(output, 'SomeClass.method') + output = self.engine.render_to_string("basic-syntax09", {"var": SomeClass()}) + self.assertEqual(output, "SomeClass.method") - @setup({'basic-syntax10': '{{ var.otherclass.method }}'}) + @setup({"basic-syntax10": "{{ var.otherclass.method }}"}) def test_basic_syntax10(self): """ Multiple levels of attribute access are allowed. """ - output = self.engine.render_to_string('basic-syntax10', {'var': SomeClass()}) - self.assertEqual(output, 'OtherClass.method') + output = self.engine.render_to_string("basic-syntax10", {"var": SomeClass()}) + self.assertEqual(output, "OtherClass.method") - @setup({'basic-syntax11': '{{ var.blech }}'}) + @setup({"basic-syntax11": "{{ var.blech }}"}) def test_basic_syntax11(self): """ Fail silently when a variable's attribute isn't found. """ - output = self.engine.render_to_string('basic-syntax11', {'var': SomeClass()}) + output = self.engine.render_to_string("basic-syntax11", {"var": SomeClass()}) if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') + self.assertEqual(output, "") - @setup({'basic-syntax12': '{{ var.__dict__ }}'}) + @setup({"basic-syntax12": "{{ var.__dict__ }}"}) def test_basic_syntax12(self): """ Raise TemplateSyntaxError when trying to access a variable beginning with an underscore. """ with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax12') + self.engine.get_template("basic-syntax12") # Raise TemplateSyntaxError when trying to access a variable # containing an illegal character. - @setup({'basic-syntax13': "{{ va>r }}"}) + @setup({"basic-syntax13": "{{ va>r }}"}) def test_basic_syntax13(self): with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax13') + self.engine.get_template("basic-syntax13") - @setup({'basic-syntax14': "{{ (var.r) }}"}) + @setup({"basic-syntax14": "{{ (var.r) }}"}) def test_basic_syntax14(self): with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax14') + self.engine.get_template("basic-syntax14") - @setup({'basic-syntax15': "{{ sp%am }}"}) + @setup({"basic-syntax15": "{{ sp%am }}"}) def test_basic_syntax15(self): with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax15') + self.engine.get_template("basic-syntax15") - @setup({'basic-syntax16': "{{ eggs! }}"}) + @setup({"basic-syntax16": "{{ eggs! }}"}) def test_basic_syntax16(self): with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax16') + self.engine.get_template("basic-syntax16") - @setup({'basic-syntax17': "{{ moo? }}"}) + @setup({"basic-syntax17": "{{ moo? }}"}) def test_basic_syntax17(self): with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax17') + self.engine.get_template("basic-syntax17") - @setup({'basic-syntax18': "{{ foo.bar }}"}) + @setup({"basic-syntax18": "{{ foo.bar }}"}) def test_basic_syntax18(self): """ Attribute syntax allows a template to call a dictionary key's value. """ - output = self.engine.render_to_string('basic-syntax18', {"foo": {"bar": "baz"}}) + output = self.engine.render_to_string("basic-syntax18", {"foo": {"bar": "baz"}}) self.assertEqual(output, "baz") - @setup({'basic-syntax19': "{{ foo.spam }}"}) + @setup({"basic-syntax19": "{{ foo.spam }}"}) def test_basic_syntax19(self): """ Fail silently when a variable's dictionary key isn't found. """ - output = self.engine.render_to_string('basic-syntax19', {"foo": {"bar": "baz"}}) + output = self.engine.render_to_string("basic-syntax19", {"foo": {"bar": "baz"}}) if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') + self.assertEqual(output, "") - @setup({'basic-syntax20': "{{ var.method2 }}"}) + @setup({"basic-syntax20": "{{ var.method2 }}"}) def test_basic_syntax20(self): """ Fail silently when accessing a non-simple method """ - output = self.engine.render_to_string('basic-syntax20', {'var': SomeClass()}) + output = self.engine.render_to_string("basic-syntax20", {"var": SomeClass()}) if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') + self.assertEqual(output, "") - @setup({'basic-syntax20b': "{{ var.method5 }}"}) + @setup({"basic-syntax20b": "{{ var.method5 }}"}) def test_basic_syntax20b(self): """ Don't silence a TypeError if it was raised inside a callable. """ - template = self.engine.get_template('basic-syntax20b') + template = self.engine.get_template("basic-syntax20b") with self.assertRaises(TypeError): - template.render(Context({'var': SomeClass()})) + template.render(Context({"var": SomeClass()})) # Don't get confused when parsing something that is almost, but not # quite, a template tag. - @setup({'basic-syntax21': "a {{ moo %} b"}) + @setup({"basic-syntax21": "a {{ moo %} b"}) def test_basic_syntax21(self): - output = self.engine.render_to_string('basic-syntax21') + output = self.engine.render_to_string("basic-syntax21") self.assertEqual(output, "a {{ moo %} b") - @setup({'basic-syntax22': "{{ moo #}"}) + @setup({"basic-syntax22": "{{ moo #}"}) def test_basic_syntax22(self): - output = self.engine.render_to_string('basic-syntax22') + output = self.engine.render_to_string("basic-syntax22") self.assertEqual(output, "{{ moo #}") - @setup({'basic-syntax23': "{{ moo #} {{ cow }}"}) + @setup({"basic-syntax23": "{{ moo #} {{ cow }}"}) def test_basic_syntax23(self): """ Treat "moo #} {{ cow" as the variable. Not ideal, but costly to work around, so this triggers an error. """ with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('basic-syntax23') + self.engine.get_template("basic-syntax23") - @setup({'basic-syntax24': "{{ moo\n }}"}) + @setup({"basic-syntax24": "{{ moo\n }}"}) def test_basic_syntax24(self): """ Embedded newlines make it not-a-tag. """ - output = self.engine.render_to_string('basic-syntax24') + output = self.engine.render_to_string("basic-syntax24") self.assertEqual(output, "{{ moo\n }}") # Literal strings are permitted inside variables, mostly for i18n # purposes. - @setup({'basic-syntax25': '{{ "fred" }}'}) + @setup({"basic-syntax25": '{{ "fred" }}'}) def test_basic_syntax25(self): - output = self.engine.render_to_string('basic-syntax25') + output = self.engine.render_to_string("basic-syntax25") self.assertEqual(output, "fred") - @setup({'basic-syntax26': r'{{ "\"fred\"" }}'}) + @setup({"basic-syntax26": r'{{ "\"fred\"" }}'}) def test_basic_syntax26(self): - output = self.engine.render_to_string('basic-syntax26') - self.assertEqual(output, "\"fred\"") + output = self.engine.render_to_string("basic-syntax26") + self.assertEqual(output, '"fred"') - @setup({'basic-syntax27': r'{{ _("\"fred\"") }}'}) + @setup({"basic-syntax27": r'{{ _("\"fred\"") }}'}) def test_basic_syntax27(self): - output = self.engine.render_to_string('basic-syntax27') - self.assertEqual(output, "\"fred\"") + output = self.engine.render_to_string("basic-syntax27") + self.assertEqual(output, '"fred"') # #12554 -- Make sure a silent_variable_failure Exception is # suppressed on dictionary and attribute lookup. - @setup({'basic-syntax28': "{{ a.b }}"}) + @setup({"basic-syntax28": "{{ a.b }}"}) def test_basic_syntax28(self): - output = self.engine.render_to_string('basic-syntax28', {'a': SilentGetItemClass()}) + output = self.engine.render_to_string( + "basic-syntax28", {"a": SilentGetItemClass()} + ) if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') + self.assertEqual(output, "") - @setup({'basic-syntax29': "{{ a.b }}"}) + @setup({"basic-syntax29": "{{ a.b }}"}) def test_basic_syntax29(self): - output = self.engine.render_to_string('basic-syntax29', {'a': SilentAttrClass()}) + output = self.engine.render_to_string( + "basic-syntax29", {"a": SilentAttrClass()} + ) if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') + self.assertEqual(output, "INVALID") else: - self.assertEqual(output, '') + self.assertEqual(output, "") # Something that starts like a number but has an extra lookup works # as a lookup. - @setup({'basic-syntax30': "{{ 1.2.3 }}"}) + @setup({"basic-syntax30": "{{ 1.2.3 }}"}) def test_basic_syntax30(self): output = self.engine.render_to_string( - 'basic-syntax30', - {"1": {"2": {"3": "d"}}} + "basic-syntax30", {"1": {"2": {"3": "d"}}} ) - self.assertEqual(output, 'd') + self.assertEqual(output, "d") - @setup({'basic-syntax31': "{{ 1.2.3 }}"}) + @setup({"basic-syntax31": "{{ 1.2.3 }}"}) def test_basic_syntax31(self): output = self.engine.render_to_string( - 'basic-syntax31', + "basic-syntax31", {"1": {"2": ("a", "b", "c", "d")}}, ) - self.assertEqual(output, 'd') + self.assertEqual(output, "d") - @setup({'basic-syntax32': "{{ 1.2.3 }}"}) + @setup({"basic-syntax32": "{{ 1.2.3 }}"}) def test_basic_syntax32(self): output = self.engine.render_to_string( - 'basic-syntax32', + "basic-syntax32", {"1": (("x", "x", "x", "x"), ("y", "y", "y", "y"), ("a", "b", "c", "d"))}, ) - self.assertEqual(output, 'd') + self.assertEqual(output, "d") - @setup({'basic-syntax33': "{{ 1.2.3 }}"}) + @setup({"basic-syntax33": "{{ 1.2.3 }}"}) def test_basic_syntax33(self): output = self.engine.render_to_string( - 'basic-syntax33', + "basic-syntax33", {"1": ("xxxx", "yyyy", "abcd")}, ) - self.assertEqual(output, 'd') + self.assertEqual(output, "d") - @setup({'basic-syntax34': "{{ 1.2.3 }}"}) + @setup({"basic-syntax34": "{{ 1.2.3 }}"}) def test_basic_syntax34(self): output = self.engine.render_to_string( - 'basic-syntax34', - {"1": ({"x": "x"}, {"y": "y"}, {"z": "z", "3": "d"})} + "basic-syntax34", {"1": ({"x": "x"}, {"y": "y"}, {"z": "z", "3": "d"})} ) - self.assertEqual(output, 'd') + self.assertEqual(output, "d") # Numbers are numbers even if their digits are in the context. - @setup({'basic-syntax35': "{{ 1 }}"}) + @setup({"basic-syntax35": "{{ 1 }}"}) def test_basic_syntax35(self): - output = self.engine.render_to_string('basic-syntax35', {"1": "abc"}) - self.assertEqual(output, '1') + output = self.engine.render_to_string("basic-syntax35", {"1": "abc"}) + self.assertEqual(output, "1") - @setup({'basic-syntax36': "{{ 1.2 }}"}) + @setup({"basic-syntax36": "{{ 1.2 }}"}) def test_basic_syntax36(self): - output = self.engine.render_to_string('basic-syntax36', {"1": "abc"}) - self.assertEqual(output, '1.2') + output = self.engine.render_to_string("basic-syntax36", {"1": "abc"}) + self.assertEqual(output, "1.2") - @setup({'basic-syntax37': '{{ callable }}'}) + @setup({"basic-syntax37": "{{ callable }}"}) def test_basic_syntax37(self): """ Call methods in the top level of the context. """ - output = self.engine.render_to_string('basic-syntax37', {"callable": lambda: "foo bar"}) - self.assertEqual(output, 'foo bar') + output = self.engine.render_to_string( + "basic-syntax37", {"callable": lambda: "foo bar"} + ) + self.assertEqual(output, "foo bar") - @setup({'basic-syntax38': '{{ var.callable }}'}) + @setup({"basic-syntax38": "{{ var.callable }}"}) def test_basic_syntax38(self): """ Call methods returned from dictionary lookups. """ - output = self.engine.render_to_string('basic-syntax38', {"var": {"callable": lambda: "foo bar"}}) - self.assertEqual(output, 'foo bar') + output = self.engine.render_to_string( + "basic-syntax38", {"var": {"callable": lambda: "foo bar"}} + ) + self.assertEqual(output, "foo bar") - @setup({'template': '{% block content %}'}) + @setup({"template": "{% block content %}"}) def test_unclosed_block(self): msg = "Unclosed tag on line 1: 'block'. Looking for one of: endblock." with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'template': '{% if a %}'}) + @setup({"template": "{% if a %}"}) def test_unclosed_block2(self): msg = "Unclosed tag on line 1: 'if'. Looking for one of: elif, else, endif." with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') + self.engine.render_to_string("template") - @setup({'tpl-str': '%s', 'tpl-percent': '%%', 'tpl-weird-percent': '% %s'}) + @setup({"tpl-str": "%s", "tpl-percent": "%%", "tpl-weird-percent": "% %s"}) def test_ignores_strings_that_look_like_format_interpolation(self): - output = self.engine.render_to_string('tpl-str') - self.assertEqual(output, '%s') - output = self.engine.render_to_string('tpl-percent') - self.assertEqual(output, '%%') - output = self.engine.render_to_string('tpl-weird-percent') - self.assertEqual(output, '% %s') + output = self.engine.render_to_string("tpl-str") + self.assertEqual(output, "%s") + output = self.engine.render_to_string("tpl-percent") + self.assertEqual(output, "%%") + output = self.engine.render_to_string("tpl-weird-percent") + self.assertEqual(output, "% %s") class BlockContextTests(SimpleTestCase): def test_repr(self): block_context = BlockContext() - block_context.add_blocks({'content': BlockNode('content', [])}) + block_context.add_blocks({"content": BlockNode("content", [])}) self.assertEqual( repr(block_context), ", " diff --git a/tests/template_tests/syntax_tests/test_builtins.py b/tests/template_tests/syntax_tests/test_builtins.py index 8ade96c65a..6e81410556 100644 --- a/tests/template_tests/syntax_tests/test_builtins.py +++ b/tests/template_tests/syntax_tests/test_builtins.py @@ -4,18 +4,17 @@ from ..utils import setup class BuiltinsTests(SimpleTestCase): - - @setup({'builtins01': '{{ True }}'}) + @setup({"builtins01": "{{ True }}"}) def test_builtins01(self): - output = self.engine.render_to_string('builtins01') - self.assertEqual(output, 'True') + output = self.engine.render_to_string("builtins01") + self.assertEqual(output, "True") - @setup({'builtins02': '{{ False }}'}) + @setup({"builtins02": "{{ False }}"}) def test_builtins02(self): - output = self.engine.render_to_string('builtins02') - self.assertEqual(output, 'False') + output = self.engine.render_to_string("builtins02") + self.assertEqual(output, "False") - @setup({'builtins03': '{{ None }}'}) + @setup({"builtins03": "{{ None }}"}) def test_builtins03(self): - output = self.engine.render_to_string('builtins03') - self.assertEqual(output, 'None') + output = self.engine.render_to_string("builtins03") + self.assertEqual(output, "None") diff --git a/tests/template_tests/syntax_tests/test_cache.py b/tests/template_tests/syntax_tests/test_cache.py index 80af1c2bd6..a722420637 100644 --- a/tests/template_tests/syntax_tests/test_cache.py +++ b/tests/template_tests/syntax_tests/test_cache.py @@ -7,176 +7,203 @@ from ..utils import setup class CacheTagTests(SimpleTestCase): libraries = { - 'cache': 'django.templatetags.cache', - 'custom': 'template_tests.templatetags.custom', + "cache": "django.templatetags.cache", + "custom": "template_tests.templatetags.custom", } def tearDown(self): cache.clear() - @setup({'cache03': '{% load cache %}{% cache 2 test %}cache03{% endcache %}'}) + @setup({"cache03": "{% load cache %}{% cache 2 test %}cache03{% endcache %}"}) def test_cache03(self): - output = self.engine.render_to_string('cache03') - self.assertEqual(output, 'cache03') - - @setup({ - 'cache03': '{% load cache %}{% cache 2 test %}cache03{% endcache %}', - 'cache04': '{% load cache %}{% cache 2 test %}cache04{% endcache %}', - }) + output = self.engine.render_to_string("cache03") + self.assertEqual(output, "cache03") + + @setup( + { + "cache03": "{% load cache %}{% cache 2 test %}cache03{% endcache %}", + "cache04": "{% load cache %}{% cache 2 test %}cache04{% endcache %}", + } + ) def test_cache04(self): - self.engine.render_to_string('cache03') - output = self.engine.render_to_string('cache04') - self.assertEqual(output, 'cache03') + self.engine.render_to_string("cache03") + output = self.engine.render_to_string("cache04") + self.assertEqual(output, "cache03") - @setup({'cache05': '{% load cache %}{% cache 2 test foo %}cache05{% endcache %}'}) + @setup({"cache05": "{% load cache %}{% cache 2 test foo %}cache05{% endcache %}"}) def test_cache05(self): - output = self.engine.render_to_string('cache05', {'foo': 1}) - self.assertEqual(output, 'cache05') + output = self.engine.render_to_string("cache05", {"foo": 1}) + self.assertEqual(output, "cache05") - @setup({'cache06': '{% load cache %}{% cache 2 test foo %}cache06{% endcache %}'}) + @setup({"cache06": "{% load cache %}{% cache 2 test foo %}cache06{% endcache %}"}) def test_cache06(self): - output = self.engine.render_to_string('cache06', {'foo': 2}) - self.assertEqual(output, 'cache06') - - @setup({ - 'cache05': '{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', - 'cache07': '{% load cache %}{% cache 2 test foo %}cache07{% endcache %}', - }) + output = self.engine.render_to_string("cache06", {"foo": 2}) + self.assertEqual(output, "cache06") + + @setup( + { + "cache05": "{% load cache %}{% cache 2 test foo %}cache05{% endcache %}", + "cache07": "{% load cache %}{% cache 2 test foo %}cache07{% endcache %}", + } + ) def test_cache07(self): - context = {'foo': 1} - self.engine.render_to_string('cache05', context) - output = self.engine.render_to_string('cache07', context) - self.assertEqual(output, 'cache05') - - @setup({ - 'cache06': '{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', - 'cache08': '{% load cache %}{% cache time test foo %}cache08{% endcache %}', - }) + context = {"foo": 1} + self.engine.render_to_string("cache05", context) + output = self.engine.render_to_string("cache07", context) + self.assertEqual(output, "cache05") + + @setup( + { + "cache06": "{% load cache %}{% cache 2 test foo %}cache06{% endcache %}", + "cache08": "{% load cache %}{% cache time test foo %}cache08{% endcache %}", + } + ) def test_cache08(self): """ Allow first argument to be a variable. """ - context = {'foo': 2, 'time': 2} - self.engine.render_to_string('cache06', context) - output = self.engine.render_to_string('cache08', context) - self.assertEqual(output, 'cache06') + context = {"foo": 2, "time": 2} + self.engine.render_to_string("cache06", context) + output = self.engine.render_to_string("cache08", context) + self.assertEqual(output, "cache06") # Raise exception if we don't have at least 2 args, first one integer. - @setup({'cache11': '{% load cache %}{% cache %}{% endcache %}'}) + @setup({"cache11": "{% load cache %}{% cache %}{% endcache %}"}) def test_cache11(self): with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('cache11') + self.engine.get_template("cache11") - @setup({'cache12': '{% load cache %}{% cache 1 %}{% endcache %}'}) + @setup({"cache12": "{% load cache %}{% cache 1 %}{% endcache %}"}) def test_cache12(self): with self.assertRaises(TemplateSyntaxError): - self.engine.get_template('cache12') + self.engine.get_template("cache12") - @setup({'cache13': '{% load cache %}{% cache foo bar %}{% endcache %}'}) + @setup({"cache13": "{% load cache %}{% cache foo bar %}{% endcache %}"}) def test_cache13(self): with self.assertRaises(TemplateSyntaxError): - self.engine.render_to_string('cache13') + self.engine.render_to_string("cache13") - @setup({'cache14': '{% load cache %}{% cache foo bar %}{% endcache %}'}) + @setup({"cache14": "{% load cache %}{% cache foo bar %}{% endcache %}"}) def test_cache14(self): with self.assertRaises(TemplateSyntaxError): - self.engine.render_to_string('cache14', {'foo': 'fail'}) + self.engine.render_to_string("cache14", {"foo": "fail"}) - @setup({'cache15': '{% load cache %}{% cache foo bar %}{% endcache %}'}) + @setup({"cache15": "{% load cache %}{% cache foo bar %}{% endcache %}"}) def test_cache15(self): with self.assertRaises(TemplateSyntaxError): - self.engine.render_to_string('cache15', {'foo': []}) + self.engine.render_to_string("cache15", {"foo": []}) - @setup({'cache16': '{% load cache %}{% cache 1 foo bar %}{% endcache %}'}) + @setup({"cache16": "{% load cache %}{% cache 1 foo bar %}{% endcache %}"}) def test_cache16(self): """ Regression test for #7460. """ - output = self.engine.render_to_string('cache16', {'foo': 'foo', 'bar': 'with spaces'}) - self.assertEqual(output, '') + output = self.engine.render_to_string( + "cache16", {"foo": "foo", "bar": "with spaces"} + ) + self.assertEqual(output, "") - @setup({'cache17': '{% load cache %}{% cache 10 long_cache_key poem %}Some Content{% endcache %}'}) + @setup( + { + "cache17": "{% load cache %}{% cache 10 long_cache_key poem %}Some Content{% endcache %}" + } + ) def test_cache17(self): """ Regression test for #11270. """ output = self.engine.render_to_string( - 'cache17', + "cache17", { - 'poem': ( - 'Oh freddled gruntbuggly/Thy micturations are to me/' - 'As plurdled gabbleblotchits/On a lurgid bee/' - 'That mordiously hath bitled out/Its earted jurtles/' - 'Into a rancid festering/Or else I shall rend thee in the gobberwarts' - 'with my blurglecruncheon/See if I don\'t.' + "poem": ( + "Oh freddled gruntbuggly/Thy micturations are to me/" + "As plurdled gabbleblotchits/On a lurgid bee/" + "That mordiously hath bitled out/Its earted jurtles/" + "Into a rancid festering/Or else I shall rend thee in the gobberwarts" + "with my blurglecruncheon/See if I don't." ), - } + }, ) - self.assertEqual(output, 'Some Content') + self.assertEqual(output, "Some Content") - @setup({'cache18': '{% load cache custom %}{% cache 2|noop:"x y" cache18 %}cache18{% endcache %}'}) + @setup( + { + "cache18": '{% load cache custom %}{% cache 2|noop:"x y" cache18 %}cache18{% endcache %}' + } + ) def test_cache18(self): """ Test whitespace in filter arguments """ - output = self.engine.render_to_string('cache18') - self.assertEqual(output, 'cache18') - - @setup({ - 'first': '{% load cache %}{% cache None fragment19 %}content{% endcache %}', - 'second': '{% load cache %}{% cache None fragment19 %}not rendered{% endcache %}' - }) + output = self.engine.render_to_string("cache18") + self.assertEqual(output, "cache18") + + @setup( + { + "first": "{% load cache %}{% cache None fragment19 %}content{% endcache %}", + "second": "{% load cache %}{% cache None fragment19 %}not rendered{% endcache %}", + } + ) def test_none_timeout(self): """A timeout of None means "cache forever".""" - output = self.engine.render_to_string('first') - self.assertEqual(output, 'content') - output = self.engine.render_to_string('second') - self.assertEqual(output, 'content') + output = self.engine.render_to_string("first") + self.assertEqual(output, "content") + output = self.engine.render_to_string("second") + self.assertEqual(output, "content") class CacheTests(SimpleTestCase): - @classmethod def setUpClass(cls): - cls.engine = Engine(libraries={'cache': 'django.templatetags.cache'}) + cls.engine = Engine(libraries={"cache": "django.templatetags.cache"}) super().setUpClass() def test_cache_regression_20130(self): - t = self.engine.from_string('{% load cache %}{% cache 1 regression_20130 %}foo{% endcache %}') + t = self.engine.from_string( + "{% load cache %}{% cache 1 regression_20130 %}foo{% endcache %}" + ) cachenode = t.nodelist[1] - self.assertEqual(cachenode.fragment_name, 'regression_20130') - - @override_settings(CACHES={ - 'default': { - 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', - 'LOCATION': 'default', - }, - 'template_fragments': { - 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', - 'LOCATION': 'fragments', - }, - }) + self.assertEqual(cachenode.fragment_name, "regression_20130") + + @override_settings( + CACHES={ + "default": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + "LOCATION": "default", + }, + "template_fragments": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + "LOCATION": "fragments", + }, + } + ) def test_cache_fragment_cache(self): """ When a cache called "template_fragments" is present, the cache tag will use it in preference to 'default' """ - t1 = self.engine.from_string('{% load cache %}{% cache 1 fragment %}foo{% endcache %}') - t2 = self.engine.from_string('{% load cache %}{% cache 1 fragment using="default" %}bar{% endcache %}') + t1 = self.engine.from_string( + "{% load cache %}{% cache 1 fragment %}foo{% endcache %}" + ) + t2 = self.engine.from_string( + '{% load cache %}{% cache 1 fragment using="default" %}bar{% endcache %}' + ) ctx = Context() o1 = t1.render(ctx) o2 = t2.render(ctx) - self.assertEqual(o1, 'foo') - self.assertEqual(o2, 'bar') + self.assertEqual(o1, "foo") + self.assertEqual(o2, "bar") def test_cache_missing_backend(self): """ When a cache that doesn't exist is specified, the cache tag will raise a TemplateSyntaxError '""" - t = self.engine.from_string('{% load cache %}{% cache 1 backend using="unknown" %}bar{% endcache %}') + t = self.engine.from_string( + '{% load cache %}{% cache 1 backend using="unknown" %}bar{% endcache %}' + ) ctx = Context() with self.assertRaises(TemplateSyntaxError): diff --git a/tests/template_tests/syntax_tests/test_comment.py b/tests/template_tests/syntax_tests/test_comment.py index d1b4dcf147..58dbe88b36 100644 --- a/tests/template_tests/syntax_tests/test_comment.py +++ b/tests/template_tests/syntax_tests/test_comment.py @@ -4,89 +4,92 @@ from ..utils import setup class CommentSyntaxTests(SimpleTestCase): - - @setup({'comment-syntax01': '{# this is hidden #}hello'}) + @setup({"comment-syntax01": "{# this is hidden #}hello"}) def test_comment_syntax01(self): - output = self.engine.render_to_string('comment-syntax01') - self.assertEqual(output, 'hello') + output = self.engine.render_to_string("comment-syntax01") + self.assertEqual(output, "hello") - @setup({'comment-syntax02': '{# this is hidden #}hello{# foo #}'}) + @setup({"comment-syntax02": "{# this is hidden #}hello{# foo #}"}) def test_comment_syntax02(self): - output = self.engine.render_to_string('comment-syntax02') - self.assertEqual(output, 'hello') + output = self.engine.render_to_string("comment-syntax02") + self.assertEqual(output, "hello") - @setup({'comment-syntax03': 'foo{# {% if %} #}'}) + @setup({"comment-syntax03": "foo{# {% if %} #}"}) def test_comment_syntax03(self): - output = self.engine.render_to_string('comment-syntax03') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax03") + self.assertEqual(output, "foo") - @setup({'comment-syntax04': 'foo{# {% endblock %} #}'}) + @setup({"comment-syntax04": "foo{# {% endblock %} #}"}) def test_comment_syntax04(self): - output = self.engine.render_to_string('comment-syntax04') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax04") + self.assertEqual(output, "foo") - @setup({'comment-syntax05': 'foo{# {% somerandomtag %} #}'}) + @setup({"comment-syntax05": "foo{# {% somerandomtag %} #}"}) def test_comment_syntax05(self): - output = self.engine.render_to_string('comment-syntax05') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax05") + self.assertEqual(output, "foo") - @setup({'comment-syntax06': 'foo{# {% #}'}) + @setup({"comment-syntax06": "foo{# {% #}"}) def test_comment_syntax06(self): - output = self.engine.render_to_string('comment-syntax06') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax06") + self.assertEqual(output, "foo") - @setup({'comment-syntax07': 'foo{# %} #}'}) + @setup({"comment-syntax07": "foo{# %} #}"}) def test_comment_syntax07(self): - output = self.engine.render_to_string('comment-syntax07') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax07") + self.assertEqual(output, "foo") - @setup({'comment-syntax08': 'foo{# %} #}bar'}) + @setup({"comment-syntax08": "foo{# %} #}bar"}) def test_comment_syntax08(self): - output = self.engine.render_to_string('comment-syntax08') - self.assertEqual(output, 'foobar') + output = self.engine.render_to_string("comment-syntax08") + self.assertEqual(output, "foobar") - @setup({'comment-syntax09': 'foo{# {{ #}'}) + @setup({"comment-syntax09": "foo{# {{ #}"}) def test_comment_syntax09(self): - output = self.engine.render_to_string('comment-syntax09') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax09") + self.assertEqual(output, "foo") - @setup({'comment-syntax10': 'foo{# }} #}'}) + @setup({"comment-syntax10": "foo{# }} #}"}) def test_comment_syntax10(self): - output = self.engine.render_to_string('comment-syntax10') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax10") + self.assertEqual(output, "foo") - @setup({'comment-syntax11': 'foo{# { #}'}) + @setup({"comment-syntax11": "foo{# { #}"}) def test_comment_syntax11(self): - output = self.engine.render_to_string('comment-syntax11') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax11") + self.assertEqual(output, "foo") - @setup({'comment-syntax12': 'foo{# } #}'}) + @setup({"comment-syntax12": "foo{# } #}"}) def test_comment_syntax12(self): - output = self.engine.render_to_string('comment-syntax12') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-syntax12") + self.assertEqual(output, "foo") - @setup({'comment-tag01': '{% comment %}this is hidden{% endcomment %}hello'}) + @setup({"comment-tag01": "{% comment %}this is hidden{% endcomment %}hello"}) def test_comment_tag01(self): - output = self.engine.render_to_string('comment-tag01') - self.assertEqual(output, 'hello') - - @setup({'comment-tag02': '{% comment %}this is hidden{% endcomment %}' - 'hello{% comment %}foo{% endcomment %}'}) + output = self.engine.render_to_string("comment-tag01") + self.assertEqual(output, "hello") + + @setup( + { + "comment-tag02": "{% comment %}this is hidden{% endcomment %}" + "hello{% comment %}foo{% endcomment %}" + } + ) def test_comment_tag02(self): - output = self.engine.render_to_string('comment-tag02') - self.assertEqual(output, 'hello') + output = self.engine.render_to_string("comment-tag02") + self.assertEqual(output, "hello") - @setup({'comment-tag03': 'foo{% comment %} {% if %} {% endcomment %}'}) + @setup({"comment-tag03": "foo{% comment %} {% if %} {% endcomment %}"}) def test_comment_tag03(self): - output = self.engine.render_to_string('comment-tag03') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-tag03") + self.assertEqual(output, "foo") - @setup({'comment-tag04': 'foo{% comment %} {% endblock %} {% endcomment %}'}) + @setup({"comment-tag04": "foo{% comment %} {% endblock %} {% endcomment %}"}) def test_comment_tag04(self): - output = self.engine.render_to_string('comment-tag04') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-tag04") + self.assertEqual(output, "foo") - @setup({'comment-tag05': 'foo{% comment %} {% somerandomtag %} {% endcomment %}'}) + @setup({"comment-tag05": "foo{% comment %} {% somerandomtag %} {% endcomment %}"}) def test_comment_tag05(self): - output = self.engine.render_to_string('comment-tag05') - self.assertEqual(output, 'foo') + output = self.engine.render_to_string("comment-tag05") + self.assertEqual(output, "foo") diff --git a/tests/template_tests/syntax_tests/test_cycle.py b/tests/template_tests/syntax_tests/test_cycle.py index 12bf66e117..d4233fac3e 100644 --- a/tests/template_tests/syntax_tests/test_cycle.py +++ b/tests/template_tests/syntax_tests/test_cycle.py @@ -5,175 +5,214 @@ from ..utils import setup class CycleTagTests(SimpleTestCase): - - @setup({'cycle01': '{% cycle a %}'}) + @setup({"cycle01": "{% cycle a %}"}) def test_cycle01(self): msg = "No named cycles in template. 'a' is not defined" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.get_template('cycle01') + self.engine.get_template("cycle01") - @setup({'cycle05': '{% cycle %}'}) + @setup({"cycle05": "{% cycle %}"}) def test_cycle05(self): msg = "'cycle' tag requires at least two arguments" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.get_template('cycle05') + self.engine.get_template("cycle05") - @setup({'cycle07': '{% cycle a,b,c as foo %}{% cycle bar %}'}) + @setup({"cycle07": "{% cycle a,b,c as foo %}{% cycle bar %}"}) def test_cycle07(self): msg = "Could not parse the remainder: ',b,c' from 'a,b,c'" with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.get_template('cycle07') + self.engine.get_template("cycle07") - @setup({'cycle10': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}"}) + @setup({"cycle10": "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}"}) def test_cycle10(self): - output = self.engine.render_to_string('cycle10') - self.assertEqual(output, 'ab') + output = self.engine.render_to_string("cycle10") + self.assertEqual(output, "ab") - @setup({'cycle11': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}"}) + @setup({"cycle11": "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}"}) def test_cycle11(self): - output = self.engine.render_to_string('cycle11') - self.assertEqual(output, 'abc') - - @setup({'cycle12': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"}) + output = self.engine.render_to_string("cycle11") + self.assertEqual(output, "abc") + + @setup( + { + "cycle12": "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}" + } + ) def test_cycle12(self): - output = self.engine.render_to_string('cycle12') - self.assertEqual(output, 'abca') + output = self.engine.render_to_string("cycle12") + self.assertEqual(output, "abca") - @setup({'cycle13': "{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}"}) + @setup({"cycle13": "{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}"}) def test_cycle13(self): - output = self.engine.render_to_string('cycle13', {'test': list(range(5))}) - self.assertEqual(output, 'a0,b1,a2,b3,a4,') + output = self.engine.render_to_string("cycle13", {"test": list(range(5))}) + self.assertEqual(output, "a0,b1,a2,b3,a4,") - @setup({'cycle14': '{% cycle one two as foo %}{% cycle foo %}'}) + @setup({"cycle14": "{% cycle one two as foo %}{% cycle foo %}"}) def test_cycle14(self): - output = self.engine.render_to_string('cycle14', {'one': '1', 'two': '2'}) - self.assertEqual(output, '12') + output = self.engine.render_to_string("cycle14", {"one": "1", "two": "2"}) + self.assertEqual(output, "12") - @setup({'cycle15': '{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}'}) + @setup({"cycle15": "{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}"}) def test_cycle15(self): - output = self.engine.render_to_string('cycle15', {'test': list(range(5)), 'aye': 'a', 'bee': 'b'}) - self.assertEqual(output, 'a0,b1,a2,b3,a4,') + output = self.engine.render_to_string( + "cycle15", {"test": list(range(5)), "aye": "a", "bee": "b"} + ) + self.assertEqual(output, "a0,b1,a2,b3,a4,") - @setup({'cycle16': '{% cycle one|lower two as foo %}{% cycle foo %}'}) + @setup({"cycle16": "{% cycle one|lower two as foo %}{% cycle foo %}"}) def test_cycle16(self): - output = self.engine.render_to_string('cycle16', {'one': 'A', 'two': '2'}) - self.assertEqual(output, 'a2') - - @setup({'cycle17': "{% cycle 'a' 'b' 'c' as abc silent %}" - "{% cycle abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}"}) + output = self.engine.render_to_string("cycle16", {"one": "A", "two": "2"}) + self.assertEqual(output, "a2") + + @setup( + { + "cycle17": "{% cycle 'a' 'b' 'c' as abc silent %}" + "{% cycle abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}" + } + ) def test_cycle17(self): - output = self.engine.render_to_string('cycle17') - self.assertEqual(output, '') + output = self.engine.render_to_string("cycle17") + self.assertEqual(output, "") - @setup({'cycle18': "{% cycle 'a' 'b' 'c' as foo invalid_flag %}"}) + @setup({"cycle18": "{% cycle 'a' 'b' 'c' as foo invalid_flag %}"}) def test_cycle18(self): msg = "Only 'silent' flag is allowed after cycle's name, not 'invalid_flag'." with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.get_template('cycle18') + self.engine.get_template("cycle18") - @setup({'cycle19': "{% cycle 'a' 'b' as silent %}{% cycle silent %}"}) + @setup({"cycle19": "{% cycle 'a' 'b' as silent %}{% cycle silent %}"}) def test_cycle19(self): - output = self.engine.render_to_string('cycle19') - self.assertEqual(output, 'ab') + output = self.engine.render_to_string("cycle19") + self.assertEqual(output, "ab") - @setup({'cycle20': '{% cycle one two as foo %} & {% cycle foo %}'}) + @setup({"cycle20": "{% cycle one two as foo %} & {% cycle foo %}"}) def test_cycle20(self): - output = self.engine.render_to_string('cycle20', {'two': 'C & D', 'one': 'A & B'}) - self.assertEqual(output, 'A & B & C & D') - - @setup({'cycle21': '{% filter force_escape %}' - '{% cycle one two as foo %} & {% cycle foo %}{% endfilter %}'}) + output = self.engine.render_to_string( + "cycle20", {"two": "C & D", "one": "A & B"} + ) + self.assertEqual(output, "A & B & C & D") + + @setup( + { + "cycle21": "{% filter force_escape %}" + "{% cycle one two as foo %} & {% cycle foo %}{% endfilter %}" + } + ) def test_cycle21(self): - output = self.engine.render_to_string('cycle21', {'two': 'C & D', 'one': 'A & B'}) - self.assertEqual(output, 'A &amp; B & C &amp; D') - - @setup({'cycle22': "{% for x in values %}{% cycle 'a' 'b' 'c' as abc silent %}{{ x }}{% endfor %}"}) + output = self.engine.render_to_string( + "cycle21", {"two": "C & D", "one": "A & B"} + ) + self.assertEqual(output, "A &amp; B & C &amp; D") + + @setup( + { + "cycle22": "{% for x in values %}{% cycle 'a' 'b' 'c' as abc silent %}{{ x }}{% endfor %}" + } + ) def test_cycle22(self): - output = self.engine.render_to_string('cycle22', {'values': [1, 2, 3, 4]}) - self.assertEqual(output, '1234') - - @setup({'cycle23': "{% for x in values %}" - "{% cycle 'a' 'b' 'c' as abc silent %}{{ abc }}{{ x }}{% endfor %}"}) + output = self.engine.render_to_string("cycle22", {"values": [1, 2, 3, 4]}) + self.assertEqual(output, "1234") + + @setup( + { + "cycle23": "{% for x in values %}" + "{% cycle 'a' 'b' 'c' as abc silent %}{{ abc }}{{ x }}{% endfor %}" + } + ) def test_cycle23(self): - output = self.engine.render_to_string('cycle23', {'values': [1, 2, 3, 4]}) - self.assertEqual(output, 'a1b2c3a4') - - @setup({ - 'cycle24': "{% for x in values %}" - "{% cycle 'a' 'b' 'c' as abc silent %}{% include 'included-cycle' %}{% endfor %}", - 'included-cycle': '{{ abc }}', - }) + output = self.engine.render_to_string("cycle23", {"values": [1, 2, 3, 4]}) + self.assertEqual(output, "a1b2c3a4") + + @setup( + { + "cycle24": "{% for x in values %}" + "{% cycle 'a' 'b' 'c' as abc silent %}{% include 'included-cycle' %}{% endfor %}", + "included-cycle": "{{ abc }}", + } + ) def test_cycle24(self): - output = self.engine.render_to_string('cycle24', {'values': [1, 2, 3, 4]}) - self.assertEqual(output, 'abca') + output = self.engine.render_to_string("cycle24", {"values": [1, 2, 3, 4]}) + self.assertEqual(output, "abca") - @setup({'cycle25': '{% cycle a as abc %}'}) + @setup({"cycle25": "{% cycle a as abc %}"}) def test_cycle25(self): - output = self.engine.render_to_string('cycle25', {'a': '<'}) - self.assertEqual(output, '<') + output = self.engine.render_to_string("cycle25", {"a": "<"}) + self.assertEqual(output, "<") - @setup({'cycle26': '{% cycle a b as ab %}{% cycle ab %}'}) + @setup({"cycle26": "{% cycle a b as ab %}{% cycle ab %}"}) def test_cycle26(self): - output = self.engine.render_to_string('cycle26', {'a': '<', 'b': '>'}) - self.assertEqual(output, '<>') - - @setup({'cycle27': '{% autoescape off %}{% cycle a b as ab %}{% cycle ab %}{% endautoescape %}'}) + output = self.engine.render_to_string("cycle26", {"a": "<", "b": ">"}) + self.assertEqual(output, "<>") + + @setup( + { + "cycle27": "{% autoescape off %}{% cycle a b as ab %}{% cycle ab %}{% endautoescape %}" + } + ) def test_cycle27(self): - output = self.engine.render_to_string('cycle27', {'a': '<', 'b': '>'}) - self.assertEqual(output, '<>') + output = self.engine.render_to_string("cycle27", {"a": "<", "b": ">"}) + self.assertEqual(output, "<>") - @setup({'cycle28': '{% cycle a|safe b as ab %}{% cycle ab %}'}) + @setup({"cycle28": "{% cycle a|safe b as ab %}{% cycle ab %}"}) def test_cycle28(self): - output = self.engine.render_to_string('cycle28', {'a': '<', 'b': '>'}) - self.assertEqual(output, '<>') - - @setup({ - 'cycle29': "{% cycle 'a' 'b' 'c' as cycler silent %}" - "{% for x in values %}" - "{% ifchanged x %}" - "{% cycle cycler %}{{ cycler }}" - "{% else %}" - "{{ cycler }}" - "{% endifchanged %}" - "{% endfor %}" - }) + output = self.engine.render_to_string("cycle28", {"a": "<", "b": ">"}) + self.assertEqual(output, "<>") + + @setup( + { + "cycle29": "{% cycle 'a' 'b' 'c' as cycler silent %}" + "{% for x in values %}" + "{% ifchanged x %}" + "{% cycle cycler %}{{ cycler }}" + "{% else %}" + "{{ cycler }}" + "{% endifchanged %}" + "{% endfor %}" + } + ) def test_cycle29(self): """ A named {% cycle %} tag works inside an {% ifchanged %} block and a {% for %} loop. """ - output = self.engine.render_to_string('cycle29', {'values': [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]}) - self.assertEqual(output, 'bcabcabcccaa') - - @setup({ - 'cycle30': "{% cycle 'a' 'b' 'c' as cycler silent %}" - "{% for x in values %}" - "{% with doesnothing=irrelevant %}" - "{% ifchanged x %}" - "{% cycle cycler %}{{ cycler }}" - "{% else %}" - "{{ cycler }}" - "{% endifchanged %}" - "{% endwith %}" - "{% endfor %}"}) + output = self.engine.render_to_string( + "cycle29", {"values": [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]} + ) + self.assertEqual(output, "bcabcabcccaa") + + @setup( + { + "cycle30": "{% cycle 'a' 'b' 'c' as cycler silent %}" + "{% for x in values %}" + "{% with doesnothing=irrelevant %}" + "{% ifchanged x %}" + "{% cycle cycler %}{{ cycler }}" + "{% else %}" + "{{ cycler }}" + "{% endifchanged %}" + "{% endwith %}" + "{% endfor %}" + } + ) def test_cycle30(self): """ A {% with %} tag shouldn't reset the {% cycle %} variable. """ output = self.engine.render_to_string( - 'cycle30', { - 'irrelevant': 1, - 'values': [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9] - }) - self.assertEqual(output, 'bcabcabcccaa') - - @setup({ - 'undefined_cycle': - "{% cycle 'a' 'b' 'c' as cycler silent %}" + "cycle30", {"irrelevant": 1, "values": [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]} + ) + self.assertEqual(output, "bcabcabcccaa") + + @setup( + { + "undefined_cycle": "{% cycle 'a' 'b' 'c' as cycler silent %}" "{% for x in values %}" "{% cycle undefined %}{{ cycler }}" "{% endfor %}" - }) + } + ) def test_cycle_undefined(self): - with self.assertRaisesMessage(TemplateSyntaxError, "Named cycle 'undefined' does not exist"): - self.engine.render_to_string('undefined_cycle') + with self.assertRaisesMessage( + TemplateSyntaxError, "Named cycle 'undefined' does not exist" + ): + self.engine.render_to_string("undefined_cycle") diff --git a/tests/template_tests/syntax_tests/test_debug.py b/tests/template_tests/syntax_tests/test_debug.py index 2f527b44ae..cbd71fa51a 100644 --- a/tests/template_tests/syntax_tests/test_debug.py +++ b/tests/template_tests/syntax_tests/test_debug.py @@ -6,41 +6,40 @@ from ..utils import setup @override_settings(DEBUG=True) class DebugTests(SimpleTestCase): - @override_settings(DEBUG=False) - @setup({'non_debug': '{% debug %}'}) + @setup({"non_debug": "{% debug %}"}) def test_non_debug(self): - output = self.engine.render_to_string('non_debug', {}) - self.assertEqual(output, '') + output = self.engine.render_to_string("non_debug", {}) + self.assertEqual(output, "") - @setup({'modules': '{% debug %}'}) + @setup({"modules": "{% debug %}"}) def test_modules(self): - output = self.engine.render_to_string('modules', {}) + output = self.engine.render_to_string("modules", {}) self.assertIn( - ''django': <module 'django' ', + "'django': <module 'django' ", output, ) - @setup({'plain': '{% debug %}'}) + @setup({"plain": "{% debug %}"}) def test_plain(self): - output = self.engine.render_to_string('plain', {'a': 1}) - self.assertTrue(output.startswith( - '{'a': 1}' - '{'False': False, 'None': None, ' - ''True': True}\n\n{' - )) + output = self.engine.render_to_string("plain", {"a": 1}) + self.assertTrue( + output.startswith( + "{'a': 1}" + "{'False': False, 'None': None, " + "'True': True}\n\n{" + ) + ) - @setup({'non_ascii': '{% debug %}'}) + @setup({"non_ascii": "{% debug %}"}) def test_non_ascii(self): group = Group(name="清風") - output = self.engine.render_to_string('non_ascii', {'group': group}) - self.assertTrue(output.startswith( - '{'group': <Group: 清風>}' - )) + output = self.engine.render_to_string("non_ascii", {"group": group}) + self.assertTrue(output.startswith("{'group': <Group: 清風>}")) - @setup({'script': '{% debug %}'}) + @setup({"script": "{% debug %}"}) def test_script(self): - output = self.engine.render_to_string('script', {'frag': '