-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrangeContextBase.cs
191 lines (163 loc) · 7.2 KB
/
ArrangeContextBase.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using ArrangeContext.Core.Helper;
using ArrangeContext.Core.Helper.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ArrangeContext.Core
{
/// <summary>
/// The base-class for everything that is ArrangeContext
/// </summary>
/// <typeparam name="TContext">The Context that is worked with</typeparam>
public abstract class ArrangeContextBase<TContext> where TContext : class
{
private readonly IReflectionHelper _reflectionHelper;
private readonly IList<ContextParameter> _contextParameters;
private readonly bool _includeOptionalParameters;
private bool _initialized = false;
/// <summary>
/// Creates a new instance of the Arrange-Context
/// </summary>
/// <param name="includeOptionalParameters">If optional parameters should be considered or not</param>
protected ArrangeContextBase(bool includeOptionalParameters)
{
_includeOptionalParameters = includeOptionalParameters;
_contextParameters = new List<ContextParameter>();
_reflectionHelper = new ReflectionHelper();
}
/// <summary>
/// Builds the Context with substituted ctor-Parameters
/// </summary>
/// <returns>A new instance of <typeparamref name="TContext"/> with substituted ctor-Parameters</returns>
public TContext Build()
{
ConsiderInitialization();
var parameterArray = _contextParameters
.Select(p => p.Instance.Instance)
.ToArray();
var instance = Activator.CreateInstance(typeof(TContext), parameterArray);
return (TContext)instance;
}
/// <summary>
/// Gets a <see cref="ContextParameter"/> of the given Type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">The type of the parameter to find</typeparam>
/// <exception cref="ArgumentException">When the parameter for the type <typeparamref name="T"/> could not be found</exception>
/// <returns>The found parameter</returns>
protected ContextParameter GetParameter<T>()
{
ConsiderInitialization();
var parameter = _contextParameters.FirstOrDefault(p => p.Type == typeof(T));
if (parameter == null)
throw new ArgumentException(
$"The specified Type '{typeof(T).Name}' is not a constructor parameter of '{typeof(TContext).Name}'");
return parameter;
}
/// <summary>
/// Gets a <see cref="ContextParameter"/> with the given <paramref name="parameterName"/>
/// </summary>
/// <exception cref="ArgumentException">When the parameter for the <paramref name="parameterName"/> could not be found</exception>
/// <returns>The found parameter</returns>
protected ContextParameter GetParameter(string parameterName)
{
ConsiderInitialization();
var parameter = _contextParameters.FirstOrDefault(p =>
p.Name.Equals(parameterName, StringComparison.InvariantCultureIgnoreCase));
if (parameter == null)
throw new ArgumentException(
$"The specified parameter with name '{parameterName}' is not a constructor parameter of '{typeof(TContext).Name}'");
return parameter;
}
/// <summary>
/// Replaces the <paramref name="parameterToReplace"/> with another provided instance
/// </summary>
/// <param name="parameterToReplace">The parameter to replace</param>
/// <param name="instance">The instance to replace the current parameter with</param>
/// <param name="mockedInstance">The mocked instance of the provided instance (i.e. for Mock<Something>)</param>
protected void ReplaceInstance(
ContextParameter parameterToReplace,
object instance,
object mockedInstance)
{
ConsiderInitialization();
var index = _contextParameters.IndexOf(parameterToReplace);
_contextParameters.Remove(parameterToReplace);
var newParameter = new ContextParameter(
parameterToReplace.Type,
parameterToReplace.Name,
new ContextInstance(instance, mockedInstance));
_contextParameters.Insert(index, newParameter);
}
/// <summary>
/// The place to create the mocked instance with the used provider
/// </summary>
/// <param name="parameter">The parameter that should be mocked</param>
/// <returns>The mocked instance</returns>
protected abstract ContextInstance CreateMockedInstance(ParameterInfo parameter);
private void ConsiderInitialization()
{
if (_initialized) return;
try
{
var parameters = GetParameters();
InitializeContextParameters(parameters);
}
finally
{
_initialized = true;
}
}
private IEnumerable<ParameterInfo> GetParameters()
{
var constructor = _reflectionHelper.GetConstructor<TContext>();
var parameters = _reflectionHelper.GetParametersFor(constructor);
return parameters;
}
private void InitializeContextParameters(IEnumerable<ParameterInfo> parameters)
{
foreach (var parameter in parameters)
{
var contextParameter = InitializeContextParameterFor(parameter);
_contextParameters.Add(contextParameter);
}
}
private ContextParameter InitializeContextParameterFor(ParameterInfo parameter)
{
var instance = CreateInstance(parameter);
var contextParameter = new ContextParameter(
parameter.ParameterType,
parameter.Name,
instance);
return contextParameter;
}
private ContextInstance CreateInstance(ParameterInfo parameter)
{
try
{
if (parameter.IsOptional &&
parameter.DefaultValue == null &&
!_includeOptionalParameters)
return new ContextInstance(null, null);
var parameterType = parameter.ParameterType;
if (parameterType.IsPrimitive ||
parameterType.IsValueType)
return CreateInstanceFromActivator(parameterType);
// because strings are _extra_
if (parameterType == typeof(string))
return new ContextInstance(default(string), null);
var instance = CreateMockedInstance(parameter);
return instance;
}
catch (Exception ex)
{
throw new InstanceCreationFailedException(parameter.Name, ex);
}
}
private ContextInstance CreateInstanceFromActivator(Type type)
{
var instance = Activator.CreateInstance(type);
return new ContextInstance(instance, null);
}
}
}