module Coco::CoverageStat

Public: Give statistics about an array of lines hit.

An “array of lines hit” is an array of integers, possibly nil. Such array is obtain from Coverage.result.

Each integer represent the state of a source line:

Public Class Methods

coverage_percent(hits) click to toggle source

Public: Compute the integer percentage of code coverage for a file. The file is represented by an array of hits.

hits - Array of Integer.

Returns a Integer rounded percentage of coverage.

# File lib/coco/cover/coverage_stat.rb, line 36
def self.coverage_percent(hits)
  real_percent(hits).round
end
number_of_covered_lines(hits) click to toggle source

Compute the total of covered lines in a hits array.

hits - Array of Integer.

Returns Integer.

# File lib/coco/cover/coverage_stat.rb, line 46
def self.number_of_covered_lines(hits)
  hits.select { |hit| hit > 0 }.size
end
real_percent(hits) click to toggle source

Public: Compute the decimal percentage of code coverage for a file. The file is represented by an array of hits.

hits - Array of Integer.

Returns a Float percentage of coverage.

# File lib/coco/cover/coverage_stat.rb, line 22
def self.real_percent(hits)
  hits = hits.compact
  return 0 if hits.empty?
  one_percent = 100.0 / hits.size
  number_of_covered_lines(hits) * one_percent
end