Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions TUnit.Engine.Tests/MetadataTypeNameFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Shouldly;
using TUnit.Engine.Helpers;

namespace TUnit.Engine.Tests;

public class MetadataTypeNameFormatterTests
{
[Test]
[Arguments(typeof(string), "System.String")]
[Arguments(typeof(void), "System.Void")]
[Arguments(typeof(int[]), "System.Int32[]")]
[Arguments(typeof(int[][]), "System.Int32[][]")]
[Arguments(typeof(int[,]), "System.Int32[,]")]
[Arguments(typeof(List<string>), "System.Collections.Generic.List`1<System.String>")]
[Arguments(typeof(List<>), "System.Collections.Generic.List`1")]
[Arguments(typeof(Dictionary<string, List<int>>), "System.Collections.Generic.Dictionary`2<System.String,System.Collections.Generic.List`1<System.Int32>>")]
[Arguments(typeof(Task<int>), "System.Threading.Tasks.Task`1<System.Int32>")]
[Arguments(typeof(int?), "System.Nullable`1<System.Int32>")]
[Arguments(typeof(Outer.Inner), "TUnit.Engine.Tests.MetadataTypeNameFormatterTests+Outer+Inner")]
[Arguments(typeof(Outer.GenericInner<string>), "TUnit.Engine.Tests.MetadataTypeNameFormatterTests+Outer+GenericInner`1<System.String>")]
public void Formats_Types_In_Metadata_Format(Type type, string expected)
{
MetadataTypeNameFormatter.GetMetadataFullName(type).ShouldBe(expected);
}

[Test]
public void Formats_Generic_Method_Parameter_As_DoubleBang_Position()
{
var method = typeof(GenericMembers).GetMethod(nameof(GenericMembers.MethodWithGenericParameters))!;
var parameters = method.GetParameters();

MetadataTypeNameFormatter.GetMetadataFullName(parameters[0].ParameterType).ShouldBe("!!0");
MetadataTypeNameFormatter.GetMetadataFullName(parameters[1].ParameterType).ShouldBe("!!1[]");
MetadataTypeNameFormatter.GetMetadataFullName(parameters[2].ParameterType).ShouldBe("System.Collections.Generic.List`1<!!0>");
}

[Test]
public void Formats_Generic_Type_Parameter_As_SingleBang_Position()
{
var typeParameter = typeof(Dictionary<,>).GetGenericArguments()[1];

MetadataTypeNameFormatter.GetMetadataFullName(typeParameter).ShouldBe("!1");
}

[Test]
public void Formats_ByRef_Parameter_With_Ampersand()
{
var method = typeof(GenericMembers).GetMethod(nameof(GenericMembers.MethodWithRefParameter))!;
var parameterType = method.GetParameters()[0].ParameterType;

MetadataTypeNameFormatter.GetMetadataFullName(parameterType).ShouldBe("System.Int32&");
}

public static class Outer
{
public class Inner;

public class GenericInner<T>;
}

public static class GenericMembers
{
public static void MethodWithGenericParameters<T1, T2>(T1 first, T2[] second, List<T1> third)
{
}

public static void MethodWithRefParameter(ref int value)
{
}
}
}
5 changes: 3 additions & 2 deletions TUnit.Engine/Extensions/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using TUnit.Core;
using TUnit.Core.Extensions;
using TUnit.Engine.Capabilities;
using TUnit.Engine.Helpers;
using TUnit.Engine.Reporters;
#pragma warning disable TPEXP

Expand Down Expand Up @@ -61,7 +62,7 @@ private static CachedTestNodeProperties GetOrCreateCachedProperties(TestContext
typeName: testContext.GetClassTypeName(),
methodName: testDetails.MethodName,
parameterTypeFullNames: CreateParameterTypeArray(testDetails.MethodMetadata.Parameters),
returnTypeFullName: testDetails.ReturnType.FullName ?? typeof(void).FullName!,
returnTypeFullName: MetadataTypeNameFormatter.GetMetadataFullName(testDetails.ReturnType),
methodArity: testDetails.MethodMetadata.GenericTypeCount
);

Expand Down Expand Up @@ -354,7 +355,7 @@ private static string[] CreateParameterTypeArray(ParameterMetadata[] parameters)
var array = new string[parameters.Length];
for (var i = 0; i < parameters.Length; i++)
{
array[i] = parameters[i].Type.FullName!;
array[i] = MetadataTypeNameFormatter.GetMetadataFullName(parameters[i].Type);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use TypeInfo for source-gen generic parameters

In source-generated metadata, parameters that depend on generic arguments are stored with Type == typeof(object) and the real signature in TypeInfo (for example the generated snapshots create IEnumerable<TSource> as typeof(object) plus ConstructedGeneric). Because this new formatting path only reads parameters[i].Type, source-gen generic tests still publish System.Object instead of metadata names like !!0 or System.Collections.Generic.IEnumerable1<!!0>`, while reflection mode now publishes the correct placeholders; that leaves MTP method identity/source navigation wrong for those generic tests and violates the dual-mode requirement.

Useful? React with 👍 / 👎.

}
return array;
}
Expand Down
89 changes: 89 additions & 0 deletions TUnit.Engine/Helpers/MetadataTypeNameFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Collections.Concurrent;
using System.Text;
using TUnit.Core.Helpers;

namespace TUnit.Engine.Helpers;

/// <summary>
/// Formats <see cref="Type"/> instances as ECMA-335 metadata-format full names, as required by
/// <c>TestMethodIdentifierProperty</c> in Microsoft.Testing.Platform. This matches the managed
/// name format (vstest RFC 0017) that platform consumers parse: constructed generics as
/// <c>List`1&lt;System.String&gt;</c>, generic method parameters as <c>!!0</c>, generic type
/// parameters as <c>!0</c>, and nested types separated by <c>+</c>.
/// </summary>
internal static class MetadataTypeNameFormatter
{
private static readonly ConcurrentDictionary<Type, string> Cache = new();

public static string GetMetadataFullName(Type type)
{
return Cache.GetOrAdd(type, static t =>
{
var builder = StringBuilderPool.Get();
try
{
AppendMetadataName(builder, t);
return builder.ToString();
}
finally
{
StringBuilderPool.Return(builder);
}
});
}

private static void AppendMetadataName(StringBuilder builder, Type type)
{
if (type.IsGenericParameter)
{
builder.Append(type.DeclaringMethod is null ? "!" : "!!");
builder.Append(type.GenericParameterPosition);
return;
}

if (type.HasElementType)
{
AppendMetadataName(builder, type.GetElementType()!);

if (type.IsArray)
{
builder.Append('[');
builder.Append(',', type.GetArrayRank() - 1);
builder.Append(']');
}
else if (type.IsPointer)
{
builder.Append('*');
}
else if (type.IsByRef)
{
builder.Append('&');
}

return;
}

if (type.IsGenericType && !type.IsGenericTypeDefinition)
{
AppendMetadataName(builder, type.GetGenericTypeDefinition());

builder.Append('<');
var genericArguments = type.GetGenericArguments();
for (var i = 0; i < genericArguments.Length; i++)
{
if (i > 0)
{
builder.Append(',');
}

AppendMetadataName(builder, genericArguments[i]);
}
builder.Append('>');
return;
}

// Non-generic types and generic type definitions: FullName is already metadata format
// (namespace-qualified, '+' for nested types, backtick arity for generics).
builder.Append(type.FullName ?? type.Name);
}
}
Loading