Skip to content

Commit 05f5a50

Browse files
committed
Test the i18n tags.
1 parent ecb6624 commit 05f5a50

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Caveats
4040

4141
The `{% ssi %}` tag does not trace the files it includes.
4242

43+
Coverage can't tell whether a `{% blocktrans %}` tag used the singular or
44+
plural text, so both are marked as used if the tag is used.
45+
4346

4447
What the? How?
4548
--------------

django_coverage_plugin/plugin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Lexer, TextNode,
1414
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR,
1515
)
16+
from django.templatetags.i18n import BlockTranslateNode
1617
try:
1718
from django.template.defaulttags import VerbatimNode
1819
except ImportError:
@@ -62,8 +63,7 @@ def sys_info(self):
6263

6364
def file_tracer(self, filename):
6465
if filename.startswith(self.django_template_dir):
65-
if "templatetags" not in filename:
66-
return self
66+
return self
6767
return None
6868

6969
def file_reporter(self, filename):
@@ -111,6 +111,12 @@ def line_number_range(self, frame):
111111
# to the end of the {% verbatim %} opening tag, not the entire
112112
# content. Adjust it to cover all of it.
113113
s_end += len(render_self.content)
114+
elif isinstance(render_self, BlockTranslateNode):
115+
# BlockTranslateNode has a list of text and variable tokens.
116+
# Get the end of the contents by looking at the last token,
117+
# and use its endpoint.
118+
last_tokens = render_self.plural or render_self.singular
119+
s_end = last_tokens[-1].source[1][1]
114120
line_map = self.get_line_map(source[0].name)
115121
start = get_line_number(line_map, s_start)
116122
end = get_line_number(line_map, s_end-1)

tests/test_i18n.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Tests of i18n tags for django_coverage_plugin."""
2+
3+
from .plugin_test import DjangoPluginTestCase
4+
5+
6+
class I18nTest(DjangoPluginTestCase):
7+
8+
def test_trans(self):
9+
self.make_template("""\
10+
{% load i18n %}
11+
Hello
12+
{% trans "World" %}
13+
done.
14+
""")
15+
text = self.run_django_coverage()
16+
self.assertEqual(text, "\nHello\nWorld\ndone.\n")
17+
self.assert_analysis([1, 2, 3, 4])
18+
19+
def test_blocktrans(self):
20+
self.make_template("""\
21+
{% load i18n %}
22+
Hello
23+
{% blocktrans with where="world" %}
24+
to {{ where }}.
25+
{% endblocktrans %}
26+
bye.
27+
""")
28+
text = self.run_django_coverage()
29+
self.assertEqual(text, "\nHello\n\nto world.\n\nbye.\n")
30+
self.assert_analysis([1, 2, 3, 4, 6])
31+
32+
def test_blocktrans_plural(self):
33+
self.make_template("""\
34+
{% load i18n %}
35+
{% blocktrans count counter=cats|length %}
36+
There is one cat.
37+
{% plural %}
38+
There are {{ counter }} cats.
39+
{% endblocktrans %}
40+
bye.
41+
""")
42+
# It doesn't make a difference whether you use the plural or not, we
43+
# can't tell, so the singluar and plural are always marked as used.
44+
text = self.run_django_coverage(context={'cats': ['snowy']})
45+
self.assertEqual(text, "\n\nThere is one cat.\n\nbye.\n")
46+
self.assert_analysis([1, 2, 3, 4, 5, 7])
47+
48+
text = self.run_django_coverage(context={'cats': ['snowy', 'coaly']})
49+
self.assertEqual(text, "\n\nThere are 2 cats.\n\nbye.\n")
50+
self.assert_analysis([1, 2, 3, 4, 5, 7])

0 commit comments

Comments
 (0)