Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Jan 26, 2024
1 parent 3cabd88 commit 24d4912
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 56 deletions.
10 changes: 10 additions & 0 deletions src/BuildingBlocks/Common/Types/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public static Result<T> Failure<T>(Exception exception)
}

public static readonly Result<Unit> UnitResult = new Success<Unit>(Unit.Value);

public static Result<B> ToError<A, B>(this Result<A> res)
{
if (res.IsSuccess)
{
throw new ValueIsSuccessException<A>(res.Value);
}

return Failure<B>(res.ErrorValue);
}
}

public abstract class Result<T>
Expand Down
66 changes: 10 additions & 56 deletions tests/BuildingBlocks/Common.Tests/Types/ResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void TestResultErValueIfIsResultIsError(Exception error)
{
var result = Result.Failure<string>(error);

var ex = Assert.Throws<ValueIsErrorException>(() => result.Value());
var ex = Assert.Throws<ValueIsErrorException>(() => result.Value);
Assert.NotNull(ex);
Assert.Equivalent(error, ex.InnerException);
}
Expand All @@ -72,9 +72,9 @@ public void TestResultToErrorIfResultIsError(Exception error)
{
var result = Result.Failure<string>(error);

var ex = result.ToError<int>();
var ex = result.ToError<string, int>();
Assert.False(ex.IsSuccess);
Assert.Equivalent(error, ex.ErrorValue());
Assert.Equivalent(error, ex.ErrorValue);
}

[Theory]
Expand All @@ -83,7 +83,7 @@ public void TestResultToErrorIfResultIsSuccess(string value)
{
var result = Result.Ok(value);

var ex = Assert.Throws<ValueIsSuccessException<string>>(() => result.ToError<int>());
var ex = Assert.Throws<ValueIsSuccessException<string>>(() => result.ToError<string, int>());
Assert.NotNull(ex);
Assert.Equal(value, ex.CurrentValue);
}
Expand All @@ -96,7 +96,7 @@ public void TestResultMapIfResultIsError(Exception error)

var ex = result.Map((x) => x + 10);
Assert.False(ex.IsSuccess);
Assert.Equivalent(error, ex.ErrorValue());
Assert.Equivalent(error, ex.ErrorValue);
}

[Theory]
Expand All @@ -107,7 +107,7 @@ public void TestResultMapIfResultIsSuccess(int value, int newValue)

var subject = result.Map(_ => newValue);
Assert.NotNull(subject);
Assert.Equal(newValue, subject.Value());
Assert.Equal(newValue, subject.Value);
}

[Theory]
Expand All @@ -116,9 +116,9 @@ public void TestResultMapWithClosureDependencyIfResultIsError(Exception error, i
{
var result = Result.Failure<int>(error);

var ex = result.Map((x, val) => x + val, newValue);
var ex = result.Map((x) => x + newValue);
Assert.False(ex.IsSuccess);
Assert.Equivalent(error, ex.ErrorValue());
Assert.Equivalent(error, ex.ErrorValue);
}

[Theory]
Expand All @@ -127,54 +127,8 @@ public void TestResultMapWithClosureDependencyIfResultIsSuccess(int value, int n
{
var result = Result.Ok(value);

var subject = result.Map((_, newVal) => newVal, newValue);
var subject = result.Map((_) => newValue);
Assert.NotNull(subject);
Assert.Equal(newValue, subject.Value());
}

// spierdlaak

[Theory]
[AutoData]
public void TestResultBindIfResultIsError(Exception error)
{
var result = Result.Failure<int>(error);

var ex = result.Bind((x) => Result.Ok(x));
Assert.False(ex.IsSuccess);
Assert.Equivalent(error, ex.ErrorValue());
}

[Theory]
[AutoData]
public void TestResultBindIfResultIsSuccess(int value, int newValue)
{
var result = Result.Ok(value);

var subject = result.Bind(_ => Result.Ok(newValue));
Assert.NotNull(subject);
Assert.Equal(newValue, subject.Value());
}

[Theory]
[AutoData]
public void TestResultBindWithClosureDependencyIfResultIsError(Exception error, int newValue)
{
var result = Result.Failure<int>(error);

var ex = result.Map((x, val) => Result.Ok(x + val), newValue);
Assert.False(ex.IsSuccess);
Assert.Equivalent(error, ex.ErrorValue());
}

[Theory]
[AutoData]
public void TestResultBindWithClosureDependencyIfResultIsSuccess(int value, int newValue)
{
var result = Result.Ok(value);

var subject = result.Bind((_, newVal) => Result.Ok(newVal), newValue);
Assert.NotNull(subject);
Assert.Equal(newValue, subject.Value());
Assert.Equal(newValue, subject.Value);
}
}

0 comments on commit 24d4912

Please sign in to comment.