diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2016-02-15 10:11:25 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-02-15 10:11:25 -0500 |
commit | babe79f03ea50e26accc397480e1dee2ea53152e (patch) | |
tree | aedae2306608c44f547b64c81d4ee55f2b4c4d7b | |
parent | d2f0e8e892417a453a9c1bd9b6237366520d7d51 (diff) | |
download | python-coveragepy-git-babe79f03ea50e26accc397480e1dee2ea53152e.tar.gz |
Add missing branch explanations for while-loop
-rw-r--r-- | coverage/parser.py | 10 | ||||
-rw-r--r-- | tests/test_parser.py | 15 |
2 files changed, 20 insertions, 5 deletions
diff --git a/coverage/parser.py b/coverage/parser.py index 84cd3d1d..f84133d2 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -870,19 +870,21 @@ class AstArcAnalyzer(object): if constant_test: to_top = self.line_for_node(node.body[0]) self.block_stack.append(LoopBlock(start=start)) - exits = self.add_body_arcs(node.body, from_start=ArcStart(start)) + from_start = ArcStart(start, cause="the condition on line {lineno} was never true") + exits = self.add_body_arcs(node.body, from_start=from_start) for xit in exits: - self.add_arc(xit.lineno, to_top) + self.add_arc(xit.lineno, to_top, xit.cause) exits = set() my_block = self.block_stack.pop() exits.update(my_block.break_exits) + from_start = ArcStart(start, cause="the condition on line {lineno} was never false") if node.orelse: - else_exits = self.add_body_arcs(node.orelse, from_start=ArcStart(start)) + else_exits = self.add_body_arcs(node.orelse, from_start=from_start) exits |= else_exits else: # No `else` clause: you can exit from the start. if not constant_test: - exits.add(ArcStart(start)) + exits.add(from_start) return exits @contract(returns='ArcStarts') diff --git a/tests/test_parser.py b/tests/test_parser.py index 0d00409e..1be5e16c 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -206,8 +206,13 @@ class ParserMissingArcDescriptionTest(CoverageTest): def func5(): for x in range(6): - if x == 3: + if x == 7: break + + def func10(): + while something(11): + thing(12) + more_stuff(13) """) self.assertEqual( parser.missing_arc_description(1, 2), @@ -225,6 +230,14 @@ class ParserMissingArcDescriptionTest(CoverageTest): parser.missing_arc_description(6, 7), "line 6 didn't jump to line 7, because the loop on line 6 never started" ) + self.assertEqual( + parser.missing_arc_description(11, 12), + "line 11 didn't jump to line 12, because the condition on line 11 was never true" + ) + self.assertEqual( + parser.missing_arc_description(11, 13), + "line 11 didn't jump to line 13, because the condition on line 11 was never false" + ) def test_missing_arc_descriptions_for_small_callables(self): # We use 2.7 features here, so just skip this test on 2.6 |