From cdd1f2ec14c568c526624a182132d01e8dfb7ac3 Mon Sep 17 00:00:00 2001 From: Tim Schneider <43130816+DerStimmler@users.noreply.github.com> Date: Wed, 22 Dec 2021 15:39:20 +0100 Subject: [PATCH 1/2] feat(ensure): ensure method takes predicate of type Result --- .../Result/Extensions/Ensure.cs | 96 ++++++++++++++++ .../Result/Extensions/EnsureAsyncBoth.cs | 108 ++++++++++++++++++ .../Result/Extensions/EnsureAsyncLeft.cs | 54 +++++++++ .../Result/Extensions/EnsureAsyncRight.cs | 96 ++++++++++++++++ 4 files changed, 354 insertions(+) diff --git a/CSharpFunctionalExtensions/Result/Extensions/Ensure.cs b/CSharpFunctionalExtensions/Result/Extensions/Ensure.cs index d750025d..f7196c8e 100644 --- a/CSharpFunctionalExtensions/Result/Extensions/Ensure.cs +++ b/CSharpFunctionalExtensions/Result/Extensions/Ensure.cs @@ -73,5 +73,101 @@ public static Result Ensure(this Result result, Func predicate, string err return result; } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static Result Ensure(this Result result, Func predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static Result Ensure(this Result result, Func predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static Result Ensure(this Result result, Func> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static Result Ensure(this Result result, Func> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static Result Ensure(this Result result, Func predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = predicate(result.Value); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static Result Ensure(this Result result, Func> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = predicate(result.Value); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } } } diff --git a/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncBoth.cs b/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncBoth.cs index 2bfabc89..0a15d453 100644 --- a/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncBoth.cs +++ b/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncBoth.cs @@ -116,5 +116,113 @@ public static async Task Ensure(this Task resultTask, Func + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task Ensure(this Task resultTask, Func> predicate) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return result; + + var predicateResult = await predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func> predicate) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return result; + + var predicateResult = await predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task Ensure(this Task resultTask, Func>> predicate) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return result; + + var predicateResult = await predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func>> predicate) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return result; + + var predicateResult = await predicate(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func> predicate) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return result; + + var predicateResult = await predicate(result.Value); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func>> predicate) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return result; + + var predicateResult = await predicate(result.Value); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } } } diff --git a/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncLeft.cs b/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncLeft.cs index 07516fe5..bf742f8a 100644 --- a/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncLeft.cs +++ b/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncLeft.cs @@ -71,5 +71,59 @@ public static async Task Ensure(this Task resultTask, Func Result result = await resultTask.DefaultAwait(); return result.Ensure(predicate, errorMessage); } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task Ensure(this Task resultTask, Func predicate) + { + Result result = await resultTask.DefaultAwait(); + return result.Ensure(predicate); + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func predicate) + { + Result result = await resultTask.DefaultAwait(); + return result.Ensure(predicate); + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task Ensure(this Task resultTask, Func> predicate) + { + Result result = await resultTask.DefaultAwait(); + return result.Ensure(predicate); + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func> predicate) + { + Result result = await resultTask.DefaultAwait(); + return result.Ensure(predicate); + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func predicate) + { + Result result = await resultTask.DefaultAwait(); + return result.Ensure(predicate); + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Task> resultTask, Func> predicate) + { + Result result = await resultTask.DefaultAwait(); + return result.Ensure(predicate); + } } } diff --git a/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncRight.cs b/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncRight.cs index a9652536..510f5896 100644 --- a/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncRight.cs +++ b/CSharpFunctionalExtensions/Result/Extensions/EnsureAsyncRight.cs @@ -105,5 +105,101 @@ public static async Task Ensure(this Result result, Func> pre return result; } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task Ensure(this Result result, Func> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = await predicate().DefaultAwait(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Result result, Func> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = await predicate().DefaultAwait(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task Ensure(this Result result, Func>> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = await predicate().DefaultAwait(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Result result, Func>> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = await predicate().DefaultAwait(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Result result, Func> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = await predicate(result.Value).DefaultAwait(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } + + /// + /// Returns a new failure result if the predicate is a failure result. Otherwise returns the starting result. + /// + public static async Task> Ensure(this Result result, Func>> predicate) + { + if (result.IsFailure) + return result; + + var predicateResult = await predicate(result.Value).DefaultAwait(); + + if (predicateResult.IsFailure) + return Result.Failure(predicateResult.Error); + + return result; + } } } From 9752c394d5e15993ef4a7a28c2bb6d47245a7269 Mon Sep 17 00:00:00 2001 From: Tim Schneider <43130816+DerStimmler@users.noreply.github.com> Date: Wed, 22 Dec 2021 15:39:32 +0100 Subject: [PATCH 2/2] test(ensure): test ensure methods with predicate of type Result --- .../Extensions/EnsureAsyncBothTests.cs | 97 ++++++++++++++++++ .../Extensions/EnsureAsyncLeftTests.cs | 88 +++++++++++++++++ .../Extensions/EnsureAsyncRightTests.cs | 97 ++++++++++++++++++ .../ResultTests/Extensions/EnsureTests.cs | 98 +++++++++++++++++++ 4 files changed, 380 insertions(+) create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncBothTests.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncRightTests.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureTests.cs diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncBothTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncBothTests.cs new file mode 100644 index 00000000..4ed392cd --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncBothTests.cs @@ -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("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("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"); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncLeftTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncLeftTests.cs index e4669364..a440c8cc 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncLeftTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncLeftTests.cs @@ -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("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("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"); + } } } \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncRightTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncRightTests.cs new file mode 100644 index 00000000..4f553c44 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureAsyncRightTests.cs @@ -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("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("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"); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureTests.cs new file mode 100644 index 00000000..f343c7e9 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/EnsureTests.cs @@ -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("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("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"); + } + } +} \ No newline at end of file