Skip to content

Commit

Permalink
Add Result.MapOr extension and unit tests for it. (#44)
Browse files Browse the repository at this point in the history
* Add Result.MapOr extension and unit tests for it.

* Remove extra newline

* Adjust method comment
  • Loading branch information
Gregory Dunn authored May 2, 2021
1 parent f3c4dd0 commit b17b217
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
19 changes: 18 additions & 1 deletion wimm.Secundatives.UnitTests/Extensions/ResultMapping_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ public async Task MapErrorTask_IsErrorAndFuncReturnsValue_ReturnsResultOfFunc()
Assert.Equal(OtherError.OtherSadness, res.Error);
}


[Fact]
public async Task MapErrorTask_IsSuccessAndFuncReturnsValue_ReturnsValue()
{
Expand All @@ -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<string, TestError>(TestError.Sadness);
var res = underTest.MapOr(_ => "deet", _ => OtherError.OtherSadness.ToString());

Assert.Equal(OtherError.OtherSadness.ToString(), res);
}

private Result<T, TestError> ConstructWith<T>(T value)
{
Expand Down
24 changes: 20 additions & 4 deletions wimm.Secundatives/Extensions/ResultMappingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wimm.Secundatives
Expand Down Expand Up @@ -125,7 +122,7 @@ public static async Task<Result<U, TError>> Map<T, U, TError>(this Result<T, TEr
return result.Error;
}


/// <summary>
/// Asynchronously transforms a <see cref="Result{T, TError}"/> into a <see cref="Result{U, TError}"/> by applying an async function and collapsing
/// the result if the mapped <see cref="Result{T, TError}"/> containts a value otherwise by constructing a new
Expand Down Expand Up @@ -197,5 +194,24 @@ public static async Task<Result<T, UError>> MapError<T, TError, UError>(this Tas
return val.MapError(func);
}

/// <summary>
/// Transforms a <see cref="Result{T, TError}"/> into a <typeparamref name="U"/> by applying a function if the mapped
/// <see cref="Result{T, TError}"/> containts a value, otherwise by applying a function to the
/// <typeparamref name="TError"/> contained within <paramref name="result"/>
/// </summary>
/// <typeparam name="T"> The success type of the initial result </typeparam>
/// <typeparam name="U"> The type that <paramref name="result"/>'s value and error are transformed to by
/// <paramref name="valueFunc"/> and <paramref name="errorFunc"/> </typeparam>
/// <typeparam name="TError"> The error type of the result </typeparam>
/// <param name="result"> A <see cref="Result{T, TError}"/> whose state determines which function is applied </param>
/// <param name="valueFunc"> The function that will transform <paramref name="result"/>'s <typeparamref name="T"/> if it exists</param>
/// <param name="errorFunc"> The function that will transform <paramref name="result"/>'s <typeparamref name="TError"/> if it exists</param>
/// <returns> A <typeparamref name="U"/> that is returned by calling <paramref name="valueFunc"/> on the value of <paramref
/// name="result"/> or by calling <paramref name="errorFunc"/> on the error from <paramref name="result"/>
/// </returns>
public static U MapOr<T, U, TError>(this Result<T, TError> result, Func<T, U> valueFunc, Func<TError, U> errorFunc)
{
return result.IsValue ? valueFunc(result.Value) : errorFunc(result.Error);
}
}
}

0 comments on commit b17b217

Please sign in to comment.