-
-
Notifications
You must be signed in to change notification settings - Fork 799
/
MethodCallReturn.cs
170 lines (147 loc) · 5.04 KB
/
MethodCallReturn.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
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using Moq.Properties;
namespace Moq
{
internal sealed partial class MethodCallReturn : MethodCall
{
// This enum exists for reasons of optimizing memory usage.
// Previously this class had two `bool` fields, `hasReturnValue` and `callBase`.
// Using an enum allows us to combine them into a single field.
private enum ReturnValueKind : byte
{
None = 0,
Explicit,
CallBase,
}
private Delegate valueDel;
private Action<object[]> afterReturnCallback;
private ReturnValueKind returnValueKind;
public MethodCallReturn(Mock mock, Condition condition, LambdaExpression originalExpression, MethodInfo method, IReadOnlyList<Expression> arguments)
: base(mock, condition, originalExpression, method, arguments)
{
}
public bool ProvidesReturnValue() => this.returnValueKind != ReturnValueKind.None;
public Type ReturnType => this.Method.ReturnType;
public override void SetCallbackResponse(Delegate callback)
{
if (this.ProvidesReturnValue())
{
if (callback is Action callbackWithoutArguments)
{
this.afterReturnCallback = delegate { callbackWithoutArguments(); };
}
else
{
this.afterReturnCallback = delegate (object[] args) { callback.InvokePreserveStack(args); };
}
}
else
{
base.SetCallbackResponse(callback);
}
}
public override void SetCallBaseResponse()
{
this.returnValueKind = ReturnValueKind.CallBase;
}
public void SetReturnsResponse(Delegate value)
{
if (value == null)
{
// A `null` reference (instead of a valid delegate) is interpreted as the actual return value.
// This is necessary because the compiler might have picked the unexpected overload for calls
// like `Returns(null)`, or the user might have picked an overload like `Returns<T>(null)`,
// and instead of in `Returns(TResult)`, we ended up in `Returns(Delegate)` or `Returns(Func)`,
// which likely isn't what the user intended.
// So here we do what we would've done in `Returns(TResult)`:
this.valueDel = new Func<object>(() => this.ReturnType.GetDefaultValue());
}
else if (this.ReturnType == typeof(Delegate))
{
// If `TResult` is `Delegate`, that is someone is setting up the return value of a method
// that returns a `Delegate`, then we have arrived here because C# picked the wrong overload:
// We don't want to invoke the passed delegate to get a return value; the passed delegate
// already is the return value.
this.valueDel = new Func<Delegate>(() => value);
}
else
{
ValidateCallback(value);
this.valueDel = value;
}
this.returnValueKind = ReturnValueKind.Explicit;
void ValidateCallback(Delegate callback)
{
var callbackMethod = callback.GetMethodInfo();
// validate number of parameters:
var numberOfActualParameters = callbackMethod.GetParameters().Length;
if (callbackMethod.IsStatic)
{
if (callbackMethod.IsExtensionMethod() || callback.Target != null)
{
numberOfActualParameters--;
}
}
if (numberOfActualParameters > 0)
{
var numberOfExpectedParameters = this.Method.GetParameters().Length;
if (numberOfActualParameters != numberOfExpectedParameters)
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.InvalidCallbackParameterCountMismatch,
numberOfExpectedParameters,
numberOfActualParameters));
}
}
// validate return type:
var actualReturnType = callbackMethod.ReturnType;
if (actualReturnType == typeof(void))
{
throw new ArgumentException(Resources.InvalidReturnsCallbackNotADelegateWithReturnType);
}
var expectedReturnType = this.Method.ReturnType;
if (!expectedReturnType.IsAssignableFrom(actualReturnType))
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.InvalidCallbackReturnTypeMismatch,
expectedReturnType,
actualReturnType));
}
}
}
public override void Execute(Invocation invocation)
{
base.Execute(invocation);
if (this.returnValueKind == ReturnValueKind.CallBase)
{
invocation.ReturnBase();
}
else if (this.valueDel != null)
{
invocation.Return(this.valueDel.CompareParameterTypesTo(Type.EmptyTypes)
? valueDel.InvokePreserveStack() //we need this, for the user to be able to use parameterless methods
: valueDel.InvokePreserveStack(invocation.Arguments)); //will throw if parameters mismatch
}
else if (this.Mock.Behavior == MockBehavior.Strict)
{
throw MockException.ReturnValueRequired(invocation);
}
else
{
invocation.Return(this.ReturnType.GetDefaultValue());
}
this.afterReturnCallback?.Invoke(invocation.Arguments);
}
}
}