From 42f1302c5dd969b2071589135814ad96d80ce783 Mon Sep 17 00:00:00 2001 From: Tomohisa Takaoka Date: Tue, 15 Oct 2024 18:56:29 -0700 Subject: [PATCH] ToResultBox --- .../ResultBoxes.Test/ToResultBoxSpecs.cs | 41 +++++++++++++++++++ src/ResultBoxes/ResultBoxes/ResultBox.cs | 2 + .../ResultBoxes/ResultBoxes.csproj | 6 +-- .../ResultBoxes/ToResultBoxExtensions.cs | 10 +++++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/ResultBoxes/ResultBoxes.Test/ToResultBoxSpecs.cs create mode 100644 src/ResultBoxes/ResultBoxes/ToResultBoxExtensions.cs diff --git a/src/ResultBoxes/ResultBoxes.Test/ToResultBoxSpecs.cs b/src/ResultBoxes/ResultBoxes.Test/ToResultBoxSpecs.cs new file mode 100644 index 0000000..60f27c3 --- /dev/null +++ b/src/ResultBoxes/ResultBoxes.Test/ToResultBoxSpecs.cs @@ -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(); + Assert.False(result.IsSuccess); + Assert.Equal(exception, result.GetException()); + } +} diff --git a/src/ResultBoxes/ResultBoxes/ResultBox.cs b/src/ResultBoxes/ResultBoxes/ResultBox.cs index ce73786..df9a748 100644 --- a/src/ResultBoxes/ResultBoxes/ResultBox.cs +++ b/src/ResultBoxes/ResultBoxes/ResultBox.cs @@ -136,6 +136,8 @@ public static ResultBox Error(Exception exception) where TValue public static ResultBox FromException(Exception exception) where TValue : notnull => new(default, exception); + public static async Task> FromException(Task exception) where TValue : notnull => + new(default, await exception); public static Task> WrapTry(Func> func, Func? exceptionMapper = null) diff --git a/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj b/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj index 378c3b4..d6db0e0 100644 --- a/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj +++ b/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj @@ -7,13 +7,13 @@ ResultBoxes preview ResultBoxes - 0.3.25 + 0.3.26 J-Tech Group J-Tech-Japan ResultBoxes - C# Results Library that focus on Railway Programming. https://github.com/J-Tech-Japan/ResultBoxes - 0.3.25 - Scan(async ()=>{}) execute order fixed + 0.3.26 + Add ToResultBox true README.md true diff --git a/src/ResultBoxes/ResultBoxes/ToResultBoxExtensions.cs b/src/ResultBoxes/ResultBoxes/ToResultBoxExtensions.cs new file mode 100644 index 0000000..12b4e03 --- /dev/null +++ b/src/ResultBoxes/ResultBoxes/ToResultBoxExtensions.cs @@ -0,0 +1,10 @@ +namespace ResultBoxes; + +public static class ToResultBoxExtensions +{ + public static Task> ToResultBox(this Task source) where T : notnull => + ResultBox.FromValue(source); + public static ResultBox ToResultBox(this T source) where T : notnull => ResultBox.FromValue(source); + + public static ResultBox ToResultBoxFromException(this Exception source) where T : notnull => ResultBox.FromException(source); +} \ No newline at end of file