# frozen_string_literal: true
require 'repo_small_badge/image'
require 'simplecov_small_badge/configuration'
module SimpleCovSmallBadge
# Basic Badge Formater Class that creates the badges.
class Formatter - SimpleCovSmallBadge::Formatter assumes too much for instance variable '@image'
def initialize(output = nil)
@output = output || $stdout - SimpleCovSmallBadge::Formatter#initialize is controlled by argument 'output'
@config = SimpleCovSmallBadge.config
end
def format(result)
percent = result.source_files.covered_percent.round(0)
@image = RepoSmallBadge::Image.new(map_image_config(state(percent)))
badge('total', 'total', percent)
group_percent_from_result(result) do |name, title, cov_percent|
badge(name, title, cov_percent.round(0))
end
end
private
def badge(name, title, percent)
percent_txt = percent_text(percent)
@image.config_merge(map_image_config(state(percent)))
@image.badge(name, title, percent_txt)
end
def percent_text(percent)
"#{percent}#{@config.percent_sign}"
end
def state(covered_percent)
if line_coverage_minimum&.positive?
if covered_percent >= line_coverage_minimum
'good'
else
'bad'
end
else
'unknown'
end
end
def line_coverage_minimum - SimpleCovSmallBadge::Formatter#line_coverage_minimum doesn't depend on instance state (maybe move it to another class?)
minimums = SimpleCov.minimum_coverage
minimums[SimpleCov.primary_coverage]
end
def map_image_config(state) - SimpleCovSmallBadge::Formatter#map_image_config has approx 6 statements
hash = {}
@config.to_hash.map do |key, value|
key = key -
-
.to_s.sub(/^coverage_background_#{state}/, 'value_background')
.to_sym
key = key.to_s.sub(/^coverage_/, 'value_').to_sym -
-
hash[key] = value
end
hash
end
# converts the result to a hash consisting of the groupname
# array of percentage (integer in percent), strength float
# and the state [ 'good', 'bad', 'unknown' ]
# consolidated for each group.
def group_percent_from_result(result) - SimpleCovSmallBadge::Formatter#group_percent_from_result doesn't depend on instance state (maybe move it to another class?)
result.groups.each do |name, files|
covered = files.covered_percent.round(0)
yield name, name, covered, covered - SimpleCovSmallBadge::Formatter#group_percent_from_result yields 4 parameters
end
end
end
end