Class: SimpleCov::CoverageStatistics

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

Overview

Uniform per-criterion coverage statistics. Counts are Integers; percent and strength are Floats. percent is 100.0 whenever nothing was missed, including the 0-of-0 case; strength is 0.0 when there is nothing to cover.

Constant Summary collapse

ZERO_STATS =

Seed for the reduce in .from: covered, missed, omitted, strength.

Returns:

  • ([Integer, Integer, Integer, Float])
[0, 0, 0, 0.0].freeze

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

percent: overrides the computed percentage (used to report 0% for tracked-but-never-loaded files).

Parameters:

  • covered: (Integer)
  • missed: (Integer)
  • omitted: (Integer) (defaults to: 0)
  • total_strength: (Integer, Float) (defaults to: 0.0)
  • percent: (Float, nil) (defaults to: nil)


39
40
41
42
43
44
45
46
# File 'lib/simplecov/coverage_statistics.rb', line 39

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

#coveredInteger (readonly)

Returns the value of attribute covered.

Returns:

  • (Integer)


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

def covered
  @covered
end

#missedInteger (readonly)

Returns the value of attribute missed.

Returns:

  • (Integer)


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

def missed
  @missed
end

#omittedInteger (readonly)

Returns the value of attribute omitted.

Returns:

  • (Integer)


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

def omitted
  @omitted
end

#percentFloat (readonly)

Returns the value of attribute percent.

Returns:

  • (Float)


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

def percent
  @percent
end

#strengthFloat (readonly)

Returns the value of attribute strength.

Returns:

  • (Float)


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

def strength
  @strength
end

#totalInteger (readonly)

Returns the value of attribute total.

Returns:

  • (Integer)


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

def total
  @total
end

Class Method Details

.from(coverage_statistics) ⇒ CoverageStatistics

Parameters:

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simplecov/coverage_statistics.rb', line 20

def self.from(coverage_statistics)
  sum_covered, sum_missed, sum_omitted, sum_total_strength =
    coverage_statistics.reduce(ZERO_STATS) 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

Instance Method Details

#compute_percent(covered, missed, total) ⇒ Float

Parameters:

  • covered (Integer)
  • missed (Integer)
  • total (Integer)

Returns:

  • (Float)


50
51
52
53
54
# File 'lib/simplecov/coverage_statistics.rb', line 50

def compute_percent(covered, missed, total)
  return 100.0 if missed.zero?

  covered * 100.0 / total
end

#compute_strength(total_strength, total) ⇒ Float

Parameters:

  • total_strength (Integer, Float)
  • total (Integer)

Returns:

  • (Float)


56
57
58
59
60
# File 'lib/simplecov/coverage_statistics.rb', line 56

def compute_strength(total_strength, total)
  return 0.0 if total.zero?

  total_strength.to_f / total
end