123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::SourceFile::LineBuilder

Relationships & Source Files
Inherits: Object
Defined in: lib/simplecov/source_file/line_builder.rb

Overview

Builds the Line objects for a source file from the raw line-coverage array. Each line carries its source text, its 1-based line number, and the Coverage hit count (or nil for never-counted lines). Applies # simplecov:disable / # :nocov: block ranges via skipped!.

Class Method Summary

Instance Method Summary

  • #call
  • #build_lines private

    When :line coverage is disabled, the Ruby Coverage module doesn't emit "lines" data, so look up nil (never-counted) for every position.

  • #mark_skipped(lines, chunks) private

    The array the lines are kept in is 0-based whereas the line numbers in the chunks are 1-based (more understandable elsewhere), so each range needs to be shifted down by one to slice into the #lines array.

Constructor Details

.new(source_file) ⇒ LineBuilder

[ GitHub ]

  
# File 'lib/simplecov/source_file/line_builder.rb', line 11

def initialize(source_file)
  @source_file = source_file
end

Instance Method Details

#build_lines (private)

When :line coverage is disabled, the Ruby Coverage module doesn't emit "lines" data, so look up nil (never-counted) for every position. The source rows are still useful — e.g. for the HTML report's source view — even without per-line hits.

[ GitHub ]

  
# File 'lib/simplecov/source_file/line_builder.rb', line 27

def build_lines
  line_coverage = @source_file.coverage_data["lines"] || []
  @source_file.src.map.with_index(1) do |src, i|
    SourceFile::Line.new(src, i, line_coverage[i - 1])
  end
end

#call

[ GitHub ]

  
# File 'lib/simplecov/source_file/line_builder.rb', line 15

def call
  lines = build_lines
  mark_skipped(lines, @source_file.skip_chunks_for(:line))
  lines
end

#mark_skipped(lines, chunks) (private)

The array the lines are kept in is 0-based whereas the line numbers in the chunks are 1-based (more understandable elsewhere), so each range needs to be shifted down by one to slice into the SimpleCov::SourceFile#lines array.

[ GitHub ]

  
# File 'lib/simplecov/source_file/line_builder.rb', line 38

def mark_skipped(lines, chunks)
  chunks.each { |chunk| lines[(chunk.begin - 1)..(chunk.end - 1)].each(&:skipped!) }
end