Skip to content

Commit

Permalink
Fixed testcase names with . in arguments (#111)
Browse files Browse the repository at this point in the history
* Moved StripTypeInfo from StringExtensions to TypeInfoHelper

* Added tests

* Fix
  • Loading branch information
gfoidl committed Aug 26, 2022
1 parent 4a9f7a5 commit 792eebc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
22 changes: 0 additions & 22 deletions source/trx2junit.Core/Extensions/StringExtensions.cs

This file was deleted.

64 changes: 64 additions & 0 deletions source/trx2junit.Core/Internal/TypeInfoHelper.cs
Original file line number Diff line number Diff line change
@@ -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<char> span = name;

int parenthesisIndex = span.IndexOf('(');
if (parenthesisIndex < 0)
{
return StripTypeInfo(span).ToString();
}

ReadOnlySpan<char> preParenthesis = span.Slice(0, parenthesisIndex);
ReadOnlySpan<char> 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<char> s0 = new(state.Item1.ToPointer(), state.Item2);
ReadOnlySpan<char> s1 = new(state.Item3.ToPointer(), state.Item4);
s0.CopyTo(buffer);
s1.CopyTo(buffer.Slice(s0.Length));
}
);
}
}
#endif
}
//-------------------------------------------------------------------------
private static ReadOnlySpan<char> StripTypeInfo(ReadOnlySpan<char> name)
{
int idx = name.LastIndexOf('.');
if (idx < 0)
{
return name;
}

return name.Slice(idx + 1);
}
}
1 change: 1 addition & 0 deletions source/trx2junit.Core/trx2junit.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
<RootNamespace>gfoidl.Trx2Junit.Core</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,11 +21,17 @@ private static IEnumerable<TestCaseData> 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)");
}
}

0 comments on commit 792eebc

Please sign in to comment.