-
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 #563 from marcinjahn/add-maybe-match-async
Add async overloads of Maybe.Match
- Loading branch information
Showing
4 changed files
with
412 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.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,133 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CSharpFunctionalExtensions.Tests.ResultTests.Extensions; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions; | ||
|
||
public class MatchTests_Task : MatchTestsBase | ||
{ | ||
[Fact] | ||
public async Task Match_follows_some_branch_where_there_is_a_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (value, ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
value.Should().Be(T.Value); | ||
|
||
return Task.CompletedTask; | ||
}, | ||
None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_follows_none_branch_where_is_no_value() | ||
{ | ||
Maybe<T> maybe = null; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (_, __) => | ||
throw new FieldAccessException("Accessed Some path while maybe has no value"), | ||
None: (ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
return Task.CompletedTask; | ||
}, | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_no_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.From( | ||
new KeyValuePair<int, string>(42, "Matrix") | ||
); | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (intValue, stringValue, ct) => | ||
{ | ||
intValue.Should().Be(42); | ||
stringValue.Should().Be("Matrix"); | ||
|
||
return Task.CompletedTask; | ||
}, | ||
None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_no_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.None; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (_, __, ___) => | ||
throw new FieldAccessException("Accessed Some path while maybe has no value"), | ||
None: (ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
return Task.CompletedTask; | ||
}, | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.From( | ||
new KeyValuePair<int, string>(42, "Matrix") | ||
); | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
var returnValue = await maybe.Match( | ||
Some: (intValue, stringValue, ct) => | ||
{ | ||
intValue.Should().Be(42); | ||
stringValue.Should().Be("Matrix"); | ||
|
||
return Task.FromResult(intValue); | ||
}, | ||
None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), | ||
cancellationToken | ||
); | ||
|
||
42.Should().Be(returnValue); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_a_return_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.None; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
var returnValue = await maybe.Match( | ||
Some: (_, __, ___) => | ||
throw new FieldAccessException("Accessed Some path while maybe has no value"), | ||
None: (ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
return Task.FromResult(-1); | ||
}, | ||
cancellationToken | ||
); | ||
|
||
(-1).Should().Be(returnValue); | ||
} | ||
|
||
private static CancellationToken GetCancellationToken() => new(canceled: true); | ||
} |
133 changes: 133 additions & 0 deletions
133
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.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,133 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CSharpFunctionalExtensions.Tests.ResultTests.Extensions; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions; | ||
|
||
public class MatchTests_ValueTask : MatchTestsBase | ||
{ | ||
[Fact] | ||
public async Task Match_follows_some_branch_where_there_is_a_value() | ||
{ | ||
Maybe<T> maybe = T.Value; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (value, ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
value.Should().Be(T.Value); | ||
|
||
return ValueTask.CompletedTask; | ||
}, | ||
None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_follows_none_branch_where_is_no_value() | ||
{ | ||
Maybe<T> maybe = null; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (_, __) => | ||
throw new FieldAccessException("Accessed Some path while maybe has no value"), | ||
None: (ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
return ValueTask.CompletedTask; | ||
}, | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_no_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.From( | ||
new KeyValuePair<int, string>(42, "Matrix") | ||
); | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (intValue, stringValue, ct) => | ||
{ | ||
intValue.Should().Be(42); | ||
stringValue.Should().Be("Matrix"); | ||
|
||
return ValueTask.CompletedTask; | ||
}, | ||
None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_no_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.None; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
await maybe.Match( | ||
Some: (_, __, ___) => | ||
throw new FieldAccessException("Accessed Some path while maybe has no value"), | ||
None: (ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
return ValueTask.CompletedTask; | ||
}, | ||
cancellationToken | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.From( | ||
new KeyValuePair<int, string>(42, "Matrix") | ||
); | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
var returnValue = await maybe.Match( | ||
Some: (intValue, stringValue, ct) => | ||
{ | ||
intValue.Should().Be(42); | ||
stringValue.Should().Be("Matrix"); | ||
|
||
return ValueTask.FromResult(intValue); | ||
}, | ||
None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), | ||
cancellationToken | ||
); | ||
|
||
42.Should().Be(returnValue); | ||
} | ||
|
||
[Fact] | ||
public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_a_return_value() | ||
{ | ||
Maybe<KeyValuePair<int, string>> maybe = Maybe<KeyValuePair<int, string>>.None; | ||
var cancellationToken = GetCancellationToken(); | ||
|
||
var returnValue = await maybe.Match( | ||
Some: (_, __, ___) => | ||
throw new FieldAccessException("Accessed Some path while maybe has no value"), | ||
None: (ct) => | ||
{ | ||
ct.Should().Be(cancellationToken); | ||
return ValueTask.FromResult(-1); | ||
}, | ||
cancellationToken | ||
); | ||
|
||
(-1).Should().Be(returnValue); | ||
} | ||
|
||
private static CancellationToken GetCancellationToken() => new(canceled: true); | ||
} |
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,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
public static partial class MaybeExtensions | ||
{ | ||
public static async Task<TE> Match<TE, T>( | ||
this Maybe<T> maybe, | ||
Func<T, CancellationToken, Task<TE>> Some, | ||
Func<CancellationToken, Task<TE>> None, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
return maybe.HasValue | ||
? await Some(maybe.GetValueOrThrow(), cancellationToken) | ||
: await None(cancellationToken); | ||
} | ||
|
||
public static async Task Match<T>( | ||
this Maybe<T> maybe, | ||
Func<T, CancellationToken, Task> Some, | ||
Func<CancellationToken, Task> None, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
if (maybe.HasValue) | ||
await Some(maybe.GetValueOrThrow(), cancellationToken); | ||
else | ||
await None(cancellationToken); | ||
} | ||
|
||
public static async Task<TE> Match<TE, TKey, TValue>( | ||
this Maybe<KeyValuePair<TKey, TValue>> maybe, | ||
Func<TKey, TValue, CancellationToken, Task<TE>> Some, | ||
Func<CancellationToken, Task<TE>> None, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
return maybe.HasValue | ||
? await Some.Invoke( | ||
maybe.GetValueOrThrow().Key, | ||
maybe.GetValueOrThrow().Value, | ||
cancellationToken | ||
) | ||
: await None.Invoke(cancellationToken); | ||
} | ||
|
||
public static async Task Match<TKey, TValue>( | ||
this Maybe<KeyValuePair<TKey, TValue>> maybe, | ||
Func<TKey, TValue, CancellationToken, Task> Some, | ||
Func<CancellationToken, Task> None, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
if (maybe.HasValue) | ||
{ | ||
await Some.Invoke( | ||
maybe.GetValueOrThrow().Key, | ||
maybe.GetValueOrThrow().Value, | ||
cancellationToken | ||
); | ||
} | ||
else | ||
{ | ||
await None.Invoke(cancellationToken); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.