Skip to content

Commit

Permalink
Replace OriginalSetup w/ OriginalExpression
Browse files Browse the repository at this point in the history
Takes away some unneeded complexity, and may be simpler to understand.
  • Loading branch information
stakx committed Apr 17, 2020
1 parent bf08221 commit e85ec02
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 285 deletions.
2 changes: 1 addition & 1 deletion src/Moq/AutoImplementedPropertyGetterSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class AutoImplementedPropertyGetterSetup : Setup
private Func<object> getter;

public AutoImplementedPropertyGetterSetup(Mock mock, LambdaExpression originalExpression, MethodInfo method, Func<object> getter)
: base(originalSetup: null, mock, new InvocationShape(originalExpression, method, noArguments))
: base(originalExpression: null, mock, new InvocationShape(originalExpression, method, noArguments))
{
this.getter = getter;

Expand Down
2 changes: 1 addition & 1 deletion src/Moq/AutoImplementedPropertySetterSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class AutoImplementedPropertySetterSetup : Setup
private Action<object> setter;

public AutoImplementedPropertySetterSetup(Mock mock, LambdaExpression originalExpression, MethodInfo method, Action<object> setter)
: base(originalSetup: null, mock, new InvocationShape(originalExpression, method, new Expression[] { It.IsAny(method.GetParameterTypes().Last()) }))
: base(originalExpression: null, mock, new InvocationShape(originalExpression, method, new Expression[] { It.IsAny(method.GetParameterTypes().Last()) }))
{
this.setter = setter;

Expand Down
113 changes: 0 additions & 113 deletions src/Moq/FluentSetup.cs

This file was deleted.

41 changes: 0 additions & 41 deletions src/Moq/IFluentSetup.cs

This file was deleted.

15 changes: 9 additions & 6 deletions src/Moq/ISetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public interface ISetup
Mock Mock { get; }

/// <summary>
/// Returns the original setup as it would appear in user code.
/// Returns the original setup expression from which this setup resulted.
/// <para>
/// For setups doing a simple member access or method invocation (such as <c>`mock => mock.Member`</c>),
/// this property will simply return the same <see cref="ISetup"/> instance.
/// this property will be equal to <see cref="Expression"/>.
/// </para>
/// <para>
/// For setups whose expression involves member chaining (such as <c>`parent => parent.Child.Member`</c>),
Expand All @@ -79,12 +79,15 @@ public interface ISetup
/// <item>on its inner mock, a setup for <c>`(child) => (child).Member`</c>.</item>
/// </list>
/// These are the setups that will be put in the mocks' <see cref="Mock.Setups"/> collections;
/// for both of those, <see cref="OriginalSetup"/> will return the same <see cref="IFluentSetup"/>
/// whose <see cref="ISetup.Expression"/> represents the original, fluent setup expression.
/// their <see cref="Expression"/> will return the partial expression for just a single member access,
/// while their <see cref="OriginalExpression"/> will return the original, full expression.
/// </para>
/// <para>
/// This property may also return <see langword="null"/> if this setup was created automatically,
/// e.g. by <see cref="Mock{T}.SetupAllProperties"/> or by <see cref="DefaultValue.Mock"/>.
/// </para>
/// </summary>
/// <seealso cref="IFluentSetup"/>
ISetup OriginalSetup { get; }
Expression OriginalExpression { get; }

/// <summary>
/// Gets whether this setup was matched by at least one invocation on the mock.
Expand Down
5 changes: 3 additions & 2 deletions src/Moq/InnerMockSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System.Diagnostics;
using System.Linq.Expressions;

namespace Moq
{
internal sealed class InnerMockSetup : SetupWithOutParameterSupport
{
private readonly object returnValue;

public InnerMockSetup(IFluentSetup originalSetup, Mock mock, InvocationShape expectation, object returnValue)
: base(originalSetup, mock, expectation)
public InnerMockSetup(Expression originalExpression, Mock mock, InvocationShape expectation, object returnValue)
: base(originalExpression, mock, expectation)
{
Debug.Assert(Unwrap.ResultIfCompletedTask(returnValue) is IMocked);

Expand Down
4 changes: 2 additions & 2 deletions src/Moq/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ internal sealed partial class MethodCall : SetupWithOutParameterSupport

private string declarationSite;

public MethodCall(ISetup originalSetup, Mock mock, Condition condition, InvocationShape expectation)
: base(originalSetup, mock, expectation)
public MethodCall(Expression originalExpression, Mock mock, Condition condition, InvocationShape expectation)
: base(originalExpression, mock, expectation)
{
this.condition = condition;
this.flags = expectation.Method.ReturnType != typeof(void) ? Flags.MethodIsNonVoid : 0;
Expand Down
1 change: 1 addition & 0 deletions src/Moq/Mock.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;

Expand Down
31 changes: 12 additions & 19 deletions src/Moq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ internal static MethodCall Setup(Mock mock, LambdaExpression expression, Conditi
{
Guard.NotNull(expression, nameof(expression));

return Mock.SetupRecursive(mock, expression, setupLast: (part, targetMock, originalSetup) =>
return Mock.SetupRecursive(mock, expression, setupLast: (targetMock, originalExpression, part) =>
{
var setup = new MethodCall(originalSetup, targetMock, condition, expectation: part);
var setup = new MethodCall(originalExpression, targetMock, condition, expectation: part);
targetMock.MutableSetups.Add(setup);
return setup;
});
Expand Down Expand Up @@ -567,40 +567,34 @@ internal static SequenceSetup SetupSequence(Mock mock, LambdaExpression expressi
{
Guard.NotNull(expression, nameof(expression));

return Mock.SetupRecursive(mock, expression, setupLast: (part, targetMock, originalSetup) =>
return Mock.SetupRecursive(mock, expression, setupLast: (targetMock, originalExpression, part) =>
{
var setup = new SequenceSetup(originalSetup, targetMock, expectation: part);
var setup = new SequenceSetup(originalExpression, targetMock, expectation: part);
targetMock.MutableSetups.Add(setup);
return setup;
});
}

private static TSetup SetupRecursive<TSetup>(Mock mock, LambdaExpression expression, Func<InvocationShape, Mock, ISetup, TSetup> setupLast)
private static TSetup SetupRecursive<TSetup>(Mock mock, LambdaExpression expression, Func<Mock, Expression, InvocationShape, TSetup> setupLast)
where TSetup : ISetup
{
Debug.Assert(mock != null);
Debug.Assert(expression != null);
Debug.Assert(setupLast != null);

var parts = expression.Split();
var fluentSetup = parts.Count > 1 ? new FluentSetup(expression) : null;
return Mock.SetupRecursive(fluentSetup, mock, expression, parts, setupLast);
return Mock.SetupRecursive(mock, originalExpression: expression, parts, setupLast);
}

private static TSetup SetupRecursive<TSetup>(FluentSetup fluentSetup, Mock mock, LambdaExpression expression, Stack<InvocationShape> parts, Func<InvocationShape, Mock, ISetup, TSetup> setupLast)
private static TSetup SetupRecursive<TSetup>(Mock mock, LambdaExpression originalExpression, Stack<InvocationShape> parts, Func<Mock, Expression, InvocationShape, TSetup> setupLast)
where TSetup : ISetup
{
var part = parts.Pop();
var (expr, method, arguments) = part;

if (parts.Count == 0)
{
var setup = setupLast(part, mock, fluentSetup);
fluentSetup?.AddPart(setup);
#if DEBUG
fluentSetup?.MarkAsComplete();
#endif
return setup;
return setupLast(mock, originalExpression, part);
}
else
{
Expand All @@ -618,15 +612,14 @@ private static TSetup SetupRecursive<TSetup>(FluentSetup fluentSetup, Mock mock,
string.Format(
CultureInfo.CurrentCulture,
Resources.UnsupportedExpression,
expr.ToStringFixed() + " in " + expression.ToStringFixed() + ":\n" + Resources.TypeNotMockable));
expr.ToStringFixed() + " in " + originalExpression.ToStringFixed() + ":\n" + Resources.TypeNotMockable));
}
setup = new InnerMockSetup(fluentSetup, mock, expectation: part, returnValue);
fluentSetup.AddPart(setup);
setup = new InnerMockSetup(originalExpression, mock, expectation: part, returnValue);
mock.MutableSetups.Add((Setup)setup);
}
Debug.Assert(innerMock != null);

return Mock.SetupRecursive(fluentSetup, innerMock, expression, parts, setupLast);
return Mock.SetupRecursive(innerMock, originalExpression, parts, setupLast);
}
}

Expand Down Expand Up @@ -818,7 +811,7 @@ internal void AddInnerMockSetup(Invocation invocation, object returnValue)
Debug.Assert(property.CanRead(out var getter) && invocation.Method == getter);
}

this.MutableSetups.Add(new InnerMockSetup(originalSetup: null, this, new InvocationShape(expression, invocation.Method, arguments, exactGenericTypeArguments: true), returnValue));
this.MutableSetups.Add(new InnerMockSetup(originalExpression: null, this, new InvocationShape(expression, invocation.Method, arguments, exactGenericTypeArguments: true), returnValue));
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/Moq/SequenceSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal sealed class SequenceSetup : SetupWithOutParameterSupport
// contains the responses set up with the `CallBase`, `Pass`, `Returns`, and `Throws` verbs
private ConcurrentQueue<Response> responses;

public SequenceSetup(ISetup originalSetup, Mock mock, InvocationShape expectation)
: base(originalSetup, mock, expectation)
public SequenceSetup(Expression originalExpression, Mock mock, InvocationShape expectation)
: base(originalExpression, mock, expectation)
{
this.responses = new ConcurrentQueue<Response>();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Moq/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ namespace Moq
internal abstract class Setup : ISetup
{
private readonly InvocationShape expectation;
private readonly ISetup originalSetup;
private readonly Expression originalExpression;
private readonly Mock mock;
private Flags flags;

protected Setup(ISetup originalSetup, Mock mock, InvocationShape expectation)
protected Setup(Expression originalExpression, Mock mock, InvocationShape expectation)
{
Debug.Assert(mock != null);
Debug.Assert(expectation != null);

this.originalSetup = originalSetup ?? this;
this.originalExpression = originalExpression;
this.expectation = expectation;
this.mock = mock;
}
Expand All @@ -47,7 +47,7 @@ protected Setup(ISetup originalSetup, Mock mock, InvocationShape expectation)

public Mock Mock => this.mock;

public ISetup OriginalSetup => this.originalSetup;
public Expression OriginalExpression => this.originalExpression;

public bool WasMatched => (this.flags & Flags.Matched) != 0;

Expand Down
4 changes: 2 additions & 2 deletions src/Moq/SetupWithOutParameterSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal abstract class SetupWithOutParameterSupport : Setup
{
private readonly List<KeyValuePair<int, object>> outValues;

protected SetupWithOutParameterSupport(ISetup originalSetup, Mock mock, InvocationShape expectation)
: base(originalSetup, mock, expectation)
protected SetupWithOutParameterSupport(Expression originalExpression, Mock mock, InvocationShape expectation)
: base(originalExpression, mock, expectation)
{
Debug.Assert(expectation != null);

Expand Down
Loading

0 comments on commit e85ec02

Please sign in to comment.