-
Notifications
You must be signed in to change notification settings - Fork 304
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 #382 from DerStimmler/feature/ensure-result
Add type Result as predicate for .Ensure()
- Loading branch information
Showing
8 changed files
with
734 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncBothTests.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,97 @@ | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions | ||
{ | ||
public class EnsureAsyncBothTests | ||
{ | ||
[Fact] | ||
public async Task Ensure_with_successInput_and_successPredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Success("Initial message")); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Success("Success message"))); | ||
|
||
result.IsSuccess.Should().BeTrue("Initial result and predicate succeeded"); | ||
result.Value.Should().Be("Initial message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_successInput_and_failurePredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Success("Initial Result")); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Failure("Error message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Predicate is failure result"); | ||
result.Error.Should().Be("Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_successPredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Failure("Initial Error message")); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Success("Success message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result"); | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_failurePredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Failure("Initial Error message")); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Failure("Error message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result"); | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_successInput_and_paramerterisedFailurePredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Success("Initial Success message")); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Failure("Error Message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Predicate is failure result"); | ||
result.Error.Should().Be("Error Message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_successInput_and_paramerterisedSuccessPredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Success("Initial Success message")); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Success("Success Message"))); | ||
|
||
result.IsSuccess.Should().BeTrue("Initial result and predicate succeeded");; | ||
result.Value.Should().Be("Initial Success message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_paramerterisedSuccessPredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Failure<string>("Initial Error message")); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Success("Success Message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result");; | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_paramerterisedFailurePredicate() | ||
{ | ||
var initialResult = Task.FromResult(Result.Failure<string>("Initial Error message")); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Failure("Success Message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result and predicate is failure result");; | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncRightTests.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,97 @@ | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions | ||
{ | ||
public class EnsureAsyncRightTests | ||
{ | ||
[Fact] | ||
public async Task Ensure_with_successInput_and_successPredicate() | ||
{ | ||
var initialResult = Result.Success("Initial message"); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Success("Success message"))); | ||
|
||
result.IsSuccess.Should().BeTrue("Initial result and predicate succeeded"); | ||
result.Value.Should().Be("Initial message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_successInput_and_failurePredicate() | ||
{ | ||
var initialResult = Result.Success("Initial Result"); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Failure("Error message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Predicate is failure result"); | ||
result.Error.Should().Be("Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_successPredicate() | ||
{ | ||
var initialResult = Result.Failure("Initial Error message"); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Success("Success message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result"); | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_failurePredicate() | ||
{ | ||
var initialResult = Result.Failure("Initial Error message"); | ||
|
||
var result = await initialResult.Ensure(() => Task.FromResult(Result.Failure("Error message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result"); | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_successInput_and_paramerterisedFailurePredicate() | ||
{ | ||
var initialResult = Result.Success("Initial Success message"); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Failure("Error Message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Predicate is failure result"); | ||
result.Error.Should().Be("Error Message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_successInput_and_paramerterisedSuccessPredicate() | ||
{ | ||
var initialResult = Result.Success("Initial Success message"); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Success("Success Message"))); | ||
|
||
result.IsSuccess.Should().BeTrue("Initial result and predicate succeeded");; | ||
result.Value.Should().Be("Initial Success message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_paramerterisedSuccessPredicate() | ||
{ | ||
var initialResult = Result.Failure<string>("Initial Error message"); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Success("Success Message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result");; | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_with_failureInput_and_paramerterisedFailurePredicate() | ||
{ | ||
var initialResult = Result.Failure<string>("Initial Error message"); | ||
|
||
var result = await initialResult.Ensure(_ => Task.FromResult(Result.Failure("Success Message"))); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result and predicate is failure result");; | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureTests.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,98 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions | ||
{ | ||
public class EnsureTests | ||
{ | ||
[Fact] | ||
public void Ensure_with_successInput_and_successPredicate() | ||
{ | ||
var initialResult = Result.Success("Initial message"); | ||
|
||
var result = initialResult.Ensure(() => Result.Success("Success message")); | ||
|
||
result.IsSuccess.Should().BeTrue("Initial result and predicate succeeded"); | ||
result.Value.Should().Be("Initial message"); | ||
} | ||
|
||
[Fact] | ||
public void Ensure_with_successInput_and_failurePredicate() | ||
{ | ||
var initialResult = Result.Success("Initial Result"); | ||
|
||
var result = initialResult.Ensure(() => Result.Failure("Error message")); | ||
|
||
result.IsSuccess.Should().BeFalse("Predicate is failure result"); | ||
result.Error.Should().Be("Error message"); | ||
} | ||
|
||
[Fact] | ||
public void Ensure_with_failureInput_and_successPredicate() | ||
{ | ||
var initialResult = Result.Failure("Initial Error message"); | ||
|
||
var result = initialResult.Ensure(() => Result.Success("Success message")); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result"); | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public void Ensure_with_failureInput_and_failurePredicate() | ||
{ | ||
var initialResult = Result.Failure("Initial Error message"); | ||
|
||
var result = initialResult.Ensure(() => Result.Failure("Error message")); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result"); | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public void Ensure_with_successInput_and_paramerterisedFailurePredicate() | ||
{ | ||
var initialResult = Result.Success("Initial Success message"); | ||
|
||
var result = initialResult.Ensure(_ => Result.Failure("Error Message")); | ||
|
||
result.IsSuccess.Should().BeFalse("Predicate is failure result"); | ||
result.Error.Should().Be("Error Message"); | ||
} | ||
|
||
[Fact] | ||
public void Ensure_with_successInput_and_paramerterisedSuccessPredicate() | ||
{ | ||
var initialResult = Result.Success("Initial Success message"); | ||
|
||
var result = initialResult.Ensure(_ => Result.Success("Success Message")); | ||
|
||
result.IsSuccess.Should().BeTrue("Initial result and predicate succeeded");; | ||
result.Value.Should().Be("Initial Success message"); | ||
} | ||
|
||
[Fact] | ||
public void Ensure_with_failureInput_and_paramerterisedSuccessPredicate() | ||
{ | ||
var initialResult = Result.Failure<string>("Initial Error message"); | ||
|
||
var result = initialResult.Ensure(_ => Result.Success("Success Message")); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result is failure result");; | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
|
||
[Fact] | ||
public void Ensure_with_failureInput_and_paramerterisedFailurePredicate() | ||
{ | ||
var initialResult = Result.Failure<string>("Initial Error message"); | ||
|
||
var result = initialResult.Ensure(_ => Result.Failure("Success Message")); | ||
|
||
result.IsSuccess.Should().BeFalse("Initial result and predicate is failure result");; | ||
result.Error.Should().Be("Initial Error message"); | ||
} | ||
} | ||
} |
Oops, something went wrong.