summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/parser.py12
-rw-r--r--tests/test_arcs.py16
2 files changed, 23 insertions, 5 deletions
diff --git a/coverage/parser.py b/coverage/parser.py
index c680f63b..b0e7371f 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -520,8 +520,6 @@ class AstArcAnalyzer(object):
my_block = self.block_stack.pop()
exits = my_block.break_exits
if node.orelse:
- else_start = self.line_for_node(node.orelse[0])
- self.arcs.add((start, else_start))
else_exits = self.add_body_arcs(node.orelse, from_line=start)
exits |= else_exits
else:
@@ -653,11 +651,15 @@ class AstArcAnalyzer(object):
for xit in exits:
self.arcs.add((xit, to_top))
exits = set()
- if not constant_test:
- exits.add(start)
my_block = self.block_stack.pop()
exits.update(my_block.break_exits)
- # TODO: orelse
+ if node.orelse:
+ else_exits = self.add_body_arcs(node.orelse, from_line=start)
+ exits |= else_exits
+ else:
+ # No `else` clause: you can exit from the start.
+ if not constant_test:
+ exits.add(start)
return exits
def handle_With(self, node):
diff --git a/tests/test_arcs.py b/tests/test_arcs.py
index fb4b99eb..cab95c8f 100644
--- a/tests/test_arcs.py
+++ b/tests/test_arcs.py
@@ -314,6 +314,22 @@ class LoopArcTest(CoverageTest):
arcz=".1 .2 23 32 34 47 26 67 7. 18 89 9."
)
+ def test_while_else(self):
+ self.check_coverage("""\
+ def whileelse(seq):
+ while seq:
+ n = seq.pop()
+ if n > 4:
+ break
+ else:
+ n = 99
+ return n
+ assert whileelse([1, 2]) == 99
+ assert whileelse([1, 5]) == 5
+ """,
+ arcz=".1 19 9A A. .2 23 34 45 58 42 27 78 8.",
+ )
+
def test_confusing_for_loop_bug_175(self):
if env.PY3:
# Py3 counts the list comp as a separate code object.