Skip to content

Commit e056c4d

Browse files
Merge pull request #7 from Felix-CodingClimber/dev-felix
Removed invalid conversion from source generated ErrorResult.
2 parents 13dd65f + ccb226e commit e056c4d

File tree

7 files changed

+129
-4
lines changed

7 files changed

+129
-4
lines changed

DotNetElements.Core.ResultObject.Examples/DotNetElements.Core.ResultObject.Examples.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="DotNetElements.Core.Result" Version="0.0.4" />
12+
<PackageReference Include="DotNetElements.Core.Result" Version="0.1.3" />
1313
</ItemGroup>
1414

1515
</Project>

DotNetElements.Core.ResultObject.SourceGenerator.Tests/SnapshotResults/ErrorResultGeneratorSnapshotTests.GeneratesErrorResultCorrectly#ErrorResult.ExampleResult.g.verified.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public bool TryGetValue([NotNullWhen(true)] out TValue? value, [NotNullWhen(fals
4141
public static implicit operator ExampleResult<TValue>(ExampleResultHelper.ExampleResult result) => result.IsFail ? new(false, default!, result.GetErrorUnsafe()) : throw new ResultException("Can not convert a successful result to a result with a value");
4242

4343
// Optional conversions
44-
public ExampleResultHelper.ExampleResult AsCrudResult() => IsOk ? ExampleResultHelper.ExampleResult.Ok() : ExampleResultHelper.ExampleResult.Fail(Error);
4544
public Result AsResult() => IsOk ? Result.Ok() : Result.Fail();
4645
public Result<TValue> AsResultWithValue() => IsOk ? Result<TValue>.Ok(Value) : Result<TValue>.Fail();
4746
}

DotNetElements.Core.ResultObject.SourceGenerator/ErrorResultOutputWriter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public bool TryGetValue([NotNullWhen(true)] out TValue? value, [NotNullWhen(fals
5555
public static implicit operator ").Append(result.FullName).Append("(").Append(result.SimpleNameHelper).Append(@" result) => result.IsFail ? new(false, default!, result.GetErrorUnsafe()) : throw new ResultException(""Can not convert a successful result to a result with a value"");
5656
5757
// Optional conversions
58-
public ").Append(result.SimpleNameHelper).Append(" AsCrudResult() => IsOk ? ").Append(result.SimpleNameHelper).Append(".Ok() : ").Append(result.SimpleNameHelper).Append(@".Fail(Error);
5958
public Result AsResult() => IsOk ? Result.Ok() : Result.Fail();
6059
public Result<TValue> AsResultWithValue() => IsOk ? Result<TValue>.Ok(Value) : Result<TValue>.Fail();
6160
}

DotNetElements.Core.ResultObject.Tests/ErrorResultTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ public partial class TestErrorResult;
66
[TestClass]
77
public class ErrorResultTests
88
{
9-
// todo
9+
1010
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using static DotNetElements.Core.ResultObject.ResultHelper;
2+
3+
namespace DotNetElements.Core.ResultObject.Tests;
4+
5+
[TestClass]
6+
public class ResultHelperTests
7+
{
8+
[TestMethod]
9+
public void ResultOk_ShouldBeOk()
10+
{
11+
Result result = Ok();
12+
13+
Assert.IsTrue(result.IsOk);
14+
Assert.IsFalse(result.IsFail);
15+
}
16+
17+
[TestMethod]
18+
public void ResultFail_ShouldBeFail()
19+
{
20+
Result result = Fail();
21+
22+
Assert.IsFalse(result.IsOk);
23+
Assert.IsTrue(result.IsFail);
24+
}
25+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace DotNetElements.Core.ResultObject.Tests;
2+
3+
[TestClass]
4+
public class ResultWithErrorTests
5+
{
6+
[TestMethod]
7+
public void ResultOk_ShouldBeOk()
8+
{
9+
ResultWithError result = ResultWithError.Ok();
10+
11+
Assert.IsTrue(result.IsOk);
12+
Assert.IsFalse(result.IsFail);
13+
}
14+
15+
[TestMethod]
16+
public void ResultFail_ShouldBeFail()
17+
{
18+
ResultWithError result = ResultWithError.Fail("TestErrorMessage");
19+
20+
Assert.IsFalse(result.IsOk);
21+
Assert.IsTrue(result.IsFail);
22+
}
23+
24+
[TestMethod]
25+
public void ResultWithError_ShouldContainError()
26+
{
27+
string error = "TestErrorMessage";
28+
ResultWithError result = ResultWithError.Fail(error);
29+
30+
Assert.IsTrue(result.IsFail);
31+
Assert.AreEqual(result.GetErrorUnsafe(), error);
32+
}
33+
34+
[TestMethod]
35+
[ExpectedException(typeof(ResultOkException))]
36+
public void ResultFail_GetErrorUnsafe_ShouldThrowException()
37+
{
38+
ResultWithError result = ResultWithError.Ok();
39+
40+
result.GetErrorUnsafe();
41+
}
42+
43+
[TestMethod]
44+
public void ResultWithError_HasError_ShouldReturnTrueAndError()
45+
{
46+
string error = "TestErrorMessage";
47+
ResultWithError result = ResultWithError.Fail(error);
48+
49+
bool hasError = result.HasError(out string? resultError);
50+
51+
Assert.IsTrue(hasError);
52+
Assert.AreEqual(error, resultError);
53+
}
54+
}

DotNetElements.Core.ResultObject/Result.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,52 @@ private Result(bool isOk)
5454
public static implicit operator Result(ResultFail _) => new Result(false);
5555
}
5656

57+
public readonly struct ResultWithError
58+
{
59+
public bool IsOk { get; private init; }
60+
61+
public bool IsFail => !IsOk;
62+
63+
private readonly string? error;
64+
65+
private ResultWithError(bool isOk, string? error = null)
66+
{
67+
IsOk = isOk;
68+
this.error = error;
69+
}
70+
71+
public static ResultWithError Ok() => new(true);
72+
public static ResultWithError Fail(string error) => new(false, error);
73+
74+
public bool HasError([NotNullWhen(true)] out string? error)
75+
{
76+
error = this.error;
77+
78+
return IsFail;
79+
}
80+
81+
public string GetErrorUnsafe() => error ?? throw new ResultOkException();
82+
83+
// Optional conversions
84+
public Result AsResult => IsOk ? Result.Ok() : Result.Fail();
85+
86+
// Helper methods
87+
public static ResultWithError OkIf(bool isSuccess, string error)
88+
{
89+
return isSuccess ? Ok() : Fail(error);
90+
}
91+
92+
public static ResultWithError OkIf(Func<bool> predicate, string error)
93+
{
94+
return predicate.Invoke() ? Ok() : Fail(error);
95+
}
96+
97+
public static ResultWithError OkIfNotNull<TValue>(TValue? value, string error)
98+
{
99+
return value is not null ? Ok() : Fail(error);
100+
}
101+
}
102+
57103
public abstract class ErrorResult<TError>
58104
{
59105
public bool IsOk { get; private init; }
@@ -78,7 +124,9 @@ public bool HasError([NotNullWhen(true)] out TError? error)
78124
public TError GetErrorUnsafe() => Error ?? throw new ResultOkException();
79125
}
80126

127+
// todo check if this is needed
81128
public readonly struct ResultOk;
129+
// todo check if this is needed
82130
public readonly struct ResultFail;
83131

84132
public static partial class ResultHelper

0 commit comments

Comments
 (0)