Skip to content

Commit

Permalink
Merge pull request #88 from J-Tech-Japan/87-needs-totask
Browse files Browse the repository at this point in the history
Add Match, ToTask, ToDto
  • Loading branch information
tomohisa authored Jun 15, 2024
2 parents 0cc777f + f1be826 commit 75dc18e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/ResultBoxes/ResultBoxes.Test/MatchSpec.cs
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);
}

}
38 changes: 38 additions & 0 deletions src/ResultBoxes/ResultBoxes.Test/ToDtoSpec.cs
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);
}

}
12 changes: 12 additions & 0 deletions src/ResultBoxes/ResultBoxes.Test/ToTaskSpec.cs
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);
}
}
16 changes: 16 additions & 0 deletions src/ResultBoxes/ResultBoxes/DtoExtensions.cs
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();
}
16 changes: 16 additions & 0 deletions src/ResultBoxes/ResultBoxes/MatchExtensions.cs
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);
}
2 changes: 1 addition & 1 deletion src/ResultBoxes/ResultBoxes/ResultBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ public static async Task<ResultBox<TValue>> CheckNull<TValue>(Func<Task<TValue?>
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};
}
}
4 changes: 2 additions & 2 deletions src/ResultBoxes/ResultBoxes/ResultBoxes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<RootNamespace>ResultBoxes</RootNamespace>
<LangVersion>preview</LangVersion>
<PackageId>ResultBoxes</PackageId>
<Version>0.3.14</Version>
<Version>0.3.15</Version>
<Authors>J-Tech Group</Authors>
<Company>J-Tech-Japan</Company>
<PackageDescription>ResultBoxes - C# Results Library that focus on Railway Programming.</PackageDescription>
<RepositoryUrl>https://github.com/J-Tech-Japan/ResultBoxes</RepositoryUrl>
<PackageVersion>0.3.14</PackageVersion>
<PackageVersion>0.3.15</PackageVersion>
<Description>Add Verify TwoValues and DoWrapTry</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
4 changes: 4 additions & 0 deletions src/ResultBoxes/ResultBoxes/ResultDto.cs
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;
7 changes: 7 additions & 0 deletions src/ResultBoxes/ResultBoxes/ToTaskExtensions.cs
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);
}

0 comments on commit 75dc18e

Please sign in to comment.