Class: Undercover::SimplecovResultAdapter

Inherits:
Object
  • Object
show all
Includes:
RootToRelativePaths
Defined in:
lib/undercover/simplecov_result_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RootToRelativePaths

#fix_relative_filepath

Constructor Details

#initialize(simplecov_result, opts) ⇒ SimplecovResultAdapter

Returns a new instance of SimplecovResultAdapter.

Parameters:

  • simplecov_result (SimpleCov::Result)


23
24
25
26
# File 'lib/undercover/simplecov_result_adapter.rb', line 23

def initialize(simplecov_result, opts)
  @simplecov_result = simplecov_result
  @code_dir = opts&.path
end

Instance Attribute Details

#simplecov_resultObject (readonly)

Returns the value of attribute simplecov_result.



9
10
11
# File 'lib/undercover/simplecov_result_adapter.rb', line 9

def simplecov_result
  @simplecov_result
end

Class Method Details

.parse(file, opts = nil) ⇒ Object

Returns SimplecovResultAdapter.

Parameters:

  • file (File)

    JSON file supplied by SimpleCov::Formatter::Undercover

Returns:

  • SimplecovResultAdapter

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/undercover/simplecov_result_adapter.rb', line 13

def self.parse(file, opts = nil)
  # :nocov:
  result_h = JSON.parse(file.read)
  raise ArgumentError, 'empty SimpleCov' if result_h.empty?

  new(result_h, opts)
  # :nocov:
end

Instance Method Details

#coverage(filepath) ⇒ Object

Returns Array tuples (lines) and quadruples (branches) compatible with LcovParser.

Parameters:

  • filepath (String)

Returns:

  • Array tuples (lines) and quadruples (branches) compatible with LcovParser



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/undercover/simplecov_result_adapter.rb', line 30

def coverage(filepath) # rubocop:disable Metrics/MethodLength
  source_file = find_file(filepath)

  return [] unless source_file

  lines = source_file['lines'].map.with_index do |line_coverage, idx|
    [idx + 1, line_coverage] if line_coverage
  end.compact
  return lines unless source_file['branches']

  branch_idx = 0
  branches = source_file['branches'].map do |branch|
    branch_idx += 1
    [branch['start_line'], 0, branch_idx, branch['coverage']]
  end
  lines + branches
end

#ignored_filesObject



59
60
61
# File 'lib/undercover/simplecov_result_adapter.rb', line 59

def ignored_files
  @ignored_files ||= simplecov_result.dig('meta', 'ignored_files') || []
end

#skipped?(filepath, line_no) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/undercover/simplecov_result_adapter.rb', line 48

def skipped?(filepath, line_no)
  source_file = find_file(filepath)
  return false unless source_file

  source_file['lines'][line_no - 1] == 'ignored'
end

#total_branch_coverageObject



57
# File 'lib/undercover/simplecov_result_adapter.rb', line 57

def total_branch_coverage; end

#total_coverageObject

unused for now



56
# File 'lib/undercover/simplecov_result_adapter.rb', line 56

def total_coverage; end