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

Distinct Method Class in Cobertura Report #688

Closed
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
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;
}
}
}
}
Loading