/
TypeRelay.cs
94 lines (88 loc) · 3.6 KB
/
TypeRelay.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ploeh.AutoFixture.Kernel
{
/// <summary>
/// Maps a request for one <see cref="Type" /> to a request for another Type.
/// </summary>
public class TypeRelay : ISpecimenBuilder
{
private readonly Type from;
private readonly Type to;
/// <summary>
/// Initializes a new instance of the <see cref="TypeRelay"/> class.
/// </summary>
/// <param name="from">
/// The <see cref="Type" /> from which the TypeRelay instance should map.
/// </param>
/// <param name="to">
/// The <see cref="Type" /> to which the TypeRelay instance should map.
/// </param>
/// <remarks>
/// <para>
/// The <paramref name="from" /> and <paramref name="to" /> parameters are used by the
/// <see cref="Create" /> method to map a request for the <i>from</i> Type into a request
/// for the <i>to</i> Type.
/// </para>
/// </remarks>
/// <example>
/// In this example, BaseType is an abstract base class, and
/// DerivedType is a concrete class that derives from BaseType. The
/// Fixture instance is configured to relay all requests for BasetType
/// to requests for DerivedType, so the actual result return from the
/// fixture when BasetType is requested is a DerivedType instance.
/// <code>
/// var fixture = new Fixture();
/// fixture.Customizations.Add(
/// new TypeRelay(
/// typeof(BaseType),
/// typeof(DerivedType)));
///
/// var actual = fixture.Create<BaseType>();
/// </code>
/// </example>
public TypeRelay(Type from, Type to)
{
if (from == null)
throw new ArgumentNullException(nameof(@from));
if (to == null)
throw new ArgumentNullException(nameof(to));
this.from = from;
this.to = to;
}
/// <summary>
/// Relays a request for the <i>from</i> Type into a request for the <i>to</i> Type.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// If <paramref name="request" /> is a request for the <i>from</i> Type, an instance of
/// the <i>to</i> Type is returned; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
/// <remarks>
/// <para>
/// The <i>from</i> and <i>to</i> Types are defined by constructor arguments. An exact
/// match is performed when evaluating <paramref name="request" /> against the <i>from</i>
/// Type - i.e. derived types are not regarded as matches.
/// </para>
/// <para>
/// If the request matches the <i>from</i> Type, an instance of the <i>to</i> Type is
/// requested from the <paramref name="context" />.
/// </para>
/// </remarks>
/// <seealso cref="TypeRelay(Type, Type)" />
public object Create(object request, ISpecimenContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
var t = request as Type;
if (t == null || t != this.from)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
return context.Resolve(this.to);
}
}
}