Skip to content

Commit 5db2a42

Browse files
committed
Remove current SetupAllProperties impl
1 parent a0243cd commit 5db2a42

File tree

7 files changed

+2
-132
lines changed

7 files changed

+2
-132
lines changed

src/Moq/AsInterface.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ public override DefaultValueProvider DefaultValueProvider
4343
set => this.owner.DefaultValueProvider = value;
4444
}
4545

46-
internal override DefaultValueProvider AutoSetupPropertiesDefaultValueProvider
47-
{
48-
get => this.owner.AutoSetupPropertiesDefaultValueProvider;
49-
set => this.owner.AutoSetupPropertiesDefaultValueProvider = value;
50-
}
51-
5246
internal override EventHandlerCollection EventHandlers => this.owner.EventHandlers;
5347

5448
internal override Type[] InheritedInterfaces => this.owner.InheritedInterfaces;

src/Moq/Interception/InterceptionAspects.cs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics;
76
using System.Linq;
8-
using System.Linq.Expressions;
97
using System.Reflection;
108

119
using Moq.Behaviors;
@@ -175,59 +173,6 @@ public static void Handle(Invocation invocation, Mock mock)
175173
}
176174
}
177175

178-
internal static class HandleAutoSetupProperties
179-
{
180-
private static readonly int AccessorPrefixLength = "?et_".Length; // get_ or set_
181-
182-
public static bool Handle(Invocation invocation, Mock mock)
183-
{
184-
if (mock.AutoSetupPropertiesDefaultValueProvider == null)
185-
{
186-
return false;
187-
}
188-
189-
MethodInfo invocationMethod = invocation.Method;
190-
if (!invocationMethod.IsPropertyAccessor())
191-
{
192-
return false;
193-
}
194-
195-
string propertyName = invocationMethod.Name.Substring(AccessorPrefixLength);
196-
PropertyInfo property = invocationMethod.DeclaringType.GetProperty(propertyName, Type.EmptyTypes);
197-
Debug.Assert(property != null);
198-
199-
bool accessorFound = property.CanRead(out var getter) | property.CanWrite(out var setter);
200-
Debug.Assert(accessorFound);
201-
202-
var expression = GetPropertyExpression(invocationMethod.DeclaringType, property);
203-
var initialValue = getter != null ? CreateInitialPropertyValue(mock, getter) : null;
204-
var setup = new StubbedPropertySetup(mock, expression, getter, setter, initialValue);
205-
mock.MutableSetups.Add(setup);
206-
setup.Execute(invocation);
207-
208-
return true;
209-
}
210-
211-
private static object CreateInitialPropertyValue(Mock mock, MethodInfo getter)
212-
{
213-
object initialValue = mock.GetDefaultValue(getter, out Mock innerMock,
214-
useAlternateProvider: mock.AutoSetupPropertiesDefaultValueProvider);
215-
216-
if (innerMock != null)
217-
{
218-
Mock.SetupAllProperties(innerMock, mock.AutoSetupPropertiesDefaultValueProvider);
219-
}
220-
221-
return initialValue;
222-
}
223-
224-
private static LambdaExpression GetPropertyExpression(Type mockType, PropertyInfo property)
225-
{
226-
var param = Expression.Parameter(mockType, "m");
227-
return Expression.Lambda(Expression.MakeMemberAccess(param, property), param);
228-
}
229-
}
230-
231176
internal static class FailForStrictMock
232177
{
233178
public static void Handle(Invocation invocation, Mock mock)

src/Moq/Interception/Mock.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ void IInterceptor.Intercept(Invocation invocation)
1919
return;
2020
}
2121

