Skip to content

Commit 47de61e

Browse files
committed
Added TypeFilterAttributeTestBuilder
1 parent 277d0af commit 47de61e

File tree

15 files changed

+737
-4
lines changed

15 files changed

+737
-4
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Attributes
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Contracts.Attributes;
8+
using Internal.TestContexts;
9+
using Utilities;
10+
using Utilities.Extensions;
11+
12+
/// <summary>
13+
/// Used for testing <see cref="TypeFilterAttribute"/>.
14+
/// </summary>
15+
public class TypeFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrder<TypeFilterAttribute>,
16+
IAndTypeFilterAttributeTestBuilder
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="TypeFilterAttributeTestBuilder"/> class.
20+
/// </summary>
21+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
22+
/// <param name="failedValidationAction">Action to call in case of failed validation.</param>
23+
public TypeFilterAttributeTestBuilder(
24+
ComponentTestContext testContext,
25+
Action<string, string> failedValidationAction)
26+
: base(testContext, nameof(TypeFilterAttribute), failedValidationAction)
27+
=> this.Attribute = new TypeFilterAttribute(typeof(object));
28+
29+
/// <inheritdoc />
30+
public IAndTypeFilterAttributeTestBuilder OfType(Type type)
31+
{
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+
46+
return this;
47+
}
48+
49+
/// <inheritdoc />
50+
public IAndTypeFilterAttributeTestBuilder WithArguments(object[] args)
51+
{
52+
this.Validations.Add((expected, actual) =>
53+
{
54+
int expectedArgsCount = expected.Arguments.Length;
55+
int actualArgsCount = actual.Arguments != null ? actual.Arguments.Length : 0;
56+
57+
if (expectedArgsCount != actualArgsCount)
58+
{
59+
this.FailedValidationAction(
60+
$"{this.ExceptionMessagePrefix}{expectedArgsCount} {(actualArgsCount != 1 ? "arguments" : "argument")}",
61+
$"in fact found {actualArgsCount}");
62+
}
63+
});
64+
65+
args.ForEach(argument => this.WithArgument(argument));
66+
67+
return this;
68+
}
69+
70+
/// <inheritdoc />
71+
public IAndTypeFilterAttributeTestBuilder WithOrder(int order)
72+
{
73+
this.ValidateOrder(order);
74+
return this;
75+
}
76+
77+
/// <inheritdoc />
78+
public ITypeFilterAttributeTestBuilder AndAlso() => this;
79+
80+
private IAndTypeFilterAttributeTestBuilder WithArgument(object arg)
81+
{
82+
var args = new List<object>(this.Attribute.Arguments ?? new object[0])
83+
{
84+
arg
85+
};
86+
87+
this.Attribute.Arguments = args.ToArray();
88+
this.Validations.Add((expected, actual) =>
89+
{
90+
var sameEntry = actual.Arguments.FirstOrDefault(entry => Reflection.AreDeeplyEqual(arg, entry));
91+
if (sameEntry == null)
92+
{
93+
this.FailedValidationAction(
94+
$"{this.ExceptionMessagePrefix}argument with the provided value",
95+
"in fact such was not found");
96+
}
97+
});
98+
99+
return this;
100+
}
101+
}
102+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
/// <summary>
4+
/// Used for adding AndAlso() method to the <see cref="Microsoft.AspNetCore.Mvc.TypeFilterAttribute"/> tests.
5+
/// </summary>
6+
public interface IAndTypeFilterAttributeTestBuilder : ITypeFilterAttributeTestBuilder
7+
{
8+
/// <summary>
9+
/// AndAlso method for better readability when testing <see cref="Microsoft.AspNetCore.Mvc.TypeFilterAttribute"/>.
10+
/// </summary>
11+
/// <returns>The same <see cref="ITypeFilterAttributeTestBuilder"/>.</returns>
12+
ITypeFilterAttributeTestBuilder AndAlso();
13+
}
14+
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Contracts/Attributes/IMiddlewareFilterAttributeTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IMiddlewareFilterAttributeTestBuilder : IBaseAttributeTestBuild
1212
/// has the same <see cref="Microsoft.AspNetCore.Mvc.MiddlewareFilterAttribute.ConfigurationType"/> value as the provided one.
1313
/// </summary>
1414
/// <param name="configurationType">A type which configures a middleware pipeline.</param>
15-
/// <returns>The same <see cref="IMiddlewareFilterAttributeTestBuilder"/>.</returns>
15+
/// <returns>The same <see cref="IAndMiddlewareFilterAttributeTestBuilder"/>.</returns>
1616
IAndMiddlewareFilterAttributeTestBuilder OfType(Type configurationType);
1717
}
1818
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Contracts/Attributes/IServiceFilterAttributeTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IServiceFilterAttributeTestBuilder : IBaseAttributeTestBuilderW
1212
/// has the same <see cref="Microsoft.AspNetCore.Mvc.ServiceFilterAttribute.ServiceType"/> value as the provided one.
1313
/// </summary>
1414
/// <param name="type">The <see cref="Type"/> of filter to find.</param>
15-
/// <returns>The same <see cref="IMiddlewareFilterAttributeTestBuilder"/>.</returns>
15+
/// <returns>The same <see cref="IAndServiceFilterAttributeTestBuilder"/>.</returns>
1616
IAndServiceFilterAttributeTestBuilder OfType(Type type);
1717
}
1818
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Used for testing <see cref="Microsoft.AspNetCore.Mvc.TypeFilterAttribute"/>.
7+
/// </summary>
8+
public interface ITypeFilterAttributeTestBuilder : IBaseAttributeTestBuilderWithOrder<IAndTypeFilterAttributeTestBuilder>
9+
{
10+
/// <summary>
11+
/// Tests whether a <see cref="Microsoft.AspNetCore.Mvc.TypeFilterAttribute"/>
12+
/// has the same <see cref="Microsoft.AspNetCore.Mvc.TypeFilterAttribute.ImplementationType"/> value as the provided one.
13+
/// </summary>
14+
/// <param name="type">The <see cref="Type"/> of filter to find.</param>
15+
/// <returns>The same <see cref="IAndTypeFilterAttributeTestBuilder"/>.</returns>
16+
IAndTypeFilterAttributeTestBuilder OfType(Type type);
17+
18+
/// <summary>
19+
/// Tests whether a <see cref="Microsoft.AspNetCore.Mvc.TypeFilterAttribute"/>
20+
/// contains the provided value in its <see cref="Microsoft.AspNetCore.Mvc.TypeFilterAttribute.Arguments"/> collection.
21+
/// </summary>
22+
/// <param name="args">Expected arguments.</param>
23+
/// <returns>The same attribute test builder.</returns>
24+
IAndTypeFilterAttributeTestBuilder WithArguments(object[] args);
25+
}
26+
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/ControllerActionAttributesTestBuilderExtensions.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,56 @@ public static TAttributesTestBuilder WithServiceFilter<TAttributesTestBuilder>(
445445
return actualBuilder.AttributesTestBuilder;
446446
}
447447

