class SimpleCov::ResultAdapter
Responsible for adapting the format of the coverage result whether it’s default or with statistics
def self.call(*args)
def self.call(*args) new(*args).adapt end
def adapt
def adapt return unless result result.to_h do |file_name, cover_statistic| [file_name, adapt_one(file_name, cover_statistic)] end end
def adapt_one(file_name, cover_statistic)
shape. Newer entries also need their methods table massaged before
array; everything since uses the `{lines:, branches:, methods:}`
Pre-0.18 resultsets pointed each filename straight at a line-coverage
def adapt_one(file_name, cover_statistic) return {"lines" => cover_statistic} if cover_statistic.is_a?(Array) adapt_oneshot_lines_if_needed(file_name, cover_statistic) normalize_method_keys(cover_statistic) cover_statistic end
def adapt_oneshot_lines_if_needed(file_name, cover_statistic)
def adapt_oneshot_lines_if_needed(file_name, cover_statistic) return unless cover_statistic.key?(:oneshot_lines) oneshot_lines = cover_statistic.delete(:oneshot_lines) line_stub = build_line_stub(file_name, oneshot_lines) oneshot_lines.each { |covered_line| line_stub[covered_line - 1] = 1 } cover_statistic[:lines] = line_stub end
def build_line_stub(file_name, oneshot_lines)
def build_line_stub(file_name, oneshot_lines) Coverage.line_stub(file_name) rescue Errno::ENOENT, SyntaxError Array.new(oneshot_lines.max || 0, nil) end
def initialize(result)
def initialize(result) @result = result end
def normalize_method_keys(cover_statistic)
def normalize_method_keys(cover_statistic) methods = cover_statistic[:methods] return unless methods cover_statistic[:methods] = methods.each_with_object({}) do |(key, count), normalized| normalized_key = key.dup normalized_key[0] = key[0].to_s .gsub(ADDRESS_PATTERN, ADDRESS_PLACEHOLDER) .sub(SINGLETON_WRAPPER_PATTERN, '\1') # Keys may collide after normalization (anonymous classes sharing a # method name, or singleton + instance forms of a module_function method). normalized[normalized_key] = normalized.fetch(normalized_key, 0) + count end end