Skip to content

Commit 7bfaec5

Browse files
committed
ManagedNames impl. refactored.
- ManagedNames support in TestCase is updated to support the latest preview. - Updated TP to `16.9.0-preview-20210201-04`.
1 parent f55f5f2 commit 7bfaec5

File tree

26 files changed

+275
-79
lines changed

26 files changed

+275
-79
lines changed

scripts/build/TestFx.Versions.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<TestPlatformVersion Condition=" '$(TestPlatformVersion)' == '' ">16.9.0-preview-20210129-05</TestPlatformVersion>
4+
<TestPlatformVersion Condition=" '$(TestPlatformVersion)' == '' ">16.9.0-preview-20210201-04</TestPlatformVersion>
55
<MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
66
</PropertyGroup>
77
</Project>

src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ internal Collection<UnitTestElement> GetTests(ICollection<string> warnings)
127127
/// <returns> Returns a UnitTestElement.</returns>
128128
internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInTestTypeAssembly, ICollection<string> warnings)
129129
{
130+
// null if the current instance represents a generic type parameter.
130131
Debug.Assert(this.type.AssemblyQualifiedName != null, "AssemblyQualifiedName for method is null.");
131132

132133
// This allows void returning async test method to be valid test method. Though they will be executed similar to non-async test method.

src/Adapter/MSTest.CoreAdapter/Execution/TypeCache.cs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution
1212
using System.Reflection;
1313
using System.Security;
1414

15+
using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
1516
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery;
1617
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions;
1718
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
@@ -635,9 +636,47 @@ private TestMethodAttribute GetTestMethodAttribute(MethodInfo methodInfo, TestCl
635636
/// <returns> The <see cref="MethodInfo"/>. </returns>
636637
private MethodInfo GetMethodInfoForTestMethod(TestMethod testMethod, TestClassInfo testClassInfo)
637638
{
638-
var methodsInClass = testClassInfo.ClassType.GetRuntimeMethods().ToArray();
639639
MethodInfo testMethodInfo;
640640

641+
if (testMethod.HasManagedMethodAndType)
642+
{
643+
var assembly = testClassInfo.ClassType.GetTypeInfo().Assembly;
644+
var methodBase = ManagedNameHelper.GetMethod(assembly, testMethod.ManagedTypeName, testMethod.ManagedMethodName);
645+
646+
if (methodBase is MethodInfo mi)
647+
{
648+
testMethodInfo = mi;
649+
}
650+
else
651+
{
652+
var parameters = methodBase.GetParameters().Select(i => i.ParameterType).ToArray();
653+
testMethodInfo = methodBase.DeclaringType.GetRuntimeMethod(methodBase.Name, parameters);
654+
}
655+
656+
testMethodInfo = testMethodInfo.HasCorrectTestMethodSignature(true) ? testMethodInfo : null;
657+
}
658+
else
659+
{
660+
testMethodInfo = this.GetMethodInfoForLegacyTestMethod(testMethod, testClassInfo);
661+
}
662+
663+
// if correct method is not found, throw appropriate
664+
// exception about what is wrong.
665+
if (testMethodInfo == null)
666+
{
667+
var errorMessage = string.Format(CultureInfo.CurrentCulture, Resource.UTA_MethodDoesNotExists, testMethod.FullClassName, testMethod.Name);
668+
throw new TypeInspectionException(errorMessage);
669+
}
670+
671+
return testMethodInfo;
672+
}
673+
674+
private MethodInfo GetMethodInfoForLegacyTestMethod(TestMethod testMethod, TestClassInfo testClassInfo)
675+
{
676+
MethodInfo testMethodInfo;
677+
678+
var methodsInClass = testClassInfo.ClassType.GetRuntimeMethods().ToArray();
679+
641680
if (testMethod.DeclaringClassFullName != null)
642681
{
643682
// Only find methods that match the given declaring name.
@@ -657,14 +696,6 @@ private MethodInfo GetMethodInfoForTestMethod(TestMethod testMethod, TestClassIn
657696
.OrderByDescending(method => method.DeclaringType.FullName.Equals(className)).FirstOrDefault();
658697
}
659698

660-
// if correct method is not found, throw appropriate
661-
// exception about what is wrong.
662-
if (testMethodInfo == null)
663-
{
664-
var errorMessage = string.Format(CultureInfo.CurrentCulture, Resource.UTA_MethodDoesNotExists, testMethod.FullClassName, testMethod.Name);
665-
throw new TypeInspectionException(errorMessage);
666-
}
667-
668699
return testMethodInfo;
669700
}
670701

src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33

44
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions
55
{
6+
using Microsoft.TestPlatform.AdapterUtilities;
67
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
78
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
89

910
using Constants = Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Constants;
10-
using ManagedNameUtilities = Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
1111

1212
/// <summary>
1313
/// Extension Methods for TestCase Class
1414
/// </summary>
1515
internal static class TestCaseExtensions
1616
{
1717
internal static readonly TestProperty ManagedTypeProperty = TestProperty.Register(
18-
id: ManagedNameUtilities.Contants.ManagedTypePropertyId,
19-
label: ManagedNameUtilities.Contants.ManagedTypeLabel,
18+
id: ManagedNameConstants.ManagedTypePropertyId,
19+
label: ManagedNameConstants.ManagedTypeLabel,
2020
category: string.Empty,
2121
description: string.Empty,
2222
valueType: typeof(string),
@@ -25,8 +25,8 @@ internal static class TestCaseExtensions
2525
owner: typeof(TestCase));
2626

2727
internal static readonly TestProperty ManagedMethodProperty = TestProperty.Register(
28-
id: ManagedNameUtilities.Contants.ManagedMethodPropertyId,
29-
label: ManagedNameUtilities.Contants.ManagedMethodLabel,
28+
id: ManagedNameConstants.ManagedMethodPropertyId,
29+
label: ManagedNameConstants.ManagedMethodLabel,
3030
category: string.Empty,
3131
description: string.Empty,
3232
valueType: typeof(string),
@@ -53,7 +53,15 @@ internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string
5353
? fullyQualifiedName.Remove(0, $"{testClassName}.".Length)
5454
: fullyQualifiedName;
5555

56-
TestMethod testMethod = new TestMethod(name, testClassName, source, isAsync);
56+
TestMethod testMethod;
57+
if (testCase.ContainsManagedMethodAndType())
58+
{
59+
testMethod = new TestMethod(testCase.GetManagedType(), testCase.GetManagedMethod(), name, testClassName, source, isAsync);
60+
}
61+
else
62+
{
63+
testMethod = new TestMethod(name, testClassName, source, isAsync);
64+
}
5765

5866
if (declaringClassName != null && declaringClassName != testClassName)
5967
{

src/Adapter/MSTest.CoreAdapter/Friends.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
using System.Runtime.CompilerServices;
66

77
[assembly: InternalsVisibleTo("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
8-
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
8+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
9+
[assembly: InternalsVisibleTo("MSTestAdapter.Smoke.E2ETests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]

src/Adapter/MSTest.CoreAdapter/Helpers/ReflectHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers
1010
using System.Linq;
1111
using System.Reflection;
1212
using System.Security;
13+
1314
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
1415
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
1516
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

src/Adapter/MSTest.CoreAdapter/ObjectModel/TestMethod.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,15 @@ public TestMethod(MethodBase method, string name, string fullClassName, string a
5757

5858
ManagedNameHelper.GetManagedName(method, out var managedType, out var managedMethod);
5959

60-
this.ManagedType = managedType;
61-
this.ManagedMethod = managedMethod;
60+
this.ManagedTypeName = managedType;
61+
this.ManagedMethodName = managedMethod;
62+
}
63+
64+
internal TestMethod(string managedTypeName, string managedMethodName, string name, string fullClassName, string assemblyName, bool isAsync)
65+
: this(name, fullClassName, assemblyName, isAsync)
66+
{
67+
this.ManagedTypeName = managedTypeName;
68+
this.ManagedMethodName = managedMethodName;
6269
}
6370

6471
/// <summary>
@@ -120,13 +127,13 @@ public string DeclaringClassFullName
120127
/// </summary>
121128
public bool IsAsync { get; private set; }
122129

123-
public string ManagedType { get; }
130+
/// <inheritdoc />
131+
public string ManagedTypeName { get; }
124132

125-
public string ManagedMethod { get; }
133+
/// <inheritdoc />
134+
public string ManagedMethodName { get; }
126135

127-
/// <summary>
128-
/// Gets a value indicating whether both <see cref="ManagedType"/> and <see cref="ManagedMethod"/> are not null or whitespace.
129-
/// </summary>
130-
public bool HasManagedMethodAndType => !string.IsNullOrWhiteSpace(this.ManagedType) && !string.IsNullOrWhiteSpace(this.ManagedMethod);
136+
/// <inheritdoc />
137+
public bool HasManagedMethodAndType => !string.IsNullOrWhiteSpace(this.ManagedTypeName) && !string.IsNullOrWhiteSpace(this.ManagedMethodName);
131138
}
132139
}

src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,29 @@ public UnitTestElement(TestMethod testMethod)
109109
/// <returns> An instance of <see cref="TestCase"/>. </returns>
110110
internal TestCase ToTestCase()
111111
{
112-
var fullName = string.Format(
113-
CultureInfo.InvariantCulture,
114-
"{0}.{1}",
115-
this.TestMethod.FullClassName,
116-
this.TestMethod.Name);
112+
string fullName = this.TestMethod.HasManagedMethodAndType
113+
? string.Format(CultureInfo.InvariantCulture, "{0}.{1}", this.TestMethod.ManagedTypeName, this.TestMethod.ManagedMethodName)
114+
: string.Format(CultureInfo.InvariantCulture, "{0}.{1}", this.TestMethod.FullClassName, this.TestMethod.Name);
117115

118116
TestCase testCase = new TestCase(fullName, TestAdapter.Constants.ExecutorUri, this.TestMethod.AssemblyName);
117+
if (string.IsNullOrWhiteSpace(this.DisplayName))
118+
{
119+
testCase.DisplayName = string.IsNullOrWhiteSpace(this.TestMethod.ManagedMethodName) ? this.TestMethod.Name : this.TestMethod.ManagedMethodName;
120+
}
121+
else
122+
{
123+
testCase.DisplayName = this.DisplayName;
124+
}
119125

120-
testCase.DisplayName = string.IsNullOrEmpty(this.DisplayName) ? this.TestMethod.Name : this.DisplayName;
121-
testCase.SetPropertyValue(TestAdapter.Constants.TestClassNameProperty, this.TestMethod.FullClassName);
122126
if (this.TestMethod.HasManagedMethodAndType)
123127
{
124-
testCase.SetPropertyValue(TestCaseExtensions.ManagedTypeProperty, this.TestMethod.ManagedType);
125-
testCase.SetPropertyValue(TestCaseExtensions.ManagedMethodProperty, this.TestMethod.ManagedMethod);
128+
testCase.SetPropertyValue(TestCaseExtensions.ManagedTypeProperty, this.TestMethod.ManagedTypeName);
129+
testCase.SetPropertyValue(TestCaseExtensions.ManagedMethodProperty, this.TestMethod.ManagedMethodName);
130+
testCase.SetPropertyValue(TestAdapter.Constants.TestClassNameProperty, this.TestMethod.ManagedTypeName);
131+
}
132+
else
133+
{
134+
testCase.SetPropertyValue(TestAdapter.Constants.TestClassNameProperty, this.TestMethod.FullClassName);
126135
}
127136

128137
// Set declaring type if present so the correct method info can be retrieved
@@ -183,9 +192,7 @@ internal TestCase ToTestCase()
183192
// Set the Do not parallelize state if present
184193
if (this.DoNotParallelize)
185194
{
186-
testCase.SetPropertyValue(
187-
TestAdapter.Constants.DoNotParallelizeProperty,
188-
this.DoNotParallelize);
195+
testCase.SetPropertyValue(TestAdapter.Constants.DoNotParallelizeProperty, this.DoNotParallelize);
189196
}
190197

191198
return testCase;

src/Adapter/PlatformServices.Desktop/Services/DesktopTestContextImplementation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ private string GetStringPropertyValue(string propertyName)
501501
private void InitializeProperties()
502502
{
503503
this.properties[TestContextPropertyStrings.FullyQualifiedTestClassName] = this.testMethod.FullClassName;
504-
this.properties[TestContextPropertyStrings.ManagedType] = this.testMethod.ManagedType;
505-
this.properties[TestContextPropertyStrings.ManagedMethod] = this.testMethod.ManagedMethod;
504+
this.properties[TestContextPropertyStrings.ManagedType] = this.testMethod.ManagedTypeName;
505+
this.properties[TestContextPropertyStrings.ManagedMethod] = this.testMethod.ManagedMethodName;
506506
this.properties[TestContextPropertyStrings.TestName] = this.testMethod.Name;
507507
}
508508
}

src/Adapter/PlatformServices.Desktop/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="MicroBuild.Core" version="0.2.0" targetFramework="net451" developmentDependency="true" />
4-
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210129-05" targetFramework="net45" />
4+
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210201-04" targetFramework="net45" />
55
<package id="NuGet.Frameworks" version="5.0.0" targetFramework="net45" />
66
<package id="StyleCop.Analyzers" version="1.0.0" targetFramework="net45" developmentDependency="true" />
77
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net45" />

src/Adapter/PlatformServices.Interface/ObjectModel/ITestMethod.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ public interface ITestMethod
4040
/// <example>
4141
/// <code>NamespaceA.NamespaceB.ClassName`1+InnerClass`2</code>
4242
/// </example>
43-
string ManagedType { get; }
43+
string ManagedTypeName { get; }
4444

4545
/// <summary>
4646
/// Gets the fully specified method name metadata format.
4747
/// </summary>
4848
/// <example>
4949
/// <code>MethodName`2(ParamTypeA,ParamTypeB,…)</code>
5050
/// </example>
51-
string ManagedMethod { get; }
51+
string ManagedMethodName { get; }
52+
53+
/// <summary>
54+
/// Gets a value indicating whether both <see cref="ManagedTypeName"/> and <see cref="ManagedMethodName"/> are not null or whitespace.
55+
/// </summary>
56+
bool HasManagedMethodAndType { get; }
5257
}
5358
}

src/Adapter/PlatformServices.Interface/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="MicroBuild.Core" version="0.2.0" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
4-
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210129-05" targetFramework="portable45-net45+win8+wp8+wpa81" />
4+
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210201-04" targetFramework="portable45-net45+win8+wp8+wpa81" />
55
<package id="StyleCop.Analyzers" version="1.0.0" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
66
<package id="System.Collections" version="4.3.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
77
<package id="System.ComponentModel" version="4.3.0" targetFramework="portable45-net45+win8+wp8+wpa81" />

src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ private string GetStringPropertyValue(string propertyName)
446446
private void InitializeProperties()
447447
{
448448
this.properties[FullyQualifiedTestClassNameLabel] = this.testMethod.FullClassName;
449-
this.properties[ManagedTypeLabel] = this.testMethod.ManagedType;
450-
this.properties[ManagedMethodLabel] = this.testMethod.ManagedMethod;
449+
this.properties[ManagedTypeLabel] = this.testMethod.ManagedTypeName;
450+
this.properties[ManagedMethodLabel] = this.testMethod.ManagedMethodName;
451451
this.properties[TestNameLabel] = this.testMethod.Name;
452452
}
453453
}

src/Adapter/PlatformServices.Portable/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="MicroBuild.Core" version="0.2.0" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
4-
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210129-05" targetFramework="portable45-net45+win8+wp8+wpa81" />
4+
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210201-04" targetFramework="portable45-net45+win8+wp8+wpa81" />
55
<package id="StyleCop.Analyzers" version="1.0.0" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
66
<package id="System.Collections" version="4.3.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
77
<package id="System.ComponentModel" version="4.3.0" targetFramework="portable45-net45+win8+wp8+wpa81" />

src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ private object GetPropertyValue(string propertyName)
341341
private void InitializeProperties()
342342
{
343343
this.properties[FullyQualifiedTestClassNameLabel] = this.testMethod.FullClassName;
344-
this.properties[ManagedTypeLabel] = this.testMethod.ManagedType;
345-
this.properties[ManagedMethodLabel] = this.testMethod.ManagedMethod;
344+
this.properties[ManagedTypeLabel] = this.testMethod.ManagedTypeName;
345+
this.properties[ManagedMethodLabel] = this.testMethod.ManagedMethodName;
346346
this.properties[TestNameLabel] = this.testMethod.Name;
347347
}
348348
}

test/ComponentTests/PlatformServices.Desktop.Component.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Castle.Core" version="4.2.1" targetFramework="net451" />
4-
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210129-05" targetFramework="net451" />
4+
<package id="Microsoft.TestPlatform.ObjectModel" version="16.9.0-preview-20210201-04" targetFramework="net451" />
55
<package id="Moq" version="4.8.2" targetFramework="net451" />
66
<package id="NuGet.Frameworks" version="5.0.0" targetFramework="net451" />
77
<package id="StyleCop.Analyzers" version="1.0.0" targetFramework="net451" developmentDependency="true" />

0 commit comments

Comments
 (0)