CoveredSourceCoveredStatistics

class Statistics

Aggregates coverage statistics across files.

Nested

Definitions

def self.for(coverage)

Build statistics for a single coverage object.

Signature

parameter coverage Covered::Coverage

The coverage object to summarize.

returns Covered::Statistics

Statistics containing the given coverage.

Implementation

def self.for(coverage)
	self.new.tap do |statistics|
		statistics << coverage
	end
end

def initialize

Initialize empty coverage statistics.

Implementation

def initialize
	@total = nil
	@paths = Hash.new
end

def total

The total aggregate statistics.

Signature

returns Covered::Statistics::Aggregate

The total aggregate statistics.

Implementation

def total
	@total ||= Aggregate.for(@paths.values)
end

attr :paths

Signature

attribute Hash(String, Covered::Coverage)

Coverage statistics indexed by path.

def count

The number of unique paths with coverage data.

Signature

returns Integer

The number of unique paths.

Implementation

def count
	@paths.size
end

def executable_count

The total number of executable lines.

Signature

returns Integer

The total executable line count.

Implementation

def executable_count
	total.executable_count
end

def executed_count

The total number of executed lines.

Signature

returns Integer

The total executed line count.

Implementation

def executed_count
	total.executed_count
end

def <<(coverage)

Add coverage to these statistics.

Signature

parameter coverage Covered::Coverage

The coverage object to add.

Implementation

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)

Get coverage for the given path.

Signature

parameter path String

The source path.

returns Covered::Coverage | Nil

The merged coverage for the path.

Implementation

def [](path)
	@paths[path]
end

def as_json

A JSON-compatible representation of these statistics.

Signature

returns Hash

The total statistics and path statistics.

Implementation

def as_json
	{
		total: total.as_json,
		paths: paths.map{|path, coverage| [path, coverage.as_json]}.to_h,
	}
end

def to_json(options)

Convert these statistics to JSON.

Signature

parameter options Hash

Options forwarded to to_json.

returns String

The JSON representation.

Implementation

def to_json(options)
	as_json.to_json(options)
end

def print(output)

Print a human-readable coverage summary.

Signature

parameter output IO

The output stream.

Implementation

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 validate!(minimum = 1.0)

Validate that coverage meets the given minimum ratio.

Signature

parameter minimum Numeric

The minimum accepted coverage ratio.

raises Covered::CoverageError

If coverage is below the minimum ratio.

Implementation

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