Class: Undercover::LcovParser

Inherits:
Object
  • Object
show all
Defined in:
lib/undercover/lcov_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lcov_io) ⇒ LcovParser

Returns a new instance of LcovParser.



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

def initialize(lcov_io)
  @io = lcov_io
  @source_files = {}
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



7
8
9
# File 'lib/undercover/lcov_parser.rb', line 7

def io
  @io
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



7
8
9
# File 'lib/undercover/lcov_parser.rb', line 7

def source_files
  @source_files
end

Class Method Details

.parse(lcov_report_path) ⇒ Object



14
15
16
17
# File 'lib/undercover/lcov_parser.rb', line 14

def self.parse(lcov_report_path)
  lcov_io = File.open(lcov_report_path)
  new(lcov_io).parse
end

Instance Method Details

#coverage(filepath) ⇒ Object



25
26
27
28
29
30
# File 'lib/undercover/lcov_parser.rb', line 25

def coverage(filepath)
  _filename, coverage = source_files.find do |relative_path, _|
    relative_path == filepath
  end
  coverage || []
end

#parseObject



19
20
21
22
23
# File 'lib/undercover/lcov_parser.rb', line 19

def parse
  io.each(&method(:parse_line))
  io.close
  self
end

#total_branch_coverageObject



41
42
43
44
45
46
47
48
# File 'lib/undercover/lcov_parser.rb', line 41

def total_branch_coverage
  all_lines = source_files.values.flatten(1)
  return 0 if all_lines.empty?

  all_branches = all_lines.select { _1.size == 4 }
  total_f = all_branches.select { |_l_no, _block_no, _br_no, hits| hits.positive? }.size.to_f / all_branches.size
  total_f.round(3)
end

#total_coverageObject



32
33
34
35
36
37
38
39
# File 'lib/undercover/lcov_parser.rb', line 32

def total_coverage
  all_lines = source_files.values.flatten(1)
  return 0 if all_lines.empty?

  all_lines = all_lines.select { _1.size == 2 }
  total_f = all_lines.select { |_line_no, hits| hits.positive? }.size.to_f / all_lines.size
  total_f.round(3)
end