Skip to content

Commit

Permalink
added static methods for abstractions (from error and from value), ad…
Browse files Browse the repository at this point in the history
…ded unit tests
  • Loading branch information
skrasekmichael committed Apr 16, 2024
1 parent 43d9d3f commit 1d60f24
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/RailwayResult/IResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ public interface IResult
public bool IsSuccess { get; }
public bool IsFailure { get; }
public Error Error { get; }

public static abstract IResult FromError<TError>(TError error) where TError : Error;
}

public interface IResult<TValue> : IResult
{
public TValue Value { get; }

public static abstract IResult<TValue> FromValue(TValue value);
}
12 changes: 8 additions & 4 deletions src/RailwayResult/Result.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ namespace RailwayResult;

public sealed class Result<TValue> : IResult<TValue>
{
public bool IsSuccess { get; }
public bool IsSuccess => _error is null;

public bool IsFailure => !IsSuccess;

private readonly TValue? _value;

public TValue Value => IsSuccess ? _value! : throw new AccessingValueOfFailureResultException();

private readonly Error? _error = null;

public Error Error => IsFailure ? _error! : throw new AccessingErrorOfSuccessResultException();

private Result(Error error)
public Result(Error error)
{
IsSuccess = false;
_error = error;
_value = default;
}

private Result(TValue? value)
{
IsSuccess = true;
_error = null;
_value = value;
}
Expand All @@ -43,6 +44,9 @@ public override string ToString()

public static Result<TValue> Success(TValue value) => new(value);

public static IResult<TValue> FromValue(TValue value) => new Result<TValue>(value);
public static IResult FromError<TError>(TError error) where TError : Error => new Result<TValue>(error);

public static implicit operator Result<TValue>(TValue? value) => new(value);
public static implicit operator Result<TValue>(Error error) => new(error);
}
6 changes: 5 additions & 1 deletion src/RailwayResult/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ namespace RailwayResult;
public sealed class Result : IResult
{
public bool IsSuccess { get; }

public bool IsFailure => !IsSuccess;

private readonly Error? _error = null;

public Error Error => IsFailure ? _error! : throw new AccessingErrorOfSuccessResultException();

public Result(Error error)
{
IsSuccess = false;
IsSuccess = error is null;
_error = error;
}

Expand All @@ -36,6 +38,8 @@ public override string ToString()
};
}

public static IResult FromError<TError>(TError error) where TError : Error => new Result(error);

public static readonly Result Success = new();

public static implicit operator Result(Error error) => new(error);
Expand Down
46 changes: 46 additions & 0 deletions tests/RailwayResult.Tests/ResultCreation_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace RailwayResult.Tests;

public sealed class ResultCreation_Tests
{
[Fact]
public void Result_FromError_Should_ReturnSameResult_AsCtor()
{
//arrange
var expectedResult = new Result(GenericError.ErrorA);

//act
var result = Result.FromError(GenericError.ErrorA);

//assert
result.IsSuccess.Should().Be(expectedResult.IsSuccess);
result.Error.Should().BeEquivalentTo(expectedResult.Error);
}

[Fact]
public void GenericResult_FromError_Should_ReturnSameResult_AsCtor()
{
//arrange
var expectedResult = new Result<string>(GenericError.ErrorA);

//act
var result = Result<string>.FromError(GenericError.ErrorA);

//assert
result.IsSuccess.Should().Be(expectedResult.IsSuccess);
result.Error.Should().BeEquivalentTo(expectedResult.Error);
}

[Fact]
public void GenericResult_FromValue_Should_ReturnSameResult_AsCtor()
{
//arrange
var expectedResult = Result<string>.Success("100");

//act
var result = Result<string>.FromValue("100");

//assert
result.IsSuccess.Should().Be(expectedResult.IsSuccess);
result.Value.Should().BeEquivalentTo(expectedResult.Value);
}
}

0 comments on commit 1d60f24

Please sign in to comment.