diff --git a/Vonage.Test/Common/Monads/ResultExtensionsTest.cs b/Vonage.Test/Common/Monads/ResultExtensionsTest.cs index a80c57be..afe46225 100644 --- a/Vonage.Test/Common/Monads/ResultExtensionsTest.cs +++ b/Vonage.Test/Common/Monads/ResultExtensionsTest.cs @@ -1,8 +1,10 @@ -using System.Threading.Tasks; +#region +using System.Threading.Tasks; using FluentAssertions; using Vonage.Common.Monads; using Vonage.Test.Common.Extensions; using Xunit; +#endregion namespace Vonage.Test.Common.Monads; @@ -127,4 +129,64 @@ public async Task Match_ShouldReturnValue_GivenSuccess() var result = await TestBehaviors.CreateSuccessAsync(10).Match(_ => "some", _ => "none"); result.Should().Be("some"); } + + [Fact] + public async Task Do_ShouldExecuteSuccessAction_GivenStateIsSuccess() + { + var value = 0; + await TestBehaviors.CreateSuccessAsync(5).Do(success => value += success, _ => { }); + value.Should().Be(5); + } + + [Fact] + public async Task Do_ShouldExecuteFailureAction_GivenStateIsFailure() + { + var value = 0; + await TestBehaviors.CreateFailureAsync().Do(_ => { }, _ => value = 10); + value.Should().Be(10); + } + + [Fact] + public async Task Do_ShouldReturnInstance() => + await TestBehaviors.CreateSuccessAsync(5).Do(_ => { }, _ => { }).Should().BeSuccessAsync(5); + + [Fact] + public async Task DoWhenSuccess_ShouldExecuteAction_GivenStateIsSuccess() + { + var value = 0; + await TestBehaviors.CreateSuccessAsync(5).DoWhenSuccess(success => value += success); + value.Should().Be(5); + } + + [Fact] + public async Task DoWhenSuccess_ShouldNotExecuteAction_GivenStateIsFailure() + { + var value = 0; + await TestBehaviors.CreateFailureAsync().DoWhenSuccess(_ => value = 10); + value.Should().Be(0); + } + + [Fact] + public async Task DoWhenSuccess_ShouldReturnInstance() => + await TestBehaviors.CreateSuccessAsync(5).DoWhenSuccess(_ => { }).Should().BeSuccessAsync(5); + + [Fact] + public async Task DoWhenFailure_ShouldExecuteAction_GivenStateIsFailure() + { + var value = 0; + await TestBehaviors.CreateFailureAsync().DoWhenFailure(_ => value = 10); + value.Should().Be(10); + } + + [Fact] + public async Task DoWhenFailure_ShouldNotExecuteAction_GivenStateIsSuccess() + { + var value = 0; + await TestBehaviors.CreateSuccessAsync(5).DoWhenFailure(_ => value = 10); + value.Should().Be(0); + } + + [Fact] + public async Task DoWhenFailure_ShouldReturnInstance() => + await TestBehaviors.CreateSuccessAsync(5).DoWhenFailure(_ => { }).Should().BeSuccessAsync(5); } \ No newline at end of file diff --git a/Vonage/Common/Monads/ResultExtensions.cs b/Vonage/Common/Monads/ResultExtensions.cs index 0761d8c0..7a88eda4 100644 --- a/Vonage/Common/Monads/ResultExtensions.cs +++ b/Vonage/Common/Monads/ResultExtensions.cs @@ -1,6 +1,8 @@ -using System; +#region +using System; using System.Threading.Tasks; using Vonage.Common.Failures; +#endregion namespace Vonage.Common.Monads; @@ -23,7 +25,7 @@ public static async Task> Bind(this var result = await task.ConfigureAwait(false); return result.Bind(bind); } - + /// /// Monadic bind operation. /// @@ -38,7 +40,7 @@ public static async Task> BindAsync( var result = await task.ConfigureAwait(false); return await result.BindAsync(bind).ConfigureAwait(false); } - + /// /// Returns the default value if the Result is in the Failure state, the success value otherwise. /// @@ -51,7 +53,7 @@ public static async Task IfFailure(this Task> var result = await task.ConfigureAwait(false); return result.IfFailure(defaultValue); } - + /// /// Invokes the action if Result is in the Success state, otherwise nothing happens. /// @@ -64,7 +66,7 @@ public static async Task> IfSuccessAsync(this Task> task, var result = await task.ConfigureAwait(false); return await result.IfSuccessAsync(action).ConfigureAwait(false); } - + /// /// Projects from one value to another. /// @@ -79,7 +81,7 @@ public static async Task> Map(this T var result = await task.ConfigureAwait(false); return result.Map(map); } - + /// /// Projects from one value to another. /// @@ -94,7 +96,7 @@ public static async Task> MapAsync(t var result = await task.ConfigureAwait(false); return await result.MapAsync(map).ConfigureAwait(false); } - + /// /// Match the two states of the Result and return a non-null TB. /// @@ -110,4 +112,47 @@ public static async Task Match(this Task + /// Executes operations depending on the current state. + /// + /// Asynchronous result. + /// Success operation. + /// Failure operation. + /// Source type. + /// The initial result. + public static async Task> Do(this Task> task, + Action successOperation, Action failureOperation) + { + var result = await task.ConfigureAwait(false); + return result.Do(successOperation, failureOperation); + } + + /// + /// Executes an operation if success. + /// + /// Asynchronous result. + /// Success operation. + /// Source type. + /// The initial result. + public static async Task> DoWhenSuccess(this Task> task, + Action successOperation) + { + var result = await task.ConfigureAwait(false); + return result.DoWhenSuccess(successOperation); + } + + /// + /// Executes an operation if failure. + /// + /// Asynchronous result. + /// Failure operation. + /// Source type. + /// The initial result. + public static async Task> DoWhenFailure(this Task> task, + Action failureOperation) + { + var result = await task.ConfigureAwait(false); + return result.DoWhenFailure(failureOperation); + } } \ No newline at end of file