-
-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Blewzman/master
Add extension methods for IReturns to allow specifying return values exceptions on async methods in a synchronous way
- Loading branch information
Showing
4 changed files
with
214 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Moq.Language.Flow | ||
{ | ||
/// <summary> | ||
/// Defines async extension methods on IReturns. | ||
/// </summary> | ||
public static class IReturnsExtensions | ||
{ | ||
/// <summary> | ||
/// Allows to specify the return value of an asynchronous method. | ||
/// </summary> | ||
public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, TResult value) where TMock : class | ||
{ | ||
var tcs = new TaskCompletionSource<TResult>(); | ||
tcs.SetResult(value); | ||
|
||
return mock.Returns(tcs.Task); | ||
} | ||
|
||
/// <summary> | ||
/// Allows to specify the exception thrown by an asynchronous method. | ||
/// </summary> | ||
public static IReturnsResult<TMock> ThrowsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, Exception exception) where TMock : class | ||
{ | ||
var tcs = new TaskCompletionSource<TResult>(); | ||
tcs.SetException(exception); | ||
|
||
return mock.Returns(tcs.Task); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Moq.Language.Flow; | ||
using Xunit; | ||
using System.Collections.Generic; | ||
|
||
namespace Moq.Tests | ||
{ | ||
public class ReturnsExtensionsFixture | ||
{ | ||
public interface IAsyncInterface | ||
{ | ||
Task<string> NoParametersRefReturnType(); | ||
|
||
Task<int> NoParametersValueReturnType(); | ||
|
||
Task<string> RefParameterRefReturnType(string value); | ||
|
||
Task<int> RefParameterValueReturnType(string value); | ||
|
||
Task<string> ValueParameterRefReturnType(int value); | ||
|
||
Task<int> ValueParameterValueReturnType(int value); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsAsync_on_NoParametersRefReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
mock.Setup(x => x.NoParametersRefReturnType()).ReturnsAsync("TestString"); | ||
|
||
var task = mock.Object.NoParametersRefReturnType(); | ||
|
||
Assert.True(task.IsCompleted); | ||
Assert.Equal("TestString", task.Result); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsAsync_on_NoParametersValueReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
mock.Setup(x => x.NoParametersValueReturnType()).ReturnsAsync(36); | ||
|
||
var task = mock.Object.NoParametersValueReturnType(); | ||
|
||
Assert.True(task.IsCompleted); | ||
Assert.Equal(36, task.Result); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsAsync_on_RefParameterRefReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
mock.Setup(x => x.RefParameterRefReturnType("Param1")).ReturnsAsync("TestString"); | ||
|
||
var task = mock.Object.RefParameterRefReturnType("Param1"); | ||
|
||
Assert.True(task.IsCompleted); | ||
Assert.Equal("TestString", task.Result); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsAsync_on_RefParameterValueReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
mock.Setup(x => x.RefParameterValueReturnType("Param1")).ReturnsAsync(36); | ||
|
||
var task = mock.Object.RefParameterValueReturnType("Param1"); | ||
|
||
Assert.True(task.IsCompleted); | ||
Assert.Equal(36, task.Result); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsAsync_on_ValueParameterRefReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
mock.Setup(x => x.ValueParameterRefReturnType(36)).ReturnsAsync("TestString"); | ||
|
||
var task = mock.Object.ValueParameterRefReturnType(36); | ||
|
||
Assert.True(task.IsCompleted); | ||
Assert.Equal("TestString", task.Result); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsAsync_on_ValueParameterValueReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
mock.Setup(x => x.ValueParameterValueReturnType(36)).ReturnsAsync(37); | ||
|
||
var task = mock.Object.ValueParameterValueReturnType(36); | ||
|
||
Assert.True(task.IsCompleted); | ||
Assert.Equal(37, task.Result); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsAsync_on_NoParametersRefReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
var exception = new InvalidOperationException(); | ||
mock.Setup(x => x.NoParametersRefReturnType()).ThrowsAsync(exception); | ||
|
||
var task = mock.Object.NoParametersRefReturnType(); | ||
|
||
Assert.True(task.IsFaulted); | ||
Assert.Equal(exception, task.Exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsAsync_on_NoParametersValueReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
var exception = new InvalidOperationException(); | ||
mock.Setup(x => x.NoParametersValueReturnType()).ThrowsAsync(exception); | ||
|
||
var task = mock.Object.NoParametersValueReturnType(); | ||
|
||
Assert.True(task.IsFaulted); | ||
Assert.Equal(exception, task.Exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsAsync_on_RefParameterRefReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
var exception = new InvalidOperationException(); | ||
mock.Setup(x => x.RefParameterRefReturnType("Param1")).ThrowsAsync(exception); | ||
|
||
var task = mock.Object.RefParameterRefReturnType("Param1"); | ||
|
||
Assert.True(task.IsFaulted); | ||
Assert.Equal(exception, task.Exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsAsync_on_RefParameterValueReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
var exception = new InvalidOperationException(); | ||
mock.Setup(x => x.RefParameterValueReturnType("Param1")).ThrowsAsync(exception); | ||
|
||
var task = mock.Object.RefParameterValueReturnType("Param1"); | ||
|
||
Assert.True(task.IsFaulted); | ||
Assert.Equal(exception, task.Exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsAsync_on_ValueParameterRefReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
var exception = new InvalidOperationException(); | ||
mock.Setup(x => x.ValueParameterRefReturnType(36)).ThrowsAsync(exception); | ||
|
||
var task = mock.Object.ValueParameterRefReturnType(36); | ||
|
||
Assert.True(task.IsFaulted); | ||
Assert.Equal(exception, task.Exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsAsync_on_ValueParameterValueReturnType() | ||
{ | ||
var mock = new Mock<IAsyncInterface>(); | ||
var exception = new InvalidOperationException(); | ||
mock.Setup(x => x.ValueParameterValueReturnType(36)).ThrowsAsync(exception); | ||
|
||
var task = mock.Object.ValueParameterValueReturnType(36); | ||
|
||
Assert.True(task.IsFaulted); | ||
Assert.Equal(exception, task.Exception.InnerException); | ||
} | ||
} | ||
} |