Class: Covered::Statistics

Inherits:
Object
  • Object
show all
Includes:
Ratio
Defined in:
lib/covered/statistics.rb

Overview

Aggregates coverage statistics across files.

Defined Under Namespace

Classes: Aggregate

Constant Summary collapse

COMPLETE =
[
	"Enter the code dojo: 100% coverage attained, bugs defeated with one swift strike.",
	"Nirvana reached: 100% code coverage, where bugs meditate and vanish like a passing cloud.",
	"With 100% coverage, your code has unlocked the path to enlightenment – bugs have no place to hide.",
	"In the realm of code serenity, 100% coverage is your ticket to coding enlightenment.",
	"100% coverage, where code and bugs coexist in perfect harmony, like Yin and Yang.",
	"Achieving the Zen of code coverage, your code is a peaceful garden where bugs find no shelter.",
	"Congratulations on coding enlightenment! 100% coverage means your code is one with the universe.",
	"With 100% coverage, your code is a tranquil pond where bugs cause no ripples.",
	"At the peak of code mastery: 100% coverage, where bugs bow down before the wisdom of your code.",
	"100% code coverage: Zen achieved! Bugs in harmony, code at peace.",
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ratio

#complete?, #percentage, #ratio

Constructor Details

#initializeStatistics

Initialize empty coverage statistics.



92
93
94
95
# File 'lib/covered/statistics.rb', line 92

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

Instance Attribute Details

#Coverage statistics indexed by path.(statisticsindexedbypath.) ⇒ Object (readonly)



104
# File 'lib/covered/statistics.rb', line 104

attr :paths

#pathsObject (readonly)

Returns the value of attribute paths.



104
105
106
# File 'lib/covered/statistics.rb', line 104

def paths
  @paths
end

Class Method Details

.for(coverage) ⇒ Object

Build statistics for a single coverage object.



21
22
23
24
25
# File 'lib/covered/statistics.rb', line 21

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

Instance Method Details

#<<(coverage) ⇒ Object

Add coverage to these statistics.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/covered/statistics.rb', line 126

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

#[](path) ⇒ Object

Get coverage for the given path.



143
144
145
# File 'lib/covered/statistics.rb', line 143

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

#as_jsonObject

A JSON-compatible representation of these statistics.



149
150
151
152
153
154
# File 'lib/covered/statistics.rb', line 149

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

#countObject

The number of unique paths with coverage data.



108
109
110
# File 'lib/covered/statistics.rb', line 108

def count
	@paths.size
end

#executable_countObject

The total number of executable lines.



114
115
116
# File 'lib/covered/statistics.rb', line 114

def executable_count
	total.executable_count
end

#executed_countObject

The total number of executed lines.



120
121
122
# File 'lib/covered/statistics.rb', line 120

def executed_count
	total.executed_count
end

Print a human-readable coverage summary.



178
179
180
181
182
183
184
# File 'lib/covered/statistics.rb', line 178

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

#to_json(options) ⇒ Object

Convert these statistics to JSON.



159
160
161
# File 'lib/covered/statistics.rb', line 159

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

#totalObject

The total aggregate statistics.



99
100
101
# File 'lib/covered/statistics.rb', line 99

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

#validate!(minimum = 1.0) ⇒ Object

Validate that coverage meets the given minimum ratio.



189
190
191
192
193
# File 'lib/covered/statistics.rb', line 189

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