Skip to content

Commit

Permalink
update signatures of calculate coverage methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed Apr 14, 2018
1 parent 84ae051 commit 4ce1b4e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 74 deletions.
136 changes: 75 additions & 61 deletions src/coverlet.core/CoverageSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,132 +6,146 @@ namespace Coverlet.Core
{
public class CoverageSummary
{
public double CalculateLineCoverage(KeyValuePair<string, Lines> method)
public double CalculateLineCoverage(Lines lines)
{
double coverage = 0, totalLines = 0, linesCovered = 0;
foreach (var line in method.Value)
{
totalLines++;
if (line.Value.Hits > 0)
linesCovered++;
}

coverage = totalLines == 0 ? totalLines : linesCovered / totalLines;
double linesCovered = lines.Where(l => l.Value.Hits > 0).Count();
double coverage = lines.Count == 0 ? lines.Count : linesCovered / lines.Count;
return Math.Round(coverage, 3);
}

public double CalculateLineCoverage(KeyValuePair<string, Methods> @class)
public double CalculateLineCoverage(Methods methods)
{
double total = 0, average = 0;
foreach (var method in @class.Value)
total += CalculateLineCoverage(method);
foreach (var method in methods)
total += CalculateLineCoverage(method.Value);

average = total / @class.Value.Count;
average = total / methods.Count;
return Math.Round(average, 3);
}

public double CalculateLineCoverage(KeyValuePair<string, Classes> document)
public double CalculateLineCoverage(Classes classes)
{
double total = 0, average = 0;
foreach (var @class in document.Value)
total += CalculateLineCoverage(@class);
foreach (var @class in classes)
total += CalculateLineCoverage(@class.Value);

average = total / document.Value.Count;
average = total / classes.Count;
return Math.Round(average, 3);
}

public double CalculateLineCoverage(KeyValuePair<string, Documents> module)
public double CalculateLineCoverage(Documents documents)
{
double total = 0, average = 0;
foreach (var document in module.Value)
total += CalculateLineCoverage(document);
foreach (var document in documents)
total += CalculateLineCoverage(document.Value);

average = total / module.Value.Count;
average = total / documents.Count;
return Math.Round(average, 3);
}

public double CalculateBranchCoverage(KeyValuePair<string, Lines> method)
public double CalculateLineCoverage(Modules modules)
{
double coverage = 0, totalPoints = 0, pointsCovered = 0;
foreach (var line in method.Value)
{
if (line.Value.IsBranchPoint)
{
totalPoints++;
if (line.Value.Hits > 0)
pointsCovered++;
}
}

coverage = totalPoints == 0 ? totalPoints : pointsCovered / totalPoints;
double total = 0, average = 0;
foreach (var module in modules)
total += CalculateLineCoverage(module.Value);

average = total / modules.Count;
return Math.Round(average, 3);
}

public double CalculateBranchCoverage(Lines lines)
{
double pointsCovered = lines.Where(l => l.Value.Hits > 0 && l.Value.IsBranchPoint).Count();
double totalPoints = lines.Where(l => l.Value.IsBranchPoint).Count();
double coverage = totalPoints == 0 ? totalPoints : pointsCovered / totalPoints;
return Math.Round(coverage, 3);
}

public double CalculateBranchCoverage(KeyValuePair<string, Methods> @class)
public double CalculateBranchCoverage(Methods methods)
{
double total = 0, average = 0;
foreach (var method in methods)
total += CalculateBranchCoverage(method.Value);

average = total / methods.Count;
return Math.Round(average, 3);
}

public double CalculateBranchCoverage(Classes classes)
{
double total = 0, average = 0;
foreach (var method in @class.Value)
total += CalculateBranchCoverage(method);
foreach (var @class in classes)
total += CalculateBranchCoverage(@class.Value);

average = total / @class.Value.Count;
average = total / classes.Count;
return Math.Round(average, 3);
}

public double CalculateBranchCoverage(KeyValuePair<string, Classes> document)
public double CalculateBranchCoverage(Documents documents)
{
double total = 0, average = 0;
foreach (var @class in document.Value)
total += CalculateBranchCoverage(@class);
foreach (var document in documents)
total += CalculateBranchCoverage(document.Value);

average = total / document.Value.Count;
average = total / documents.Count;
return Math.Round(average, 3);
}

public double CalculateBranchCoverage(KeyValuePair<string, Documents> module)
public double CalculateBranchCoverage(Modules modules)
{
double total = 0, average = 0;
foreach (var document in module.Value)
total += CalculateBranchCoverage(document);
foreach (var module in modules)
total += CalculateBranchCoverage(module.Value);

average = total / module.Value.Count;
average = total / modules.Count;
return Math.Round(average, 3);
}

public double CalculateMethodCoverage(KeyValuePair<string, Lines> method)
public double CalculateMethodCoverage(Lines lines)
{
if (method.Value.Any(l => l.Value.Hits > 0))
if (lines.Any(l => l.Value.Hits > 0))
return 1;

return 0;
}

public double CalculateMethodCoverage(KeyValuePair<string, Methods> @class)
public double CalculateMethodCoverage(Methods methods)
{
double total = 0, average = 0;
foreach (var method in methods)
total += CalculateMethodCoverage(method.Value);

average = total / methods.Count;
return Math.Round(average, 3);
}

public double CalculateMethodCoverage(Classes classes)
{
double total = 0, average = 0;
foreach (var method in @class.Value)
total += CalculateMethodCoverage(method);
foreach (var @class in classes)
total += CalculateMethodCoverage(@class.Value);

average = total / @class.Value.Count;
average = total / classes.Count;
return Math.Round(average, 3);
}

public double CalculateMethodCoverage(KeyValuePair<string, Classes> document)
public double CalculateMethodCoverage(Documents documents)
{
double total = 0, average = 0;
foreach (var @class in document.Value)
total += CalculateMethodCoverage(@class);
foreach (var document in documents)
total += CalculateMethodCoverage(document.Value);

average = total / document.Value.Count;
average = total / documents.Count;
return Math.Round(average, 3);
}

public double CalculateMethodCoverage(KeyValuePair<string, Documents> module)
public double CalculateMethodCoverage(Modules modules)
{
double total = 0, average = 0;
foreach (var document in module.Value)
total += CalculateMethodCoverage(document);
foreach (var module in modules)
total += CalculateMethodCoverage(module.Value);

average = total / module.Value.Count;
average = total / modules.Count;
return Math.Round(average, 3);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/coverlet.msbuild.tasks/CoverageResultTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override bool Execute()

foreach (var module in result.Modules)
{
double percent = summary.CalculateLineCoverage(module) * 100;
double percent = summary.CalculateLineCoverage(module.Value) * 100;
table.AddRow(System.IO.Path.GetFileNameWithoutExtension(module.Key), $"{percent}%");
total += percent;
}
Expand Down
24 changes: 12 additions & 12 deletions test/coverlet.core.tests/CoverageSummaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void TestCalculateLineCoverage()
var @class = document.Value.First();
var method = @class.Value.First();

Assert.Equal(0.5, summary.CalculateLineCoverage(module));
Assert.Equal(0.5, summary.CalculateLineCoverage(document));
Assert.Equal(0.5, summary.CalculateLineCoverage(@class));
Assert.Equal(0.5, summary.CalculateLineCoverage(method));
Assert.Equal(0.5, summary.CalculateLineCoverage(module.Value));
Assert.Equal(0.5, summary.CalculateLineCoverage(document.Value));
Assert.Equal(0.5, summary.CalculateLineCoverage(@class.Value));
Assert.Equal(0.5, summary.CalculateLineCoverage(method.Value));
}

[Fact]
Expand All @@ -56,10 +56,10 @@ public void TestCalculateBranchCoverage()
var @class = document.Value.First();
var method = @class.Value.First();

Assert.Equal(1, summary.CalculateBranchCoverage(module));
Assert.Equal(1, summary.CalculateBranchCoverage(document));
Assert.Equal(1, summary.CalculateBranchCoverage(@class));
Assert.Equal(1, summary.CalculateBranchCoverage(method));
Assert.Equal(1, summary.CalculateBranchCoverage(module.Value));
Assert.Equal(1, summary.CalculateBranchCoverage(document.Value));
Assert.Equal(1, summary.CalculateBranchCoverage(@class.Value));
Assert.Equal(1, summary.CalculateBranchCoverage(method.Value));
}

[Fact]
Expand All @@ -72,10 +72,10 @@ public void TestCalculateMethodCoverage()
var @class = document.Value.First();
var method = @class.Value.First();

Assert.Equal(1, summary.CalculateMethodCoverage(module));
Assert.Equal(1, summary.CalculateMethodCoverage(document));
Assert.Equal(1, summary.CalculateMethodCoverage(@class));
Assert.Equal(1, summary.CalculateMethodCoverage(method));
Assert.Equal(1, summary.CalculateMethodCoverage(module.Value));
Assert.Equal(1, summary.CalculateMethodCoverage(document.Value));
Assert.Equal(1, summary.CalculateMethodCoverage(@class.Value));
Assert.Equal(1, summary.CalculateMethodCoverage(method.Value));
}
}
}

0 comments on commit 4ce1b4e

Please sign in to comment.