summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-10-28 06:17:58 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-10-28 06:17:58 -0400
commitb7fc90dca7bed9f5dffe615a4fdba1dac9ea57d5 (patch)
tree625dc523ea6968b286ed96a6849d7af9b4aa336c
parentdaee486d28d25eae6748873b3aae823e39035809 (diff)
downloadpython-coveragepy-git-b7fc90dca7bed9f5dffe615a4fdba1dac9ea57d5.tar.gz
test: add a annotate test, and simplify the logic
-rw-r--r--coverage/annotate.py7
-rw-r--r--tests/gold/annotate/mae/mae.py,cover10
-rw-r--r--tests/test_annotate.py20
3 files changed, 32 insertions, 5 deletions
diff --git a/coverage/annotate.py b/coverage/annotate.py
index 9ca1b80a..07ff644d 100644
--- a/coverage/annotate.py
+++ b/coverage/annotate.py
@@ -74,8 +74,7 @@ class AnnotateReporter:
dest_file = fr.filename + ",cover"
with open(dest_file, 'w', encoding='utf-8') as dest:
- i = 0
- j = 0
+ i = j = 0
covered = True
source = fr.source()
for lineno, line in enumerate(source.splitlines(True), start=1):
@@ -89,9 +88,7 @@ class AnnotateReporter:
dest.write(' ')
elif self.else_re.match(line):
# Special logic for lines containing only 'else:'.
- if i >= len(statements) and j >= len(missing):
- dest.write('! ')
- elif i >= len(statements) or j >= len(missing):
+ if j >= len(missing):
dest.write('> ')
elif statements[i] == missing[j]:
dest.write('! ')
diff --git a/tests/gold/annotate/mae/mae.py,cover b/tests/gold/annotate/mae/mae.py,cover
new file mode 100644
index 00000000..ca086e93
--- /dev/null
+++ b/tests/gold/annotate/mae/mae.py,cover
@@ -0,0 +1,10 @@
+> def f(x):
+> if x == 1:
+> print("1")
+> else:
+> print("2")
+
+> if f(1):
+! print("nope")
+> if f(2):
+! print("nope")
diff --git a/tests/test_annotate.py b/tests/test_annotate.py
index de6edcd0..f85450c7 100644
--- a/tests/test_annotate.py
+++ b/tests/test_annotate.py
@@ -105,3 +105,23 @@ class AnnotationGoldTest(CoverageTest):
self.start_import_stop(cov, "white")
cov.annotate()
compare(gold_path("annotate/annotate"), ".", "*,cover")
+
+ def test_missing_after_else(self):
+ self.make_file("mae.py", """\
+ def f(x):
+ if x == 1:
+ print("1")
+ else:
+ print("2")
+
+ if f(1):
+ print("nope")
+ if f(2):
+ print("nope")
+ """)
+
+ cov = coverage.Coverage()
+ self.start_import_stop(cov, "mae")
+ cov.annotate()
+ assert self.stdout() == "1\n2\n"
+ compare(gold_path("annotate/mae"), ".", "*,cover")