forked from AutoFixture/AutoFixture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreezeOnParameterMatchCustomization.cs
66 lines (61 loc) · 2.36 KB
/
FreezeOnParameterMatchCustomization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using Ploeh.AutoFixture.Kernel;
namespace Ploeh.AutoFixture.Xunit2
{
/// <summary>
/// A customization that freezes specimens using the <see cref="ParameterInfo"/>
/// and uses them to satisfy requests that match a set of criteria.
/// </summary>
/// <remarks>
/// That allows to use the <see cref="ParameterInfo"/> custom attributes
/// (such as <see cref="StringLengthAttribute"/>) in the specimen creation pipeline.
/// </remarks>
internal class FreezeOnParameterMatchCustomization : ICustomization
{
/// <summary>
/// Initializes a new instance of the <see cref="FreezeOnParameterMatchCustomization"/> class.
/// </summary>
/// <param name="parameter">
/// The <see cref="ParameterInfo"/> used to describe a specimen to freeze.
/// </param>
/// <param name="matcher">
/// The <see cref="IRequestSpecification"/> used to match the requests
/// that will be satisfied by the frozen specimen.
/// </param>
public FreezeOnParameterMatchCustomization(
ParameterInfo parameter,
IRequestSpecification matcher)
{
this.Parameter = parameter;
this.Matcher = matcher;
}
/// <summary>
/// The <see cref="ParameterInfo"/> described a specimen to freeze.
/// </summary>
public ParameterInfo Parameter { get; }
/// <summary>
/// The <see cref="IRequestSpecification"/> used to match the requests
/// that will be satisfied by the frozen specimen.
/// </summary>
public IRequestSpecification Matcher { get; }
/// <summary>
/// Customizes the specified fixture.
/// </summary>
/// <param name="fixture">The fixture to customize.</param>
public void Customize(IFixture fixture)
{
fixture.Customizations.Insert(
0,
new FilteringSpecimenBuilder(
FreezeTargetType(fixture),
this.Matcher));
}
private ISpecimenBuilder FreezeTargetType(IFixture fixture)
{
var context = new SpecimenContext(fixture);
var specimen = context.Resolve(this.Parameter);
return new FixedBuilder(specimen);
}
}
}