Skip to content

Commit

Permalink
Merge pull request #531 from redx177/issue/528
Browse files Browse the repository at this point in the history
Added Task and ValueTask versions for AsMaybe
  • Loading branch information
vkhorikov authored Feb 24, 2024
2 parents 81405ec + b34ea7b commit 831df32
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@
<Compile Update="ResultTests\Extensions\MatchTests.ValueTask.cs">
<DependentUpon>MatchTests.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\AsMaybeTests.Task.cs">
<DependentUpon>AsMaybeTests.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\AsMaybeTests.ValueTask.cs">
<DependentUpon>AsMaybeTests.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\BindTests.Task.Right.cs">
<DependentUpon>BindTests.Task.cs</DependentUpon>
</Compile>
Expand Down
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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<double> maybeNone = none.AsMaybe();
maybeNone.HasValue.Should().Be(none.HasValue);
}

[Fact]
public void Struct_maybe_conversion_equality_some()
public void AsMaybe_Struct_maybe_conversion_equality_some()
{
double? some = 123;
Maybe<double> someMaybe = some.AsMaybe();
Expand All @@ -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<T> maybeT = null;
maybeT.HasValue.Should().BeFalse();
}

[Fact]
public void Class_maybe_conversion_some()
public void AsMaybe_Class_maybe_conversion_some()
{
Maybe<T> maybeT = T.Value.AsMaybe();
maybeT.HasValue.Should().BeTrue();
maybeT.Value.Should().Be(T.Value);
}
}
}
8 changes: 7 additions & 1 deletion CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@
<DependentUpon>GetValueOrDefault.cs</DependentUpon>
</Compile>
<Compile Update="Maybe\Extensions\Map.Task.cs">
<DependentUpon>Map.cs</DependentUpon>
<DependentUpon>Map.cs</DependentUpon>
</Compile>
<Compile Update="Maybe\Extensions\AsMaybe.Task.cs">
<DependentUpon>AsMaybe.cs</DependentUpon>
</Compile>
<Compile Update="Maybe\Extensions\AsMaybe.ValueTask.cs">
<DependentUpon>AsMaybe.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\MapIf.Task.cs">
<DependentUpon>MapIf.cs</DependentUpon>
Expand Down
33 changes: 33 additions & 0 deletions CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.Task.cs
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 CSharpFunctionalExtensions/Maybe/Extensions/AsMaybe.ValueTask.cs
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

0 comments on commit 831df32

Please sign in to comment.