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

Make event accessor recognition logic more correct #376

Merged
merged 1 commit into from
Jun 16, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 37 additions & 25 deletions Source/InterceptorStrategies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class InvokeBase : IInterceptStrategy
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
{
if (invocation.Method.DeclaringType == typeof(object) || // interface proxy
ctx.Mock.ImplementedInterfaces.Contains(invocation.Method.DeclaringType) && !invocation.Method.IsEventAttach() && !invocation.Method.IsEventDetach() && ctx.Mock.CallBase && !ctx.Mock.MockedType.GetTypeInfo().IsInterface || // class proxy with explicitly implemented interfaces. The method's declaring type is the interface and the method couldn't be abstract
ctx.Mock.ImplementedInterfaces.Contains(invocation.Method.DeclaringType) && !invocation.Method.LooksLikeEventAttach() && !invocation.Method.LooksLikeEventDetach() && ctx.Mock.CallBase && !ctx.Mock.MockedType.GetTypeInfo().IsInterface || // class proxy with explicitly implemented interfaces. The method's declaring type is the interface and the method couldn't be abstract
invocation.Method.DeclaringType.GetTypeInfo().IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase // class proxy
)
{
Expand Down Expand Up @@ -263,39 +263,51 @@ public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorCo
if (!FluentMockContext.IsActive)
{
//Special case for events
if (invocation.Method.IsEventAttach())
if (invocation.Method.LooksLikeEventAttach())
{
var delegateInstance = (Delegate)invocation.Arguments[0];
// TODO: validate we can get the event?
var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(4));

if (ctx.Mock.CallBase && !eventInfo.DeclaringType.GetTypeInfo().IsInterface)
{
invocation.InvokeBase();
}
else if (delegateInstance != null)
var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring("add_".Length));
if (eventInfo != null)
{
ctx.AddEventHandler(eventInfo, (Delegate)invocation.Arguments[0]);
// TODO: We could compare `invocation.Method` and `eventInfo.GetAddMethod()` here.
// If they are equal, then `invocation.Method` is definitely an event `add` accessor.
// Not sure whether this would work with F# and COM; see commit 44070a9.
if (ctx.Mock.CallBase && !eventInfo.DeclaringType.GetTypeInfo().IsInterface)
{
invocation.InvokeBase();
return InterceptionAction.Stop;
}
else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
{
ctx.AddEventHandler(eventInfo, delegateInstance);
return InterceptionAction.Stop;
}
}

return InterceptionAction.Stop;
// wasn't an event attach accessor after all
return InterceptionAction.Continue;
}
else if (invocation.Method.IsEventDetach())
else if (invocation.Method.LooksLikeEventDetach())
{
var delegateInstance = (Delegate)invocation.Arguments[0];
// TODO: validate we can get the event?
var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(7));

if (ctx.Mock.CallBase && !eventInfo.DeclaringType.GetTypeInfo().IsInterface)
{
invocation.InvokeBase();
}
else if (delegateInstance != null)
var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring("remove_".Length));
if (eventInfo != null)
{
ctx.RemoveEventHandler(eventInfo, (Delegate)invocation.Arguments[0]);
// TODO: We could compare `invocation.Method` and `eventInfo.GetRemoveMethod()` here.
// If they are equal, then `invocation.Method` is definitely an event `remove` accessor.
// Not sure whether this would work with F# and COM; see commit 44070a9.
if (ctx.Mock.CallBase && !eventInfo.DeclaringType.GetTypeInfo().IsInterface)
{
invocation.InvokeBase();
return InterceptionAction.Stop;
}
else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
{
ctx.RemoveEventHandler(eventInfo, delegateInstance);
return InterceptionAction.Stop;
}
}

return InterceptionAction.Stop;
// wasn't an event detach accessor after all
return InterceptionAction.Continue;
}

// Save to support Verify[expression] pattern.
Expand Down
4 changes: 2 additions & 2 deletions Source/MemberInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public static bool IsDestructor(this MethodInfo method)
}


public static bool IsEventAttach(this MethodBase method)
public static bool LooksLikeEventAttach(this MethodBase method)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's be honest and call it what it really does.

{
return method.Name.StartsWith("add_", StringComparison.Ordinal);
}

public static bool IsEventDetach(this MethodBase method)
public static bool LooksLikeEventDetach(this MethodBase method)
{
return method.Name.StartsWith("remove_", StringComparison.Ordinal);
}
Expand Down
38 changes: 38 additions & 0 deletions UnitTests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,44 @@ public void DoTest()
#endif
#endregion

#region 82

public class Issue82
{
public interface ILogger
{
event Action info;
void add_info(string info);
void Add_info(string info);
void remove_info(string info);
void Remove_info(string info);
}

[Fact]
public void MethodWithAddUnderscoreNamePrefixDoesNotGetMisrecognizedAsEventAccessor()
{
var mock = new Mock<ILogger>();
mock.Setup(x => x.add_info(It.IsAny<string>()));
mock.Setup(x => x.Add_info(It.IsAny<string>()));

mock.Object.add_info(string.Empty);
mock.Object.Add_info(string.Empty);
}

[Fact]
public void MethodWithRemoveUnderscoreNamePrefixDoesNotGetMisrecognizedAsEventAccessor()
{
var mock = new Mock<ILogger>();
mock.Setup(x => x.remove_info(It.IsAny<string>()));
mock.Setup(x => x.Remove_info(It.IsAny<string>()));

mock.Object.remove_info(string.Empty);
mock.Object.Remove_info(string.Empty);
}
}

#endregion

#region 163

#if !NETCORE
Expand Down