Skip to content

Commit

Permalink
Threshold not fail when no module (#1115)
Browse files Browse the repository at this point in the history
Threshold not fail when no module
  • Loading branch information
daveMueller authored Mar 25, 2021
1 parent 3ae1976 commit b1c20d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/coverlet.core/CoverageDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ namespace Coverlet.Core
internal class CoverageDetails
{
private double _averageModulePercent;

public Modules Modules { get; internal set; }
public double Covered { get; internal set; }
public int Total { get; internal set; }
public double Percent
{
get
{
if (Modules?.Count == 0) return 0;
return Total == 0 ? 100D : Math.Floor((Covered / Total) * 10000) / 100;
}
}

public double AverageModulePercent
{
get { return Math.Floor(_averageModulePercent * 100) / 100; }
internal set { _averageModulePercent = value; }
}

public double Percent => Total == 0 ? 100D : Math.Floor((Covered / Total) * 10000) / 100;
}
}
22 changes: 17 additions & 5 deletions src/coverlet.core/CoverageSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public CoverageDetails CalculateLineCoverage(Documents documents)

public CoverageDetails CalculateLineCoverage(Modules modules)
{
var details = new CoverageDetails();
var details = new CoverageDetails{Modules = modules};
var accumPercent = 0.0D;

if (modules.Count == 0)
return details;

foreach (var module in modules)
{
var moduleCoverage = CalculateLineCoverage(module.Value);
Expand Down Expand Up @@ -185,16 +189,20 @@ public CoverageDetails CalculateBranchCoverage(Documents documents)

public CoverageDetails CalculateBranchCoverage(Modules modules)
{
var details = new CoverageDetails();
var details = new CoverageDetails{ Modules = modules };
var accumPercent = 0.0D;

if (modules.Count == 0)
return details;

foreach (var module in modules)
{
var moduleCoverage = CalculateBranchCoverage(module.Value);
details.Covered += moduleCoverage.Covered;
details.Total += moduleCoverage.Total;
accumPercent += moduleCoverage.Percent;
}
details.AverageModulePercent = accumPercent / modules.Count;
details.AverageModulePercent = modules.Count == 0 ? 0 : accumPercent / modules.Count;
return details;
}

Expand Down Expand Up @@ -245,16 +253,20 @@ public CoverageDetails CalculateMethodCoverage(Documents documents)

public CoverageDetails CalculateMethodCoverage(Modules modules)
{
var details = new CoverageDetails();
var details = new CoverageDetails{ Modules = modules };
var accumPercent = 0.0D;

if (modules.Count == 0)
return details;

foreach (var module in modules)
{
var moduleCoverage = CalculateMethodCoverage(module.Value);
details.Covered += moduleCoverage.Covered;
details.Total += moduleCoverage.Total;
accumPercent += moduleCoverage.Percent;
}
details.AverageModulePercent = accumPercent / modules.Count;
details.AverageModulePercent = modules.Count == 0 ? 0 : accumPercent / modules.Count;
return details;
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/coverlet.core.tests/Coverage/CoverageSummaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ private void SetupDataMultipleModule()
{ "aditionalModule", documents }
};
}

[Fact]
public void TestCalculateLineCoverage_NoModules()
{
CoverageSummary summary = new CoverageSummary();
var modules = new Modules();

Assert.Equal(0, summary.CalculateLineCoverage(modules).Percent);
Assert.Equal(0, summary.CalculateLineCoverage(modules).AverageModulePercent);
Assert.Equal(0, summary.CalculateBranchCoverage(modules).Percent);
Assert.Equal(0, summary.CalculateBranchCoverage(modules).AverageModulePercent);
Assert.Equal(0, summary.CalculateMethodCoverage(modules).Percent);
Assert.Equal(0, summary.CalculateMethodCoverage(modules).AverageModulePercent);
}

[Fact]
public void TestCalculateLineCoverage_SingleModule()
Expand Down

0 comments on commit b1c20d5

Please sign in to comment.