Skip to content

Commit

Permalink
Allow callback completion after ReturnAsync method
Browse files Browse the repository at this point in the history
  • Loading branch information
lconstan committed Jun 11, 2022
1 parent 2a16700 commit 102cb18
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class CallbackMethodProviderTests: CodeCompletionTestBase

[TestCase("callbackCompletion")]
[TestCase("callbackCompletion_afterReturn")]
[TestCase("callbackCompletion_afterReturnAsync")]
[TestCase("callbackCompletion_afterReturnAsyncMultiple")]
[TestCase("callbackCompletion_afterReturnAsyncMultiple2")]
[TestCase("callbackCompletion_generic")]
public void should_fill_with_callback(string testSrc) => DoOneTest(testSrc);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {\})}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
Task<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>()))
.ReturnsAsync(0)
.c{caret}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {\})}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
Task<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>()))
.ReturnsAsync(0)
.Callback<int,string,bool>((i,toto,ok) => {{caret}})
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {\})}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
Task<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>()))
.ReturnsAsync<int, string, bool, ITestInterface, int>((i, j, k) => 42)
.c{caret}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {\})}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
Task<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>()))
.ReturnsAsync<int, string, bool, ITestInterface, int>((i, j, k) => 42)
.Callback<int,string,bool>((i,toto,ok) => {{caret}})
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {\})}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
Task<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>()))
.ReturnsAsync((int i, string j, bool k) => 42)
.c{caret}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:Callback<int,string,bool>((i,toto,ok) => {\})}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
Task<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>()))
.ReturnsAsync((int i, string j, bool k) => 42)
.Callback<int,string,bool>((i,toto,ok) => {{caret}})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected override bool AddLookupItems(CSharpCodeCompletionContext context, IIte
var solution = context.BasicContext.Solution;
var methodIdentifier = solution.GetComponent<IMoqMethodIdentifier>();

if (methodIdentifier.IsMoqReturnMethod(invocation))
if (methodIdentifier.IsMoqReturnMethod(invocation) || methodIdentifier.IsMoqReturnAsyncMethod(invocation))
{
invocation = invocation.InvokedExpression?.FirstChild as IInvocationExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IMoqMethodIdentifier
bool IsMoqSetupMethod([CanBeNull] IInvocationExpression invocationExpression);
bool IsMoqVerifyMethod([CanBeNull] IInvocationExpression invocationExpression);
bool IsMoqReturnMethod([CanBeNull] IInvocationExpression invocationExpression);
bool IsMoqReturnAsyncMethod([CanBeNull] IInvocationExpression invocationExpression);
bool IsMoqCallbackMethod([CanBeNull] IInvocationExpression invocationExpression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public bool IsMoqReturnMethod(IInvocationExpression invocationExpression)
"Method:Moq.Language.IReturns`2.Returns",
"Method:Moq.Language.IReturns`1.Returns"));

public bool IsMoqReturnAsyncMethod(IInvocationExpression invocationExpression)
=> IsMethod(invocationExpression,
declaredElement => IsMethodStartingWithString(declaredElement,
"Method:Moq.GeneratedReturnsExtensions.ReturnsAsync(Moq.Language.IReturns`2",
"Method:Moq.ReturnsExtensions.ReturnsAsync(Moq.Language.IReturns`2"));

public bool IsMoqCallbackMethod(IInvocationExpression expression)
=> IsMethod(expression,
declaredElement => IsMethodStartingWithString(declaredElement,
Expand Down

0 comments on commit 102cb18

Please sign in to comment.