Skip to content
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

Simplify reflection #4702

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private static bool TryUnfoldITestDataSources(UnitTestElement test, TestMethodIn
// We don't have a special method to filter attributes that are not derived from Attribute, so we take all
// attributes and filter them. We don't have to care if there is one, because this method is only entered when
// there is at least one (we determine this in TypeEnumerator.GetTestFromMethod.
IEnumerable<ITestDataSource> testDataSources = ReflectHelper.Instance.GetDerivedAttributes<Attribute>(testMethodInfo.MethodInfo, inherit: false).OfType<ITestDataSource>();
IEnumerable<ITestDataSource> testDataSources = ReflectHelper.Instance.GetAttributes<Attribute>(testMethodInfo.MethodInfo, inherit: false).OfType<ITestDataSource>();

// We need to use a temporary list to avoid adding tests to the main list if we fail to expand any data source.
List<UnitTestElement> tempListOfTests = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal virtual bool IsValidTestMethod(MethodInfo testMethodInfo, Type type, IC
// but the difference is quite small, and we don't expect a huge amount of non-test methods in the assembly.
//
// Also skip all methods coming from object, because they cannot be tests.
if (testMethodInfo.DeclaringType == typeof(object) || !_reflectHelper.IsDerivedAttributeDefined<TestMethodAttribute>(testMethodInfo, inherit: false))
if (testMethodInfo.DeclaringType == typeof(object) || !_reflectHelper.IsAttributeDefined<TestMethodAttribute>(testMethodInfo, inherit: false))
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal virtual bool IsValidTestClass(Type type, List<string> warnings)
// gives us a better performance.
// It would be possible to use non-caching reflection here if we knew that we are only doing discovery that won't be followed by run,
// but the difference is quite small, and we don't expect a huge amount of non-test classes in the assembly.
if (!type.IsClass || !_reflectHelper.IsDerivedAttributeDefined<TestClassAttribute>(type, inherit: false))
if (!type.IsClass || !_reflectHelper.IsAttributeDefined<TestClassAttribute>(type, inherit: false))
{
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions;
Expand Down Expand Up @@ -411,7 +411,7 @@ internal UnitTestResult GetResultOrRunClassInitialize(ITestContext testContext,

DebugEx.Assert(!IsClassInitializeExecuted, "If class initialize was executed, we should have been in the previous if were we have a result available.");

bool isSTATestClass = AttributeComparer.IsDerived<STATestClassAttribute>(ClassAttribute);
bool isSTATestClass = ClassAttribute is STATestClassAttribute;
bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isSTATestClass
&& isWindowsOS
Expand Down Expand Up @@ -631,7 +631,7 @@ internal void ExecuteClassCleanup(TestContext testContext)
if (classCleanupMethod is not null)
{
if (ClassAttribute.IgnoreMessage is null &&
!ReflectHelper.Instance.IsNonDerivedAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
!ReflectHelper.Instance.IsAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
{
ClassCleanupException = InvokeCleanupMethod(classCleanupMethod, remainingCleanupCount: BaseClassCleanupMethods.Count, testContext);
}
Expand All @@ -643,7 +643,7 @@ internal void ExecuteClassCleanup(TestContext testContext)
{
classCleanupMethod = BaseClassCleanupMethods[i];
if (ClassAttribute.IgnoreMessage is null &&
!ReflectHelper.Instance.IsNonDerivedAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
!ReflectHelper.Instance.IsAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
{
ClassCleanupException = InvokeCleanupMethod(classCleanupMethod, remainingCleanupCount: BaseClassCleanupMethods.Count - 1 - i, testContext);
if (ClassCleanupException is not null)
Expand Down Expand Up @@ -711,7 +711,7 @@ internal void RunClassCleanup(ITestContext testContext, ClassCleanupManager clas
return;
}

bool isSTATestClass = AttributeComparer.IsDerived<STATestClassAttribute>(ClassAttribute);
bool isSTATestClass = ClassAttribute is STATestClassAttribute;
bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isSTATestClass
&& isWindowsOS
Expand Down
8 changes: 4 additions & 4 deletions src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ internal TestMethodInfo(

internal RetryBaseAttribute? RetryAttribute { get; }

public Attribute[]? GetAllAttributes(bool inherit) => ReflectHelper.Instance.GetDerivedAttributes<Attribute>(TestMethod, inherit).ToArray();
public Attribute[]? GetAllAttributes(bool inherit) => ReflectHelper.Instance.GetAttributes<Attribute>(TestMethod, inherit).ToArray();

public TAttributeType[] GetAttributes<TAttributeType>(bool inherit)
where TAttributeType : Attribute
=> ReflectHelper.Instance.GetDerivedAttributes<TAttributeType>(TestMethod, inherit).ToArray();
=> ReflectHelper.Instance.GetAttributes<TAttributeType>(TestMethod, inherit).ToArray();

/// <summary>
/// Execute test method. Capture failures, handle async and return result.
Expand Down Expand Up @@ -231,7 +231,7 @@ public virtual TestResult Invoke(object?[]? arguments)

try
{
expectedExceptions = ReflectHelper.Instance.GetDerivedAttributes<ExpectedExceptionBaseAttribute>(TestMethod, inherit: true);
expectedExceptions = ReflectHelper.Instance.GetAttributes<ExpectedExceptionBaseAttribute>(TestMethod, inherit: true);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -267,7 +267,7 @@ public virtual TestResult Invoke(object?[]? arguments)
/// </returns>
private RetryBaseAttribute? GetRetryAttribute()
{
IEnumerable<RetryBaseAttribute> attributes = ReflectHelper.Instance.GetDerivedAttributes<RetryBaseAttribute>(TestMethod, inherit: true);
IEnumerable<RetryBaseAttribute> attributes = ReflectHelper.Instance.GetAttributes<RetryBaseAttribute>(TestMethod, inherit: true);
using IEnumerator<RetryBaseAttribute> enumerator = attributes.GetEnumerator();
if (!enumerator.MoveNext())
{
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/MSTest.TestAdapter/Execution/TestMethodRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public TestMethodRunner(TestMethodInfo testMethodInfo, TestMethod testMethod, IT
/// <returns>The test results.</returns>
internal List<TestResult> Execute(string initializationLogs, string initializationErrorLogs, string initializationTrace, string initializationTestContextMessages)
{
bool isSTATestClass = AttributeComparer.IsDerived<STATestClassAttribute>(_testMethodInfo.Parent.ClassAttribute);
bool isSTATestMethod = AttributeComparer.IsDerived<STATestMethodAttribute>(_testMethodInfo.TestMethodOptions.Executor);
bool isSTATestClass = _testMethodInfo.Parent.ClassAttribute is STATestClassAttribute;
bool isSTATestMethod = _testMethodInfo.TestMethodOptions.Executor is STATestMethodAttribute;
bool isSTARequested = isSTATestClass || isSTATestMethod;
bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isSTARequested && isWindowsOS && Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
Expand Down
14 changes: 7 additions & 7 deletions src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@

private TimeoutInfo? TryGetTimeoutInfo(MethodInfo methodInfo, FixtureKind fixtureKind)
{
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);

Check failure on line 359 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L359

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(359,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 359 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L359

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(359,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 359 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L359

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(359,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 359 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L359

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(359,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 359 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L359

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(359,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)
if (timeoutAttribute != null)
{
if (!timeoutAttribute.HasCorrectTimeout)
Expand Down Expand Up @@ -397,7 +397,7 @@
try
{
// Only examine classes which are TestClass or derives from TestClass attribute
if (!_reflectionHelper.IsDerivedAttributeDefined<TestClassAttribute>(t, inherit: false))
if (!_reflectionHelper.IsAttributeDefined<TestClassAttribute>(t, inherit: false))
{
continue;
}
Expand Down Expand Up @@ -448,7 +448,7 @@
// {
// return false;
// }
if (!_reflectionHelper.IsNonDerivedAttributeDefined<TInitializeAttribute>(methodInfo, false))
if (!_reflectionHelper.IsAttributeDefined<TInitializeAttribute>(methodInfo, false))
{
return false;
}
Expand Down Expand Up @@ -476,7 +476,7 @@
// {
// return false;
// }
if (!_reflectionHelper.IsNonDerivedAttributeDefined<TCleanupAttribute>(methodInfo, false))
if (!_reflectionHelper.IsAttributeDefined<TCleanupAttribute>(methodInfo, false))
{
return false;
}
Expand Down Expand Up @@ -607,8 +607,8 @@
bool isBase,
Dictionary<string, string?> instanceMethods)
{
bool hasTestInitialize = _reflectionHelper.IsNonDerivedAttributeDefined<TestInitializeAttribute>(methodInfo, inherit: false);
bool hasTestCleanup = _reflectionHelper.IsNonDerivedAttributeDefined<TestCleanupAttribute>(methodInfo, inherit: false);
bool hasTestInitialize = _reflectionHelper.IsAttributeDefined<TestInitializeAttribute>(methodInfo, inherit: false);
bool hasTestCleanup = _reflectionHelper.IsAttributeDefined<TestCleanupAttribute>(methodInfo, inherit: false);

if (!hasTestCleanup && !hasTestInitialize)
{
Expand Down Expand Up @@ -807,7 +807,7 @@
private TimeoutInfo GetTestTimeout(MethodInfo methodInfo, TestMethod testMethod)
{
DebugEx.Assert(methodInfo != null, "TestMethod should be non-null");
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);

Check failure on line 810 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L810

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(810,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 810 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L810

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(810,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 810 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L810

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(810,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 810 in src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs#L810

src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs(810,64): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'ReflectHelper' does not contain a definition for 'GetFirstNonDerivedAttributeOrDefault' and no accessible extension method 'GetFirstNonDerivedAttributeOrDefault' accepting a first argument of type 'ReflectHelper' could be found (are you missing a using directive or an assembly reference?)

if (timeoutAttribute != null)
{
Expand All @@ -833,12 +833,12 @@
DebugEx.Assert(testMethodInfo != null, "testMethodInfo is Null");
DebugEx.Assert(testMethodInfo.TestMethod != null, "testMethodInfo.TestMethod is Null");

IEnumerable<TestPropertyAttribute> attributes = _reflectionHelper.GetDerivedAttributes<TestPropertyAttribute>(testMethodInfo.TestMethod, inherit: true);
IEnumerable<TestPropertyAttribute> attributes = _reflectionHelper.GetAttributes<TestPropertyAttribute>(testMethodInfo.TestMethod, inherit: true);
DebugEx.Assert(attributes != null, "attributes is null");

if (testMethodInfo.TestMethod.DeclaringType is { } testClass)
{
attributes = attributes.Concat(_reflectionHelper.GetDerivedAttributes<TestPropertyAttribute>(testClass, inherit: true));
attributes = attributes.Concat(_reflectionHelper.GetAttributes<TestPropertyAttribute>(testClass, inherit: true));
}

foreach (TestPropertyAttribute attribute in attributes)
Expand Down
14 changes: 0 additions & 14 deletions src/Adapter/MSTest.TestAdapter/Helpers/AttributeComparer.cs

This file was deleted.

Loading
Loading