-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added helper extensions to determine status of the result * Added tests for IResultExtensions --------- Co-authored-by: Steve Smith <[email protected]>
- Loading branch information
1 parent
dfc2cfc
commit c57fad2
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace Ardalis.Result; | ||
|
||
public static class IResultExtensions | ||
{ | ||
/// <summary> | ||
/// Returns true if the result is successful (status is Ok). | ||
/// </summary> | ||
public static bool IsOk(this IResult result) => result.Status == ResultStatus.Ok; | ||
|
||
/// <summary> | ||
/// Returns true if the result is created (status is Created). | ||
/// </summary> | ||
public static bool IsCreated(this IResult result) => result.Status == ResultStatus.Created; | ||
|
||
/// <summary> | ||
/// Returns true if the result is an error (status is Error). | ||
/// </summary> | ||
public static bool IsError(this IResult result) => result.Status == ResultStatus.Error; | ||
|
||
/// <summary> | ||
/// Returns true if the result is forbidden (status is Forbidden). | ||
/// </summary> | ||
public static bool IsForbidden(this IResult result) => result.Status == ResultStatus.Forbidden; | ||
|
||
/// <summary> | ||
/// Returns true if the result is unauthorized (status is Unauthorized). | ||
/// </summary> | ||
public static bool IsUnauthorized(this IResult result) => result.Status == ResultStatus.Unauthorized; | ||
|
||
/// <summary> | ||
/// Returns true if the result is invalid (status is Invalid). | ||
/// </summary> | ||
public static bool IsInvalid(this IResult result) => result.Status == ResultStatus.Invalid; | ||
|
||
/// <summary> | ||
/// Returns true if the result is not found (status is NotFound). | ||
/// </summary> | ||
public static bool IsNotFound(this IResult result) => result.Status == ResultStatus.NotFound; | ||
|
||
/// <summary> | ||
/// Returns true if the result is no content (status is NoContent). | ||
/// </summary> | ||
public static bool IsNoContent(this IResult result) => result.Status == ResultStatus.NoContent; | ||
|
||
/// <summary> | ||
/// Returns true if the result is a conflict (status is Conflict). | ||
/// </summary> | ||
public static bool IsConflict(this IResult result) => result.Status == ResultStatus.Conflict; | ||
|
||
/// <summary> | ||
/// Returns true if the result is a critical error (status is CriticalError). | ||
/// </summary> | ||
public static bool IsCriticalError(this IResult result) => result.Status == ResultStatus.CriticalError; | ||
|
||
/// <summary> | ||
/// Returns true if the result is unavailable (status is Unavailable). | ||
/// </summary> | ||
public static bool IsUnavailable(this IResult result) => result.Status == ResultStatus.Unavailable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using Xunit; | ||
|
||
namespace Ardalis.Result.UnitTests; | ||
|
||
public class IResultExtensions | ||
{ | ||
[Fact] | ||
public void IsOk_ReturnsTrue_WhenStatusIsOk() | ||
{ | ||
// Arrange & Act | ||
var result = Result.Success(); | ||
|
||
// Assert | ||
Assert.True(result.IsOk()); | ||
} | ||
|
||
[Fact] | ||
public void IsCreated_ReturnsTrue_WhenStatusIsCreated() | ||
{ | ||
// Arrange & Act | ||
var foo = new Foo("Bar"); | ||
var result = Result<Foo>.Created(foo); | ||
|
||
// Assert | ||
Assert.True(result.IsCreated()); | ||
} | ||
|
||
[Fact] | ||
public void IsError_ReturnsTrue_WhenStatusIsError() | ||
{ | ||
// Arrange & Act | ||
var result = Result.Error(); | ||
|
||
// Assert | ||
Assert.True(result.IsError()); | ||
} | ||
|
||
[Fact] | ||
public void IsForbidden_ReturnsTrue_WhenStatusIsForbidden() | ||
{ | ||
// Arrange & Act | ||
var result = Result.Forbidden(); | ||
|
||
// Assert | ||
Assert.True(result.IsForbidden()); | ||
} | ||
|
||
[Fact] | ||
public void IsUnauthorized_ReturnsTrue_WhenStatusIsUnauthorized() | ||
{ | ||
// Arrange & Act | ||
var result = Result.Unauthorized(); | ||
|
||
// Assert | ||
Assert.True(result.IsUnauthorized()); | ||
} | ||
|
||
[Fact] | ||
public void IsInvalid_ReturnsTrue_WhenStatusIsInvalid() | ||
{ | ||
// Arrange & Act | ||
var result = Result.Invalid(); | ||
|
||
// Assert | ||
Assert.True(result.IsInvalid()); | ||
} | ||
|
||
[Fact] | ||
public void IsNotFound_ReturnsTrue_WhenStatusIsNotFound() | ||
{ | ||
// Arrange & Act | ||
var result = Result.NotFound(); | ||
|
||
// Assert | ||
Assert.True(result.IsNotFound()); | ||
} | ||
|
||
[Fact] | ||
public void IsNoContent_ReturnsTrue_WhenStatusIsNoContent() | ||
{ | ||
// Arrange & ActI | ||
var result = Result.NoContent(); | ||
|
||
// Assert | ||
Assert.True(result.IsNoContent()); | ||
} | ||
|
||
[Fact] | ||
public void IsConflict_ReturnsTrue_WhenStatusIsConflict() | ||
{ | ||
// Arrange & Act | ||
var result = Result.Conflict(); | ||
|
||
// Assert | ||
Assert.True(result.IsConflict()); | ||
} | ||
|
||
[Fact] | ||
public void IsCriticalError_ReturnsTrue_WhenStatusIsCriticalError() | ||
{ | ||
// Arrange & Act | ||
var result = Result.CriticalError(); | ||
|
||
// Assert | ||
Assert.True(result.IsCriticalError()); | ||
} | ||
|
||
[Fact] | ||
public void IsUnavailable_ReturnsTrue_WhenStatusIsUnavailable() | ||
{ | ||
// Arrange & Act | ||
var result = Result.Unavailable(); | ||
|
||
// Assert | ||
Assert.True(result.IsUnavailable()); | ||
} | ||
|
||
private record Foo(string Value); | ||
|
||
} |