Skip to content

Commit

Permalink
Merge pull request #133 from J-Tech-Japan/132-typed-objectstoresultbo…
Browse files Browse the repository at this point in the history
…x-might-be-useful

ToResultBox
  • Loading branch information
tomohisa authored Oct 16, 2024
2 parents 42bdc99 + 42f1302 commit 9c0be5f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
41 changes: 41 additions & 0 deletions src/ResultBoxes/ResultBoxes.Test/ToResultBoxSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace ResultBoxes.Test;

public class ToResultBoxSpecs
{
[Fact]
public void ToResultBoxFromPrimitive()
{
var result = 1.ToResultBox();
Assert.True(result.IsSuccess);
Assert.Equal(1, result.GetValue());
}
[Fact]
public void ToResultBoxFromString()
{
var result = "Hello".ToResultBox();
Assert.True(result.IsSuccess);
Assert.Equal("Hello", result.GetValue());
}
[Fact]
public async Task ToResultBoxFromPrimitiveTask()
{
var result = Task.FromResult(1).ToResultBox();
Assert.True((await result).IsSuccess);
Assert.Equal(1, (await result).GetValue());
}
[Fact]
public async Task ToResultBoxFromStringTask()
{
var result = Task.FromResult("Hello").ToResultBox();
Assert.True((await result).IsSuccess);
Assert.Equal("Hello", (await result).GetValue());
}
[Fact]
public void ToResultBoxFromException()
{
var exception = new ApplicationException("Can not execute");
var result = exception.ToResultBoxFromException<int>();
Assert.False(result.IsSuccess);
Assert.Equal(exception, result.GetException());
}
}
2 changes: 2 additions & 0 deletions src/ResultBoxes/ResultBoxes/ResultBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public static ResultBox<TValue> Error<TValue>(Exception exception) where TValue

public static ResultBox<TValue> FromException<TValue>(Exception exception) where TValue : notnull =>
new(default, exception);
public static async Task<ResultBox<TValue>> FromException<TValue>(Task<Exception> exception) where TValue : notnull =>
new(default, await exception);

public static Task<ResultBox<TValueResult>> WrapTry<TValueResult>(Func<Task<TValueResult>> func,
Func<Exception, Exception>? exceptionMapper = null)
Expand Down
6 changes: 3 additions & 3 deletions src/ResultBoxes/ResultBoxes/ResultBoxes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<RootNamespace>ResultBoxes</RootNamespace>
<LangVersion>preview</LangVersion>
<PackageId>ResultBoxes</PackageId>
<Version>0.3.25</Version>
<Version>0.3.26</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.25</PackageVersion>
<Description>Scan(async ()=>{}) execute order fixed</Description>
<PackageVersion>0.3.26</PackageVersion>
<Description>Add ToResultBox</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
10 changes: 10 additions & 0 deletions src/ResultBoxes/ResultBoxes/ToResultBoxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ResultBoxes;

public static class ToResultBoxExtensions
{
public static Task<ResultBox<T>> ToResultBox<T>(this Task<T> source) where T : notnull =>
ResultBox.FromValue(source);
public static ResultBox<T> ToResultBox<T>(this T source) where T : notnull => ResultBox.FromValue(source);

public static ResultBox<T> ToResultBoxFromException<T>(this Exception source) where T : notnull => ResultBox.FromException<T>(source);
}

0 comments on commit 9c0be5f

Please sign in to comment.