Sha256: 495decc3856fcc9f2d0087568af906490260f82d73efaaf831e049dc3b5c5152

Contents?: true

Size: 1.88 KB

Versions: 19

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module SimpleCov
  # 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
  # * percent - percentage as covered/missed
  # * strength - average hits per/coverable (will not exist for one shot lines format)
  class CoverageStatistics
    attr_reader :total, :covered, :missed, :strength, :percent

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

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

    # Requires only covered, missed and strength to be initialized.
    #
    # Other values are computed by this class.
    def initialize(covered:, missed:, total_strength: 0.0)
      @covered  = covered
      @missed   = missed
      @total    = covered + missed
      @percent  = compute_percent(covered, missed, total)
      @strength = compute_strength(total_strength, total)
    end

  private

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

      covered * 100.0 / total
    end

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

      total_strength.to_f / total
    end
  end
end

Version data entries

19 entries across 18 versions & 8 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/simplecov-0.22.0/lib/simplecov/coverage_statistics.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/simplecov-0.22.0/lib/simplecov/coverage_statistics.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/simplecov-0.22.0/lib/simplecov/coverage_statistics.rb
strongmind-simplecov-0.23.0 lib/simplecov/coverage_statistics.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/simplecov-0.22.0/lib/simplecov/coverage_statistics.rb
simplecov-0.22.0 lib/simplecov/coverage_statistics.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/simplecov-0.21.2/lib/simplecov/coverage_statistics.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/simplecov-0.21.2/lib/simplecov/coverage_statistics.rb
simplecov-0.21.2 lib/simplecov/coverage_statistics.rb
simplecov-0.21.1 lib/simplecov/coverage_statistics.rb
simplecov-0.21.0 lib/simplecov/coverage_statistics.rb
simplecov-0.20.0 lib/simplecov/coverage_statistics.rb
simplecov-0.19.1 lib/simplecov/coverage_statistics.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/simplecov-0.19.0/lib/simplecov/coverage_statistics.rb
simplecov-0.19.0 lib/simplecov/coverage_statistics.rb
simplecov-0.18.5 lib/simplecov/coverage_statistics.rb
simplecov-0.18.4 lib/simplecov/coverage_statistics.rb
simplecov-0.18.3 lib/simplecov/coverage_statistics.rb
simplecov-0.18.2 lib/simplecov/coverage_statistics.rb