class ReverseCoverage::Main

Attributes

config[RW]
coverage_matrix[R]
output_path[RW]

Public Class Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/reverse_coverage/main.rb, line 68
def method_missing(method, *args, &block)
  instance.respond_to?(method) ? instance.send(method, *args, &block) : super
end
new() click to toggle source
# File lib/reverse_coverage/main.rb, line 12
def initialize
  @config = {
    file_filter: ->(file_path) { file_of_project?(file_path) }
  }
  @output_path = 'tmp'
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/reverse_coverage/main.rb, line 72
def respond_to_missing?(method, include_private = false)
  instance.respond_to?(method) || super
end

Public Instance Methods

add(example) click to toggle source
# File lib/reverse_coverage/main.rb, line 19
def add(example)
  coverage_result = Coverage.peek_result
  example_data = slice_attributes(example.metadata, *example_attributes)
  example_data[:example_ref] = example_data.hash
  current_state = select_project_files(coverage_result)
  all_changed_files = changed_lines(@last_state, current_state)

  changes = {}

  all_changed_files.each do |file_path, lines|
    lines.each_with_index do |changed, line_index|
      next if changed.nil? || changed.zero?

      file_info = { file_path: file_path, line_index: line_index }

      save_changes(changes, example_data, file_info)
      save_changes(coverage_matrix, example_data, file_info)
    end
  end

  reset_last_state
  changes
end
reset_last_state(result = Coverage.peek_result) click to toggle source
# File lib/reverse_coverage/main.rb, line 43
def reset_last_state(result = Coverage.peek_result)
  @last_state = select_project_files(result)
end
result_and_stop_coverage() click to toggle source
# File lib/reverse_coverage/main.rb, line 63
def result_and_stop_coverage
  Coverage.result
end
save_results(file_name: 'reverse_coverage.yml') click to toggle source
# File lib/reverse_coverage/main.rb, line 52
def save_results(file_name: 'reverse_coverage.yml')
  result_and_stop_coverage
  path = File.join(output_path, file_name)
  FileUtils.mkdir_p(output_path)

  File.open(path, 'w') do |f|
    results = @coverage_matrix.sort.map { |k, v| [k, v.sort.to_h] }.to_h
    f.write results.to_yaml
  end
end
start() click to toggle source
# File lib/reverse_coverage/main.rb, line 47
def start
  @coverage_matrix = {}
  reset_last_state
end

Private Instance Methods

changed_lines(prev_state, current_state) click to toggle source
# File lib/reverse_coverage/main.rb, line 89
def changed_lines(prev_state, current_state)
  prev_state.merge(current_state) do |_file_path, prev_line, current_line|
    prev_line.zip(current_line).map { |values| values[0] == values[1] ? nil : (values[1] - values[0]) }
  end
end
example_attributes() click to toggle source
# File lib/reverse_coverage/main.rb, line 95
def example_attributes
  %I[description full_description file_path line_number scoped_id type]
end
file_of_project?(file_path) click to toggle source
# File lib/reverse_coverage/main.rb, line 103
def file_of_project?(file_path)
  file_path.start_with?(Dir.pwd) && !file_path.start_with?(Dir.pwd + '/spec')
end
save_changes(hash, example_data, file_path:, line_index:) click to toggle source
# File lib/reverse_coverage/main.rb, line 79
def save_changes(hash, example_data, file_path:, line_index:)
  hash[file_path] ||= {}
  hash[file_path][line_index] ||= []
  hash[file_path][line_index] << example_data
end
select_project_files(coverage_result) click to toggle source
# File lib/reverse_coverage/main.rb, line 99
def select_project_files(coverage_result)
  coverage_result.select { |file_path, _lines| @config[:file_filter].call(file_path) }
end
slice_attributes(hash, *keys) click to toggle source
# File lib/reverse_coverage/main.rb, line 85
def slice_attributes(hash, *keys)
  keys.each_with_object({}) { |k, new_hash| new_hash[k] = hash[k] if hash.key?(k) }
end