-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d70cd39
commit e8ea754
Showing
17 changed files
with
299 additions
and
8 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
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
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,201 @@ | ||
using FluentAssertions; | ||
using FluentAssertions.Equivalency; | ||
using FluentAssertions.Execution; | ||
using FluentAssertions.Primitives; | ||
|
||
#pragma warning disable CS1591 | ||
|
||
namespace Resulteles; | ||
|
||
using ResultT = Result<Success, string>; | ||
|
||
/// <inheritdoc /> | ||
public class ResultSimpleAssertions | ||
: ReferenceTypeAssertions<ResultT, ResultSimpleAssertions> | ||
{ | ||
/// <inheritdoc /> | ||
protected override string Identifier => "Result"; | ||
|
||
/// <inheritdoc /> | ||
public ResultSimpleAssertions(ResultT subject) : base(subject) { } | ||
|
||
public AndConstraint<ResultSimpleAssertions> BeOk() | ||
{ | ||
Execute.Assertion | ||
.Given(() => Subject) | ||
.ForCondition(status => status.IsOk) | ||
.FailWith( | ||
"Expected {context:Result} to be Ok, but found Error {0}.", | ||
sub => sub.Value); | ||
|
||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultSimpleAssertions> BeError() | ||
{ | ||
Execute.Assertion | ||
.Given(() => Subject) | ||
.ForCondition(status => status.IsError) | ||
.FailWith("Expected {context:Result} to be Error, but found Ok."); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultSimpleAssertions> HaveMessage(string message) | ||
{ | ||
Execute.Assertion | ||
.Given(() => Subject) | ||
.ForCondition(status => status.Value is string err && err == message) | ||
.FailWith("Expected {context:Result} to have message {0}, but found {1}.", | ||
_ => message, sub => sub.Value); | ||
|
||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultSimpleAssertions> BeErrorWithMessage(string message) => | ||
BeError().And.HaveMessage(message); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public class ResultTStringAssertions<T> | ||
: ReferenceTypeAssertions<Result<T, string>, ResultTStringAssertions<T>> | ||
{ | ||
/// <inheritdoc /> | ||
protected override string Identifier => "ResultT"; | ||
|
||
readonly ResultSimpleAssertions baseSimpleAssertion; | ||
|
||
/// <inheritdoc /> | ||
public ResultTStringAssertions(Result<T, string> subject) : base(subject) => | ||
baseSimpleAssertion = new(subject.Select(_ => new Success())); | ||
|
||
|
||
public AndConstraint<ResultTStringAssertions<T>> BeOk() | ||
{ | ||
baseSimpleAssertion.BeOk(); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTStringAssertions<T>> BeError() | ||
{ | ||
baseSimpleAssertion.BeError(); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTStringAssertions<T>> HaveMessage(string message) | ||
{ | ||
baseSimpleAssertion.HaveMessage(message); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTStringAssertions<T>> ErrorWithMessage(string message) => | ||
BeError().And.HaveMessage(message); | ||
|
||
public AndConstraint<ResultTStringAssertions<T>> Be(T other) | ||
{ | ||
Subject.Value.Should().Be(other); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTStringAssertions<T>> BeOkThen(Action<T> action) | ||
{ | ||
baseSimpleAssertion.BeOk(); | ||
action((T)Subject.Value); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTStringAssertions<T>> BeEquivalentTo<TOther>(TOther other) | ||
{ | ||
Subject.Value.Should().BeEquivalentTo(other); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTStringAssertions<T>> BeEquivalentTo<TOther>(TOther other, | ||
Func<EquivalencyAssertionOptions<TOther>, EquivalencyAssertionOptions<TOther>> options) | ||
{ | ||
Subject.Value.Should().BeEquivalentTo(other, options); | ||
return new(this); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public class ResultTypeAssertions<TOk, TError> | ||
: ReferenceTypeAssertions<Result<TOk, TError>, ResultTypeAssertions<TOk, TError>> | ||
{ | ||
/// <inheritdoc /> | ||
protected override string Identifier => "ResultType"; | ||
|
||
/// <inheritdoc /> | ||
public ResultTypeAssertions(Result<TOk, TError> subject) : base(subject) { } | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeOk() | ||
{ | ||
Execute.Assertion | ||
.Given(() => Subject) | ||
.ForCondition(status => status.IsOk) | ||
.FailWith( | ||
"Expected {context:Result} to be Ok, but found Error {0}.", | ||
sub => sub.Value); | ||
|
||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeOk(TOk okValue) | ||
{ | ||
Execute.Assertion | ||
.Given(() => Subject) | ||
.ForCondition(sub => sub.IsOk && sub.Value.Equals(okValue)) | ||
.FailWith( | ||
"Expected {context:Result} to be Ok with value {0}, but found Error {1}.", | ||
_ => okValue, sub => sub.Value | ||
); | ||
|
||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeError() | ||
{ | ||
Execute.Assertion | ||
.Given(() => Subject) | ||
.ForCondition(status => status.IsError) | ||
.FailWith("Expected {context:Result} to be Error, but found Ok."); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeError(TError errorValue) | ||
{ | ||
Execute.Assertion | ||
.Given(() => Subject) | ||
.ForCondition(sub => sub.IsError && sub.Value.Equals(errorValue)) | ||
.FailWith("Expected {context:Result} to be Error {0}, but found Ok {1}", | ||
_ => errorValue, s => s.Value | ||
); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeEquivalentTo<TOther>(TOther other) | ||
{ | ||
Subject.Value.Should().BeEquivalentTo(other); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeEquivalentTo<TOther>(TOther other, | ||
Func<EquivalencyAssertionOptions<TOther>, EquivalencyAssertionOptions<TOther>> options) | ||
{ | ||
Subject.Value.Should().BeEquivalentTo(other, options); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeOkThen(Action<TOk> action) | ||
{ | ||
BeOk(); | ||
action((TOk)Subject.Value); | ||
return new(this); | ||
} | ||
|
||
public AndConstraint<ResultTypeAssertions<TOk, TError>> BeErrorThen(Action<TError> action) | ||
{ | ||
BeError(); | ||
action((TError)Subject.Value); | ||
return new(this); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Resulteles.FluentAssertions/ResultFluentAssertionsExtensions.cs
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,22 @@ | ||
namespace Resulteles; | ||
|
||
/// <summary> | ||
/// Result assertion extensions | ||
/// </summary> | ||
public static class ResultFluentAssertionsExtensions | ||
{ | ||
/// <summary> | ||
/// Simple result assertions | ||
/// </summary> | ||
public static ResultSimpleAssertions Should(this Result<Success, string> result) => new(result); | ||
|
||
/// <summary> | ||
/// String Error result assertions | ||
/// </summary> | ||
public static ResultTStringAssertions<T> Should<T>(this Result<T, string> result) => new(result); | ||
|
||
/// <summary> | ||
/// Result assertions | ||
/// </summary> | ||
public static ResultTypeAssertions<TOk, TError> Should<TOk, TError>(this Result<TOk, TError> result) => new(result); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Resulteles.FluentAssertions/Resulteles.FluentAssertions.csproj
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,47 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<PackageId>Resulteles.FluentAssertions</PackageId> | ||
<Authors>Lucas Teles - [email protected]</Authors> | ||
<Company/> | ||
<Description>FluentAssertions extensions for Resulteles</Description> | ||
<RepositoryType>GitHub</RepositoryType> | ||
<PackageProjectUrl>https://github.com/lucasteles/Resulteles</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/lucasteles/Resulteles</RepositoryUrl> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
|
||
<RootNamespace>Resulteles</RootNamespace> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
<PackageTags>extensions, tools, helpers, utils</PackageTags> | ||
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="..\..\README.md" Pack="true" PackagePath="\"/> | ||
</ItemGroup> | ||
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'"> | ||
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.11.0"/> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Resulteles\Resulteles.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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,3 @@ | ||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.Linq; |
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
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