22-
if (HandleAutoSetupProperties.Handle(invocation, this))
23-
{
24-
return;
25-
}
26-
2722
if (HandleEventSubscription.Handle(invocation, this))
2823
{
2924
return;

src/Moq/Mock.cs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,6 @@ public DefaultValue DefaultValue
209209
/// </summary>
210210
public abstract DefaultValueProvider DefaultValueProvider { get; set; }
211211

212-
/// <summary>
213-
/// The <see cref="Moq.DefaultValueProvider"/> used to initialize automatically stubbed properties.
214-
/// It is equal to the value of <see cref="DefaultValueProvider"/> at the time when
215-
/// <see cref="SetupAllProperties"/> was last called.
216-
/// </summary>
217-
internal abstract DefaultValueProvider AutoSetupPropertiesDefaultValueProvider { get; set; }
218-
219212
internal abstract SetupCollection MutableSetups { get; }
220213

221214
/// <summary>
@@ -539,26 +532,7 @@ internal static void SetupSet(Mock mock, LambdaExpression expression, PropertyIn
539532

540533
Mock.SetupRecursive<MethodCall>(mock, expression, setupLast: (targetMock, _, __) =>
541534
{
542-
// Setting a mock's property through reflection will only work (i.e. the property will only remember the value
543-
// it's being set to) if it is being stubbed. In order to ensure it's stubbed, we temporarily enable
544-
// auto-stubbing (if that isn't already switched on).
545-
546-
var temporaryAutoSetupProperties = targetMock.AutoSetupPropertiesDefaultValueProvider == null;
547-
if (temporaryAutoSetupProperties)
548-
{
549-
targetMock.AutoSetupPropertiesDefaultValueProvider = targetMock.DefaultValueProvider;
550-
}
551-
try
552-
{
553-
propertyToSet.SetValue(targetMock.Object, value, null);
554-
}
555-
finally
556-
{
557-
if (temporaryAutoSetupProperties)
558-
{
559-
targetMock.AutoSetupPropertiesDefaultValueProvider = null;
560-
}
561-
}
535+
propertyToSet.SetValue(targetMock.Object, value, null);
562536
return null;
563537
}, allowNonOverridableLastProperty: true);
564538
}
@@ -637,21 +611,7 @@ private static TSetup SetupRecursive<TSetup>(Mock mock, LambdaExpression origina
637611

638612
internal static void SetupAllProperties(Mock mock)
639613
{
640-
SetupAllProperties(mock, mock.DefaultValueProvider);
641-
}
642-
643-
internal static void SetupAllProperties(Mock mock, DefaultValueProvider defaultValueProvider)
644-
{
645-
mock.MutableSetups.RemoveAllPropertyAccessorSetups();
646-
// Removing all the previous properties setups to keep the behaviour of overriding
647-
// existing setups in `SetupAllProperties`.
648-
649-
mock.AutoSetupPropertiesDefaultValueProvider = defaultValueProvider;
650-
// `SetupAllProperties` no longer performs properties setup like in previous versions.
651-
// Instead it just enables a switch to setup properties on-demand at the moment of first access.
652-
// In order for `SetupAllProperties`'s new mode of operation to be indistinguishable
653-
// from how it worked previously, it's important to capture the default value provider at this precise
654-
// moment, since it might be changed later (before queries to properties).
614+
// TODO: implement!
655615
}
656616

657617
#endregion

src/Moq/MockDefaultValueProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ protected override object GetFallbackDefaultValue(Type type, Mock mock)
3636
var mockType = typeof(Mock<>).MakeGenericType(type);
3737
Mock newMock = (Mock)Activator.CreateInstance(mockType, mock.Behavior);
3838
newMock.DefaultValueProvider = mock.DefaultValueProvider;
39-
newMock.AutoSetupPropertiesDefaultValueProvider = mock.AutoSetupPropertiesDefaultValueProvider;
4039
if(!type.IsDelegateType())
4140
{
4241
newMock.CallBase = mock.CallBase;

src/Moq/Mock`1.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ public override DefaultValueProvider DefaultValueProvider
265265
set => this.defaultValueProvider = value ?? throw new ArgumentNullException(nameof(value));
266266
}
267267

268-
internal override DefaultValueProvider AutoSetupPropertiesDefaultValueProvider { get; set; }
269-
270268
internal override EventHandlerCollection EventHandlers => this.eventHandlers;
271269

272270
internal override List<Type> AdditionalInterfaces => this.additionalInterfaces;

src/Moq/SetupCollection.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7-
using System.Linq;
87

98
namespace Moq
109
{
@@ -72,26 +71,6 @@ private void MarkOverriddenSetups()
7271
}
7372
}
7473

75-
public void RemoveAllPropertyAccessorSetups()
76-
{
77-
// Fast path (no `lock`) when there are no setups:
78-
if (this.setups.Count == 0)
79-
{
80-
return;
81-
}
82-
83-
lock (this.setups)
84-
{
85-
this.setups.RemoveAll(s => s is StubbedPropertySetup || (s is MethodSetup ms && ms.Method.IsPropertyAccessor()));
86-
87-
// NOTE: In the general case, removing a setup means that some overridden setups might no longer
88-
// be shadowed, and their `IsOverridden` flag should go back from `true` to `false`.
89-
//
90-
// In this particular case however, we don't need to worry about this because we are categorically
91-
// removing all property accessors, and they could only have overridden other property accessors.
92-
}
93-
}
94-
9574
public void Clear()
9675
{
9776
lock (this.setups)

0 commit comments

Comments
 (0)