Skip to content

Commit 9574f08

Browse files
committed
Simplify reflection
1 parent 4a47388 commit 9574f08

File tree

13 files changed

+59
-127
lines changed

13 files changed

+59
-127
lines changed

src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ private static bool TryUnfoldITestDataSources(UnitTestElement test, TestMethodIn
359359
// We don't have a special method to filter attributes that are not derived from Attribute, so we take all
360360
// attributes and filter them. We don't have to care if there is one, because this method is only entered when
361361
// there is at least one (we determine this in TypeEnumerator.GetTestFromMethod.
362-
IEnumerable<ITestDataSource> testDataSources = ReflectHelper.Instance.GetDerivedAttributes<Attribute>(testMethodInfo.MethodInfo, inherit: false).OfType<ITestDataSource>();
362+
IEnumerable<ITestDataSource> testDataSources = ReflectHelper.Instance.GetAttributes<Attribute>(testMethodInfo.MethodInfo, inherit: false).OfType<ITestDataSource>();
363363

364364
// We need to use a temporary list to avoid adding tests to the main list if we fail to expand any data source.
365365
List<UnitTestElement> tempListOfTests = new();

src/Adapter/MSTest.TestAdapter/Discovery/TestMethodValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal virtual bool IsValidTestMethod(MethodInfo testMethodInfo, Type type, IC
5353
// but the difference is quite small, and we don't expect a huge amount of non-test methods in the assembly.
5454
//
5555
// Also skip all methods coming from object, because they cannot be tests.
56-
if (testMethodInfo.DeclaringType == typeof(object) || !_reflectHelper.IsDerivedAttributeDefined<TestMethodAttribute>(testMethodInfo, inherit: false))
56+
if (testMethodInfo.DeclaringType == typeof(object) || !_reflectHelper.IsAttributeDefined<TestMethodAttribute>(testMethodInfo, inherit: false))
5757
{
5858
return false;
5959
}

src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal virtual bool IsValidTestClass(Type type, List<string> warnings)
5252
// gives us a better performance.
5353
// 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,
5454
// but the difference is quite small, and we don't expect a huge amount of non-test classes in the assembly.
55-
if (!type.IsClass || !_reflectHelper.IsDerivedAttributeDefined<TestClassAttribute>(type, inherit: false))
55+
if (!type.IsClass || !_reflectHelper.IsAttributeDefined<TestClassAttribute>(type, inherit: false))
5656
{
5757
return false;
5858
}

src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

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

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

414-
bool isSTATestClass = AttributeComparer.IsDerived<STATestClassAttribute>(ClassAttribute);
414+
bool isSTATestClass = ClassAttribute is STATestClassAttribute;
415415
bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
416416
if (isSTATestClass
417417
&& isWindowsOS
@@ -631,7 +631,7 @@ internal void ExecuteClassCleanup(TestContext testContext)
631631
if (classCleanupMethod is not null)
632632
{
633633
if (ClassAttribute.IgnoreMessage is null &&
634-
!ReflectHelper.Instance.IsNonDerivedAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
634+
!ReflectHelper.Instance.IsAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
635635
{
636636
ClassCleanupException = InvokeCleanupMethod(classCleanupMethod, remainingCleanupCount: BaseClassCleanupMethods.Count, testContext);
637637
}
@@ -643,7 +643,7 @@ internal void ExecuteClassCleanup(TestContext testContext)
643643
{
644644
classCleanupMethod = BaseClassCleanupMethods[i];
645645
if (ClassAttribute.IgnoreMessage is null &&
646-
!ReflectHelper.Instance.IsNonDerivedAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
646+
!ReflectHelper.Instance.IsAttributeDefined<IgnoreAttribute>(classCleanupMethod.DeclaringType!, false))
647647
{
648648
ClassCleanupException = InvokeCleanupMethod(classCleanupMethod, remainingCleanupCount: BaseClassCleanupMethods.Count - 1 - i, testContext);
649649
if (ClassCleanupException is not null)
@@ -711,7 +711,7 @@ internal void RunClassCleanup(ITestContext testContext, ClassCleanupManager clas
711711
return;
712712
}
713713

714-
bool isSTATestClass = AttributeComparer.IsDerived<STATestClassAttribute>(ClassAttribute);
714+
bool isSTATestClass = ClassAttribute is STATestClassAttribute;
715715
bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
716716
if (isSTATestClass
717717
&& isWindowsOS

src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ internal TestMethodInfo(
9595

9696
internal RetryBaseAttribute? RetryAttribute { get; }
9797

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

100100
public TAttributeType[] GetAttributes<TAttributeType>(bool inherit)
101101
where TAttributeType : Attribute
102-
=> ReflectHelper.Instance.GetDerivedAttributes<TAttributeType>(TestMethod, inherit).ToArray();
102+
=> ReflectHelper.Instance.GetAttributes<TAttributeType>(TestMethod, inherit).ToArray();
103103

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

232232
try
233233
{
234-
expectedExceptions = ReflectHelper.Instance.GetDerivedAttributes<ExpectedExceptionBaseAttribute>(TestMethod, inherit: true);
234+
expectedExceptions = ReflectHelper.Instance.GetAttributes<ExpectedExceptionBaseAttribute>(TestMethod, inherit: true);
235235
}
236236
catch (Exception ex)
237237
{
@@ -267,7 +267,7 @@ public virtual TestResult Invoke(object?[]? arguments)
267267
/// </returns>
268268
private RetryBaseAttribute? GetRetryAttribute()
269269
{
270-
IEnumerable<RetryBaseAttribute> attributes = ReflectHelper.Instance.GetDerivedAttributes<RetryBaseAttribute>(TestMethod, inherit: true);
270+
IEnumerable<RetryBaseAttribute> attributes = ReflectHelper.Instance.GetAttributes<RetryBaseAttribute>(TestMethod, inherit: true);
271271
using IEnumerator<RetryBaseAttribute> enumerator = attributes.GetEnumerator();
272272
if (!enumerator.MoveNext())
273273
{

src/Adapter/MSTest.TestAdapter/Execution/TestMethodRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public TestMethodRunner(TestMethodInfo testMethodInfo, TestMethod testMethod, IT
6464
/// <returns>The test results.</returns>
6565
internal List<TestResult> Execute(string initializationLogs, string initializationErrorLogs, string initializationTrace, string initializationTestContextMessages)
6666
{
67-
bool isSTATestClass = AttributeComparer.IsDerived<STATestClassAttribute>(_testMethodInfo.Parent.ClassAttribute);
68-
bool isSTATestMethod = AttributeComparer.IsDerived<STATestMethodAttribute>(_testMethodInfo.TestMethodOptions.Executor);
67+
bool isSTATestClass = _testMethodInfo.Parent.ClassAttribute is STATestClassAttribute;
68+
bool isSTATestMethod = _testMethodInfo.TestMethodOptions.Executor is STATestMethodAttribute;
6969
bool isSTARequested = isSTATestClass || isSTATestMethod;
7070
bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
7171
if (isSTARequested && isWindowsOS && Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ private TestAssemblyInfo GetAssemblyInfo(Type type)
397397
try
398398
{
399399
// Only examine classes which are TestClass or derives from TestClass attribute
400-
if (!_reflectionHelper.IsDerivedAttributeDefined<TestClassAttribute>(t, inherit: false))
400+
if (!_reflectionHelper.IsAttributeDefined<TestClassAttribute>(t, inherit: false))
401401
{
402402
continue;
403403
}
@@ -448,7 +448,7 @@ private bool IsAssemblyOrClassInitializeMethod<TInitializeAttribute>(MethodInfo
448448
// {
449449
// return false;
450450
// }
451-
if (!_reflectionHelper.IsNonDerivedAttributeDefined<TInitializeAttribute>(methodInfo, false))
451+
if (!_reflectionHelper.IsAttributeDefined<TInitializeAttribute>(methodInfo, false))
452452
{
453453
return false;
454454
}
@@ -476,7 +476,7 @@ private bool IsAssemblyOrClassCleanupMethod<TCleanupAttribute>(MethodInfo method
476476
// {
477477
// return false;
478478
// }
479-
if (!_reflectionHelper.IsNonDerivedAttributeDefined<TCleanupAttribute>(methodInfo, false))
479+
if (!_reflectionHelper.IsAttributeDefined<TCleanupAttribute>(methodInfo, false))
480480
{
481481
return false;
482482
}
@@ -607,8 +607,8 @@ private void UpdateInfoIfTestInitializeOrCleanupMethod(
607607
bool isBase,
608608
Dictionary<string, string?> instanceMethods)
609609
{
610-
bool hasTestInitialize = _reflectionHelper.IsNonDerivedAttributeDefined<TestInitializeAttribute>(methodInfo, inherit: false);
611-
bool hasTestCleanup = _reflectionHelper.IsNonDerivedAttributeDefined<TestCleanupAttribute>(methodInfo, inherit: false);
610+
bool hasTestInitialize = _reflectionHelper.IsAttributeDefined<TestInitializeAttribute>(methodInfo, inherit: false);
611+
bool hasTestCleanup = _reflectionHelper.IsAttributeDefined<TestCleanupAttribute>(methodInfo, inherit: false);
612612

613613
if (!hasTestCleanup && !hasTestInitialize)
614614
{
@@ -833,12 +833,12 @@ private void SetCustomProperties(TestMethodInfo testMethodInfo, ITestContext tes
833833
DebugEx.Assert(testMethodInfo != null, "testMethodInfo is Null");
834834
DebugEx.Assert(testMethodInfo.TestMethod != null, "testMethodInfo.TestMethod is Null");
835835

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

839839
if (testMethodInfo.TestMethod.DeclaringType is { } testClass)
840840
{
841-
attributes = attributes.Concat(_reflectionHelper.GetDerivedAttributes<TestPropertyAttribute>(testClass, inherit: true));
841+
attributes = attributes.Concat(_reflectionHelper.GetAttributes<TestPropertyAttribute>(testClass, inherit: true));
842842
}
843843

844844
foreach (TestPropertyAttribute attribute in attributes)

src/Adapter/MSTest.TestAdapter/Helpers/AttributeComparer.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)