Skip to content

Commit

Permalink
Remove current SetupAllProperties impl
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Feb 13, 2022
1 parent a0243cd commit 5db2a42
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 132 deletions.
6 changes: 0 additions & 6 deletions src/Moq/AsInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ public override DefaultValueProvider DefaultValueProvider
set => this.owner.DefaultValueProvider = value;
}

internal override DefaultValueProvider AutoSetupPropertiesDefaultValueProvider
{
get => this.owner.AutoSetupPropertiesDefaultValueProvider;
set => this.owner.AutoSetupPropertiesDefaultValueProvider = value;
}

internal override EventHandlerCollection EventHandlers => this.owner.EventHandlers;

internal override Type[] InheritedInterfaces => this.owner.InheritedInterfaces;
Expand Down
55 changes: 0 additions & 55 deletions src/Moq/Interception/InterceptionAspects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

using Moq.Behaviors;
Expand Down Expand Up @@ -175,59 +173,6 @@ public static void Handle(Invocation invocation, Mock mock)
}
}

internal static class HandleAutoSetupProperties
{
private static readonly int AccessorPrefixLength = "?et_".Length; // get_ or set_

public static bool Handle(Invocation invocation, Mock mock)
{
if (mock.AutoSetupPropertiesDefaultValueProvider == null)
{
return false;
}

MethodInfo invocationMethod = invocation.Method;
if (!invocationMethod.IsPropertyAccessor())
{
return false;
}

string propertyName = invocationMethod.Name.Substring(AccessorPrefixLength);
PropertyInfo property = invocationMethod.DeclaringType.GetProperty(propertyName, Type.EmptyTypes);
Debug.Assert(property != null);

bool accessorFound = property.CanRead(out var getter) | property.CanWrite(out var setter);
Debug.Assert(accessorFound);

var expression = GetPropertyExpression(invocationMethod.DeclaringType, property);
var initialValue = getter != null ? CreateInitialPropertyValue(mock, getter) : null;
var setup = new StubbedPropertySetup(mock, expression, getter, setter, initialValue);
mock.MutableSetups.Add(setup);
setup.Execute(invocation);

return true;
}

private static object CreateInitialPropertyValue(Mock mock, MethodInfo getter)
{
object initialValue = mock.GetDefaultValue(getter, out Mock innerMock,
useAlternateProvider: mock.AutoSetupPropertiesDefaultValueProvider);

if (innerMock != null)
{
Mock.SetupAllProperties(innerMock, mock.AutoSetupPropertiesDefaultValueProvider);
}

return initialValue;
}

private static LambdaExpression GetPropertyExpression(Type mockType, PropertyInfo property)
{
var param = Expression.Parameter(mockType, "m");
return Expression.Lambda(Expression.MakeMemberAccess(param, property), param);
}
}

internal static class FailForStrictMock
{
public static void Handle(Invocation invocation, Mock mock)
Expand Down
5 changes: 0 additions & 5 deletions src/Moq/Interception/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ void IInterceptor.Intercept(Invocation invocation)
return;
}

if (HandleAutoSetupProperties.Handle(invocation, this))
{
return;
}