448+
/// <summary>
449+
/// Tests whether the collected attributes contain <see cref="TypeFilterAttribute"/>.
450+
/// </summary>
451+
/// <param name="controllerActionAttributesTestBuilder">
452+
/// Instance of <see cref="IControllerActionAttributesTestBuilder{TAttributesTestBuilder}"/> type.
453+
/// </param>
454+
/// <param name="type">The <see cref="Type"/> of filter to create.</param>
455+
/// <returns>The same attributes test builder.</returns>
456+
public static TAttributesTestBuilder WithTypeFilter<TAttributesTestBuilder>(
457+
this IControllerActionAttributesTestBuilder<TAttributesTestBuilder> controllerActionAttributesTestBuilder,
458+
Type type)
459+
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
460+
=> controllerActionAttributesTestBuilder
461+
.WithTypeFilter(filter => filter.OfType(type));
462+
463+
/// <summary>
464+
/// Tests whether the collected attributes contain <see cref="TypeFilterAttribute"/>.
465+
/// </summary>
466+
/// <param name="controllerActionAttributesTestBuilder">
467+
/// Instance of <see cref="IControllerActionAttributesTestBuilder{TAttributesTestBuilder}"/> type.
468+
/// </param>
469+
/// <param name="typeFilterAttributeBuilder">Expected <see cref="TypeFilterAttribute"/> builder.</param>
470+
/// <returns>The same attributes test builder.</returns>
471+
public static TAttributesTestBuilder WithTypeFilter<TAttributesTestBuilder>(
472+
this IControllerActionAttributesTestBuilder<TAttributesTestBuilder> controllerActionAttributesTestBuilder,
473+
Action<ITypeFilterAttributeTestBuilder> typeFilterAttributeBuilder)
474+
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
475+
{
476+
var actualBuilder = (BaseAttributesTestBuilder<TAttributesTestBuilder>)controllerActionAttributesTestBuilder;
477+
478+
actualBuilder.ContainingAttributeOfType<TypeFilterAttribute>();
479+
480+
actualBuilder.Validations.Add(attrs =>
481+
{
482+
var newTypeFilterAttributeBuilder = new TypeFilterAttributeTestBuilder(
483+
actualBuilder.TestContext,
484+
actualBuilder.ThrowNewAttributeAssertionException);
485+
486+
typeFilterAttributeBuilder(newTypeFilterAttributeBuilder);
487+
488+
var expectedAttribute = newTypeFilterAttributeBuilder.GetAttribute();
489+
var actualAttribute = actualBuilder.GetAttributeOfType<TypeFilterAttribute>(attrs);
490+
491+
var validations = newTypeFilterAttributeBuilder.GetAttributeValidations();
492+
validations.ForEach(v => v(expectedAttribute, actualAttribute));
493+
});
494+
495+
return actualBuilder.AttributesTestBuilder;
496+
}
497+
448498
/// <summary>
449499
/// Tests whether the collected attributes contain <see cref="RequireHttpsAttribute"/>.
450500
/// </summary>

test/MyTested.AspNetCore.Mvc.Abstractions.Test/UtilitiesTests/ExpressionParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void GetMethodAttributesShouldReturnProperAttributes()
153153
};
154154

155155
Assert.NotNull(attributes);
156-
Assert.Equal(10, attributes.Count);
156+
Assert.Equal(11, attributes.Count);
157157

158158
var allAttributesArePresent = expectedTypes.All(attributes.Contains);
159159
Assert.True(allAttributesArePresent);

0 commit comments

Comments
 (0)