Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions django_coverage_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ def lines(self):
if lines[0].isspace():
lineno += 1
num_lines -= 1
# When a tag is not at the start of a line, the preceding
# TEXT token ends with whitespace and no newline.
# That partial line is not executable content.
if num_lines > 0 and (
lines[-1].isspace() and not lines[-1].endswith(("\n", "\r"))
):
num_lines -= 1
source_lines.update(range(lineno, lineno+num_lines))

if SHOW_PARSING:
Expand Down
68 changes: 68 additions & 0 deletions tests/test_extends.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,74 @@ def test_inheriting_with_unused_blocks(self):
self.assert_analysis([1, 2, 3], name="base.html")
self.assert_analysis([1, 4, 8], [8], name="specific.html")

def test_empty_parent_block_on_new_line_when_extended(self):
"""
When a block is empty and extended, endblock should not appear
as an uncovered line.

https://github.com/coveragepy/django_coverage_plugin/issues/74
"""
self.make_template(name="base.html", text="""\
Hello
{% block content %}
{% endblock content %}
Goodbye
""")
self.make_template(name="child.html", text="""\
{% extends "base.html" %}
{% block content %}
Override
{% endblock %}
""")
text = self.run_django_coverage(name="child.html")
self.assert_analysis([1, 2, 4], name="base.html")
self.assert_analysis([1, 3], name="child.html")
self.assertEqual(text.strip(), "Hello\n \n Override\n\nGoodbye")

def test_non_empty_parent_block_when_extended(self):
self.make_template(name="base.html", text="""\
Hello
{% block content %}
This line should be reported as uncovered.
{% endblock content %}
Goodbye
""")
self.make_template(name="child.html", text="""\
{% extends "base.html" %}
{% block content %}
Override
{% endblock %}
""")
text = self.run_django_coverage(name="child.html")
self.assert_analysis([1, 2, 3, 5], missing=[3], name="base.html")
self.assert_analysis([1, 3], name="child.html")

self.assertEqual(text.strip(), "Hello\n \n Override\n\nGoodbye")

def test_nested_blocks_outer_endblock_on_its_own_line(self):
"""
When blocks are nested, on their own lines, and extended,
then endblock should not appear as uncovered.

Ref: https://github.com/coveragepy/django_coverage_plugin/issues/74
"""
self.make_template(name="base.html", text="""\
{% block outer %}
{% block inner %}
{% endblock inner %}
{% endblock outer %}
""")
self.make_template(name="child.html", text="""\
{% extends "base.html" %}
{% block inner %}
Override
{% endblock %}
""")
text = self.run_django_coverage(name="child.html")
self.assert_analysis([1, 2], missing=[], name="base.html")
self.assert_analysis([1, 3], name="child.html")
self.assertEqual(text.strip(), "Override")


class LoadTest(DjangoPluginTestCase):
def test_load(self):
Expand Down
46 changes: 41 additions & 5 deletions tests/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ def test_if(self):
self.assertEqual(text.strip(), '')
self.assert_analysis([1, 2], [2])

def test_endif_not_at_start_of_line(self):
self.make_template("""\
<article>
{% if foo %}
Hello
{% endif %}
After
</article>
""")
self.run_django_coverage(context={'foo': False})
self.assert_analysis([1, 2, 3, 5, 6], missing=[3])

def test_else_not_at_start_of_line(self):
self.make_template("""\
<article>
{% if foo %}
Hello
{% else %}
Goodbye
{% endif %}
</article>
""")
self.run_django_coverage(context={'foo': True})
self.assert_analysis([1, 2, 3, 5, 7], missing=[5])

def test_if_else(self):
self.make_template("""\
{% if foo %}
Expand Down Expand Up @@ -84,6 +109,17 @@ def test_loop(self):
self.assertEqual(text, "Before\n\nAfter\n")
self.assert_analysis([1, 2, 3, 5], [3])

def test_endfor_not_at_start_of_line(self):
self.make_template("""\
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
""")
self.run_django_coverage(context={'items': []})
self.assert_analysis([1, 2, 3, 5], missing=[3])

def test_loop_with_empty_clause(self):
self.make_template("""\
Before
Expand Down Expand Up @@ -135,7 +171,7 @@ def test_regroup(self):
<ul><li>New York: 20</li><li>Chicago: 7</li></ul></li><li>Japan
<ul><li>Tokyo: 33</li></ul></li></ul>
"""))
self.assert_analysis([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13])
self.assert_analysis([1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13])


class IfChangedTest(DjangoPluginTestCase):
Expand All @@ -154,7 +190,7 @@ def test_ifchanged(self):
'items': [("A", "X"), ("A", "Y"), ("B", "Z"), ("B", "W")],
})
self.assertEqual(squashed(text), 'AXYBZW')
self.assert_analysis([1, 2, 3, 4, 5])
self.assert_analysis([1, 2, 3, 5])

def test_ifchanged_variable(self):
self.make_template("""\
Expand All @@ -170,7 +206,7 @@ def test_ifchanged_variable(self):
'items': [("A", "X"), ("A", "Y"), ("B", "Z"), ("B", "W")],
})
self.assertEqual(squashed(text), 'AXYBZW')
self.assert_analysis([1, 2, 3, 4, 5])
self.assert_analysis([1, 2, 3, 5])


@django_stop_before(4, 0)
Expand All @@ -190,7 +226,7 @@ def test_ifequal(self):
'items': [(0, 'A'), (1, 'X'), (2, 'X'), (3, 'B')],
})
self.assertEqual(squashed(text), '0X1X23')
self.assert_analysis([1, 2, 3, 4, 5])
self.assert_analysis([1, 2, 3, 5])

def test_ifnotequal(self):
self.make_template("""\
Expand All @@ -206,4 +242,4 @@ def test_ifnotequal(self):
'items': [(0, 'A'), (1, 'X'), (2, 'X'), (3, 'B')],
})
self.assertEqual(squashed(text), 'X012X3')
self.assert_analysis([1, 2, 3, 4, 5])
self.assert_analysis([1, 2, 3, 5])
11 changes: 11 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ def test_with(self):
self.assertEqual(text, "\nalpha = 1, beta = 2.\n\n")
self.assert_analysis([1, 2])

def test_endwith_not_at_start_of_line(self):
self.make_template("""\
<div>
{% with alpha=1 %}
{{ alpha }}
{% endwith %}
</div>
""")
self.run_django_coverage()
self.assert_analysis([1, 2, 3, 5])


class StringTemplateTest(DjangoPluginTestCase):

Expand Down