diff --git a/assets/file_report.html.erb b/assets/file_report.html.erb index e69de29..51e56c6 100644 --- a/assets/file_report.html.erb +++ b/assets/file_report.html.erb @@ -0,0 +1,60 @@ + + + Coverage for <%= @source_file_name %> + + + +

Coverage for <%= @source_file_name %> + + + + <% if @total_lines > 0 %> + + <% else %> + + <% end %> + + + + <% if @total_branches > 0 %> + + <% else %> + + <% end %> + +
Line Coverage:<%= @covered_lines %> / <%= @total_lines %> = <%= (100.0 * @covered_lines / @total_lines).to_i %>%-
Branch Coverage:<%= @covered_branches %> / <%= @total_branches %> = <%= (100.0 * @covered_branches / @total_branches).to_i %>%-
+ + + + + + + + <% source_file_lines.each_with_index do |line, i| %> + <% line_number = i + 1 %> + + + + + + + <% end %> +
Execution CountBranch CountLineSource
<%= file_coverage.get_line_count(line_number) %> + <% if branches = file_coverage.get_branches(line_number) %> + <% branches.each_with_index do |(branch_id, branch_coverage), i| %> + <% if i > 0 %> +
+ <% end %> + <%= branch_coverage[:taken_count] %> + <% if branch_coverage[:branch_info] %> + (<%= branch_coverage[:branch_info] %>) + <% end %> + <% end %> + <% end %> +
<%= line_number %><%= CGI.escape_html(line.chomp).gsub("\t", " " * 4) %>
+ + diff --git a/lib/gcovinator/file_coverage.rb b/lib/gcovinator/file_coverage.rb index 4b4d785..4b746ab 100644 --- a/lib/gcovinator/file_coverage.rb +++ b/lib/gcovinator/file_coverage.rb @@ -1,6 +1,9 @@ module Gcovinator class FileCoverage + attr_reader :line_counts + attr_reader :branches + def initialize @line_counts = {} @branches = {} @@ -25,7 +28,7 @@ module Gcovinator @branches[line_number][branch_id][:branch_info] = branch_info end - def get_branch(line_number) + def get_branches(line_number) @branches[line_number] end diff --git a/lib/gcovinator/file_report.rb b/lib/gcovinator/file_report.rb index 6ae91b2..113b8a2 100644 --- a/lib/gcovinator/file_report.rb +++ b/lib/gcovinator/file_report.rb @@ -8,7 +8,7 @@ module Gcovinator @covered_lines = 0 @total_branches = 0 @covered_branches = 0 - run(source_file_name, File.join(output_dir, report_file_name)) + run(source_file_name, File.join(output_dir, report_file_name), file_coverage) end private @@ -23,18 +23,33 @@ module Gcovinator source_file_name end - def run(source_file_name, output_file_name) + def run(source_file_name, output_file_name, file_coverage) + analyze(file_coverage) + require "cgi" require "erb" source_file = read_source_file(source_file_name) source_file_lines = source_file.lines.to_a file_report_template = File.read(File.join(File.dirname(__FILE__), "../../assets/file_report.html.erb")) erb = ERB.new(file_report_template, nil, "<>") - report = erb.run(binding.clone) + report = erb.result(binding.clone) File.open(output_file_name, "w") do |fh| fh.write(report) end end + def analyze(file_coverage) + file_coverage.line_counts.each do |line_number, count| + @total_lines += 1 + @covered_lines += 1 if count > 0 + end + file_coverage.branches.each do |line_number, branches| + branches.each do |branch_id, branch_coverage| + @total_branches += 1 + @covered_branches += 1 if branch_coverage[:taken_count] > 0 + end + end + end + def read_source_file(source_file_name) begin File.read(source_file_name)