Skip to content

Commit

Permalink
OpenCover: Improved handling of generic classes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpalme committed Sep 4, 2023
1 parent a170c5d commit c5ad502
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ For further details take a look at LICENSE.txt.

CHANGELOG

5.1.25.0

* Fix: OpenCover: Improved handling of generic classes

5.1.24.0

* Fix: #612 Increase percentage accuracy in reports
Expand Down
37 changes: 34 additions & 3 deletions src/ReportGenerator.Core/Parser/OpenCoverParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ internal class OpenCoverParser : ParserBase
/// </summary>
private static readonly ILogger Logger = LoggerFactory.GetLogger(typeof(OpenCoverParser));

/// <summary>
/// Regex to analyze if a class name represents a generic class.
/// </summary>
private static readonly Regex GenericClassRegex = new Regex("<.*>$", RegexOptions.Compiled);

/// <summary>
/// Regex to analyze if a class name represents an async (generic) class.
/// Format gets generated by 'dotnet test --collect "Code Coverage;Format=Cobertura"'.
/// </summary>
private static readonly Regex AsyncClassRegex = new Regex("^(?<ClassName>.+)\\.<.*>.*__(?:.+(?<GenericTypes><.+>))?", RegexOptions.Compiled);

/// <summary>
/// Regex to analyze if a method name belongs to a lamda expression.
/// </summary>
Expand Down Expand Up @@ -144,9 +155,28 @@ private Assembly ProcessAssembly(IDictionary<string, XElement[]> assemblyModules
{
string fullname = c.Element("FullName").Value;
int nestedClassSeparatorIndex = fullname.IndexOf('/');
return nestedClassSeparatorIndex > -1 ? fullname.Substring(0, nestedClassSeparatorIndex) : fullname;
if (nestedClassSeparatorIndex > -1)
{
string className = fullname.Substring(0, nestedClassSeparatorIndex);
return Tuple.Create(className, className);
}
if (fullname.Contains("<"))
{
var match = AsyncClassRegex.Match(fullname);
if (match.Success)
{
return Tuple.Create(
match.Groups["ClassName"].Value,
match.Groups["ClassName"].Value + match.Groups["GenericTypes"].Value);
}
}
return Tuple.Create(fullname, fullname);
})
.Where(name => name.IndexOf("<") < 1)
.Where(c => !c.Item1.Contains("<") || GenericClassRegex.IsMatch(c.Item1))
.Select(i => i.Item1)
.Distinct()
.Where(c => this.ClassFilter.IsElementIncludedInReport(c))
.OrderBy(name => name)
Expand Down Expand Up @@ -174,7 +204,8 @@ private void ProcessClass(IDictionary<string, XElement[]> assemblyModules, XElem
.Elements("Classes")
.Elements("Class")
.Where(c => c.Element("FullName").Value.Equals(className)
|| c.Element("FullName").Value.StartsWith(className + "/", StringComparison.Ordinal))
|| c.Element("FullName").Value.StartsWith(className + "/", StringComparison.Ordinal)
|| c.Element("FullName").Value.StartsWith(className + ".", StringComparison.Ordinal))
.Elements("Methods")
.Elements("Method")
.Where(m => m.Attribute("skippedDueTo") == null)
Expand Down

0 comments on commit c5ad502

Please sign in to comment.