Skip to content

Commit

Permalink
Implemented AsMaybe for tasks (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Insomniak47 authored Aug 8, 2019
1 parent 64068c2 commit 7fc6751
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
44 changes: 43 additions & 1 deletion wimm.Secundatives.UnitTests/Extensions/AsMaybe_Test.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using wimm.Secundatives.Extensions;
using System.Threading.Tasks;
using wimm.Secundatives.Extensions;
using Xunit;

namespace wimm.Secundatives.UnitTests.Extensions
Expand Down Expand Up @@ -44,6 +45,47 @@ public void AsMaybeNullable_Value_ReturnsSome()
Assert.Equal(value.Value, res.Value);
}

[Fact]
public async Task AsMaybeTaskClass_Null_ReturnsNone()
{
var value = Task.FromResult((string)null);

var res = await value.AsMaybe();

Assert.Equal(Maybe<string>.None, res);
}


[Fact]
public async Task AsMaybeTaskClass_Value_ReturnsSome()
{
var value = Task.FromResult("doot");

var res = await value.AsMaybe();

Assert.Equal("doot", res.Value);
}

[Fact]
public async Task AsMaybeTaskNullable_Null_ReturnsNone()
{
var value = Task.FromResult((int?)null);

var res = await value.AsMaybe();

Assert.Equal(Maybe<int>.None, res);
}

[Fact]
public async Task AsMaybeTaskNullable_Value_ReturnsSome()
{
var value = Task.FromResult((int?)25);

var res = await value.AsMaybe();

Assert.Equal(25, res.Value);
}


}
}
36 changes: 34 additions & 2 deletions wimm.Secundatives/Extensions/MaybeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public static Maybe<T> AsMaybe<T>(this T value) where T : class
return value != null ? new Maybe<T>(value) : Maybe<T>.None;
}



/// <summary>
/// An explicit conversion from nullable struct types to <see cref="Maybe{T}"/> for ease of use and compatibility
/// </summary>
Expand All @@ -99,5 +97,39 @@ public static Maybe<T> AsMaybe<T>(this T? value) where T : struct
}


/// <summary>
/// Helper function that awaits a <see cref="Task{T}"/>'s result and converts it into a <see cref="Maybe{T}"/>
/// </summary>
/// <typeparam name="T">
/// <typeparam name="T"> The type that will be returned from awaiting the task and that
/// will be tested and the type of the resultant <see cref="Maybe{T}"/> </typeparam>
/// </typeparam>
/// <param name="value"> A <see cref="Task{T}"/> That will contain the value to be tested </param
/// <returns> A <see cref="Maybe{T}"/> with a value if <see cref="value.Result"/> is not <c> null </c> else
/// <see cref="Maybe{T}.None"/>
/// </returns>
public static async Task<Maybe<T>> AsMaybe<T>(this Task<T> value) where T : class
{
return (await value).AsMaybe();
}


/// <summary>
/// Helper function that awaits a <see cref="Task{Nullable{T}}"/>'s result and converts it into a <see cref="Maybe{T}"/>
/// </summary>
/// <typeparam name="T">
/// The underlying type of the <see cref="Nullable{T}"/> and the type of the resultant <see cref="Maybe{T}"/>
/// </typeparam>
/// <param name="value"> A <see cref="Task{T}"/> That will contain the value to be tested </param
/// <returns> A <see cref="Maybe{T}"/> with a value if <see cref="value.Result"/> is not <c> null </c> else
/// <see cref="Maybe{T}.None"/>
/// </returns>
public static async Task<Maybe<T>> AsMaybe<T>(this Task<T?> value) where T : struct
{
return (await value).AsMaybe();
}



}
}

0 comments on commit 7fc6751

Please sign in to comment.