Skip to content

Commit

Permalink
Merge pull request #382 from DerStimmler/feature/ensure-result
Browse files Browse the repository at this point in the history
Add type Result as predicate for .Ensure()
  • Loading branch information
vkhorikov authored Dec 24, 2021
2 parents d0bf48e + 9752c39 commit a4adb8d
Show file tree
Hide file tree
Showing 8 changed files with 734 additions and 0 deletions.
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,93 @@ public async Task Ensure_with_errorPredicate_using_errorPredicate()
result.IsSuccess.Should().BeFalse("Input Result fails predicate condition");
result.Error.Should().Be("new error message: string should not be empty");
}

[Fact]
public async Task Ensure_with_successInput_and_successPredicate()
{
var initialResult = Task.FromResult(Result.Success("Initial message"));

var result = await initialResult.Ensure(() => 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(() => 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(() => 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(() => 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(_ => 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(_ => 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(_ => 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(_ => Result.Failure("Success Message"));

result.IsSuccess.Should().BeFalse("Initial result and predicate is failure result");;
result.Error.Should().Be("Initial Error message");
}
}
}
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");
}
}
}
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");
}
}
}
Loading

0 comments on commit a4adb8d

Please sign in to comment.