-
-
Notifications
You must be signed in to change notification settings - Fork 128
fix(engine): emit ECMA-335 metadata-format type names in TestMethodIdentifierProperty #6433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+163
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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<System.String></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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In source-generated metadata, parameters that depend on generic arguments are stored with
Type == typeof(object)and the real signature inTypeInfo(for example the generated snapshots createIEnumerable<TSource>astypeof(object)plusConstructedGeneric). Because this new formatting path only readsparameters[i].Type, source-gen generic tests still publishSystem.Objectinstead of metadata names like!!0orSystem.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 👍 / 👎.