Skip to content

Commit 147fdc5

Browse files
committed
Support {% verbatim %}
1 parent 1cd72c3 commit 147fdc5

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

django_coverage_plugin/plugin.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
Lexer, TextNode,
1414
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR,
1515
)
16+
try:
17+
from django.template.defaulttags import VerbatimNode
18+
except ImportError:
19+
# Django 1.4 didn't have VerbatimNode
20+
VerbatimNode = None
1621

1722

1823
SHOW_PARSING = False
@@ -98,10 +103,14 @@ def line_number_range(self, frame):
98103
print("{!r}: {}".format(render_self, source))
99104
s_start, s_end = source[1]
100105
if isinstance(render_self, TextNode):
101-
text = render_self.s
102-
first_line = text.splitlines(True)[0]
106+
first_line = render_self.s.splitlines(True)[0]
103107
if first_line.isspace():
104108
s_start += len(first_line)
109+
elif VerbatimNode and isinstance(render_self, VerbatimNode):
110+
# VerbatimNode doesn't track source the same way. s_end only points
111+
# to the end of the {% verbatim %} opening tag, not the entire
112+
# content. Adjust it to cover all of it.
113+
s_end += len(render_self.content)
105114
line_map = self.get_line_map(source[0].name)
106115
start = get_line_number(line_map, s_start)
107116
end = get_line_number(line_map, s_end-1)

tests/test_simple.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def test_templatetag(self):
186186
self.assertEqual(text, "{%\nurl 'entry_list'\n%}\n")
187187
self.assert_analysis([1, 2, 3])
188188

189-
@needs_django(1, 5) # not sure why, but 1.4 won't reverse "index".
189+
@needs_django(1, 5) # 1.4 had different syntax for {% url %}
190190
def test_url(self):
191191
self.make_template("""\
192192
{% url 'index' %}
@@ -196,6 +196,24 @@ def test_url(self):
196196
self.assertEqual(text, "/home\nnice.\n")
197197
self.assert_analysis([1, 2])
198198

199+
@needs_django(1, 5) # {% verbatim %} is new in 1.5
200+
def test_verbatim(self):
201+
self.make_template("""\
202+
1
203+
{% verbatim %}
204+
{{if dying}}Alive.{{/if}}
205+
second.
206+
{%third%}.
207+
{% endverbatim %}
208+
7
209+
""")
210+
text = self.run_django_coverage()
211+
self.assertEqual(
212+
text,
213+
"1\n\n{{if dying}}Alive.{{/if}}\nsecond.\n{%third%}.\n\n7\n"
214+
)
215+
self.assert_analysis([1, 2, 3, 4, 5, 7])
216+
199217

200218
class StringTemplateTest(DjangoPluginTestCase):
201219

0 commit comments

Comments
 (0)