Skip to content

Commit 4e029c3

Browse files
committed
add method to calculate threshold complaince
1 parent d61927d commit 4e029c3

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

src/coverlet.core/CoverageResult.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using Coverlet.Core.Enums;
56

67
namespace Coverlet.Core
78
{
@@ -105,5 +106,101 @@ internal void Merge(Modules modules)
105106
}
106107
}
107108
}
109+
110+
public ThresholdTypeFlags GetThresholdTypesBelowThreshold(CoverageSummary summary, double threshold, ThresholdTypeFlags thresholdTypes, ThresholdStatistic thresholdStat)
111+
{
112+
var thresholdTypeFlags = ThresholdTypeFlags.None;
113+
switch (thresholdStat)
114+
{
115+
case ThresholdStatistic.Minimum:
116+
{
117+
foreach (var module in Modules)
118+
{
119+
var line = summary.CalculateLineCoverage(module.Value).Percent * 100;
120+
var branch = summary.CalculateBranchCoverage(module.Value).Percent * 100;
121+
var method = summary.CalculateMethodCoverage(module.Value).Percent * 100;
122+
123+
if ((thresholdTypes & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
124+
{
125+
if (line < threshold)
126+
thresholdTypeFlags |= ThresholdTypeFlags.Line;
127+
}
128+
129+
if ((thresholdTypes & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
130+
{
131+
if (branch < threshold)
132+
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
133+
}
134+
135+
if ((thresholdTypes & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
136+
{
137+
if (method < threshold)
138+
thresholdTypeFlags |= ThresholdTypeFlags.Method;
139+
}
140+
}
141+
}
142+
break;
143+
case ThresholdStatistic.Average:
144+
{
145+
double line = 0;
146+
double branch = 0;
147+
double method = 0;
148+
int numModules = Modules.Count;
149+
150+
foreach (var module in Modules)
151+
{
152+
line += summary.CalculateLineCoverage(module.Value).Percent * 100;
153+
branch += summary.CalculateBranchCoverage(module.Value).Percent * 100;
154+
method += summary.CalculateMethodCoverage(module.Value).Percent * 100;
155+
}
156+
157+
if ((thresholdTypes & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
158+
{
159+
if ((line / numModules) < threshold)
160+
thresholdTypeFlags |= ThresholdTypeFlags.Line;
161+
}
162+
163+
if ((thresholdTypes & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
164+
{
165+
if ((branch / numModules) < threshold)
166+
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
167+
}
168+
169+
if ((thresholdTypes & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
170+
{
171+
if ((method / numModules) < threshold)
172+
thresholdTypeFlags |= ThresholdTypeFlags.Method;
173+
}
174+
}
175+
break;
176+
case ThresholdStatistic.Total:
177+
{
178+
var line = summary.CalculateLineCoverage(Modules).Percent * 100;
179+
var branch = summary.CalculateBranchCoverage(Modules).Percent * 100;
180+
var method = summary.CalculateMethodCoverage(Modules).Percent * 100;
181+
182+
if ((thresholdTypes & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
183+
{
184+
if (line < threshold)
185+
thresholdTypeFlags |= ThresholdTypeFlags.Line;
186+
}
187+
188+
if ((thresholdTypes & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
189+
{
190+
if (branch < threshold)
191+
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
192+
}
193+
194+
if ((thresholdTypes & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
195+
{
196+
if (method < threshold)
197+
thresholdTypeFlags |= ThresholdTypeFlags.Method;
198+
}
199+
}
200+
break;
201+
}
202+
203+
return thresholdTypeFlags;
204+
}
108205
}
109206
}

0 commit comments

Comments
 (0)