From b17b2177e6c0fe4afa18e0ada5771d706fe0bb9d Mon Sep 17 00:00:00 2001 From: Gregory Dunn <44067421+beyondunn@users.noreply.github.com> Date: Sat, 1 May 2021 21:21:35 -0300 Subject: [PATCH] Add Result.MapOr extension and unit tests for it. (#44) * Add Result.MapOr extension and unit tests for it. * Remove extra newline * Adjust method comment --- .../Extensions/ResultMapping_Test.cs | 19 ++++++++++++++- .../Extensions/ResultMappingExtensions.cs | 24 +++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/wimm.Secundatives.UnitTests/Extensions/ResultMapping_Test.cs b/wimm.Secundatives.UnitTests/Extensions/ResultMapping_Test.cs index f42db91..3911ade 100644 --- a/wimm.Secundatives.UnitTests/Extensions/ResultMapping_Test.cs +++ b/wimm.Secundatives.UnitTests/Extensions/ResultMapping_Test.cs @@ -153,7 +153,6 @@ public async Task MapErrorTask_IsErrorAndFuncReturnsValue_ReturnsResultOfFunc() Assert.Equal(OtherError.OtherSadness, res.Error); } - [Fact] public async Task MapErrorTask_IsSuccessAndFuncReturnsValue_ReturnsValue() { @@ -163,6 +162,24 @@ public async Task MapErrorTask_IsSuccessAndFuncReturnsValue_ReturnsValue() Assert.Equal("doot", res.Value); } + [Fact] + public void MapOr_IsSuccess_ReturnsResultOfFunc() + { + var underTest = ConstructWith(1); + var res = underTest.MapOr(_ => "deet", _ => OtherError.OtherSadness.ToString()); + + Assert.Equal("deet", res); + } + + + [Fact] + public void MapOr_IsError_ReturnsResultOfErrorFunc() + { + var underTest = new Result(TestError.Sadness); + var res = underTest.MapOr(_ => "deet", _ => OtherError.OtherSadness.ToString()); + + Assert.Equal(OtherError.OtherSadness.ToString(), res); + } private Result ConstructWith(T value) { diff --git a/wimm.Secundatives/Extensions/ResultMappingExtensions.cs b/wimm.Secundatives/Extensions/ResultMappingExtensions.cs index 7f2958c..dced3c5 100644 --- a/wimm.Secundatives/Extensions/ResultMappingExtensions.cs +++ b/wimm.Secundatives/Extensions/ResultMappingExtensions.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; namespace wimm.Secundatives @@ -125,7 +122,7 @@ public static async Task> Map(this Result /// Asynchronously transforms a into a by applying an async function and collapsing /// the result if the mapped containts a value otherwise by constructing a new @@ -197,5 +194,24 @@ public static async Task> MapError(this Tas return val.MapError(func); } + /// + /// Transforms a into a by applying a function if the mapped + /// containts a value, otherwise by applying a function to the + /// contained within + /// + /// The success type of the initial result + /// The type that 's value and error are transformed to by + /// and + /// The error type of the result + /// A whose state determines which function is applied + /// The function that will transform 's if it exists + /// The function that will transform 's if it exists + /// A that is returned by calling on the value of or by calling on the error from + /// + public static U MapOr(this Result result, Func valueFunc, Func errorFunc) + { + return result.IsValue ? valueFunc(result.Value) : errorFunc(result.Error); + } } }