diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-10-22 22:04:46 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-10-22 22:04:46 -0400 |
commit | f1fa57a4b57f42694ca4d82c876e5f347dcaa381 (patch) | |
tree | 26cf4c2b8e22f2fb7ddccc78525613982e01aa48 | |
parent | e938c7058a1f109697de21baa34e9348594e6c59 (diff) | |
download | python-coveragepy-git-f1fa57a4b57f42694ca4d82c876e5f347dcaa381.tar.gz |
Filtering now computes coverage percentages properly.
-rw-r--r-- | coverage/html.py | 5 | ||||
-rw-r--r-- | coverage/htmlfiles/coverage_html.js | 23 | ||||
-rw-r--r-- | coverage/htmlfiles/index.html | 4 | ||||
-rw-r--r-- | coverage/results.py | 11 | ||||
-rw-r--r-- | tests/farm/html/gold_omit_1/index.html | 30 | ||||
-rw-r--r-- | tests/farm/html/run_a.py | 3 | ||||
-rw-r--r-- | tests/farm/html/run_b_branch.py | 3 | ||||
-rw-r--r-- | tests/test_farm.py | 3 | ||||
-rw-r--r-- | 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+<p ", ">\n<p ", html) return html + +def pair(ratio): + """Format a pair of numbers so JavaScript can read them in an attribute.""" + return "%s %s" % ratio diff --git a/coverage/htmlfiles/coverage_html.js b/coverage/htmlfiles/coverage_html.js index 6eb23fee..336b97b4 100644 --- a/coverage/htmlfiles/coverage_html.js +++ b/coverage/htmlfiles/coverage_html.js @@ -116,9 +116,17 @@ coverage.wire_up_filter = function () { break; } - var sum = 0; + var sum = 0, numer = 0, denom = 0; $.each(cells.filter(':visible'), function () { - sum += parseInt(this.innerHTML, 10); + var ratio = $(this).data("ratio"); + if (ratio) { + var splitted = ratio.split(" "); + numer += parseInt(splitted[0], 10); + denom += parseInt(splitted[1], 10); + } + else { + sum += parseInt(this.innerHTML, 10); + } }); // Get footer cell element. @@ -126,8 +134,15 @@ coverage.wire_up_filter = function () { // Set value into dynamic footer cell element. if (cells[0].innerHTML.indexOf('%') > -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 @@ <td>{{totals.n_branches}}</td> <td>{{totals.n_partial_branches}}</td> {% endif %} - <td class='right'>{{totals.pc_covered_str}}%</td> + <td class='right' data-ratio='{{totals.ratio_covered|pair}}'>{{totals.pc_covered_str}}%</td> </tr> </tfoot> <tbody> @@ -92,7 +92,7 @@ <td>{{file.nums.n_branches}}</td> <td>{{file.nums.n_partial_branches}}</td> {% endif %} - <td class='right'>{{file.nums.pc_covered_str}}%</td> + <td class='right' data-ratio='{{file.nums.ratio_covered|pair}}'>{{file.nums.pc_covered_str}}%</td> </tr> {% endfor %} </tbody> 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 @@ <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> <title>Coverage report</title> <link rel='stylesheet' href='style.css' type='text/css'> + <script type='text/javascript' src='jquery.min.js'></script> + <script type='text/javascript' src='jquery.debounce.min.js'></script> <script type='text/javascript' src='jquery.tablesorter.min.js'></script> <script type='text/javascript' src='jquery.hotkeys.js'></script> <script type='text/javascript' src='coverage_html.js'></script> @@ -12,19 +14,25 @@ jQuery(document).ready(coverage.index_ready); </script> </head> -<body id='indexfile'> +<body class='indexfile'> <div id='header'> <div class='content'> <h1>Coverage report: <span class='pc_cov'>100%</span> </h1> - <img id='keyboard_icon' src='keybd_closed.png'> + + <img id='keyboard_icon' src='keybd_closed.png' /> + + <form id="filter_container"> + <label for="filter">Filter</label> + <input id="filter" type="text" value="" /> + </form> </div> </div> <div class='help_panel'> - <img id='panel_icon' src='keybd_open.png'> + <img id='panel_icon' src='keybd_open.png' /> <p class='legend'>Hot-keys on this page</p> <div> <p class='keyhelp'> @@ -59,7 +67,7 @@ <td>0</td> <td>0</td> - <td class='right'>100%</td> + <td class='right' data-ratio='14 14'>100%</td> </tr> </tfoot> <tbody> @@ -70,7 +78,7 @@ <td>0</td> <td>0</td> - <td class='right'>100%</td> + <td class='right' data-ratio='2 2'>100%</td> </tr> <tr class='file'> @@ -79,7 +87,7 @@ <td>0</td> <td>0</td> - <td class='right'>100%</td> + <td class='right' data-ratio='2 2'>100%</td> </tr> <tr class='file'> @@ -88,7 +96,7 @@ <td>0</td> <td>0</td> - <td class='right'>100%</td> + <td class='right' data-ratio='2 2'>100%</td> </tr> <tr class='file'> @@ -97,17 +105,21 @@ <td>0</td> <td>0</td> - <td class='right'>100%</td> + <td class='right' data-ratio='8 8'>100%</td> </tr> </tbody> </table> + + <p id="no_rows"> + No items found using the specified filter. + </p> </div> <div id='footer'> <div class='content'> <p> - <a class='nav' href='http://nedbatchelder.com/code/coverage/3.5a1'>coverage.py v3.5a1</a> + <a class='nav' href='http://nedbatchelder.com/code/coverage/4.0a1'>coverage.py v4.0a1</a> </p> </div> </div> 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 href='a.html'>a</a>", - "<span class='pc_cov'>67%</span>" + "<span class='pc_cov'>67%</span>", + "<td class='right' data-ratio='2 3'>67%</td>", ) 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", "<a href='b.html'>b</a>", - "<span class='pc_cov'>70%</span>" + "<span class='pc_cov'>70%</span>", + "<td class='right' data-ratio='16 23'>70%</td>", ) 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)) |