diff --git a/source/trx2junit.Core/Extensions/StringExtensions.cs b/source/trx2junit.Core/Extensions/StringExtensions.cs deleted file mode 100644 index 99e5f3e..0000000 --- a/source/trx2junit.Core/Extensions/StringExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -// (c) gfoidl, all rights reserved - -using System.Diagnostics.CodeAnalysis; - -namespace gfoidl.Trx2Junit.Core; - -internal static class StringExtensions -{ - [return: NotNullIfNotNull("name")] - public static string? StripTypeInfo(this string? name) - { - if (name is null) return null; - - int idx = name.LastIndexOf('.'); - if (idx < 0) - { - return name; - } - - return name[(idx + 1)..]; - } -} diff --git a/source/trx2junit.Core/Internal/TypeInfoHelper.cs b/source/trx2junit.Core/Internal/TypeInfoHelper.cs new file mode 100644 index 0000000..2077403 --- /dev/null +++ b/source/trx2junit.Core/Internal/TypeInfoHelper.cs @@ -0,0 +1,64 @@ +// (c) gfoidl, all rights reserved + +using System; +using System.Diagnostics.CodeAnalysis; + +namespace gfoidl.Trx2Junit.Core; + +internal static class TypeInfoHelper +{ + [return: NotNullIfNotNull("name")] + public static string? StripTypeInfo(this string? name) + { + if (name is null) return null; + + ReadOnlySpan span = name; + + int parenthesisIndex = span.IndexOf('('); + if (parenthesisIndex < 0) + { + return StripTypeInfo(span).ToString(); + } + + ReadOnlySpan preParenthesis = span.Slice(0, parenthesisIndex); + ReadOnlySpan parenthisContent = span.Slice(parenthesisIndex); + + preParenthesis = StripTypeInfo(preParenthesis); + +#if NET6_0_OR_GREATER + return string.Concat(preParenthesis, parenthisContent); +#else + int finalLength = preParenthesis.Length + parenthisContent.Length; + unsafe + { + fixed (char* ptr0 = preParenthesis) + fixed (char* ptr1 = parenthisContent) + { + return string.Create( + finalLength, + ((IntPtr)ptr0, preParenthesis.Length, (IntPtr)ptr1, parenthisContent.Length), + static (buffer, state) => + { + ReadOnlySpan s0 = new(state.Item1.ToPointer(), state.Item2); + ReadOnlySpan s1 = new(state.Item3.ToPointer(), state.Item4); + + s0.CopyTo(buffer); + s1.CopyTo(buffer.Slice(s0.Length)); + } + ); + } + } +#endif + } + //------------------------------------------------------------------------- + private static ReadOnlySpan StripTypeInfo(ReadOnlySpan name) + { + int idx = name.LastIndexOf('.'); + if (idx < 0) + { + return name; + } + + return name.Slice(idx + 1); + } +} diff --git a/source/trx2junit.Core/trx2junit.Core.csproj b/source/trx2junit.Core/trx2junit.Core.csproj index 9187317..6d071f9 100644 --- a/source/trx2junit.Core/trx2junit.Core.csproj +++ b/source/trx2junit.Core/trx2junit.Core.csproj @@ -3,6 +3,7 @@ net6.0;netstandard2.1 gfoidl.Trx2Junit.Core + true diff --git a/tests/trx2junit.Core.Tests/Extensions/StringExtensionsTests/StripTypeInfo.cs b/tests/trx2junit.Core.Tests/Internal/TypeInfoHelperTests/StripTypeInfo.cs similarity index 64% rename from tests/trx2junit.Core.Tests/Extensions/StringExtensionsTests/StripTypeInfo.cs rename to tests/trx2junit.Core.Tests/Internal/TypeInfoHelperTests/StripTypeInfo.cs index b6b4220..137eb1d 100644 --- a/tests/trx2junit.Core.Tests/Extensions/StringExtensionsTests/StripTypeInfo.cs +++ b/tests/trx2junit.Core.Tests/Internal/TypeInfoHelperTests/StripTypeInfo.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using NUnit.Framework; -namespace gfoidl.Trx2Junit.Core.Tests.Extensions.StringExtensionsTests; +namespace gfoidl.Trx2Junit.Core.Tests.Internal.TypeInfoHelperTests; [TestFixture] public class StripTypeInfo @@ -21,11 +21,17 @@ private static IEnumerable Name_given___correct_Name_returned_Test yield return new TestCaseData("Method1") .Returns("Method1"); yield return new TestCaseData("Method1(arg: { a = 3 })").Returns("Method1(arg: { a = 3 })"); + yield return new TestCaseData("Method1(3.14)") .Returns("Method1(3.14)"); + yield return new TestCaseData("Method1(3.14, 2.72)") .Returns("Method1(3.14, 2.72)"); yield return new TestCaseData("Class1.Method1") .Returns("Method1"); yield return new TestCaseData("Class1.Method1(arg: { a = 3 })").Returns("Method1(arg: { a = 3 })"); + yield return new TestCaseData("Class1.Method1(3.14)") .Returns("Method1(3.14)"); + yield return new TestCaseData("Class1.Method1(3.14, 2.72)") .Returns("Method1(3.14, 2.72)"); yield return new TestCaseData("SimpleUnitTest.Class1.Method1") .Returns("Method1"); yield return new TestCaseData("SimpleUnitTest.Class1.Method1(arg: { a = 3 })").Returns("Method1(arg: { a = 3 })"); + yield return new TestCaseData("SimpleUnitTest.Class1.Method1(3.14)") .Returns("Method1(3.14)"); + yield return new TestCaseData("SimpleUnitTest.Class1.Method1(3.14, 2.72)") .Returns("Method1(3.14, 2.72)"); } }