Skip to content

Commit

Permalink
Added some capabilities to package utilities (#2854) (#2862)
Browse files Browse the repository at this point in the history
* Hierarchy output is added to ManagedName APIs.
  • Loading branch information
Haplois committed Apr 21, 2021
1 parent 8a8fd6b commit 45fcca5
Show file tree
Hide file tree
Showing 7 changed files with 755 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/Microsoft.TestPlatform.AdapterUtilities/Friends.cs
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.TestPlatform.AdapterUtilities.UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
Expand Up @@ -26,7 +26,7 @@ public static class Levels
/// <summary>
/// Total length of Hierarchy array.
/// </summary>
public const int TotalLevelCount = 4;
public const int TotalLevelCount = 2;

/// <summary>
/// Index of the namespace element of the array.
Expand All @@ -37,16 +37,6 @@ public static class Levels
/// Index of the class element of the array.
/// </summary>
public const int ClassIndex = 1;

/// <summary>
/// Index of the test group element of the array.
/// </summary>
public const int TestGroupIndex = 2;

/// <summary>
/// Index of the display name element of the array.
/// </summary>
public const int DisplayNameIndex = 3;
}
}
}
Expand Up @@ -47,6 +47,42 @@ public static partial class ManagedNameHelper
/// <see href="https://github.com/microsoft/vstest-docs/blob/master/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.
/// </remarks>
public static void GetManagedName(MethodBase method, out string managedTypeName, out string managedMethodName)
=> GetManagedName(method, out managedTypeName, out managedMethodName, out _);

/// <summary>
/// Gets fully qualified managed type and method name from given <see href="MethodBase" /> instance.
/// </summary>
/// <param name="method">
/// A <see cref="MethodBase" /> instance to get fully qualified managed type and method name.
/// </param>
/// <param name="managedTypeName">
/// When this method returns, contains the fully qualified managed type name of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// 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>.
/// </param>
/// <param name="managedMethodName">
/// When this method returns, contains the fully qualified managed method name of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// 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>.
/// </param>
/// <param name="hierarchyValues">
/// When this method returns, contains the default test hierarchy values of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="method"/> is null.
/// </exception>
/// <exception cref="NotSupportedException">
/// <paramref name="method"/> must describe a method.
/// </exception>
/// <exception cref="NotImplementedException">
/// Required functionality on <paramref name="method"/> is missing on the current platform.
/// </exception>
/// <remarks>
/// More information about <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/> can be found in
/// <see href="https://github.com/microsoft/vstest-docs/blob/master/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.
/// </remarks>
public static void GetManagedName(MethodBase method, out string managedTypeName, out string managedMethodName, out string[] hierarchyValues)
{
if (method == null)
{
Expand Down Expand Up @@ -84,7 +120,7 @@ public static void GetManagedName(MethodBase method, out string managedTypeName,
var methodBuilder = new StringBuilder();

// Namespace and Type Name (with arity designation)
AppendTypeString(typeBuilder, semanticType, closedType: false);
var hierarchyPos = AppendTypeString(typeBuilder, semanticType, closedType: false);

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

managedTypeName = typeBuilder.ToString();
managedMethodName = methodBuilder.ToString();
hierarchyValues = new[] {
managedTypeName.Substring(hierarchyPos[0], hierarchyPos[1] - hierarchyPos[0]),
managedTypeName.Substring(hierarchyPos[1] + 1, hierarchyPos[2] - hierarchyPos[1] - 1),
};
}

/// <summary>
Expand Down Expand Up @@ -233,11 +273,13 @@ bool filter(MemberInfo mbr, object param)
#endif
}

private static void AppendTypeString(StringBuilder b, Type type, bool closedType)
private static int[] AppendTypeString(StringBuilder b, Type type, bool closedType)
{
int[] hierarchies = null;

if (type.IsArray)
{
AppendTypeString(b, type.GetElementType(), closedType);
hierarchies = AppendTypeString(b, type.GetElementType(), closedType);
b.Append('[');
for (int i = 0; i < type.GetArrayRank() - 1; i++)
{
Expand All @@ -256,16 +298,23 @@ private static void AppendTypeString(StringBuilder b, Type type, bool closedType
}
else
{
hierarchies = new int[3];
hierarchies[0] = b.Length;

AppendNamespace(b, type.Namespace);
hierarchies[1] = b.Length;

b.Append('.');

AppendNestedTypeName(b, type);

if (closedType)
{
AppendGenericTypeParameters(b, type);
}
hierarchies[2] = b.Length;
}

return hierarchies;
}

private static void AppendNamespace(StringBuilder b, string namespaceString)
Expand Down

0 comments on commit 45fcca5

Please sign in to comment.