forked from codeclimate-community/simplecov_json_formatter
/
source_file_formatter.rb
65 lines (53 loc) · 1.08 KB
/
source_file_formatter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
module SimpleCovJSONFormatter
class SourceFileFormatter
def initialize(source_file)
@source_file = source_file
@line_coverage = nil
end
def format
if SimpleCov.branch_coverage?
line_coverage.merge(branch_coverage)
else
line_coverage
end
end
private
def line_coverage
@line_coverage ||= {
lines: lines
}
end
def branch_coverage
{
branches: branches
}
end
def lines
lines = []
@source_file.lines.each do |line|
lines << parse_line(line)
end
lines
end
def branches
branches = []
@source_file.branches.each do |branch|
branches << parse_branch(branch)
end
branches
end
def parse_line(line)
return line.coverage unless line.skipped?
'ignored'
end
def parse_branch(branch)
{
type: branch.type,
start_line: branch.start_line,
end_line: branch.end_line,
coverage: parse_line(branch)
}
end
end
end