class Covered::Statistics
Aggregates coverage statistics across files.
def self.for(coverage)
@parameter coverage [Covered::Coverage] The coverage object to summarize.
Build statistics for a single coverage object.
def self.for(coverage) self.new.tap do |statistics| statistics << coverage end end
def << coverage
Add coverage to these statistics.
def << coverage if current = @paths[coverage.path] current.merge!(coverage) @total = nil else coverage = @paths[coverage.path] = coverage.dup @total << coverage if @total end self end
def [](path)
@parameter path [String] The source path.
Get coverage for the given path.
def [](path) @paths[path] end
def as_json
A JSON-compatible representation of these statistics.
def as_json { total: total.as_json, paths: paths.map{|path, coverage| [path, coverage.as_json]}.to_h, } end
def count
The number of unique paths with coverage data.
def count @paths.size end
def executable_count
The total number of executable lines.
def executable_count total.executable_count end
def executed_count
The total number of executed lines.
def executed_count total.executed_count end
def initialize
def initialize @total = nil @paths = Hash.new end
def print(output)
Print a human-readable coverage summary.
def print(output) output.puts "#{count} files checked; #{total.executed_count}/#{total.executable_count} lines executed; #{total.percentage.to_f.round(2)}% covered." if self.complete? output.puts "🧘 #{COMPLETE.sample}" end end
def to_json(options)
@parameter options [Hash] Options forwarded to `to_json`.
Convert these statistics to JSON.
def to_json(options) as_json.to_json(options) end
def total
The total aggregate statistics.
def total @total ||= Aggregate.for(@paths.values) end
def validate!(minimum = 1.0)
@parameter minimum [Numeric] The minimum accepted coverage ratio.
Validate that coverage meets the given minimum ratio.
def validate!(minimum = 1.0) if total.ratio < minimum raise CoverageError, "Coverage of #{self.percentage.to_f.round(2)}% is less than required minimum of #{(minimum * 100.0).round(2)}%!" end end