-
Notifications
You must be signed in to change notification settings - Fork 0
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 #101 from doomchild/bugfix/issue-100
Issue-100: Fix async Filters double-wrapping exceptions
- Loading branch information
Showing
8 changed files
with
522 additions
and
453 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
69 changes: 69 additions & 0 deletions
69
tests/unit/Filter/WithAsyncPredicate/WithMorphism/WithRawMorphism.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,69 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using RLC.TaskChaining; | ||
using Xunit; | ||
|
||
namespace RLC.TaskChainingTests.Filter.WithAsyncPredicate.WithMorphism; | ||
|
||
public class WithRawMorphism | ||
{ | ||
[Fact] | ||
public async Task ItShouldNotFaultForASuccessfulPredicate() | ||
{ | ||
Task<int> testTask = Task.FromResult(2) | ||
.Filter(AsyncPredicate, value => new Exception("not even")); | ||
|
||
await Task.Delay(10); | ||
|
||
Assert.True(testTask.IsCompletedSuccessfully); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldNotChangeTheValueForASuccessfulPredicate() | ||
{ | ||
int expectedValue = 2; | ||
int actualValue = await Task.FromResult(2) | ||
.Filter(AsyncPredicate, value => new Exception("not even")); | ||
|
||
Assert.Equal(expectedValue, actualValue); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldTransitionForAFailedPredicate() | ||
{ | ||
string expectedMessage = Guid.NewGuid().ToString(); | ||
Task<int> testTask = Task.FromResult(1) | ||
.Filter(AsyncPredicate, value => new ArgumentException(expectedMessage)); | ||
Exception thrownException = new(); | ||
|
||
try | ||
{ | ||
await testTask; | ||
} | ||
catch (ArgumentException exception) | ||
{ | ||
thrownException = exception; | ||
} | ||
|
||
Assert.Equal(expectedMessage, thrownException.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldNotDoubleWrapAnExistingException() | ||
{ | ||
Task<int> testTask = Task.FromException<int>(new ArithmeticException()) | ||
.Filter( | ||
AsyncPredicate, | ||
_ => new ArgumentException() | ||
); | ||
|
||
await Assert.ThrowsAsync<ArithmeticException>(() => testTask); | ||
} | ||
|
||
private async Task<bool> AsyncPredicate(int value) | ||
{ | ||
await Task.Delay(1); | ||
|
||
return value % 2 == 0; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
tests/unit/Filter/WithAsyncPredicate/WithMorphism/WithTaskMorphism.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,78 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using RLC.TaskChaining; | ||
using Xunit; | ||
|
||
namespace RLC.TaskChainingTests.Filter.WithAsyncPredicate.WithMorphism; | ||
|
||
public class WithTaskMorphism | ||
{ | ||
[Fact] | ||
public async Task ItShouldNotFaultForASuccessfulPredicate() | ||
{ | ||
Task<int> testTask = Task.FromResult(2) | ||
.Filter( | ||
AsyncPredicate, | ||
_ => Task.FromResult(new Exception("not even")) | ||
); | ||
|
||
await Task.Delay(10); | ||
|
||
Assert.True(testTask.IsCompletedSuccessfully); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldNotChangeTheValueForASuccessfulPredicate() | ||
{ | ||
int expectedValue = 2; | ||
int actualValue = await Task.FromResult(2) | ||
.Filter( | ||
AsyncPredicate, | ||
_ => Task.FromResult(new Exception("not even")) | ||
); | ||
|
||
Assert.Equal(expectedValue, actualValue); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldTransitionForAFailedPredicate() | ||
{ | ||
string expectedMessage = Guid.NewGuid().ToString(); | ||
Task<int> testTask = Task.FromResult(1) | ||
.Filter( | ||
AsyncPredicate, | ||
_ => Task.FromResult(new ArgumentException(expectedMessage)) | ||
); | ||
Exception thrownException = new(); | ||
|
||
try | ||
{ | ||
await testTask; | ||
} | ||
catch (ArgumentException exception) | ||
{ | ||
thrownException = exception; | ||
} | ||
|
||
Assert.Equal(expectedMessage, thrownException.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldNotDoubleWrapAnExistingException() | ||
{ | ||
Task<int> testTask = Task.FromException<int>(new ArithmeticException()) | ||
.Filter( | ||
AsyncPredicate, | ||
_ => Task.FromResult(new ArgumentException()) | ||
); | ||
|
||
await Assert.ThrowsAsync<ArithmeticException>(() => testTask); | ||
} | ||
|
||
private async Task<bool> AsyncPredicate(int value) | ||
{ | ||
await Task.Delay(1); | ||
|
||
return value % 2 == 0; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
tests/unit/Filter/WithAsyncPredicate/WithMorphism/WithTaskSupplier.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,76 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using RLC.TaskChaining; | ||
using Xunit; | ||
|
||
namespace RLC.TaskChainingTests.Filter.WithAsyncPredicate.WithMorphism; | ||
|
||
public class WithTaskSupplier | ||
{ | ||
[Fact] | ||
public async Task ItShouldNotFaultForASuccessfulPredicate() | ||
{ | ||
Task<int> testTask = Task.FromResult(2).Filter( | ||
AsyncPredicate, | ||
() => Task.FromResult(new Exception("not even")) | ||
); | ||
|
||
await Task.Delay(10); | ||
|
||
Assert.True(testTask.IsCompletedSuccessfully); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldNotChangeTheValueForASuccessfulPredicate() | ||
{ | ||
int expectedValue = 2; | ||
int actualValue = await Task.FromResult(2).Filter( | ||
AsyncPredicate, | ||
() => Task.FromResult(new Exception("not even")) | ||
); | ||
|
||
Assert.Equal(expectedValue, actualValue); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldTransitionForAFailedPredicate() | ||
{ | ||
string expectedMessage = Guid.NewGuid().ToString(); | ||
Task<int> testTask = Task.FromResult(1) | ||
.Filter( | ||
AsyncPredicate, | ||
() => Task.FromResult(new ArgumentException(expectedMessage)) | ||
); | ||
Exception thrownException = new(); | ||
|
||
try | ||
{ | ||
await testTask; | ||
} | ||
catch (ArgumentException exception) | ||
{ | ||
thrownException = exception; | ||
} | ||
|
||
Assert.Equal(expectedMessage, thrownException.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task ItShouldNotDoubleWrapAnExistingException() | ||
{ | ||
Task<int> testTask = Task.FromException<int>(new ArithmeticException()) | ||
.Filter( | ||
AsyncPredicate, | ||
() => Task.FromResult(new ArgumentException()) | ||
); | ||
|
||
await Assert.ThrowsAsync<ArithmeticException>(() => testTask); | ||
} | ||
|
||
private async Task<bool> AsyncPredicate(int value) | ||
{ | ||
await Task.Delay(1); | ||
|
||
return value % 2 == 0; | ||
} | ||
} |
Oops, something went wrong.