-
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 #560 from DerStimmler/feat/of
Add `Result.Of` methods
- Loading branch information
Showing
3 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
CSharpFunctionalExtensions.Tests/ResultTests/Methods/OfTests.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,105 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Methods; | ||
|
||
public class OfTests | ||
{ | ||
[Fact] | ||
public void Of_can_create_ResultT_from_value() | ||
{ | ||
string value = "value"; | ||
|
||
Result<string> result = Result.Of(value); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public void Of_can_create_ResultT_from_func() | ||
{ | ||
string value = "value"; | ||
Func<string> func = () => value; | ||
|
||
Result<string> result = Result.Of(func); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public async Task Of_can_create_ResultT_from_valueTask() | ||
{ | ||
string value = "value"; | ||
Task<string> valueTask = Task.FromResult(value); | ||
|
||
Result<string> result = await Result.Of(valueTask); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public async Task Of_can_create_ResultT_from_valueTaskFunc() | ||
{ | ||
string value = "value"; | ||
Task<string> valueTask = Task.FromResult(value); | ||
Func<Task<string>> valueTaskFunc = () => valueTask; | ||
|
||
Result<string> result = await Result.Of(valueTaskFunc); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public void Of_can_create_ResultTE_from_value() | ||
{ | ||
string value = "value"; | ||
|
||
Result<string, object> result = Result.Of<string, object>(value); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public void Of_can_create_ResultTE_from_func() | ||
{ | ||
string value = "value"; | ||
Func<string> func = () => value; | ||
|
||
Result<string, object> result = Result.Of<string, object>(func); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public async Task Of_can_create_ResultTE_from_valueTask() | ||
{ | ||
string value = "value"; | ||
Task<string> valueTask = Task.FromResult(value); | ||
|
||
Result<string, object> result = await Result.Of<string, object>(valueTask); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(value); | ||
} | ||
|
||
[Fact] | ||
public async Task Of_can_create_ResultTE_from_valueTaskFunc() | ||
{ | ||
string value = "value"; | ||
Task<string> valueTask = Task.FromResult(value); | ||
Func<Task<string>> valueTaskFunc = () => valueTask; | ||
|
||
Result<string, object> result = await Result.Of<string, object>(valueTaskFunc); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
public partial struct Result | ||
{ | ||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given value. | ||
/// </summary> | ||
public static Result<T> Of<T>(T value) where T : notnull | ||
{ | ||
return new Result<T>(false, default, value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given value from a <see cref="Func{T}" />. | ||
/// </summary> | ||
public static Result<T> Of<T>(Func<T> func) where T : notnull | ||
{ | ||
T value = func(); | ||
|
||
return new Result<T>(false, default, value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given async value. | ||
/// </summary> | ||
public static async Task<Result<T>> Of<T>(Task<T> valueTask) where T : notnull | ||
{ | ||
T value = await valueTask; | ||
|
||
return new Result<T>(false, default, value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given value from an async <see cref="Func{T}" />. | ||
/// </summary> | ||
public static async Task<Result<T>> Of<T>(Func<Task<T>> valueTaskFunc) where T : notnull | ||
{ | ||
T value = await valueTaskFunc(); | ||
|
||
return new Result<T>(false, default, value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given value. | ||
/// </summary> | ||
public static Result<T, E> Of<T, E>(T value) where T : notnull | ||
{ | ||
return new Result<T, E>(false, default, value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given value from a <see cref="Func{T}" />. | ||
/// </summary> | ||
public static Result<T, E> Of<T, E>(Func<T> func) where T : notnull | ||
{ | ||
T value = func(); | ||
|
||
return new Result<T, E>(false, default, value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given async value. | ||
/// </summary> | ||
public static async Task<Result<T, E>> Of<T, E>(Task<T> valueTask) where T : notnull | ||
{ | ||
T value = await valueTask; | ||
|
||
return new Result<T, E>(false, default, value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a successful <see cref="Result{T}" /> containing the given value from an async <see cref="Func{T}" />. | ||
/// </summary> | ||
public static async Task<Result<T, E>> Of<T, E>(Func<Task<T>> valueTaskFunc) where T : notnull | ||
{ | ||
T value = await valueTaskFunc(); | ||
|
||
return new Result<T, E>(false, default, 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