diff --git a/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs index 2cc8842a..672b1ed0 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs @@ -29,20 +29,20 @@ public class CoberturaReportBuilder : IReportBuilder private readonly Dictionary packageElementsByName = new Dictionary(); /// - /// Gets the type of the report. + /// Gets or sets the report context. /// /// - /// The type of the report. + /// The report context. /// - public string ReportType => "Cobertura"; + public IReportContext ReportContext { get; set; } /// - /// Gets or sets the report context. + /// Gets the type of the report. /// /// - /// The report context. + /// The type of the report. /// - public IReportContext ReportContext { get; set; } + public string ReportType => "Cobertura"; /// /// Creates a class report. @@ -75,21 +75,21 @@ public void CreateClassReport(Class @class, IEnumerable 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); @@ -97,8 +97,8 @@ public void CreateClassReport(Class @class, IEnumerable fileAnalys 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) { @@ -266,6 +266,17 @@ public void CreateSummaryReport(SummaryResult summaryResult) } } + /// + /// Merge touched lines from multiple coverage sources + /// + private IEnumerable DistinctTouchedLines(FileAnalysis fileAnalysis, IGrouping 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); + } + /// /// Adds the lines to the given parent element. /// @@ -324,4 +335,4 @@ private void AddLineElements(XElement parent, IEnumerable lines, o branchRate = totalBranches == 0 ? 1 : coveredBranches / (double)totalBranches; } } -} +} \ No newline at end of file