Skip to content

Commit

Permalink
feat(Extension): Result supports Nullable Value and Error
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBogomips committed Dec 13, 2024
1 parent 745ea77 commit 8243938
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sample/ConsoleApp/Pipelines/CreateUserPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ private static Maybe<User> LookupUser(Username username)

private static Result<User> CreateUser(User user)
{
#pragma warning disable CS0162 // Unreachable code detected
if (false) return Result.Failure<User>("Error creating user");
#pragma warning restore CS0162 // Unreachable code detected
return new(user);
}
private static void NotifyCreation(User user)
Expand Down
11 changes: 9 additions & 2 deletions src/Monads/Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,15 @@ public static async Task<Result<TValue>> Execute<TValue>(Func<Task<TValue>> func
/// <typeparam name="TValue"></typeparam>
public readonly struct Result<TValue> : IResult<TValue>, IEquatable<Result<TValue>>, IEnumerable<TValue>
{
internal readonly TValue? Value;
internal readonly Error? Error;
/// <summary>
/// The value of the <see cref="Result{TValue}"/> if the <see cref="IsSuccess"/> is <c>true</c>.
/// </summary>
public TValue? Value { get; }

/// <summary>
/// The error of the <see cref="Result{TValue}"/> if the <see cref="IsFailure"/> is <c>true</c>.
/// </summary>
public Error? Error { get; }

/// <summary>
/// Initializes a successful instance of the <see cref="Result{TValue}"/> with the given <paramref name="value"/>.
Expand Down
31 changes: 31 additions & 0 deletions test/Monads.UnitTests/ResultTests/ResultValueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Bogoware.Monads.UnitTests.ResultTests;

public class ResultValueTests
{
[Fact]
public void Success_returns_itsValue_andNullError()
{
var sut = Result.Success(new Value(666));

sut.Value.Should().NotBeNull();
sut.Error.Should().BeNull();
}

[Fact]
public void Failure_returns_nullValue_andItsError()
{
var sut = Result.Failure<Value>("Error");

sut.Value.Should().BeNull();
sut.Error.Should().NotBeNull();
}

[Fact]
public void Failure_returns_defaultValue_andItsError()
{
var sut = Result.Failure<Guid>("Error");

sut.Value.Should().Be(default(Guid));
sut.Error.Should().NotBeNull();
}
}

0 comments on commit 8243938

Please sign in to comment.