summaryrefslogtreecommitdiff
path: root/tests/test_arcs.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_arcs.py')
-rw-r--r--tests/test_arcs.py128
1 files changed, 122 insertions, 6 deletions
diff --git a/tests/test_arcs.py b/tests/test_arcs.py
index 81fa7e6a..df303d8b 100644
--- a/tests/test_arcs.py
+++ b/tests/test_arcs.py
@@ -1,4 +1,7 @@
-"""Tests for Coverage.py's arc measurement."""
+# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
+# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
+
+"""Tests for coverage.py's arc measurement."""
from tests.coveragetest import CoverageTest
@@ -8,7 +11,7 @@ from coverage.files import abs_file
class SimpleArcTest(CoverageTest):
- """Tests for Coverage.py's arc measurement."""
+ """Tests for coverage.py's arc measurement."""
def test_simple_sequence(self):
self.check_coverage("""\
@@ -560,10 +563,123 @@ class ExceptionArcTest(CoverageTest):
arcz_missing="67 7B", arcz_unpredicted="68")
+class YieldTest(CoverageTest):
+ """Arc tests for generators."""
+
+ def test_yield_in_loop(self):
+ self.check_coverage("""\
+ def gen(inp):
+ for n in inp:
+ yield n
+
+ list(gen([1,2,3]))
+ """,
+ arcz=".1 .2 23 2. 32 15 5.",
+ arcz_missing="",
+ arcz_unpredicted="")
+
+ def test_padded_yield_in_loop(self):
+ self.check_coverage("""\
+ def gen(inp):
+ i = 2
+ for n in inp:
+ i = 4
+ yield n
+ i = 6
+ i = 7
+
+ list(gen([1,2,3]))
+ """,
+ arcz=".1 19 9. .2 23 34 45 56 63 37 7.",
+ arcz_missing="",
+ arcz_unpredicted="")
+
+ def test_bug_308(self):
+ self.check_coverage("""\
+ def run():
+ for i in range(10):
+ yield lambda: i
+
+ for f in run():
+ print(f())
+ """,
+ arcz=".1 15 56 65 5. .2 23 32 2. .3 3-3",
+ arcz_missing="",
+ arcz_unpredicted="")
+
+ self.check_coverage("""\
+ def run():
+ yield lambda: 100
+ for i in range(10):
+ yield lambda: i
+
+ for f in run():
+ print(f())
+ """,
+ arcz=".1 16 67 76 6. .2 23 34 43 3. 2-2 .4 4-4",
+ arcz_missing="",
+ arcz_unpredicted="")
+
+ self.check_coverage("""\
+ def run():
+ yield lambda: 100 # no branch miss
+
+ for f in run():
+ print(f())
+ """,
+ arcz=".1 14 45 54 4. .2 2. 2-2",
+ arcz_missing="",
+ arcz_unpredicted="")
+
+ def test_bug_324(self):
+ # This code is tricky: the list() call pulls all the values from gen(),
+ # but each of them is a generator itself that is never iterated. As a
+ # result, the generator expression on line 3 is never entered or run.
+ self.check_coverage("""\
+ def gen(inp):
+ for n in inp:
+ yield (i * 2 for i in range(n))
+
+ list(gen([1,2,3]))
+ """,
+ arcz=
+ ".1 15 5. " # The module level
+ ".2 23 32 2. " # The gen() function
+ ".3 3-3", # The generator expression
+ arcz_missing=".3 3-3",
+ arcz_unpredicted="")
+
+ def test_coroutines(self):
+ self.check_coverage("""\
+ def double_inputs():
+ while [1]: # avoid compiler differences
+ x = yield
+ x *= 2
+ yield x
+
+ gen = double_inputs()
+ next(gen)
+ print(gen.send(10))
+ next(gen)
+ print(gen.send(6))
+ """,
+ arcz=
+ ".1 17 78 89 9A AB B. "
+ ".2 23 34 45 52 2.",
+ arcz_missing="2.",
+ arcz_unpredicted="")
+ self.assertEqual(self.stdout(), "20\n12\n")
+
+
class MiscArcTest(CoverageTest):
"""Miscellaneous arc-measuring tests."""
def test_dict_literal(self):
+ if env.PYVERSION < (3, 5):
+ arcz = ".1 19 9."
+ else:
+ # Python 3.5 changed how dict literals are constructed.
+ arcz = ".1 19 9-2"
self.check_coverage("""\
d = {
'a': 2,
@@ -575,7 +691,7 @@ class MiscArcTest(CoverageTest):
}
assert d
""",
- arcz=".1 19 9.")
+ arcz=arcz)
def test_pathologically_long_code_object(self):
# https://bitbucket.org/ned/coveragepy/issue/359
@@ -647,6 +763,6 @@ class LineDataTest(CoverageTest):
self.start_import_stop(cov, "fun1")
- cov._harvest_data()
- fun1_lines = cov.data.line_data()[abs_file("fun1.py")]
- self.assertEqual(fun1_lines, [1, 2, 5])
+ data = cov.get_data()
+ fun1_lines = data.lines(abs_file("fun1.py"))
+ self.assertCountEqual(fun1_lines, [1, 2, 5])