Skip to content

Commit

Permalink
Improved name of generic classes (coverlet-coverage/coverlet#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpalme committed Feb 10, 2021
1 parent 46ae4cc commit 4c0d2ed
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ For further details take a look at LICENSE.txt.

CHANGELOG

4.8.6.0

* New: Improved name of generic classes (https://github.com/coverlet-coverage/coverlet/issues/1077)

4.8.5.0

* Fix: #406: Fixed reference version of McMaster.NETCore.Plugins for package ReportGenerator.Core
Expand Down
47 changes: 47 additions & 0 deletions src/ReportGenerator.Core/Parser/Analysis/Class.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Palmmedia.ReportGenerator.Core.Parser.Analysis
{
Expand All @@ -9,6 +10,11 @@ namespace Palmmedia.ReportGenerator.Core.Parser.Analysis
/// </summary>
public class Class
{
/// <summary>
/// Regex to analyze if a class is generic.
/// </summary>
private static Regex genericClassRegex = new Regex("^(?<Name>.+)`(?<Number>\\d+)$", RegexOptions.Compiled);

/// <summary>
/// The object to lock the class add.
/// </summary>
Expand Down Expand Up @@ -38,6 +44,47 @@ internal Class(string name, Assembly assembly)
{
this.Name = name ?? throw new ArgumentNullException(nameof(name));
this.Assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));

/*
* Convert class name of generic classes:
* See: https://github.com/coverlet-coverage/coverlet/issues/1077
*
* SomeClass`1 -> SomeClass<T>
* SomeClass`2 -> SomeClass<T1, T2>
* SomeClass`3 -> SomeClass<T1, T2, T3>
*/
if (name.Contains("`"))
{
Match match = genericClassRegex.Match(name);

if (match.Success)
{
this.Name = match.Groups["Name"].Value;

int number = int.Parse(match.Groups["Number"].Value);

if (number == 1)
{
this.Name += "<T>";
}
else if (number > 1)
{
this.Name += "<";

for (int i = 1; i <= number; i++)
{
if (i > 1)
{
this.Name += ", ";
}

this.Name += "T" + i;
}

this.Name += ">";
}
}
}
}

/// <summary>
Expand Down

0 comments on commit 4c0d2ed

Please sign in to comment.