Skip to content

Commit 68dbf2b

Browse files
committed
Always round coverage percentage down, never up
When the coverage percentage is rounded up, people may end up misconfiguring SimpleCov with a number that 0.01 too high. They may even think they have a 100% converage, when in fact the coverage is only marginally higher than 99.995%. This commit changes all uses of `#round` for `#floor`, thus always rounding _down_ and never _up_.
1 parent 48505a4 commit 68dbf2b

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

lib/simplecov-html.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ def line_status?(source_file, line)
5555

5656
def output_message(result)
5757
output = "Coverage report generated for #{result.command_name} to #{output_path}."
58-
output += "\nLine Coverage: #{result.covered_percent.round(2)}% (#{result.covered_lines} / #{result.total_lines})"
59-
output += "\nBranch Coverage: #{result.coverage_statistics[:branch].percent.round(2)}% (#{result.covered_branches} / #{result.total_branches})" if branchable_result?
58+
output += "\nLine Coverage: #{result.covered_percent.floor(2)}% (#{result.covered_lines} / #{result.total_lines})"
59+
60+
output += "\nBranch Coverage: #{result.coverage_statistics[:branch].percent.floor(2)}% (#{result.covered_branches} / #{result.total_branches})" if branchable_result?
6061
output
6162
end
6263

test/test_simple_cov-html.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,26 @@ def test_output # rubocop:disable Metrics
170170
"uneven_nocovs.rb" => coverage_for_uneven_nocov_rb,
171171
})
172172

173-
# All Files ( 74.12% covered at 0.84 hits/line )
173+
# All Files ( 74.11% covered at 0.83 hits/line )
174174
header_line_coverage = html_doc.at_css("div#AllFiles span.covered_percent span").content.strip
175175

176-
assert_equal("74.12%", header_line_coverage)
176+
assert_equal("74.11%", header_line_coverage)
177177

178-
# 85 relevant lines, 63 lines covered and 22 lines missed. ( 74.12% )
178+
# 85 relevant lines, 63 lines covered and 22 lines missed. ( 74.11% )
179179
subheader_line_coverage = html_doc.at_css("div#AllFiles div.t-line-summary span:last-child").content.strip
180180

181-
assert_equal("74.12%", subheader_line_coverage)
181+
assert_equal("74.11%", subheader_line_coverage)
182182

183-
# 58 total branches, 28 branches covered and 30 branches missed. ( 48.28% )
183+
# 58 total branches, 28 branches covered and 30 branches missed. ( 48.27% )
184184
subheader_branch_coverage = html_doc.at_css("div#AllFiles div.t-branch-summary span:last-child").content.strip
185185

186-
assert_equal("48.28%", subheader_branch_coverage)
186+
assert_equal("48.27%", subheader_branch_coverage)
187187

188188
sorted_line_coverages = [
189189
"57.14%",
190-
"64.29%",
191-
"66.67%",
192-
"66.67%",
190+
"64.28%",
191+
"66.66%",
192+
"66.66%",
193193
"80.00%",
194194
"85.71%",
195195
"85.71%",
@@ -225,7 +225,7 @@ def test_output # rubocop:disable Metrics
225225

226226
assert_equal sorted_branch_coverages, all_files_table_branch_coverages.sort_by(&:to_f)
227227

228-
# 66.67 lines covered
228+
# 66.66 lines covered
229229
single_file_page_line_coverages = html_doc.css("div.source_files div.header h4:nth-child(2) span").map { |m| m.content.strip }
230230

231231
assert_equal(sorted_line_coverages, single_file_page_line_coverages.sort_by(&:to_f))

views/covered_percent.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<span class="<%= coverage_css_class(percent) %>">
2-
<%= percent.round(2) %>%
2+
<%= sprintf("%.2f", percent.floor(2)) %>%
33
</span>

views/file_list.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
covered at
88
<span class="covered_strength">
99
<span class="<%= strength_css_class(source_files.covered_strength) %>">
10-
<%= source_files.covered_strength.round(2) %>
10+
<%= source_files.covered_strength %>
1111
</span>
1212
</span> hits/line
1313
)
@@ -58,14 +58,14 @@
5858
<% source_files.each do |source_file| %>
5959
<tr class="t-file">
6060
<td class="strong t-file__name"><%= link_to_source_file(source_file) %></td>
61-
<td class="<%= coverage_css_class(source_file.covered_percent) %> strong cell--number t-file__coverage"><%= sprintf("%.2f", source_file.covered_percent.round(2)) %>%</td>
61+
<td class="strong cell--number t-file__coverage"><%= covered_percent(source_file.covered_percent) %></td>
6262
<td class="cell--number"><%= source_file.lines.count %></td>
6363
<td class="cell--number"><%= source_file.covered_lines.count + source_file.missed_lines.count %></td>
6464
<td class="cell--number"><%= source_file.covered_lines.count %></td>
6565
<td class="cell--number"><%= source_file.missed_lines.count %></td>
6666
<td class="cell--number"><%= sprintf("%.2f", source_file.covered_strength.round(2)) %></td>
6767
<% if branchable_result? %>
68-
<td class="<%= coverage_css_class(source_file.branches_coverage_percent) %> strong cell--number t-file__branch-coverage"><%= sprintf("%.2f", source_file.branches_coverage_percent.round(2)) %>%</td>
68+
<td class="strong cell--number t-file__branch-coverage"><%= covered_percent(source_file.branches_coverage_percent) %></td>
6969
<td class="cell--number"><%= source_file.total_branches.count %></td>
7070
<td class="cell--number"><%= source_file.covered_branches.count %></td>
7171
<td class="cell--number"><%= source_file.missed_branches.count %></td>

0 commit comments

Comments
 (0)