Skip to content

Commit 8fa129c

Browse files
committed
more tests and simplify ReflectionExtensions
1 parent 0364226 commit 8fa129c

2 files changed

Lines changed: 86 additions & 14 deletions

File tree

src/Paramore.Brighter/Extensions/ReflectionExtensions.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ THE SOFTWARE. */
2222

2323
#endregion
2424

25-
using System;
2625
using System.Collections.Generic;
27-
using System.Linq;
2826
using System.Reflection;
2927
using Paramore.Brighter.Inbox.Attributes;
3028

@@ -33,22 +31,13 @@ namespace Paramore.Brighter.Extensions
3331
internal static class ReflectionExtensions
3432
{
3533
internal static IEnumerable<RequestHandlerAttribute> GetOtherHandlersInPipeline(this MethodInfo targetMethod)
36-
{
37-
var customAttributes = targetMethod.GetCustomAttributes(true);
38-
return customAttributes.OfType<RequestHandlerAttribute>();
39-
}
34+
=> targetMethod.GetCustomAttributes<RequestHandlerAttribute>(true);
4035

4136
internal static IEnumerable<WrapWithAttribute> GetOtherWrapsInPipeline(this MethodInfo targetMethod)
42-
{
43-
var customAttributes = targetMethod.GetCustomAttributes(true);
44-
return customAttributes.OfType<WrapWithAttribute>();
45-
}
37+
=> targetMethod.GetCustomAttributes<WrapWithAttribute>(true);
4638

4739
internal static IEnumerable<UnwrapWithAttribute> GetOtherUnwrapsInPipeline(this MethodInfo targetMethod)
48-
{
49-
var customAttributes = targetMethod.GetCustomAttributes(true);
50-
return customAttributes.OfType<UnwrapWithAttribute>();
51-
}
40+
=> targetMethod.GetCustomAttributes<UnwrapWithAttribute>(true);
5241

5342
internal static bool HasNoInboxAttributesInPipeline(this MethodInfo targetMethod)
5443
=> targetMethod.IsDefined(typeof(NoGlobalInboxAttribute), true);

tests/Paramore.Brighter.Core.Tests/Extensions/ReflectionExtensionsTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ private sealed class AnotherUnwrapWithAttribute(int step) :
6969
public override Type GetHandlerType() => typeof(object);
7070
}
7171

72+
// Deeply inherited attributes (two levels of inheritance)
73+
private class BaseRequestHandlerAttribute(int step) :
74+
RequestHandlerAttribute(step)
75+
{
76+
public override Type GetHandlerType() => typeof(object);
77+
}
78+
79+
private sealed class DerivedRequestHandlerAttribute(int step) :
80+
BaseRequestHandlerAttribute(step);
81+
82+
private class BaseWrapWithAttribute(int step) :
83+
WrapWithAttribute(step)
84+
{
85+
public override Type GetHandlerType() => typeof(object);
86+
}
87+
88+
private sealed class DerivedWrapWithAttribute(int step) :
89+
BaseWrapWithAttribute(step);
90+
91+
private class BaseUnwrapWithAttribute(int step) :
92+
UnwrapWithAttribute(step)
93+
{
94+
public override Type GetHandlerType() => typeof(object);
95+
}
96+
97+
private sealed class DerivedUnwrapWithAttribute(int step) :
98+
BaseUnwrapWithAttribute(step);
99+
72100
private sealed class NoAttributesHandler
73101
{
74102
public void Handle() { }
@@ -140,6 +168,25 @@ private sealed class MixedAttributesHandler
140168
public void Handle() { }
141169
}
142170

171+
// Handlers with deeply inherited attributes
172+
private sealed class DeeplyInheritedRequestHandlerAttributeHandler
173+
{
174+
[DerivedRequestHandler(1)]
175+
public void Handle() { }
176+
}
177+
178+
private sealed class DeeplyInheritedWrapWithAttributeMapper
179+
{
180+
[DerivedWrapWith(1)]
181+
public void MapToMessage() { }
182+
}
183+
184+
private sealed class DeeplyInheritedUnwrapWithAttributeMapper
185+
{
186+
[DerivedUnwrapWith(1)]
187+
public void MapToRequest() { }
188+
}
189+
143190
[Fact]
144191
public void When_method_has_no_attributes_should_return_empty()
145192
{
@@ -188,6 +235,18 @@ public void When_method_has_non_request_handler_attributes_should_not_return_the
188235
Assert.IsType<TestRequestHandlerAttribute>(result[0]);
189236
}
190237

238+
[Fact]
239+
public void When_method_has_deeply_inherited_request_handler_attribute_should_return_it()
240+
{
241+
var method = typeof(DeeplyInheritedRequestHandlerAttributeHandler)
242+
.GetMethod(nameof(DeeplyInheritedRequestHandlerAttributeHandler.Handle))!;
243+
244+
var result = method.GetOtherHandlersInPipeline().ToList();
245+
246+
Assert.Single(result);
247+
Assert.IsType<DerivedRequestHandlerAttribute>(result[0]);
248+
}
249+
191250
[Fact]
192251
public void When_method_has_no_wrap_attributes_should_return_empty()
193252
{
@@ -236,6 +295,18 @@ public void When_method_has_non_wrap_attributes_should_not_return_them()
236295
Assert.IsType<TestWrapWithAttribute>(result[0]);
237296
}
238297

298+
[Fact]
299+
public void When_method_has_deeply_inherited_wrap_attribute_should_return_it()
300+
{
301+
var method = typeof(DeeplyInheritedWrapWithAttributeMapper)
302+
.GetMethod(nameof(DeeplyInheritedWrapWithAttributeMapper.MapToMessage))!;
303+
304+
var result = method.GetOtherWrapsInPipeline().ToList();
305+
306+
Assert.Single(result);
307+
Assert.IsType<DerivedWrapWithAttribute>(result[0]);
308+
}
309+
239310
[Fact]
240311
public void When_method_has_no_unwrap_attributes_should_return_empty()
241312
{
@@ -284,6 +355,18 @@ public void When_method_has_non_unwrap_attributes_should_not_return_them()
284355
Assert.IsType<TestUnwrapWithAttribute>(result[0]);
285356
}
286357

358+
[Fact]
359+
public void When_method_has_deeply_inherited_unwrap_attribute_should_return_it()
360+
{
361+
var method = typeof(DeeplyInheritedUnwrapWithAttributeMapper)
362+
.GetMethod(nameof(DeeplyInheritedUnwrapWithAttributeMapper.MapToRequest))!;
363+
364+
var result = method.GetOtherUnwrapsInPipeline().ToList();
365+
366+
Assert.Single(result);
367+
Assert.IsType<DerivedUnwrapWithAttribute>(result[0]);
368+
}
369+
287370
[Fact]
288371
public void When_method_has_no_inbox_attribute_should_return_true()
289372
{

0 commit comments

Comments
 (0)