Skip to content

Commit 655bfbb

Browse files
committed
Extracted common methods and interfaces in all attributes test builders (#34)
1 parent 1333c08 commit 655bfbb

File tree

13 files changed

+292
-273
lines changed

13 files changed

+292
-273
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Attributes/BaseAttributesTestBuilder.cs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using Base;
76
using Internal.TestContexts;
8-
using Utilities;
9-
using Exceptions;
10-
using Utilities.Extensions;
117

128
/// <summary>
139
/// Base class for all attribute test builders.
@@ -30,62 +26,6 @@ protected BaseAttributesTestBuilder(ComponentTestContext testContext)
3026
/// <value>Collection of validation actions for the attributes.</value>
3127
protected ICollection<Action<IEnumerable<object>>> Validations { get; private set; }
3228

33-
internal ICollection<Action<IEnumerable<object>>> GetAttributeValidations()
34-
{
35-
return this.Validations;
36-
}
37-
38-
/// <summary>
39-
/// Tests whether the attributes contain the provided attribute type.
40-
/// </summary>
41-
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
42-
/// <param name="failedValidationAction">Action to execute, if the validation fails.</param>
43-
protected void ContainingAttributeOfType<TAttribute>(Action<string, string> failedValidationAction)
44-
where TAttribute : Attribute
45-
{
46-
var expectedAttributeType = typeof(TAttribute);
47-
this.Validations.Add(attrs =>
48-
{
49-
if (attrs.All(a => a.GetType() != expectedAttributeType))
50-
{
51-
failedValidationAction(
52-
expectedAttributeType.ToFriendlyTypeName(),
53-
"in fact such was not found");
54-
}
55-
});
56-
}
57-
58-
/// <summary>
59-
/// Gets an attribute of the given type from the provided collection of objects and throws exception if such is not found.
60-
/// </summary>
61-
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
62-
/// <param name="attributes">Collection of attributes.</param>
63-
/// <returns>The found attribute of the given type.</returns>
64-
protected TAttribute GetAttributeOfType<TAttribute>(IEnumerable<object> attributes)
65-
where TAttribute : Attribute
66-
{
67-
return (TAttribute)attributes.First(a => a.GetType() == typeof(TAttribute));
68-
}
69-
70-
/// <summary>
71-
/// Gets an attribute of the given type from the provided collection of objects.
72-
/// </summary>
73-
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
74-
/// <param name="attributes">Collection of attributes.</param>
75-
/// <returns>The found attribute of the given type or null, if such attribute is not found.</returns>
76-
protected TAttribute TryGetAttributeOfType<TAttribute>(IEnumerable<object> attributes)
77-
where TAttribute : Attribute
78-
{
79-
return attributes.FirstOrDefault(a => a.GetType() == typeof(TAttribute)) as TAttribute;
80-
}
81-
82-
protected virtual void ThrowNewAttributeAssertionException(string expectedValue, string actualValue)
83-
{
84-
throw new AttributeAssertionException(string.Format(
85-
"When testing {0} was expected to have {1}, but {2}.",
86-
this.TestContext.Component.GetName(),
87-
expectedValue,
88-
actualValue));
89-
}
29+
internal IEnumerable<Action<IEnumerable<object>>> GetAttributeValidations() => this.Validations;
9030
}
9131
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Attributes
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Base;
7+
using Exceptions;
8+
using Internal.TestContexts;
9+
using Utilities;
10+
using Utilities.Extensions;
11+
using Contracts.Attributes;
12+
13+
/// <summary>
14+
/// Base class for all attribute test builders containing common assertion methods.
15+
/// </summary>
16+
/// <typeparam name="TAttributesTestBuilder">Type of attributes test builder to use as a return type for common methods.</typeparam>
17+
public abstract class BaseAttributesTestBuilder<TAttributesTestBuilder> : BaseAttributesTestBuilder
18+
where TAttributesTestBuilder : IBaseAttributesTestBuilder<TAttributesTestBuilder>
19+
{
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="BaseAttributesTestBuilder{TAttributesBuilder}"/> class.
22+
/// </summary>
23+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
24+
protected BaseAttributesTestBuilder(ComponentTestContext testContext)
25+
: base(testContext)
26+
{
27+
this.AttributesBuilder = this.GetAttributesTestBuilder();
28+
}
29+
30+
protected TAttributesTestBuilder AttributesBuilder { get; private set; }
31+
32+
protected abstract TAttributesTestBuilder GetAttributesTestBuilder();
33+
34+
/// <summary>
35+
/// Tests whether the attributes contain the provided attribute type.
36+
/// </summary>
37+
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
38+
/// <returns>The same test builder of <see cref="BaseAttributesTestBuilder{TAttributesBuilder}"/> type.</returns>
39+
public TAttributesTestBuilder ContainingAttributeOfType<TAttribute>()
40+
where TAttribute : Attribute
41+
{
42+
var expectedAttributeType = typeof(TAttribute);
43+
this.Validations.Add(attrs =>
44+
{
45+
if (attrs.All(a => a.GetType() != expectedAttributeType))
46+
{
47+
this.ThrowNewAttributeAssertionException(
48+
expectedAttributeType.ToFriendlyTypeName(),
49+
"in fact such was not found");
50+
}
51+
});
52+
53+
return this.AttributesBuilder;
54+
}
55+
56+
/// <summary>
57+
/// Gets an attribute of the given type from the provided collection of objects and throws exception if such is not found.
58+
/// </summary>
59+
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
60+
/// <param name="attributes">Collection of attributes.</param>
61+
/// <returns>The found attribute of the given type.</returns>
62+
protected TAttribute GetAttributeOfType<TAttribute>(IEnumerable<object> attributes)
63+
where TAttribute : Attribute
64+
{
65+
return (TAttribute)attributes.First(a => a.GetType() == typeof(TAttribute));
66+
}
67+
68+
/// <summary>
69+
/// Gets an attribute of the given type from the provided collection of objects.
70+
/// </summary>
71+
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
72+
/// <param name="attributes">Collection of attributes.</param>
73+
/// <returns>The found attribute of the given type or null, if such attribute is not found.</returns>
74+
protected TAttribute TryGetAttributeOfType<TAttribute>(IEnumerable<object> attributes)
75+
where TAttribute : Attribute
76+
{
77+
return attributes.FirstOrDefault(a => a.GetType() == typeof(TAttribute)) as TAttribute;
78+
}
79+
80+
protected virtual void ThrowNewAttributeAssertionException(string expectedValue, string actualValue)
81+
{
82+
throw new AttributeAssertionException(string.Format(
83+
"When testing {0} was expected to have {1}, but {2}.",
84+
this.TestContext.Component.GetName(),
85+
expectedValue,
86+
actualValue));
87+
}
88+
}
89+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Attributes/IBaseAttributesTestBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
{
33
using System;
44

5+
/// <summary>
6+
/// Base interface for all attribute test builders.
7+
/// </summary>
8+
/// <typeparam name="TAttributesTestBuilder">Type of attributes test builder to use as a return type for common methods.</typeparam>
59
public interface IBaseAttributesTestBuilder<TAttributesTestBuilder>
610
{
711
/// <summary>

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

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Contracts.Attributes;
77
using Exceptions;
88
using Internal.TestContexts;
9-
using Microsoft.AspNetCore.Authorization;
109
using Microsoft.AspNetCore.Mvc;
1110
using Microsoft.AspNetCore.Mvc.Routing;
1211
using Utilities.Extensions;
@@ -16,7 +15,8 @@
1615
/// <summary>
1716
/// Used for testing action attributes.
1817
/// </summary>
19-
public class ActionAttributesTestBuilder : ControllerActionAttributesTestBuilder, IAndActionAttributesTestBuilder
18+
public class ActionAttributesTestBuilder : ControllerActionAttributesTestBuilder<IAndActionAttributesTestBuilder>,
19+
IAndActionAttributesTestBuilder
2020
{
2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="ActionAttributesTestBuilder"/> class.
@@ -27,14 +27,6 @@ public ActionAttributesTestBuilder(ControllerTestContext testContext)
2727
{
2828
}
2929

30-
/// <inheritdoc />
31-
public IAndActionAttributesTestBuilder ContainingAttributeOfType<TAttribute>()
32-
where TAttribute : Attribute
33-
{
34-
this.ContainingAttributeOfType<TAttribute>(this.ThrowNewAttributeAssertionException);
35-
return this;
36-
}
37-
3830
/// <inheritdoc />
3931
public IAndActionAttributesTestBuilder ChangingActionNameTo(string actionName)
4032
{
@@ -53,75 +45,31 @@ public IAndActionAttributesTestBuilder ChangingActionNameTo(string actionName)
5345

5446
return this;
5547
}
56-
57-
/// <inheritdoc />
58-
public IAndActionAttributesTestBuilder ChangingRouteTo(
59-
string template,
60-
string withName = null,
61-
int? withOrder = null)
62-
{
63-
this.ChangingRouteTo(
64-
template,
65-
this.ThrowNewAttributeAssertionException,
66-
withName,
67-
withOrder);
68-
69-
return this;
70-
}
71-
72-
/// <inheritdoc />
73-
public IAndActionAttributesTestBuilder AllowingAnonymousRequests()
74-
{
75-
return this.ContainingAttributeOfType<AllowAnonymousAttribute>();
76-
}
77-
78-
/// <inheritdoc />
79-
public IAndActionAttributesTestBuilder RestrictingForAuthorizedRequests(
80-
string withAllowedRoles = null)
81-
{
82-
this.RestrictingForAuthorizedRequests(
83-
this.ThrowNewAttributeAssertionException,
84-
withAllowedRoles);
85-
86-
return this;
87-
}
88-
48+
8949
/// <inheritdoc />
9050
public IAndActionAttributesTestBuilder DisablingActionCall()
91-
{
92-
return this.ContainingAttributeOfType<NonActionAttribute>();
93-
}
51+
=> this.ContainingAttributeOfType<NonActionAttribute>();
9452

9553
/// <inheritdoc />
9654
public IAndActionAttributesTestBuilder RestrictingForHttpMethod<THttpMethod>()
9755
where THttpMethod : Attribute, IActionHttpMethodProvider, new()
98-
{
99-
return this.RestrictingForHttpMethods(new THttpMethod().HttpMethods);
100-
}
56+
=> this.RestrictingForHttpMethods(new THttpMethod().HttpMethods);
10157

10258
/// <inheritdoc />
10359
public IAndActionAttributesTestBuilder RestrictingForHttpMethod(string httpMethod)
104-
{
105-
return this.RestrictingForHttpMethod(new HttpMethod(httpMethod));
106-
}
60+
=> this.RestrictingForHttpMethod(new HttpMethod(httpMethod));
10761

10862
/// <inheritdoc />
10963
public IAndActionAttributesTestBuilder RestrictingForHttpMethod(HttpMethod httpMethod)
110-
{
111-
return this.RestrictingForHttpMethods(new List<HttpMethod> { httpMethod });
112-
}
64+
=> this.RestrictingForHttpMethods(new List<HttpMethod> { httpMethod });
11365

11466
/// <inheritdoc />
11567
public IAndActionAttributesTestBuilder RestrictingForHttpMethods(IEnumerable<string> httpMethods)
116-
{
117-
return this.RestrictingForHttpMethods(httpMethods.Select(m => new HttpMethod(m)));
118-
}
68+
=> this.RestrictingForHttpMethods(httpMethods.Select(m => new HttpMethod(m)));
11969

12070
/// <inheritdoc />
12171
public IAndActionAttributesTestBuilder RestrictingForHttpMethods(params string[] httpMethods)
122-
{
123-
return this.RestrictingForHttpMethods(httpMethods.AsEnumerable());
124-
}
72+
=> this.RestrictingForHttpMethods(httpMethods.AsEnumerable());
12573

12674
/// <inheritdoc />
12775
public IAndActionAttributesTestBuilder RestrictingForHttpMethods(IEnumerable<HttpMethod> httpMethods)
@@ -148,15 +96,12 @@ public IAndActionAttributesTestBuilder RestrictingForHttpMethods(IEnumerable<Htt
14896

14997
/// <inheritdoc />
15098
public IAndActionAttributesTestBuilder RestrictingForHttpMethods(params HttpMethod[] httpMethods)
151-
{
152-
return this.RestrictingForHttpMethods(httpMethods.AsEnumerable());
153-
}
99+
=> this.RestrictingForHttpMethods(httpMethods.AsEnumerable());
154100

155101
/// <inheritdoc />
156-
public IActionAttributesTestBuilder AndAlso()
157-
{
158-
return this;
159-
}
102+
public IActionAttributesTestBuilder AndAlso() => this;
103+
104+
protected override IAndActionAttributesTestBuilder GetAttributesTestBuilder() => this;
160105

161106
protected override void ThrowNewAttributeAssertionException(string expectedValue, string actualValue)
162107
{

0 commit comments

Comments
 (0)