Skip to content

Commit 45fcca5

Browse files
authored
Added some capabilities to package utilities (#2854) (#2862)
* Hierarchy output is added to ManagedName APIs.
1 parent 8a8fd6b commit 45fcca5

File tree

7 files changed

+755
-16
lines changed

7 files changed

+755
-16
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("Microsoft.TestPlatform.AdapterUtilities.UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]

src/Microsoft.TestPlatform.AdapterUtilities/HierarchyConstants.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class Levels
2626
/// <summary>
2727
/// Total length of Hierarchy array.
2828
/// </summary>
29-
public const int TotalLevelCount = 4;
29+
public const int TotalLevelCount = 2;
3030

3131
/// <summary>
3232
/// Index of the namespace element of the array.
@@ -37,16 +37,6 @@ public static class Levels
3737
/// Index of the class element of the array.
3838
/// </summary>
3939
public const int ClassIndex = 1;
40-
41-
/// <summary>
42-
/// Index of the test group element of the array.
43-
/// </summary>
44-
public const int TestGroupIndex = 2;
45-
46-
/// <summary>
47-
/// Index of the display name element of the array.
48-
/// </summary>
49-
public const int DisplayNameIndex = 3;
5040
}
5141
}
5242
}

src/Microsoft.TestPlatform.AdapterUtilities/ManagedNameUtilities/ManagedNameHelper.Reflection.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,42 @@ public static partial class ManagedNameHelper
4747
/// <see href="https://github.com/microsoft/vstest-docs/blob/master/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.
4848
/// </remarks>
4949
public static void GetManagedName(MethodBase method, out string managedTypeName, out string managedMethodName)
50+
=> GetManagedName(method, out managedTypeName, out managedMethodName, out _);
51+
52+
/// <summary>
53+
/// Gets fully qualified managed type and method name from given <see href="MethodBase" /> instance.
54+
/// </summary>
55+
/// <param name="method">
56+
/// A <see cref="MethodBase" /> instance to get fully qualified managed type and method name.
57+
/// </param>
58+
/// <param name="managedTypeName">
59+
/// When this method returns, contains the fully qualified managed type name of the <paramref name="method"/>.
60+
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
61+
/// The format is defined in <see href="https://github.com/microsoft/vstest-docs/blob/master/RFCs/0017-Managed-TestCase-Properties.md#managedtype-property">the RFC</see>.
62+
/// </param>
63+
/// <param name="managedMethodName">
64+
/// When this method returns, contains the fully qualified managed method name of the <paramref name="method"/>.
65+
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
66+
/// The format is defined in <see href="https://github.com/microsoft/vstest-docs/blob/master/RFCs/0017-Managed-TestCase-Properties.md#managedmethod-property">the RFC</see>.
67+
/// </param>
68+
/// <param name="hierarchyValues">
69+
/// When this method returns, contains the default test hierarchy values of the <paramref name="method"/>.
70+
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
71+
/// </param>
72+
/// <exception cref="ArgumentNullException">
73+
/// <paramref name="method"/> is null.
74+
/// </exception>
75+
/// <exception cref="NotSupportedException">
76+
/// <paramref name="method"/> must describe a method.
77+
/// </exception>
78+
/// <exception cref="NotImplementedException">
79+
/// Required functionality on <paramref name="method"/> is missing on the current platform.
80+
/// </exception>
81+
/// <remarks>
82+
/// More information about <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/> can be found in
83+
/// <see href="https://github.com/microsoft/vstest-docs/blob/master/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.
84+
/// </remarks>
85+
public static void GetManagedName(MethodBase method, out string managedTypeName, out string managedMethodName, out string[] hierarchyValues)
5086
{
5187
if (method == null)
5288
{
@@ -84,7 +120,7 @@ public static void GetManagedName(MethodBase method, out string managedTypeName,
84120
var methodBuilder = new StringBuilder();
85121

86122
// Namespace and Type Name (with arity designation)
87-
AppendTypeString(typeBuilder, semanticType, closedType: false);
123+
var hierarchyPos = AppendTypeString(typeBuilder, semanticType, closedType: false);
88124

89125
// Method Name with method arity
90126
var arity = method.GetGenericArguments().Length;
@@ -111,6 +147,10 @@ public static void GetManagedName(MethodBase method, out string managedTypeName,
111147

112148
managedTypeName = typeBuilder.ToString();
113149
managedMethodName = methodBuilder.ToString();
150+
hierarchyValues = new[] {
151+
managedTypeName.Substring(hierarchyPos[0], hierarchyPos[1] - hierarchyPos[0]),
152+
managedTypeName.Substring(hierarchyPos[1] + 1, hierarchyPos[2] - hierarchyPos[1] - 1),
153+
};
114154
}
115155

116156
/// <summary>
@@ -233,11 +273,13 @@ bool filter(MemberInfo mbr, object param)
233273
#endif
234274
}
235275

236-
private static void AppendTypeString(StringBuilder b, Type type, bool closedType)
276+
private static int[] AppendTypeString(StringBuilder b, Type type, bool closedType)
237277
{
278+
int[] hierarchies = null;
279+
238280
if (type.IsArray)
239281
{
240-
AppendTypeString(b, type.GetElementType(), closedType);
282+
hierarchies = AppendTypeString(b, type.GetElementType(), closedType);
241283
b.Append('[');
242284
for (int i = 0; i < type.GetArrayRank() - 1; i++)
243285
{
@@ -256,16 +298,23 @@ private static void AppendTypeString(StringBuilder b, Type type, bool closedType
256298
}
257299
else
258300
{
301+
hierarchies = new int[3];
302+
hierarchies[0] = b.Length;
303+
259304
AppendNamespace(b, type.Namespace);
305+
hierarchies[1] = b.Length;
306+
260307
b.Append('.');
261308

262309
AppendNestedTypeName(b, type);
263-
264310
if (closedType)
265311
{
266312
AppendGenericTypeParameters(b, type);
267313
}
314+
hierarchies[2] = b.Length;
268315
}
316+
317+
return hierarchies;
269318
}
270319

271320
private static void AppendNamespace(StringBuilder b, string namespaceString)

0 commit comments

Comments
 (0)