class SimpleCovTextFormatter::SourceFileFormatter

Constants

FILE_COLUMN
NOT_COVERED_LINE_CODE
VALID_MSG_PARTS_COUNT

Public Class Methods

new(source_file) click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 7
def initialize(source_file)
  @source_file = source_file
end

Public Instance Methods

format() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 11
def format
  not_covered_lines_in_batches.map do |batch|
    build_message(batch)
  end.reject(&:nil?)
end

Private Instance Methods

build_error_text(batch) click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 36
def build_error_text(batch)
  case batch.count
  when 1
    batch.first
  when 2
    "#{batch.first} and #{batch.last}"
  else
    "#{batch.first} to #{batch.last}"
  end
end
build_message(batch) click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 19
def build_message(batch)
  message_parts = build_message_parts(batch)
  return unless message_parts.count == VALID_MSG_PARTS_COUNT
  return if full_coverage?

  message_parts.join(":")
end
build_message_parts(batch) click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 27
def build_message_parts(batch)
  [
    @source_file.filename,
    batch.first.to_s,
    FILE_COLUMN.to_s,
    " Not covered lines: #{build_error_text(batch)}"
  ].reject(&:nil?)
end
covered_lines() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 76
def covered_lines
  @covered_lines ||= lines.select { |l| l.coverage && l.coverage > NOT_COVERED_LINE_CODE }
end
covered_percentage() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 72
def covered_percentage
  @covered_percentage ||= (covered_lines.count.to_f / valid_lines.count * 100).round(2)
end
full_coverage?() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 68
def full_coverage?
  covered_percentage.to_i == 100
end
lines() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 88
def lines
  @lines ||= @source_file.lines
end
near_lines?(line, next_line) click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 64
def near_lines?(line, next_line)
  [*line..line + 2].include?(next_line)
end
not_covered_lines() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 80
def not_covered_lines
  @not_covered_lines ||= lines.select { |l| l.coverage == NOT_COVERED_LINE_CODE }
end
not_covered_lines_in_batches() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 47
def not_covered_lines_in_batches
  line_numbers = not_covered_lines.map(&:line_number).sort
  batches = []
  current_batch = []

  line_numbers.each_with_index do |line_number, index|
    current_batch << line_number

    if !near_lines?(line_number, line_numbers[index + 1])
      batches << current_batch
      current_batch = []
    end
  end

  batches
end
valid_lines() click to toggle source
# File lib/simplecov_text_formatter/source_file_formatter.rb, line 84
def valid_lines
  @valid_lines ||= lines.reject { |l| l.coverage.nil? }
end