diff --git a/src/FluentAssertions.CSharpFunctionalExtensions.Spec/UnitResultEAssertionsSpec.cs b/src/FluentAssertions.CSharpFunctionalExtensions.Spec/UnitResultEAssertionsSpec.cs new file mode 100644 index 0000000..5a1064b --- /dev/null +++ b/src/FluentAssertions.CSharpFunctionalExtensions.Spec/UnitResultEAssertionsSpec.cs @@ -0,0 +1,73 @@ +using CSharpFunctionalExtensions; +using System; +using System.Text; +using Xunit; +using Xunit.Sdk; + +namespace FluentAssertions.CSharpFunctionalExtensions.Spec +{ + public class UnitUnitResultEAssertionsSpec + { + private readonly DummyError _dummyError = new DummyError + { + TheError = "This is the error" + }; + + [Fact] + public void When_result_is_expected_to_be_success_and_it_is_should_not_throw() + { + // Arrange + var subject = UnitResult.Success(); + + // Act / Assert + subject.Should().BeSuccess(); + } + + [Fact] + public void When_result_is_expected_to_be_success_and_it_is_not_should_throw() + { + // Arrange + var subject = UnitResult.Failure(_dummyError); + + // Act + Action act = () => subject.Should().BeSuccess("Success means {0}", "it works"); + + // Assert + var expectedErrorMessageBuilder = new StringBuilder(); + expectedErrorMessageBuilder.AppendLine( + "Expected subject to be Success because Success means it works, but it is failure with error "); + expectedErrorMessageBuilder.AppendLine(); + expectedErrorMessageBuilder.AppendLine(typeof(DummyError).FullName); + expectedErrorMessageBuilder.AppendLine("{"); + expectedErrorMessageBuilder.AppendLine(" TheError = \"This is the error\""); + expectedErrorMessageBuilder.Append("}"); + + var expectedErrorMessage = expectedErrorMessageBuilder.ToString(); + act.Should().Throw().WithMessage(expectedErrorMessage); + } + + [Fact] + public void When_result_is_expected_to_be_failure_and_it_is_should_not_throw() + { + // Arrange + var subject = UnitResult.Failure(_dummyError); + + // Act / Assert + subject.Should().BeFailure(); + } + + [Fact] + public void When_result_is_expected_to_be_failure_and_it_is_not_should_throw() + { + // Arrange + var subject = UnitResult.Success(); + + // Act + Action act = () => subject.Should().BeFailure("this operation {0}", "should not work"); + + // Assert + act.Should().Throw().WithMessage( + $"Expected subject to be Failure because this operation should not work, but it is Success"); + } + } +} diff --git a/src/FluentAssertions.CSharpFunctionalExtensions/Extensions.cs b/src/FluentAssertions.CSharpFunctionalExtensions/Extensions.cs index 1faacb4..bcb3ef6 100644 --- a/src/FluentAssertions.CSharpFunctionalExtensions/Extensions.cs +++ b/src/FluentAssertions.CSharpFunctionalExtensions/Extensions.cs @@ -18,5 +18,10 @@ public static ResultTEAssertions Should(this Result subject) { return new ResultTEAssertions(subject); } + + public static UnitResultEAssertions Should(this UnitResult subject) + { + return new UnitResultEAssertions(subject); + } } } diff --git a/src/FluentAssertions.CSharpFunctionalExtensions/FluentAssertions.CSharpFunctionalExtensions.csproj b/src/FluentAssertions.CSharpFunctionalExtensions/FluentAssertions.CSharpFunctionalExtensions.csproj index 8903fb4..789555c 100644 --- a/src/FluentAssertions.CSharpFunctionalExtensions/FluentAssertions.CSharpFunctionalExtensions.csproj +++ b/src/FluentAssertions.CSharpFunctionalExtensions/FluentAssertions.CSharpFunctionalExtensions.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/FluentAssertions.CSharpFunctionalExtensions/UnitResultEAssertions.cs b/src/FluentAssertions.CSharpFunctionalExtensions/UnitResultEAssertions.cs new file mode 100644 index 0000000..3af9194 --- /dev/null +++ b/src/FluentAssertions.CSharpFunctionalExtensions/UnitResultEAssertions.cs @@ -0,0 +1,18 @@ +using CSharpFunctionalExtensions; +using FluentAssertions.Execution; +using System; + +namespace FluentAssertions.CSharpFunctionalExtensions +{ + public class UnitResultEAssertions : BaseResultAssertions> + { + public UnitResultEAssertions(UnitResult subject) : base(subject) + { + } + + protected override Func GetErrorMessageFromSubject() + { + return () => new FailReason($"Expected {{context:result}} to be Success{{reason}}, but it is failure with error {{0}}", Subject.Error); + } + } +}