Skip to content

Commit

Permalink
update the branch coverage information
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed Jun 23, 2018
1 parent 554f054 commit 4f220f5
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 72 deletions.
31 changes: 9 additions & 22 deletions src/coverlet.core/Coverage.cs
Expand Up @@ -99,36 +99,24 @@ public CoverageResult GetCoverageResult()
{
if (methods.TryGetValue(branch.Method, out Method method))
{
if (method.Branches.TryGetValue(branch.Number, out List<BranchInfo> branchInfo))
{
documents[doc.Path][branch.Class][branch.Method].Branches[branch.Number].Add(new BranchInfo
{ Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
);
}
else
{
documents[doc.Path][branch.Class][branch.Method].Branches.Add(branch.Number, new List<BranchInfo>());
documents[doc.Path][branch.Class][branch.Method].Branches[branch.Number].Add(new BranchInfo
{ Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
);
}
method.Branches.Add(new BranchInfo
{ Line = branch.Number, Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
);
}
else
{
documents[doc.Path][branch.Class].Add(branch.Method, new Method());
documents[doc.Path][branch.Class][branch.Method].Branches.Add(branch.Number, new List<BranchInfo>());
documents[doc.Path][branch.Class][branch.Method].Branches[branch.Number].Add(new BranchInfo
{ Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
documents[doc.Path][branch.Class][branch.Method].Branches.Add(new BranchInfo
{ Line = branch.Number, Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
);
}
}
else
{
documents[doc.Path].Add(branch.Class, new Methods());
documents[doc.Path][branch.Class].Add(branch.Method, new Method());
documents[doc.Path][branch.Class][branch.Method].Branches.Add(branch.Number, new List<BranchInfo>());
documents[doc.Path][branch.Class][branch.Method].Branches[branch.Number].Add(new BranchInfo
{ Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
documents[doc.Path][branch.Class][branch.Method].Branches.Add(new BranchInfo
{ Line = branch.Number, Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
);
}
}
Expand All @@ -137,9 +125,8 @@ public CoverageResult GetCoverageResult()
documents.Add(doc.Path, new Classes());
documents[doc.Path].Add(branch.Class, new Methods());
documents[doc.Path][branch.Class].Add(branch.Method, new Method());
documents[doc.Path][branch.Class][branch.Method].Branches.Add(branch.Number, new List<BranchInfo>());
documents[doc.Path][branch.Class][branch.Method].Branches[branch.Number].Add(new BranchInfo
{ Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
documents[doc.Path][branch.Class][branch.Method].Branches.Add(new BranchInfo
{ Line = branch.Number, Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/coverlet.core/CoverageResult.cs
Expand Up @@ -7,6 +7,7 @@ namespace Coverlet.Core
{
public class BranchInfo
{
public int Line { get; set; }
public int Offset { get; set; }
public int EndOffset { get; set; }
public int Path { get; set; }
Expand All @@ -15,7 +16,9 @@ public class BranchInfo
}

public class Lines : SortedDictionary<int, int> { }
public class Branches : SortedDictionary<int, List<BranchInfo>> { }

public class Branches : List<BranchInfo> { }

public class Method
{
internal Method()
Expand Down
14 changes: 3 additions & 11 deletions src/coverlet.core/CoverageSummary.cs
Expand Up @@ -62,19 +62,11 @@ public CoverageDetails CalculateLineCoverage(Modules modules)
return details;
}

public CoverageDetails CalculateBranchCoverage(List<BranchInfo> branchInfo)
public CoverageDetails CalculateBranchCoverage(IList<BranchInfo> branches)
{
var details = new CoverageDetails();
details.Covered = branchInfo.Count(bi => bi.Hits > 0);
details.Total = branchInfo.Count;
return details;
}

public CoverageDetails CalculateBranchCoverage(Branches branches)
{
var details = new CoverageDetails();
details.Covered = branches.Sum(b => b.Value.Where(bi => bi.Hits > 0).Count());
details.Total = branches.Sum(b => b.Value.Count());
details.Covered = branches.Count(bi => bi.Hits > 0);
details.Total = branches.Count;
return details;
}

Expand Down
6 changes: 4 additions & 2 deletions src/coverlet.core/Reporters/CoberturaReporter.cs
Expand Up @@ -71,13 +71,15 @@ public string Report(CoverageResult result)
XElement lines = new XElement("lines");
foreach (var ln in meth.Value.Lines)
{
bool isBranchPoint = meth.Value.Branches.Any(b => b.Line == ln.Key);
XElement line = new XElement("line");
line.Add(new XAttribute("number", ln.Key.ToString()));
line.Add(new XAttribute("hits", ln.Value.ToString()));
line.Add(new XAttribute("branch", meth.Value.Branches.ContainsKey(ln.Key).ToString()));
line.Add(new XAttribute("branch", isBranchPoint.ToString()));

if (meth.Value.Branches.TryGetValue(ln.Key, out List<BranchInfo> branches))
if (isBranchPoint)
{
var branches = meth.Value.Branches.Where(b => b.Line == ln.Key).ToList();
var branchInfoCoverage = summary.CalculateBranchCoverage(branches);
line.Add(new XAttribute("condition-coverage", $"{branchInfoCoverage.Percent*100}% ({branchInfoCoverage.Covered}/{branchInfoCoverage.Total})"));
XElement conditions = new XElement("conditions");
Expand Down
5 changes: 2 additions & 3 deletions src/coverlet.core/Reporters/LcovReporter.cs
Expand Up @@ -38,10 +38,9 @@ public string Report(CoverageResult result)
foreach (var line in method.Value.Lines)
lcov.Add($"DA:{line.Key},{line.Value}");

foreach (var branchs in method.Value.Branches)
foreach (var branch in method.Value.Branches)
{
foreach (var branch in branchs.Value)
lcov.Add($"BRDA:{branchs.Key},{branch.Offset},{branch.Path},{branch.Hits}");
lcov.Add($"BRDA:{branch.Line},{branch.Offset},{branch.Path},{branch.Hits}");
}
}
}
Expand Down
27 changes: 12 additions & 15 deletions src/coverlet.core/Reporters/OpenCoverReporter.cs
Expand Up @@ -131,22 +131,19 @@ public string Report(CoverageResult result)
k++;
}

foreach (var branches in meth.Value.Branches)
foreach (var branche in meth.Value.Branches)
{
foreach (var branch in branches.Value)
{
XElement branchPoint = new XElement("BranchPoint");
branchPoint.Add(new XAttribute("vc", branch.Hits.ToString()));
branchPoint.Add(new XAttribute("upsid", branches.Key.ToString()));
branchPoint.Add(new XAttribute("ordinal", branch.Ordinal.ToString()));
branchPoint.Add(new XAttribute("path", branch.Path.ToString()));
branchPoint.Add(new XAttribute("offset", branch.Offset.ToString()));
branchPoint.Add(new XAttribute("offsetend", branch.EndOffset.ToString()));
branchPoint.Add(new XAttribute("sl", branches.Key.ToString()));
branchPoint.Add(new XAttribute("fileid", i.ToString()));
branchPoints.Add(branchPoint);
kBr++;
}
XElement branchPoint = new XElement("BranchPoint");
branchPoint.Add(new XAttribute("vc", branche.Hits.ToString()));
branchPoint.Add(new XAttribute("upsid", branche.Line.ToString()));
branchPoint.Add(new XAttribute("ordinal", branche.Ordinal.ToString()));
branchPoint.Add(new XAttribute("path", branche.Path.ToString()));
branchPoint.Add(new XAttribute("offset", branche.Offset.ToString()));
branchPoint.Add(new XAttribute("offsetend", branche.EndOffset.ToString()));
branchPoint.Add(new XAttribute("sl", branche.Line.ToString()));
branchPoint.Add(new XAttribute("fileid", i.ToString()));
branchPoints.Add(branchPoint);
kBr++;
}

numMethods++;
Expand Down
5 changes: 2 additions & 3 deletions test/coverlet.core.tests/CoverageSummaryTests.cs
Expand Up @@ -18,9 +18,8 @@ public CoverageSummaryTests()
lines.Add(1, 1);
lines.Add(2, 0);
Branches branches = new Branches();
branches.Add(1, new List<BranchInfo>());
branches[1].Add(new BranchInfo { Hits = 1, Offset = 1, Path = 0, Ordinal = 1 });
branches[1].Add(new BranchInfo { Hits = 1, Offset = 1, Path = 1, Ordinal = 2 });
branches.Add(new BranchInfo { Line = 1, Hits = 1, Offset = 1, Path = 0, Ordinal = 1 });
branches.Add(new BranchInfo { Line = 1, Hits = 1, Offset = 1, Path = 1, Ordinal = 2 });

Methods methods = new Methods();
var methodString = "System.Void Coverlet.Core.Tests.CoverageSummaryTests::TestCalculateSummary()";
Expand Down
12 changes: 8 additions & 4 deletions test/coverlet.core.tests/Reporters/CoberturaReporterTests.cs
Expand Up @@ -11,23 +11,27 @@ public void TestReport()
{
CoverageResult result = new CoverageResult();
result.Identifier = Guid.NewGuid().ToString();

Lines lines = new Lines();
lines.Add(1, 1);
lines.Add(2, 0);

Branches branches = new Branches();
branches.Add(1, new List<BranchInfo> {
new BranchInfo{ Hits = 1, Offset = 23, EndOffset = 24, Path = 0, Ordinal = 1 },
new BranchInfo{ Hits = 0, Offset = 23, EndOffset = 27, Path = 1, Ordinal = 2 }
});
branches.Add(new BranchInfo { Line = 1, Hits = 1, Offset = 23, EndOffset = 24, Path = 0, Ordinal = 1 });
branches.Add(new BranchInfo { Line = 1, Hits = 0, Offset = 23, EndOffset = 27, Path = 1, Ordinal = 2 });

Methods methods = new Methods();
var methodString = "System.Void Coverlet.Core.Reporters.Tests.CoberturaReporterTests::TestReport()";
methods.Add(methodString, new Method());
methods[methodString].Lines = lines;
methods[methodString].Branches = branches;

Classes classes = new Classes();
classes.Add("Coverlet.Core.Reporters.Tests.CoberturaReporterTests", methods);

Documents documents = new Documents();
documents.Add("doc.cs", classes);

result.Modules = new Modules();
result.Modules.Add("module", documents);

Expand Down
5 changes: 5 additions & 0 deletions test/coverlet.core.tests/Reporters/JsonReporterTests.cs
Expand Up @@ -10,17 +10,22 @@ public void TestReport()
{
CoverageResult result = new CoverageResult();
result.Identifier = Guid.NewGuid().ToString();

Lines lines = new Lines();
lines.Add(1, 1);
lines.Add(2, 0);

Methods methods = new Methods();
var methodString = "System.Void Coverlet.Core.Reporters.Tests.JsonReporterTests.TestReport()";
methods.Add(methodString, new Method());
methods[methodString].Lines = lines;

Classes classes = new Classes();
classes.Add("Coverlet.Core.Reporters.Tests.JsonReporterTests", methods);

Documents documents = new Documents();
documents.Add("doc.cs", classes);

result.Modules = new Modules();
result.Modules.Add("module", documents);

Expand Down
11 changes: 7 additions & 4 deletions test/coverlet.core.tests/Reporters/LcovReporterTests.cs
Expand Up @@ -11,21 +11,24 @@ public void TestReport()
{
CoverageResult result = new CoverageResult();
result.Identifier = Guid.NewGuid().ToString();

Lines lines = new Lines();
lines.Add(1, 1);
lines.Add(2, 0);

Branches branches = new Branches();
branches.Add(1, new List<BranchInfo> {
new BranchInfo{ Hits = 1, Offset = 23, EndOffset = 24, Path = 0, Ordinal = 1 },
new BranchInfo{ Hits = 0, Offset = 23, EndOffset = 27, Path = 1, Ordinal = 2 }
});
branches.Add(new BranchInfo { Line = 1, Hits = 1, Offset = 23, EndOffset = 24, Path = 0, Ordinal = 1 });
branches.Add(new BranchInfo { Line = 1, Hits = 0, Offset = 23, EndOffset = 27, Path = 1, Ordinal = 2 });

Methods methods = new Methods();
var methodString = "System.Void Coverlet.Core.Reporters.Tests.LcovReporterTests.TestReport()";
methods.Add(methodString, new Method());
methods[methodString].Lines = lines;
methods[methodString].Branches = branches;

Classes classes = new Classes();
classes.Add("Coverlet.Core.Reporters.Tests.LcovReporterTests", methods);

Documents documents = new Documents();
documents.Add("doc.cs", classes);
result.Modules = new Modules();
Expand Down
17 changes: 10 additions & 7 deletions test/coverlet.core.tests/Reporters/OpenCoverReporterTests.cs
Expand Up @@ -11,7 +11,7 @@ public void TestReport()
{
CoverageResult result = new CoverageResult();
result.Identifier = Guid.NewGuid().ToString();

result.Modules = new Modules();
result.Modules.Add("Coverlet.Core.Reporters.Tests", CreateFirstDocuments());

Expand All @@ -24,7 +24,7 @@ public void TestFilesHaveUniqueIdsOverMultipleModules()
{
CoverageResult result = new CoverageResult();
result.Identifier = Guid.NewGuid().ToString();

result.Modules = new Modules();
result.Modules.Add("Coverlet.Core.Reporters.Tests", CreateFirstDocuments());
result.Modules.Add("Some.Other.Module", CreateSecondDocuments());
Expand All @@ -42,25 +42,28 @@ private static Documents CreateFirstDocuments()
Lines lines = new Lines();
lines.Add(1, 1);
lines.Add(2, 0);

Branches branches = new Branches();
branches.Add(1, new List<BranchInfo> {
new BranchInfo{ Hits = 1, Offset = 23, EndOffset = 24, Path = 0, Ordinal = 1 },
new BranchInfo{ Hits = 0, Offset = 23, EndOffset = 27, Path = 1, Ordinal = 2 }
});
branches.Add(new BranchInfo { Line = 1, Hits = 1, Offset = 23, EndOffset = 24, Path = 0, Ordinal = 1 });
branches.Add(new BranchInfo { Line = 1, Hits = 0, Offset = 23, EndOffset = 27, Path = 1, Ordinal = 2 });

Methods methods = new Methods();
var methodString = "System.Void Coverlet.Core.Reporters.Tests.OpenCoverReporterTests.TestReport()";
methods.Add(methodString, new Method());
methods[methodString].Lines = lines;
methods[methodString].Branches = branches;

Classes classes = new Classes();
classes.Add("Coverlet.Core.Reporters.Tests.OpenCoverReporterTests", methods);

Documents documents = new Documents();
documents.Add("doc.cs", classes);

return documents;
}

private static Documents CreateSecondDocuments()
{
{
Lines lines = new Lines();
lines.Add(1, 1);
lines.Add(2, 0);
Expand Down

0 comments on commit 4f220f5

Please sign in to comment.