Skip to content

Commit 81dd1bf

Browse files
committed
feat(result): refactor result handling to use private fields
- Replaces public properties with private fields in TryPickResult and TryPickProblem methods for improved encapsulation. - Adds a new test case for handling null values in TypedResult, ensuring correct behavior when results are null.
1 parent c8998e2 commit 81dd1bf

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/ES.FX/Results/Result.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public Result(Problem problem)
103103
/// </summary>
104104
public bool TryPickResult([NotNullWhen(true)] out T? result, [NotNullWhen(false)] out Problem? problem)
105105
{
106-
result = IsResult ? AsResult : default;
107-
problem = IsProblem ? AsProblem : null;
106+
result = IsResult ? _result : default;
107+
problem = IsProblem ? _problem : null;
108108
return IsResult;
109109
}
110110

@@ -119,8 +119,8 @@ public bool TryPickResult([NotNullWhen(true)] out T? result, [NotNullWhen(false)
119119
/// </summary>
120120
public bool TryPickProblem([NotNullWhen(true)] out Problem? problem, [NotNullWhen(false)] out T? result)
121121
{
122-
result = IsResult ? AsResult : default;
123-
problem = IsProblem ? AsProblem : null;
122+
result = IsResult ? _result : default;
123+
problem = IsProblem ? _problem : null;
124124
return IsProblem;
125125
}
126126

tests/ES.FX.Tests/Results/ResultsTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ namespace ES.FX.Tests.Results;
55

66
public class ResultsTests
77
{
8+
9+
[Fact]
10+
public void TypedResult_CanBe_Null()
11+
{
12+
var result = TypedResultAsNull();
13+
Assert.True(result.IsResult);
14+
15+
if(result.TryPickResult(out var resultValue))
16+
{
17+
Assert.Null(resultValue);
18+
}
19+
20+
if (!result.TryPickProblem(out var problemValue))
21+
{
22+
Assert.Null(problemValue);
23+
}
24+
}
25+
826
[Fact]
927
public void Result_CanBe_Success()
1028
{
@@ -103,4 +121,7 @@ public void Results_CanBeEqualityChecked()
103121

104122
private static Result<string> TypedResultAsProblem() => new Problem();
105123
private static Result<string> TypedResultAsValue() => string.Empty;
124+
private static Result<object?> TypedResultAsNull() => (object?)null;
125+
126+
106127
}

0 commit comments

Comments
 (0)