/
StubbedPropertySetup.cs
108 lines (90 loc) · 2.89 KB
/
StubbedPropertySetup.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
// 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.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
namespace Moq
{
internal sealed class StubbedPropertySetup : Setup
{
private object value;
public StubbedPropertySetup(Mock mock, LambdaExpression expression, MethodInfo getter, MethodInfo setter, object initialValue)
: base(originalExpression: null, mock, new PropertyAccessorExpectation(expression, getter, setter))
{
// NOTE:
//
// At this time, this setup type does not require both a `getter` and a `setter` to be present,
// even though a stubbed property doesn't make much sense if either one is missing.
//
// This supports the `HandleAutoSetupProperties` interception step backing `SetupAllProperties`.
// Once there is another dedicated setup type for `SetupAllProperties`, we may want to require
// both accessors here.
this.value = initialValue;
this.MarkAsVerifiable();
}
public override IEnumerable<Mock> InnerMocks
{
get
{
var innerMock = TryGetInnerMockFrom(this.value);
if (innerMock != null)
{
yield return innerMock;
}
}
}
protected override void ExecuteCore(Invocation invocation)
{
if (invocation.Method.ReturnType == typeof(void))
{
Debug.Assert(invocation.Method.IsSetAccessor());
Debug.Assert(invocation.Arguments.Length == 1);
this.value = invocation.Arguments[0];
}
else
{
Debug.Assert(invocation.Method.IsGetAccessor());
invocation.ReturnValue = this.value;
}
}
public override string ToString()
{
return base.ToString() + " (stubbed)";
}
protected override void VerifySelf()
{
}
private sealed class PropertyAccessorExpectation : Expectation
{
private readonly LambdaExpression expression;
private readonly MethodInfo getter;
private readonly MethodInfo setter;
public PropertyAccessorExpectation(LambdaExpression expression, MethodInfo getter, MethodInfo setter)
{
Debug.Assert(expression != null);
Debug.Assert(expression.IsProperty());
Debug.Assert(getter != null || setter != null);
this.expression = expression;
this.getter = getter;
this.setter = setter;
}
public override LambdaExpression Expression => this.expression;
public override bool Equals(Expectation obj)
{
return obj is PropertyAccessorExpectation other
&& other.getter == this.getter
&& other.setter == this.setter;
}
public override int GetHashCode()
{
return unchecked((this.getter?.GetHashCode() ?? 0) + 103 * (this.setter?.GetHashCode() ?? 0));
}
public override bool IsMatch(Invocation invocation)
{
var method = invocation.Method;
return method == this.getter || method == this.setter;
}
}
}
}