-
-
Notifications
You must be signed in to change notification settings - Fork 794
/
StubbedPropertiesSetup.cs
107 lines (88 loc) · 3.11 KB
/
StubbedPropertiesSetup.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
// 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.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using E = System.Linq.Expressions.Expression;
namespace Moq
{
internal sealed class StubbedPropertiesSetup : Setup
{
private readonly ConcurrentDictionary<string, object> values;
private readonly DefaultValueProvider defaultValueProvider;
public StubbedPropertiesSetup(Mock mock, DefaultValueProvider defaultValueProvider = null)
: base(originalExpression: null, mock, new PropertyAccessorExpectation(mock))
{
this.values = new ConcurrentDictionary<string, object>();
this.defaultValueProvider = defaultValueProvider ?? mock.DefaultValueProvider;
this.MarkAsVerifiable();
}
public DefaultValueProvider DefaultValueProvider => this.defaultValueProvider;
public override IEnumerable<Mock> InnerMocks
{
get
{
foreach (var value in this.values.Values)
{
var innerMock = TryGetInnerMockFrom(value);
if (innerMock != null)
{
yield return innerMock;
}
}
}
}
public void SetProperty(string propertyName, object value)
{
this.values[propertyName] = value;
}
protected override void ExecuteCore(Invocation invocation)
{
if (invocation.Method.ReturnType == typeof(void))
{
Debug.Assert(invocation.Method.IsSetAccessor());
Debug.Assert(invocation.Arguments.Length == 1);
var propertyName = invocation.Method.Name.Substring(4);
this.values[propertyName] = invocation.Arguments[0];
}
else
{
Debug.Assert(invocation.Method.IsGetAccessor());
var propertyName = invocation.Method.Name.Substring(4);
var value = this.values.GetOrAdd(propertyName, pn => this.Mock.GetDefaultValue(invocation.Method, out _, this.defaultValueProvider));
invocation.ReturnValue = value;
}
}
protected override void VerifySelf()
{
}
private sealed class PropertyAccessorExpectation : Expectation
{
private readonly LambdaExpression expression;
public PropertyAccessorExpectation(Mock mock)
{
Debug.Assert(mock != null);
var mockType = mock.GetType();
var mockedType = mockType.GetGenericArguments()[0];
var mockGetMethod = Mock.GetMethod.MakeGenericMethod(mockedType);
var setupAllPropertiesMethod = mockType.GetMethod(nameof(Mock<object>.SetupAllProperties));
var mockParam = E.Parameter(mockedType, "m");
this.expression = E.Lambda(E.Call(E.Call(mockGetMethod, mockParam), setupAllPropertiesMethod), mockParam);
}
public override LambdaExpression Expression => this.expression;
public override bool Equals(Expectation other)
{
return other is PropertyAccessorExpectation pae && ExpressionComparer.Default.Equals(this.expression, pae.expression);
}
public override int GetHashCode()
{
return typeof(PropertyAccessorExpectation).GetHashCode();
}
public override bool IsMatch(Invocation invocation)
{
return invocation.Method.IsPropertyAccessor();
}
}
}
}