Skip to content

Commit c745dbc

Browse files
authored
Implementation of Npath complexity for the OpenCover reports (#1058)
Implementation of Npath complexity for the OpenCover reports
1 parent f27f4f5 commit c745dbc

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/coverlet.core/CoverageSummary.cs

+34
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,40 @@ public CoverageDetails CalculateBranchCoverage(IList<BranchInfo> branches)
7373
return details;
7474
}
7575

76+
public int CalculateNpathComplexity(IList<BranchInfo> branches)
77+
{
78+
// Adapted from OpenCover see https://github.com/OpenCover/opencover/blob/master/main/OpenCover.Framework/Persistance/BasePersistance.cs#L419
79+
if (!branches.Any())
80+
{
81+
return 0;
82+
}
83+
84+
var paths = new Dictionary<int, int>();
85+
foreach (var branch in branches)
86+
{
87+
if (!paths.TryGetValue(branch.Offset, out int count))
88+
{
89+
count = 0;
90+
}
91+
paths[branch.Offset] = ++count;
92+
}
93+
94+
int npath = 1;
95+
foreach (var branchPoints in paths.Values)
96+
{
97+
try
98+
{
99+
npath = checked(npath * branchPoints);
100+
}
101+
catch (OverflowException)
102+
{
103+
npath = int.MaxValue;
104+
break;
105+
}
106+
}
107+
return npath;
108+
}
109+
76110
public int CalculateCyclomaticComplexity(IList<BranchInfo> branches)
77111
{
78112
return Math.Max(1, branches.Count);

src/coverlet.core/Reporters/OpenCoverReporter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ public string Report(CoverageResult result, ISourceRootTranslator sourceRootTran
7878
var methLineCoverage = summary.CalculateLineCoverage(meth.Value.Lines);
7979
var methBranchCoverage = summary.CalculateBranchCoverage(meth.Value.Branches);
8080
var methCyclomaticComplexity = summary.CalculateCyclomaticComplexity(meth.Value.Branches);
81+
var methNpathComplexity = summary.CalculateNpathComplexity(meth.Value.Branches);
8182

8283
XElement method = new XElement("Method");
8384

8485
method.Add(new XAttribute("cyclomaticComplexity", methCyclomaticComplexity.ToString()));
85-
method.Add(new XAttribute("nPathComplexity", "0"));
86+
method.Add(new XAttribute("nPathComplexity", methCyclomaticComplexity.ToString()));
8687
method.Add(new XAttribute("sequenceCoverage", methLineCoverage.Percent.ToString("G", CultureInfo.InvariantCulture)));
8788
method.Add(new XAttribute("branchCoverage", methBranchCoverage.Percent.ToString("G", CultureInfo.InvariantCulture)));
8889
method.Add(new XAttribute("isConstructor", meth.Key.Contains("ctor").ToString()));

test/coverlet.core.tests/Reporters/OpenCoverReporterTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void TestReport()
2727
XDocument doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
2828
Assert.Empty(doc.Descendants().Attributes("sequenceCoverage").Where(v => v.Value != "33.33"));
2929
Assert.Empty(doc.Descendants().Attributes("branchCoverage").Where(v => v.Value != "25"));
30+
Assert.Empty(doc.Descendants().Attributes("nPathComplexity").Where(v => v.Value != "4"));
3031
}
3132

3233
[Fact]

0 commit comments

Comments
 (0)