diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-16 17:36:36 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-16 17:36:36 -0500 |
commit | c82d2f8e207d814dd2b0c532e7d5d514ce578f53 (patch) | |
tree | e11f945c0fb817a190d5cad2e8355499aba6a0f9 /tests/test_arcs.py | |
parent | 050d768cd646a87a5c1578791546a71501bf518f (diff) | |
download | python-coveragepy-git-c82d2f8e207d814dd2b0c532e7d5d514ce578f53.tar.gz |
Properly handle if-statements optimized away. #522
Diffstat (limited to 'tests/test_arcs.py')
-rw-r--r-- | tests/test_arcs.py | 111 |
1 files changed, 96 insertions, 15 deletions
diff --git a/tests/test_arcs.py b/tests/test_arcs.py index 505cab73..1388d506 100644 --- a/tests/test_arcs.py +++ b/tests/test_arcs.py @@ -1032,6 +1032,102 @@ class YieldTest(CoverageTest): ) +class OptimizedIfTest(CoverageTest): + """Tests of if statements being optimized away.""" + + def test_optimized_away_if_0(self): + self.check_coverage("""\ + a = 1 + if len([2]): + c = 3 + if 0: # this line isn't in the compiled code. + if len([5]): + d = 6 + else: + e = 8 + f = 9 + """, + lines=[1, 2, 3, 8, 9], + arcz=".1 12 23 28 38 89 9.", + arcz_missing="28", + ) + + def test_optimized_away_if_1(self): + self.check_coverage("""\ + a = 1 + if len([2]): + c = 3 + if 1: # this line isn't in the compiled code, + if len([5]): # but these are. + d = 6 + else: + e = 8 + f = 9 + """, + lines=[1, 2, 3, 5, 6, 9], + arcz=".1 12 23 25 35 56 69 59 9.", + arcz_missing="25 59", + ) + self.check_coverage("""\ + a = 1 + if 1: + b = 3 + c = 4 + d = 5 + """, + lines=[1, 3, 4, 5], + arcz=".1 13 34 45 5.", + ) + + def test_optimized_nested(self): + self.check_coverage("""\ + a = 1 + if 0: + if 0: + b = 4 + else: + c = 6 + else: + if 0: + d = 9 + else: + if 0: e = 11 + f = 12 + if 0: g = 13 + h = 14 + """, + lines=[1, 12, 14], + arcz=".1 1C CE E.", + ) + + def test_constant_if(self): + if env.PYPY: + self.skipTest("PyPy doesn't optimize away 'if __debug__:'") + # CPython optimizes away "if __debug__:" + self.check_coverage("""\ + for value in [True, False]: + if value: + if __debug__: + x = 4 + else: + x = 6 + """, + arcz=".1 12 24 41 26 61 1.", + ) + # No Python optimizes away "if not __debug__:" + self.check_coverage("""\ + for value in [True, False]: + if value: + if not __debug__: + x = 4 + else: + x = 6 + """, + arcz=".1 12 23 31 34 41 26 61 1.", + arcz_missing="34 41", + ) + + class MiscArcTest(CoverageTest): """Miscellaneous arc-measuring tests.""" @@ -1127,21 +1223,6 @@ class MiscArcTest(CoverageTest): arcs_missing=[], arcs_unpredicted=[], ) - def test_optimized_away_lines(self): - self.check_coverage("""\ - a = 1 - if len([2]): - c = 3 - if 0: # this line isn't in the compiled code. - if len([5]): - d = 6 - e = 7 - """, - lines=[1, 2, 3, 7], - arcz=".1 12 23 27 37 7.", - arcz_missing="27", - ) - def test_partial_generators(self): # https://bitbucket.org/ned/coveragepy/issues/475/generator-expression-is-marked-as-not # Line 2 is executed completely. |