|
1 """Miscellaneous stuff for Coverage.""" |
|
2 |
|
3 def nice_pair(pair): |
|
4 """Make a nice string representation of a pair of numbers. |
|
5 |
|
6 If the numbers are equal, just return the number, otherwise return the pair |
|
7 with a dash between them, indicating the range. |
|
8 |
|
9 """ |
|
10 start, end = pair |
|
11 if start == end: |
|
12 return "%d" % start |
|
13 else: |
|
14 return "%d-%d" % (start, end) |
|
15 |
|
16 |
|
17 def format_lines(statements, lines): |
|
18 """Nicely format a list of line numbers. |
|
19 |
|
20 Format a list of line numbers for printing by coalescing groups of lines as |
|
21 long as the lines represent consecutive statements. This will coalesce |
|
22 even if there are gaps between statements. |
|
23 |
|
24 For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and |
|
25 `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14". |
|
26 |
|
27 """ |
|
28 pairs = [] |
|
29 i = 0 |
|
30 j = 0 |
|
31 start = None |
|
32 pairs = [] |
|
33 while i < len(statements) and j < len(lines): |
|
34 if statements[i] == lines[j]: |
|
35 if start == None: |
|
36 start = lines[j] |
|
37 end = lines[j] |
|
38 j = j + 1 |
|
39 elif start: |
|
40 pairs.append((start, end)) |
|
41 start = None |
|
42 i = i + 1 |
|
43 if start: |
|
44 pairs.append((start, end)) |
|
45 ret = ', '.join(map(nice_pair, pairs)) |
|
46 return ret |
|
47 |
|
48 |
|
49 class CoverageException(Exception): |
|
50 """An exception specific to Coverage.""" |
|
51 pass |