-
Notifications
You must be signed in to change notification settings - Fork 0
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 #88 from J-Tech-Japan/87-needs-totask
Add Match, ToTask, ToDto
- Loading branch information
Showing
9 changed files
with
134 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace ResultBoxes.Test; | ||
|
||
public class MatchSpec | ||
{ | ||
[Fact] | ||
public void MatchTest1() | ||
{ | ||
var result = ResultBox.FromValue(2) | ||
.Match(value => value.ToString(), exception => exception.Message); | ||
Assert.Equal("2", result); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchTest2() | ||
{ | ||
var result = await ResultBox.FromValue(2).ToTask() | ||
.Match(value => value.ToString(), exception => exception.Message); | ||
Assert.Equal("2", result); | ||
} | ||
|
||
|
||
[Fact] | ||
public void MatchTest3() | ||
{ | ||
var result = ResultBox.FromException<int>(new ApplicationException("err 2")) | ||
.Match(value => value.ToString(), exception => exception.Message); | ||
Assert.Equal("err 2", result); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchTest4() | ||
{ | ||
var result = await ResultBox.FromException<int>(new ApplicationException("err 2")).ToTask() | ||
.Match(value => value.ToString(), exception => exception.Message); | ||
Assert.Equal("err 2", result); | ||
} | ||
|
||
} |
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,38 @@ | ||
namespace ResultBoxes.Test; | ||
|
||
public class ToDtoSpec | ||
{ | ||
[Fact] | ||
public void TestDto1() | ||
{ | ||
var dto = ResultBox.Start.ToDto(); | ||
|
||
Assert.True(dto.IsSuccess); | ||
} | ||
[Fact] | ||
public void TestDto2() | ||
{ | ||
var dto = ResultBox.FromValue(2).Verify(_ => new ApplicationException("test")).ToDto(); | ||
|
||
Assert.False(dto.IsSuccess); | ||
Assert.Equal("test", dto.Exception?.Message); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task TestDto3() | ||
{ | ||
var dto = await ResultBox.Start.ToTask().ToDto(); | ||
|
||
Assert.True(dto.IsSuccess); | ||
} | ||
[Fact] | ||
public async Task TestDto4() | ||
{ | ||
var dto = await ResultBox.FromValue(2).Verify(_ => new ApplicationException("test")).ToTask().ToDto(); | ||
|
||
Assert.False(dto.IsSuccess); | ||
Assert.Equal("test", dto.Exception?.Message); | ||
} | ||
|
||
} |
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,12 @@ | ||
namespace ResultBoxes.Test; | ||
|
||
public class ToTaskSpec | ||
{ | ||
[Fact] | ||
public async Task TestTask() | ||
{ | ||
var taskResult = await ResultBox.Start.ToTask(); | ||
|
||
Assert.True(taskResult.IsSuccess); | ||
} | ||
} |
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,16 @@ | ||
namespace ResultBoxes; | ||
|
||
public static class DtoExtensions | ||
{ | ||
public static ResultDto<TValue> ToDto<TValue>(this ResultBox<TValue> result) where TValue : notnull => | ||
new(result.Value, result.Exception, result.IsSuccess); | ||
public static async Task<ResultDto<TValue>> ToDto<TValue>(this Task<ResultBox<TValue>> result) where TValue : notnull => | ||
(await result).ToDto(); | ||
public static ResultBox<TValue> ToBox<TValue>(this ResultDto<TValue> result) | ||
where TValue : notnull => | ||
result is { IsSuccess: true, Value: not null } ? ResultBox.FromValue(result.Value) | ||
: ResultBox.FromException<TValue>(result.Exception ?? new ResultValueNullException()); | ||
public static async Task<ResultBox<TValue>> ToBox<TValue>(this Task<ResultDto<TValue>> result) | ||
where TValue : notnull => | ||
(await result).ToBox(); | ||
} |
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,16 @@ | ||
namespace ResultBoxes; | ||
|
||
public static class MatchExtensions | ||
{ | ||
public static TUnionResult Match<TValue, TUnionResult>(this ResultBox<TValue> result, | ||
Func<TValue, TUnionResult> successFunc, Func<Exception, TUnionResult> errorFunc) | ||
where TValue : notnull | ||
=> result.IsSuccess ? successFunc(result.GetValue()) : errorFunc(result.GetException()); | ||
|
||
public static async Task<TUnionResult> Match<TValue, TUnionResult>( | ||
this Task<ResultBox<TValue>> result, | ||
Func<TValue, TUnionResult> successFunc, | ||
Func<Exception, TUnionResult> errorFunc) | ||
where TValue : notnull | ||
=> (await result).Match(successFunc, errorFunc); | ||
} |
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
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,4 @@ | ||
namespace ResultBoxes; | ||
|
||
public record ResultDto<TValue>(TValue? Value, Exception? Exception, bool IsSuccess) | ||
where TValue : notnull; |
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,7 @@ | ||
namespace ResultBoxes; | ||
|
||
public static class ToTaskExtensions | ||
{ | ||
public static Task<ResultBox<TValue>> ToTask<TValue>(this ResultBox<TValue> result) where TValue : notnull => | ||
Task.FromResult(result); | ||
} |