|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SimpleCov |
| 4 | + module Formatter |
| 5 | + class HTMLFormatter |
| 6 | + # Helpers for rendering coverage bars, cells, and summaries in ERB templates. |
| 7 | + module CoverageHelpers |
| 8 | + def coverage_bar(pct) |
| 9 | + css = coverage_css_class(pct) |
| 10 | + width = Kernel.format("%.1f", pct.floor(1)) |
| 11 | + fill = %(<div class="coverage-bar__fill coverage-bar__fill--#{css}" style="width: #{width}%"></div>) |
| 12 | + %(<div class="bar-sizer"><div class="coverage-bar">#{fill}</div></div>) |
| 13 | + end |
| 14 | + |
| 15 | + def coverage_cells(pct, covered, total, type:, totals: false) |
| 16 | + cov_cls, num_cls, den_cls, order = coverage_cell_attrs(pct, type, totals) |
| 17 | + pct_str = Kernel.format("%.2f", pct.floor(2)) |
| 18 | + bar_and_pct = %(<div class="coverage-cell">#{coverage_bar(pct)}<span class="coverage-pct">#{pct_str}%</span></div>) |
| 19 | + %(<td class="#{cov_cls}"#{order}>#{bar_and_pct}</td>) + |
| 20 | + %(<td class="#{num_cls}">#{fmt(covered)}/</td>) + |
| 21 | + %(<td class="#{den_cls}">#{fmt(total)}</td>) |
| 22 | + end |
| 23 | + |
| 24 | + def coverage_header_cells(label, type, covered_label, total_label) |
| 25 | + <<~HTML |
| 26 | + <th class="cell--coverage"> |
| 27 | + <div class="th-with-filter"> |
| 28 | + <span class="th-label">#{label}</span> |
| 29 | + <div class="col-filter__coverage"> |
| 30 | + <select class="col-filter__op" data-type="#{type}"><option value="lt"><</option><option value="lte" selected>≤</option><option value="eq">=</option><option value="gte">≥</option><option value="gt">></option></select> |
| 31 | + <span class="col-filter__pct-wrap"><input type="number" class="col-filter__value" min="0" max="100" data-type="#{type}" value="100" step="any"></span> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | + </th> |
| 35 | + <th class="cell--numerator">#{covered_label}</th> |
| 36 | + <th class="cell--denominator">#{total_label}</th> |
| 37 | + HTML |
| 38 | + end |
| 39 | + |
| 40 | + def file_data_attrs(source_file) |
| 41 | + build_data_attr_pairs(source_file).map { |k, v| %(data-#{k}="#{v}") }.join(" ") |
| 42 | + end |
| 43 | + |
| 44 | + def coverage_type_summary(type, label, summary, enabled:, **opts) |
| 45 | + return disabled_summary(type, label) unless enabled |
| 46 | + |
| 47 | + enabled_type_summary(type, label, summary.fetch(type.to_sym), opts) |
| 48 | + end |
| 49 | + |
| 50 | + def coverage_summary(stats, show_method_toggle: false) |
| 51 | + _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)), |
| 55 | + show_method_toggle: show_method_toggle, |
| 56 | + } |
| 57 | + template("coverage_summary").result(binding) |
| 58 | + end |
| 59 | + |
| 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 | + |
| 65 | + private |
| 66 | + |
| 67 | + def totals_cell_attrs(type, css) |
| 68 | + ["cell--coverage strong t-totals__#{type}-pct #{css}", |
| 69 | + "cell--numerator strong t-totals__#{type}-num", |
| 70 | + "cell--denominator strong t-totals__#{type}-den", ""] |
| 71 | + end |
| 72 | + |
| 73 | + def regular_cell_attrs(pct, type, css) |
| 74 | + ["cell--coverage cell--#{type}-pct #{css}", |
| 75 | + "cell--numerator", "cell--denominator", |
| 76 | + %( data-order="#{Kernel.format('%.2f', pct)}")] |
| 77 | + end |
| 78 | + |
| 79 | + def coverage_cell_attrs(pct, type, totals) |
| 80 | + css = coverage_css_class(pct) |
| 81 | + totals ? totals_cell_attrs(type, css) : regular_cell_attrs(pct, type, css) |
| 82 | + end |
| 83 | + |
| 84 | + def build_data_attr_pairs(source_file) |
| 85 | + covered = source_file.covered_lines.count |
| 86 | + pairs = {"covered-lines" => covered, "relevant-lines" => covered + source_file.missed_lines.count} |
| 87 | + append_branch_attrs(pairs, source_file) |
| 88 | + append_method_attrs(pairs, source_file) |
| 89 | + pairs |
| 90 | + end |
| 91 | + |
| 92 | + def append_branch_attrs(pairs, source_file) |
| 93 | + return unless branch_coverage? |
| 94 | + |
| 95 | + pairs["covered-branches"] = source_file.covered_branches.count |
| 96 | + pairs["total-branches"] = source_file.total_branches.count |
| 97 | + end |
| 98 | + |
| 99 | + def append_method_attrs(pairs, source_file) |
| 100 | + return unless method_coverage? |
| 101 | + |
| 102 | + pairs["covered-methods"] = source_file.covered_methods.count |
| 103 | + pairs["total-methods"] = source_file.methods.count |
| 104 | + end |
| 105 | + |
| 106 | + def enabled_type_summary(type, label, stats, opts) |
| 107 | + css = coverage_css_class(stats.fetch(:pct)) |
| 108 | + missed = stats.fetch(:missed) |
| 109 | + parts = [ |
| 110 | + %(<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>), |
| 113 | + ] |
| 114 | + parts << missed_summary_html(missed, opts.fetch(:missed_class, "red"), opts.fetch(:toggle, false)) if missed.positive? |
| 115 | + parts << "\n </div>" |
| 116 | + parts.join |
| 117 | + end |
| 118 | + |
| 119 | + def disabled_summary(type, label) |
| 120 | + %(<div class="t-#{type}-summary">\n #{label}: <span class="coverage-disabled">disabled</span>\n </div>) |
| 121 | + end |
| 122 | + |
| 123 | + def missed_summary_html(count, missed_class, toggle) |
| 124 | + missed = if toggle |
| 125 | + %(<a href="#" class="t-missed-method-toggle"><b>#{count}</b> missed</a>) |
| 126 | + else |
| 127 | + %(<span class="#{missed_class}"><b>#{count}</b> missed</span>) |
| 128 | + end |
| 129 | + %(<span class="coverage-cell__fraction">,</span>\n #{missed}) |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments