123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::Formatter::JSONFormatter::ResultHashFormatter

Relationships & Source Files
Inherits: Object
Defined in: lib/simplecov/formatter/json_formatter/result_hash_formatter.rb

Overview

Builds the hash that ::SimpleCov::Formatter::JSONFormatter serializes to coverage.json: meta, per-file coverage data, group totals, and aggregate stats.

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.new(result) ⇒ ResultHashFormatter

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 11

def initialize(result)
  @result = result
end

Instance Method Details

#ensure_utf8(str) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 120

def ensure_utf8(str)
  str.encode("UTF-8", invalid: :replace, undef: :replace)
end

#format

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 15

def format
  format_total
  format_files
  format_groups
  format_errors

  formatted_result
end

#format_branch(branch) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 162

def format_branch(branch)
  {
    type: branch.type,
    start_line: branch.start_line,
    end_line: branch.end_line,
    coverage: format_line(branch),
    inline: branch.inline?,
    report_line: branch.report_line
  }
end

#format_branch_coverage(source_file) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 136

def format_branch_coverage(source_file)
  {
    branches: source_file.branches.map { |branch| format_branch(branch) },
    branches_covered_percent: source_file.branches_coverage_percent,
    covered_branches: source_file.covered_branches.count,
    missed_branches: source_file.missed_branches.count,
    total_branches: source_file.total_branches.count
  }
end

#format_coverage_statistics(statistics) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 182

def format_coverage_statistics(statistics)
  result = {lines: format_line_statistic(statistics[:line])}
  if SimpleCov.branch_coverage? && statistics[:branch]
    result[:branches] = format_single_statistic(statistics[:branch])
  end
  if SimpleCov.method_coverage? && statistics[:method]
    result[:methods] = format_single_statistic(statistics[:method])
  end
  result
end

#format_errors (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 45

def format_errors
  format_minimum_coverage_errors
  format_minimum_coverage_by_file_errors
  format_minimum_coverage_by_group_errors
  format_maximum_coverage_drop_errors
end

#format_files (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 30

def format_files
  @result.files.each do |source_file|
    formatted_result[:coverage][source_file.project_filename] =
      format_source_file(source_file)
  end
end

#format_groups (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 37

def format_groups
  @result.groups.each do |name, file_list|
    group_data = format_coverage_statistics(file_list.coverage_statistics)
    group_data[:files] = file_list.map(&:project_filename)
    formatted_result[:groups][name] = group_data
  end
end

#format_line(line) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 156

def format_line(line)
  return line.coverage unless line.skipped?

  "ignored"
end

#format_line_coverage(source_file) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 124

def format_line_coverage(source_file)
  covered = source_file.covered_lines.count
  missed = source_file.missed_lines.count
  {
    lines: source_file.lines.map { |line| format_line(line) },
    lines_covered_percent: source_file.covered_percent,
    covered_lines: covered,
    missed_lines: missed,
    total_lines: covered + missed
  }
end

#format_line_statistic(stat) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 193

def format_line_statistic(stat)
  {
    covered: stat.covered,
    missed: stat.missed,
    omitted: stat.omitted,
    total: stat.total,
    percent: stat.percent,
    strength: stat.strength
  }
end

#format_maximum_coverage_drop_errors (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 84

def format_maximum_coverage_drop_errors
  SimpleCov::CoverageViolations.maximum_drop(@result, SimpleCov.maximum_coverage_drop).each do |violation|
    key = CRITERION_KEYS.fetch(SimpleCov.coverage_statistics_key(violation.fetch(:criterion)))
    bucket = formatted_result[:errors][:maximum_coverage_drop] ||= {}
    bucket[key] = {maximum: violation.fetch(:maximum), actual: violation.fetch(:actual)}
  end
end

#format_meta (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 96

def format_meta
  {
    simplecov_version: SimpleCov::VERSION,
    command_name: @result.command_name,
    project_name: SimpleCov.project_name,
    timestamp: @result.created_at.iso8601(3),
    root: SimpleCov.root,
    branch_coverage: SimpleCov.branch_coverage?,
    method_coverage: SimpleCov.method_coverage?
  }
end

#format_method(method) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 173

def format_method(method)
  {
    name: method.to_s,
    start_line: method.start_line,
    end_line: method.end_line,
    coverage: format_line(method)
  }
end

#format_method_coverage(source_file) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 146

def format_method_coverage(source_file)
  {
    methods: source_file.methods.map { |method| format_method(method) },
    methods_covered_percent: source_file.methods_coverage_percent,
    covered_methods: source_file.covered_methods.count,
    missed_methods: source_file.missed_methods.count,
    total_methods: source_file.methods.count
  }
end

#format_minimum_coverage_by_file_errors (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 63

def format_minimum_coverage_by_file_errors
  violations = SimpleCov::CoverageViolations.minimum_by_file(@result, SimpleCov.minimum_coverage_by_file)
  violations.each do |violation|
    key = CRITERION_KEYS.fetch(SimpleCov.coverage_statistics_key(violation.fetch(:criterion)))
    bucket = formatted_result[:errors][:minimum_coverage_by_file] ||= {}
    criterion_errors = bucket[key] ||= {}
    criterion_errors[violation.fetch(:project_filename)] =
      {expected: violation.fetch(:expected), actual: violation.fetch(:actual)}
  end
end

#format_minimum_coverage_by_group_errors (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 74

def format_minimum_coverage_by_group_errors
  violations = SimpleCov::CoverageViolations.minimum_by_group(@result, SimpleCov.minimum_coverage_by_group)
  violations.each do |violation|
    key = CRITERION_KEYS.fetch(SimpleCov.coverage_statistics_key(violation.fetch(:criterion)))
    bucket = formatted_result[:errors][:minimum_coverage_by_group] ||= {}
    group_errors = bucket[violation.fetch(:group_name)] ||= {}
    group_errors[key] = {expected: violation.fetch(:expected), actual: violation.fetch(:actual)}
  end
end

#format_minimum_coverage_errors (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 55

def format_minimum_coverage_errors
  SimpleCov::CoverageViolations.minimum_overall(@result, SimpleCov.minimum_coverage).each do |violation|
    key = CRITERION_KEYS.fetch(SimpleCov.coverage_statistics_key(violation.fetch(:criterion)))
    bucket = formatted_result[:errors][:minimum_coverage] ||= {}
    bucket[key] = {expected: violation.fetch(:expected), actual: violation.fetch(:actual)}
  end
end

#format_single_statistic(stat) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 204

def format_single_statistic(stat)
  {
    covered: stat.covered,
    missed: stat.missed,
    total: stat.total,
    percent: stat.percent,
    strength: stat.strength
  }
end

#format_source_code(source_file) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 116

def format_source_code(source_file)
  {source: source_file.lines.map { |line| ensure_utf8(line.src.chomp) }}
end

#format_source_file(source_file) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 108

def format_source_file(source_file)
  result = format_line_coverage(source_file)
  result.merge!(format_source_code(source_file))
  result.merge!(format_branch_coverage(source_file)) if SimpleCov.branch_coverage?
  result.merge!(format_method_coverage(source_file)) if SimpleCov.method_coverage?
  result
end

#format_total (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 26

def format_total
  formatted_result[:total] = format_coverage_statistics(@result.coverage_statistics)
end

#formatted_result (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 92

def formatted_result
  @formatted_result ||= {meta: format_meta, total: {}, coverage: {}, groups: {}, errors: {}}
end