From b34ea7b9c3af497f655103947a4887dca8218f53 Mon Sep 17 00:00:00 2001 From: Simon Lang Date: Thu, 22 Feb 2024 13:54:17 +0100 Subject: [PATCH] Added Task and ValueTask versions for AsMaybe --- .../CSharpFunctionalExtensions.Tests.csproj | 6 +++ .../Extensions/AsMaybeTests.Task.cs | 41 ++++++++++++++++++ .../Extensions/AsMaybeTests.ValueTask.cs | 42 +++++++++++++++++++ .../MaybeTests/Extensions/AsMaybeTests.cs | 10 ++--- .../CSharpFunctionalExtensions.csproj | 8 +++- .../Maybe/Extensions/AsMaybe.Task.cs | 33 +++++++++++++++ .../Maybe/Extensions/AsMaybe.ValueTask.cs | 35 ++++++++++++++++ 7 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.Task.cs create mode 100644 CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.ValueTask.cs create mode 100644 CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.Task.cs create mode 100644 CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.ValueTask.cs diff --git a/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj b/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj index 2728130d..6669b138 100644 --- a/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj +++ b/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj @@ -397,6 +397,12 @@ MatchTests.cs + + AsMaybeTests.cs + + + AsMaybeTests.cs + BindTests.Task.cs diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.Task.cs new file mode 100644 index 00000000..99d9124b --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.Task.cs @@ -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.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); + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.ValueTask.cs new file mode 100644 index 00000000..5b6161cf --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.ValueTask.cs @@ -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.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); + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.cs index 18d20f46..1088149c 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/AsMaybeTests.cs @@ -7,7 +7,7 @@ namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions; public class AsMaybeTests : TestBase { [Fact] - public void Struct_maybe_conversion_equality_none() + public void AsMaybe_Struct_maybe_conversion_equality_none() { double? none = default; Maybe maybeNone = none.AsMaybe(); @@ -15,7 +15,7 @@ public void Struct_maybe_conversion_equality_none() } [Fact] - public void Struct_maybe_conversion_equality_some() + public void AsMaybe_Struct_maybe_conversion_equality_some() { double? some = 123; Maybe someMaybe = some.AsMaybe(); @@ -24,17 +24,17 @@ public void Struct_maybe_conversion_equality_some() } [Fact] - public void Class_maybe_conversion_none() + public void AsMaybe_Class_maybe_conversion_none() { Maybe maybeT = null; maybeT.HasValue.Should().BeFalse(); } [Fact] - public void Class_maybe_conversion_some() + public void AsMaybe_Class_maybe_conversion_some() { Maybe maybeT = T.Value.AsMaybe(); maybeT.HasValue.Should().BeTrue(); maybeT.Value.Should().Be(T.Value); } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj b/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj index 93a0b638..89f2249b 100644 --- a/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj +++ b/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj @@ -118,7 +118,13 @@ GetValueOrDefault.cs - Map.cs + Map.cs + + + AsMaybe.cs + + + AsMaybe.cs MapIf.cs diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.Task.cs new file mode 100644 index 00000000..a845df12 --- /dev/null +++ b/CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.Task.cs @@ -0,0 +1,33 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + // ReSharper disable ConvertNullableToShortForm + /// + /// Converts the struct to a . + /// + /// Returns the equivalent to the . + public static async Task> AsMaybe(this Task> nullableTask) + where T : struct + { + var nullable = await nullableTask.DefaultAwait(); + return nullable.AsMaybe(); + } + +#nullable enable + /// + /// Wraps the class instance in a . + /// + /// Returns if the class instance is null, otherwise returns . + public static async Task> AsMaybe(this Task nullableTask) + where T : class + { + var nullable = await nullableTask.DefaultAwait(); + return nullable.AsMaybe(); + } +#nullable restore + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.ValueTask.cs new file mode 100644 index 00000000..30690881 --- /dev/null +++ b/CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.ValueTask.cs @@ -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 + /// + /// Converts the struct to a . + /// + /// Returns the equivalent to the . + public static async ValueTask> AsMaybe(this ValueTask> nullableTask) + where T : struct + { + var nullable = await nullableTask; + return nullable.AsMaybe(); + } + +#nullable enable + /// + /// Wraps the class instance in a . + /// + /// Returns if the class instance is null, otherwise returns . + public static async ValueTask> AsMaybe(this ValueTask nullableTask) + where T : class + { + var nullable = await nullableTask; + return nullable.AsMaybe(); + } +#nullable restore + } +} +#endif