Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added overload to enforce old behavior of exact parameter matching #347

Merged
merged 2 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Source/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public static TAttribute GetCustomAttribute<TAttribute>(this ICustomAttributePro
}
#endif

public static bool HasCompatibleParameterTypes(this MethodInfo method, Type[] paramTypes)
public static bool HasCompatibleParameterTypes(this MethodInfo method, Type[] paramTypes, bool exactParameterMatch)
{
var types = method.GetParameterTypes().ToArray();
if (types.Length != paramTypes.Length)
Expand All @@ -284,6 +284,10 @@ public static bool HasCompatibleParameterTypes(this MethodInfo method, Type[] pa
{
continue;
}
else if (exactParameterMatch && types[i] != parameterType)
{
return false;
}
else if (!types[i].IsAssignableFrom(parameterType))
{
return false;
Expand Down
11 changes: 11 additions & 0 deletions Source/Protected/IProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public interface IProtectedMock<TMock> : IFluentInterface
/// <typeparam name="TResult">The return type of the method or property.</typeparam>
ISetup<TMock, TResult> Setup<TResult>(string methodOrPropertyName, params object[] args);

/// <summary>
/// Specifies a setup for an invocation on a property or a non void method with the given
/// <paramref name="methodOrPropertyName"/>, optionally specifying arguments for the method call.
/// </summary>
/// <param name="methodOrPropertyName">The name of the method or property to be invoked.</param>
/// <param name="args">The optional arguments for the invocation. If argument matchers are used,
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
/// <param name="exactParameterMatch">Should the parameter types match exactly types that were provided</param>
/// <typeparam name="TResult">The return type of the method or property.</typeparam>
ISetup<TMock, TResult> Setup<TResult>(string methodOrPropertyName, bool exactParameterMatch, params object[] args);

/// <summary>
/// Specifies a setup for an invocation on a property getter with the given
/// <paramref name="propertyName"/>.
Expand Down
16 changes: 14 additions & 2 deletions Source/Protected/ProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public ISetup<T> Setup(string methodName, params object[] args)
{
Guard.NotNullOrEmpty(() => methodName, methodName);

return Setup<TResult>(methodName, false, args);
}

public ISetup<T, TResult> Setup<TResult>(string methodName, bool exactParameterMatch, params object[] args)
{
Guard.NotNullOrEmpty(() => methodName, methodName);

var property = GetProperty(methodName);
if (property != null)
{
Expand All @@ -84,7 +91,7 @@ public ISetup<T> Setup(string methodName, params object[] args)
return Mock.SetupGet(mock, GetMemberAccess<TResult>(property), null);
}

var method = GetMethod(methodName, args);
var method = GetMethod(methodName, exactParameterMatch, args);
ThrowIfMemberMissing(methodName, method);
ThrowIfVoidMethod(method);
ThrowIfPublicMethod(method, typeof(T).Name);
Expand Down Expand Up @@ -189,10 +196,15 @@ public void VerifySet<TProperty>(string propertyName, Times times, object value)
}

private static MethodInfo GetMethod(string methodName, params object[] args)
{
return GetMethod(methodName, false, args);
}

private static MethodInfo GetMethod(string methodName, bool exactParameterMatch, params object[] args)
{
var argTypes = ToArgTypes(args);
return typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.SingleOrDefault(m => m.Name == methodName && m.HasCompatibleParameterTypes(argTypes));
.SingleOrDefault(m => m.Name == methodName && m.HasCompatibleParameterTypes(argTypes, exactParameterMatch));
}

private static Expression<Func<T, TResult>> GetMethodCall<TResult>(MethodInfo method, object[] args)
Expand Down
34 changes: 34 additions & 0 deletions UnitTests/ProtectedMockFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ public void SetupResultAllowsProtectedMethodInBaseClass()
Assert.Equal(5, mock.Object.DoProtectedInt());
}

[Fact]
public void SetupResultDefaulTwoOverloadsWithDerivedClassThrowsInvalidOperationException()
{
var mock = new Mock<MethodOverloads>();
Assert.Throws<InvalidOperationException>(() => mock.Protected()
.Setup<FooBase>("OverloadWithDerived", ItExpr.IsAny<MyDerived>())
.Returns(new FooBase()));

}

[Fact]
public void SetupResultExactParameterMatchTwoOverloadsWithDerivedClassShouldNotThrow()
{
var mock = new Mock<MethodOverloads>();
var fooBase = new FooBase();
mock.Protected()
.Setup<FooBase>("OverloadWithDerived", true, ItExpr.IsAny<MyDerived>())
.Returns(fooBase);
}

[Fact]
public void ThrowsIfVerifyNullVoidMethodName()
{
Expand Down Expand Up @@ -751,6 +771,16 @@ protected virtual string DoReturn(string a, string b)
protected virtual void SameFirstParameter(object a) { }

protected virtual void SameFirstParameter(object a, object b) { }

protected virtual FooBase OverloadWithDerived(MyBase myBase)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kinda confusing that the method is called WithDerived yet it receives a base type. How about naming it simply Overloaded or the like?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your totally right. Updated the method name accordingly. But a better name than "overloaded" also did not come into my mind. Finding good names is harder than coding ;)

{
return null;
}

protected virtual FooBase OverloadWithDerived(MyDerived myBase)
{
return null;
}
}

public class FooBase
Expand Down Expand Up @@ -869,5 +899,9 @@ protected virtual string TwoArgs(string arg, int arg1)
public class FooDerived : FooBase
{
}

public class MyBase { }

public class MyDerived : MyBase { }
}
}