Skip to content

Commit bb56ec9

Browse files
Implement IncludingInherited for Controller attributes
1 parent 1d9464e commit bb56ec9

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestContexts/ComponentTestContext.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ public abstract class ComponentTestContext : HttpTestContext
2222

2323
public object Component => this.component ??= this.ComponentConstructionDelegate();
2424

25-
public IEnumerable<object> ComponentAttributes
26-
=> this.componentAttributes ??= Reflection.GetCustomAttributes(this.Component);
25+
public IEnumerable<object> ComponentAttributes
26+
{
27+
get => this.componentAttributes ??= Reflection.GetCustomAttributes(this.Component);
28+
private set => this.componentAttributes = value;
29+
}
2730

2831
public IDictionary<Type, object> AggregatedDependencies
2932
=> this.aggregatedDependencies ??= new Dictionary<Type, object>();
@@ -116,6 +119,9 @@ public void Apply<TMethodResult>(InvocationTestContext<TMethodResult> invocation
116119
this.CaughtException = invocationTestContext.CaughtException;
117120
}
118121

122+
public void IncludeInheritedComponentAttributes()
123+
=> this.ComponentAttributes = Reflection.GetCustomAttributesIncludingInherited(this.Component);
124+
119125
protected virtual object ConvertMethodResult(object convertibleMethodResult) => convertibleMethodResult;
120126
}
121127
}

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Reflection.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static class Reflection
1919
{
2020
private static readonly ConcurrentDictionary<Type, ConstructorInfo> TypesWithOneConstructorCache = new ConcurrentDictionary<Type, ConstructorInfo>();
2121
private static readonly ConcurrentDictionary<Type, IEnumerable<object>> TypeAttributesCache = new ConcurrentDictionary<Type, IEnumerable<object>>();
22+
private static readonly ConcurrentDictionary<Type, IEnumerable<object>> TypeInheritedAttributesCache = new ConcurrentDictionary<Type, IEnumerable<object>>();
2223
private static readonly ConcurrentDictionary<MethodInfo, IEnumerable<object>> MethodAttributesCache = new ConcurrentDictionary<MethodInfo, IEnumerable<object>>();
2324
private static readonly ConcurrentDictionary<Type, string> FriendlyTypeNames = new ConcurrentDictionary<Type, string>();
2425
private static readonly ConcurrentDictionary<Type, string> FullFriendlyTypeNames = new ConcurrentDictionary<Type, string>();
@@ -291,6 +292,18 @@ public static IEnumerable<object> GetCustomAttributes(object obj)
291292
.GetOrAdd(type, _ => type.GetTypeInfo().GetCustomAttributes(false));
292293
}
293294

295+
/// <summary>
296+
/// Gets custom attributes including inherited ones on the provided object.
297+
/// </summary>
298+
/// <param name="obj">Object decorated with custom attribute.</param>
299+
/// <returns>IEnumerable of objects representing the custom attributes.</returns>
300+
public static IEnumerable<object> GetCustomAttributesIncludingInherited(object obj)
301+
{
302+
var type = obj.GetType();
303+
return TypeInheritedAttributesCache
304+
.GetOrAdd(type, _ => type.GetTypeInfo().GetCustomAttributes(true));
305+
}
306+
294307
public static IEnumerable<object> GetCustomAttributes(MethodInfo method)
295308
=> MethodAttributesCache
296309
.GetOrAdd(method, _ => method.GetCustomAttributes(false));

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Attributes/ControllerAttributesTestBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public ControllerAttributesTestBuilder(ControllerTestContext testContext)
2727
/// <inheritdoc />
2828
public IControllerAttributesTestBuilder AndAlso() => this;
2929

30+
/// <inheritdoc />
3031
public IAndControllerAttributesTestBuilder IncludingInherited()
31-
=> throw new System.NotImplementedException();
32+
{
33+
this.TestContext.IncludeInheritedComponentAttributes();
34+
return this;
35+
}
3236
}
3337
}

0 commit comments

Comments
 (0)