Class: CovLoupe::CoverageCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/cov_loupe/coverage/coverage_calculator.rb

Overview

Provides coverage data transformations and calculations. All methods are pure functions that operate on SimpleCov line coverage arrays.

SimpleCov line coverage array convention:

- Integer → coverable line with hit count (0 = uncovered, >0 = covered)
- nil     → non-code line (not counted in totals)

Line numbers in output are 1-indexed (matching editor display).

Class Method Summary collapse

Class Method Details

.aggregate(file_summaries) ⇒ Hash

Aggregates coverage summaries from multiple files.

Parameters:

  • file_summaries (Array<Hash>)

    array of hashes with 'covered' and 'total' keys

Returns:

  • (Hash)

    aggregated summary with 'covered', 'uncovered', 'total', and 'percentage' keys



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cov_loupe/coverage/coverage_calculator.rb', line 63

def self.aggregate(file_summaries)
  covered = file_summaries.sum { |row| row['covered'].to_i }
  total = file_summaries.sum { |row| row['total'].to_i }
  uncovered = total - covered
  percentage = total.zero? ? nil : (covered.to_f / total * 100.0).round(2)

  {
    'covered'    => covered,
    'uncovered'  => uncovered,
    'total'      => total,
    'percentage' => percentage,
  }
end

.detailed(coverage_lines) ⇒ Array<Hash>

Generates detailed line-by-line coverage information.

Parameters:

  • coverage_lines (Array<Integer, nil>)

    SimpleCov coverage array

Returns:

  • (Array<Hash>)

    array of hashes with 'line', 'hits', and 'covered' keys



48
49
50
51
52
53
54
55
56
57
# File 'lib/cov_loupe/coverage/coverage_calculator.rb', line 48

def self.detailed(coverage_lines)
  rows = []
  coverage_lines.each_with_index do |hits, i|
    next if hits.nil?

    h = hits.to_i
    rows << { 'line' => i + 1, 'hits' => h, 'covered' => h.positive? }
  end
  rows
end

.summary(coverage_lines) ⇒ Hash

Calculates coverage summary statistics from a coverage array.

Parameters:

  • coverage_lines (Array<Integer, nil>)

    SimpleCov coverage array where each element represents a line: Integer for hit count, nil for non-code lines

Returns:

  • (Hash)

    summary with 'covered', 'total', and 'percentage' keys



18
19
20
21
22
23
24
25
26
27
# File 'lib/cov_loupe/coverage/coverage_calculator.rb', line 18

def self.summary(coverage_lines)
  total = 0
  covered = 0
  coverage_lines.compact.each do |hits|
    total += 1
    covered += 1 if hits.to_i > 0
  end
  percentage = total <= 0 ? nil : (covered.to_f / total * 100.0).round(2)
  { 'covered' => covered, 'total' => total, 'percentage' => percentage }
end

.uncovered(coverage_lines) ⇒ Array<Integer>

Identifies uncovered line numbers from a coverage array.

Parameters:

  • coverage_lines (Array<Integer, nil>)

    SimpleCov coverage array

Returns:

  • (Array<Integer>)

    array of uncovered line numbers (1-indexed)



33
34
35
36
37
38
39
40
41
42
# File 'lib/cov_loupe/coverage/coverage_calculator.rb', line 33

def self.uncovered(coverage_lines)
  out = []

  coverage_lines.each_with_index do |hits, i|
    next if hits.nil?

    out << (i + 1) if hits.to_i.zero?
  end
  out
end