Module: Moult::Formatters::CoverageTable

Defined in:
lib/moult/formatters/coverage_table.rb

Overview

Human-readable runtime coverage map. Renders from the same CoverageReport as the JSON formatter so the two cannot disagree; sorting already happened in CoverageReport.build.

Constant Summary collapse

HEADERS =
["RUNTIME", "KIND", "SYMBOL", "LOCATION"].freeze
GUTTER =
"  "

Class Method Summary collapse

Class Method Details

.column_widths(rows) ⇒ Object



49
50
51
52
53
# File 'lib/moult/formatters/coverage_table.rb', line 49

def column_widths(rows)
  HEADERS.each_index.map do |col|
    ([HEADERS[col]] + rows.map { |r| r[col] }).map(&:length).max
  end
end

.format_row(cells, widths) ⇒ Object



55
56
57
# File 'lib/moult/formatters/coverage_table.rb', line 55

def format_row(cells, widths)
  cells.each_with_index.map { |cell, col| cell.ljust(widths[col]) }.join(GUTTER).rstrip
end

.heading(summary) ⇒ Object



24
25
26
# File 'lib/moult/formatters/coverage_table.rb', line 24

def heading(summary)
  "Runtime coverage map: #{summary[:hot]} hot, #{summary[:cold]} cold, #{summary[:untracked]} untracked"
end

.location(entry) ⇒ Object



37
38
39
40
41
42
# File 'lib/moult/formatters/coverage_table.rb', line 37

def location(entry)
  # symbol_id is "<path>:<start_line>:<fqname>"; the path:line prefix is the
  # most useful location and avoids re-deriving it.
  path, line, _ = entry.symbol_id.split(":", 3)
  "#{path}:#{line}"
end

.render(report) ⇒ String

Parameters:

Returns:

  • (String)


16
17
18
19
20
21
22
# File 'lib/moult/formatters/coverage_table.rb', line 16

def render(report)
  entries = report.entries
  return "No symbols found." if entries.empty?

  rows = entries.map { |e| row(e, report.root) }
  [heading(report.summary), "", table(rows)].join("\n")
end

.row(entry, _root) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/moult/formatters/coverage_table.rb', line 28

def row(entry, _root)
  [
    entry.runtime.to_s,
    entry.kind.to_s,
    entry.name.to_s,
    location(entry)
  ]
end

.table(rows) ⇒ Object



44
45
46
47
# File 'lib/moult/formatters/coverage_table.rb', line 44

def table(rows)
  widths = column_widths(rows)
  ([HEADERS] + rows).map { |cells| format_row(cells, widths) }.join("\n")
end