class Covered::Statistics

Aggregates coverage statistics across files.

def self.for(coverage)

@returns [Covered::Statistics] Statistics containing the given 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

@parameter coverage [Covered::Coverage] The coverage object to add.
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)

@returns [Covered::Coverage | Nil] The merged coverage for the path.
@parameter path [String] The source path.
Get coverage for the given path.
def [](path)
	@paths[path]
end

def as_json

@returns [Hash] The total statistics and path statistics.
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

@returns [Integer] The number of unique paths.
The number of unique paths with coverage data.
def count
	@paths.size
end

def executable_count

@returns [Integer] The total executable line count.
The total number of executable lines.
def executable_count
	total.executable_count
end

def executed_count

@returns [Integer] The total executed line count.
The total number of executed lines.
def executed_count
	total.executed_count
end

def initialize

Initialize empty coverage statistics.
def initialize
	@total = nil
	@paths = Hash.new
end

def print(output)

@parameter output [IO] The output stream.
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)

@returns [String] The JSON representation.
@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

@returns [Covered::Statistics::Aggregate] The total aggregate statistics.
The total aggregate statistics.
def total
	@total ||= Aggregate.for(@paths.values)
end

def validate!(minimum = 1.0)

@raises [Covered::CoverageError] If coverage is below the minimum ratio.
@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