From 427a76e2ce4221e533cb5e8240f49ae9a5cdcd56 Mon Sep 17 00:00:00 2001 From: Cole Dunsby Date: Mon, 28 Mar 2022 12:11:12 -0400 Subject: [PATCH] Calculate total project cov according to line count for each target --- lib/xcov/model/report.rb | 9 +++++---- lib/xcov/model/target.rb | 8 +++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/xcov/model/report.rb b/lib/xcov/model/report.rb index e93dfad..e4aca04 100644 --- a/lib/xcov/model/report.rb +++ b/lib/xcov/model/report.rb @@ -9,18 +9,19 @@ class Report < Xcov::Base def initialize(targets) @targets = targets - @coverage = average_coverage(targets) + @coverage = total_project_coverage(targets) @displayable_coverage = self.create_displayable_coverage @coverage_color = self.create_coverage_color @summary = self.create_summary end - def average_coverage targets + def total_project_coverage targets return 0 if targets.count == 0 return targets.first.coverage if targets.count == 1 - acc_coverage = targets.reduce(0) { |acc, target| acc + target.coverage } - acc_coverage.to_f / targets.count + acc_total_covered_lines = targets.reduce(0) { |acc, target| acc + target.total_covered_lines } + acc_total_executable_lines = targets.reduce(0) { |acc, target| acc + target.total_executable_lines } + acc_total_covered_lines.to_f / acc_total_executable_lines.to_f end def print_description diff --git a/lib/xcov/model/target.rb b/lib/xcov/model/target.rb index 59f495c..71d5571 100644 --- a/lib/xcov/model/target.rb +++ b/lib/xcov/model/target.rb @@ -6,13 +6,15 @@ class Target < Xcov::Base attr_accessor :name attr_accessor :files attr_accessor :file_templates + attr_accessor :total_covered_lines + attr_accessor :total_executable_lines def initialize(name, files) @name = CGI::escapeHTML(name) @files = files - totalCoveredLines = files.reduce(0) { |acc, file| acc + file.coveredLines } - totalExecutableLines = files.reduce(0) { |acc, file| acc + file.executableLines } - @coverage = files.count == 0 || totalExecutableLines == 0 ? 0.0 : totalCoveredLines.to_f / totalExecutableLines.to_f + @total_covered_lines = files.reduce(0) { |acc, file| acc + file.coveredLines } + @total_executable_lines = files.reduce(0) { |acc, file| acc + file.executableLines } + @coverage = files.count == 0 || @total_executable_lines == 0 ? 0.0 : @total_covered_lines.to_f / @total_executable_lines.to_f @displayable_coverage = self.create_displayable_coverage @coverage_color = self.create_coverage_color @id = Target.create_id(name)