Skip to content

Commit

Permalink
More resilient approach in GetTypeFullyQualifiedName()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Aug 24, 2023
1 parent bcf52ac commit ce67785
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions GitHubActionsTestLogger/Utils/Extensions/TestCaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@ namespace GitHubActionsTestLogger.Utils.Extensions;
internal static class TestCaseExtensions
{
public static string GetTypeFullyQualifiedName(this TestCase testCase) =>
testCase.FullyQualifiedName.SubstringUntilLast(".", StringComparison.OrdinalIgnoreCase);
testCase.FullyQualifiedName
// Strip the test cases (if this is a parameterized test method)
.SubstringUntil("(", StringComparison.OrdinalIgnoreCase)
// Strip everything after the last dot, to leave the full type name
.SubstringUntilLast(".", StringComparison.OrdinalIgnoreCase);

public static string GetTypeMinimallyQualifiedName(this TestCase testCase)
{
var ns = Path.GetFileNameWithoutExtension(testCase.Source);
var fullyQualifiedName = testCase.GetTypeFullyQualifiedName();

return fullyQualifiedName.StartsWith(ns + '.', StringComparison.Ordinal)
? fullyQualifiedName[(ns.Length + 1)..]
: fullyQualifiedName;
// We assume that the test assembly name matches the namespace.
// This is not always true, but it's the best we can do.
var nameSpace = Path.GetFileNameWithoutExtension(testCase.Source);

// Strip the namespace from the type name, if it's there
if (fullyQualifiedName.StartsWith(nameSpace + '.', StringComparison.Ordinal))
return fullyQualifiedName[(nameSpace.Length + 1)..];

return fullyQualifiedName;
}

public static string GetMinimallyQualifiedName(this TestCase testCase) =>
testCase.FullyQualifiedName.SubstringAfterLast(".", StringComparison.OrdinalIgnoreCase);
public static string GetMinimallyQualifiedName(this TestCase testCase)
{
var fullyQualifiedName = testCase.GetTypeFullyQualifiedName();

// Strip the full type name from the test method name, if it's there
return testCase.FullyQualifiedName.StartsWith(fullyQualifiedName, StringComparison.Ordinal)
? testCase.FullyQualifiedName[(fullyQualifiedName.Length + 1)..]
: testCase.FullyQualifiedName;
}
}

0 comments on commit ce67785

Please sign in to comment.