-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
ActionObserver.cs
318 lines (284 loc) · 15.4 KB
/
ActionObserver.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Moq.Async;
using Moq.Expressions.Visitors;
using Moq.Internals;
using Moq.Properties;
using TypeNameFormatter;
namespace Moq
{
/// <summary>
/// <see cref="ActionObserver"/> is a kind of <see cref="ExpressionReconstructor"/> that works by
/// applying a <see cref="Action{T}"/> delegate to a light-weight proxy that records the invocation
/// happening to it, and auto-generates the same kind of recording proxy for its return value.
/// That way, a chain of invocation records is generated from which a LINQ expression tree can be
/// reconstructed.
/// </summary>
sealed class ActionObserver : ExpressionReconstructor
{
public override Expression<Action<T>> ReconstructExpression<T>(Action<T> action, object[] ctorArgs = null)
{
using (var matcherObserver = MatcherObserver.Activate())
{
// Create the root recording proxy:
var root = (T)CreateProxy(typeof(T), ctorArgs, matcherObserver, out var rootRecorder);
Exception error = null;
try
{
// Execute the delegate. The root recorder will automatically "mock" return values
// and so build a chain of recorders, whereby each one records a single invocation
// in a method chain `o.X.Y.Z`:
action.Invoke(root);
}
catch (Exception ex)
{
// Something went wrong. We don't return this error right away. We want to
// rebuild the expression tree as far as possible for diagnostic purposes.
error = ex;
}
// Start the expression tree with a parameter of type `T`:
var actionParameters = action.GetMethodInfo().GetParameters();
var actionParameterName = actionParameters[actionParameters.Length - 1].Name;
var rootExpression = Expression.Parameter(typeof(T), actionParameterName);
Expression body = rootExpression;
// Then step through one recorded invocation at a time:
for (var recorder = rootRecorder; recorder != null; recorder = recorder.Next)
{
var invocation = recorder.Invocation;
if (invocation != null)
{
var resultType = invocation.Method.DeclaringType;
if (resultType.IsAssignableFrom(body.Type) == false)
{
if (AwaitableFactory.TryGet(body.Type) is { } awaitableHandler
&& awaitableHandler.ResultType.IsAssignableFrom(resultType))
{
// We are here because the current invocation cannot be chained onto the previous one,
// however it *can* be chained if we assume that there was a `.Result` query on the
// former invocation that we don't see because non-virtual members aren't recorded.
// In this case, we make things work by adding back the missing `.Result`:
body = awaitableHandler.CreateResultExpression(body);
}
}
body = Expression.Call(body, invocation.Method, GetArgumentExpressions(invocation, recorder.Matches.ToArray()));
}
else
{
// A recorder was set up, but it recorded no invocation. This means
// that the invocation could not be intercepted:
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.UnsupportedExpressionWithHint,
$"{actionParameterName} => {body.ToStringFixed()}...",
Resources.NextMemberNonInterceptable));
}
}
// Now we've either got no error and a completely reconstructed expression, or
// we have an error and a partially reconstructed expression which we can use for
// diagnostic purposes:
if (error == null)
{
return Expression.Lambda<Action<T>>(body.Apply(UpgradePropertyAccessorMethods.Rewriter), rootExpression);
}
else
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.UnsupportedExpressionWithHint,
$"{actionParameterName} => {body.ToStringFixed()}...",
error.Message));
}
}
Expression[] GetArgumentExpressions(Invocation invocation, Match[] matches)
{
// First, let's pretend that all arguments are constant values:
var parameterTypes = invocation.Method.GetParameterTypes();
var parameterCount = parameterTypes.Count;
var expressions = new Expression[parameterCount];
for (int i = 0; i < parameterCount; ++i)
{
expressions[i] = Expression.Constant(invocation.Arguments[i], parameterTypes[i]);
}
// Now let's override the above constant expressions with argument matchers, if available:
if (matches.Length > 0)
{
int matchIndex = 0;
for (int argumentIndex = 0; matchIndex < matches.Length && argumentIndex < expressions.Length; ++argumentIndex)
{
// We are assuming that by default matchers return `default(T)`. If a matcher was used,
// it will have left behind a `default(T)` argument, possibly coerced to the parameter type.
// Therefore, we attempt to reproduce such coercions using `Convert.ChangeType`:
Type defaultValueType = matches[matchIndex].RenderExpression.Type;
object defaultValue = defaultValueType.GetDefaultValue();
try
{
defaultValue = Convert.ChangeType(defaultValue, parameterTypes[argumentIndex]);
}
catch
{
// Never mind, we tried.
}
if (!object.Equals(invocation.Arguments[argumentIndex], defaultValue))
{
// This parameter has a non-`default` value. We therefore assume that it isn't
// a value that was produced by a matcher. (See explanation in comment above.)
continue;
}
if (parameterTypes[argumentIndex].IsAssignableFrom(defaultValue?.GetType() ?? defaultValueType))
{
// We found a potential match. (Matcher type is assignment-compatible to parameter type.)
if (matchIndex < matches.Length - 1
&& !(argumentIndex < expressions.Length - 1 || CanDistribute(matchIndex + 1, argumentIndex + 1)))
{
// We get here if there are more matchers to distribute,
// but we either:
// * ran out of parameters to distribute over, or
// * the remaining matchers can't be distributed over the remaining parameters.
// In this case, we bail out, which will lead to an exception being thrown.
break;
}
// The remaining matchers can be distributed over the remaining parameters,
// so we can use up this matcher:
expressions[argumentIndex] = new MatchExpression(matches[matchIndex]);
++matchIndex;
}
}
if (matchIndex < matches.Length)
{
// If we get here, we can be almost certain that matchers weren't distributed properly
// across the invocation's parameters. We could hope for the best and just leave it
// at that; however, it's probably better to let client code know, so it can be either
// adjusted or reported to Moq.
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.MatcherAssignmentFailedDuringExpressionReconstruction,
matches.Length,
$"{invocation.Method.DeclaringType.GetFormattedName()}.{invocation.Method.Name}"));
}
bool CanDistribute(int msi, int asi)
{
var match = matches[msi];
var matchType = match.RenderExpression.Type;
for (int ai = asi; ai < expressions.Length; ++ai)
{
if (parameterTypes[ai].IsAssignableFrom(matchType)
&& CanDistribute(msi + 1, ai + 1))
{
return true;
}
}
return false;
}
}
// Finally, add explicit type casts (aka `Convert` nodes) where necessary:
for (int i = 0; i < expressions.Length; ++i)
{
var argument = expressions[i];
var parameterType = parameterTypes[i];
if (argument.Type == parameterType) continue;
// nullable type coercion:
if (Nullable.GetUnderlyingType(parameterType) != null && Nullable.GetUnderlyingType(argument.Type) == null)
{
expressions[i] = Expression.Convert(argument, parameterType);
}
// boxing of value types (i.e. where a value-typed value is assigned to a reference-typed parameter):
else if (argument.Type.IsValueType && !parameterType.IsValueType)
{
expressions[i] = Expression.Convert(argument, parameterType);
}
// if types don't match exactly and aren't assignment compatible:
else if (argument.Type != parameterType && !parameterType.IsAssignableFrom(argument.Type))
{
expressions[i] = Expression.Convert(argument, parameterType);
}
}
return expressions;
}
}
// Creates a proxy (way more light-weight than a `Mock<T>`!) with an invocation `Recorder` attached to it.
static IProxy CreateProxy(Type type, object[] ctorArgs, MatcherObserver matcherObserver, out Recorder recorder)
{
recorder = new Recorder(matcherObserver);
return (IProxy)ProxyFactory.Instance.CreateProxy(type, recorder, Type.EmptyTypes, ctorArgs ?? new object[0]);
}
// Records an invocation, mocks return values, and builds a chain to the return value's recorder.
// This record represents the basis for reconstructing an expression tree.
sealed class Recorder : IInterceptor
{
readonly MatcherObserver matcherObserver;
int creationTimestamp;
Invocation invocation;
int invocationTimestamp;
object returnValue;
public Recorder(MatcherObserver matcherObserver)
{
Debug.Assert(matcherObserver != null);
this.matcherObserver = matcherObserver;
this.creationTimestamp = this.matcherObserver.GetNextTimestamp();
}
public Invocation Invocation => this.invocation;
public IEnumerable<Match> Matches
{
get
{
Debug.Assert(this.invocationTimestamp != default);
return this.matcherObserver.GetMatchesBetween(this.creationTimestamp, this.invocationTimestamp);
}
}
public Recorder Next => (Awaitable.TryGetResultRecursive(this.returnValue) as IProxy)?.Interceptor as Recorder;
public void Intercept(Invocation invocation)
{
var returnType = invocation.Method.ReturnType;
// In theory, each recorder should receive exactly one invocation.
// There are some reasons why that may not always be true:
//
// 1. You may be inspecting a `Recorder` object in your IDE, causing
// additional calls e.g. to `ToString`. In this case, any such
// subsequent calls should be ignored.
//
// 2. The proxied type may perform virtual calls in its own ctor.
// In this case, *only* the last call is going to be relevant.
//
// Getting (2) right is more important than getting (1) right, so we
// disable the following guard and allow subsequent calls to override
// earlier ones:
//if (this.invocation == null)
{
this.invocation = invocation;
this.invocationTimestamp = this.matcherObserver.GetNextTimestamp();
if (returnType == typeof(void))
{
this.returnValue = null;
}
else if (AwaitableFactory.TryGet(returnType) is { } awaitableFactory)
{
var result = CreateProxy(awaitableFactory.ResultType, null, this.matcherObserver, out _);
this.returnValue = awaitableFactory.CreateCompleted(result);
}
else if (returnType.IsMockable())
{
this.returnValue = CreateProxy(returnType, null, this.matcherObserver, out _);
}
else
{
throw new NotSupportedException(Resources.LastMemberHasNonInterceptableReturnType);
}
}
if (returnType != typeof(void))
{
invocation.ReturnValue = this.returnValue;
}
}
}
}
}