|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace System.Reflection.Context.Tests |
| 9 | +{ |
| 10 | + // Custom attributes for testing inheritance |
| 11 | + [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)] |
| 12 | + internal class InheritedSingleAttribute : Attribute |
| 13 | + { |
| 14 | + public string Value { get; set; } |
| 15 | + public InheritedSingleAttribute(string value) => Value = value; |
| 16 | + } |
| 17 | + |
| 18 | + [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)] |
| 19 | + internal class InheritedMultipleAttribute : Attribute |
| 20 | + { |
| 21 | + public string Value { get; set; } |
| 22 | + public InheritedMultipleAttribute(string value) => Value = value; |
| 23 | + } |
| 24 | + |
| 25 | + [AttributeUsage(AttributeTargets.All, Inherited = false)] |
| 26 | + internal class NonInheritedAttribute : Attribute |
| 27 | + { |
| 28 | + public string Value { get; set; } |
| 29 | + public NonInheritedAttribute(string value) => Value = value; |
| 30 | + } |
| 31 | + |
| 32 | + // Base class with attributes |
| 33 | + [InheritedSingle("Base")] |
| 34 | + [InheritedMultiple("BaseMultiple")] |
| 35 | + [NonInherited("BaseNonInherited")] |
| 36 | + internal class BaseWithAttributes |
| 37 | + { |
| 38 | + [InheritedSingle("BaseMethod")] |
| 39 | + [InheritedMultiple("BaseMethodMultiple")] |
| 40 | + public virtual void VirtualMethod() { } |
| 41 | + } |
| 42 | + |
| 43 | + // Derived class that overrides |
| 44 | + [InheritedMultiple("DerivedMultiple")] |
| 45 | + internal class DerivedWithAttributes : BaseWithAttributes |
| 46 | + { |
| 47 | + [InheritedMultiple("DerivedMethodMultiple")] |
| 48 | + public override void VirtualMethod() { } |
| 49 | + } |
| 50 | + |
| 51 | + // Another derived class with same attribute |
| 52 | + [InheritedSingle("Derived2")] |
| 53 | + internal class DerivedWithSameAttribute : BaseWithAttributes |
| 54 | + { |
| 55 | + } |
| 56 | + |
| 57 | + public class AttributeInheritanceTests |
| 58 | + { |
| 59 | + private readonly CustomReflectionContext _customReflectionContext = new TestCustomReflectionContext(); |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void GetCustomAttributes_InheritTrue_OnDerivedType_ReturnsAttributes() |
| 63 | + { |
| 64 | + TypeInfo derivedTypeInfo = typeof(DerivedWithAttributes).GetTypeInfo(); |
| 65 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 66 | + |
| 67 | + object[] attrs = customDerivedType.GetCustomAttributes(typeof(InheritedMultipleAttribute), true); |
| 68 | + Assert.All(attrs, a => Assert.IsType<InheritedMultipleAttribute>(a)); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void GetCustomAttributes_InheritFalse_OnDerivedType_ReturnsOnlyDeclaredAttributes() |
| 73 | + { |
| 74 | + TypeInfo derivedTypeInfo = typeof(DerivedWithAttributes).GetTypeInfo(); |
| 75 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 76 | + |
| 77 | + object[] attrs = customDerivedType.GetCustomAttributes(typeof(InheritedMultipleAttribute), false); |
| 78 | + Assert.All(attrs, a => Assert.IsType<InheritedMultipleAttribute>(a)); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void GetCustomAttributes_NonInherited_OnDerivedType_ReturnsEmpty() |
| 83 | + { |
| 84 | + TypeInfo derivedTypeInfo = typeof(DerivedWithAttributes).GetTypeInfo(); |
| 85 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 86 | + |
| 87 | + object[] attrs = customDerivedType.GetCustomAttributes(typeof(NonInheritedAttribute), true); |
| 88 | + Assert.Empty(attrs); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void GetCustomAttributes_InheritedSingle_OnDerivedWithSame_ReturnsMatchingAttributes() |
| 93 | + { |
| 94 | + TypeInfo derivedTypeInfo = typeof(DerivedWithSameAttribute).GetTypeInfo(); |
| 95 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 96 | + |
| 97 | + object[] attrs = customDerivedType.GetCustomAttributes(typeof(InheritedSingleAttribute), true); |
| 98 | + Assert.All(attrs, a => Assert.IsType<InheritedSingleAttribute>(a)); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void GetCustomAttributes_OnOverriddenMethod_WithInherit_ReturnsMatchingAttributes() |
| 103 | + { |
| 104 | + TypeInfo derivedTypeInfo = typeof(DerivedWithAttributes).GetTypeInfo(); |
| 105 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 106 | + MethodInfo method = customDerivedType.GetMethod("VirtualMethod"); |
| 107 | + |
| 108 | + object[] attrs = method.GetCustomAttributes(typeof(InheritedMultipleAttribute), true); |
| 109 | + Assert.All(attrs, a => Assert.IsType<InheritedMultipleAttribute>(a)); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void GetCustomAttributes_OnOverriddenMethod_WithoutInherit_ReturnsDeclaredAttributes() |
| 114 | + { |
| 115 | + TypeInfo derivedTypeInfo = typeof(DerivedWithAttributes).GetTypeInfo(); |
| 116 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 117 | + MethodInfo method = customDerivedType.GetMethod("VirtualMethod"); |
| 118 | + |
| 119 | + object[] attrs = method.GetCustomAttributes(typeof(InheritedMultipleAttribute), false); |
| 120 | + Assert.All(attrs, a => Assert.IsType<InheritedMultipleAttribute>(a)); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void GetCustomAttributes_OnBaseMethod_ReturnsMatchingAttributes() |
| 125 | + { |
| 126 | + TypeInfo baseTypeInfo = typeof(BaseWithAttributes).GetTypeInfo(); |
| 127 | + TypeInfo customBaseType = _customReflectionContext.MapType(baseTypeInfo); |
| 128 | + MethodInfo method = customBaseType.GetMethod("VirtualMethod"); |
| 129 | + |
| 130 | + object[] attrs = method.GetCustomAttributes(typeof(InheritedSingleAttribute), false); |
| 131 | + Assert.All(attrs, a => Assert.IsType<InheritedSingleAttribute>(a)); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public void IsDefined_OnDerivedType_WithInherit_ReturnsFalseForInheritedSingle() |
| 136 | + { |
| 137 | + TypeInfo derivedTypeInfo = typeof(DerivedWithAttributes).GetTypeInfo(); |
| 138 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 139 | + |
| 140 | + // InheritedSingleAttribute is defined on BaseWithAttributes but not on DerivedWithAttributes |
| 141 | + // CustomReflectionContext.IsDefined returns false for inherited attributes |
| 142 | + bool isDefined = customDerivedType.IsDefined(typeof(InheritedSingleAttribute), true); |
| 143 | + Assert.False(isDefined); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void IsDefined_OnDerivedType_NonInherited_ReturnsFalse() |
| 148 | + { |
| 149 | + TypeInfo derivedTypeInfo = typeof(DerivedWithAttributes).GetTypeInfo(); |
| 150 | + TypeInfo customDerivedType = _customReflectionContext.MapType(derivedTypeInfo); |
| 151 | + |
| 152 | + Assert.False(customDerivedType.IsDefined(typeof(NonInheritedAttribute), true)); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void GetCustomAttributes_FilteredByAttributeType_ReturnsMatchingAttributes() |
| 157 | + { |
| 158 | + TypeInfo baseTypeInfo = typeof(BaseWithAttributes).GetTypeInfo(); |
| 159 | + TypeInfo customBaseType = _customReflectionContext.MapType(baseTypeInfo); |
| 160 | + |
| 161 | + object[] attrs = customBaseType.GetCustomAttributes(typeof(InheritedSingleAttribute), true); |
| 162 | + Assert.All(attrs, a => Assert.IsType<InheritedSingleAttribute>(a)); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void GetCustomAttributes_OnConstructor_ReturnsEmptyForUnattributedConstructor() |
| 167 | + { |
| 168 | + TypeInfo typeInfo = typeof(TestObject).GetTypeInfo(); |
| 169 | + TypeInfo customType = _customReflectionContext.MapType(typeInfo); |
| 170 | + ConstructorInfo ctor = customType.GetConstructor(new[] { typeof(string) }); |
| 171 | + |
| 172 | + object[] attrs = ctor.GetCustomAttributes(typeof(Attribute), false); |
| 173 | + Assert.Empty(attrs); |
| 174 | + } |
| 175 | + |
| 176 | + [Fact] |
| 177 | + public void GetCustomAttributes_OnProperty_ReturnsMatchingAttributes() |
| 178 | + { |
| 179 | + TypeInfo typeInfo = typeof(TypeWithProperties).GetTypeInfo(); |
| 180 | + TypeInfo customType = _customReflectionContext.MapType(typeInfo); |
| 181 | + PropertyInfo prop = customType.GetProperty("AttributedProperty"); |
| 182 | + |
| 183 | + object[] attrs = prop.GetCustomAttributes(typeof(Attribute), false); |
| 184 | + Assert.All(attrs, a => Assert.IsAssignableFrom<Attribute>(a)); |
| 185 | + } |
| 186 | + |
| 187 | + [Fact] |
| 188 | + public void GetCustomAttributes_OnEvent_ReturnsEmptyForUnattributedEvent() |
| 189 | + { |
| 190 | + TypeInfo typeInfo = typeof(TypeWithEvent).GetTypeInfo(); |
| 191 | + TypeInfo customType = _customReflectionContext.MapType(typeInfo); |
| 192 | + EventInfo evt = customType.GetEvent("TestEvent"); |
| 193 | + |
| 194 | + object[] attrs = evt.GetCustomAttributes(typeof(Attribute), false); |
| 195 | + Assert.Empty(attrs); |
| 196 | + } |
| 197 | + |
| 198 | + [Fact] |
| 199 | + public void GetCustomAttributes_OnField_ReturnsEmptyForUnattributedField() |
| 200 | + { |
| 201 | + TypeInfo typeInfo = typeof(SecondTestObject).GetTypeInfo(); |
| 202 | + TypeInfo customType = _customReflectionContext.MapType(typeInfo); |
| 203 | + FieldInfo field = customType.GetField("field"); |
| 204 | + |
| 205 | + object[] attrs = field.GetCustomAttributes(typeof(Attribute), false); |
| 206 | + Assert.Empty(attrs); |
| 207 | + } |
| 208 | + |
| 209 | + [Fact] |
| 210 | + public void GetCustomAttributes_OnParameter_ReturnsEmptyForUnattributedParameter() |
| 211 | + { |
| 212 | + TypeInfo typeInfo = typeof(TypeWithParameters).GetTypeInfo(); |
| 213 | + TypeInfo customType = _customReflectionContext.MapType(typeInfo); |
| 214 | + MethodInfo method = customType.GetMethod("MethodWithOptionalParam"); |
| 215 | + ParameterInfo param = method.GetParameters()[1]; |
| 216 | + |
| 217 | + object[] attrs = param.GetCustomAttributes(typeof(Attribute), false); |
| 218 | + Assert.Empty(attrs); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments