Skip to content

Commit

Permalink
feat: Added new factory Result.From(List<IError> errors)
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonBN committed Mar 9, 2023
1 parent 31074dc commit 10140de
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/ResultUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@ public sealed partial record Result : IResult
/// </summary>
public static Result From(IError error) => new(error);

/// <summary>
/// Creates an <see cref="Result"/> from an <see cref="List{IError}"/>
/// </summary>
public static Result From(List<IError> errors) => new(errors);

/// <summary>
/// Creates an <see cref="Result{TValue}"/> from an <see cref="IError"/>
/// </summary>
public static Result<TValue> From<TValue>(IError error) => Result<TValue>.From(error);

/// <summary>
/// Creates an <see cref="Result{TValue}"/> from an <see cref="List{IError}"/>
/// </summary>
public static Result<TValue> From<TValue>(List<IError> errors) => Result<TValue>.From(errors);

/// <summary>
/// Creates an <see cref="Result{TValue}"/> if <see cref="IEnumerable{IError}"/> is empty or null. If not create an error result
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/ValueResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,10 @@ public new Type GetType()
/// Creates an <see cref="Result{TValue}"/> from an <see cref="IError"/>
/// </summary>
public static Result<TValue> From(IError error) => new(error);

/// <summary>
/// Creates an <see cref="Result{TValue}"/> from an <see cref="List{IError}"/>
/// </summary>
public static Result<TValue> From(List<IError> errors) => new(errors);
}
}
42 changes: 41 additions & 1 deletion tests/PowerUtils.Results.Tests/ValueResults/MethodFromTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using FluentAssertions;
using System.Collections.Generic;
using FluentAssertions;
using FluentAssertions.Execution;
using PowerUtils.Results.Tests.Fakes;
using Xunit;

Expand Down Expand Up @@ -50,5 +52,43 @@ public void CustomError_ResultFromWithType_ResultModel()
code,
description);
}

[Fact]
public void ErrorList_ResultFrom_ResultModelWithError()
{
// Arrange
var property1 = "fakeProperty1";
var code1 = "fakeCode1";
var description1 = "fakeDescription1";

var property2 = "fakeProperty2";
var code2 = "fakeCode2";
var description2 = "fakeDescription2";

var errors = new List<IError>
{
new CustomError(property1, code1, description1),
new ForbiddenError(property2, code2, description2)
};


// Act
var act = Result.From<FakeModel>(errors);


// Assert
using(new AssertionScope())
{
act.Should().ContainsError<CustomError>(
property1,
code1,
description1);

act.Should().ContainsError<ForbiddenError>(
property2,
code2,
description2);
}
}
}
}
44 changes: 42 additions & 2 deletions tests/PowerUtils.Results.Tests/VoidResults/MethodFromTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using FluentAssertions;
using System.Collections.Generic;
using FluentAssertions;
using FluentAssertions.Execution;
using PowerUtils.Results.Tests.Fakes;
using Xunit;

Expand All @@ -7,7 +9,7 @@ namespace PowerUtils.Results.Tests.VoidResults
public class MethodFromTests
{
[Fact]
public void CustomError_ResultFrom_Result()
public void CustomError_ResultFrom_ResultWithError()
{
// Arrange
var property = "fakeCustomProperty";
Expand All @@ -27,5 +29,43 @@ public void CustomError_ResultFrom_Result()
code,
description);
}

[Fact]
public void ErrorList_ResultFrom_ResultWithError()
{
// Arrange
var property1 = "fakeProperty1";
var code1 = "fakeCode1";
var description1 = "fakeDescription1";

var property2 = "fakeProperty2";
var code2 = "fakeCode2";
var description2 = "fakeDescription2";

var errors = new List<IError>
{
new CustomError(property1, code1, description1),
new ForbiddenError(property2, code2, description2)
};


// Act
var act = Result.From(errors);


// Assert
using(new AssertionScope())
{
act.Should().ContainsError<CustomError>(
property1,
code1,
description1);

act.Should().ContainsError<ForbiddenError>(
property2,
code2,
description2);
}
}
}
}

0 comments on commit 10140de

Please sign in to comment.