Increased test coverage for i18n template tags.

This commit is contained in:
Mads Jensen 2018-03-21 08:20:04 +01:00 committed by Tim Graham
parent c76d87427d
commit bb79e480e1
7 changed files with 101 additions and 0 deletions

View File

@ -233,6 +233,45 @@ class I18nBlockTransTagTests(SimpleTestCase):
output = self.engine.render_to_string('template')
self.assertEqual(output, '%s')
@setup({'template': '{% load i18n %}{% blocktrans %}{% block b %} {% endblock %}{% endblocktrans %}'})
def test_with_block(self):
msg = "'blocktrans' doesn't allow other block tags (seen 'block b') inside it"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('template')
@setup({'template': '{% load i18n %}{% blocktrans %}{% for b in [1, 2, 3] %} {% endfor %}{% endblocktrans %}'})
def test_with_for(self):
msg = "'blocktrans' doesn't allow other block tags (seen 'for b in [1, 2, 3]') inside it"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('template')
@setup({'template': '{% load i18n %}{% blocktrans with foo=bar with %}{{ foo }}{% endblocktrans %}'})
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 %}{% blocktrans with %}{% endblocktrans %}'})
def test_no_args_with(self):
msg = '"with" in \'blocktrans\' tag needs at least one keyword argument.'
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('template')
@setup({'template': '{% load i18n %}{% blocktrans count a %}{% endblocktrans %}'})
def test_count(self):
msg = '"count" in \'blocktrans\' tag expected exactly one keyword argument.'
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('template', {'a': [1, 2, 3]})
@setup({'template': (
'{% load i18n %}{% blocktrans count count=var|length %}'
'There is {{ count }} object. {% block a %} {% endblock %}'
'{% endblocktrans %}'
)})
def test_plural_bad_syntax(self):
msg = "'blocktrans' doesn't allow other block tags inside it"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('template', {'var': [1, 2, 3]})
class TranslationBlockTransTagTests(SimpleTestCase):

View File

@ -1,3 +1,4 @@
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase
from ...utils import setup
@ -12,3 +13,9 @@ class GetAvailableLanguagesTagTests(SimpleTestCase):
def test_i18n12(self):
output = self.engine.render_to_string('i18n12')
self.assertEqual(output, 'de')
@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')

View File

@ -0,0 +1,14 @@
from template_tests.utils import setup
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase
class I18nGetCurrentLanguageTagTests(SimpleTestCase):
libraries = {'i18n': 'django.templatetags.i18n'}
@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')

View File

@ -0,0 +1,14 @@
from template_tests.utils import setup
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase
class I18nGetCurrentLanguageBidiTagTests(SimpleTestCase):
libraries = {'i18n': 'django.templatetags.i18n'}
@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')

View File

@ -1,3 +1,4 @@
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase
from django.utils import translation
@ -34,3 +35,9 @@ class I18nGetLanguageInfoTagTests(SimpleTestCase):
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 %}'})
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')

View File

@ -1,3 +1,4 @@
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase
from django.utils import translation
@ -43,3 +44,9 @@ class GetLanguageInfoListTests(SimpleTestCase):
'it: Italian/italiano/italsky bidi=False; '
'fr: French/français/francouzsky bidi=False; '
)
@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')

View File

@ -0,0 +1,13 @@
from template_tests.utils import setup
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase
class I18nLanguageTagTests(SimpleTestCase):
libraries = {'i18n': 'django.templatetags.i18n'}
@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')