Class: XCPerfect::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/xcperfect/parser.rb

Overview

Parser is used by ‘Formatter`s to extract information from the JSON file generated by xccov

Constant Summary collapse

KEY_TARGETS =
'targets'.freeze
KEY_EXECUTABLE_LINES =
'executableLines'.freeze
KEY_COVERED_LINES =
'coveredLines'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Parser

Returns a new instance of Parser.



11
12
13
# File 'lib/xcperfect/parser.rb', line 11

def initialize(json)
  @json = json
end

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



9
10
11
# File 'lib/xcperfect/parser.rb', line 9

def json
  @json
end

Instance Method Details

#all_targetsObject



15
16
17
# File 'lib/xcperfect/parser.rb', line 15

def all_targets
  @json[KEY_TARGETS]
end

#coverage_percentage(target) ⇒ Object



41
42
43
# File 'lib/xcperfect/parser.rb', line 41

def coverage_percentage(target)
  target['lineCoverage'].round(2)
end

#covered_line_num(target) ⇒ Object



45
46
47
# File 'lib/xcperfect/parser.rb', line 45

def covered_line_num(target)
  target['coveredLines']
end

#extract_coverage_info(target) ⇒ Object



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

def extract_coverage_info(target)
  [
    target['name'],
    covered_line_num(target),
    total_line_num(target),
    coverage_percentage(target)
  ]
end

#extract_targets(desirables) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xcperfect/parser.rb', line 19

def extract_targets(desirables)
  if desirables.length.zero?
    all_targets
  else
    all_targets.select do |target_info|
      pure_name = target_info['name'].split('.')
      pure_name.pop(1)
      pure_name = pure_name.join('.')
      desirables.include?(pure_name)
    end
  end
end

#total_line_num(target) ⇒ Object



49
50
51
# File 'lib/xcperfect/parser.rb', line 49

def total_line_num(target)
  target['executableLines']
end