-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #531 from redx177/issue/528
Added Task and ValueTask versions for AsMaybe
- Loading branch information
Showing
7 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.Task.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Threading.Tasks; | ||
using CSharpFunctionalExtensions.Tests.ResultTests; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions; | ||
|
||
public class AsMaybeTests_Task : TestBase | ||
{ | ||
[Fact] | ||
public async Task AsMaybe_Task_Struct_maybe_conversion_equality_none() | ||
{ | ||
double? none = default; | ||
var maybeNone = await none.AsTask().AsMaybe(); | ||
maybeNone.HasValue.Should().Be(none.HasValue); | ||
} | ||
|
||
[Fact] | ||
public async Task AsMaybe_Task_Struct_maybe_conversion_equality_some() | ||
{ | ||
double? some = 123; | ||
var someMaybe = await some.AsTask().AsMaybe(); | ||
someMaybe.HasValue.Should().Be(some.HasValue); | ||
someMaybe.Value.Should().Be(some); | ||
} | ||
|
||
[Fact] | ||
public async Task AsMaybe_Task_Class_maybe_conversion_none() | ||
{ | ||
var maybeT = await Maybe<T>.None.AsTask(); | ||
maybeT.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task AsMaybe_Task_Class_maybe_conversion_some() | ||
{ | ||
var maybeT = await T.Value.AsMaybe().AsTask(); | ||
maybeT.HasValue.Should().BeTrue(); | ||
maybeT.Value.Should().Be(T.Value); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.ValueTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Threading.Tasks; | ||
using CSharpFunctionalExtensions.Tests.ResultTests; | ||
using CSharpFunctionalExtensions.ValueTasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions; | ||
|
||
public class AsMaybeTests_ValueTask : TestBase | ||
{ | ||
[Fact] | ||
public async Task AsMaybe_ValueTask_Struct_maybe_conversion_equality_none() | ||
{ | ||
double? none = default; | ||
var maybeNone = await none.AsValueTask().AsMaybe(); | ||
maybeNone.HasValue.Should().Be(none.HasValue); | ||
} | ||
|
||
[Fact] | ||
public async Task AsMaybe_ValueTask_Struct_maybe_conversion_equality_some() | ||
{ | ||
double? some = 123; | ||
var someMaybe = await some.AsValueTask().AsMaybe(); | ||
someMaybe.HasValue.Should().Be(some.HasValue); | ||
someMaybe.Value.Should().Be(some); | ||
} | ||
|
||
[Fact] | ||
public async Task AsMaybe_ValueTask_Class_maybe_conversion_none() | ||
{ | ||
var maybeT = await Maybe<T>.None.AsValueTask(); | ||
maybeT.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task AsMaybe_ValueTask_Class_maybe_conversion_some() | ||
{ | ||
var maybeT = await T.Value.AsValueTask().AsMaybe(); | ||
maybeT.HasValue.Should().BeTrue(); | ||
maybeT.Value.Should().Be(T.Value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.Task.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
public static partial class MaybeExtensions | ||
{ | ||
// ReSharper disable ConvertNullableToShortForm | ||
/// <summary> | ||
/// Converts the <see cref="Nullable"/> struct to a <see cref="Maybe{T}"/>. | ||
/// </summary> | ||
/// <returns>Returns the <see cref="Maybe{T}"/> equivalent to the <see cref="Nullable{T}"/>.</returns> | ||
public static async Task<Maybe<T>> AsMaybe<T>(this Task<Nullable<T>> nullableTask) | ||
where T : struct | ||
{ | ||
var nullable = await nullableTask.DefaultAwait(); | ||
return nullable.AsMaybe(); | ||
} | ||
|
||
#nullable enable | ||
/// <summary> | ||
/// Wraps the class instance in a <see cref="Maybe{T}"/>. | ||
/// </summary> | ||
/// <returns>Returns <see cref="Maybe.None"/> if the class instance is null, otherwise returns <see cref="Maybe.From{T}(T)"/>.</returns> | ||
public static async Task<Maybe<T>> AsMaybe<T>(this Task<T?> nullableTask) | ||
where T : class | ||
{ | ||
var nullable = await nullableTask.DefaultAwait(); | ||
return nullable.AsMaybe(); | ||
} | ||
#nullable restore | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.ValueTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#if NET5_0_OR_GREATER | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions.ValueTasks | ||
{ | ||
public static partial class MaybeExtensions | ||
{ | ||
// ReSharper disable ConvertNullableToShortForm | ||
/// <summary> | ||
/// Converts the <see cref="Nullable"/> struct to a <see cref="Maybe{T}"/>. | ||
/// </summary> | ||
/// <returns>Returns the <see cref="Maybe{T}"/> equivalent to the <see cref="Nullable{T}"/>.</returns> | ||
public static async ValueTask<Maybe<T>> AsMaybe<T>(this ValueTask<Nullable<T>> nullableTask) | ||
where T : struct | ||
{ | ||
var nullable = await nullableTask; | ||
return nullable.AsMaybe(); | ||
} | ||
|
||
#nullable enable | ||
/// <summary> | ||
/// Wraps the class instance in a <see cref="Maybe{T}"/>. | ||
/// </summary> | ||
/// <returns>Returns <see cref="Maybe.None"/> if the class instance is null, otherwise returns <see cref="Maybe.From{T}(T)"/>.</returns> | ||
public static async ValueTask<Maybe<T>> AsMaybe<T>(this ValueTask<T?> nullableTask) | ||
where T : class | ||
{ | ||
var nullable = await nullableTask; | ||
return nullable.AsMaybe(); | ||
} | ||
#nullable restore | ||
} | ||
} | ||
#endif |