-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
158 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Abc.MoqComplete/MoqComplete/Services/IMockedMethodProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using JetBrains.Annotations; | ||
using JetBrains.ReSharper.Psi; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
|
||
namespace MoqComplete.Services | ||
{ | ||
public interface IMockedMethodProvider | ||
{ | ||
IMethod GetMockedMethodFromSetupMethod([CanBeNull] IInvocationExpression invocationExpression); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Abc.MoqComplete/MoqComplete/Services/IMoqMethodIdentifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using JetBrains.Annotations; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
|
||
namespace MoqComplete.Services | ||
{ | ||
public interface IMoqMethodIdentifier | ||
{ | ||
bool IsMoqSetupMethod([CanBeNull] IInvocationExpression invocationExpression); | ||
bool IsMoqVerifyMethod([CanBeNull] IInvocationExpression invocationExpression); | ||
bool IsMoqReturnMethod([CanBeNull] IInvocationExpression invocationExpression); | ||
bool IsMoqCallbackMethod([CanBeNull] IInvocationExpression invocationExpression); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Abc.MoqComplete/MoqComplete/Services/MockedMethodProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using JetBrains.ProjectModel; | ||
using JetBrains.ReSharper.Psi; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
using JetBrains.ReSharper.Psi.Resolve; | ||
|
||
namespace MoqComplete.Services | ||
{ | ||
[SolutionComponent] | ||
public class MockedMethodProvider : IMockedMethodProvider | ||
{ | ||
private readonly IMoqMethodIdentifier _methodIdentifier; | ||
|
||
public MockedMethodProvider(IMoqMethodIdentifier methodIdentifier) | ||
{ | ||
_methodIdentifier = methodIdentifier; | ||
} | ||
|
||
public IMethod GetMockedMethodFromSetupMethod(IInvocationExpression invocationExpression) | ||
{ | ||
if (invocationExpression == null || !_methodIdentifier.IsMoqSetupMethod(invocationExpression)) | ||
return null; | ||
|
||
var methodArguments = invocationExpression.ArgumentList.Arguments; | ||
if (methodArguments.Count != 1) | ||
return null; | ||
|
||
var lambdaExpression = methodArguments[0].Value as ILambdaExpression; | ||
if (lambdaExpression == null) | ||
return null; | ||
|
||
var mockMethodInvocationExpression = lambdaExpression.BodyExpression as IInvocationExpression; | ||
if (mockMethodInvocationExpression == null || mockMethodInvocationExpression.Reference == null) | ||
return null; | ||
|
||
var targetMethodResolveResult = mockMethodInvocationExpression.Reference.Resolve(); | ||
if (targetMethodResolveResult.ResolveErrorType == ResolveErrorType.OK) | ||
{ | ||
return targetMethodResolveResult.DeclaredElement as IMethod; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
Abc.MoqComplete/MoqComplete/Services/MoqMethodIdentifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using JetBrains.Annotations; | ||
using JetBrains.ProjectModel; | ||
using JetBrains.ReSharper.Psi; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
using JetBrains.ReSharper.Psi.Resolve; | ||
using JetBrains.ReSharper.Psi.Resx.Utils; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace MoqComplete.Services | ||
{ | ||
[SolutionComponent] | ||
public class MoqMethodIdentifier : IMoqMethodIdentifier | ||
{ | ||
public bool IsMoqSetupMethod(IInvocationExpression invocationExpression) | ||
=> IsMethod(invocationExpression, | ||
declaredElement => IsMethodString(declaredElement, | ||
"Method:Moq.Mock`1.Setup(System.Linq.Expressions.Expression`1[TDelegate -> System.Action`1[T -> T]] expression)", | ||
"Method:Moq.Mock`1.Setup(System.Linq.Expressions.Expression`1[TDelegate -> System.Func`2[T -> T, TResult -> TResult]] expression)")); | ||
|
||
public bool IsMoqVerifyMethod(IInvocationExpression invocationExpression) | ||
=> IsMethod(invocationExpression, | ||
declaredElement => IsMethodString(declaredElement, | ||
"Method:Moq.Mock`1.Verify(System.Linq.Expressions.Expression`1[TDelegate -> System.Action`1[T -> T]] expression)", | ||
"Method:Moq.Mock`1.Verify(System.Linq.Expressions.Expression`1[TDelegate -> System.Func`2[T -> T, TResult -> TResult]] expression)")); | ||
|
||
public bool IsMoqReturnMethod(IInvocationExpression invocationExpression) | ||
=> IsMethod(invocationExpression, | ||
declaredElement => IsMethodStartingWithString(declaredElement, | ||
"Method:Moq.Language.IReturns`2.Returns", | ||
"Method:Moq.Language.IReturns`1.Returns")); | ||
|
||
public bool IsMoqCallbackMethod(IInvocationExpression expression) | ||
=> IsMethod(expression, | ||
declaredElement => IsMethodStartingWithString(declaredElement, | ||
"Method:Moq.Language.ICallback.Callback(System.Action", | ||
"Method:Moq.Language.ICallback`2.Callback(System.Action")); | ||
|
||
private bool IsMethod([CanBeNull] IInvocationExpression invocationExpression, Func<IDeclaredElement, bool> methodFilter) | ||
{ | ||
if (invocationExpression == null || invocationExpression.Reference == null) | ||
return false; | ||
|
||
var resolveResult = invocationExpression.Reference.Resolve(); | ||
if (resolveResult.ResolveErrorType == ResolveErrorType.MULTIPLE_CANDIDATES) | ||
return resolveResult.Result.Candidates.Any(methodFilter); | ||
|
||
return methodFilter(resolveResult.DeclaredElement); | ||
} | ||
|
||
private bool IsMethodString([CanBeNull] IDeclaredElement declaredElement, params string[] methodName) | ||
{ | ||
var declaredElementAsString = declaredElement.ConvertToString(); | ||
return methodName.Contains(declaredElementAsString); | ||
} | ||
|
||
private bool IsMethodStartingWithString([CanBeNull] IDeclaredElement declaredElement, params string[] methodName) | ||
{ | ||
var methodString = declaredElement.ConvertToString(); | ||
return methodString != null && methodName.Any(m => methodString.StartsWith(m)); | ||
} | ||
} | ||
} |