diff options
-rw-r--r-- | coverage/collector.py | 8 | ||||
-rw-r--r-- | tests/coveragetest.py | 4 | ||||
-rw-r--r-- | tests/test_arcs.py | 24 |
3 files changed, 34 insertions, 2 deletions
diff --git a/coverage/collector.py b/coverage/collector.py index c1560902..948cbbb0 100644 --- a/coverage/collector.py +++ b/coverage/collector.py @@ -291,10 +291,14 @@ class Collector(object): """ if self.branch: # If we were measuring branches, then we have to re-build the dict - # to show line data. + # to show line data. We'll use the first lines of all the arcs, + # if they are actual lines. We don't need the second lines, because + # the second lines will also be first lines, sometimes to exits. line_data = {} for f, arcs in self.data.items(): - line_data[f] = dict((l1, None) for l1, _ in arcs.keys() if l1) + line_data[f] = dict( + (l1, None) for l1, _ in arcs.keys() if l1 > 0 + ) return line_data else: return self.data diff --git a/tests/coveragetest.py b/tests/coveragetest.py index cf3d2474..0e80f4a9 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -166,6 +166,8 @@ class CoverageTest( `arcs_unpredicted` are the arcs executed in the code, but not deducible from the code. + Returns the Coverage object, in case you want to poke at it some more. + """ # We write the code into a file so that we can import it. # Coverage wants to deal with things as modules with file names. @@ -248,6 +250,8 @@ class CoverageTest( rep = " ".join(frep.getvalue().split("\n")[2].split()[1:]) self.assertEqual(report, rep) + return cov + def nice_file(self, *fparts): """Canonicalize the filename composed of the parts in `fparts`.""" fname = os.path.join(*fparts) diff --git a/tests/test_arcs.py b/tests/test_arcs.py index 42e10510..6f28d05e 100644 --- a/tests/test_arcs.py +++ b/tests/test_arcs.py @@ -1,7 +1,10 @@ """Tests for Coverage.py's arc measurement.""" +import os.path + from tests.coveragetest import CoverageTest +import coverage from coverage import env @@ -627,3 +630,24 @@ class ExcludeTest(CoverageTest): [1,2,3,4,5], partials=["only some"], arcz=".1 12 23 34 45 25 5.", arcz_missing="") + + +class LineDataTest(CoverageTest): + """Tests that line_data gives us what we expect.""" + + def test_branch(self): + cov = coverage.Coverage(branch=True) + + self.make_file("fun1.py", """\ + def fun1(x): + if x == 1: + return + + fun1(3) + """) + + self.start_import_stop(cov, "fun1") + + cov._harvest_data() + fun1_lines = cov.data.line_data()[os.path.abspath("fun1.py")] + self.assertEqual(fun1_lines, [1, 2, 5]) |