Skip to content

Commit

Permalink
fix (#686): Distinct Method Class in Cobertura Report#
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Jenni committed Aug 15, 2024
1 parent 8a3ad8e commit 6f851de
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public class CoberturaReportBuilder : IReportBuilder
private readonly Dictionary<string, XElement> packageElementsByName = new Dictionary<string, XElement>();

/// <summary>
/// Gets the type of the report.
/// Gets or sets the report context.
/// </summary>
/// <value>
/// The type of the report.
/// The report context.
/// </value>
public string ReportType => "Cobertura";
public IReportContext ReportContext { get; set; }

/// <summary>
/// Gets or sets the report context.
/// Gets the type of the report.
/// </summary>
/// <value>
/// The report context.
/// The type of the report.
/// </value>
public IReportContext ReportContext { get; set; }
public string ReportType => "Cobertura";

/// <summary>
/// Creates a class report.
Expand Down Expand Up @@ -75,30 +75,30 @@ public void CreateClassReport(Class @class, IEnumerable<FileAnalysis> fileAnalys
{
if (file.Path == fileAnalysis.Path)
{
foreach (var codeElement in file.CodeElements)
foreach (var codeElement in file.CodeElements.GroupBy(x => x.Name))
{
int index = codeElement.Name.LastIndexOf('(');
int index = codeElement.First().Name.LastIndexOf('(');

var methodLinesElement = new XElement("lines");

var methodElement = new XElement(
"method",
new XAttribute("name", index == -1 ? codeElement.Name : codeElement.Name.Substring(0, index)),
new XAttribute("signature", index == -1 ? string.Empty : codeElement.Name.Substring(index)),
new XAttribute("name", index == -1 ? codeElement.First().Name : codeElement.First().Name.Substring(0, index)),
new XAttribute("signature", index == -1 ? string.Empty : codeElement.First().Name.Substring(index)),
methodLinesElement);

this.AddLineElements(
methodLinesElement,
fileAnalysis.Lines.Skip(codeElement.FirstLine - 1).Take(codeElement.LastLine - codeElement.FirstLine + 1),
DistinctTouchedLines(fileAnalysis, codeElement),
out double methodLineRate,
out double methodBranchRate);

methodElement.Add(new XAttribute("line-rate", methodLineRate.ToString(CultureInfo.InvariantCulture)));
methodElement.Add(new XAttribute("branch-rate", methodBranchRate.ToString(CultureInfo.InvariantCulture)));

var methodMetrics = file.MethodMetrics
.FirstOrDefault(q => q.FullName == codeElement.FullName
&& q.Line == codeElement.FirstLine);
.FirstOrDefault(q => q.FullName == codeElement.First().FullName
&& q.Line == codeElement.First().FirstLine);

if (methodMetrics != null)
{
Expand Down Expand Up @@ -266,6 +266,17 @@ public void CreateSummaryReport(SummaryResult summaryResult)
}
}

/// <summary>
/// Merge touched lines from multiple coverage sources
/// </summary>
private IEnumerable<LineAnalysis> DistinctTouchedLines(FileAnalysis fileAnalysis, IGrouping<string, CodeElement> codeElement)
{
return codeElement.SelectMany(element => fileAnalysis.Lines.Skip(element.FirstLine - 1)
.Take(element.LastLine - element.FirstLine + 1))
.DistinctBy(x => x.LineNumber)
.OrderBy(x => x.LineNumber);
}

/// <summary>
/// Adds the lines to the given parent element.
/// </summary>
Expand Down Expand Up @@ -324,4 +335,4 @@ private void AddLineElements(XElement parent, IEnumerable<LineAnalysis> lines, o
branchRate = totalBranches == 0 ? 1 : coveredBranches / (double)totalBranches;
}
}
}
}

0 comments on commit 6f851de

Please sign in to comment.