summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/parser.py7
-rw-r--r--test/test_arcs.py23
-rw-r--r--test/test_parser.py31
3 files changed, 58 insertions, 3 deletions
diff --git a/coverage/parser.py b/coverage/parser.py
index 4878bdb5..eeed779a 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -215,13 +215,18 @@ class CodeParser(object):
exit_counts = {}
for l1, l2 in self.arcs():
if l1 == -1:
+ # Don't ever report -1 as a line number
continue
if l1 in excluded_lines:
+ # Don't report excluded lines as line numbers.
+ continue
+ if l2 in excluded_lines:
+ # Arcs to excluded lines shouldn't count.
continue
if l1 not in exit_counts:
exit_counts[l1] = 0
exit_counts[l1] += 1
-
+
# Class definitions have one extra exit, so remove one for each:
for l in self.classdefs:
exit_counts[l] -= 1
diff --git a/test/test_arcs.py b/test/test_arcs.py
index dea3700f..effc6e52 100644
--- a/test/test_arcs.py
+++ b/test/test_arcs.py
@@ -113,6 +113,29 @@ class SimpleArcTest(CoverageTest):
arcz=".1 16 67 7. .2 23 24 3. 45 5.", arcz_missing=""
)
+ def test_dont_confuse_exit_and_else(self):
+ self.check_coverage("""\
+ def foo():
+ if foo:
+ a = 3
+ else:
+ a = 5
+ return a
+ assert foo() == 3
+ """,
+ arcz=".1 17 7. .2 23 36 25 56 6.", arcz_missing="25 56"
+ )
+ self.check_coverage("""\
+ def foo():
+ if foo:
+ a = 3
+ else:
+ a = 5
+ foo()
+ """,
+ arcz=".1 16 6. .2 23 3. 25 5.", arcz_missing="25 5."
+ )
+
class LoopArcTest(CoverageTest):
"""Arc-measuring tests involving loops."""
diff --git a/test/test_parser.py b/test/test_parser.py
index 3d5726de..7a9ff066 100644
--- a/test/test_parser.py
+++ b/test/test_parser.py
@@ -16,7 +16,7 @@ class ParserTest(CoverageTest):
def parse_source(self, text):
"""Parse `text` as source, and return the `CodeParser` used."""
text = textwrap.dedent(text)
- cp = CodeParser(text)
+ cp = CodeParser(text, exclude="nocover")
cp.parse_source()
return cp
@@ -52,4 +52,31 @@ class ParserTest(CoverageTest):
self.assertEqual(cp.exit_counts(), {
1: 1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1
})
- \ No newline at end of file
+
+ def test_missing_branch_to_excluded_code(self):
+ cp = self.parse_source("""\
+ if fooey:
+ a = 2
+ else: # nocover
+ a = 4
+ b = 5
+ """)
+ self.assertEqual(cp.exit_counts(), { 1:1, 2:1, 5:1 })
+ cp = self.parse_source("""\
+ def foo():
+ if fooey:
+ a = 3
+ else:
+ a = 5
+ b = 6
+ """)
+ self.assertEqual(cp.exit_counts(), { 1:1, 2:2, 3:1, 5:1, 6:1 })
+ cp = self.parse_source("""\
+ def foo():
+ if fooey:
+ a = 3
+ else: # nocover
+ a = 5
+ b = 6
+ """)
+ self.assertEqual(cp.exit_counts(), { 1:13, 2:1, 6:1 })