if (HandleEventSubscription.Handle(invocation, this))
{
return;
Expand Down
44 changes: 2 additions & 42 deletions src/Moq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,6 @@ public DefaultValue DefaultValue
/// </summary>
public abstract DefaultValueProvider DefaultValueProvider { get; set; }

/// <summary>
/// The <see cref="Moq.DefaultValueProvider"/> used to initialize automatically stubbed properties.
/// It is equal to the value of <see cref="DefaultValueProvider"/> at the time when
/// <see cref="SetupAllProperties"/> was last called.
/// </summary>
internal abstract DefaultValueProvider AutoSetupPropertiesDefaultValueProvider { get; set; }

internal abstract SetupCollection MutableSetups { get; }

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

Mock.SetupRecursive<MethodCall>(mock, expression, setupLast: (targetMock, _, __) =>
{
// Setting a mock's property through reflection will only work (i.e. the property will only remember the value
// it's being set to) if it is being stubbed. In order to ensure it's stubbed, we temporarily enable
// auto-stubbing (if that isn't already switched on).
var temporaryAutoSetupProperties = targetMock.AutoSetupPropertiesDefaultValueProvider == null;
if (temporaryAutoSetupProperties)
{
targetMock.AutoSetupPropertiesDefaultValueProvider = targetMock.DefaultValueProvider;
}
try
{
propertyToSet.SetValue(targetMock.Object, value, null);
}
finally
{
if (temporaryAutoSetupProperties)
{
targetMock.AutoSetupPropertiesDefaultValueProvider = null;
}
}
propertyToSet.SetValue(targetMock.Object, value, null);
return null;
}, allowNonOverridableLastProperty: true);
}
Expand Down Expand Up @@ -637,21 +611,7 @@ private static TSetup SetupRecursive<TSetup>(Mock mock, LambdaExpression origina

internal static void SetupAllProperties(Mock mock)
{
SetupAllProperties(mock, mock.DefaultValueProvider);
}

internal static void SetupAllProperties(Mock mock, DefaultValueProvider defaultValueProvider)
{
mock.MutableSetups.RemoveAllPropertyAccessorSetups();
// Removing all the previous properties setups to keep the behaviour of overriding
// existing setups in `SetupAllProperties`.

mock.AutoSetupPropertiesDefaultValueProvider = defaultValueProvider;
// `SetupAllProperties` no longer performs properties setup like in previous versions.
// Instead it just enables a switch to setup properties on-demand at the moment of first access.
// In order for `SetupAllProperties`'s new mode of operation to be indistinguishable
// from how it worked previously, it's important to capture the default value provider at this precise
// moment, since it might be changed later (before queries to properties).
// TODO: implement!
}

#endregion
Expand Down
1 change: 0 additions & 1 deletion src/Moq/MockDefaultValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected override object GetFallbackDefaultValue(Type type, Mock mock)
var mockType = typeof(Mock<>).MakeGenericType(type);
Mock newMock = (Mock)Activator.CreateInstance(mockType, mock.Behavior);
newMock.DefaultValueProvider = mock.DefaultValueProvider;
newMock.AutoSetupPropertiesDefaultValueProvider = mock.AutoSetupPropertiesDefaultValueProvider;
if(!type.IsDelegateType())
{
newMock.CallBase = mock.CallBase;
Expand Down
2 changes: 0 additions & 2 deletions src/Moq/Mock`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ public override DefaultValueProvider DefaultValueProvider
set => this.defaultValueProvider = value ?? throw new ArgumentNullException(nameof(value));
}

internal override DefaultValueProvider AutoSetupPropertiesDefaultValueProvider { get; set; }

internal override EventHandlerCollection EventHandlers => this.eventHandlers;

internal override List<Type> AdditionalInterfaces => this.additionalInterfaces;
Expand Down
21 changes: 0 additions & 21 deletions src/Moq/SetupCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Moq
{
Expand Down Expand Up @@ -72,26 +71,6 @@ private void MarkOverriddenSetups()
}
}

public void RemoveAllPropertyAccessorSetups()
{
// Fast path (no `lock`) when there are no setups:
if (this.setups.Count == 0)
{
return;
}

lock (this.setups)
{
this.setups.RemoveAll(s => s is StubbedPropertySetup || (s is MethodSetup ms && ms.Method.IsPropertyAccessor()));

// NOTE: In the general case, removing a setup means that some overridden setups might no longer
// be shadowed, and their `IsOverridden` flag should go back from `true` to `false`.
//
// In this particular case however, we don't need to worry about this because we are categorically
// removing all property accessors, and they could only have overridden other property accessors.
}
}

public void Clear()
{
lock (this.setups)
Expand Down

0 comments on commit 5db2a42

Please sign in to comment.