Sha256: 9208f4d30ebe7005d89bafa47dca6c46c2c8dc369e934cfe9b8664d70e46b607

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module SimpleCovJSONFormatter
  class SourceFileFormatter
    def initialize(source_file)
      @source_file = source_file
      @line_coverage = nil
    end

    def format
      if SimpleCov.branch_coverage?
        line_coverage.merge(branch_coverage)
      else
        line_coverage
      end
    end

    private

    def line_coverage
      @line_coverage ||= {
        lines: lines
      }
    end

    def branch_coverage
      {
        branches: branches
      }
    end

    def lines
      lines = []
      @source_file.lines.each do |line|
        lines << parse_line(line)
      end

      lines
    end

    def branches
      branches = []
      @source_file.branches.each do |branch|
        branches << parse_branch(branch)
      end

      branches
    end

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

      'ignored'
    end

    def parse_branch(branch)
      {
        type: branch.type,
        start_line: branch.start_line,
        end_line: branch.end_line,
        coverage: parse_line(branch)
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/simplecov_json_formatter-0.1.4/lib/simplecov_json_formatter/source_file_formatter.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/simplecov_json_formatter-0.1.4/lib/simplecov_json_formatter/source_file_formatter.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/simplecov_json_formatter-0.1.4/lib/simplecov_json_formatter/source_file_formatter.rb
simplecov_json_formatter-0.1.4 lib/simplecov_json_formatter/source_file_formatter.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/simplecov_json_formatter-0.1.3/lib/simplecov_json_formatter/source_file_formatter.rb
simplecov_json_formatter-0.1.3 lib/simplecov_json_formatter/source_file_formatter.rb