Skip to content

Commit 51ad2bd

Browse files
committed
Use CoverageStatistics directly in HTML coverage_summary
The HTML formatter's `build_stats` reimplemented CoverageStatistics with different key names (`:pct` vs `percent`). Replace it by having `coverage_summary` take a source_file and read from `source_file.coverage_statistics`, and update `enabled_type_summary` to call CoverageStatistics methods directly. Source file ERB template simplifies from hand-building a per-criterion counts hash to a single argument pass-through.
1 parent afc617b commit 51ad2bd

3 files changed

Lines changed: 56 additions & 155 deletions

File tree

lib/simplecov/formatter/html_formatter/coverage_helpers.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,17 @@ def coverage_type_summary(type, label, summary, enabled:, **opts)
4747
enabled_type_summary(type, label, summary.fetch(type.to_sym), opts)
4848
end
4949

50-
def coverage_summary(stats, show_method_toggle: false)
50+
def coverage_summary(source_file, show_method_toggle: false)
51+
stats = source_file.coverage_statistics
5152
_summary = {
52-
line: build_stats(stats.fetch(:covered_lines), stats.fetch(:total_lines)),
53-
branch: build_stats(stats.fetch(:covered_branches, 0), stats.fetch(:total_branches, 0)),
54-
method: build_stats(stats.fetch(:covered_methods, 0), stats.fetch(:total_methods, 0)),
53+
line: stats[:line],
54+
branch: stats[:branch],
55+
method: stats[:method],
5556
show_method_toggle: show_method_toggle
5657
}
5758
template("coverage_summary").result(binding)
5859
end
5960

60-
def build_stats(covered, total)
61-
pct = total.positive? ? (covered * 100.0 / total) : 100.0
62-
{covered: covered, total: total, missed: total - covered, pct: pct}
63-
end
64-
6561
private
6662

6763
def totals_cell_attrs(type, css)
@@ -104,12 +100,12 @@ def append_method_attrs(pairs, source_file)
104100
end
105101

106102
def enabled_type_summary(type, label, stats, opts)
107-
css = coverage_css_class(stats.fetch(:pct))
108-
missed = stats.fetch(:missed)
103+
css = coverage_css_class(stats.percent)
104+
missed = stats.missed
109105
parts = [
110106
%(<div class="t-#{type}-summary">\n #{label}: ),
111-
%(<span class="#{css}"><b>#{Kernel.format('%.2f', stats.fetch(:pct).floor(2))}%</b></span>),
112-
%(<span class="coverage-cell__fraction"> #{stats.fetch(:covered)}/#{stats.fetch(:total)} #{opts.fetch(:suffix, 'covered')}</span>)
107+
%(<span class="#{css}"><b>#{Kernel.format('%.2f', stats.percent.floor(2))}%</b></span>),
108+
%(<span class="coverage-cell__fraction"> #{stats.covered}/#{stats.total} #{opts.fetch(:suffix, 'covered')}</span>)
113109
]
114110
parts << missed_summary_html(missed, opts.fetch(:missed_class, "red"), opts.fetch(:toggle, false)) if missed.positive?
115111
parts << "\n </div>"

lib/simplecov/formatter/html_formatter/views/source_file.erb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
<div class="source_table" id="<%= id source_file %>">
22
<div class="header">
33
<h2><%= shortened_filename source_file %></h2>
4-
<%= coverage_summary({
5-
covered_lines: source_file.covered_lines.count, total_lines: source_file.covered_lines.count + source_file.missed_lines.count,
6-
covered_branches: branch_coverage? ? source_file.covered_branches.count : 0, total_branches: branch_coverage? ? source_file.total_branches.count : 0,
7-
covered_methods: method_coverage? ? source_file.covered_methods.count : 0, total_methods: method_coverage? ? source_file.methods.count : 0
8-
}, show_method_toggle: method_coverage? && source_file.missed_methods.any?) %>
4+
<%= coverage_summary(source_file, show_method_toggle: method_coverage? && source_file.missed_methods.any?) %>
95
<%- if method_coverage? && source_file.missed_methods.any? -%>
106
<div class="t-missed-method-list" style="display: none">
117
<ul>

0 commit comments

Comments
 (0)