From f1fa57a4b57f42694ca4d82c876e5f347dcaa381 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 22 Oct 2014 22:04:46 -0400 Subject: Filtering now computes coverage percentages properly. --- coverage/html.py | 5 +++++ coverage/htmlfiles/coverage_html.js | 23 +++++++++++++++++++---- coverage/htmlfiles/index.html | 4 ++-- coverage/results.py | 11 +++++++++-- tests/farm/html/gold_omit_1/index.html | 30 +++++++++++++++++++++--------- tests/farm/html/run_a.py | 3 ++- tests/farm/html/run_b_branch.py | 3 ++- tests/test_farm.py | 3 +-- tests/test_results.py | 10 ++++++++++ 9 files changed, 71 insertions(+), 21 deletions(-) diff --git a/coverage/html.py b/coverage/html.py index 91ae2c27..0829f219 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -67,6 +67,7 @@ class HtmlReporter(Reporter): self.directory = None self.template_globals = { 'escape': escape, + 'pair': pair, 'title': self.config.html_title, '__url__': coverage.__url__, '__version__': coverage.__version__, @@ -379,3 +380,7 @@ def spaceless(html): """ html = re.sub(r">\s+

\n

-1) { - // Value of "coverage" column is expressed as a percentage - footer_cell.text(parseInt((sum / show.length), 10) + '%'); + // Percentage columns use the numerator and denominator, + // and adapt to the number of decimal places. + var match = /\.([0-9]+)/.exec(cells[0].innerHTML); + var places = 0; + if (match) { + places = match[1].length; + } + var pct = numer * 100 / denom; + footer_cell.text(pct.toFixed(places) + '%'); } else { footer_cell.text(sum); diff --git a/coverage/htmlfiles/index.html b/coverage/htmlfiles/index.html index 4b572711..bea715e1 100644 --- a/coverage/htmlfiles/index.html +++ b/coverage/htmlfiles/index.html @@ -78,7 +78,7 @@ {{totals.n_branches}} {{totals.n_partial_branches}} {% endif %} - {{totals.pc_covered_str}}% + {{totals.pc_covered_str}}% @@ -92,7 +92,7 @@ {{file.nums.n_branches}} {{file.nums.n_partial_branches}} {% endif %} - {{file.nums.pc_covered_str}}% + {{file.nums.pc_covered_str}}% {% endfor %} diff --git a/coverage/results.py b/coverage/results.py index 6c41a944..5eff0f3e 100644 --- a/coverage/results.py +++ b/coverage/results.py @@ -204,8 +204,8 @@ class Numbers(object): def pc_covered(self): """Returns a single percentage value for coverage.""" if self.n_statements > 0: - pc_cov = (100.0 * (self.n_executed + self.n_executed_branches) / - (self.n_statements + self.n_branches)) + numerator, denominator = self.ratio_covered + pc_cov = (100.0 * numerator) / denominator else: pc_cov = 100.0 return pc_cov @@ -236,6 +236,13 @@ class Numbers(object): width += 1 + cls._precision return width + @property + def ratio_covered(self): + """Return a numerator and denominator for the coverage ratio.""" + numerator = self.n_executed + self.n_executed_branches + denominator = self.n_statements + self.n_branches + return numerator, denominator + def __add__(self, other): nums = Numbers() nums.n_files = self.n_files + other.n_files diff --git a/tests/farm/html/gold_omit_1/index.html b/tests/farm/html/gold_omit_1/index.html index b7ee3594..34fd8fcb 100644 --- a/tests/farm/html/gold_omit_1/index.html +++ b/tests/farm/html/gold_omit_1/index.html @@ -4,7 +4,9 @@ Coverage report + + @@ -12,19 +14,25 @@ jQuery(document).ready(coverage.index_ready); - +

- +

Hot-keys on this page

@@ -59,7 +67,7 @@ 0 0 - 100% + 100% @@ -70,7 +78,7 @@ 0 0 - 100% + 100% @@ -79,7 +87,7 @@ 0 0 - 100% + 100% @@ -88,7 +96,7 @@ 0 0 - 100% + 100% @@ -97,17 +105,21 @@ 0 0 - 100% + 100% + +

+ No items found using the specified filter. +

diff --git a/tests/farm/html/run_a.py b/tests/farm/html/run_a.py index 59cc1703..3fabd6f5 100644 --- a/tests/farm/html/run_a.py +++ b/tests/farm/html/run_a.py @@ -19,7 +19,8 @@ contains("html_a/a.html", ) contains("html_a/index.html", "a", - "67%" + "67%", + "67%", ) clean("html_a") diff --git a/tests/farm/html/run_b_branch.py b/tests/farm/html/run_b_branch.py index d0955a2a..0e584a6d 100644 --- a/tests/farm/html/run_b_branch.py +++ b/tests/farm/html/run_b_branch.py @@ -22,7 +22,8 @@ contains("html_b_branch/b.html", ) contains("html_b_branch/index.html", "b", - "70%" + "70%", + "70%", ) clean("html_b_branch") diff --git a/tests/test_farm.py b/tests/test_farm.py index 214681ef..18c6b4a7 100644 --- a/tests/test_farm.py +++ b/tests/test_farm.py @@ -4,7 +4,6 @@ import difflib, filecmp, fnmatch, glob, os, re, shutil, sys from nose.plugins.skip import SkipTest from tests.backtest import run_command, execfile # pylint: disable=W0622 -from tests.coveragetest import CoverageTest from coverage.control import _TEST_NAME_FILE @@ -367,7 +366,7 @@ def main(): # pragma: not covered Commands: run testcase ... - Run specific test case(s) - out testcase ... - Run test cases, but don't clean up, to see the output. + out testcase ... - Run test cases, but don't clean up, leaving output. clean - Clean all the output for all tests. """ diff --git a/tests/test_results.py b/tests/test_results.py index 92c174b6..4ce048b6 100644 --- a/tests/test_results.py +++ b/tests/test_results.py @@ -58,3 +58,13 @@ class NumbersTest(CoverageTest): self.assertEqual(n9999.pc_covered_str, "0.1") self.assertEqual(n10000.pc_covered_str, "0.0") Numbers.set_precision(0) + + def test_covered_ratio(self): + n = Numbers(n_files=1, n_statements=200, n_missing=47) + self.assertEqual(n.ratio_covered, (153, 200)) + + n = Numbers( + n_files=1, n_statements=200, n_missing=47, + n_branches=10, n_missing_branches=3, n_partial_branches=1000, + ) + self.assertEqual(n.ratio_covered, (160, 210)) -- cgit v1.2.1