Skip to content
Merged
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 @@ -16,15 +16,6 @@ internal interface IReflectionOperations
[return: NotNullIfNotNull(nameof(memberInfo))]
object[]? GetCustomAttributes(MemberInfo memberInfo);

/// <summary>
/// Gets all the custom attributes of a given type adorned on a member.
/// </summary>
/// <param name="memberInfo"> The member info. </param>
/// <param name="type"> The attribute type. </param>
/// <returns> The list of attributes on the member. Empty list if none found. </returns>
[return: NotNullIfNotNull(nameof(memberInfo))]
object[]? GetCustomAttributes(MemberInfo memberInfo, Type type);

/// <summary>
/// Gets all the custom attributes of a given type on an assembly.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ internal sealed class ReflectionOperations : IReflectionOperations
}
#endif

/// <summary>
/// Gets all the custom attributes of a given type adorned on a member.
/// </summary>
/// <param name="memberInfo"> The member info. </param>
/// <param name="type"> The attribute type. </param>
/// <returns> The list of attributes on the member. Empty list if none found. </returns>
[return: NotNullIfNotNull(nameof(memberInfo))]
public object[]? GetCustomAttributes(MemberInfo memberInfo, Type type) =>
#if NETFRAMEWORK
[.. ReflectionUtility.GetCustomAttributesCore(memberInfo, type)];
#else
memberInfo.GetCustomAttributes(type, inherit: true);
#endif

/// <summary>
/// Gets all the custom attributes of a given type on an assembly.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,6 @@ public async Task RunTestMethodShouldRunDataDrivenTestsWhenDataIsProvidedUsingDa
var testMethodInfo = new TestableTestMethodInfo(_methodInfo, _testClassInfo, _testMethodOptions, () => testResult);
var testMethodRunner = new TestMethodRunner(testMethodInfo, _testMethod, _testContextImplementation);

int dummyIntData = 2;
string dummyStringData = "DummyString";
DataRowAttribute dataRowAttribute = new(
dummyIntData,
dummyStringData);

var attributes = new Attribute[] { dataRowAttribute };

// Setup mocks
_testablePlatformServiceProvider.MockReflectionOperations.Setup(ro => ro.GetCustomAttributes(_methodInfo, It.IsAny<Type>())).Returns(attributes);

TestResult[] results = await testMethodRunner.RunTestMethodAsync();
results[0].Outcome.Should().Be(UTF.UnitTestOutcome.Inconclusive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ public void IsAttributeDefinedShouldReturnFromCache()
// Validate that reflection APIs are not called again.
rh.IsAttributeDefined<TestMethodAttribute>(memberInfo).Should().BeTrue();
_testablePlatformServiceProvider.MockReflectionOperations.Verify(ro => ro.GetCustomAttributes(memberInfo), Times.Once);

// Also validate that reflection APIs for an individual type is not called since the cache gives us what we need already.
_testablePlatformServiceProvider.MockReflectionOperations.Verify(ro => ro.GetCustomAttributes(It.IsAny<MemberInfo>(), It.IsAny<Type>()), Times.Never);
}

public void HasAttributeDerivedFromShouldReturnTrueIfSpecifiedAttributeIsDefinedOnAMember()
Expand Down Expand Up @@ -237,25 +234,17 @@ public void HasAttributeDerivedFromShouldReturnFromCache()
// Validate that reflection APIs are not called again.
rh.IsAttributeDefined<TestMethodAttribute>(memberInfo).Should().BeTrue();
_testablePlatformServiceProvider.MockReflectionOperations.Verify(ro => ro.GetCustomAttributes(memberInfo), Times.Once);

// Also validate that reflection APIs for an individual type is not called since the cache gives us what we need already.
_testablePlatformServiceProvider.MockReflectionOperations.Verify(ro => ro.GetCustomAttributes(It.IsAny<MemberInfo>(), It.IsAny<Type>()), Times.Never);
}

public void HasAttributeDerivedFromShouldReturnFalseQueryingProvidedAttributesExistenceIfGettingAllAttributesFail()
{
var rh = new ReflectHelper();
var mockMemberInfo = new Mock<MemberInfo>();
var attributes = new Attribute[] { new TestableExtendedTestMethod() };

_testablePlatformServiceProvider.MockReflectionOperations.
Setup(ro => ro.GetCustomAttributes(mockMemberInfo.Object)).
Returns((object[])null!);

_testablePlatformServiceProvider.MockReflectionOperations.
Setup(ro => ro.GetCustomAttributes(mockMemberInfo.Object, typeof(TestMethodAttribute))).
Returns(attributes);

rh.IsAttributeDefined<TestMethodAttribute>(mockMemberInfo.Object).Should().BeFalse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,58 +71,6 @@ public void GetCustomAttributesOnTypeShouldReturnAllAttributesWithBaseInheritanc
GetAttributeValuePairs(attributes).SequenceEqual(expectedAttributes).Should().BeTrue();
}

public void GetSpecificCustomAttributesShouldReturnAllAttributes()
{
MethodInfo methodInfo = typeof(DummyBaseTestClass).GetMethod("DummyVTestMethod1")!;

object[] attributes = _reflectionOperations.GetCustomAttributes(methodInfo, typeof(DummyAAttribute));

attributes.Should().NotBeNull();
attributes.Length.Should().Be(1);

string[] expectedAttributes = ["DummyA : base"];
GetAttributeValuePairs(attributes).SequenceEqual(expectedAttributes).Should().BeTrue();
}

public void GetSpecificCustomAttributesShouldReturnAllAttributesWithBaseInheritance()
{
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("DummyVTestMethod1")!;

object[] attributes = _reflectionOperations.GetCustomAttributes(methodInfo, typeof(DummyAAttribute));

attributes.Should().NotBeNull();
attributes.Length.Should().Be(2);

string[] expectedAttributes = ["DummyA : derived", "DummyA : base"];
GetAttributeValuePairs(attributes).SequenceEqual(expectedAttributes).Should().BeTrue();
}

public void GetSpecificCustomAttributesOnTypeShouldReturnAllAttributes()
{
Type type = typeof(DummyBaseTestClass);

object[] attributes = _reflectionOperations.GetCustomAttributes(type, typeof(DummyAAttribute));

attributes.Should().NotBeNull();
attributes.Length.Should().Be(1);

string[] expectedAttributes = ["DummyA : ba"];
GetAttributeValuePairs(attributes).SequenceEqual(expectedAttributes).Should().BeTrue();
}

public void GetSpecificCustomAttributesOnTypeShouldReturnAllAttributesWithBaseInheritance()
{
Type type = typeof(DummyTestClass);

object[] attributes = _reflectionOperations.GetCustomAttributes(type, typeof(DummyAAttribute));

attributes.Should().NotBeNull();
attributes.Length.Should().Be(2);

string[] expectedAttributes = ["DummyA : a", "DummyA : ba"];
GetAttributeValuePairs(attributes).SequenceEqual(expectedAttributes).Should().BeTrue();
}

public void GetSpecificCustomAttributesOnAssemblyShouldReturnAllAttributes()
{
Assembly asm = typeof(DummyTestClass).Assembly;
Expand Down