|
| 1 | +// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. |
| 2 | +// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq.Expressions; |
| 7 | + |
| 8 | +using E = System.Linq.Expressions.Expression; |
| 9 | + |
| 10 | +namespace Moq |
| 11 | +{ |
| 12 | + internal sealed class StubbedPropertiesSetup : Setup |
| 13 | + { |
| 14 | + private readonly Dictionary<string, object> values; |
| 15 | + private readonly DefaultValueProvider defaultValueProvider; |
| 16 | + |
| 17 | + public StubbedPropertiesSetup(Mock mock, DefaultValueProvider defaultValueProvider = null) |
| 18 | + : base(originalExpression: null, mock, new PropertyAccessorExpectation(mock)) |
| 19 | + { |
| 20 | + this.values = new Dictionary<string, object>(); |
| 21 | + this.defaultValueProvider = defaultValueProvider ?? mock.DefaultValueProvider; |
| 22 | + |
| 23 | + this.MarkAsVerifiable(); |
| 24 | + } |
| 25 | + |
| 26 | + public DefaultValueProvider DefaultValueProvider => this.defaultValueProvider; |
| 27 | + |
| 28 | + public override IEnumerable<Mock> InnerMocks |
| 29 | + { |
| 30 | + get |
| 31 | + { |
| 32 | + foreach (var value in this.values.Values) |
| 33 | + { |
| 34 | + var innerMock = TryGetInnerMockFrom(value); |
| 35 | + if (innerMock != null) |
| 36 | + { |
| 37 | + yield return innerMock; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void SetProperty(string propertyName, object value) |
| 44 | + { |
| 45 | + this.values[propertyName] = value; |
| 46 | + } |
| 47 | + |
| 48 | + protected override void ExecuteCore(Invocation invocation) |
| 49 | + { |
| 50 | + if (invocation.Method.ReturnType == typeof(void)) |
| 51 | + { |
| 52 | + Debug.Assert(invocation.Method.IsSetAccessor()); |
| 53 | + Debug.Assert(invocation.Arguments.Length == 1); |
| 54 | + |
| 55 | + var propertyName = invocation.Method.Name.Substring(4); |
| 56 | + this.values[propertyName] = invocation.Arguments[0]; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Debug.Assert(invocation.Method.IsGetAccessor()); |
| 61 | + |
| 62 | + var propertyName = invocation.Method.Name.Substring(4); |
| 63 | + if (!this.values.TryGetValue(propertyName, out var value)) |
| 64 | + { |
| 65 | + value = this.values[propertyName] = this.Mock.GetDefaultValue(invocation.Method, out _, this.defaultValueProvider); |
| 66 | + } |
| 67 | + |
| 68 | + invocation.ReturnValue = value; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + protected override void VerifySelf() |
| 73 | + { |
| 74 | + } |
| 75 | + |
| 76 | + private sealed class PropertyAccessorExpectation : Expectation |
| 77 | + { |
| 78 | + private readonly LambdaExpression expression; |
| 79 | + |
| 80 | + public PropertyAccessorExpectation(Mock mock) |
| 81 | + { |
| 82 | + Debug.Assert(mock != null); |
| 83 | + |
| 84 | + var mockType = mock.GetType(); |
| 85 | + var mockedType = mockType.GetGenericArguments()[0]; |
| 86 | + var mockGetMethod = Mock.GetMethod.MakeGenericMethod(mockedType); |
| 87 | + var setupAllPropertiesMethod = mockType.GetMethod(nameof(Mock<object>.SetupAllProperties)); |
| 88 | + var mockParam = E.Parameter(mockedType, "m"); |
| 89 | + this.expression = E.Lambda(E.Call(E.Call(mockGetMethod, mockParam), setupAllPropertiesMethod), mockParam); |
| 90 | + } |
| 91 | + |
| 92 | + public override LambdaExpression Expression => this.expression; |
| 93 | + |
| 94 | + public override bool Equals(Expectation other) |
| 95 | + { |
| 96 | + return other is PropertyAccessorExpectation pae && ExpressionComparer.Default.Equals(this.expression, pae.expression); |
| 97 | + } |
| 98 | + |
| 99 | + public override int GetHashCode() |
| 100 | + { |
| 101 | + return typeof(PropertyAccessorExpectation).GetHashCode(); |
| 102 | + } |
| 103 | + |
| 104 | + public override bool IsMatch(Invocation invocation) |
| 105 | + { |
| 106 | + return invocation.Method.IsPropertyAccessor(); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments