123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::Formatter::JSONFormatter::SourceFileFormatter

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

Overview

Renders a single ::SimpleCov::SourceFile as the per-file payload in coverage.json: source code plus per-enabled-criterion arrays and totals.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(source_file, include_source: true) ⇒ SourceFileFormatter

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 10

def initialize(source_file, include_source: true)
  @source_file = source_file
  @include_source = include_source
end

Instance Attribute Details

#line_coverage_enabled?Boolean (readonly, private)

:oneshot_line is a synonym for :line for stats purposes (see SimpleCov.coverage_statistics_key), so treat either as "line coverage is on" for the line-block emit decisions.

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 28

def line_coverage_enabled?
  SimpleCov.coverage_criterion_enabled?(:line) || SimpleCov.coverage_criterion_enabled?(:oneshot_line)
end

Instance Method Details

#branch_coverage_section (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 53

def branch_coverage_section
  {
    branches: @source_file.branches.map { |branch| format_branch(branch) },
    branches_covered_percent: @source_file.covered_percent(:branch),
    covered_branches: @source_file.covered_branches.size,
    missed_branches: @source_file.missed_branches.size,
    total_branches: @source_file.total_branches.size
  }
end

#call

[ GitHub ]

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

def call
  result = @include_source ? format_source_code : {}
  result.merge!(line_coverage_section) if line_coverage_enabled?
  result.merge!(branch_coverage_section) if SimpleCov.branch_coverage?
  result.merge!(method_coverage_section) if SimpleCov.method_coverage?
  result
end

#ensure_utf8(str) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 36

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

#format_branch(branch) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 77

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_line(line) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 73

def format_line(line)
  line.skipped? ? "ignored" : line.coverage
end

#format_method(method) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 88

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

#format_source_code (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 32

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

#line_coverage_section (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/source_file_formatter.rb', line 40

def line_coverage_section
  covered = @source_file.covered_lines.size
  missed = @source_file.missed_lines.size
  {
    lines: @source_file.lines.map { |line| format_line(line) },
    lines_covered_percent: @source_file.covered_percent,
    covered_lines: covered,
    missed_lines: missed,
    omitted_lines: @source_file.never_lines.size,
    total_lines: covered + missed
  }
end

#method_coverage_section (private)

[ GitHub ]

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

def method_coverage_section
  {
    methods: @source_file.methods.map { |method| format_method(method) },
    methods_covered_percent: @source_file.covered_percent(:method),
    covered_methods: @source_file.covered_methods.size,
    missed_methods: @source_file.missed_methods.size,
    total_methods: @source_file.methods.size
  }
end