Skip to content

Commit

Permalink
Provide Callback Completion after Returns
Browse files Browse the repository at this point in the history
  • Loading branch information
lconstan committed Feb 5, 2019
1 parent 90da795 commit 0ada658
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Abc.MoqComplete/Deployment/MoqComplete.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<id>Abc.MoqComplete</id>
<version>$version$</version>
<releaseNotes>
Provide completion for Moq
Provide Callback completion after Returns
</releaseNotes>
<title>MoqComplete</title>
<authors>ABC Arbitrage Asset Management</authors>
Expand All @@ -13,6 +13,7 @@
<iconUrl>https://raw.githubusercontent.com/Abc-Arbitrage/Abc.MoqComplete/master/Abc.MoqComplete/Deployment/icon.png</iconUrl>
<projectUrl>https://github.com/Abc-Arbitrage/Abc.MoqComplete</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<licenseUrl>https://opensource.org/licenses/MIT</licenseUrl>
<description>Code completion for Moq</description>
<language>en-US</language>
<summary>Provide completion for It.IsAny, Callback, suggest mock in constructor</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void SetUp()
}

[TestCase("callbackCompletion")]
[TestCase("callbackCompletion_afterReturn")]
public void should_fill_with_callback(string testSrc) => DoOneTest(testSrc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {})}
using Moq;
using NUnit.Framework;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
int BuildSomething(int i, string toto, bool ok);
}

[TestFixture]
public class Test1
{
[Test]
public void METHOD()
{
Mock<ITestInterface> temp = new Mock<ITestInterface>();
temp.Setup(x => x.BuildSomething(It.IsAny<int>(),It.IsAny<string>(), It.IsAny<bool>()))
.Returns(0)
.c{caret}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {})}
using Moq;
using NUnit.Framework;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
int BuildSomething(int i, string toto, bool ok);
}

[TestFixture]
public class Test1
{
[Test]
public void METHOD()
{
Mock<ITestInterface> temp = new Mock<ITestInterface>();
temp.Setup(x => x.BuildSomething(It.IsAny<int>(),It.IsAny<string>(), It.IsAny<bool>()))
.Returns(0)
.Callback<int,string,bool>((i,toto,ok) => {{caret}})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using JetBrains.ReSharper.Psi.ExpectedTypes;
using JetBrains.ReSharper.Psi.Resources;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.UI.RichText;
using MoqComplete.Extensions;
using System.Linq;

Expand All @@ -29,14 +28,18 @@ protected override bool IsAvailable(CSharpCodeCompletionContext context)
protected override bool AddLookupItems(CSharpCodeCompletionContext context, IItemsCollector collector)
{
var identifier = context.TerminatedContext.TreeNode as IIdentifier;
var setupExpression = identifier.GetParentSafe<IReferenceExpression>();
if (setupExpression == null)
var expression = identifier.GetParentSafe<IReferenceExpression>();
if (expression == null)
return false;

if (!(setupExpression.ConditionalQualifier is IInvocationExpression setupInvocation))
if (!(expression.ConditionalQualifier is IInvocationExpression invocation))
return false;

var mockedMethod = setupInvocation.GetMockedMethodFromSetupMethod();
if (invocation.IsMoqReturnMethod())
invocation = invocation.InvokedExpression?.FirstChild as IInvocationExpression;

var mockedMethod = invocation.GetMockedMethodFromSetupMethod();

if (mockedMethod == null || mockedMethod.Parameters.Count == 0)
return false;

Expand Down
8 changes: 8 additions & 0 deletions Abc.MoqComplete/MoqComplete/Extensions/MoqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ public static bool IsMoqSetupMethod([CanBeNull] IDeclaredElement declaredElement
public static bool IsMoqCallbackMethod([CanBeNull] this IInvocationExpression expression)
=> IsMethod(expression, IsMoqCallbackMethod);

public static bool IsMoqReturnMethod([CanBeNull] this IInvocationExpression invocationExpression) => IsMethod(invocationExpression, IsMoqReturnMethod);

public static bool IsMoqVerifyMethod([CanBeNull] IDeclaredElement 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 static bool IsMoqReturnMethod([CanBeNull] IDeclaredElement declaredElement)
{
var methodString = declaredElement.ConvertToString();
return methodString != null && (methodString.StartsWith("Method:Moq.Language.IReturns`2.Returns") || methodString.StartsWith("Method:Moq.Language.IReturns`1.Returns"));
}

private static bool IsMoqCallbackMethod(IDeclaredElement declaredElement)
{
var methodString = declaredElement.ConvertToString();
Expand Down

0 comments on commit 0ada658

Please sign in to comment.