summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-02-03 07:29:59 -0500
committerNed Batchelder <ned@nedbatchelder.com>2017-02-03 07:29:59 -0500
commit1b757d383820b78b97e86f2902aa6c339bddeda3 (patch)
tree9dde322fb401a2655b52a37be77ac950a6a81093
parent179ffff7263c5e978a03366d86819b6e4c1f8714 (diff)
downloadpython-coveragepy-1b757d383820b78b97e86f2902aa6c339bddeda3.tar.gz
Fiddle with, and test, format_lines
-rw-r--r--coverage/misc.py27
-rw-r--r--tests/test_misc.py16
2 files changed, 31 insertions, 12 deletions
diff --git a/coverage/misc.py b/coverage/misc.py
index 9c4843e..28aa3b0 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -110,23 +110,28 @@ def format_lines(statements, lines):
For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
`lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".
+ Both `lines` and `statements` can be any iterable. All of the elements of
+ `lines` must be in `statements`, and all of the values must be positive
+ integers.
+
"""
- pairs = []
- i = 0
- j = 0
- start = None
statements = sorted(statements)
lines = sorted(lines)
- while i < len(statements) and j < len(lines):
- if statements[i] == lines[j]:
- if start is None:
- start = lines[j]
- end = lines[j]
- j += 1
+
+ pairs = []
+ start = None
+ lidx = 0
+ for stmt in statements:
+ if lidx >= len(lines):
+ break
+ if stmt == lines[lidx]:
+ lidx += 1
+ if not start:
+ start = stmt
+ end = stmt
elif start:
pairs.append((start, end))
start = None
- i += 1
if start:
pairs.append((start, end))
ret = ', '.join(map(nice_pair, pairs))
diff --git a/tests/test_misc.py b/tests/test_misc.py
index be6d110..939b1c9 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -5,7 +5,8 @@
import pytest
-from coverage.misc import contract, dummy_decorator_with_args, file_be_gone, Hasher, one_of
+from coverage.misc import contract, dummy_decorator_with_args, file_be_gone
+from coverage.misc import format_lines, Hasher, one_of
from tests.coveragetest import CoverageTest
@@ -114,3 +115,16 @@ class ContractTest(CoverageTest):
assert undecorated(17) == (17, None)
assert undecorated(b=23) == (None, 23)
assert undecorated(b=42, a=3) == (3, 42)
+
+
+@pytest.mark.parametrize("statements, lines, result", [
+ (set([1,2,3,4,5,10,11,12,13,14]), set([1,2,5,10,11,13,14]), "1-2, 5-11, 13-14"),
+ ([1,2,3,4,5,10,11,12,13,14,98,99], [1,2,5,10,11,13,14,99], "1-2, 5-11, 13-14, 99"),
+ ([1,2,3,4,98,99,100,101,102,103,104], [1,2,99,102,103,104], "1-2, 99, 102-104"),
+ ([17], [17], "17"),
+ ([90,91,92,93,94,95], [90,91,92,93,94,95], "90-95"),
+ ([1, 2, 3, 4, 5], [], ""),
+ ([1, 2, 3, 4, 5], [4], "4"),
+])
+def test_format_lines(statements, lines, result):
+ assert format_lines(statements, lines) == result