Skip to content

Commit

Permalink
Add ReturnsAsync completion
Browse files Browse the repository at this point in the history
  • Loading branch information
lconstan committed Jun 11, 2022
1 parent 2053df6 commit 2a16700
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using JetBrains.ReSharper.FeaturesTestFramework.Completion;
using JetBrains.ReSharper.TestFramework;
using NUnit.Framework;

namespace Abc.MoqComplete.Tests.Completion
{
[TestNetCore21("Moq/4.18.1")]
public class ReturnsAsyncMethodProviderTests: CodeCompletionTestBase
{
protected override CodeCompletionTestType TestType => CodeCompletionTestType.Action;
protected override string RelativeTestDataPath => "ReturnsAsyncCompletion";

[TestCase("returnsAsyncCompletion")]
[TestCase("returnsAsyncCompletion_afterCallback")]
[TestCase("returnsAsyncCompletion_generic")]
[TestCase("ValueTask_returnsAsyncCompletion")]
[TestCase("ValueTask_returnsAsyncCompletion_afterCallback")]
[TestCase("ValueTask_returnsAsyncCompletion_generic")]
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:ReturnsAsync((int i, string toto, bool ok) => )}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
ValueTask 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>()))
.R{caret}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Returns<int, string, bool>((i, toto, ok) => )
Returns{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:ReturnsAsync((int i, string toto, bool ok) => )}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

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

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

namespace ConsoleApp1.Tests
{
public interface ITest<T, U>
{
ValueTask<int> Coco(ITest2 test, T test2, U test3);

}
public interface ITest2 : ITest<double, string>
{

}

[TestFixture]
public class TestClass
{

[Test]
public void METHOD()
{
var m = new Mock<ITest2>();
m.Setup(x=>x.Coco(It.IsAny<ITest2>(), It.IsAny<double>(), It.IsAny<string>()))
.{caret}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ${COMPLETE_ITEM:ReturnsAsync((ITest2 test, double test2, string test3) => )}
using NUnit.Framework;
using Moq;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITest<T, U>
{
ValueTask<int> Coco(ITest2 test, T test2, U test3);

}
public interface ITest2 : ITest<double, string>
{

}

[TestFixture]
public class TestClass
{

[Test]
public void METHOD()
{
var m = new Mock<ITest2>();
m.Setup(x=>x.Coco(It.IsAny<ITest2>(), It.IsAny<double>(), It.IsAny<string>()))
.ReturnsAsync((ITest2 test, double test2, string test3) => {caret})

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ${COMPLETE_ITEM:ReturnsAsync((int i, string toto, bool ok) => )}
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITestInterface
{
Task 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>()))
.R{caret}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Returns<int, string, bool>((i, toto, ok) => )
Returns{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:ReturnsAsync((int i, string toto, bool 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>()))
.Callback<int, string, bool>((i, toto, ok) => {})
.R{caret}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ${COMPLETE_ITEM:ReturnsAsync((int i, string toto, bool 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>()))
.Callback<int, string, bool>((i, toto, ok) => {})
.ReturnsAsync((int i, string toto, bool ok) => {caret})
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ${COMPLETE_ITEM:ReturnsAsync((ITest2 test, double test2, string test3) => )}
using NUnit.Framework;
using Moq;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITest<T, U>
{
Task<int> Coco(ITest2 test, T test2, U test3);

}
public interface ITest2 : ITest<double, string>
{

}

[TestFixture]
public class TestClass
{

[Test]
public void METHOD()
{
var m = new Mock<ITest2>();
m.Setup(x=>x.Coco(It.IsAny<ITest2>(), It.IsAny<double>(), It.IsAny<string>()))
.{caret}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ${COMPLETE_ITEM:ReturnsAsync((ITest2 test, double test2, string test3) => )}
using NUnit.Framework;
using Moq;
using System.Threading.Tasks;

namespace ConsoleApp1.Tests
{
public interface ITest<T, U>
{
Task<int> Coco(ITest2 test, T test2, U test3);

}
public interface ITest2 : ITest<double, string>
{

}

[TestFixture]
public class TestClass
{

[Test]
public void METHOD()
{
var m = new Mock<ITest2>();
m.Setup(x=>x.Coco(It.IsAny<ITest2>(), It.IsAny<double>(), It.IsAny<string>()))
.ReturnsAsync((ITest2 test, double test2, string test3) => {caret})

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private IMethod GetMockedMethodFromSetupMethod(ISolution solution, IInvocationEx
private IEnumerable<string> GetMockedMethodParameterTypes(ISolution solution, IInvocationExpression invocation)
{
var mockedMethodProvider = solution.GetComponent<T>();
return mockedMethodProvider.GetMockedMethodParameterTypes(invocation);
return mockedMethodProvider.GetMockedMethodParameterTypesString(invocation);
}

protected override bool IsAvailable(CSharpCodeCompletionContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abc.MoqComplete.Extensions;
using Abc.MoqComplete.Services;
using Abc.MoqComplete.Services.MethodProvider;
Expand Down Expand Up @@ -59,11 +62,28 @@ protected override bool AddLookupItems(CSharpCodeCompletionContext context, IIte
return false;
}

List<string> types = methodProvider.GetMockedMethodParameterTypes(invocation).ToList();
List<string> types = methodProvider.GetMockedMethodParameterTypesString(invocation).ToList();
List<string> variablesName = mockedMethod.Parameters.Select(p => p.ShortName).ToList();
string returnCallback = $"Returns<{string.Join(", ", types)}>(({string.Join(", ", variablesName)}) => )";
AddProposedCallback(context, collector, returnCallback);

IType returnType = mockedMethod.ReturnType;

if (returnType.IsGenericTask() || returnType.IsGenericValueTask())
{
var returnsAsyncCallback = new StringBuilder("ReturnsAsync((");
for (var i = 0; i < types.Count; i++)
{
returnsAsyncCallback.Append($"{types[i]} {variablesName[i]}, ");
}

// Remove last space and comma
returnsAsyncCallback.Remove(returnsAsyncCallback.Length - 2, 2);
returnsAsyncCallback.Append(") => )");

AddProposedCallback(context, collector, returnsAsyncCallback.ToString());
}

return true;
}

Expand Down
Loading

0 comments on commit 2a16700

Please sign in to comment.