Skip to content

Commit 415b0fa

Browse files
committed
Added BaseAttributeTestBuilderWithOrderAndType base class.
1 parent 47de61e commit 415b0fa

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Attributes
2+
{
3+
using System;
4+
using Internal.TestContexts;
5+
using Utilities;
6+
7+
public abstract class BaseAttributeTestBuilderWithOrderAndType<TAttribute> : BaseAttributeTestBuilderWithOrder<TAttribute>
8+
where TAttribute : Attribute
9+
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="BaseAttributeTestBuilderWithOrderAndType{TAttribute}"/> class.
12+
/// </summary>
13+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
14+
/// <param name="attributeName">Attribute name to use in case of failed validation.</param>
15+
/// <param name="failedValidationAction">Action to call in case of failed validation.</param>
16+
protected BaseAttributeTestBuilderWithOrderAndType(
17+
ComponentTestContext testContext,
18+
string attributeName,
19+
Action<string, string> failedValidationAction)
20+
: base(testContext, attributeName, failedValidationAction)
21+
{
22+
}
23+
24+
protected virtual void ValidateType(Type type, Func<TAttribute, Type> getTypeValueFunc)
25+
{
26+
this.Attribute = (TAttribute)Activator.CreateInstance(typeof(TAttribute), type);
27+
this.Validations.Add((expected, actual) =>
28+
{
29+
var expectedType = getTypeValueFunc(expected);
30+
var actualType = getTypeValueFunc(actual);
31+
32+
if (Reflection.AreDifferentTypes(expectedType, actualType))
33+
{
34+
this.FailedValidationAction(
35+
$"{this.ExceptionMessagePrefix}'{expectedType.ToFriendlyTypeName()}' type",
36+
$"in fact found '{actualType.ToFriendlyTypeName()}'");
37+
}
38+
});
39+
}
40+
}
41+
}

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// <summary>
1010
/// Used for testing <see cref="MiddlewareFilterAttribute"/>.
1111
/// </summary>
12-
public class MiddlewareFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrder<MiddlewareFilterAttribute>,
12+
public class MiddlewareFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrderAndType<MiddlewareFilterAttribute>,
1313
IAndMiddlewareFilterAttributeTestBuilder
1414
{
1515
/// <summary>
@@ -26,20 +26,7 @@ public MiddlewareFilterAttributeTestBuilder(
2626
/// <inheritdoc />
2727
public IAndMiddlewareFilterAttributeTestBuilder OfType(Type configurationType)
2828
{
29-
this.Attribute = new MiddlewareFilterAttribute(configurationType);
30-
this.Validations.Add((expected, actual) =>
31-
{
32-
var expectedType = expected.ConfigurationType;
33-
var actualType = actual.ConfigurationType;
34-
35-
if (Reflection.AreDifferentTypes(expectedType, actualType))
36-
{
37-
this.FailedValidationAction(
38-
$"{this.ExceptionMessagePrefix}'{expectedType.ToFriendlyTypeName()}' type",
39-
$"in fact found '{actualType.ToFriendlyTypeName()}'");
40-
}
41-
});
42-
29+
this.ValidateType(configurationType, attr => attr.ConfigurationType);
4330
return this;
4431
}
4532

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// <summary>
1010
/// Used for testing <see cref="ServiceFilterAttribute"/>.
1111
/// </summary>
12-
public class ServiceFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrder<ServiceFilterAttribute>,
12+
public class ServiceFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrderAndType<ServiceFilterAttribute>,
1313
IAndServiceFilterAttributeTestBuilder
1414
{
1515
/// <summary>
@@ -26,20 +26,7 @@ public ServiceFilterAttributeTestBuilder(
2626
/// <inheritdoc />
2727
public IAndServiceFilterAttributeTestBuilder OfType(Type type)
2828
{
29-
this.Attribute = new ServiceFilterAttribute(type);
30-
this.Validations.Add((expected, actual) =>
31-
{
32-
var expectedType = expected.ServiceType;
33-
var actualType = actual.ServiceType;
34-
35-
if (Reflection.AreDifferentTypes(expectedType, actualType))
36-
{
37-
this.FailedValidationAction(
38-
$"{this.ExceptionMessagePrefix}'{expectedType.ToFriendlyTypeName()}' type",
39-
$"in fact found '{actualType.ToFriendlyTypeName()}'");
40-
}
41-
});
42-
29+
this.ValidateType(type, attr => attr.ServiceType);
4330
return this;
4431
}
4532

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/// <summary>
1313
/// Used for testing <see cref="TypeFilterAttribute"/>.
1414
/// </summary>
15-
public class TypeFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrder<TypeFilterAttribute>,
15+
public class TypeFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrderAndType<TypeFilterAttribute>,
1616
IAndTypeFilterAttributeTestBuilder
1717
{
1818
/// <summary>
@@ -29,20 +29,7 @@ public TypeFilterAttributeTestBuilder(
2929
/// <inheritdoc />
3030
public IAndTypeFilterAttributeTestBuilder OfType(Type type)
3131
{
32-
this.Attribute = new TypeFilterAttribute(type);
33-
this.Validations.Add((expected, actual) =>
34-
{
35-
var expectedType = expected.ImplementationType;
36-
var actualType = actual.ImplementationType;
37-
38-
if (Reflection.AreDifferentTypes(expectedType, actualType))
39-
{
40-
this.FailedValidationAction(
41-
$"{this.ExceptionMessagePrefix}'{expectedType.ToFriendlyTypeName()}' type",
42-
$"in fact found '{actualType.ToFriendlyTypeName()}'");
43-
}
44-
});
45-
32+
this.ValidateType(type, attr => attr.ImplementationType);
4633
return this;
4734
}
4835

0 commit comments

Comments
 (0)