Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net8.0;net9.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
<PackageReference Include="xunit" Version="2.*" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentResults.Extensions.AwesomeAssertions\FluentResults.Extensions.AwesomeAssertions.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using System;
global using AwesomeAssertions;
global using Xunit;
global using Xunit.Sdk;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace FluentResults.Extensions.AwesomeAssertions.Test.ResultTests
{
public class BeFailureTests
{
[Fact]
public void A_failed_result_throw_no_exception()
{
var failedResult = Result.Fail("Error 1");

Action action = () => failedResult.Should().BeFailure();

action.Should().NotThrow();
}

[Fact]
public void A_success_result_throw_a_exception()
{
var successResult = Result.Ok();

Action action = () => successResult.Should().BeFailure();

action.Should()
.Throw<XunitException>()
.WithMessage("Expected result be failed, but is success");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ReSharper disable once CheckNamespace
namespace FluentResults.Extensions.AwesomeAssertions.Test.ResultTests
{
public class BeSuccessTests
{
[Fact]
public void A_success_result_throw_no_exception()
{
var successResult = Result.Ok();

Action action = () => successResult.Should().BeSuccess();

action.Should().NotThrow();
}

[Fact]
public void A_failed_result_with_one_error_throw_a_exception()
{
var failedResult = Result.Fail("Error 1");

Action action = () => failedResult.Should().BeSuccess();

action.Should()
.Throw<XunitException>()
.WithMessage("Expected result be success, but is failed because of '{Error with Message='Error 1'}'");
}

[Fact]
public void A_failed_result_with_multiple_errors_throw_a_exception()
{
var failedResult = Result.Fail("Error 1")
.WithError("Error 2");

Action action = () => failedResult.Should().BeSuccess();

action.Should()
.Throw<XunitException>()
.WithMessage(
"Expected result be success, but is failed because of '{Error with Message='Error 1', Error with Message='Error 2'}'");
}

[Fact]
public void A_failed_result_with_a_success_throw_a_exception()
{
var failedResult = Result.Fail("Error 1")
.WithSuccess("Success 1");

Action action = () => failedResult.Should().BeSuccess();

action.Should()
.Throw<XunitException>()
.WithMessage("Expected result be success, but is failed because of '{Error with Message='Error 1'}'");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace FluentResults.Extensions.AwesomeAssertions.Test.ResultTests
{
public class HaveErrorTests
{
[Theory]
[InlineData("Error 1")]
[InlineData("Error")]
public void A_result_with_expected_reason_throw_no_exception(string expectedError)
{
var failedResult = Result.Fail("Error 1");

Action action = () =>
failedResult.Should().BeFailure().And
.HaveError(expectedError, MessageComparisonLogics.ActualContainsExpected);

action.Should().NotThrow();
}

[Fact]
public void A_result_without_expected_reason_throw_a_exception()
{
var successResult = Result.Fail("Error 2");

Action action = () => successResult.Should().BeFailure().And.HaveError("Error 1");

action.Should()
.Throw<XunitException>()
.WithMessage(
"Expected result to contain error with message containing \"Error 1\", but found error '{Error with Message='Error 2'}'");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
namespace FluentResults.Extensions.AwesomeAssertions.Test.ResultTests
{
public class HaveReasonTests
{
[Theory]
[InlineData("Error 1")]
[InlineData("Error")]
public void A_result_with_expected_reason_throw_no_exception(string expectedError)
{
var failedResult = Result.Fail("Error 1");

Action action = () =>
failedResult.Should().BeFailure().And
.HaveReason(expectedError, MessageComparisonLogics.ActualContainsExpected);

action.Should().NotThrow();
}

[Fact]
public void A_result_without_expected_reason_throw_a_exception()
{
var successResult = Result.Fail("Error 2");

Action action = () => successResult.Should().BeFailure().And.HaveReason("Error 1");

action.Should()
.Throw<XunitException>()
.WithMessage(
"Expected result to contain reason with message containing \"Error 1\", but found reasons '{Error with Message='Error 2'}'");
}

[Theory]
[InlineData("Error")]
public void A_result_with_expected_reason_of_type_throw_no_exception_equal(string expectedError)
{
var successResult = Result.Fail(new SomeReason("Error 1"));

Action action = () => successResult.Should().BeFailure().And.HaveReason<SomeReason>(expectedError);

action.Should().Throw<XunitException>();
}

[Theory]
[InlineData("Error 1")]
[InlineData("Error")]
public void A_result_with_expected_reason_of_type_throw_no_exception(string expectedError)
{
var successResult = Result.Fail(new SomeReason("Error 1"));

Action action = () =>
successResult.Should().BeFailure().And
.HaveReason<SomeReason>(expectedError, MessageComparisonLogics.ActualContainsExpected);

action.Should().NotThrow();
}

[Fact]
public void A_result_without_expected_reason_of_type_throw_a_exception()
{
var successResult = Result.Fail("Error 1");

Action action = () => successResult.Should().BeFailure().And.HaveReason<SomeReason>("Error 1");

action.Should()
.Throw<XunitException>()
.WithMessage(
"Expected result to contain reason of type \"SomeReason\" with message containing \"Error 1\", but found reasons '{Error with Message='Error 1'}'");
}

[Fact]
public void A_result_with_expected_reason_object_throw_no_exception()
{
var successResult = Result.Fail("Error 1");

Action action = () => successResult.Should().BeFailure().And.HaveReason(new Error("Error 1"));

action.Should().NotThrow();
}

[Fact]
public void A_result_without_expected_reason_object_throw_a_exception()
{
var successResult = Result.Fail("Error 1");

Action action = () => successResult.Should().BeFailure().And.HaveReason(new Error("Error 2"));

action.Should()
.Throw<XunitException>()
.WithMessage(
"Expected Subject.Reasons {Error with Message='Error 1'} to contain equivalent of Error with Message='Error 2'*");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace FluentResults.Extensions.AwesomeAssertions.Test.ResultTests
{
public class ReasonHaveMetadataTests
{
[Fact]
public void A_result_with_reason_with_specific_metadata_throw_no_exception()
{
var failedResult = Result.Fail(new SomeReason("Error 1")
.WithMetadata("key", "value"));

Action action = () => failedResult
.Should()
.BeFailure()
.And.HaveReason("Error 1")
.That.HaveMetadata("key", "value");

action.Should().NotThrow();
}

[Theory]
[InlineData(null)]
[InlineData("value1")]
[InlineData(1)]
public void A_result_with_reason_with_another_metadata_because_of_value_throw_exception(object expectedMetadataValue)
{
var failedResult = Result.Fail(new SomeReason("Error 1")
.WithMetadata("key", "value"));

Action action = () => failedResult
.Should()
.BeFailure()
.And.HaveReason("Error 1")
.That.HaveMetadata("key", expectedMetadataValue);

action.Should()
.Throw<XunitException>()
.WithMessage($"Reason should contain 'key' with '{expectedMetadataValue}', but not contain it");
}

[Theory]
[InlineData("ke")]
[InlineData("key1")]
public void A_result_with_reason_with_another_metadata_because_of_key_throw_exception(string expectedMetadataKey)
{
var failedResult = Result.Fail(new SomeReason("Error 1")
.WithMetadata("key", "value"));

Action action = () => failedResult
.Should()
.BeFailure()
.And.HaveReason("Error 1")
.That.HaveMetadata(expectedMetadataKey, "value");

action.Should()
.Throw<XunitException>()
.WithMessage($"Reason should contain '{expectedMetadataKey}' with 'value', but not contain it");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace FluentResults.Extensions.AwesomeAssertions.Test.ResultTests
{
public class ReasonSatisfyTests
{

[Fact]
public void A_reason_with_the_expected_property_throw_no_exception()
{
var failedResult = Result.Fail(new SomeReason("Error 1")
{
Prop = "Prop1"
});

Action action = () => failedResult
.Should()
.BeFailure()
.And.HaveReason("Error 1")
.That.Satisfy<SomeReason>(r => r.Prop.Should().Be("Prop1"));

action.Should().NotThrow();
}

[Fact]
public void A_reason_with_another_property_throw_exception()
{
var failedResult = Result.Fail(new SomeReason("Error 1")
{
Prop = "Prop1"
});

Action action = () => failedResult
.Should()
.BeFailure()
.And.HaveReason("Error 1")
.That.Satisfy<SomeReason>(r => r.Prop.Should().Be("Prop2"));

action.Should()
.Throw<XunitException>();
}

[Fact]
public void A_reason_with_another_type_throw_exception()
{
var failedResult = Result.Fail(new SomeReason("Error 1")
{
Prop = "Prop1"
});

Action action = () => failedResult
.Should()
.BeFailure()
.And.HaveReason("Error 1")
.That.Satisfy<AnotherReason>(r => {});

action.Should()
.Throw<XunitException>();
}
}
}
20 changes: 20 additions & 0 deletions src/FluentResults.Extensions.AwesomeAssertions.Test/SomeReason.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace FluentResults.Extensions.AwesomeAssertions.Test
{
internal class SomeReason : Error
{
public string Prop { get; set; }

public SomeReason(string message) : base(message)
{

}
}

internal class AnotherReason : Error
{
public AnotherReason(string message) : base(message)
{

}
}
}
Loading