diff --git a/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs b/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs index 74654b6bca..69c5d1e277 100644 --- a/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs +++ b/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs @@ -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 testDataSources = ReflectHelper.Instance.GetDerivedAttributes(testMethodInfo.MethodInfo, inherit: false).OfType(); + IEnumerable testDataSources = ReflectHelper.Instance.GetAttributes(testMethodInfo.MethodInfo, inherit: false).OfType(); // We need to use a temporary list to avoid adding tests to the main list if we fail to expand any data source. List tempListOfTests = new(); diff --git a/src/Adapter/MSTest.TestAdapter/Discovery/TestMethodValidator.cs b/src/Adapter/MSTest.TestAdapter/Discovery/TestMethodValidator.cs index 2669c119ad..043a3e7d60 100644 --- a/src/Adapter/MSTest.TestAdapter/Discovery/TestMethodValidator.cs +++ b/src/Adapter/MSTest.TestAdapter/Discovery/TestMethodValidator.cs @@ -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(testMethodInfo, inherit: false)) + if (testMethodInfo.DeclaringType == typeof(object) || !_reflectHelper.IsAttributeDefined(testMethodInfo, inherit: false)) { return false; } diff --git a/src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs b/src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs index 9e8e1c98d6..b82623ca7c 100644 --- a/src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs +++ b/src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs @@ -52,7 +52,7 @@ internal virtual bool IsValidTestClass(Type type, List 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(type, inherit: false)) + if (!type.IsClass || !_reflectHelper.IsAttributeDefined(type, inherit: false)) { return false; } diff --git a/src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs b/src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs index 2139e2340b..4d5261ecd3 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs @@ -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; @@ -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(ClassAttribute); + bool isSTATestClass = ClassAttribute is STATestClassAttribute; bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); if (isSTATestClass && isWindowsOS @@ -631,7 +631,7 @@ internal void ExecuteClassCleanup(TestContext testContext) if (classCleanupMethod is not null) { if (ClassAttribute.IgnoreMessage is null && - !ReflectHelper.Instance.IsNonDerivedAttributeDefined(classCleanupMethod.DeclaringType!, false)) + !ReflectHelper.Instance.IsAttributeDefined(classCleanupMethod.DeclaringType!, false)) { ClassCleanupException = InvokeCleanupMethod(classCleanupMethod, remainingCleanupCount: BaseClassCleanupMethods.Count, testContext); } @@ -643,7 +643,7 @@ internal void ExecuteClassCleanup(TestContext testContext) { classCleanupMethod = BaseClassCleanupMethods[i]; if (ClassAttribute.IgnoreMessage is null && - !ReflectHelper.Instance.IsNonDerivedAttributeDefined(classCleanupMethod.DeclaringType!, false)) + !ReflectHelper.Instance.IsAttributeDefined(classCleanupMethod.DeclaringType!, false)) { ClassCleanupException = InvokeCleanupMethod(classCleanupMethod, remainingCleanupCount: BaseClassCleanupMethods.Count - 1 - i, testContext); if (ClassCleanupException is not null) @@ -711,7 +711,7 @@ internal void RunClassCleanup(ITestContext testContext, ClassCleanupManager clas return; } - bool isSTATestClass = AttributeComparer.IsDerived(ClassAttribute); + bool isSTATestClass = ClassAttribute is STATestClassAttribute; bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); if (isSTATestClass && isWindowsOS diff --git a/src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs b/src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs index 4085813c23..6551b6c106 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs @@ -95,11 +95,11 @@ internal TestMethodInfo( internal RetryBaseAttribute? RetryAttribute { get; } - public Attribute[]? GetAllAttributes(bool inherit) => ReflectHelper.Instance.GetDerivedAttributes(TestMethod, inherit).ToArray(); + public Attribute[]? GetAllAttributes(bool inherit) => ReflectHelper.Instance.GetAttributes(TestMethod, inherit).ToArray(); public TAttributeType[] GetAttributes(bool inherit) where TAttributeType : Attribute - => ReflectHelper.Instance.GetDerivedAttributes(TestMethod, inherit).ToArray(); + => ReflectHelper.Instance.GetAttributes(TestMethod, inherit).ToArray(); /// /// Execute test method. Capture failures, handle async and return result. @@ -231,7 +231,7 @@ public virtual TestResult Invoke(object?[]? arguments) try { - expectedExceptions = ReflectHelper.Instance.GetDerivedAttributes(TestMethod, inherit: true); + expectedExceptions = ReflectHelper.Instance.GetAttributes(TestMethod, inherit: true); } catch (Exception ex) { @@ -267,7 +267,7 @@ public virtual TestResult Invoke(object?[]? arguments) /// private RetryBaseAttribute? GetRetryAttribute() { - IEnumerable attributes = ReflectHelper.Instance.GetDerivedAttributes(TestMethod, inherit: true); + IEnumerable attributes = ReflectHelper.Instance.GetAttributes(TestMethod, inherit: true); using IEnumerator enumerator = attributes.GetEnumerator(); if (!enumerator.MoveNext()) { diff --git a/src/Adapter/MSTest.TestAdapter/Execution/TestMethodRunner.cs b/src/Adapter/MSTest.TestAdapter/Execution/TestMethodRunner.cs index 249301cd2d..1c4ddb68f3 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/TestMethodRunner.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/TestMethodRunner.cs @@ -64,8 +64,8 @@ public TestMethodRunner(TestMethodInfo testMethodInfo, TestMethod testMethod, IT /// The test results. internal List Execute(string initializationLogs, string initializationErrorLogs, string initializationTrace, string initializationTestContextMessages) { - bool isSTATestClass = AttributeComparer.IsDerived(_testMethodInfo.Parent.ClassAttribute); - bool isSTATestMethod = AttributeComparer.IsDerived(_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) diff --git a/src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs b/src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs index 2b13c8384d..8323241a16 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs @@ -397,7 +397,7 @@ private TestAssemblyInfo GetAssemblyInfo(Type type) try { // Only examine classes which are TestClass or derives from TestClass attribute - if (!_reflectionHelper.IsDerivedAttributeDefined(t, inherit: false)) + if (!_reflectionHelper.IsAttributeDefined(t, inherit: false)) { continue; } @@ -448,7 +448,7 @@ private bool IsAssemblyOrClassInitializeMethod(MethodInfo // { // return false; // } - if (!_reflectionHelper.IsNonDerivedAttributeDefined(methodInfo, false)) + if (!_reflectionHelper.IsAttributeDefined(methodInfo, false)) { return false; } @@ -476,7 +476,7 @@ private bool IsAssemblyOrClassCleanupMethod(MethodInfo method // { // return false; // } - if (!_reflectionHelper.IsNonDerivedAttributeDefined(methodInfo, false)) + if (!_reflectionHelper.IsAttributeDefined(methodInfo, false)) { return false; } @@ -607,8 +607,8 @@ private void UpdateInfoIfTestInitializeOrCleanupMethod( bool isBase, Dictionary instanceMethods) { - bool hasTestInitialize = _reflectionHelper.IsNonDerivedAttributeDefined(methodInfo, inherit: false); - bool hasTestCleanup = _reflectionHelper.IsNonDerivedAttributeDefined(methodInfo, inherit: false); + bool hasTestInitialize = _reflectionHelper.IsAttributeDefined(methodInfo, inherit: false); + bool hasTestCleanup = _reflectionHelper.IsAttributeDefined(methodInfo, inherit: false); if (!hasTestCleanup && !hasTestInitialize) { @@ -833,12 +833,12 @@ private void SetCustomProperties(TestMethodInfo testMethodInfo, ITestContext tes DebugEx.Assert(testMethodInfo != null, "testMethodInfo is Null"); DebugEx.Assert(testMethodInfo.TestMethod != null, "testMethodInfo.TestMethod is Null"); - IEnumerable attributes = _reflectionHelper.GetDerivedAttributes(testMethodInfo.TestMethod, inherit: true); + IEnumerable attributes = _reflectionHelper.GetAttributes(testMethodInfo.TestMethod, inherit: true); DebugEx.Assert(attributes != null, "attributes is null"); if (testMethodInfo.TestMethod.DeclaringType is { } testClass) { - attributes = attributes.Concat(_reflectionHelper.GetDerivedAttributes(testClass, inherit: true)); + attributes = attributes.Concat(_reflectionHelper.GetAttributes(testClass, inherit: true)); } foreach (TestPropertyAttribute attribute in attributes) diff --git a/src/Adapter/MSTest.TestAdapter/Helpers/AttributeComparer.cs b/src/Adapter/MSTest.TestAdapter/Helpers/AttributeComparer.cs deleted file mode 100644 index c82f46d996..0000000000 --- a/src/Adapter/MSTest.TestAdapter/Helpers/AttributeComparer.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers; - -internal static class AttributeComparer -{ - public static bool IsNonDerived(Attribute attribute) => - // We are explicitly checking the given type *exactly*. We don't want to consider inheritance. - // So, this should NOT be refactored to 'attribute is TAttribute'. - attribute.GetType() == typeof(TAttribute); - - public static bool IsDerived(Attribute attribute) => attribute is TAttribute; -} diff --git a/src/Adapter/MSTest.TestAdapter/Helpers/ReflectHelper.cs b/src/Adapter/MSTest.TestAdapter/Helpers/ReflectHelper.cs index 7dcd86d56d..9800bd70ce 100644 --- a/src/Adapter/MSTest.TestAdapter/Helpers/ReflectHelper.cs +++ b/src/Adapter/MSTest.TestAdapter/Helpers/ReflectHelper.cs @@ -28,35 +28,6 @@ internal class ReflectHelper : MarshalByRefObject public static ReflectHelper Instance => InstanceValue.Value; - /// - /// Checks to see if a member or type is decorated with the given attribute. The type is checked exactly. If attribute is derived (inherits from) a class, e.g. [MyTestClass] from [TestClass] it won't match if you look for [TestClass]. The inherit parameter does not impact this checking. - /// - /// - /// Note that because derived attribute types are not considered, should be sealed. - /// - /// Attribute to search for by fully qualified name. - /// Member/Type to test. - /// Inspect inheritance chain of the member or class. E.g. if parent class has this attribute defined. - /// True if the attribute of the specified type is defined on this member or a class. - public virtual bool IsNonDerivedAttributeDefined(MemberInfo memberInfo, bool inherit) - where TAttribute : Attribute - { - Guard.NotNull(memberInfo); - - // Get attributes defined on the member from the cache. - Attribute[] attributes = GetCustomAttributesCached(memberInfo, inherit); - - foreach (Attribute attribute in attributes) - { - if (AttributeComparer.IsNonDerived(attribute)) - { - return true; - } - } - - return false; - } - /// /// Checks to see if a member or type is decorated with the given attribute, or an attribute that derives from it. e.g. [MyTestClass] from [TestClass] will match if you look for [TestClass]. The inherit parameter does not impact this checking. /// @@ -64,7 +35,7 @@ public virtual bool IsNonDerivedAttributeDefined(MemberInfo memberIn /// Member to inspect for attributes. /// Inspect inheritance chain of the member or class. E.g. if parent class has this attribute defined. /// True if the attribute of the specified type is defined on this member or a class. - public virtual /* for testing */ bool IsDerivedAttributeDefined(MemberInfo memberInfo, bool inherit) + public virtual /* for testing */ bool IsAttributeDefined(MemberInfo memberInfo, bool inherit) where TAttribute : Attribute { Guard.NotNull(memberInfo); @@ -77,7 +48,7 @@ public virtual bool IsNonDerivedAttributeDefined(MemberInfo memberIn { DebugEx.Assert(attribute != null, $"{nameof(ReflectHelper)}.{nameof(GetCustomAttributesCached)}: internal error: wrong value in the attributes dictionary."); - if (AttributeComparer.IsDerived(attribute)) + if (attribute is TAttribute) { return true; } @@ -98,31 +69,6 @@ public virtual bool IsNonDerivedAttributeDefined(MemberInfo memberIn #endif public override object InitializeLifetimeService() => null!; - /// - /// Gets first attribute that matches the type (but is not derived from it). Use this together with attribute that is both sealed and does not allow multiple. - /// In such case there cannot be more attributes, and this will avoid the cost of - /// checking for more than one attribute. - /// - /// Type of the attribute to find. - /// The type, assembly or method. - /// If we should inspect parents of this type. - /// The attribute that is found or null. - public TAttribute? GetFirstNonDerivedAttributeOrDefault(ICustomAttributeProvider attributeProvider, bool inherit) - where TAttribute : Attribute - { - Attribute[] cachedAttributes = GetCustomAttributesCached(attributeProvider, inherit); - - foreach (Attribute cachedAttribute in cachedAttributes) - { - if (AttributeComparer.IsNonDerived(cachedAttribute)) - { - return (TAttribute)cachedAttribute; - } - } - - return null; - } - /// /// Gets first attribute that matches the type or is derived from it. /// Use this together with attribute that does not allow multiple. In such case there cannot be more attributes, and this will avoid the cost of @@ -140,9 +86,9 @@ public virtual bool IsNonDerivedAttributeDefined(MemberInfo memberIn foreach (Attribute cachedAttribute in cachedAttributes) { - if (AttributeComparer.IsDerived(cachedAttribute)) + if (cachedAttribute is TAttribute cachedAttributeAsTAttribute) { - return (TAttribute)cachedAttribute; + return cachedAttributeAsTAttribute; } } @@ -179,9 +125,9 @@ internal virtual bool IsMethodDeclaredInSameAssemblyAsType(MethodInfo method, Ty /// Categories defined. internal virtual /* for tests, we are mocking this */ string[] GetTestCategories(MemberInfo categoryAttributeProvider, Type owningType) { - IEnumerable methodCategories = GetDerivedAttributes(categoryAttributeProvider, inherit: true); - IEnumerable typeCategories = GetDerivedAttributes(owningType, inherit: true); - IEnumerable assemblyCategories = GetDerivedAttributes(owningType.Assembly, inherit: true); + IEnumerable methodCategories = GetAttributes(categoryAttributeProvider, inherit: true); + IEnumerable typeCategories = GetAttributes(owningType, inherit: true); + IEnumerable assemblyCategories = GetAttributes(owningType.Assembly, inherit: true); return methodCategories.Concat(typeCategories).Concat(assemblyCategories).SelectMany(c => c.TestCategories).ToArray(); } @@ -240,8 +186,8 @@ internal static TestIdGenerationStrategy GetTestIdGenerationStrategy(Assembly as /// The type that owns . /// True if test method should not run in parallel. internal bool IsDoNotParallelizeSet(MemberInfo testMethod, Type owningType) - => IsDerivedAttributeDefined(testMethod, inherit: true) - || IsDerivedAttributeDefined(owningType, inherit: true); + => IsAttributeDefined(testMethod, inherit: true) + || IsAttributeDefined(owningType, inherit: true); /// /// Get the parallelization behavior for a test assembly. @@ -327,11 +273,11 @@ internal static bool IsDoNotParallelizeSet(Assembly assembly) /// List of traits. internal virtual IEnumerable GetTestPropertiesAsTraits(MemberInfo testPropertyProvider) { - IEnumerable testPropertyAttributes = GetDerivedAttributes(testPropertyProvider, inherit: true); + IEnumerable testPropertyAttributes = GetAttributes(testPropertyProvider, inherit: true); if (testPropertyProvider.DeclaringType is { } testClass) { - testPropertyAttributes = testPropertyAttributes.Concat(GetDerivedAttributes(testClass, inherit: true)); + testPropertyAttributes = testPropertyAttributes.Concat(GetAttributes(testClass, inherit: true)); } foreach (TestPropertyAttribute testProperty in testPropertyAttributes) @@ -348,7 +294,7 @@ internal virtual IEnumerable GetTestPropertiesAsTraits(MemberInfo testPro /// The member to inspect. /// Look at inheritance chain. /// An instance of the attribute. - internal virtual /* for tests, for moq */ IEnumerable GetDerivedAttributes(ICustomAttributeProvider attributeProvider, bool inherit) + internal virtual /* for tests, for moq */ IEnumerable GetAttributes(ICustomAttributeProvider attributeProvider, bool inherit) where TAttributeType : Attribute { Attribute[] attributes = GetCustomAttributesCached(attributeProvider, inherit); @@ -358,9 +304,9 @@ internal virtual IEnumerable GetTestPropertiesAsTraits(MemberInfo testPro { DebugEx.Assert(attribute != null, "ReflectHelper.DefinesAttributeDerivedFrom: internal error: wrong value in the attributes dictionary."); - if (AttributeComparer.IsDerived(attribute)) + if (attribute is TAttributeType attributeAsAttributeType) { - yield return (TAttributeType)attribute; + yield return attributeAsAttributeType; } } } diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TestMethodValidatorTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TestMethodValidatorTests.cs index ef2d8b35b7..c12268190f 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TestMethodValidatorTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TestMethodValidatorTests.cs @@ -34,7 +34,7 @@ public TestMethodValidatorTests() public void IsValidTestMethodShouldReturnFalseForMethodsWithoutATestMethodAttributeOrItsDerivedAttributes() { _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(It.IsAny(), false)).Returns(false); + rh => rh.IsAttributeDefined(It.IsAny(), false)).Returns(false); Verify(!_testMethodValidator.IsValidTestMethod(_mockMethodInfo.Object, _type, _warnings)); } @@ -183,7 +183,7 @@ public void WhenDiscoveryOfInternalsIsEnabledIsValidTestMethodShouldReturnFalseF #endregion private void SetupTestMethod() => _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(It.IsAny(), false)).Returns(true); + rh => rh.IsAttributeDefined(It.IsAny(), false)).Returns(true); } #region Dummy types diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TypeValidatorTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TypeValidatorTests.cs index 68687c1bd7..d1ce91ead0 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TypeValidatorTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Discovery/TypeValidatorTests.cs @@ -49,7 +49,7 @@ public void IsValidTestClassShouldReturnTrueForClassesMarkedByAnAttributeDerived { _mockReflectHelper.Setup(rh => rh.IsNonDerivedAttributeDefined(It.IsAny(), false)).Returns(false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(It.IsAny(), false)).Returns(true); + rh => rh.IsAttributeDefined(It.IsAny(), false)).Returns(true); Verify(_typeValidator.IsValidTestClass(typeof(TypeValidatorTests), _warnings)); } @@ -393,7 +393,7 @@ private static Type[] GetAllTestTypes() #region private methods - private void SetupTestClass() => _mockReflectHelper.Setup(rh => rh.IsDerivedAttributeDefined(It.IsAny(), false)).Returns(true); + private void SetupTestClass() => _mockReflectHelper.Setup(rh => rh.IsAttributeDefined(It.IsAny(), false)).Returns(true); #endregion } diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs index 2398bd2457..65fc5893da 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs @@ -233,7 +233,7 @@ public void GetTestMethodInfoShouldCacheAssemblyInitializeAttribute() var testMethod = new TestMethod("TestInit", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyInit"), false)).Returns(true); @@ -253,7 +253,7 @@ public void GetTestMethodInfoShouldCacheAssemblyCleanupAttribute() var testMethod = new TestMethod("TestCleanup", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyCleanup"), false)).Returns(true); @@ -273,7 +273,7 @@ public void GetTestMethodInfoShouldCacheAssemblyInitAndCleanupAttribute() var testMethod = new TestMethod("TestInitOrCleanup", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyInit"), false)).Returns(true); @@ -296,7 +296,7 @@ public void GetTestMethodInfoShouldThrowIfAssemblyInitHasIncorrectSignature() var testMethod = new TestMethod("M", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyInit"), false)).Returns(true); @@ -326,7 +326,7 @@ public void GetTestMethodInfoShouldThrowIfAssemblyCleanupHasIncorrectSignature() var testMethod = new TestMethod("M", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyCleanup"), false)).Returns(true); @@ -357,7 +357,7 @@ public void GetTestMethodInfoShouldCacheAssemblyInfoInstanceAndReuseTheCache() var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _typeCache.GetTestMethodInfo( testMethod, @@ -369,7 +369,7 @@ public void GetTestMethodInfoShouldCacheAssemblyInfoInstanceAndReuseTheCache() new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - _mockReflectHelper.Verify(rh => rh.IsDerivedAttributeDefined(type, false), Times.Once); + _mockReflectHelper.Verify(rh => rh.IsAttributeDefined(type, false), Times.Once); Verify(_typeCache.AssemblyInfoCache.Count == 1); } @@ -425,7 +425,7 @@ public void GetTestMethodInfoShouldCacheBaseClassInitializeAttributes() var testMethod = new TestMethod("TestMethod", type.FullName, "A", false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(baseType.GetMethod("AssemblyInit"), false)).Returns(true); @@ -474,7 +474,7 @@ public void GetTestMethodInfoShouldCacheBaseClassCleanupAttributes() var testMethod = new TestMethod("TestMethod", type.FullName, "A", false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(baseType.GetMethod("AssemblyCleanup"), false)).Returns(true); _mockReflectHelper.Setup( @@ -523,7 +523,7 @@ public void GetTestMethodInfoShouldCacheBaseClassInitAndCleanupAttributes() MethodInfo baseCleanupMethod = baseType.GetMethod("ClassCleanup"); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(baseInitializeMethod, false)).Returns(true); @@ -623,7 +623,7 @@ public void GetTestMethodInfoShouldThrowIfClassInitHasIncorrectSignature() var testMethod = new TestMethod("M", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyInit"), false)).Returns(true); @@ -653,7 +653,7 @@ public void GetTestMethodInfoShouldThrowIfClassCleanupHasIncorrectSignature() var testMethod = new TestMethod("M", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyCleanup"), false)).Returns(true); @@ -723,7 +723,7 @@ public void GetTestMethodInfoShouldThrowIfTestInitOrCleanupHasIncorrectSignature var testMethod = new TestMethod("M", type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("TestInit"), false)).Returns(true); @@ -1277,7 +1277,7 @@ public void AssemblyInfoListWithExecutableCleanupMethodsShouldReturnEmptyListWhe var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyCleanup"), false)).Returns(false); @@ -1299,7 +1299,7 @@ public void AssemblyInfoListWithExecutableCleanupMethodsShouldReturnAssemblyInfo var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false); _mockReflectHelper.Setup( - rh => rh.IsDerivedAttributeDefined(type, false)).Returns(true); + rh => rh.IsAttributeDefined(type, false)).Returns(true); _mockReflectHelper.Setup( rh => rh.IsNonDerivedAttributeDefined(type.GetMethod("AssemblyCleanup"), false)).Returns(true); diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Helpers/ReflectHelperTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Helpers/ReflectHelperTests.cs index 54a001d1e8..5ea8ae6689 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Helpers/ReflectHelperTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Helpers/ReflectHelperTests.cs @@ -201,7 +201,7 @@ public void HasAttributeDerivedFromShouldReturnTrueIfSpecifiedAttributeIsDefined Setup(ro => ro.GetCustomAttributes(mockMemberInfo.Object, true)). Returns(attributes); - Verify(rh.IsDerivedAttributeDefined(mockMemberInfo.Object, true)); + Verify(rh.IsAttributeDefined(mockMemberInfo.Object, true)); } public void HasAttributeDerivedFromShouldReturnFalseIfSpecifiedAttributeIsNotDefinedOnAMember() @@ -231,10 +231,10 @@ public void HasAttributeDerivedFromShouldReturnFromCache() Setup(ro => ro.GetCustomAttributes(memberInfo, true)). Returns(attributes); - Verify(rh.IsDerivedAttributeDefined(memberInfo, true)); + Verify(rh.IsAttributeDefined(memberInfo, true)); // Validate that reflection APIs are not called again. - Verify(rh.IsDerivedAttributeDefined(memberInfo, true)); + Verify(rh.IsAttributeDefined(memberInfo, true)); _testablePlatformServiceProvider.MockReflectionOperations.Verify(ro => ro.GetCustomAttributes(memberInfo, true), Times.Once); // Also validate that reflection APIs for an individual type is not called since the cache gives us what we need already. @@ -275,8 +275,8 @@ public void GettingAttributesShouldNotReturnInheritedAttributesWhenAskingForNonI Setup(ro => ro.GetCustomAttributes(It.IsAny(), /* inherit */ false)). Returns([new TestClassAttribute()]); - TestClassAttribute[] inheritedAttributes = rh.GetDerivedAttributes(typeof(object), inherit: true).ToArray(); - TestClassAttribute[] nonInheritedAttributes = rh.GetDerivedAttributes(typeof(object), inherit: false).ToArray(); + TestClassAttribute[] inheritedAttributes = rh.GetAttributes(typeof(object), inherit: true).ToArray(); + TestClassAttribute[] nonInheritedAttributes = rh.GetAttributes(typeof(object), inherit: false).ToArray(); Verify(inheritedAttributes.Length == 2); Verify(nonInheritedAttributes.Length == 1);