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

System.Object methods should always work. #279

Merged
merged 1 commit into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Source/Interceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private IEnumerable<IInterceptStrategy> InterceptionStrategies()
yield return new HandleDestructor();
yield return new HandleTracking();
yield return new InterceptMockPropertyMixin();
yield return new InterceptToStringMixin();
yield return new InterceptObjectMethodsMixin();
yield return new AddActualInvocation();
yield return new ExtractProxyCall();
yield return new ExecuteCall();
Expand Down
27 changes: 22 additions & 5 deletions Source/InterceptorStrategies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,42 @@ public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorCo
}
}

internal class InterceptToStringMixin : IInterceptStrategy
/// <summary>
/// Intercept strategy that handles `System.Object` methods.
/// </summary>
internal class InterceptObjectMethodsMixin : IInterceptStrategy
{
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
{
var method = invocation.Method;

// Only if there is no corresponding setup
if (IsObjectToStringMethod(method) && !ctx.OrderedCalls.Select(c => IsObjectToStringMethod(c.Method)).Any())
// Only if there is no corresponding setup for `ToString()`
if (IsObjectMethod(method, "ToString") && !ctx.OrderedCalls.Select(c => IsObjectMethod(c.Method, "ToString")).Any())
Copy link
Member

Choose a reason for hiding this comment

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

We should probably switch to using nameof(object.ToString) now that C# 6 is here :)

{
invocation.ReturnValue = ctx.Mock.ToString() + ".Object";
return InterceptionAction.Stop;
}

// Only if there is no corresponding setup for `GetHashCode()`
if (IsObjectMethod(method, "GetHashCode") && !ctx.OrderedCalls.Select(c => IsObjectMethod(c.Method, "GetHashCode")).Any())
{
invocation.ReturnValue = ctx.Mock.GetHashCode();
return InterceptionAction.Stop;
}

// Only if there is no corresponding setup for `Equals()`
if (IsObjectMethod(method, "Equals") && !ctx.OrderedCalls.Select(c => IsObjectMethod(c.Method, "Equals")).Any())
{
invocation.ReturnValue = ReferenceEquals(invocation.Arguments.First(), ctx.Mock.Object);
return InterceptionAction.Stop;
}

return InterceptionAction.Continue;
}

protected bool IsObjectToStringMethod(MethodInfo method)
protected bool IsObjectMethod(MethodInfo method, string name)
{
if (method.DeclaringType == typeof(object) && method.Name == "ToString")
if (method.DeclaringType == typeof(object) && method.Name == name)
{
return true;
}
Expand Down
Loading