lib/coco/cover/coverage_result.rb in coco-0.13.0 vs lib/coco/cover/coverage_result.rb in coco-0.14.0
- old
+ new
@@ -1,57 +1,113 @@
module Coco
# Compute results of interest from the big results information (from
# Coverage.result)
+ #
class CoverageResult
# Returns a Hash coverage for all the sources that live in the root
# project folder.
- attr_reader :all_from_domain
+ #
+ attr_reader :coverable_files
# Returns a Hash coverage for sources that are not sufficiently
# covered. More technically, the sources that live in the root
# project folder and for which the coverage percentage is under the
# threshold.
- attr_reader :covered_from_domain
+ #
+ attr_reader :not_covered_enough
# Public: Initialize a CoverageResult.
#
# config - Hash
- # raw_results - Hash results obtained from Coverage.result.
+ # raw_results - The Hash from Coverage.result. Keys are filenames
+ # and values are an Array representing each lines of
+ # the file :
+ # + nil : Unreacheable (comments, etc).
+ # + 0 : Not hit.
+ # + 1 or more : Number of hits.
+ #
def initialize(config, raw_results)
- @exclude_files = config[:excludes]
+ raise ArgumentError if config[:threshold] < 0
+
+ @exclude_files = config[:exclude]
@threshold = config[:threshold]
- raise ArgumentError if @threshold < 0
@result = raw_results
+
exclude_external_sources
- exclude_files_user_dont_want
- if config[:exclude_above_threshold]
- @covered_from_domain = exclude_sources_above_threshold
- else
- @covered_from_domain = @all_from_domain
- end
+ exclude_files_user_dont_want if @exclude_files
+
+ @not_covered_enough = if config[:exclude_above_threshold]
+ exclude_sources_above_threshold
+ else
+ @coverable_files
+ end
end
+ # Public: Count the number of "coverable" files.
+ #
+ # Returns the Fixnum number of files.
+ #
+ def count
+ coverable_files.size
+ end
+
+ # Public: Count the number of uncovered files, that is, files with a
+ # coverage rate of 0%.
+ #
+ # Returns the Fixnum number of uncovered files.
+ #
+ def uncovered_count
+ not_covered_enough.select do |_, hits|
+ CoverageStat.coverage_percent(hits).zero?
+ end.size
+ end
+
+ # Public: Computes the average coverage rate.
+ # The formula is simple:
+ #
+ # N = number of files
+ # f = a file
+ # average = sum(f_i%) / N
+ #
+ # In words: Take the sum of the coverage's percentage of all files
+ # and divide this sum by the number of files.
+ #
+ # Returns the Fixnum rounded average rate of coverage.
+ #
+ def average
+ files_present? ? (sum / count).round : 0
+ end
+
private
def exclude_external_sources
here = Dir.pwd
- @all_from_domain = @result.select {|key, value| key.start_with?(here) }
+ @coverable_files = @result.select { |key, _| key.start_with?(here) }
end
def exclude_files_user_dont_want
- return if @exclude_files.nil?
@exclude_files.each do |filename|
- @all_from_domain.delete(File.expand_path(filename))
+ @coverable_files.delete(File.expand_path(filename))
end
end
def exclude_sources_above_threshold
- @all_from_domain.select do |key, value|
+ @coverable_files.select do |_, value|
CoverageStat.coverage_percent(value) < @threshold
end
end
- end
+ # Returns the Float sum of all files' percentage.
+ #
+ def sum
+ @coverable_files.values.map do |hits|
+ CoverageStat.real_percent(hits)
+ end.reduce(&:+)
+ end
+ def files_present?
+ count > 0
+ end
+ end
end