diff --git a/wimm.Secundatives.UnitTests/Extensions/UnwrapOr_Test.cs b/wimm.Secundatives.UnitTests/Extensions/UnwrapOr_Test.cs index 4d4d0ce..cb1ae4f 100644 --- a/wimm.Secundatives.UnitTests/Extensions/UnwrapOr_Test.cs +++ b/wimm.Secundatives.UnitTests/Extensions/UnwrapOr_Test.cs @@ -9,9 +9,15 @@ public class UnwrapOr_Test { private const string _valid = "doot"; private const string _default = "tood"; - private readonly Func _defaultFunc = () => _default; + private readonly Func _defaultFunc = () => { _set = true; return _default; }; private readonly Func> _defaultAsyncFunc = async () => { _set = true; ; return await Task.FromResult(_default); }; private static bool _set = false; + + public UnwrapOr_Test() + { + _set = false; + } + [Fact] public void UnwrapOr_Value_ReturnsValue() { @@ -43,7 +49,7 @@ public void UnwrapOr_NoValue_ReturnsDefaultParam() } [Fact] - public async Task UnwrapOrAsync_NoValue_ExecutesFunction() + public async Task UnwrapOrAsyncFunc_NoValue_ExecutesFunction() { var underTest = new Maybe(); Assert.Equal(_default, await underTest.UnwrapOr(_defaultAsyncFunc)); @@ -51,18 +57,64 @@ public async Task UnwrapOrAsync_NoValue_ExecutesFunction() } [Fact] - public async Task UnwrapOrAsync_Value_ReturnsValue() + public async Task UnwrapOrAsyncFunc_Value_ReturnsValue() { var underTest = new Maybe(_valid); Assert.Equal(_valid, await underTest.UnwrapOr(_defaultAsyncFunc)); } [Fact] - public async Task UnwrapOrAsync_Value_DoesNotExecuteFunction() + public async Task UnwrapOrAsyncFunc_Value_DoesNotExecuteFunction() { var underTest = new Maybe(_valid); _ = await underTest.UnwrapOr(_defaultAsyncFunc); Assert.False(_set); } + + [Fact] + public async Task UnwrapOrTaskMaybeAsyncFunc_Value_ReturnsValue() + { + var underTest = Task.FromResult(new Maybe(_valid)); + Assert.Equal(_valid, await underTest.UnwrapOr(_defaultAsyncFunc)); + } + + [Fact] + public async Task UnwrapOrTaskMaybeAsyncFunc_Value_DoesNotExecuteFunction() + { + var underTest = Task.FromResult(new Maybe(_valid)); + _ = await underTest.UnwrapOr(_defaultAsyncFunc); + Assert.False(_set); + } + + [Fact] + public async Task UnwrapOrTaskMaybeAsyncFunc_NoValue_ExecutesFunction() + { + var underTest = Task.FromResult(new Maybe()); + Assert.Equal(_default, await underTest.UnwrapOr(_defaultAsyncFunc)); + Assert.True(_set); + } + + [Fact] + public async Task UnwrapOrTaskMaybe_Value_ReturnsValue() + { + var underTest = Task.FromResult(new Maybe(_valid)); + Assert.Equal(_valid, await underTest.UnwrapOr(_defaultFunc)); + } + + [Fact] + public async Task UnwrapOrTaskMaybe_Value_DoesNotExecuteFunction() + { + var underTest = Task.FromResult(new Maybe(_valid)); + _ = await underTest.UnwrapOr(_defaultFunc); + Assert.False(_set); + } + + [Fact] + public async Task UnwrapOrTaskMaybe_NoValue_ExecutesFunction() + { + var underTest = Task.FromResult(new Maybe()); + Assert.Equal(_default, await underTest.UnwrapOr(_defaultFunc)); + Assert.True(_set); + } } } diff --git a/wimm.Secundatives/Extensions/MaybeExtensions.cs b/wimm.Secundatives/Extensions/MaybeExtensions.cs index 752f212..96f1471 100644 --- a/wimm.Secundatives/Extensions/MaybeExtensions.cs +++ b/wimm.Secundatives/Extensions/MaybeExtensions.cs @@ -82,7 +82,7 @@ public static async Task Expect(this Task> maybeTask) /// - /// Unwraps the if possible and returns a the results of a provided function otherwise + /// Unwraps the if possible and returns the results of a provided function otherwise /// /// The type of value contained with the /// The to be unwrapped @@ -103,6 +103,35 @@ public static async Task Expect(this Task> maybeTask) /// the result of awaiting the execution of public static async Task UnwrapOr(this Maybe maybe, Func> func) => maybe.Exists ? maybe.Value : await func(); + + /// + /// Unwraps the that results from the if possible and returns the results of awaiting the provided async function otherwise + /// + /// The type of value contained with the + /// A task resulting in a to be inspected + /// A function that asynchronosly returns a that will be called if + /// unwrapping of is not possible + /// The held within if it exists. Otherwise + /// the result of awaiting the execution of + public static async Task UnwrapOr(this Task> maybe, Func> func) + { + return await (await maybe).UnwrapOr(func); + } + + /// + /// Unwraps the that results from the if possible and returns the results of the provided function otherwise + /// + /// The type of value contained with the + /// A task resulting in a to be inspected + /// A function that returns a that will be called if + /// unwrapping of is not possible + /// The held within if it exists. Otherwise + /// the result of calling + public static async Task UnwrapOr(this Task> maybe, Func func) + { + return (await maybe).UnwrapOr(func); + } + /// /// An explicit conversion of class types to for ease of use and compatibility ///