Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threshold not fail when no module #1115

Merged
merged 5 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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