-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>Ardalis.Result.FluentAssertions</PackageId> | ||
<Title>Ardalis.Result.FluentAssertions</Title> | ||
<Description>A simple package to implement FluentAssertions with the Ardalis.Result package.</Description> | ||
<Summary>A simple package to implement FluentAssertions with the Ardalis.Result package.</Summary> | ||
<PackageTags>result;pattern;web;api;aspnetcore;mvc;FluentAssertions;Validation</PackageTags> | ||
<PackageReleaseNotes> | ||
</PackageReleaseNotes> | ||
<AssemblyName>Ardalis.Result.FluentAssertions</AssemblyName> | ||
<Version>1.0.0</Version> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<None Include="icon.png" Pack="true" Visible="false" PackagePath="" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Ardalis.Result\Ardalis.Result.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using FluentAssertions.Primitives; | ||
|
||
namespace Ardalis.Result.FluentAssertions; | ||
|
||
public static class FluentAssertionsResultExtensions | ||
{ | ||
public static AndConstraint<ObjectAssertions> ShouldBeFailure(this Result result) | ||
{ | ||
result.IsSuccess.Should().BeFalse(); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeConflict(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.Conflict); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeConflict(this Result result, params string[] errorMessages) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.Conflict(errorMessages)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeCriticalError(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.CriticalError); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeCriticalError(this Result result, params string[] errorMessages) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.CriticalError(errorMessages)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeError(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.Error); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeError(this Result result, string errorMessage) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.Error(errorMessage)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeError(this Result result, ErrorList errorList) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.Error(errorList)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeError(this Result result, IEnumerable<string> errorMessages, string? correlationId) | ||
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
Check warning on line 58 in src/Ardalis.Result.FluentAssertions/FluentAssertionsResultExtensions.cs GitHub Actions / build
|
||
{ | ||
return result.ShouldBeEquivalentTo(Result.Error(new ErrorList(errorMessages, correlationId))); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeForbidden(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.Forbidden); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeForbidden(this Result result, params string[] errorMessages) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.Forbidden(errorMessages)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeInvalid(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.Invalid); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeInvalid(this Result result, params ValidationError[] validationErrors) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.Invalid(validationErrors)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeNotFound(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.NotFound); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeNotFound(this Result result, params string[] errorMessages) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.NotFound(errorMessages)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeUnauthorized(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.Unauthorized); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeUnauthorized(this Result result, params string[] errorMessages) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.Unauthorized(errorMessages)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeUnavailable(this Result result) | ||
{ | ||
result.Status.Should().Be(ResultStatus.Unavailable); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeUnavailable(this Result result, params string[] errorMessages) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.Unavailable(errorMessages)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeFailure(this Result result, params string[] errorMessages) | ||
{ | ||
var andConstraint = result.ShouldBeFailure(); | ||
|
||
result.Errors.Should().BeEquivalentTo(errorMessages); | ||
|
||
return andConstraint; | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldHaveValidationErrorWithCode(this Result result, string errorCode) | ||
{ | ||
var andConstraint = result.ShouldBeInvalid(); | ||
|
||
result.ValidationErrors.Count().Should().BePositive(); | ||
|
||
result.ValidationErrors.Should().Satisfy(validationError => validationError.ErrorCode == errorCode); | ||
|
||
return andConstraint; | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldHaveValidationErrorWithIdentifier(this Result result, string identifier) | ||
{ | ||
var andConstraint = result.ShouldBeInvalid(); | ||
|
||
result.ValidationErrors.Count().Should().BePositive(); | ||
|
||
result.ValidationErrors.Should().Satisfy(validationError => validationError.Identifier == identifier); | ||
|
||
return andConstraint; | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldHaveValidationErrorWithMessage(this Result result, string errorMessage) | ||
{ | ||
var andConstraint = result.ShouldBeInvalid(); | ||
|
||
result.ValidationErrors.Count().Should().BePositive(); | ||
|
||
result.ValidationErrors.Should().Satisfy(validationError => validationError.ErrorMessage == errorMessage); | ||
|
||
return andConstraint; | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldHaveValidationErrorWithSeverity(this Result result, ValidationSeverity severity) | ||
{ | ||
var andConstraint = result.ShouldBeInvalid(); | ||
|
||
result.ValidationErrors.Count().Should().BePositive(); | ||
|
||
result.ValidationErrors.Should().Satisfy(validationError => validationError.Severity == severity); | ||
|
||
return andConstraint; | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeSuccess<TResult>(this Result<TResult> result) | ||
{ | ||
result.IsSuccess.Should().BeTrue(); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeSuccess(this Result result) | ||
{ | ||
result.IsSuccess.Should().BeTrue(); | ||
|
||
return new AndConstraint<ObjectAssertions>(result.Should()); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeCreatedWithLocation<TResult>(this Result<TResult> result, string location) | ||
{ | ||
return result.Should().BeEquivalentTo(Result.Created(result.Value, location)); | ||
} | ||
|
||
public static AndConstraint<ObjectAssertions> ShouldBeSuccessWithMessage(this Result result, string successMessage) | ||
{ | ||
return result.ShouldBeEquivalentTo(Result.SuccessWithMessage(successMessage)); | ||
} | ||
|
||
private static AndConstraint<ObjectAssertions> ShouldBeEquivalentTo(this Result result, Result assertingResult) | ||
{ | ||
return result.Should().BeEquivalentTo(assertingResult); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" /> | ||
<PackageReference Include="Moq" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Ardalis.Result.FluentAssertions\Ardalis.Result.FluentAssertions.csproj" /> | ||
<ProjectReference Include="..\..\src\Ardalis.Result\Ardalis.Result.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Xunit; | ||
using static Ardalis.Result.Result; | ||
using static Ardalis.Result.FluentAssertions.UnitTests.Utils.Constants; | ||
|
||
namespace Ardalis.Result.FluentAssertions.UnitTests.FailureResults; | ||
|
||
public class ConflictResult | ||
{ | ||
[Fact] | ||
public void ShouldBeFailure() | ||
{ | ||
Conflict().ShouldBeFailure(); | ||
} | ||
|
||
[Fact] | ||
public void WithErrorMessages_ShouldBeFailureWithErrorMessages() | ||
{ | ||
Conflict(ErrorMessage).ShouldBeFailure(ErrorMessage); | ||
} | ||
|
||
[Fact] | ||
public void ShouldBeConflict() | ||
{ | ||
Conflict().ShouldBeConflict(); | ||
} | ||
|
||
[Fact] | ||
public void WithErrorMessages_ShouldBeConflict() | ||
{ | ||
Conflict(ErrorMessage).ShouldBeConflict(); | ||
} | ||
|
||
[Fact] | ||
public void WithErrorMessages_ShouldBeConflictWithErrorMessages() | ||
{ | ||
Conflict(ErrorMessage).ShouldBeConflict(ErrorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Xunit; | ||
using static Ardalis.Result.Result; | ||
using static Ardalis.Result.FluentAssertions.UnitTests.Utils.Constants; | ||
|
||
namespace Ardalis.Result.FluentAssertions.UnitTests.FailureResults; | ||
|
||
public class CriticalErrorResult | ||
{ | ||
[Fact] | ||
public void ShouldBeFailure() | ||
{ | ||
CriticalError().ShouldBeFailure(); | ||
} | ||
|
||
[Fact] | ||
public void WithErrorMessages_ShouldBeFailureWithErrorMessages() | ||
{ | ||
CriticalError(ErrorMessage).ShouldBeFailure(ErrorMessage); | ||
} | ||
|
||
[Fact] | ||
public void ShouldBeCriticalError() | ||
{ | ||
CriticalError().ShouldBeCriticalError(); | ||
} | ||
|
||
[Fact] | ||
public void WithErrorMessages_ShouldBeCriticalError() | ||
{ | ||
CriticalError(ErrorMessage).ShouldBeCriticalError(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldBeCriticalErrorWithErrorMessages() | ||
{ | ||
CriticalError(ErrorMessage).ShouldBeCriticalError(ErrorMessage); | ||
} | ||
} |