Class: RubyBranchCover

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

Instance Method Summary collapse

Instance Method Details

#check_for_duplicate_line(line_number, line_tocover_elements) ⇒ Object

To check line already present in list of lineToCover element



7
8
9
10
11
12
13
14
15
# File 'lib/ruby_branch_cover.rb', line 7

def check_for_duplicate_line(line_number, line_tocover_elements)
    line_tocover_elements.each_with_index do
        |element, idx|
        if element[:lineNumber] == line_number
            return idx
        end
    end
    return -1
end

#find_notcovered_branches(condtions_map) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_branch_cover.rb', line 17

def find_notcovered_branches(condtions_map)
    not_covered = 0
    condtions_map.each do
        |condition, hitCount|
        if hitCount == 0
            not_covered+=1
        end
    end
    return not_covered
end

#find_rspec_key(count) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_branch_cover.rb', line 43

def find_rspec_key(count)
    key_to_append = 'RSpec'
    final_key = ''
    count.times do
        |index| 
        final_key = final_key + key_to_append
        if index < count-1
            final_key = final_key + ', '
        end
    end
    return final_key
end

#prepare_xml_builder(xml, file_elements) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_branch_cover.rb', line 28

def prepare_xml_builder(xml, file_elements)
    xml.coverage("version" => "1") do
        file_elements.each do
            |fileElement|
            xml.file("path" => fileElement[:path]) do
                fileElement[:lineElements].each do
                    |element|
                    xml.lineToCover("lineNumber"=>element[:lineNumber], "covered"=>element[:covered], 
                    "branchesToCover"=>element[:branchesToCover], "coveredBranches"=>element[:coveredBranches])
                end
            end
        end
    end
end

#read_json_and_getxml(filepath, parallelism_count) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby_branch_cover.rb', line 57

def read_json_and_getxml(filepath, parallelism_count)
    file = File.read(filepath)
    data_hash = JSON.parse(file)
    file_elements = []
    rspec_key = find_rspec_key parallelism_count
    unless data_hash.empty? || data_hash[rspec_key] == nil || data_hash[rspec_key]['coverage'] == nil 
        files_map = data_hash[rspec_key]['coverage']
        files_map.each do
            |filename,value| 
            branches = value['branches']
            line_to_cover_array = []
            unless branches == nil || branches.keys.size == 0
                branches.each do 
                    |branch, condtions_map| 
                    line_number = branch.split(',')[2].strip
                    not_covered = find_notcovered_branches condtions_map
                    covered = condtions_map.keys.size-not_covered
                    line_tocover_element = {lineNumber:line_number , covered: covered > 0 , branchesToCover: condtions_map.keys.size, coveredBranches:covered}
                    index = check_for_duplicate_line line_number, line_to_cover_array
                    if index != -1
                        old_line_tocover_element = line_to_cover_array[index]
                        old_line_tocover_element[:branchesToCover] = old_line_tocover_element[:branchesToCover] + line_tocover_element[:branchesToCover]
                        old_line_tocover_element[:coveredBranches] = old_line_tocover_element[:coveredBranches] + line_tocover_element[:coveredBranches]
                        old_line_tocover_element[:covered] = old_line_tocover_element[:covered] && line_tocover_element[:covered]
                        line_to_cover_array[index] = old_line_tocover_element
                    else
                        line_to_cover_array.push(line_tocover_element)
                    end
                end
                file_element = {path: filename, lineElements: line_to_cover_array}
                file_elements.push(file_element)
            end
        end

        xml = Builder::XmlMarkup.new(:indent => 2)
        puts "Total Files: #{file_elements.size}"
        prepare_xml_builder xml, file_elements
        File.write("coverage/branch-coverage.xml", xml.target!)
        if file_elements.size > 0
            return true
        else
            return false
        end
    else
        xml = Builder::XmlMarkup.new(:indent => 2)
        puts "Total Files: #{file_elements.size}"
        # xml.target!
        prepare_xml_builder xml, file_elements
        File.write("coverage/branch-coverage.xml", xml.target!)
        return false
    end

end