Class: SimpleCov::CoverageStatistics

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/coverage_statistics.rb

Overview

Holds the individual data of a coverage result.

This is uniform across coverage criteria as they all have:

  • total - how many things to cover there are (total relevant loc/branches)

  • covered - how many of the coverables are hit

  • missed - how many of the coverables are missed

  • omitted - how many lines cannot be covered (blank lines/comments); only meaningful for line coverage

  • percent - percentage as covered/missed

  • strength - average hits per/coverable (will not exist for one shot lines format)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(covered:, missed:, omitted: 0, total_strength: 0.0, percent: nil) ⇒ CoverageStatistics

Requires only covered, missed and strength to be initialized.

Other values are computed by this class.



36
37
38
39
40
41
42
43
# File 'lib/simplecov/coverage_statistics.rb', line 36

def initialize(covered:, missed:, omitted: 0, total_strength: 0.0, percent: nil)
  @covered  = covered
  @missed   = missed
  @omitted  = omitted
  @total    = covered + missed
  @percent  = percent || compute_percent(covered, missed, total)
  @strength = compute_strength(total_strength, total)
end

Instance Attribute Details

#coveredObject (readonly)

Returns the value of attribute covered.



15
16
17
# File 'lib/simplecov/coverage_statistics.rb', line 15

def covered
  @covered
end

#missedObject (readonly)

Returns the value of attribute missed.



15
16
17
# File 'lib/simplecov/coverage_statistics.rb', line 15

def missed
  @missed
end

#omittedObject (readonly)

Returns the value of attribute omitted.



15
16
17
# File 'lib/simplecov/coverage_statistics.rb', line 15

def omitted
  @omitted
end

#percentObject (readonly)

Returns the value of attribute percent.



15
16
17
# File 'lib/simplecov/coverage_statistics.rb', line 15

def percent
  @percent
end

#strengthObject (readonly)

Returns the value of attribute strength.



15
16
17
# File 'lib/simplecov/coverage_statistics.rb', line 15

def strength
  @strength
end

#totalObject (readonly)

Returns the value of attribute total.



15
16
17
# File 'lib/simplecov/coverage_statistics.rb', line 15

def total
  @total
end

Class Method Details

.from(coverage_statistics) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simplecov/coverage_statistics.rb', line 17

def self.from(coverage_statistics)
  sum_covered, sum_missed, sum_omitted, sum_total_strength =
    coverage_statistics.reduce([0, 0, 0, 0.0]) do |(covered, missed, omitted, total_strength), stats|
      [
        covered + stats.covered,
        missed + stats.missed,
        omitted + stats.omitted,
        # gotta remultiply with loc because files have different strength and loc
        # giving them a different "weight" in total
        total_strength + (stats.strength * stats.total)
      ]
    end

  new(covered: sum_covered, missed: sum_missed, omitted: sum_omitted, total_strength: sum_total_strength)
end