Skip to content

Commit b7fc90d

Browse files
committed
test: add a annotate test, and simplify the logic
1 parent daee486 commit b7fc90d

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

coverage/annotate.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def annotate_file(self, fr, analysis):
7474
dest_file = fr.filename + ",cover"
7575

7676
with open(dest_file, 'w', encoding='utf-8') as dest:
77-
i = 0
78-
j = 0
77+
i = j = 0
7978
covered = True
8079
source = fr.source()
8180
for lineno, line in enumerate(source.splitlines(True), start=1):
@@ -89,9 +88,7 @@ def annotate_file(self, fr, analysis):
8988
dest.write(' ')
9089
elif self.else_re.match(line):
9190
# Special logic for lines containing only 'else:'.
92-
if i >= len(statements) and j >= len(missing):
93-
dest.write('! ')
94-
elif i >= len(statements) or j >= len(missing):
91+
if j >= len(missing):
9592
dest.write('> ')
9693
elif statements[i] == missing[j]:
9794
dest.write('! ')

tests/gold/annotate/mae/mae.py,cover

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> def f(x):
2+
> if x == 1:
3+
> print("1")
4+
> else:
5+
> print("2")
6+
7+
> if f(1):
8+
! print("nope")
9+
> if f(2):
10+
! print("nope")

tests/test_annotate.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,23 @@ def h(x):
105105
self.start_import_stop(cov, "white")
106106
cov.annotate()
107107
compare(gold_path("annotate/annotate"), ".", "*,cover")
108+
109+
def test_missing_after_else(self):
110+
self.make_file("mae.py", """\
111+
def f(x):
112+
if x == 1:
113+
print("1")
114+
else:
115+
print("2")
116+
117+
if f(1):
118+
print("nope")
119+
if f(2):
120+
print("nope")
121+
""")
122+
123+
cov = coverage.Coverage()
124+
self.start_import_stop(cov, "mae")
125+
cov.annotate()
126+
assert self.stdout() == "1\n2\n"
127+
compare(gold_path("annotate/mae"), ".", "*,cover")

0 commit comments

Comments
 (0)